package thyyxxk.webserver.service.dictionary;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.BedDeptDictDao;
import thyyxxk.webserver.entity.ResultVo;
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;

/**
 * @ClassName BedDeptDictService
 * @Author hsh
 * @Date 2024/9/9 16:01
 * @Version 1.0
 * @Description 床位（科室）字典维护
 **/
@Service
@Slf4j
public class BedDeptDictService {

    private final BedDeptDictDao dao;

    public BedDeptDictService(BedDeptDictDao dao) {
        this.dao = dao;
    }

    /**
     * @Description 查询科室标准床位信息
     * @Author hsh
     * @param text 关键字
     * @return list
     * @Date 2024/9/12 15:01
     */
    public ResultVo<List<Map<String, Object>>> selectDeptInfoForBed(String text){
        return ResultVoUtil.success(dao.selectDeptInfoForBed(text));
    }

    /**
     * @Description 根据病房id查询床位基本信息
     * @Author hsh
     * @param wardCode 病房id
     * @return list
     * @Date 2024/9/12 15:02
     */
    public ResultVo<List<Map<String, Object>>> selectBedInfoForDept(String wardCode){
        return ResultVoUtil.success(dao.selectBedInfoForDept(wardCode));
    }

    /**
     * @Description 查询空调费/床位费项目信息
     * @Author hsh
     * @param text 关键字
     * @return list
     * @Date 2024/9/12 15:03
     */
    public ResultVo<List<Map<String, Object>>> selectAirItemInfo(String text){
        List<Map<String, Object>> list = dao.selectAirItemInfo(text);
        return ResultVoUtil.success(list);
    }

    /**
     * @Description 更新科室标准床位数
     * @Author hsh
     * @param deptInfoList 科室标准床位数
     * @return map
     * @Date 2024/9/14 12:06
     */
    public ResultVo<Map<String, Object>> updateDeptInfoForBed(List<Map<String, Object>> deptInfoList){
        Map<String, Object> map = new HashMap<>();
        int n = dao.updateDeptInfoForBed(deptInfoList);
        if(n > 0){
            map.put("cg", "更新科室标准床位数成功！");
            return ResultVoUtil.success(map);
        } else {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新科室标准床位数失败，请检查！");
        }
    }

    /**
     * @Description 根据科室id更新科室床位信息
     * @Author hsh
     * @param bedInfoList 科室床位信息
     * @return map
     * @Date 2024/9/18 10:35
     */
    public ResultVo<Map<String, Object>> updateBedInfoForDept(List<Map<String, Object>> bedInfoList){
        Map<String, Object> map = new HashMap<>();
        int n = dao.updateBedInfoForDept(bedInfoList);
        if(n > 0){
            map.put("cg", "更新科室床位信息成功！");
            return ResultVoUtil.success(map);
        } else {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新科室床位信息失败，请检查！");
        }
    }

    /**
     * @Description 新增科室床位信息
     * @Author hsh
     * @param bedInfo 科室床位信息
     * @return map
     * @Date 2024/9/19 16:23
     */
    public ResultVo<Map<String, Object>> saveBedForDept(Map<String, Object> bedInfo){
        Map<String, Object> map = new HashMap<>();
        String wardCode = String.valueOf(bedInfo.get("wardCode"));
        String deptCode = String.valueOf(bedInfo.get("deptCode"));
        String bedNo = String.valueOf(bedInfo.get("bedNo"));
        if(StringUtil.isBlank(wardCode) || StringUtil.isBlank(deptCode) || StringUtil.isBlank(bedNo)){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "科室，病区或者床号为空，请检查！");
        }
        int num = dao.selectBedInfoForDeptAndBedNo(wardCode, deptCode, bedNo);
        if(num > 0){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, bedNo + "此床号已存在，请检查！");
        }
        int n = dao.saveBedForDeptAndBedNo(bedInfo);
        if(n > 0){
            map.put("cg", "新增科室床位信息成功！");
            return ResultVoUtil.success(map);
        } else {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "新增科室床位信息失败，请检查！");
        }
    }

    /**
     * @Description 查询病房科室字典
     * @Author hsh
     * @param text 关键字
     * @return list
     * @Date 2024/9/20 16:02
     */
    public ResultVo<List<Map<String, Object>>> selectRoomDept(String text){
        return ResultVoUtil.success(dao.selectDeptInfoForBed(text));
    }

    /**
     * @Description 更新病房科室字典
     * @Author hsh
     * @param roomDept 病房科室字典
     * @return map
     * @Date 2024/9/20 16:03
     */
    public ResultVo<Map<String, Object>> saveRoomDept(Map<String, Object> roomDept){
        String wardCode = String.valueOf(roomDept.get("wardCode"));
        String deptCode = String.valueOf(roomDept.get("deptCode"));
        String wardName = String.valueOf(roomDept.get("wardName"));
        String deptName = String.valueOf(roomDept.get("deptName"));
        if(StringUtil.isBlank(wardCode) || StringUtil.isBlank(deptCode)){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "科室或者病区为空，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> room = dao.selectRoomDeptByCode(wardCode, deptCode);
        // 根据名称生成新的拼音码和五笔码
        roomDept.put("wardPyCode", PingYinUtils.pyShouZiMuDaXie(wardName).length() > 8 ? PingYinUtils.pyShouZiMuDaXie(wardName).substring(0,8) : PingYinUtils.pyShouZiMuDaXie(wardName));
        roomDept.put("wardDCode", PingYinUtils.getWBCode(wardName).length() > 8 ? PingYinUtils.getWBCode(wardName).substring(0,8) : PingYinUtils.getWBCode(wardName));
        roomDept.put("deptPyCode", PingYinUtils.pyShouZiMuDaXie(deptName).length() > 8 ? PingYinUtils.pyShouZiMuDaXie(deptName).substring(0,8) : PingYinUtils.pyShouZiMuDaXie(deptName));
        roomDept.put("deptDCode", PingYinUtils.getWBCode(deptName).length() > 8 ? PingYinUtils.getWBCode(deptName).substring(0,8) : PingYinUtils.getWBCode(deptName));
        int num;
        try{
            if(null != room){
                num = dao.updateRoomDeptByCode(roomDept);
            } else {
                num = dao.insertRoomDept(roomDept);
            }
            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 根据病房id，科室id删除病房科室字典
     * @Author hsh
     * @param wardCode 病房id
     * @param deptCode 科室id
     * @return map
     * @Date 2024/9/20 16:03
     */
    public ResultVo<Map<String, Object>> delRoomDeptByCode(String wardCode, String deptCode) {
        if (StringUtil.isBlank(wardCode) || StringUtil.isBlank(deptCode)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "科室或者病区为空，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.deleteRoomDeptByCode(wardCode, deptCode);
            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
     * @param text 关键字
     * @return list
     * @Date 2024/9/20 16:05
     */
    public ResultVo<List<Map<String, Object>>> selectSmallDept(String text){
        return ResultVoUtil.success(dao.selectSmallDept(text));
    }

    /**
     * @Description 更新小科室字典
     * @Author hsh
     * @param smallMap 小科室字典
     * @return map
     * @Date 2024/9/20 16:05
     */
    public ResultVo<Map<String, Object>> saveSmallDept(Map<String, Object> smallMap){
        String dept = String.valueOf(smallMap.get("dept"));
        String smallDept = String.valueOf(smallMap.get("smallDept"));
        Integer deptId = (Integer) smallMap.get("deptId");
        if(StringUtil.isBlank(dept) || StringUtil.isBlank(smallDept)){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "小科室或者病区为空，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> small = dao.selectSmallDeptByCode(deptId);
        int nn = dao.selectSmallDeptByDept(dept, smallDept);
        if(null == small && nn > 0){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "存在相同的大科室-小科室关系，不能新增！");
        }
        int num;
        try{
            if(null != small){
                num = dao.updateSmallDeptByCode(smallMap);
            } else {
                num = dao.insertSmallDept(smallMap);
            }
            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
     * @param deptId 科室序列号
     * @return map
     * @Date 2024/9/20 16:05
     */
    public ResultVo<Map<String, Object>> delSmallDeptByCode(String deptId) {
        if (StringUtil.isBlank(deptId)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "科室序列号为空，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.deleteSmallDeptByCode(deptId);
            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, "删除小科室字典失败！");
        }
    }

}
