package com.persagy.dmp.dic.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.dic.dao.DictDataMapper;
import com.persagy.dmp.dic.entity.DictData;
import com.persagy.dmp.dic.entity.DictDataMap;
import com.persagy.dmp.dic.entity.DictType;
import com.persagy.dmp.dic.service.DictDataMapService;
import com.persagy.dmp.dic.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典数据
 * @author:linhuili
 * @date:2021/6/10
 */
@Service
public class DictDataServiceImpl implements DictDataService {

    @Autowired
    private DictDataMapper dao;
    
    @Autowired
    private DictDataMapService dictDataMapService;

    /**
     * 按条件计数
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Integer queryCount(Wrapper<DictData> queryWrapper) {
        return dao.selectCount(queryWrapper);
    }

    /**
     * 分页查询
     *
     * @param page         分页条件
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Page queryByCondition(Page page, Wrapper<DictData> queryWrapper) {
        return dao.selectPage(page, queryWrapper);
    }

    /**
     * 查询
     *
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public List<DictData> queryByCondition(Wrapper<DictData> queryWrapper) {
        return dao.selectList(queryWrapper);
    }

    /**
     * 新增
     *
     * @param vo 待新增数据
     * @return
     */
    @Override
    public DictData insert(DictData vo) {
        vo.setValid(ValidEnum.TRUE.getType());
        dao.insert(vo);
        return vo;
    }

    /**
     * 修改
     *
     * @param vo 待修改数据
     * @return
     */
    @Override
    public DictData update(DictData vo) {
        DictData dictData = dao.selectById(vo.getId());
        if (dictData == null || 0 == dictData.getValid()) {
            throw new BusinessException("数据不存在！");
        }
        dao.updateById(vo);
        return vo;
    }

    /**
     * 删除
     *
     * @param vo 主键
     */
    @Override
    public void delete(DictData vo) {
        DictData dictData = dao.selectById(vo.getId());
        if (dictData == null || 0 == dictData.getValid()) {
            throw new BusinessException("数据不存在！");
        }
        dictData.setValid(ValidEnum.FALSE.getType());
        dao.updateById(dictData);
    }


    /**
     * 返回映射表替代字典数据的数据集
     * @param dictType
     * @return
     */
    @Override
    public List<DictData> queryData(DictType dictType) {

        //查询字典数据
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",dictType.getType());
        queryWrapper.eq("valid",ValidEnum.TRUE.getType());
        List<DictData> dictDataList = queryByCondition(queryWrapper);

        //查询字典映射数据
        QueryWrapper<DictDataMap> queryMapWrapper = new QueryWrapper<>();
        queryMapWrapper.eq("type",dictType.getType());
        queryMapWrapper.eq("group_code",dictType.getGroupCode());
        queryMapWrapper.eq("valid",ValidEnum.TRUE.getType());
        List<DictDataMap> dictDataMapList = dictDataMapService.queryByCondition(queryMapWrapper);

        //存储映射关系
        Map<String, DictDataMap> dtDataMapMap = new HashMap<>(16);
        for(DictDataMap dictDataMap : dictDataMapList){
           dtDataMapMap.put(dictDataMap.getTargetCode(),dictDataMap);
        }

        //通过targetCode将DictData中的code，label，替换为DictDataMap中的code，label
        for(DictData dictData: dictDataList){
            DictDataMap ddm = dtDataMapMap.get(dictData.getCode());
            if(ddm == null){
                continue;
            }
            dictData.setCode(ddm.getCode());
            dictData.setLabel(ddm.getLabel());
        }
        return dictDataList;
    }
}

