package thyyxxk.webserver.service.crbmanagement;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.crbmanagement.CrbBaseDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.crbmanagement.BaseDept;
import thyyxxk.webserver.entity.crbmanagement.BaseUser;
import thyyxxk.webserver.entity.crbmanagement.EmrPatientInfo;
import thyyxxk.webserver.service.externalhttp.CrbUploadService;
import thyyxxk.webserver.utils.ResultVoUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CrbBaseService
 * @Author hsh
 * @Date 2025/7/28 0028 14:50
 * @Version 1.0
 * @Description 国家传染病上报前置机软件接口
 **/
@Service
@Slf4j
public class CrbBaseService {

    @Resource
    private CrbBaseDao dao;

    @Resource
    private CrbUploadService crbUploadService;

    @Value("${si-crb-url}")
    private String siCrbUrl;


    /**
     * @Description 医院信息系统用户信息表数据上传
     * @Author hsh
     * @param baseUser 系统用户信息表数据
     * @return JSONObject
     * @Date 2025/7/30 0030 10:34
     */
    public ResultVo<JSONObject> uploadBaseUser(BaseUser baseUser) {
        String orgCode = dao.selectOrgCodeByCode("orgCode");
        if(StringUtils.isBlank(baseUser.getOrgCode())){
            baseUser.setOrgCode(orgCode);
        }
        baseUser.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:dd"));
        JSONObject object = crbUploadService.uploadBaseUser(siCrbUrl, baseUser);
        boolean f = (boolean) object.get("result");
        if(!f){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, errInfoTips(object));
        }
        return ResultVoUtil.success(object);
    }

    /**
     * @Description 医院信息系统科室信息数据上传
     * @Author hsh
     * @param baseDept 科室信息数据
     * @return JSONObject
     * @Date 2025/7/30 0030 16:06
     */
    public ResultVo<JSONObject> uploadBaseDept(BaseDept baseDept) {
        baseDept.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:dd"));
        JSONObject object = crbUploadService.uploadBaseDept(siCrbUrl, baseDept);
        boolean f = (boolean) object.get("result");
        if(!f){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, errInfoTips(object));
        }
        return ResultVoUtil.success(object);
    }

    /**
     * @Description 获取医院信息系统用户信息（所有参与诊疗活动的用户）
     * @Author hsh
     * @param baseUser 参数
     * @return list
     * @Date 2025/7/29 0029 11:10
     */
    public ResultVo<List<BaseUser>> selectBaseUser(BaseUser baseUser) {
        String orgCode = dao.selectOrgCodeByCode("orgCode");
        QueryWrapper<BaseUser> qw = new QueryWrapper<>();
        qw.like("mi.name", baseUser.getUserName());
        if(StringUtils.isNotEmpty(baseUser.getLoginName())){
            qw.eq("mi.code_rs", baseUser.getLoginName());
        }
        if(StringUtils.isNotEmpty(baseUser.getIdCard())){
            qw.eq("mi.social_no", baseUser.getIdCard());
        }
        return ResultVoUtil.success(dao.selectBaseUser(orgCode, qw));
    }

    /**
     * @Description 获取全部院内科室信息
     * @Author hsh
     * @param baseDept 参数
     * @return list 院内科室信息
     * @Date 2025/7/29 0029 11:09
     */
    public ResultVo<List<BaseDept>> selectBaseDept(BaseDept baseDept) {
        QueryWrapper<BaseDept> qw = new QueryWrapper<>();
        qw.like("name", baseDept.getDeptName());
        if(StringUtils.isNotEmpty(baseDept.getDeptCode())){
            qw.eq("code", baseDept.getDeptCode());
        }
        if(StringUtils.isNotEmpty(baseDept.getTargetDeptCode())){
            qw.eq("si_caty", baseDept.getTargetDeptCode());
        }
        if(StringUtils.isNotEmpty(baseDept.getTargetDeptName())){
            qw.like("si_caty_name", baseDept.getTargetDeptName());
        }
        return ResultVoUtil.success(dao.selectBaseDept(qw));
    }

    /**
     * @Description 患者基本信息表数据上传
     * @Author hsh
     * @param emrPatientInfo 患者基本信息表数据
     * @return JSONObject
     * @Date 2025/7/30 0030 16:51
     */
    public ResultVo<JSONObject> uploadEmrPatientInfo(EmrPatientInfo emrPatientInfo) {
        JSONObject object = crbUploadService.uploadEmrPatientInfo(siCrbUrl, emrPatientInfo);
        boolean f = (boolean) object.get("result");
        if(!f){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, errInfoTips(object));
        }
        return ResultVoUtil.success(object);
    }

    /**
     * @Description 错误提示信息返回
     * @Author hsh
     * @param object 返回json
     * @return String 错误提示信息
     * @Date 2025/7/30 0030 16:47
     */
    private String errInfoTips(JSONObject object) {
        String errorName = object.getString("errorName");
        String desc = object.getString("desc");
        return errorName + ": " + desc;
    }
    
}
