package com.docus.server.collection.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.docus.core.util.DateUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.collection.dto.TBasicDto;
import com.docus.server.collection.entity.TBasic;
import com.docus.server.collection.entity.TBasicExtend;
import com.docus.server.collection.mapper.TBasicMapper;
import com.docus.server.collection.service.ITBasicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @BelongsProject: docus-webservice-sdry
 * @BelongsPackage: com.docus.server.collection.service.impl
 * @Author: chierhao
 * @CreateTime: 2023-02-28  08:37
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class TBasicServiceImpl implements ITBasicService {

    @Resource
    private TBasicMapper tBasicMapper;

    @Resource
    private IdService idService;

    @Override
    @Transactional
    public void setTBasic(TBasicDto tBasicDto) throws Exception {
        //判断jzh是否重复
        Integer num = tBasicMapper.selectOne(tBasicDto.getJzh());
        if (num>0) {
            updateTBasic(tBasicDto);
            return;
        }
        Long patientId = idService.getDateSeq();

        //数据类型转化，格式处理
        Date admissDate=null;
        if(Func.isNotEmpty(tBasicDto.getAdmissDate())){
            admissDate = Func.parseDate(tBasicDto.getAdmissDate(), DateUtil.PATTERN_DATETIME_MINI);
        }
        Date disDate=null;
        if(Func.isNotEmpty(tBasicDto.getDisDate())){
            disDate = Func.parseDate(tBasicDto.getDisDate(), DateUtil.PATTERN_DATETIME_MINI);
        }
        String admissTimesStr = tBasicDto.getAdmissTimes();
        Integer admissTimes=null;
        if(NumberUtil.isInteger(admissTimesStr)){
            admissTimes=Integer.parseInt(admissTimesStr);
        }
        String ageStr = tBasicDto.getAge();
        if(Func.isNotEmpty(ageStr)){
            ageStr =ageStr.substring(0,ageStr.length()-1);
        }
        Integer age=null;
        if(NumberUtil.isInteger(ageStr)){
            age=Integer.parseInt(ageStr);
        }
        String sexName=tBasicDto.getSexName();
        if(sexName.length()>1){
            sexName=sexName.substring(0,1);
        }
        String admissDaysStr = tBasicDto.getAdmissDays();
        Integer admissDays=null;
        if(NumberUtil.isInteger(admissDaysStr)){
            admissDays=Integer.parseInt(admissDaysStr);
        }
        String isDeadStr = tBasicDto.getIsDead();
        Integer isDead=0;
        if("死亡".equals(isDeadStr)){
            isDead=1;
        }

        //组装数据
        TBasic tBasic=new TBasic();
        tBasic.setPatientId(patientId.toString());
        tBasic.setJzh(tBasicDto.getJzh());
        tBasic.setInpatientNo(tBasicDto.getInpatientNo());
        tBasic.setAdmissTimes(admissTimes);
        tBasic.setName(tBasicDto.getName());
        tBasic.setAdmissDate(admissDate);
        tBasic.setDisDate(disDate);
        tBasic.setAdmissDeptName(tBasicDto.getAdmissDeptName());
        tBasic.setDisDeptName(tBasicDto.getDisDeptName());
        tBasic.setAttendingName(tBasicDto.getAttendingName());
        tBasic.setAge(age);
        tBasic.setSex(tBasicDto.getSex());
        tBasic.setIdCard(tBasicDto.getIdCard());
        tBasic.setDisDept(tBasicDto.getDisDept());
        tBasic.setSexName(sexName);
        tBasic.setBedNum(tBasicDto.getBedNum());
        tBasic.setAdmissDays(admissDays);
        tBasic.setIsDead(isDead);

        TBasicExtend tBasicExtend=new TBasicExtend();
        tBasicExtend.setPatientId(patientId.toString());
        tBasicExtend.setWardCode(tBasicDto.getWardCode());
        tBasicExtend.setWardName(tBasicDto.getWardName());
        tBasicExtend.setSdryIndex(tBasicDto.getSdryIndex());

        //持久化
        tBasicMapper.insert(tBasic);
        tBasicMapper.insertExtend(tBasicExtend);
    }

    @Override
    @Transactional
    public void updateTBasic(TBasicDto tBasicDto) throws Exception {
        //判断jzh是否存在
        String patientId = tBasicMapper.getPatientId(tBasicDto.getJzh());
        if (Func.isEmpty(patientId)) {
            setTBasic(tBasicDto);
            return;
        }
        //数据类型转化，格式处理
        Date admissDate=null;
        if(Func.isNotEmpty(tBasicDto.getAdmissDate())){
            admissDate = Func.parseDate(tBasicDto.getAdmissDate(), DateUtil.PATTERN_DATETIME_MINI);
        }
        Date disDate=null;
        if(Func.isNotEmpty(tBasicDto.getDisDate())){
            disDate = Func.parseDate(tBasicDto.getDisDate(), DateUtil.PATTERN_DATETIME_MINI);
        }
        String admissTimesStr = tBasicDto.getAdmissTimes();
        Integer admissTimes=null;
        if(NumberUtil.isInteger(admissTimesStr)){
            admissTimes=Integer.parseInt(admissTimesStr);
        }
        String ageStr = tBasicDto.getAge();
        if(Func.isNotEmpty(ageStr)){
            ageStr =ageStr.substring(0,ageStr.length()-1);
        }
        Integer age=null;
        if(NumberUtil.isInteger(ageStr)){
            age=Integer.parseInt(ageStr);
        }
        String sexName=tBasicDto.getSexName();
        if(sexName.length()>1){
            sexName=sexName.substring(0,1);
        }
        String admissDaysStr = tBasicDto.getAdmissDays();
        Integer admissDays=null;
        if(NumberUtil.isInteger(admissDaysStr)){
            admissDays=Integer.parseInt(admissDaysStr);
        }
        String isDeadStr = tBasicDto.getIsDead();
        Integer isDead=0;
        if("死亡".equals(isDeadStr)){
            isDead=1;
        }

        //组装数据
        TBasic tBasic=new TBasic();

        tBasic.setJzh(tBasicDto.getJzh());
        tBasic.setPatientId(patientId);
        tBasic.setInpatientNo(tBasicDto.getInpatientNo());
        tBasic.setAdmissTimes(admissTimes);
        tBasic.setName(tBasicDto.getName());
        tBasic.setAdmissDate(admissDate);
        tBasic.setDisDate(disDate);
        tBasic.setAdmissDeptName(tBasicDto.getAdmissDeptName());
        tBasic.setDisDeptName(tBasicDto.getDisDeptName());
        tBasic.setAttendingName(tBasicDto.getAttendingName());
        tBasic.setAge(age);
        tBasic.setSex(tBasicDto.getSex());
        tBasic.setIdCard(tBasicDto.getIdCard());
        tBasic.setDisDept(tBasicDto.getDisDept());
        tBasic.setSexName(sexName);
        tBasic.setBedNum(tBasicDto.getBedNum());
        tBasic.setAdmissDays(admissDays);
        tBasic.setIsDead(isDead);

        TBasicExtend tBasicExtend=new TBasicExtend();
        tBasicExtend.setPatientId(patientId);
        tBasicExtend.setWardCode(tBasicDto.getWardCode());
        tBasicExtend.setWardName(tBasicDto.getWardName());
        tBasicExtend.setSdryIndex(tBasicDto.getSdryIndex());

        //持久化
        tBasicMapper.update(tBasic);
        tBasicMapper.updateExtend(tBasicExtend);
    }
}


