package thyyxxk.webserver.service.dictionary;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.PersonnelDictDao;
import thyyxxk.webserver.dao.his.dictionary.ZdUnitCodeDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.ZdUnitCode;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.PingYinUtils;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

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

/**
 * @Description: 人事字典
 * @Author: hsh
 * @CreateTime: 2023-10-16  16:39
 * @Version: 1.0
 */
@Service
@Slf4j
public class PersonnelDictService {

    private final PersonnelDictDao dao;

    private final ZdUnitCodeDao deptDao;

    private final RedisLikeService redis;

    @Autowired
    public PersonnelDictService(PersonnelDictDao dao, ZdUnitCodeDao deptDao, RedisLikeService redis) {
        this.dao = dao;
        this.deptDao = deptDao;
        this.redis = redis;
    }

    /**
     * @description: 查询科室字典信息
     * @author: hsh
     * @date: 2023/10/16 16:41
     * @param: [text]
     * @return: ResultVo<List<ZdUnitCode>>
     **/
    public ResultVo<List<ZdUnitCode>> selectDeptDict(String text){
        return ResultVoUtil.success(deptDao.selectDeptDictAll(text));
    }

    /**
     * @description: 查询有效科室
     * @author: hsh
     * @date: 2023/10/17 9:48
     * @param: []
     * @return: ResultVo<List<ZdUnitCode>>
     **/
    public ResultVo<List<ZdUnitCode>> selectDeptDictForEffective(){
        return ResultVoUtil.success(deptDao.selectDeptDictForEffective());
    }

    /**
     * @description: 查询科室分类字典
     * @author: hsh
     * @date: 2023/10/17 9:48
     * @param: []
     * @return: ResultVo<List<Map<String,Object>>>
     **/
    public ResultVo<List<Map<String, Object>>> selectUnitClass(){
        return ResultVoUtil.success(dao.selectUnitClass());
    }

    /**
     * @description: 根据科室code查询科室字典信息
     * @author: hsh
     * @date: 2023/10/19 16:23
     * @param: [code]
     * @return: ResultVo<ZdUnitCode>
     **/
    public ResultVo<ZdUnitCode> selectDeptDictByCode(String code){
        return ResultVoUtil.success(deptDao.selectDeptDictByCode(code));
    }

    /**
     * @description: 保存或更新科室字典信息
     * @author: hsh
     * @date: 2023/10/20 16:25
     * @param: [zdUnitCode]
     * @return: ResultVo<Map<String,Object>>
     **/
    public ResultVo<Map<String, Object>> saveDeptDict(ZdUnitCode zdUnitCode){

        if(null == zdUnitCode || zdUnitCode.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "科室字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = zdUnitCode.getCode();
        ZdUnitCode dept = deptDao.selectDeptDictByCode(code);

        UserInfo user = redis.getUserInfoByCode(TokenUtil.getInstance().getTokenUserId());
        // 根据名称生成新的拼音码和五笔码
        zdUnitCode.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdUnitCode.getName()));
        zdUnitCode.setDCode(PingYinUtils.getWBCode(zdUnitCode.getName()));
        zdUnitCode.setOpId(user.getCodeRs());
        zdUnitCode.setOpDate(DateUtil.now());
        int num = 0;
        if(null != dept){
            // 更新科室字典信息(这里仅限于更新老系统科室字典信息，其他信息修改待定)
            num = deptDao.updateDeptDictByCode(zdUnitCode);
        } else {
            // 新增科室字典信息(这里仅限于新增老系统科室字典信息，其他信息新增待定)
            num = deptDao.insert(zdUnitCode);
        }
        if(num == 0){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存科室字典失败！");
        }
        resultMap.put("cg", "保存科室字典成功！");
        return ResultVoUtil.success(resultMap);
    }


}
