package thyyxxk.webserver.service.examinations;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.Capacity;
import thyyxxk.webserver.dao.lis.CovidExamDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.examinations.covidexam.CovidExamResult;
import thyyxxk.webserver.entity.examinations.covidexam.QueryCovidExamParam;
import thyyxxk.webserver.service.externalhttp.ThmzSystem;
import thyyxxk.webserver.utils.FilterUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dj
 */
@Slf4j
@Service
@DS("lis")
public class CovidExamService {
    private final CovidExamDao dao;
    private final ThmzSystem thmz;
    @Value("${thmz-api-url}")
    private String thmzApiUrl;

    @Autowired
    public CovidExamService(CovidExamDao dao, ThmzSystem thmz) {
        this.dao = dao;
        this.thmz = thmz;
    }

    public ResultVo<Map<String, Object>> queryResult(QueryCovidExamParam param) {
        log.info("查询新冠检测结果：{}", JSON.toJSONString(param));
        Map<String, Object> map = new HashMap<>(Capacity.TWO);
        IPage<CovidExamResult> iPage = getCovidExamResultPage(param);
        List<CovidExamResult> list = iPage.getRecords();
        List<CovidExamResult> childrenList = new ArrayList<>();
        List<Integer> parentList = new ArrayList<>();
        for (int i = 0; i < list.size(); i ++) {
            CovidExamResult itm = list.get(i);
            if (StringUtil.notBlank(itm.getPtntName()) && itm.getPtntName().startsWith("核酸混检")) {
                parentList.add(i);
                Map<String, Object> childrenMap = thmz.getPatientIdByParentId(thmzApiUrl, itm.getPtntNo());
                if ((int) childrenMap.get("code") == 0) {
                    List<Map<String, String>> children = FilterUtil.cast(childrenMap.get("data"));
                    for (Map<String, String> child : children) {
                        if (StringUtil.notBlank(child.get("socialNo"))) {
                            CovidExamResult result = new CovidExamResult();
                            result.setIdCard(child.get("socialNo"));
                            result.setPtntName(child.get("name"));
                            result.setItmStrValue(itm.getItmStrValue());
                            result.setOrdrCreateDate(itm.getOrdrCreateDate());
                            childrenList.add(result);
                        }
                    }
                }
            }
        }
        for (int i = 0; i < parentList.size(); i++) {
            int index = parentList.get(i) - i;
            list.remove(index);
        }
        list.addAll(childrenList);
        map.put("totalSize", iPage.getTotal());
        map.put("list", list);
        return ResultVoUtil.success(map);
    }

    private IPage<CovidExamResult> getCovidExamResultPage(QueryCovidExamParam param) {
        IPage<CovidExamResult> iPage = new Page<>(param.getCurrentPage(), param.getPageSize());
        if (param.getType() == 1) {
            return dao.selectHstResult(iPage, param.getStart(), param.getEnd(), "%%");
        }
        if (param.getType() == 9) {
            return dao.selectHstResult(iPage, param.getStart(), param.getEnd(), "阳性");
        }
        return dao.selectKtResult(iPage, param.getStart(), param.getEnd());
    }
}
