package thyyxxk.webserver.controller.zygl;

import cn.hutool.core.convert.Convert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.executeItem.NumberEnum;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.inpatient.charge.Windows;
import thyyxxk.webserver.entity.zygl.APatientMiVo;
import thyyxxk.webserver.service.zygl.AdmissionRegistrationService;
import thyyxxk.webserver.entity.zygl.PatientMiSfz;
import thyyxxk.webserver.service.zygl.RegionService;
import thyyxxk.webserver.service.zygl.ZyActpatientService;
import thyyxxk.webserver.entity.zygl.ZyXnhRecord;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.CommonUtil;
import thyyxxk.webserver.utils.IpAddressUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description: 入院登记
 * @Author:lihong
 * @Date: 2025/5/6
 */
@RequestMapping("/admissionRegistration")
@RestController
public class AdmissionRegistrationController {
    @Resource
    private AdmissionRegistrationService service;
    @Resource
    private ZyActpatientService zyActpatientService;
    @Resource
    private RegionService regionService;

    /**
     * @description: 婚姻
     * @author: lihong
     * @date: 2025/5/6 17:25
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryMarital")
    public ResultVo<List<Map<String, Object>>> queryMarital() {
        return ResultVoUtil.success(service.selectMarital());
    }

    /**
     * @description: 证件类型
     * @author: lihong
     * @date: 2025/5/6 17:25
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryPsnCertType")
    public ResultVo<List<Map<String, Object>>> queryPsnCertType() {
        return ResultVoUtil.success(service.selectPsnCertType());
    }

    /**
     * @description: 病区
     * @author: lihong
     * @date: 2025/5/6 17:24
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryAllInpatientWard")
    public ResultVo<List<Map<String, Object>>> queryAllInpatientWard() {
        return ResultVoUtil.success(service.selectAllInpatientWard());
    }

    /**
     * @description: 医生
     * @author: lihong
     * @date: 2025/5/6 17:24
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryDoctor")
    public ResultVo<List<Map<String, Object>>> queryDoctor() {
        return ResultVoUtil.success(service.selectEmployeeMi());
    }

    /**
     * @description: 国家
     * @author: lihong
     * @date: 2025/5/6 17:24
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryCountry")
    public ResultVo<List<Map<String, Object>>> queryCountry() {
        return ResultVoUtil.success(service.selectCountry());
    }

    /**
     * @description: 民族
     * @author: lihong
     * @date: 2025/5/6 17:24
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryNation")
    public ResultVo<List<Map<String, Object>>> queryNation() {
        return ResultVoUtil.success(service.selectNation());
    }

    /**
     * @description: 关系
     * @author: lihong
     * @date: 2025/5/6 17:24
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryRelation")
    public ResultVo<List<Map<String, Object>>> queryRelation() {
        return ResultVoUtil.success(service.selectRelation());
    }

    /**
     * @description: 职业
     * @author: lihong
     * @date: 2025/5/6 17:23
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryOccupation")
    public ResultVo<List<Map<String, Object>>> queryOccupation() {
        return ResultVoUtil.success(service.selectOccupation());
    }

    /**
     * @description:入院状态
     * @author: lihong
     * @date: 2025/5/6 17:23
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryAdmissStatus")
    public ResultVo<List<Map<String, Object>>> queryAdmissStatus() {
        return ResultVoUtil.success(service.selectAdmissStatus());
    }

    /**
     * @description:查询
     * @author: lihong
     * @date: 2025/5/6 17:21
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryZyResponceType")
    public ResultVo<List<Map<String, Object>>> queryZyResponceType() {
        return ResultVoUtil.success(service.selectZyResponceType());
    }

    /**
     * @description: 查询省
     * @author: lihong
     * @date: 2025/5/6 17:21
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryProvince")
    public ResultVo<List<Map<String, Object>>> queryProvince() {
        return ResultVoUtil.success(service.selectProvince());
    }

    /**
     * @description: 查询下级市 区
     * @author: lihong
     * @date: 2025/5/6 17:21
     * @param: code
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryRegionByParentCode")
    public ResultVo<List<Map<String, Object>>> queryRegionByParentCode(String code) {
        return ResultVoUtil.success(service.selectRegionByParentCode(code));
    }

    /**
     * @description: 查询小科室
     * @author: lihong
     * @date: 2025/5/7 11:40
     * @param: code
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/querySmallDeptByCode")
    public ResultVo<List<Map<String, Object>>> querySmallDeptByCode(String code) {
        return ResultVoUtil.success(service.querySmallDeptByCode(code));
    }

    /**
     * @description: 查询床
     * @author: lihong
     * @date: 2025/5/7 14:27
     * @param: code
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryBedNumByCode")
    public ResultVo<List<Map<String, Object>>> queryBedNumByCode(String code) {
        return ResultVoUtil.success(service.queryBedNumByCode(code));
    }

    /**
     * @description: 查询病人 基本信息和 住院证信息
     * @author: lihong
     * @date: 2025/5/7 15:13
     * @param: inpatientNo
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     **/
    @GetMapping("/queryPatientAllInfoByInpatientNo")
    public ResultVo<APatientMiVo> queryPatientAllInfoByInpatientNo(String inpatientNo) {
        return ResultVoUtil.success(service.queryPatientAllInfoByInpatientNo(inpatientNo));
    }

    @GetMapping("/queryPatientInfoByKeyWard")
    public ResultVo<List<APatientMiVo>> queryPatientInfoByKeyWard(String keyWard) {
        return ResultVoUtil.success(service.queryPatientInfoByKeyWard(keyWard));
    }

    /**
     * @description: 保存患者 住院档案信息
     * @author: lihong
     * @date: 2025/5/8 9:55
     * @param: aPatientMiVo
     * @return: thyyxxk.webserver.entity.ResultVo<thyyxxk.webserver.entity.zygl.APatientMi>
     **/
    @PostMapping("/saveAptientMi")
    public ResultVo<APatientMiVo> saveAptientMi(@RequestBody APatientMiVo aPatientMiVo) {
        return ResultVoUtil.success(service.saveAptientMi(aPatientMiVo));
    }

    /**
     * @description: 保存入院信息
     * @author: lihong
     * @date: 2025/5/8 9:58
     * @param: aPatientMiVo
     * @return: thyyxxk.webserver.entity.ResultVo<thyyxxk.webserver.entity.inpatient.ZyActpatient>
     **/
    @PostMapping("/saveZyPatient")
    public ResultVo<String> saveZyPatient(@RequestBody ZyActpatient zyActpatient) {
        int num = zyActpatientService.saveZyActpatientFull(zyActpatient);
        if (num > 0) {
            zyActpatientService.createDoorPatient(zyActpatient.getInpatientNo());
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "保存入院信息成功");
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存入院信息失败");
    }

    @GetMapping("/deleteAdmissInfo")
    public ResultVo<List<APatientMiVo>> deleteAdmissInfo(String inpatientNo) {
        ZyActpatient zyActpatient = new ZyActpatient();
        zyActpatient.setInpatientNo(inpatientNo);
        zyActpatientService.deleteZyActpatient(zyActpatient);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "删除入院登记信息成功");
    }

    @GetMapping("/createDoorPatient")
    public ResultVo createDoorPatient(String inpatientNo) {
        return zyActpatientService.createDoorPatient(inpatientNo);
    }



    /**
     * @description: 获取窗口信息
     * @author: lihong
     * @date: 2025/5/8 17:15
     * @param: inpatientNo
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < thyyxxk.webserver.entity.zygl.APatientMiVo>>
     **/
    @GetMapping("/queryLastWindowsIpAddress")
    public ResultVo<Windows> queryLastWindowsIpAddress(HttpServletRequest request) {
        String ipAddress = IpAddressUtil.getIPAddress(request);
        return service.queryLastWindowsIpAddress(ipAddress);
    }

    @GetMapping("/getMzZdDeptNos")
    public ResultVo getMzZdDeptNos() {
        return service.getMzZdDeptNos();
    }

    /**
     * @description: 保存打印机设置
     * @author: lihong
     * @date: 2025/7/3 9:33
     * @param: windows
     * @param: request
     * @return: thyyxxk.webserver.entity.ResultVo<java.lang.String>
     **/
    @PostMapping("/saveWindowPrint")
    public ResultVo<String> saveWindowPrint(@RequestBody Windows windows,HttpServletRequest request) {
        String ipAddress = IpAddressUtil.getIPAddress(request);
        windows.setIpAddress(ipAddress);
        return service.saveWindowPrint(windows);
    }



    @PostMapping("/querySfzOrHkb")
    public ResultVo querySfzOrHkb(@RequestBody Map<String, Object> param) {
        Integer type = Convert.toInt(param.get("type"));
        String patientId = Convert.toStr(param.get("patientId"));
        AssertUtil.isnotBlank(patientId, "门诊号/住院号不能为空");
        byte[] base64Data = service.querySfzOrHkb(type, patientId);
        if (base64Data == null) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(base64Data);
    }

    @PostMapping("/saveUpdatePatientId")
    public ResultVo<String> saveUpdatePatientId(@RequestBody APatientMiVo aPatientMiVo) {
        service.saveUpdatePatientId(aPatientMiVo.getInpatientNo(), aPatientMiVo.getPatientId());
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE,"修改门诊号成功");
    }


    @PostMapping("/hkbUploadFile")
    public ResultVo hkbUploadFile(@RequestParam("file") MultipartFile file, @RequestParam("patNo")String patNo) {
        try {
            byte[] bytes = CommonUtil.image2byte(file.getInputStream());
            ZyXnhRecord zyXnhRecord = new ZyXnhRecord();
            zyXnhRecord.setInpatientNo(patNo);
            zyXnhRecord.setAdmissTimes(1);
            zyXnhRecord.setFileXnh(bytes);
            zyXnhRecord.setItemNo(NumberEnum.ONE.getCode());
            zyXnhRecord.setResult(NumberEnum.ZERO.getCode());
            zyXnhRecord.setLedgerSn(1);
            service.saveZyXnhRecord(zyXnhRecord);
        }catch (Exception e){
            ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "上传户口本报错"+e.getMessage());
        }
        return ResultVoUtil.success();
    }
    @PostMapping("/saveSfzPatient")
    public ResultVo saveSfzPatient(@RequestBody PatientMiSfz patientMiSfz){
        service.saveSfzPatient(patientMiSfz);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "上传身份证成功");
    }


    @PostMapping("/queryFullRegionFromSfzAddress")
    public ResultVo queryFullRegionFromSfzAddress(@RequestBody APatientMiVo aPatientMiVo){
        Map<String, String> addressResolution = CommonUtil.addressResolution(aPatientMiVo.getAddressDetail());
        Map<String, Object> addressCodes = regionService.getAddressCodes(addressResolution);
        addressCodes.put("detail",addressResolution.get("detail"));
        return ResultVoUtil.success(addressCodes);
    }

}
