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

import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.archive.entity.AfCollectTask;
import com.docus.server.archive.entity.TBasic;
import com.docus.server.archive.mapper.AfCollectTaskMapper;
import com.docus.server.synchis.entity.InLocEmrView;
import com.docus.server.synchis.mapper.YdZyyHisDbMapper;
import com.docus.server.synchis.service.HisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author YongBin Wen
 * @date 2025/1/23 13:22
 */
@Service
@Slf4j
public class HisServiceImpl implements HisService {
    @Resource
    private YdZyyHisDbMapper ydZyyHisDbMapper;
    @Resource
    private AfCollectTaskMapper afCollectTaskMapper;
    @Resource
    private IdService idService;

    @Override
    public void checkTask(TBasic tbasic) {
        String hisSource="2";
        String jzh = tbasic.getJzh();
        String patientId = tbasic.getPatientId();
        List<AfCollectTask> collectTasks = afCollectTaskMapper.findByPatientIds(Collections.singletonList(patientId));
        List<InLocEmrView> emrViews = ydZyyHisDbMapper.queryByPatId(jzh);

        List<AfCollectTask>   hisCollectTasks = collectTasks.stream()
                .filter(tsk->hisSource.equals(tsk.getSysflag()))
                .collect(Collectors.toList());

        Map<String, InLocEmrView> emrViewMap = emrViews.stream()
                .collect(Collectors.toMap(v->v.getId().toString(), Function.identity()));

        List<AfCollectTask> cancelTasks=new ArrayList<>();
        List<AfCollectTask> updateTasks=new ArrayList<>();
        for (AfCollectTask task : hisCollectTasks) {
            String fileId = task.getC7();
            // 病程记录不检查
            if (fileId.contains("bcjl")) {
                continue;
            }
            // 视图没有作废
            InLocEmrView emrView =emrViewMap.get(fileId);
            if(Objects.isNull(emrView)){
                cancelTasks.add(task);
                continue;
            }
            // 如果更新修改状态
            Date modifytime = task.getT2();
            Date viewModifyopertime = emrView.getModifyopertime();
            if(viewModifyopertime.after(modifytime)) {
                task.setSyncTime(new Date());
                task.setState("0");
                task.setC9("1");
                task.setPresult("");
                task.setT1(emrView.getRecordingtime());
                task.setT2(viewModifyopertime);
                updateTasks.add(task);
            }
        }
        if (Func.isNotEmpty(cancelTasks)) {
            List<Long> fileIds = cancelTasks.stream().map(AfCollectTask::getAfArchiveDetailId).collect(Collectors.toList());
            List<Long> taskIds = cancelTasks.stream().map(AfCollectTask::getId).collect(Collectors.toList());
            afCollectTaskMapper.cancelTask(taskIds);
            afCollectTaskMapper.cancelFileById(fileIds);
        }
        if (Func.isNotEmpty(updateTasks)) {
            afCollectTaskMapper.update(updateTasks);
        }

    }
}
