package com.docus.server.record.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.record.common.pojo.dto.TBasicDTO;
import com.docus.server.record.common.pojo.entity.TBasic;
import com.docus.server.record.common.pojo.entity.TBasicExtend;
import com.docus.server.record.mapper.TBasicMapper;
import com.docus.server.record.service.ITBasicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class TBasicServiceImpl implements ITBasicService {

    @Resource
    private TBasicMapper tBasicMapper;

    @Resource
    private IdService idService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertTBasic(TBasicDTO tBasicDTO) {
        //判断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.setAttending(tBasicDTO.getAttending());
        tBasic.setDutyNurse(tBasicDTO.getDutyNurse());
        tBasic.setAdmissDept(tBasicDTO.getAdmissDept());
        tBasic.setCreateTime(DateUtil.now());
        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.insertTBasic(tBasic);
        tBasicMapper.insertExtend(tBasicExtend);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTBasic(TBasicDTO tBasicDTO) {
        //判断jzh是否存在
        String patientId = tBasicMapper.getPatientId(tBasicDTO.getJzh());
        if (Func.isEmpty(patientId)) {
            insertTBasic(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);
        tBasic.setAttending(tBasicDTO.getAttending());
        tBasic.setDutyNurse(tBasicDTO.getDutyNurse());
        tBasic.setAdmissDept(tBasicDTO.getAdmissDept());
        tBasic.setCreateTime(DateUtil.now());

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

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

    private void convert() {

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSaveBasics(List<TBasicDTO> basicDTOList) {
        basicDTOList.forEach(this::updateTBasic);
    }

    @Override
    public Integer findByJzh(String jzh) {
        //判断jzh是否重复
        return tBasicMapper.selectOne(jzh);
    }

    /**
     * 根据住院查询住院信息
     *
     * @param zyhs
     * @return
     */
    @Override
    public List<Map<String, Object>> getBasicForZYHs(List<String> zyhs) {
        return tBasicMapper.getBasicForZYHs(zyhs);
    }

    /**
     * 查询基础出院数据增量数据
     */
    @Override
    public List<Map<String, Object>> getBasicOutData(Map<String, Object> param) {
        return tBasicMapper.getBasicOutData(param);
    }

    /**
     * 查询基础创建数据增量数据
     */
    @Override
    public List<Map<String, Object>> getBasicData(Map<String, Object> param) {
        return tBasicMapper.getBasicData(param);
    }
}


