package com.gz.service.system.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.common.DataGlobalVariable;
import com.gz.core.exception.BusinessException;
import com.gz.core.exception.CustomExceptionEnum;
import com.gz.dto.system.DictDTO;
import com.gz.dto.system.MenuRoleDTO;
import com.gz.dto.system.gdndDTO;
import com.gz.mapper.system.DictMapper;
import com.gz.mapper.system.MenuRoleMapper;
import com.gz.service.system.DictService;
import com.gz.utils.JwtUtils;
import com.gz.vo.system.DictVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiuchangLan
 * @date 2020/9/7 10:17
 */
@Service
public class DictServiceImpl implements DictService {
    @Resource
    private DictMapper dictMapper;

    @Resource
    private MenuRoleMapper menuRoleMapper;

    @Override
    public Integer insert(DictDTO dictDTO) throws BusinessException {
        DictDTO codeIsExist = new DictDTO();
        codeIsExist.setDictCode(dictDTO.getDictCode());
        List<DictDTO> select = dictMapper.select(codeIsExist);
        if (select != null && select.size() > 0) {
            throw new BusinessException(CustomExceptionEnum.DICT_CODE_EXISTS);
        }
        // 创建人
        dictDTO.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        // 执行入库
        return dictMapper.insertSelective(dictDTO);
    }

    @Override
    public Integer delete(Integer id) {
        return dictMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(DictDTO dictDTO) throws BusinessException {
        DictDTO codeIsExist = new DictDTO();
        codeIsExist.setDictCode(dictDTO.getDictCode());
        List<DictDTO> select = dictMapper.select(codeIsExist);
        Integer count = select.size();
        // 除了自己原来的code
        for (DictDTO dto : select) {
            if (dictDTO.getId().equals(dto.getId())) {
                count--;
            }
        }
        // code已经重复
        if (select != null && count > 0) {
            throw new BusinessException(CustomExceptionEnum.DICT_CODE_EXISTS);
        }
        // 修改人
        dictDTO.setUpdated(JwtUtils.getCurrentUserJwtPayload().getId());
        // 修改时间
        dictDTO.setUpdateTime(DateUtil.now());
        // 执行入库
        return dictMapper.updateByPrimaryKeySelective(dictDTO);
    }

    @Override
    public PageInfo<DictDTO> selectByPage(DictVO dictVO) {
        if (dictVO.getPageNum() != null && dictVO.getPageSize() != null) {
            PageHelper.startPage(dictVO.getPageNum(), dictVO.getPageSize());
        }
        // 查询条件转换
        DictDTO dictDTO = BeanUtil.copyProperties(dictVO, DictDTO.class);
        // 查询未删除的数据
        return new PageInfo<>(dictMapper.select(dictDTO));
    }

    @Override
    public DictDTO selectByPrimaryKey(Integer id) {
        DictDTO adminDTO = new DictDTO();
        adminDTO.setId(id);
        return dictMapper.selectOne(adminDTO);
    }

    @Override
    public List<DictDTO> select() {
        // 查询条件转换
        DictDTO dictDTO = new DictDTO();
        // 查询未删除的数据
        return dictMapper.select(dictDTO);
    }

    @Override
    public List<Tree<String>> getDictTree() {
        List<DictDTO> select = select();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("id");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(select, "-1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setName(treeNode.getDictName());
                });
        return treeNodes;
    }

    @Override
    public List<DictDTO> selectDictByCode(String code) {
        return dictMapper.selectDictByCode(code);
    }

    /**
     * @description 根据中文名查询字典
     * @author LiuChangLan
     * @since 2020/9/18 10:20
     */
    @Override
    public String getDictCode(String chineseName) {
        if (chineseName != null && !"".equals(chineseName)) {
            DictDTO dictDTO = new DictDTO();
            dictDTO.setDictName(chineseName);
            return dictMapper.selectOne(dictDTO).getDictCode();
        }
        return "";
    }

    @Override
    public String getDictName(String code) {
        DictDTO dictDTO = new DictDTO();
        dictDTO.setDictCode(code);
        DictDTO dictDTO1 = dictMapper.selectOne(dictDTO);
        return dictDTO1.getDictName();
    }

    @Override
    public DictDTO selectByCode(String code) {
        DictDTO dictDTO = new DictDTO();
        dictDTO.setDictCode(code);
        return dictMapper.selectOne(dictDTO);
    }

    public List<gdndDTO> selectGdnd(){
        return this.dictMapper.selectGdnd();
    }


    @Override
    public List<Map> selectMiJiRole(Integer roleId) {
        List<DictDTO> miji = this.selectDictByCode("MIJI");
        // 查询已有权限列表
        MenuRoleDTO dto = new MenuRoleDTO();
        dto.setRoleId(roleId);
        dto.setRoleType(DataGlobalVariable.ROLE_TYPE_MIJI);
        List<MenuRoleDTO> select = menuRoleMapper.select(dto);
        List<Map> result = new ArrayList<>();
        for (DictDTO dictDTO : miji) {
            Map a = new HashMap();
            a.put("id", dictDTO.getId());
            a.put("title", dictDTO.getDictName());
            a.put("checked", false);
            for (MenuRoleDTO menuRoleDTO : select) {
                if (dictDTO.getId().equals(menuRoleDTO.getMenuId())) {
                    a.put("checked", true);
                    break;
                }
            }
            result.add(a);
        }
        return result;
    }

    @Override
    public Integer selectDeptQx(Integer roleId) {

        return this.menuRoleMapper.selectDeptQx(roleId);
    }
}
