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.core.util.ObjectUtil;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.collection.dto.SzyTBasicDto;
import com.docus.server.collection.dto.TBasicDto;
import com.docus.server.collection.infrastructure.dao.entity.TBasic;
import com.docus.server.collection.infrastructure.dao.mapper.TBasicMapper;
import com.docus.server.collection.service.ITBasicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.time.Duration;
import java.util.*;

/**
 * @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
@Slf4j
public class TBasicServiceImpl implements ITBasicService {

    @Resource
    private TBasicMapper tBasicMapper;

    @Resource
    private IdService idService;

    @Override
    public void setSzyTBasic(List<SzyTBasicDto> dtos) {
        //持久化对像集合转化
        List<TBasic> tBasicList=new ArrayList<TBasic>();
        for(SzyTBasicDto dto:dtos){
            //判断jzh是否重复
            Integer num = tBasicMapper.selectOne(dto.getJzh());

            Long patientId = idService.getDateSeq();

            String sexName="";
            if("1".equals(dto.getSex())){
                sexName="男";
            }
            if("2".equals(dto.getSex())){
                sexName="女";
            }

            //组装数据
            TBasic tBasic=new TBasic();
            tBasic.setPatientId(patientId);
            tBasic.setJzh(dto.getJzh());
            tBasic.setInpatientNo(dto.getInpatientNo());
            tBasic.setAdmissTimes(dto.getAdmissTimes());
            tBasic.setAdmissDays(dto.getAdmissDays());
            tBasic.setName(dto.getName());
            tBasic.setAge(dto.getAge());
            tBasic.setSexName(sexName);
            tBasic.setSex(dto.getSex());
            tBasic.setIsDead(dto.getIsDead());
            tBasic.setAttendingName(dto.getAttendingName());
            tBasic.setAttending(dto.getAttending());
            tBasic.setIdCard(dto.getIdCard());
            tBasic.setAdmissDate(dto.getAdmissDate());
            tBasic.setAdmissDept(dto.getAdmissDept());
            tBasic.setAdmissDeptName(dto.getAdmissDeptName());
            tBasic.setDisDate(dto.getDisDate());
            tBasic.setDisDept(dto.getDisDept());
            tBasic.setDisDeptName(dto.getDisDeptName());
            tBasic.setCreateTime(DateUtil.now());
            if (num>0) {
                log.info("更新,jzh为"+tBasic.getJzh());
                tBasicMapper.update(tBasic);
            }else{
                tBasicList.add(tBasic);
            }
            log.info("tBasic:"+tBasic.toString());
        }
        //批量插入
        if(Func.isNotEmpty(tBasicList)){
            log.info("新增"+tBasicList.size()+"条基础数据开始");
            tBasicMapper.insertBatch(tBasicList);
            log.info("新增"+tBasicList.size()+"条基础数据结束");
        }
    }

}


