package com.dxhy.config.controller;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.dxhy.api.model.bigB.BigBMenuInfoEntity;
import com.dxhy.api.model.config.*;
import com.dxhy.auth.service.UserCenterService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.ConfigureConstant;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.JsonUtils;
import com.dxhy.common.utils.R;
import com.dxhy.jxpt.baseService.config.service.SysUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 用户信息管理
 *
 * @author Allen
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/sys/user")
@Slf4j
public class SysUserInfoController extends AbstractController {

    @Autowired
    private UserCenterService userCenterService;
    @Autowired
    private SysUserInfoService sysUserInfoService;

    /**
     * 获取用户信息
     */
    @RequestMapping("/userInfo")
//    @SysLog("获取用户信息")
    public ResponseEntity<?> userInfo() {
        log.info("获取用户信息。。。");
        UserInfo userInfo = getUserInfo();
        List<Tax> org = userInfo.getOrg();
        List<Business> business = userInfo.getBusiness();
        userInfo.setUserBusiness(business);
        userInfo.setUserOrg(org);
        userInfo.setOrg(null);
        userInfo.setBusiness(null);
        return ResponseEntity.ok(R.ok().put("data", Base64.encode(JsonUtils.getInstance().toJsonString(userInfo))));
    }

    /**
     * 获取用户菜单
     */
    @RequestMapping("/getMenu")
//    @SysLog("获取用户菜单")
    public ResponseEntity<?> getMenu() {
        log.info("获取用户菜单信息。。。");
        String menuInfo = userCenterService.getUserMenus(getToken(), "JXXT");
        List<BigBMenuInfoEntity> bigBMenuList = JsonUtils.getInstance().jsonToList(menuInfo, BigBMenuInfoEntity.class);
        List<SysMenuEntity> sysMenuList = new ArrayList<>();
        bigBMenuList.forEach(e->{
            SysMenuEntity sme = new SysMenuEntity();
            BeanUtils.copyProperties(e,sme);
            if (e.getUrl().contains(ConfigureConstant.STRING_JH)) {
                String[] urlSplits = e.getUrl().split(ConfigureConstant.STRING_JH);
                if (urlSplits.length > 1) {
                    sme.setUrl(urlSplits[1]);
                } else {
                    sme.setUrl(null);
                }
            }
            sme.setType(Integer.valueOf(e.getType()));
            sme.setOrderNum(Integer.valueOf(e.getOrderNum()));
            sme.setComponent(e.getRemark());
            sme.setSystemCode(e.getSystemSign());
            sme.setNoCache(false);
            sme.setBtnRuleFlag(false);
            sme.setQueryRuleFlag(false);
            sysMenuList.add(sme);
        });
        sysMenuList.sort(Comparator.comparing(SysMenuEntity::getOrderNum));
        MenuModel menuModel = sysUserInfoService.getMenuInfo(sysMenuList);
        return ResponseEntity.ok(R.ok().put("data", Base64.encode(JSON.toJSONString(menuModel))));
    }

    /**
     * 获取字典缓存
     *
     */
    @RequestMapping(value = "/getDict", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> getDict() {
        log.info("获取字典信息");
        return ResponseEntity.ok(R.ok().put("data", sysUserInfoService.getDict()));
    }

    /**
     * 获取用户菜单树结构
     */
    @RequestMapping("/getMenuList")
//    @SysLog("获取用户菜单树结构")
    public ResponseEntity<?> getMenuList() {
        log.info("获取用户菜单信息。。。");
        String menuInfo = userCenterService.getUserMenus(getToken(), "JXXT");
        List<BigBMenuInfoEntity> bigBMenuList = JsonUtils.getInstance().jsonToList(menuInfo, BigBMenuInfoEntity.class);
        List<SysMenuEntity> sysMenuList = new ArrayList<>();
        bigBMenuList.forEach(e->{
            SysMenuEntity sme = new SysMenuEntity();
            BeanUtils.copyProperties(e,sme);
            sme.setType(Integer.valueOf(e.getType()));
            sme.setOrderNum(Integer.valueOf(e.getOrderNum()));
            sme.setComponent(e.getRemark());
            sme.setSystemCode(e.getSystemSign());
            sme.setNoCache(false);
            sme.setBtnRuleFlag(false);
            sme.setQueryRuleFlag(false);
            sysMenuList.add(sme);
        });
        sysMenuList.sort(Comparator.comparing(SysMenuEntity::getOrderNum));
        List<SysMenuEntity> menuList = sysUserInfoService.getUserMenuList(sysMenuList);
        return ResponseEntity.ok(R.ok().put("data", menuList));
    }

}
