package thyyxxk.webserver.service.surgicalmanagement;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.surgicalmanagement.SurgeryArrangementDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.surgeryarrangement.ShouShuChaXun;
import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.OpRecord;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;

@Service
@Slf4j
public class SurgeryArrangementServer {
    private final SurgeryArrangementDao dao;

    public SurgeryArrangementServer(SurgeryArrangementDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<GetDropdownBox>> getASurgicalWard() {
        return ResultVoUtil.success(dao.getASurgicalWard());
    }

    public ResultVo<List<OpRecord>> huoQuShouShuAnPaiXinXi(ShouShuChaXun param) {
        QueryWrapper<?> qw = new QueryWrapper<>();
        qw.ge(param.getDateType(), param.getStartTime());
        qw.le(param.getDateType(), param.getEndTime());
        if (param.getJzFlag()) {
            qw.eq("urgent_clinic_flag", "1");
        }
        if (param.getStatus() != 4) {
            qw.eq("a.status", param.getStatus());
        }
        if (StringUtil.notBlank(param.getPatNo())) {
            qw.eq("a.inpatient_no", param.getPatNo());
        }
        if (StringUtil.notBlank(param.getWard())) {
            qw.eq("a.ward_code", param.getWard());
        }
        List<OpRecord> list = dao.huoQuShouShuAnPaiXinXi(qw);
        if (ListUtil.notBlank(list)) {
            return ResultVoUtil.success(list);
        }
        return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
    }

    public ResultVo<String> modifySurgeryTime(OpRecord param) {
        PublicServer.surgeryTimeLimit(param.getOpStartDate(), param.getOpEndDate(), param.getAnstStartDate(), param.getAnstEndDate());
        dao.modifySurgeryTime(param);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
    }
}
