package com.fs.hisStore.service.impl;


import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.his.utils.TestReportUtil;
import com.fs.hisStore.domain.FsTestReportScrm;
import com.fs.hisStore.domain.FsTestTempScrm;
import com.fs.hisStore.domain.FsTestTempItemScrm;
import com.fs.hisStore.mapper.FsTestReportScrmMapper;
import com.fs.hisStore.mapper.FsTestTempItemScrmMapper;
import com.fs.hisStore.mapper.FsTestTempScrmMapper;
import com.fs.hisStore.param.FsTestReportParam;
import com.fs.hisStore.service.IFsTestReportScrmService;
import com.fs.hisStore.utils.TestReportUtilScrm;
import com.fs.hisStore.vo.FsTestReportListVO;
import com.fs.hisStore.vo.FsTestReportVO;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 测试报告Service业务层处理
 *
 * @author fs
 * @date 2023-09-04
 */
@SuppressWarnings("all")
@Service
public class FsTestReportScrmServiceImpl implements IFsTestReportScrmService
{
    @Autowired
    private FsTestReportScrmMapper fsTestReportMapper;

    @Autowired
    private FsTestTempItemScrmMapper fsTestTempItemMapper;
    @Autowired
    private FsTestTempScrmMapper fsTestTempMapper;
    /**
     * 查询测试报告
     *
     * @param reportId 测试报告主键
     * @return 测试报告
     */
    @Override
    public FsTestReportScrm selectFsTestReportByReportId(Long reportId)
    {

        return fsTestReportMapper.selectFsTestReportByReportId(reportId);
    }

    /**
     * 查询测试报告列表
     *
     * @param fsTestReport 测试报告
     * @return 测试报告
     */
    @Override
    public List<FsTestReportScrm> selectFsTestReportList(FsTestReportScrm fsTestReport)
    {
        return fsTestReportMapper.selectFsTestReportList(fsTestReport);
    }

    /**
     * 新增测试报告
     *
     * @param fsTestReport 测试报告
     * @return 结果
     */
    @Override
    public int insertFsTestReport(FsTestReportScrm fsTestReport)
    {
        fsTestReport.setCreateTime(DateUtils.getNowDate());
        return fsTestReportMapper.insertFsTestReport(fsTestReport);
    }



    public R addReport(FsTestReportScrm testReport) {
        String  formJson=testReport.getFormJson();
        FsTestTempItemScrm item = new FsTestTempItemScrm();
        item.setItemId(testReport.getTempId());
        List<FsTestTempItemScrm> fsTestTempItems = fsTestTempItemMapper.selectFsTestTempItemList(item);
        List<Map> maps = (List<Map>) JSON.parse(formJson);
        Integer allScore=0;
        for (Map map1 : maps) {
            Integer itemId = (Integer)map1.get("itemId");
            String option = (String)map1.get("option");
            for (FsTestTempItemScrm fsTestTempItem : fsTestTempItems) {
                if (fsTestTempItem.getItemId()==itemId+0){
                    String scoreJson = fsTestTempItem.getScoreJson();
                    List<Map> scores = (List<Map>) JSON.parse(scoreJson);
                    for (Map score : scores) {
                        String name =(String)score.get("name");
                        if (name.equals(option)){
                            Integer score1 = (Integer)score.get("score");
                            allScore+=score1;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        String itemType="";
        String itemTypeJSonValue="";
        FsTestTempScrm fsTestTemp = fsTestTempMapper.selectFsTestTempByTempId(testReport.getTempId());
        String itemTypeJson = fsTestTemp.getItemTypeJson();
        List<Map<String, Object>> itemTypeJsonList = (List<Map<String, Object>>) JSON.parse(itemTypeJson);
        for (Map<String, Object> stringStringMap : itemTypeJsonList) {
            Integer min = (Integer) stringStringMap.get("min");
            Integer max = (Integer) stringStringMap.get("max");
            if (allScore>=min&&allScore<=max){
                itemType = (String) stringStringMap.get("itemType");
                itemTypeJSonValue=Json.encode(stringStringMap.get("item"));
            }
        }
        testReport.setCreateTime(DateUtils.getNowDate());
        testReport.setConditioningPlanJson(itemTypeJSonValue);
        testReport.setTestResult(itemType);
        testReport.setScore(allScore.longValue());
        if(fsTestReportMapper.insertFsTestReport(testReport)>0){
            return R.ok().put("reportId",testReport.getReportId());
        }
        else{
            return R.error();
        }
    }



    @Override
    public R doReport(FsTestReportScrm testReport) {
        if (testReport.getTempId()!=5){
            String  formJson=testReport.getFormJson();
            FsTestTempItemScrm item = new FsTestTempItemScrm();
            item.setItemId(testReport.getTempId());
            List<FsTestTempItemScrm> fsTestTempItems = fsTestTempItemMapper.selectFsTestTempItemList(item);
            List<Map> maps = (List<Map>) JSON.parse(formJson);
            Integer allScore=0;
            for (Map map1 : maps) {
                Integer itemId = (Integer)map1.get("itemId");
                String option = (String)map1.get("option");
                for (FsTestTempItemScrm fsTestTempItem : fsTestTempItems) {
                    if (fsTestTempItem.getItemId()==itemId+0){
                        String scoreJson = fsTestTempItem.getScoreJson();
                        List<Map> scores = (List<Map>) JSON.parse(scoreJson);
                        for (Map score : scores) {
                            String name =(String)score.get("name");
                            if (name.equals(option)){
                                Integer score1 = (Integer)score.get("score");
                                allScore+=score1;
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            String itemType="";
            String itemTypeJSonValue="";
            FsTestTempScrm fsTestTemp = fsTestTempMapper.selectFsTestTempByTempId(testReport.getTempId());
            String itemTypeJson = fsTestTemp.getItemTypeJson();
            List<Map<String, Object>> itemTypeJsonList = (List<Map<String, Object>>) JSON.parse(itemTypeJson);
            for (Map<String, Object> stringStringMap : itemTypeJsonList) {
                Integer min = (Integer) stringStringMap.get("min");
                Integer max = (Integer) stringStringMap.get("max");
                if (allScore>=min&&allScore<=max){
                    itemType = (String) stringStringMap.get("itemType");
                    itemTypeJSonValue=Json.encode(stringStringMap.get("item"));
                }
            }
            testReport.setCreateTime(DateUtils.getNowDate());
            testReport.setConditioningPlanJson(itemTypeJSonValue);
            testReport.setTestResult(itemType);
            testReport.setScore(Long.parseLong(allScore+""));
            if(fsTestReportMapper.insertFsTestReport(testReport)>0){
                testReportUtil.getTestReportImageUrlByReportId(testReport.getReportId());
                return R.ok().put("reportId",testReport.getReportId());
            }
            else{
                return R.error();
            }
        }else {
            String  formJson=testReport.getFormJson();
            FsTestTempItemScrm item = new FsTestTempItemScrm();
            item.setItemId(testReport.getTempId());
            List<FsTestTempItemScrm> fsTestTempItems = fsTestTempItemMapper.selectFsTestTempItemList(item);
            // 按照字段类型进行分类
            Map<String,Map<String,Integer>> map=new HashMap<>();
            List<Map> maps = (List<Map>) JSON.parse(formJson);
            for (Map map1 : maps) {
                Integer itemId = (Integer)map1.get("itemId");
                String option = (String)map1.get("option");
                for (FsTestTempItemScrm fsTestTempItem : fsTestTempItems) {
                    if (fsTestTempItem.getItemId()==itemId+0){
                        String scoreJson = fsTestTempItem.getScoreJson();
                        List<Map> scores = (List<Map>) JSON.parse(scoreJson);
                        for (Map score : scores) {
                            String name =(String)score.get("name");
                            if (name.equals(option)){
                                Integer score1 = (Integer)score.get("score");
                                // 如果存在，从Map中获取当前计数值并加分，然后放回Map中
                                Map<String, Integer> co = map.getOrDefault(fsTestTempItem.getItemType(), new HashMap<>());
                                co.put("score",co.getOrDefault("score", 0) + score1);
                                co.put("num",co.getOrDefault("num", 0) + 1);
                                map.put(fsTestTempItem.getItemType(),co);
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            String tempType="";
            Map<String, Double> scoreMap = new HashMap<>();
            // 遍历 map
            for (Map.Entry<String, Map<String,Integer>> entry : map.entrySet()) {
                String key = entry.getKey(); // 获取键
                Map<String, Integer> value = entry.getValue();
                Integer num = value.get("num");
                Integer score = value.get("score");
                double i = ((score - num) / (num * 4.0)) * 100;
                scoreMap.put(key,scoreMap.getOrDefault("num", 0.0) + i);
            }
            Double peace = scoreMap.remove("平和质");
            Double score=peace;
            // 选出值最大的项
            Map.Entry<String, Double> maxEntry = null;
            for (Map.Entry<String, Double> entry : scoreMap.entrySet()) {
                if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
                    maxEntry = entry;
                }
            }
            if (maxEntry.getValue()>=40){
                tempType=maxEntry.getKey();
            }else if (maxEntry.getValue()>=30){
                if (peace>=60){
                    tempType="平和质";
                }else {
                    tempType=maxEntry.getKey();
                    score=maxEntry.getValue();
                }
            }else {
                tempType="平和质";
            }
            String itemTypeJSonValue="[]";
            FsTestTempScrm fsTestTemp = fsTestTempMapper.selectFsTestTempByTempId(testReport.getTempId());
            String itemTypeJson = fsTestTemp.getItemTypeJson();
            List<Map<String, Object>> itemTypeJsonList = (List<Map<String, Object>>) JSON.parse(itemTypeJson);
            for (Map<String, Object> stringStringMap : itemTypeJsonList) {
                String itemType = (String) stringStringMap.get("itemType");
                if (itemType.equals(tempType)){
                    itemTypeJSonValue=Json.encode(stringStringMap.get("item"));
                }
            }
            testReport.setCreateTime(DateUtils.getNowDate());
            testReport.setConditioningPlanJson(itemTypeJSonValue);
            testReport.setTestResult(tempType);
            testReport.setScore( Math.round(score));
            if(fsTestReportMapper.insertFsTestReport(testReport)>0){
                testReportUtil.getTestReportImageUrlByReportId(testReport.getReportId());
                return R.ok().put("reportId",testReport.getReportId());
            }
            else{
                return R.error();
            }
        }


    }

    /**
     * 修改测试报告
     *
     * @param fsTestReport 测试报告
     * @return 结果
     */
    @Override
    public int updateFsTestReport(FsTestReportScrm fsTestReport)
    {
        return fsTestReportMapper.updateFsTestReport(fsTestReport);
    }

    /**
     * 批量删除测试报告
     *
     * @param reportIds 需要删除的测试报告主键
     * @return 结果
     */
    @Override
    public int deleteFsTestReportByReportIds(Long[] reportIds)
    {
        return fsTestReportMapper.deleteFsTestReportByReportIds(reportIds);
    }

    /**
     * 删除测试报告信息
     *
     * @param reportId 测试报告主键
     * @return 结果
     */
    @Override
    public int deleteFsTestReportByReportId(Long reportId)
    {
        return fsTestReportMapper.deleteFsTestReportByReportId(reportId);
    }

    @Override
    public List<FsTestReportListVO> selectFsTestReportListVO(FsTestReportParam fsTestReport) {
        return fsTestReportMapper.selectFsTestReportListVO(fsTestReport);
    }

    @Autowired
    TestReportUtil testReportUtil;
    @Override
    public FsTestReportVO selectFsTestReportByReportIdVO(Long reportId) {
        return fsTestReportMapper.selectFsTestReportByReportIdVO(reportId);
    }





}
