package thyyxxk.webserver.service.zhuyuanyisheng;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
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.zhuyuanyisheng.ContagiousDiseaseDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.contagiousdisease.CrbMainCard;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ContagiousDiseaseService {

    private final ContagiousDiseaseDao dao;
    private final PublicServer publicServer;


    @Data
    public static class PatientDiagnosisIsCrb {
        private String patNo;
        private Integer times;
        private List<String> diagList;
    }

    @Autowired
    public ContagiousDiseaseService(ContagiousDiseaseDao dao, PublicServer publicServer) {
        this.dao = dao;
        this.publicServer = publicServer;
    }

    private ContagiousDiseaseService getThis() {
        return SpringUtil.getBean(this.getClass());
    }

    public ResultVo<List<CrbMainCard>> getContagiousDiseaseByPatNo(String patNo) {
        List<CrbMainCard> list = dao.seleceContagiousDisease(patNo);
        return ResultVoUtil.success(list);
    }

    public ResultVo<String> confirmCrb(CrbMainCard param) {
        param.setCardNo(publicServer.getCrbNo().toString());
        setTheValue(param);
        dao.insert(param);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "保存成功。");
    }

    private void setTheValue(CrbMainCard param) {
        Date date = new Date();
        String code = TokenUtil.getInstance().getTokenUserId();

        param.setReportOpera(code);
        param.setReportDate(date);
        param.setOpId(code);
        param.setInputDt(date);
        param.setZyMzFlag("1");
        param.setStatusFlag("1");
    }

    public ResultVo<String> updateCrb(CrbMainCard param) {
        setTheValue(param);
        QueryWrapper<CrbMainCard> qw = new QueryWrapper<>();

        qw.eq("card_no", param.getCardNo());
        dao.update(param, qw);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "修改成功。");
    }

    public ResultVo<String> delCrb(String patNo, Integer times, String cardNo) {
        QueryWrapper<CrbMainCard> qw = new QueryWrapper<>();
        qw.eq("patient_id", patNo)
                .eq("times", times)
                .eq("card_no", cardNo);
        dao.delete(qw);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
    }

    public Boolean notExistCrbByPatNo(String patNo, Integer times) {
        QueryWrapper<CrbMainCard> qw = new QueryWrapper<>();
        qw.eq("patient_id", patNo)
                .eq("times", times);
        return dao.selectCount(qw) < 1;
    }

    public ResultVo<JSONObject> infectiousDiseasesAreRequired(PatientDiagnosisIsCrb p) {
        JSONObject js = new JSONObject();

        js.put("fillOut", false);

        QueryWrapper<?> qw = new QueryWrapper<>();
        qw.eq("infectiousdiseases_flag", 1)
                .in("code", p.getDiagList());

        List<String> list = dao.selectCrbDiseList(qw);

        if (ListUtil.notBlank(list)) {
            if (notExistCrbByPatNo(p.getPatNo(), p.getTimes())) {
                js.replace("fillOut", true);
                js.put("list", list);
                return ResultVoUtil.success(js);
            }
        }

        return ResultVoUtil.success(js);
    }

}
