package com.hzya.frame.sysnew.menuConfig.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson2.JSON;
import com.hzya.frame.sysnew.menuConfig.entity.ModuleVo;
import com.hzya.frame.sysnew.menuConfig.entity.SysMenuConfigEntity;
import com.hzya.frame.sysnew.menuConfig.dao.ISysMenuConfigDao;
import com.hzya.frame.sysnew.menuConfig.service.ISysMenuConfigService;
import com.hzya.frame.sysnew.organ.entity.SysOrganEntity;
import com.hzya.frame.sysnew.person.entity.SysPersonEntity;
import com.hzya.frame.sysnew.popedomMenu.dao.ISysPopedomMenuDao;
import com.hzya.frame.sysnew.popedomMenu.entity.SysPopedomMenuEntity;
import com.hzya.frame.sysnew.popedomOperate.dao.ISysPopedomOperateDao;
import com.hzya.frame.sysnew.popedomOperate.entity.SysPopedomOperateEntity;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.sysnew.userCompany.entity.SysUserCompanyEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

import com.hzya.frame.basedao.service.impl.BaseService;

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

/**
 * 系统菜单表(SysMenuConfig)表服务实现类
 *
 * @author makejava
 * @since 2024-03-26 15:23:53
 */
@Service(value = "sysMenuConfigService")
public class SysMenuConfigServiceImpl extends BaseService<SysMenuConfigEntity, String> implements ISysMenuConfigService {

    private ISysMenuConfigDao sysMenuConfigDao;
    @Resource
    private ISysPopedomMenuDao sysPopedomMenuDao;
    @Resource
    private ISysPopedomOperateDao sysPopedomOperateDao;

    @Autowired
    public void setSysMenuConfigDao(ISysMenuConfigDao dao) {
        this.sysMenuConfigDao = dao;
        this.dao = dao;
    }


    /**
     * @Author lvleigang
     * @Description  查询用户菜单树
     * @Date 2:55 下午 2024/4/2
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity queryTreeById(JSONObject jsonObject) {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }

        SysMenuConfigEntity sysMenuConfigEntity = new SysMenuConfigEntity();
        sysMenuConfigEntity.setUserId(entity.getId());
        //sysMenuConfigEntity.setVisibles("0");
        List<SysMenuConfigEntity> sysMenuConfigEntities = sysMenuConfigDao.queryUserMenu(sysMenuConfigEntity);

        List<ModuleVo> moduleVos = new ArrayList<>();
        if (sysMenuConfigEntities != null && sysMenuConfigEntities.size() > 0) {
            for (int i = 0; i < sysMenuConfigEntities.size(); i++) {
                if ("0".equals(sysMenuConfigEntities.get(i).getParentMenuId())) {
                    moduleVos.add(recursiveSetUseTree(sysMenuConfigEntities.get(i), sysMenuConfigEntities));
                }
            }
        }

        return BaseResult.getSuccessMessageEntity("查询菜单树成功", moduleVos);
    }

    /**
     * @param sysMenuConfigEntity
     * @param sysMenuConfigEntities
     * @return com.hzya.frame.sysnew.menuConfig.entity.ModuleVo
     * @Author lvleigang
     * @Description
     * @Date 9:21 上午 2024/4/2
     **/
    private ModuleVo recursiveSetUseTree(SysMenuConfigEntity sysMenuConfigEntity, List<SysMenuConfigEntity> sysMenuConfigEntities) {
        ModuleVo moduleVo = new ModuleVo();
        moduleVo.setId(sysMenuConfigEntity.getId());
        moduleVo.setPath(sysMenuConfigEntity.getRoute());
        moduleVo.setName(sysMenuConfigEntity.getMenuEnglishName());
        moduleVo.setComponent(sysMenuConfigEntity.getMenuPage());
        JSONObject meta = new JSONObject();
        meta.put("id", sysMenuConfigEntity.getId());
        meta.put("title", sysMenuConfigEntity.getMenuName());
        meta.put("icon", sysMenuConfigEntity.getMenuIcon());
        if("0".equals(sysMenuConfigEntity.getVisibles())){
            meta.put("hidden", false);
        }else {
            meta.put("hidden", true);
        }
        if(sysMenuConfigEntity.getOptions() != null && !"".equals(sysMenuConfigEntity.getOptions())){
            Map<String, Object> map = JSONObject.parseObject(sysMenuConfigEntity.getOptions(), new TypeReference<Map<String, Object>>(){});
            if(map != null && map.size() > 0){
                meta.putAll(map);
            }
        }
        moduleVo.setMeta(meta);
        List<ModuleVo> children = new ArrayList<>();
        for (int i = 0; i < sysMenuConfigEntities.size(); i++) {
            if (sysMenuConfigEntity.getId().equals(sysMenuConfigEntities.get(i).getParentMenuId())) {
                children.add(recursiveSetUseTree(sysMenuConfigEntities.get(i),sysMenuConfigEntities));
            }
        }
        moduleVo.setChildren(children);
        return moduleVo;

    }


    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询菜单树
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntityTree(JSONObject jsonObject) {
        SysMenuConfigEntity entity = getData("jsonStr", jsonObject, SysMenuConfigEntity.class);
        if (entity == null) {
            entity = new SysMenuConfigEntity();
        }
        List<SysMenuConfigEntity> returnMenu = new ArrayList<>();
        //查询符合条件的菜单数据
        List<SysMenuConfigEntity> sysMenuConfigEntityList = sysMenuConfigDao.queryByLike(entity);
        if (sysMenuConfigEntityList != null && sysMenuConfigEntityList.size() > 0) {
            for (int i = 0; i < sysMenuConfigEntityList.size(); i++) {
                if ("0".equals(sysMenuConfigEntityList.get(i).getParentMenuId())) {
                    recursiveSetTree(sysMenuConfigEntityList.get(i), sysMenuConfigEntityList);
                    sysMenuConfigEntityList.get(i).setLabel(sysMenuConfigEntityList.get(i).getMenuName());
                    returnMenu.add(sysMenuConfigEntityList.get(i));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("查询菜单树成功", returnMenu);
    }

    /**
     * @param sysMenuConfigEntity
     * @param sysMenuConfigEntityList
     * @return void
     * @Author lvleigang
     * @Description 递归设置树
     * @Date 9:19 上午 2024/3/28
     **/
    private void recursiveSetTree(SysMenuConfigEntity sysMenuConfigEntity, List<SysMenuConfigEntity> sysMenuConfigEntityList) {
        List<SysMenuConfigEntity> children = new ArrayList<>();
        for (int i = 0; i < sysMenuConfigEntityList.size(); i++) {
            if (sysMenuConfigEntityList.get(i).getParentMenuId() != null && !"".equals(sysMenuConfigEntityList.get(i).getParentMenuId())) {
                if (sysMenuConfigEntity.getId().equals(sysMenuConfigEntityList.get(i).getParentMenuId())) {
                    sysMenuConfigEntityList.get(i).setLabel(sysMenuConfigEntityList.get(i).getMenuName());
                    children.add(sysMenuConfigEntityList.get(i));
                    recursiveSetTree(sysMenuConfigEntityList.get(i), sysMenuConfigEntityList);
                }
            }
        }
        sysMenuConfigEntity.setChildren(children);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存菜单
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity saveEntity(JSONObject jsonObject) {
        SysMenuConfigEntity entity = getData("jsonStr", jsonObject, SysMenuConfigEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMenuName() == null || "".equals(entity.getMenuName())) {
            return BaseResult.getFailureMessageEntity("请先输入菜单名称");
        }
        if (entity.getParentMenuId() == null || "".equals(entity.getParentMenuId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setCreate();
        //保存
        sysMenuConfigDao.save(entity);
        return BaseResult.getSuccessMessageEntity("保存菜单成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取菜单
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity getEntity(JSONObject jsonObject) {
        SysMenuConfigEntity entity = getData("jsonStr", jsonObject, SysMenuConfigEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = sysMenuConfigDao.get(entity.getId());
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("获取菜单失败");
        }
        return BaseResult.getSuccessMessageEntity("获取菜单成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改菜单
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject jsonObject) {
        SysMenuConfigEntity entity = getData("jsonStr", jsonObject, SysMenuConfigEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMenuName() == null || "".equals(entity.getMenuName())) {
            return BaseResult.getFailureMessageEntity("请先输入菜单名称");
        }
        if (entity.getParentMenuId() == null || "".equals(entity.getParentMenuId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        //保存
        sysMenuConfigDao.update(entity);
        return BaseResult.getSuccessMessageEntity("修改菜单成功", entity);

    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除菜单, 只能删除最后一层
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity deleteEntity(JSONObject jsonObject) {
        SysMenuConfigEntity entity = getData("jsonStr", jsonObject, SysMenuConfigEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (!checkUse(entity)) {
            return BaseResult.getFailureMessageEntity("当前菜单已经使用，不允许删除");
        }
        entity.setUpdate();
        sysMenuConfigDao.logicRemove(entity);
        return BaseResult.getSuccessMessageEntity("删除菜单成功");
    }

    /**
     * @param entity
     * @return boolean
     * @Author lvleigang
     * @Description 校验菜单是否使用 目前只校验（用户\人员）
     * @Date 3:27 下午 2024/3/27
     **/
    private boolean checkUse(SysMenuConfigEntity entity) {
        SysPopedomMenuEntity sysPopedomMenuEntity = new SysPopedomMenuEntity();
        sysPopedomMenuEntity.setMenuId(entity.getId());
        if (sysPopedomMenuDao.getCount(sysPopedomMenuEntity) > 0) {
            return false;
        }
        //没有配置菜单，肯定没有按钮，所以不查询
        //SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
        //sysPopedomOperateEntity.setMenuId(entity.getId());
        //if(sysPopedomOperateDao.getCount(sysPopedomOperateEntity) > 0){
        //    return false;
        //}
        return true;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 启用停用菜单
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
        SysMenuConfigEntity entity = getData("jsonStr", jsonObject, SysMenuConfigEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getVisibles() == null || "".equals(entity.getVisibles())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //0启用 1 停用
        if ("0".equals(entity.getVisibles())) {
            entity.setUpdate();
            sysMenuConfigDao.update(entity);
            return BaseResult.getSuccessMessageEntity("启用菜单成功");
        } else {
            //删除用户和角色配置的菜单，以及按钮
            SysPopedomMenuEntity sysPopedomMenuEntity = new SysPopedomMenuEntity();
            sysPopedomMenuEntity.setMenuId(entity.getId());
            sysPopedomMenuEntity.setUpdate();
            sysPopedomMenuDao.logicRemoveMultiCondition(sysPopedomMenuEntity);

            SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
            sysPopedomOperateEntity.setMenuId(entity.getId());
            sysPopedomOperateEntity.setUpdate();
            sysPopedomOperateDao.logicRemoveMultiCondition(sysPopedomOperateEntity);

            entity.setUpdate();
            sysMenuConfigDao.update(entity);
            return BaseResult.getSuccessMessageEntity("停用菜单成功");
        }
    }
}
