package thyyxxk.webserver.service.casefrontsheet;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.casefrontsheet.JieShouHuiZhenDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.casefrontsheet.JieShouHuiZhenPojo;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.*;

import java.util.List;

/**
 * <p>
 * 描述： 接受会诊申请
 * </p>
 *
 * @author xc
 * @date 2021-07-27 10:29
 */
@Service
@Slf4j
public class JieShouHuiZhenService {

    private final JieShouHuiZhenDao dao;
    private final PublicServer publicServer;

    @Autowired
    public JieShouHuiZhenService(JieShouHuiZhenDao dao, PublicServer publicServer) {
        this.dao = dao;
        this.publicServer = publicServer;
    }

    /**
     * @param deptCode    科室code
     * @param currentPage 当前页
     * @param pageSize    页大小
     * @param startTime   开始时间
     * @param endTime     接受时间
     * @return 返回没有会诊的信息
     */
    public ResultVo<Page<JieShouHuiZhenPojo>> getHuiZhenData(String deptCode, long currentPage, long pageSize, String startTime, String endTime) {
        List<String> deptList = publicServer.getKeShiLieBiaoList(deptCode);
        QueryWrapper<?> qw = new QueryWrapper<>();
        qw.eq("status_flag", "1")
                .ge("req_date", startTime)
                .le("req_date", endTime);
        if (ListUtil.notBlank(deptList)) {
            qw.and(QueryWrapper -> QueryWrapper.in("req_dept1", deptList).or().in("req_dept2", deptList));
        }
        Page<JieShouHuiZhenPojo> page = new Page<>(currentPage, pageSize);
        dao.getHuiZhenData(page, qw);
        return ResultVoUtil.success(page);
    }

    /**
     * @param deptCode 根据科室的编码来获取 科室的名字
     * @return 返回科室名字
     */
    public ResultVo<String> getDeptName(String deptCode) {
        return ResultVoUtil.success(dao.getDeptName(deptCode));
    }

    /**
     * @param admissTimes 住院次数
     * @param reqTimes    申请次数
     * @param inpatientNo 住院号
     * @return 返回具体信息
     */
    public ResultVo<JieShouHuiZhenPojo> getHuanZheXinXi(Integer admissTimes, Integer reqTimes, String inpatientNo) {
        JieShouHuiZhenPojo pojo = dao.getHuanZheXinXi(admissTimes, reqTimes, inpatientNo);
        if (pojo.getBirthDate() != null) {
            pojo.setAge(DateUtil.calculateAge(pojo.getBirthDate(),null));
        }
        return ResultVoUtil.success(pojo);
    }

    /**
     * @param param 更新的 主键 以及 更新的数据
     * @return 返回更新 提示
     */
    public ResultVo<String> wanChenHuiZhen(JieShouHuiZhenPojo param) {
        if (param.getAdmissTimes() != null && param.getReqTimes() != null && StringUtil.notBlank(param.getInpatientNo())) {
            if (StringUtil.isBlank(param.getHzComment())) {
                return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "会诊意见不能为空。");
            }
            param.setHzDoctor2(TokenUtil.getTokenUserId());
            dao.wanChenHuiZhen(param);
            log.info("完成会诊 ==》 操作人：{}，数据：{}", TokenUtil.getTokenUserId(), param);
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, param.getStatusFlag() == 1 ? "仅保存" : "完成会诊");
        }
        return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER);
    }
}
