package thyyxxk.webserver.service.surgicalmanagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.surgicalmanagement.SurgeryRecordDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.surgeryarrangement.BriefOpRecord;
import thyyxxk.webserver.entity.surgeryarrangement.QuerySurgeryParams;
import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.OpRecord;
import thyyxxk.webserver.service.hutoolcache.DeptCache;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.service.inpatient.casefrontsheet.VerifyCaseFrontSheet;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

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

@Service
public class SurgeryRecordService {
    private final SurgeryRecordDao dao;
    private final DeptCache deptCache;
    private final UserCache userCache;

    @Autowired
    public SurgeryRecordService(SurgeryRecordDao dao, DeptCache deptCache, UserCache userCache) {
        this.dao = dao;
        this.deptCache = deptCache;
        this.userCache = userCache;
    }

    public ResultVo<Map<String, List<CodeName>>> getDicList() {
        Map<String, List<CodeName>> map = new HashMap<>();
        map.put("anstWays", dao.selectAnstWays());
        map.put("srgyParts", dao.selectSurgeryParts());
        map.put("allRooms", dao.selectSurgeryRooms());
        map.put("allWards", dao.getAllSurgicalWard());
        map.put("allStaffs", dao.selectSurgeryRoomStaffs());
        return ResultVoUtil.success(map);
    }

    public ResultVo<List<BriefOpRecord>> fetchBriefOpRecordList(QuerySurgeryParams params) {
        QueryWrapper<?> qw = new QueryWrapper<>();
        qw.ge("op_datetime", params.getStartTime());
        qw.le("op_datetime", params.getEndTime());
        if (!params.getStatus().equals("0")) {
            qw.eq("status", params.getStatus());
        }
        if (StringUtil.notBlank(params.getPatNo())) {
            qw.eq("inpatient_no", params.getPatNo());
        }
        if (StringUtil.notBlank(params.getWard())) {
            qw.eq("ward_code", params.getWard());
        }
        List<BriefOpRecord> list = dao.selectBriefOpRecordList(qw);
        if (ListUtil.isBlank(list)) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        list.forEach(item -> item.setWardName(deptCache.getDeptName(item.getWardCode())));
        return ResultVoUtil.success(list);
    }

    public ResultVo<OpRecord> fetchSurgeryRecord(Integer recordId) {
        OpRecord opRecord = dao.selectOpRecord(recordId);
        opRecord.setWardName(deptCache.getDeptName(opRecord.getWardCode()));
        opRecord.setApplyDocName(userCache.getEmployeeName(opRecord.getApplyDoc()));
        opRecord.setDoctorZdName(userCache.getEmployeeName(opRecord.getDoctorZd()));
        opRecord.setDoctor1Name(userCache.getEmployeeName(opRecord.getDoctor1()));
        opRecord.setDoctor2Name(userCache.getEmployeeName(opRecord.getDoctor2()));
        opRecord.setDoctor3Name(userCache.getEmployeeName(opRecord.getDoctor3()));
        opRecord.setNurseXhName(userCache.getEmployeeName(opRecord.getNurseXh()));
        opRecord.setNurseQxName(userCache.getEmployeeName(opRecord.getNurseQx()));
        opRecord.setDoctorMzName(userCache.getEmployeeName(opRecord.getDoctorMz()));
        opRecord.setDoctorMzYsName(userCache.getEmployeeName(opRecord.getDoctorMzYs()));
        opRecord.setDoctorMzHsName(userCache.getEmployeeName(opRecord.getDoctorMzHs()));
        return ResultVoUtil.success(opRecord);
    }

    public ResultVo<String> saveSurgeryRecord(OpRecord record) {
        if (null == record.getOpStartDate() || null == record.getOpEndDate()) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "手术开始时间和手术结束时间不能为空。");
        }
        if (StringUtil.isBlank(record.getOpScale())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "手术级别不能为空。");
        }
        if (record.getOpScale().equals("1") || record.getOpScale().equals("2")) {
            if (StringUtil.isBlank(record.getHocusCode()) || record.getHocusCode().equals("29")) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "三、四级手术麻醉方法不能为空。");
            }
        }
        if (StringUtil.notBlank(record.getHocusCode()) && !record.getHocusCode().equals("23") && !record.getHocusCode().equals("29")) {
            if (StringUtil.isBlank(record.getDoctorMzYs())) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "麻醉医生不能为空。");
            }
            if (null == record.getAnstStartDate() || null == record.getAnstEndDate()) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "麻醉开始时间和麻醉结束时间不能为空。");
            }
        }
        List<String> list = VerifyCaseFrontSheet.logicalSurgeryTime(null,null,
                record.getOpStartDate(), record.getOpEndDate(), record.getAnstStartDate(), record.getAnstEndDate());
        if (list.isEmpty()) {
            dao.updateRecord(record);
            return ResultVoUtil.success("保存成功");
        }
        StringBuilder builder = new StringBuilder();
        for (String message : list) {
            builder.append(message);
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, builder.toString());
    }
}
