package com.hxhq.system.service.impl;

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

import com.hxhq.business.domain.SystemLog;
import com.hxhq.business.service.ISystemLogService;
import com.hxhq.business.utils.ObjectCompareUtil;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.api.domain.SysDictType;
import com.hxhq.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.common.security.utils.DictUtils;
import com.hxhq.system.api.domain.SysDictData;
import com.hxhq.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 * 
 * @author hxhq
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private ISystemLogService systemLogService;

    /**
     * 根据条件分页查询字典数据
     * 
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData)
    {
        String split = ",";
        if(dictData.getDictType().contains(split)) {
            List<SysDictData> sysDictDataList = new ArrayList<>();
            String[] dictTypeList = dictData.getDictType().split(",");
            for (String dictType : dictTypeList) {
                dictData.setDictType(dictType);
                List<SysDictData> sysDictData = dictDataMapper.selectDictDataList(dictData);
                sysDictDataList.addAll(sysDictData);
            }
            return sysDictDataList;
        } else {
            return dictDataMapper.selectDictDataList(dictData);
        }
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);

            systemLogService.saveInfo(data.getDictLabel(),null,"删除字典数据","Remove Term Book Data",null,null, SecurityUtils.getUserId(),SecurityUtils.getNickName(),SecurityUtils.getUsername(),null);
        }
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        systemLogService.saveInfo(data.getDictLabel(),null,"新增字典数据","Create Term Book Data",null,null, SecurityUtils.getUserId(),SecurityUtils.getNickName(),SecurityUtils.getUsername(),null);
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data)
    {
        SysDictData oldDict = dictDataMapper.selectDictDataById(data.getDictCode());
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }

        List<SystemLog> logList = getModifyLogList(data,oldDict);
        if(logList.size()>0){
            systemLogService.saveBatch(logList);
        }
        return row;
    }

    /**
     * 获取修改日志
     * @param info
     * @param old
     * @return
     */
    private List<SystemLog> getModifyLogList(SysDictData info, SysDictData old){
        List<SystemLog> list = new ArrayList<>();

        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjects(old, info);
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            SystemLog log = getLogInfo(old);
            log.setJcnr(fieldChange.toString());
            log.setJcnrEn(fieldChange.toEnString());
            list.add(log);
        }
        return list;
    }

    /**
     * 获取日志基础信息
     * @return
     */
    private SystemLog getLogInfo(SysDictData sysDictData){
        SystemLog log = new SystemLog();
        log.setName(sysDictData.getDictLabel());
        log.setNameEn(sysDictData.getDictLabel());
        log.setJcmc("编辑字典数据");
        log.setJcmcEn("Edit Term Book Data");
        log.setQmrId(SecurityUtils.getUserId());
        log.setQmrMc(SecurityUtils.getNickName());
        log.setQmrMcEn(SecurityUtils.getUsername());
        return log;
    }
}
