package com.ho.user.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageHelper;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.entity.PageVO;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.util.PageUtilsOld;
import com.ho.user.entity.SysDict;
import com.ho.user.vo.req.*;
import com.ho.user.api.entity.SysSubDict;
import com.ho.user.mapper.SysDictMapper;
import com.ho.user.service.DictService;
import com.ho.user.vo.resp.DictRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author fancl
 * @desc: 字典实现类
 * @date 2022/8/18
 */
@Service
@Slf4j
public class DictServiceImpl implements DictService {

    @Autowired
    SysDictMapper dictMapper;

    @Autowired
    Snowflake snowflake;

    @Override
    public SysDict addTypeDict(DictAddTypeVO vo) {
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(vo,sysDict);
        dictMapper.insertDict(sysDict);
        return sysDict;
    }

    @Override
    public SysSubDict addSubDict(DictAddReqVO vo) {
        SysSubDict  subDict = new SysSubDict();
        BeanUtils.copyProperties(vo, subDict);
        subDict.setLabelEn(vo.getLabel());
        dictMapper.insertSubDict(subDict);
        return subDict;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDict updateDict(DictUpdateReqVO vo) {
        SysDict dict = new SysDict();
        BeanUtils.copyProperties(vo, dict);
        SysDict sysDict = dictMapper.selectById(vo.getId());
        if (sysDict == null){
            throw new BusinessException(BaseResponseCode.DICT_DELETED_EXISTS);
        }
        int upDictRet = dictMapper.updateDict(dict);
        log.info("修改sys_dict记录数:" + upDictRet);
        List<SysSubDict> sysSubDicts = selectSubDictByType(sysDict.getType(),null);
        int upSubDictRet = 0;
        for (SysSubDict sysSubDict : sysSubDicts) {
            sysSubDict.setType(vo.getType());
            upSubDictRet = dictMapper.updateSubDict(sysSubDict);
        }
        log.info("修改sys_sub_dict记录数:" + upSubDictRet);
        return dict;
    }

    @Override
    public SysSubDict updateSubDict(DictSubUpdateReqVO vo) {
        SysSubDict sysSubDict = dictMapper.selectBySubId(vo.getId());
        //先判断是否有相同value的值
//        if(!vo.getValue().equals(sysSubDict.getValue())){
//            SysSubDict subDict = selectSubDictByTV(vo.getType(), vo.getValue());
//            if (subDict != null) {
//                throw new BusinessException(BaseResponseCode.DICT_ALREADY_EXISTS);
//            }
//        }
        if(!vo.getSort().equals(sysSubDict.getSort())){
            //判断排序
            SysSubDict subDictSort = selectSubDictBySort(vo.getType(), vo.getSort());
            if (subDictSort != null) {
                throw new BusinessException(BaseResponseCode.SORT_CODE_BEEN_USED_PLEASE_CORRECT_SORT);
            }
        }


        SysSubDict subDict = new SysSubDict();
        BeanUtils.copyProperties(vo,subDict);
        List<SysSubDict> sysSubDicts = selectSubDictByType(vo.getType(),null);
        if (sysSubDicts.size() == 0){
            throw new BusinessException(BaseResponseCode.DICT_DELETED_EXISTS);
        }
        dictMapper.updateSubDict(subDict);
        return subDict;
    }
//
//    @Override
//    public SysDict detailInfoById(Integer id) {
//        SysDict sysDict = dictMapper.selectByPrimaryKey(id);
//        return sysDict;
//    }


//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void deletedById(Integer id) {
//        //通过id删除dict
//        SysDict dict = new SysDict();
//        dict.setId(id);
//        SysDict sysDict1 = dictMapper.selectById(id);
//        if (sysDict1 == null){
//            throw new BusinessException(BaseResponseCode.DICT_DELETED_EXISTS);
//        }
//        SysDict sysDict = dictMapper.selectById(id);
//        dictMapper.deleteByPrimaryKey(id);
//        //先查询sub_dict中的type,在对其进行删除
//        SysSubDict subDict = new SysSubDict();
//        subDict.setType(sysDict.getType());
//        subDict.setDeleted(CommonConstant.DELETED_FLAG);
//        dictMapper.updateByPrimaryKey(subDict);
//
//    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void deletedByType(String type) {
//        dictMapper.deleteByType(type);
//        SysSubDict subDict = new SysSubDict();
//        subDict.setType(type);
//        subDict.setDeleted(CommonConstant.DELETED_FLAG);
//        dictMapper.updateByPrimaryKey(subDict);
//    }

    @Override
    public void deletedSubById(Integer id) {
        SysSubDict subDict = new SysSubDict();
        subDict.setDeleted(CommonConstant.DELETED_FLAG);
        subDict.setId(id);
        dictMapper.updateSubDict(subDict);
    }

    @Override
    public List<SysSubDict> selectSubDictByType(String type,String lang) {
        List<SysSubDict> sysSubDicts = dictMapper.selectSubDictByType(type,lang);
        return sysSubDicts;
    }

    @Override
    public List<SysSubDict> selectSubDictByTypeList(List<String> typeList) {
        List<SysSubDict> sysSubDicts = dictMapper.selectSubDictByTypeList(typeList);
        return sysSubDicts;
    }

    @Override
    public DictRespVo selectDict(DictTypeAndLabelReqVO vo) {
        SysDict sysDict = dictMapper.selectByType(vo.getType());
        DictRespVo dictRespVo = new DictRespVo();
        BeanUtils.copyProperties(sysDict,dictRespVo);
        List<SysSubDict> sysSubDicts = selectSubDictByType(vo.getType(),vo.getLang());
        dictRespVo.setList(sysSubDicts);
        return dictRespVo;
    }



    @Override
    public PageVO<DictRespVo> all(DictPageReqVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<DictRespVo> sysDicts = dictMapper.selectAll(vo);
//        for (DictRespVo sysDict : sysDicts) {
//            List<SysSubDict> sysSubDicts = dictMapper.selectSubDictByType(sysDict.getType());
//            sysDict.setList(sysSubDicts);
//        }
        return PageUtilsOld.getPageVO(sysDicts);
    }

    /* @Override
    public PageVO<SysDept> pageInfo(DeptPageReqVO vo) {
        return null;
    }*/

    @Override
    public List<DictRespVo> selectDictType(String lang) {
        List<SysDict> sysDicts = dictMapper.selectTypeAll();
        List<DictRespVo> respVos = new ArrayList<>();
        for (SysDict sysDict : sysDicts) {
            DictRespVo vo = new DictRespVo();
            BeanUtils.copyProperties(sysDict,vo);
            List<SysSubDict> sysSubDicts = selectSubDictByType(sysDict.getType(),lang);
            vo.setList(sysSubDicts);
            respVos.add(vo);
        }
        return respVos;
    }

    @Override
    public SysDict selectDictByTypeAndName(DictAddTypeVO vo) {
        SysDict queryDict = new SysDict();
        BeanUtils.copyProperties(vo,queryDict);
        SysDict sysDict = dictMapper.selectDictByTypeAndName(queryDict);
        return sysDict;
    }

    @Override
    public SysDict selectDictByType(DictAddTypeVO vo) {
        SysDict sysDict = dictMapper.selectByType(vo.getType());
        return sysDict;
    }

    @Override
    public List<SysDict> selectDictListByType(String  type) {
        List<SysDict> sysDicts = dictMapper.selectDictListByType(type);
        return sysDicts;
    }

    @Override
    public SysSubDict selectSubDictByValue(DictAddReqVO vo) {
        SysSubDict subDict = dictMapper.selectSubDictByValue(vo.getValue());
        return subDict;
    }

    @Override
    public SysSubDict selectSubDictByTV(String type, String value) {
        SysSubDict subDict = dictMapper.selectSubDictByTV(type,value);
        return subDict;
    }

    @Override
    public List<SysSubDict> selectSubDictByKey(SysSubDict sysSubDict) {
        List<SysSubDict> subDictList = dictMapper.selectSubDictByKey(sysSubDict);
        return subDictList;
    }

    @Override
    public SysSubDict selectSubDictBySort(String type, Long sort) {
        SysSubDict subDictSort = dictMapper.selectSubDictBySort(type,sort);
        return subDictSort;

    }
}
