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

import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.archive.entity.TBasic;
import com.docus.server.archive.entity.TBasicZkInfo;
import com.docus.server.archive.mapper.TBasicMapper;
import com.docus.server.archive.mapper.TBasicZkInfoMapper;
import com.docus.server.archive.service.ZkInfoSyncService;
import com.docus.server.his.entity.VJswzhZkxx;
import com.docus.server.his.mapper.VJswzhZkxxMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YongBin Wen
 * @date 2024/4/9 14:16
 */
@Service
@Slf4j
public class ZkInfoSyncServiceImpl implements ZkInfoSyncService {
    @Resource
    private VJswzhZkxxMapper vJswzhZkxxMapper;
    @Resource
    private TBasicMapper tBasicMapper;
    @Resource
    private TBasicZkInfoMapper tBasicZkInfoMapper;
    @Resource
    private IdService idService;

    @Override
    public int syncZkInfo(String disDateStart, String disDateEnd, Integer pageNumber, Integer pageSize) {
        log.info("同步转科信息参数：disDateStart：{}，disDateEnd：{}，pageNumber：{}，pageSize：{}", disDateStart, disDateEnd, pageNumber, pageSize);
        Date nowDate = new Date();
        int offset = (pageNumber - 1) * pageSize;
        List<TBasic> basicList = tBasicMapper.getByDisDate(disDateStart, disDateEnd, offset, pageSize);
        if (Func.isEmpty(basicList)) {
            return 0;
        }
        int querySize = basicList.size();
        basicList = basicList.stream().filter(e -> Func.isNotBlank(e.getJzh())).collect(Collectors.toList());
        if (Func.isEmpty(basicList)) {
            return querySize;
        }
        List<String> jzhs = basicList.stream().map(TBasic::getJzh).collect(Collectors.toList());
        List<String> patientIds = basicList.stream().map(TBasic::getPatientId).collect(Collectors.toList());
        Map<String, String> basicJzhPidMap = basicList.stream().collect(Collectors.toMap(TBasic::getJzh, TBasic::getPatientId));
        List<VJswzhZkxx> zkxxList = vJswzhZkxxMapper.getZkInfoByJzhs(jzhs);
        if (Func.isNotEmpty(zkxxList)) {
            List<TBasicZkInfo> zkInfos = new ArrayList<>();
            for (VJswzhZkxx vJswzhZkxx : zkxxList) {
                TBasicZkInfo zkInfo = new TBasicZkInfo();
                zkInfo.setId(idService.getDateSeq());
                zkInfo.setPatientId(basicJzhPidMap.get(vJswzhZkxx.getJzh()));
                zkInfo.setOutDeptCode(vJswzhZkxx.getZcbq());
                zkInfo.setOutDeptName(vJswzhZkxx.getZcks());
                zkInfo.setInDeptCode(vJswzhZkxx.getZrbq());
                zkInfo.setInDeptName(vJswzhZkxx.getZrks());
                zkInfo.setOutDate(vJswzhZkxx.getZcrq());
                zkInfo.setCreateTime(nowDate);
                zkInfos.add(zkInfo);
            }
            // 根据病案主键查询已经同步的转科信息，未同步的插入
            List<String> existsZkInfoPatientIds = tBasicZkInfoMapper.existsZkInfoPatientIds(patientIds);
            zkInfos = zkInfos.stream().filter(zk -> !existsZkInfoPatientIds.contains(zk.getPatientId())).collect(Collectors.toList());
            if (Func.isNotEmpty(zkInfos)) {
                tBasicZkInfoMapper.saveBatch(zkInfos);
            }
            log.info("出院日期范围：{} - {} ,页码：{} size：{},查询：{}基础数据，本次新增转科信息数据：{} 条", disDateStart, disDateEnd, pageNumber, pageSize, basicList.size(), zkInfos.size());
        }
        return querySize;
    }
}
