package thyyxxk.webserver.service.dictionary;

import lombok.RequiredArgsConstructor;
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.EmployeeMiDao;
import thyyxxk.webserver.dao.his.dictionary.PersonnelDictDao;
import thyyxxk.webserver.dao.his.dictionary.ZdEducationCodeDao;
import thyyxxk.webserver.dao.his.dictionary.ZdEmpInmarkDao;
import thyyxxk.webserver.dao.his.dictionary.ZdEmpPositionDao;
import thyyxxk.webserver.dao.his.dictionary.ZdEmpTitleDao;
import thyyxxk.webserver.dao.his.dictionary.ZdIfcadreDao;
import thyyxxk.webserver.dao.his.dictionary.ZdUnitClassDao;
import thyyxxk.webserver.dao.his.dictionary.ZdUnitCodeDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.dictionary.EmployeeMi;
import thyyxxk.webserver.entity.dictionary.EmployeeMiVo;
import thyyxxk.webserver.entity.dictionary.ZdEducationCode;
import thyyxxk.webserver.entity.dictionary.ZdEmpInmark;
import thyyxxk.webserver.entity.dictionary.ZdEmpPosition;
import thyyxxk.webserver.entity.dictionary.ZdEmpTitle;
import thyyxxk.webserver.entity.dictionary.ZdIfcadre;
import thyyxxk.webserver.entity.dictionary.ZdUnitClass;
import thyyxxk.webserver.entity.dictionary.ZdUnitCode;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.PingYinUtils;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

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
@RequiredArgsConstructor
public class PersonnelDictService {

    private final PersonnelDictDao dao;

    private final ZdUnitCodeDao deptDao;

    private final ZdUnitClassDao deptClassDao;

    private final ZdEmpPositionDao empPositionDao;

    private final ZdEmpTitleDao empTitleDao;

    private final ZdEducationCodeDao educationCodeDao;

    private final ZdIfcadreDao ifcadreDao;

    private final ZdEmpInmarkDao empInmarkDao;

    private final EmployeeMiDao employeeMiDao;

    private final UserCache userCache;

    public ResultVo<List<ZdUnitCode>> selectZdUnitCode(String text) {
        return ResultVoUtil.success(deptDao.selectZdUnitCode(text));
    }

    /**
     * @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 = userCache.getUserInfoByToken();
        // 根据名称生成新的拼音码和五笔码
        zdUnitCode.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdUnitCode.getName()));
        zdUnitCode.setDCode(PingYinUtils.getWBCode(zdUnitCode.getName()));
        zdUnitCode.setOpId(user.getCode());
        zdUnitCode.setOpDate(DateUtil.now());
        int num;
        try {
            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);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存科室字典失败！");
        }
    }

    /**
     * @description: 根据科室编码更新科室停用状态
     * @author: hsh
     * @date: 2023/11/9 17:18
     * @param: [code, delFlag]
     * @return: ResultVo<ZdUnitCode>
     **/
    public ResultVo<Map<String, Object>> updateDeptStopOrUsed(String code, String delFlag) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有科室编码，请检查！");
        }

        ZdUnitCode dept = deptDao.selectDeptDictByCode(code);
        if (null == dept) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, code + "的科室字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = deptDao.updateDeptDictDelFlagByCode(code, delFlag);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新科室字典停用或启用失败！");
            }
            resultMap.put("cg", "更新科室字典停用或启用成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新科室字典停用或启用失败！");
        }

    }

    /**
     * 根据科室编码删除科室字典
     * @param code 科室编码
     * @return map
     */
    public ResultVo<Map<String, Object>> delDeptDictByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有科室编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = deptDao.deleteByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除科室字典失败！");
            }
            resultMap.put("cg", "删除科室字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除科室字典失败！");
        }
    }

    /**
     * 查询科室分类字典
     * @return ResultVo<ZdUnitClass>
     */
    public ResultVo<List<ZdUnitClass>> selectDeptClass() {
        return ResultVoUtil.success(deptClassDao.selectDeptClass());
    }

    /**
     *  保存科室分类
     * @param zdUnitClass 科室分类信息
     * @return map
     */
    public ResultVo<Map<String, Object>> saveDeptClass(ZdUnitClass zdUnitClass) {

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

        Map<String, Object> resultMap = new HashMap<>();
        String code = zdUnitClass.getCode();
        ZdUnitClass deptClass = deptClassDao.selectDeptClassByCode(code);

        // 根据名称生成新的拼音码和五笔码
        zdUnitClass.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdUnitClass.getName()));
        zdUnitClass.setDCode(PingYinUtils.getWBCode(zdUnitClass.getName()));
        int num;
        try {
            if (null != deptClass) {
                num = deptClassDao.updateDeptClassByCode(zdUnitClass);
            } else {
                num = deptClassDao.insert(zdUnitClass);
            }
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存科室分类失败！");
            }
            resultMap.put("cg", "保存科室分类成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存科室分类失败！");
        }
    }

    /**
     *  根据code删除科室分类
     * @param code 科室分类code
     * @return map
     */
    public ResultVo<Map<String, Object>> delDeptClassByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有科室分类编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = deptClassDao.deleteDeptClassByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除科室分类失败！");
            }
            resultMap.put("cg", "删除科室分类成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除科室分类失败！");
        }
    }

    /**
     * @Description 查询行政职务字典
     * @Author hsh
     * @Date 2023/12/19 15:17
     */
    public ResultVo<List<ZdEmpPosition>> selectEmpPosition() {
        return ResultVoUtil.success(empPositionDao.selectEmpPosition());
    }

    /**
     * @Description 保存行政职务字典
     * @Author hsh
     * @param zdEmpPosition 行政职务字典信息
     * @return map
     * @Date 2023/12/20 9:47
     */
    public ResultVo<Map<String, Object>> saveEmpPosition(ZdEmpPosition zdEmpPosition) {

        if (null == zdEmpPosition || zdEmpPosition.getCode() == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "行政职务字典信息不存在，请检查！");
        }

        if (StringUtil.notBlank(zdEmpPosition.getDelFlag()) && zdEmpPosition.getDelFlag().length() > 1) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除标志过长，不允许超出一个字符，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = zdEmpPosition.getCode();
        ZdEmpPosition empPosition = empPositionDao.selectEmpPositionByCode(code);

        // 根据名称生成新的拼音码和五笔码
        zdEmpPosition.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdEmpPosition.getName()));
        zdEmpPosition.setDCode(PingYinUtils.getWBCode(zdEmpPosition.getName()));
        int num;
        try {
            if (null != empPosition) {
                num = empPositionDao.updateEmpPositionByCode(zdEmpPosition);
            } else {
                num = empPositionDao.insert(zdEmpPosition);
            }
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存行政职务字典失败！");
            }
            resultMap.put("cg", "保存行政职务字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存行政职务字典失败！");
        }
    }

    /**
     * @Description 根据code删除行政职务字典
     * @Author hsh
     * @param code 行政职务字典code
     * @return map
     * @Date 2023/12/20 9:48
     */
    public ResultVo<Map<String, Object>> delEmpPositionByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有行政职务字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = empPositionDao.deleteEmpPositionByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除行政职务字典失败！");
            }
            resultMap.put("cg", "删除行政职务字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除行政职务字典失败！");
        }
    }

    /**
     * @Description 查询专业技术职称
     * @Author hsh
     * @return ZdEmpTitle
     * @Date 2023/12/20 14:39
     */
    public ResultVo<List<ZdEmpTitle>> selectEmpTitle() {
        return ResultVoUtil.success(empTitleDao.selectEmpTitle());
    }

    /**
     * @Description 保存专业技术职称
     * @Author hsh
     * @param zdEmpTitle 专业技术职称信息
     * @return map
     * @Date 2023/12/20 14:40
     */
    public ResultVo<Map<String, Object>> saveEmpTitle(ZdEmpTitle zdEmpTitle) {

        if (null == zdEmpTitle || zdEmpTitle.getCode() == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "专业技术职称信息不存在，请检查！");
        }

        if (StringUtil.notBlank(zdEmpTitle.getDelFlag()) && zdEmpTitle.getDelFlag().length() > 1) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除标志过长，不允许超出一个字符，请检查！");
        }

        if (StringUtil.notBlank(zdEmpTitle.getTitGrade()) && zdEmpTitle.getTitGrade().length() > 1) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "专业技术等级过长，不允许超出一个字符，请检查！");
        }

        if (StringUtil.notBlank(zdEmpTitle.getOrderCode()) && zdEmpTitle.getOrderCode().length() > 6) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "专业技术等级过长，不允许超出六个字符，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = zdEmpTitle.getCode();
        ZdEmpTitle empTitle = empTitleDao.selectEmpTitleByCode(code);

        // 根据名称生成新的拼音码和五笔码
        zdEmpTitle.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdEmpTitle.getName()));
        zdEmpTitle.setDCode(PingYinUtils.getWBCode(zdEmpTitle.getName()));
        int num;
        try {
            if (null != empTitle) {
                num = empTitleDao.updateEmpTitleByCode(zdEmpTitle);
            } else {
                num = empTitleDao.insert(zdEmpTitle);
            }
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存专业技术职称失败！");
            }
            resultMap.put("cg", "保存专业技术职称成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存专业技术职称失败！");
        }
    }

    /**
     * @Description 根据code删除专业技术职称
     * @Author hsh
     * @param code 专业技术职称编码
     * @return map
     * @Date 2023/12/20 14:47
     */
    public ResultVo<Map<String, Object>> delEmpTitleByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有专业技术职称编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = empTitleDao.deleteEmpTitleByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除专业技术职称失败！");
            }
            resultMap.put("cg", "删除专业技术职称成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除专业技术职称失败！");
        }
    }

    /**
     * @Description 查询文化程度字典
     * @Author hsh
     * @return ZdEducationCode
     * @Date 2023/12/20 16:00
     */
    public ResultVo<List<ZdEducationCode>> selectEducation() {
        return ResultVoUtil.success(educationCodeDao.selectEducation());
    }

    /**
     * @Description 保存文化程度字典
     * @Author hsh
     * @param zdEducationCode 文化程度字典信息
     * @return map
     * @Date 2023/12/20 16:01
     */
    public ResultVo<Map<String, Object>> saveEducation(ZdEducationCode zdEducationCode) {

        if (null == zdEducationCode || zdEducationCode.getCode() == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "文化程度字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = zdEducationCode.getCode();
        ZdEducationCode education = educationCodeDao.selectEducationByCode(code);

        // 根据名称生成新的拼音码和五笔码
        zdEducationCode.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdEducationCode.getName()));
        zdEducationCode.setDCode(PingYinUtils.getWBCode(zdEducationCode.getName()));
        int num;
        try {
            if (null != education) {
                num = educationCodeDao.updateEducationByCode(zdEducationCode);
            } else {
                num = educationCodeDao.insert(zdEducationCode);
            }
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存文化程度字典失败！");
            }
            resultMap.put("cg", "保存文化程度字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存文化程度字典失败！");
        }
    }

    /**
     * @Description 根据code删除文化程度字典
     * @Author hsh
     * @param code 文化程度字典编码
     * @return map
     * @Date 2023/12/20 16:02
     */
    public ResultVo<Map<String, Object>> delEducationByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有文化程度字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = educationCodeDao.deleteEducationByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除文化程度字典失败！");
            }
            resultMap.put("cg", "删除文化程度字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除文化程度字典失败！");
        }
    }

    /**
     * @Description 查询干工标志字典
     * @Author hsh
     * @return ZdIfcadre 干工标志字典信息
     * @Date 2023/12/20 16:22
     */
    public ResultVo<List<ZdIfcadre>> selectIfcadre() {
        return ResultVoUtil.success(ifcadreDao.selectIfcadre());
    }

    /**
     * @Description 保存干工标志字典
     * @Author hsh
     * @param zdIfcadre 干工标志字典信息
     * @return map
     * @Date 2023/12/20 16:22
     */
    public ResultVo<Map<String, Object>> saveIfcadre(ZdIfcadre zdIfcadre) {

        if (null == zdIfcadre || zdIfcadre.getCode() == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "干工标志字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = zdIfcadre.getCode();
        ZdIfcadre cadre = ifcadreDao.selectIfcadreByCode(code);

        // 根据名称生成新的拼音码和五笔码
        zdIfcadre.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdIfcadre.getName()));
        zdIfcadre.setDCode(PingYinUtils.getWBCode(zdIfcadre.getName()));
        int num;
        try {
            if (null != cadre) {
                num = ifcadreDao.updateIfcadreByCode(zdIfcadre);
            } else {
                num = ifcadreDao.insert(zdIfcadre);
            }
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存干工标志字典失败！");
            }
            resultMap.put("cg", "保存干工标志字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存干工标志字典失败！");
        }
    }

    /**
     * @Description 根据code删除干工标志字典
     * @Author hsh
     * @param code 干工标志字典编码
     * @return map
     * @Date 2023/12/20 16:23
     */
    public ResultVo<Map<String, Object>> delIfcadreByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有干工标志字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = ifcadreDao.deleteIfcadreByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除干工标志字典失败！");
            }
            resultMap.put("cg", "删除干工标志字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除干工标志字典失败！");
        }
    }

    /**
     * @Description 查询在院标志字典
     * @Author hsh
     * @return ZdEmpInmark 在院标志字典信息
     * @Date 2023/12/20 16:44
     */
    public ResultVo<List<ZdEmpInmark>> selectEmpInmark() {
        return ResultVoUtil.success(empInmarkDao.selectEmpInmark());
    }

    /**
     * @Description 保存在院标志字典
     * @Author hsh
     * @param zdEmpInmark 在院标志字典信息
     * @return map
     * @Date 2023/12/20 16:45
     */
    public ResultVo<Map<String, Object>> saveEmpInmark(ZdEmpInmark zdEmpInmark) {

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

        if (StringUtil.notBlank(zdEmpInmark.getCode()) && zdEmpInmark.getCode().length() > 1) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "在院标志字典编码仅限一个字符，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = zdEmpInmark.getCode();
        ZdEmpInmark mark = empInmarkDao.selectEmpInmarkByCode(code);

        // 根据名称生成新的拼音码和五笔码
        zdEmpInmark.setPyCode(PingYinUtils.pyShouZiMuDaXie(zdEmpInmark.getName()));
        zdEmpInmark.setDCode(PingYinUtils.getWBCode(zdEmpInmark.getName()));
        int num;
        try {
            if (null != mark) {
                num = empInmarkDao.updateEmpInmarkByCode(zdEmpInmark);
            } else {
                num = empInmarkDao.insert(zdEmpInmark);
            }
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存在院标志字典失败！");
            }
            resultMap.put("cg", "保存在院标志字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存在院标志字典失败！");
        }
    }

    /**
     * @Description 根据code删除在院标志字典
     * @Author hsh
     * @param code 在院标志字典编码
     * @return map
     * @Date 2023/12/20 16:46
     */
    public ResultVo<Map<String, Object>> delEmpInmarkByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有在院标志字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = empInmarkDao.deleteEmpInmarkByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除在院标志字典失败！");
            }
            resultMap.put("cg", "删除在院标志字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除在院标志字典失败！");
        }
    }

    /**
     * @Description 根据text查询职工信息
     * @Author hsh
     * @param text 关键字
     * @return EmployeeMiVo 职工信息
     * @Date 2023/12/25 10:51
     */
    public ResultVo<List<EmployeeMiVo>> selectEmployeeMi(String text) {
        return ResultVoUtil.success(employeeMiDao.selectEmployeeMiAll(text));
    }

    /**
     * @Description 根据code查询职工信息
     * @Author hsh
     * @param code 职工编码
     * @return EmployeeMi 职工信息
     * @Date 2023/12/26 10:39
     */
    public ResultVo<EmployeeMi> selectEmployeeMiByCode(String code) {
        return ResultVoUtil.success(employeeMiDao.selectEmployeeMiByCode(code));
    }

    /**
     * @Description 根据code删除职工信息
     * @Author hsh
     * @param code 职工编码
     * @return map
     * @Date 2023/12/26 10:54
     */
    public ResultVo<Map<String, Object>> delEmployeeMiByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有职工编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = employeeMiDao.deleteEmployeeMiByCode(code);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除职工字典失败！");
            }
            resultMap.put("cg", "删除职工字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除职工字典失败！");
        }
    }

    /**
     * @Description 根据code更新员工信息停用状态
     * @Author hsh
     * @param code 员工编码
     * @return map
     * @Date 2023/12/26 14:46
     */
    public ResultVo<Map<String, Object>> updateEmployeeMiDelFlagByCode(String code, String delFlag) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有职工编码，请检查！");
        }

        EmployeeMi emp = employeeMiDao.selectEmployeeMiByCode(code);
        if (null == emp) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, code + "的职工字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try {
            int num = employeeMiDao.updateEmployeeMiDelFlagByCode(code, delFlag);
            if (num == 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新职工字典停用或启用失败！");
            }
            resultMap.put("cg", "更新职工字典停用或启用成功！");
            return ResultVoUtil.success(resultMap);
        } catch (Exception e) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新职工字典停用或启用失败！");
        }

    }

    /**
     * @Description 根据text模糊查询，获取所有在职医生（字典用）
     * @Author hsh
     * @param text 模糊查询
     * @return CodeName 字典信息
     * @Date 2024/1/29 10:38
     */
    public ResultVo<List<CodeName>> selectEmployeeByCode(String text) {
        return ResultVoUtil.success(employeeMiDao.selectEmployeeByCode(text));
    }

}
