package com.mosty.common.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.config.Excel.DictUtils;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.config.entity.SysDictItem;
import com.mosty.common.config.entity.vo.AddDictItemVO;
import com.mosty.common.config.mapper.SysDictItemMapper;
import com.mosty.common.config.service.SysDictItemService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典代码值表 服务实现类
 * </p>
 *
 * @author kevin
 * @since 2022-02-09
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {

    private final SysDictItemMapper sysDictItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysDictItem(AddDictItemVO addDictItemVo, String rootPath, String ipAddress) {
        SysDictItem one = sysDictItemMapper.selectOne(new LambdaQueryWrapper<SysDictItem>()
                .eq(SysDictItem::getZdbh, addDictItemVo.getZdbh())
                .eq(SysDictItem::getDm, addDictItemVo.getDm())
                .eq(SysDictItem::getXtZxbz, "0")
        );
        Asserts.check(one != null, "当前字典编码 %s 下的字典代码 %s 已经存在！",
                addDictItemVo.getZdbh(), addDictItemVo.getDm());
        SysDictItem sysDictItem = new SysDictItem();
        sysDictItem.setRootPath(rootPath);
        sysDictItem.setZdId(addDictItemVo.getParentId());

        sysDictItem.setZdmc(addDictItemVo.getZdmc());
        sysDictItem.setPy(addDictItemVo.getPy());
        sysDictItem.setPx(addDictItemVo.getPx());
        sysDictItem.setBz(addDictItemVo.getBz());
        sysDictItem.setDm(addDictItemVo.getDm());
        sysDictItem.setZdbh(addDictItemVo.getZdbh());

        UserInfo userInfo = UserInfoManager.get();
        sysDictItem.setXtZxbz(DeletedEnum.NATURE.code);

        sysDictItem.setXtCjsj(new Date());
        sysDictItem.setXtZhxgsj(new Date());
        sysDictItem.setXtZhxgrid(userInfo.getUserId().toString());
        sysDictItem.setXtZhxgrxm(userInfo.getUserName());
        sysDictItem.setXtZhxgrbmid(userInfo.getDeptId().toString());
        sysDictItem.setXtZhxgrbm(userInfo.getDeptName());

        sysDictItem.setXtLrsj(new Date());
        sysDictItem.setXtLrrid(userInfo.getUserId().toString());
        sysDictItem.setXtLrrxm(userInfo.getUserName());
        sysDictItem.setXtLrrbmid(userInfo.getDeptId().toString());
        sysDictItem.setXtLrrbm(userInfo.getDeptName());

        sysDictItem.setXtLrip(ipAddress);
        sysDictItem.setXtZhxgip(ipAddress);
        // 先保存信息，返回当前的主键id
        sysDictItemMapper.insert(sysDictItem);

        // 根据主键id，修改rootPath
        SysDictItem updateEntity = new SysDictItem();
        updateEntity.setRootPath(sysDictItem.getRootPath() + sysDictItem.getId());
        updateEntity.setId(sysDictItem.getId());
        int row = sysDictItemMapper.updateById(updateEntity);
        if (row > 0) {
            List<SysDictItem> dictDatas = sysDictItemMapper.selectList(new LambdaQueryWrapper<SysDictItem>()
                    .eq(SysDictItem::getXtZxbz, 0)
                    .eq(SysDictItem::getZdbh, sysDictItem.getZdbh())
            );
            DictUtils.setDictCache(sysDictItem.getZdbh(), dictDatas);
        }


    }

}
