package com.huimv.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.entity.BasePigpen;
import com.huimv.admin.entity.EnergyEnvDevice;
import com.huimv.admin.entity.Menu;
import com.huimv.admin.service.IAuthorizeService;
import com.huimv.admin.service.IBasePigpenService;
import com.huimv.admin.service.IEnergyEnvDeviceService;
import com.huimv.admin.service.IMenuService;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/admin/auth")
@CrossOrigin
public class AuthorizeController {

    @Autowired
    IAuthorizeService iAuthorizeService;
    @Autowired
    IMenuService iMenuService;

    @Autowired
    IBasePigpenService basePigpenService;

    @Autowired
    IEnergyEnvDeviceService energyEnvDeviceService;

    @GetMapping("/test")
    public Result test(@RequestBody Map<String, String> map){
//        List<EnergyEnvDevice> farm_id = energyEnvDeviceService.list(new QueryWrapper<EnergyEnvDevice>().eq("farm_id", 21));

        List<BasePigpen> build_name = basePigpenService.list(new QueryWrapper<BasePigpen>().eq("farm_id", 21).like("build_name","一单元"));
        for (BasePigpen basePigpen : build_name) {
            System.out.println(basePigpen.getBuildName()+"------>"+basePigpen.getId());
        }
//        for (EnergyEnvDevice energyEnvDevice : farm_id) {
//            String deviceName = energyEnvDevice.getDeviceName();
//            String[] s = deviceName.split(" ");
//            String s1 = s[0];
//            if ("1楼".equals(s[s.length-1])){
//                s1 += "一层";
//            }
//            if ("2楼".equals(s[s.length-1])){
//                s1 += "二层";
//            }
//            if ("3楼".equals(s[s.length-1])){
//                s1 += "三层";
//            }
//            if ("4楼".equals(s[s.length-1])){
//                s1 += "四层";
//            }
//            if ("5楼".equals(s[s.length-1])){
//                s1 += "五层";
//            }
//            if ("6楼".equals(s[s.length-1])){
//                s1 += "六层";
//            }
//
//            BasePigpen build_name = basePigpenService.getOne(new QueryWrapper<BasePigpen>().like("build_name", s1).like("build_name","一单元"));
//            if (ObjectUtil.isNotEmpty(build_name)){
//                energyEnvDevice.setUnitId(build_name.getId());
//                energyEnvDevice.setUnitName(build_name.getBuildName());
//                energyEnvDeviceService.updateById(energyEnvDevice);
//            }
//
//        }
        return null;
    }

    public static void main(String[] args) {
        String str = "育肥4区BD  1楼";
        str =  str.replaceAll("楼","层").replaceAll(" ","");
        System.out.println(str);
    }


    @PostMapping("/saveAccountGroup")
    public Result saveAccountGroup(@RequestBody Map<String, String> map){
        /*
         * 保存账号关联权限组数据
         */
        String accountId = map.get("accountId");
        String groupIds = map.get("groupIds");
        return iAuthorizeService.saveAccountGroup(accountId,groupIds);
    }

    @PostMapping("/saveGroupMenu")
    public Result saveGroupMenu(@RequestBody Map<String, String> map){
        /*
         * 保存权限组关联菜单数据
         */
        String groupId = map.get("groupId");
        String menuIds = map.get("menuIds");
        String isAll = map.get("isAll");
        return iAuthorizeService.saveGroupMenu(groupId,menuIds,isAll);
    }

    @RequestMapping("/getGroupByAccount")
    public Result getGroupByAccount(@RequestParam(name = "accountId", required = true) String accountId){
        /*
         * 根据账号获取权限组
         */
        return iAuthorizeService.getGroupByAccount (accountId);
    }

    @PostMapping("/getMenuByGroup")
    public Result getMenuByGroup(@RequestBody Map<String, String> map){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getMenuByGroup(map);
    }

    @PostMapping("/getMenuByGroupAndFarm")
    public Result getMenuByGroupAndFarm(@RequestBody Map<String, String> map){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getMenuByGroupAndFarm(map);
    }

    @PostMapping("/getMyMenu")
    public Result getMyMenuAndButton(@RequestBody  Map<String,String> map){
        /*
         * 获取用户的菜单数据
         */
        String userId = map.get("userId");
        String farmId = map.get("farmId");
        List<Menu> myMenu = iAuthorizeService.getMyMenuAll(userId,farmId);

        List<Map> list = iMenuService.optimizeListUtil(0,myMenu);
        return new Result(ResultCode.SUCCESS,list);
    }
//    //获取菜单-多级
//    @RequestMapping("/getMyMenuMultilevel")
//    public Result getMyMenuMultilevel(@RequestParam(name = "userId", required = true) String userId){
//        return new Result(ResultCode.SUCCESS,iAuthorizeService.getMyMenuMultilevel(userId));
//    }


    @PostMapping("/getMyButton")
    public Result getMyButton(@RequestBody Map<String,Integer> map){
        /*
         * 获取用户的菜单数据
         */
        Integer userId = map.get("userId");
        Integer farmId = map.get("farmId");
        List list = iAuthorizeService.getMyButton(userId,farmId);
        return new Result(ResultCode.SUCCESS,list);
    }


    @GetMapping("/getAcountByGroup")
    public Result getAccountByGroup(String groupId){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getAccountByGroup(groupId);
    }
}
