package com.lantone.security.facade;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.enums.StatusEnum;
import com.lantone.common.exception.ApiException;
import com.lantone.common.util.StringUtil;
import com.lantone.common.util.SysUserUtils;
import com.lantone.security.vo.GetDictionaryVO;
import com.lantone.security.vo.SaveDictionaryVO;
import com.lantone.security.entity.DictionaryInfo;
import com.lantone.security.facade.base.DictionaryInfoFacade;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 系统字典表 服务实现类Facade
 * </p>
 */
@Component
public class DictionaryManagementFacade {
    @Autowired
    private DictionaryInfoFacade dictionaryInfoFacade;

    /**
     * @Description: 新增字典
     * @Param: [saveDictionaryVO]
     * @return: java.lang.Object
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Object addDictionary(SaveDictionaryVO saveDictionaryVO) {
        //数据校验
        getCheckDictionary(saveDictionaryVO);
        DictionaryInfo dictionaryInfo = new DictionaryInfo();
        BeanUtils.copyProperties(saveDictionaryVO, dictionaryInfo);
        return addDictionary(saveDictionaryVO.getUserId(), dictionaryInfo);
    }

    /**
     * @Description: 修改字典
     * @Param: [saveDictionaryVO]
     * @return: java.lang.Object
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Object updateDictionary(SaveDictionaryVO saveDictionaryVO) {
        saveDictionaryVO.setUserId(SysUserUtils.getCurrentPrincipleId());
        DictionaryInfo dictionaryInfo = new DictionaryInfo();
        BeanUtils.copyProperties(saveDictionaryVO, dictionaryInfo);
        return updateDictionary(saveDictionaryVO.getUserId(), dictionaryInfo);
    }

    /**
     * @Description: 查看字典管理
     * @Param: [getDictionaryVO]
     * @return: IPage<com.lantone.security.entity.DictionaryInfo>
     * @Author: cy
     * @Date: 2021/7/30
     */
    public IPage<DictionaryInfo> getDictionary(GetDictionaryVO getDictionaryVO) {
        IPage<DictionaryInfo> pageInfo = new Page();
        pageInfo.setCurrent(getDictionaryVO.getCurrent());
        pageInfo.setSize(getDictionaryVO.getSize());
        LambdaQueryChainWrapper<DictionaryInfo> dictionaryInfoLambdaQuery = dictionaryInfoFacade.lambdaQuery();
        dictionaryInfoLambdaQuery.eq(DictionaryInfo::getIsDeleted, IsDeleteEnum.N.getKey());
        if (StringUtil.isNotEmpty(getDictionaryVO.getName())) {
            dictionaryInfoLambdaQuery.like(DictionaryInfo::getName, getDictionaryVO.getName());
        }
        if (null != getDictionaryVO.getGroupType()) {
            dictionaryInfoLambdaQuery.like(DictionaryInfo::getGroupType, getDictionaryVO.getGroupType());
        }
        if (null != getDictionaryVO.getId()) {
            dictionaryInfoLambdaQuery.eq(DictionaryInfo::getId, getDictionaryVO.getId());
        }
        dictionaryInfoLambdaQuery.orderByDesc(DictionaryInfo::getGmtCreate);
        IPage<DictionaryInfo> page = dictionaryInfoLambdaQuery.page(pageInfo);
        page.getRecords().forEach(obj -> {
            if (StringUtil.isNotBlank(obj.getStatus())) {
                if (obj.getStatus().equals(StatusEnum.Enable.getKey())) {
                    obj.setStatus("启用");
                }
                if (obj.getStatus().equals(StatusEnum.Disable.getKey())) {
                    obj.setStatus("禁用");
                }
            }
        });
        page.setRecords(page.getRecords());
        return page;
    }

    /**
     * @Description: 删除字典管理--逻辑删除
     * @Param: [id]
     * @return: java.lang.Boolean
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Boolean deleteDictionary(Integer id) {
        return dictionaryInfoFacade.lambdaUpdate().eq(DictionaryInfo::getId, id).set(DictionaryInfo::getIsDeleted, IsDeleteEnum.Y.getKey()).update();
    }


    /**
     * @Description: 新增-校验重复数据
     * @Param: [saveDictionaryVO]
     * @return: void
     * @Author: cy
     * @Date: 2021/7/30
     */
    public void getCheckDictionary(SaveDictionaryVO saveDictionaryVO) {
        saveDictionaryVO.setUserId(SysUserUtils.getCurrentPrincipleId());
        if (null == saveDictionaryVO.getId()) {
            Integer count = dictionaryInfoFacade.lambdaQuery()
                    .eq(DictionaryInfo::getIsDeleted, IsDeleteEnum.N.getKey())
                    .eq(DictionaryInfo::getName, saveDictionaryVO.getName())
                    .eq(DictionaryInfo::getVal, saveDictionaryVO.getVal())
                    .eq(DictionaryInfo::getStatus, saveDictionaryVO.getStatus())
                    .eq(DictionaryInfo::getGroupType, saveDictionaryVO.getGroupType())
                    .count();
            if (count > 0) {
                throw new ApiException("当前字典已重复添加");
            }
        }
    }

    /**
     * @Description: 新增字典
     * @Param: [dictionaryInfo]
     * @return: java.lang.Boolean
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Boolean addDictionary(Long userId, DictionaryInfo dictionaryInfo) {
        dictionaryInfo.setGmtCreate(new Date());
        dictionaryInfo.setCreator(userId.toString());
        return dictionaryInfoFacade.save(dictionaryInfo);
    }

    /**
     * @Description: 修改字典
     * @Param: [dictionaryInfo]
     * @return: java.lang.Boolean
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Boolean updateDictionary(Long userId, DictionaryInfo dictionaryInfo) {
        dictionaryInfo.setGmtModified(new Date());
        dictionaryInfo.setModifier(userId.toString());
        return dictionaryInfoFacade.updateById(dictionaryInfo);
    }


    /**
     * @Description: 查看字典管理代码类别
     * @Param: [groupType]
     * @return: java.util.List<java.lang.String>
     * @Author: cy
     * @Date: 2021/8/9
     */
    public List<String> getCodeCategory(String groupType) {
        LambdaQueryChainWrapper<DictionaryInfo> dictionaryInfoLambdaQuery = dictionaryInfoFacade.lambdaQuery();
        dictionaryInfoLambdaQuery.eq(DictionaryInfo::getIsDeleted, IsDeleteEnum.N.getKey());
        if (StringUtil.isNotBlank(groupType)) {
            dictionaryInfoLambdaQuery.like(DictionaryInfo::getGroupType, groupType);
        }
        return dictionaryInfoLambdaQuery.groupBy(DictionaryInfo::getGroupType).list()
                .stream().filter(obj -> null != obj.getGroupType())
                .map(obj -> obj.getGroupType()).sorted().collect(Collectors.toList());
    }

    public boolean initDicMap(){
        dictionaryInfoFacade.initDicMap();
        return true;
    }

}