package com.emr.service.tTumour;

import com.emr.dao.CommomMapper;
import com.emr.dao.tTumour.TTumourChemotherapyMapper;
import com.emr.entity.Power_User;
import com.emr.entity.tTumour.TTumour;
import com.emr.entity.tTumour.TTumourChemotherapy;
import com.emr.service.CommomService;
import com.emr.vo.commomSearch.CommomVo;
import com.emr.vo.tTumourVo.TTumourVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2020/1/6 16:42
 * @UpdateUser: 曾文和
 * @UpdateDate: 2020/1/6 16:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class TTumourService {
    @Autowired
    private TTumourChemotherapyMapper tumourChemotherapyMapper;
    @Autowired
    private CommomMapper commomMapper;
    @Autowired
    private CommomService commomService;
    public void saveTumour(TTumourVo tumourVo, HttpServletRequest request){
        String patientId = tumourVo.getPatientId();
        CommomVo commomVo = new CommomVo();
        BeanUtils.copyProperties(tumourVo,commomVo);
        //更新
        commomMapper.updateByPrimaryKeySelective1(commomVo);
        //操作化疗表
        //先删除化疗数据
        tumourChemotherapyMapper.deleteByPatientId(patientId);
        //定义批量添加化疗数据集合
        List<TTumourChemotherapy> list = new ArrayList<>();
        //分割数据
        String[] startTimes = tumourVo.getStartTimes().split(",");
        String[] endTimes = tumourVo.getEndTimes().split(",");
        String[] drugNameAndDoses = tumourVo.getDrugNameAndDoses().split(",");
        String[] treatmentCourses = tumourVo.getTreatmentCourses().split(",");
        String[] curativeEffects = tumourVo.getCurativeEffects().split(",");
        for (int i = 0; i < startTimes.length; i++) {
            if((!"null".equals(startTimes[i]) && StringUtils.isNotBlank(startTimes[i])) ||
                    (!"null".equals(endTimes[i]) && StringUtils.isNotBlank(endTimes[i])) ||
                    (!"null".equals(drugNameAndDoses[i]) && StringUtils.isNotBlank(drugNameAndDoses[i])) ||
                    (!"null".equals(treatmentCourses[i]) && StringUtils.isNotBlank(treatmentCourses[i])) ||
                    (!"null".equals(curativeEffects[i]) && StringUtils.isNotBlank(curativeEffects[i]))){
                TTumourChemotherapy tumourChemotherapy = new TTumourChemotherapy();
                if(!"null".equals(startTimes[i])){
                    tumourChemotherapy.setStartTime(startTimes[i]);
                }
                if(!"null".equals(endTimes[i])){
                    tumourChemotherapy.setEndTime(endTimes[i]);
                }
                if(!"null".equals(drugNameAndDoses[i])){
                    tumourChemotherapy.setDrugNameAndDose(drugNameAndDoses[i]);
                }
                if(!"null".equals(treatmentCourses[i])){
                    tumourChemotherapy.setTreatmentCourse(treatmentCourses[i]);
                }
                if(!"null".equals(curativeEffects[i])){
                    tumourChemotherapy.setCurativeEffect(curativeEffects[i]);
                }
                tumourChemotherapy.setPatientId(patientId);
                list.add(tumourChemotherapy);
            }
        }

        if(!list.isEmpty()){
            commomService.simpleInsert("t_tumour_chemotherapy",tumourChemotherapyMapper,list);
        }
    }
}
