package cn.com.ty.lift.ud.appmenu.controller;

import java.util.ArrayList;
import java.util.List;

import cn.com.ty.lift.ud.operation.mapper.entity.VipFeeSettingEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.appmenu.controller.query.AppuserMenuQuery;
import cn.com.ty.lift.ud.appmenu.mapper.entity.AppMenuRequest;
import cn.com.ty.lift.ud.appmenu.mapper.entity.Appmenu;
import cn.com.ty.lift.ud.appmenu.mapper.entity.AppuserMenu;
import cn.com.ty.lift.ud.appmenu.mapper.entity.MemberMeunResp;
import cn.com.ty.lift.ud.appmenu.service.IAppuserMenuService;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.operation.mapper.entity.UserLevelRuleEntity;
import cn.com.ty.lift.ud.operation.service.IUserLevelRuleService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * app角色权限API
 * 
 * @author Xson
 * @date 2020-06-04
 */
@Api(tags = "app角色权限API")
@RestController
@RequestMapping("/api/appuserMenus")
public class AppuserMenuController extends BaseController<AppuserMenu, AppuserMenuQuery> {

    private final Logger log = LoggerFactory.getLogger(AppuserMenuController.class);

    private @Autowired IAppuserMenuService appuserMenuService;
    
    private @Autowired IUserLevelRuleService userLevelRuleService;

     @Autowired
     private IUserInfoService iUserInfoService;

    @Override
    protected IService<AppuserMenu> getService() {
        return appuserMenuService;
    }

    // 根据条件查询
    @GetMapping("/getAllMenu")
    @ApiOperation(value = "根据app用户角色查权限")
    public RestResponse getAllMenuByLevelId(Long levelId) {
        List<Appmenu> allMenuByLevelId = appuserMenuService.getAllMenuByLevelId(levelId);
        return RestResponse.success(allMenuByLevelId, "Queried successfully");
    }

    @ApiOperation(value = "修改权限")
    @PostMapping("updateUserMenu")
    public RestResponse updateUserMenu(@RequestBody AppMenuRequest request) {
        appuserMenuService.updateUserMenu(request);
        return RestResponse.success(null, "修改成功");
    }
    
    @GetMapping("/getMemberAllMenu")
    @ApiOperation(value = "会员权限")
    public RestResponse getMemberAllMenu() {
        List<UserLevelRuleEntity> list = userLevelRuleService.getVipUserLevel();
        List<MemberMeunResp> resultList = new ArrayList<MemberMeunResp>();
        for(UserLevelRuleEntity ure : list) {
            MemberMeunResp resp= new MemberMeunResp();
            List<Appmenu> allMenuByLevelId = appuserMenuService.getMenuByLevelIdLimit5(ure.getId());
            resp.setId(ure.getId());
            resp.setName(ure.getName());
            resp.setImage(ure.getImage());
            resp.setMenuList(allMenuByLevelId);
            resultList.add(resp);
        }
        return RestResponse.success(resultList, "Queried successfully");
    }
}