package thyyxxk.webserver.controller.jiekou;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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.RestController;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.jiekou.FluorescenceTestDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.fluorescenceTest.FluorescenceSpecimenResult;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.Date;

@RestController
@RequestMapping("/fluorescenceTest")
@Slf4j
public class FluorescenceTestController {
    private final FluorescenceTestDao dao;

    public FluorescenceTestController(FluorescenceTestDao dao) {
        this.dao = dao;
    }

    /**
     * 荧光检验保存的接口
     *
     * @param js js
     * @return 提示
     */
    @PostMapping("/save")
    @PassToken
    public ResultVo<String> save(@RequestBody JSONObject js) {
        JSONObject personInfo = js.getJSONObject("ReportPersonInfo");
        String caseStr = personInfo.getString("Case");
        if (StringUtil.isBlank(caseStr)) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "病例号为空无法保存。");
        }
        Integer id = Integer.valueOf(caseStr);
        // 查询门诊患者
        Patient p = dao.selectPatInfoMz(id);
        // 查询住院患者
        p = p == null ? dao.selectPatInfoByReqNo(id) : p;
        if (p == null) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "未查询到患者信息。");
        }
        Integer count = dao.selectCountById(id, p.getInpatientNo(), p.getAdmissTimes());
        if (count > 0) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请勿重复上传。");
        }
        FluorescenceSpecimenResult f = new FluorescenceSpecimenResult();
        f.setHospitalName(personInfo.getString("HospitalName"))
                .setId(id)
                .setSample(personInfo.getString("Sample"))
                .setSpecimen(personInfo.getString("Specimen"))
                .setPatientName(personInfo.getString("PatientName"))
                .setPatientAge(Integer.getInteger(personInfo.getString("PatientAge")))
                .setDiagnosis(personInfo.getString("Diagnosis"))
                .setOffice(personInfo.getString("Office"))
                .setDoctor(personInfo.getString("Doctor"))
                .setSendTime(strToDate(personInfo.getString("SendTime")))
                .setProofer(personInfo.getString("Proofer"))
                .setTestTime(strToDate(personInfo.getString("TestTime")))
                .setReporter(personInfo.getString("Reporter"))
                .setReportTime(strToDate(personInfo.getString("ReportTime")))
                .setBedNo(personInfo.getString("BedNo"))
                .setPatNo(p.getInpatientNo())
                .setTimes(p.getAdmissTimes());
        js.remove("ReportPersonInfo");
        f.setOutcome(js.toString());
        dao.insert(f);
        return ResultVoUtil.success();
    }


    private static Date strToDate(String str) {
        if (StringUtil.notBlank(str)) {
            str = str.replace("T", " ");
            return DateUtil.parse(str);
        }
        return null;
    }
}
