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.converter.PatientInfoConverter;
import com.docus.server.archive.entity.PowerDept;
import com.docus.server.archive.entity.PowerUser;
import com.docus.server.archive.entity.TBasic;
import com.docus.server.archive.mapper.PowerDeptMapper;
import com.docus.server.archive.mapper.PowerUserMapper;
import com.docus.server.archive.mapper.TBasicMapper;
import com.docus.server.archive.service.PatientInfoSyncService;
import com.docus.server.fsy.entity.JswzhPatientInfoView;
import com.docus.server.fsy.mapper.JswzhPatientInfoViewMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author YongBin Wen
 * @date 2024/3/27 16:25
 */
@Service
@Slf4j
public class PatientInfoSyncServiceImpl implements PatientInfoSyncService {
    @Resource
    private TBasicMapper tBasicMapper;
    @Resource
    private JswzhPatientInfoViewMapper jswzhPatientInfoViewMapper;
    @Resource
    private IdService idService;
    @Resource
    private PowerUserMapper powerUserMapper;
    @Resource
    private PowerDeptMapper powerDeptMapper;

    @Override
    public void syncInHospitalData() {
        List<JswzhPatientInfoView> inHospital = jswzhPatientInfoViewMapper.getInHospital();
        // 在院数据将出院日期设置为空
        for (JswzhPatientInfoView view : inHospital) {
            view.setCYRQ(null);
        }
        convertAndSave(inHospital);
    }


    @Override
    public int syncDischargeData(String startDateTime, String endDateTime, Integer pageNumber, Integer pageSize) {
        int startRow = (pageNumber - 1) * pageSize + 1;
        int endRow = pageNumber * pageSize;
        List<JswzhPatientInfoView> discharge = jswzhPatientInfoViewMapper.getDischarge(startDateTime, endDateTime, startRow, endRow);
        convertAndSave(discharge);
        return discharge.size();
    }

    @Override
    public void insertOrUpdate(List<TBasic> tBasicList) {
        if (Func.isEmpty(tBasicList)) {
            return;
        }
        List<String> jzhList = tBasicList.stream().map(TBasic::getJzh).collect(Collectors.toList());
        Date nowDate = new Date();
        List<TBasic> existsBasicList = tBasicMapper.getByJzh(jzhList);

        // 区分新增和更新的数据
        Map<String, TBasic> existsJzhPatientMap = Objects.isNull(existsBasicList) ? new HashMap<>() : existsBasicList.stream().collect(Collectors.toMap(TBasic::getJzh, Function.identity()));
        List<TBasic> insertList = new ArrayList<>();
        List<TBasic> updateList = new ArrayList<>();
        if (Func.isEmpty(existsBasicList)) {
            insertList = tBasicList;
        } else {
            for (TBasic basic : tBasicList) {
                String jzh = basic.getJzh();
                if (existsJzhPatientMap.containsKey(jzh)) {
                    updateList.add(basic);
                } else {
                    insertList.add(basic);
                }
            }
        }

        // 相应的数据进行数据填充并进行持久化,更新/新增 需要填充patientId,create_time,update_time
        if (Func.isNotEmpty(insertList)) {
            for (TBasic basic : insertList) {
                if (Func.isBlank(basic.getPatientId())) {
                    basic.setPatientId(idService.getDateSeq() + "");
                }
                basic.setCreateTime(nowDate);
                basic.setUpdateTime(nowDate);
            }
            tBasicMapper.insertBatch(insertList);
        }
        if (Func.isNotEmpty(updateList)) {
            for (TBasic basic : updateList) {
                TBasic existsBasic = existsJzhPatientMap.get(basic.getJzh());
                basic.setCreateTime(existsBasic.getCreateTime());
                basic.setUpdateTime(nowDate);
                basic.setPatientId(existsBasic.getPatientId());
            }
            tBasicMapper.updateBatch(updateList);
        }
    }

    private void convertAndSave(List<JswzhPatientInfoView> jswzhPatientInfoViews) {
        if (Func.isEmpty(jswzhPatientInfoViews)) {
            return;
        }
        List<PowerUser> powerUserList = powerUserMapper.list();
        List<PowerDept> powerDeptList = powerDeptMapper.list();
        Map<String, String> userNameMap = powerUserList.stream().collect(Collectors.toMap(PowerUser::getUserName, PowerUser::getName));
        Map<String, String> deptCodeNameMap = powerDeptList.stream().collect(Collectors.toMap(PowerDept::getDeptCode, PowerDept::getDeptName));
        PatientInfoConverter converter = new PatientInfoConverter(userNameMap, deptCodeNameMap);
        List<TBasic> basicList = jswzhPatientInfoViews.stream().map(converter::convertArchiveBasicInfo).collect(Collectors.toList());
        insertOrUpdate(basicList);
    }
}
