package com.emr.controller;

import com.emr.dao.CommomMapper;
import com.emr.dao.Emr_DictionaryMapper;
import com.emr.dao.emrCode.T_DiagMapper;
import com.emr.dao.emrCode.T_OperateMapper;
import com.emr.dao.tBoby.TBabyMapper;
import com.emr.dao.tTumour.TTumourChemotherapyMapper;
import com.emr.entity.Emr_Dictionary;
import com.emr.entity.Power_User;
import com.emr.entity.ResultUtil;
import com.emr.entity.emrCode.T_Diag;
import com.emr.entity.emrCode.T_Operate;
import com.emr.entity.tBoby.TBaby;
import com.emr.entity.tTumour.TTumourChemotherapy;
import com.emr.service.CommomService;
import com.emr.service.his.HisService;
import com.emr.vo.User;
import com.emr.vo.commomSearch.CommomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2020/1/14 17:16
 * @UpdateUser: 曾文和
 * @UpdateDate: 2020/1/14 17:16
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
@RequestMapping("his/")
public class HisController {
    @Autowired
    private HisService hisService;
    @Autowired
    private CommomMapper commomMapper;
    @Autowired
    private T_DiagMapper diagMapper;
    @Autowired
    private T_OperateMapper operateMapper;
    @Autowired
    private TBabyMapper babyMapper;
    @Autowired
    private TTumourChemotherapyMapper tumourChemotherapyMapper;
    @Autowired
    private Emr_DictionaryMapper dictionaryMapper;
    @Autowired
    private CommomService commomService;
    @PostMapping("saveProvincial")
    @ResponseBody
    public ResultUtil saveProvincial(String patientId, HttpServletRequest request){
        try {
            //查询基本信息
            String sql = "SELECT commomtable.*,commomtable1.*,t_charge.case_type FROM commomtable LEFT JOIN commomtable1 ON commomtable.patient_id = commomtable1.patient_id LEFT JOIN t_charge on commomtable.patient_id = t_charge.patient_id" +
                    " WHERE commomtable.patient_id = '"+ patientId +"'";
            List<CommomVo> commomVos = commomMapper.selectAll(sql);
            if(null != commomVos && !commomVos.isEmpty()){
                //查询一对多诊断记录
                List<T_Diag> diags = diagMapper.selectAllByPatientId(patientId, null);
                //查询一对多手术记录
                List<T_Operate> operates = operateMapper.selectAllByPatientId(patientId, null);
                //查询一对多婴儿记录
                List<TBaby> babies = babyMapper.selectAllByPatientId(patientId, null);
                //查询一对多化疗记录
                List<TTumourChemotherapy> tTumourChemotherapies = tumourChemotherapyMapper.selectByPatientId(patientId);
                //查询相关字典
                String typeCodes = "'sex_code','country','nation_code','marital_status','dept_code','quality_level','idDeadPsy_code','blood_type','rh_code','recordType_code'," +
                        "'job','admiss_type','clinicalRecord_code','isDrugAllergy_code','zd_dis_type','isAdmissPlan_code','pay_type','admiss_thing','cut_code','heal_code','anaesthesia','whether','operate_class','childbirthResult_code','childStatus_code','breath_code','radiotherapy_type_code','radiotherapy_program_code'," +
                        "'radiotherapy_device_code','chemotherapy_type_code','chemotherapy_method_code','curative_effect_code','cut_heal_grade'";
                List<Emr_Dictionary> dictionarys = dictionaryMapper.getDictionaryByTypeCodes(typeCodes);
                //查询用户列表
                Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
                List<User> userList = commomService.getUserList(user.getUserName(), request);
                CommomVo commomVo = commomVos.get(0);
                //插入HIS_BA1基本信息表
                hisService.insertHisBa1(commomVo,dictionarys,userList,babies,operates.size());
                //插入HIS_BA6 HIS_病人转科情况
                hisService.insertHisBa2(commomVo,dictionarys);
                //插入HIS_BA3 病人诊断信息
                hisService.insertHisBa3(commomVo,dictionarys,diags);
                //插入HIS_BA4 病人手术信息
                hisService.insertHisBa4(commomVo,dictionarys,operates,userList);
                //插入HIS_BA5 妇婴卡
                hisService.insertHisBa5(commomVo,dictionarys,babies);
                //插入HIS_BA6 肿瘤卡
                hisService.insertHisBa6(commomVo,dictionarys);
                //插入HIS_BA7 化疗卡
                hisService.insertHisBa7(commomVo,dictionarys,tTumourChemotherapies);
            }
            return ResultUtil.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error("保存出错,请联系系统管理员!");
        }
    }
}
