package thyyxxk.webserver.service.examinations;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.Capacity;
import thyyxxk.webserver.dao.his.examinations.Covid19AssessmentDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.examinations.covidexam.CovidExamResult;
import thyyxxk.webserver.entity.examinations.covidexam.QueryCovidExamParam;
import thyyxxk.webserver.utils.ResultVoUtil;

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

@Slf4j
@Service
public class CovidAssessmentService {
    private final Covid19AssessmentDao dao;

    public CovidAssessmentService(Covid19AssessmentDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> queryResult(QueryCovidExamParam param) {
        log.info("查询新冠检测结果：{}", JSON.toJSONString(param));
        IPage<CovidExamResult> iPage = new Page<>(param.getCurrentPage(), param.getPageSize());
        iPage = dao.selectAssessments(iPage, param.getStart(), param.getEnd());
        Map<String, Object> map = new HashMap<>(Capacity.TWO);
        map.put("totalSize", iPage.getTotal());
        List<CovidExamResult> list = iPage.getRecords();
        list.forEach(itm -> {
            itm.setAddress(itm.getAddrProvince() + itm.getAddrCity() + itm.getAddrDistrict() + itm.getAddrAdditional());
            String[] symps = itm.getSymptomsString()
                    .replace("[", "")
                    .replace("]", "")
                    .replaceAll(" ", "")
                    .split(",");
            if (symps.length > 0) {
                StringBuilder sb = new StringBuilder();
                for (String s : symps) {
                    switch (s) {
                        case "511":
                            sb.append("，发热");
                            break;
                        case "512":
                            sb.append("，咳嗽");
                            break;
                        case "513":
                            sb.append("，乏力");
                            break;
                        case "514":
                            sb.append("，腹泻/呕吐");
                            break;
                    }
                }
                if (sb.length() > 1) {
                    itm.setSymptomsString(sb.substring(1, sb.length()));
                } else {
                    itm.setSymptomsString("");
                }
            }
        });
        map.put("list", list);
        return ResultVoUtil.success(map);
    }
}
