package com.rf.psychological.scale.resultBusiness.scaleResult;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.AnswerEntity;
import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.scale.dao.model.DimensionOrgEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.model.WarnEntity;
import com.rf.psychological.scale.service.DimensionOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import sun.util.resources.cldr.zh.CalendarData_zh_Hans_HK;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @author zsy
 * @description:简单相加和分维度量表通用算法
 * @date 2021/7/20 15:55
 */
public class COMMONScale extends BaseScale {


    public COMMONScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("FESCVSCALE.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("JSONObject:"+resultJson);
        ScalesJNI scalesJNI = new ScalesJNI(jsonArray,resultJson);
        String result = scalesJNI.commonScale(jsonArray.toString(), resultJson.toString());
        JSONObject jsonObjectc =JSONObject.parseObject(result);
        System.out.println("jsonObjectc:"+jsonObjectc);
        return jsonObjectc;*/

        //总分
        double score = 0;
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        List<Map<String, String>> resultMapList = new ArrayList<>();

        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取评分规则列表
        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        double[] dimensionScore = new double[dimensionEntities.size()];
        Map<String,Double> dimensionScoreMap = new HashMap<>();
        if (dimensionEntities.size() > 0) {

            //计算每个维度得分

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                for (int y = 0; y < dimensionEntities.size(); y++) {
                    //for (DimensionEntity dimensionEntity:dimensionEntities){
                    DimensionEntity dimensionEntity = dimensionEntities.get(y);
                    String questionNos = dimensionEntity.getQuestionNo();
                    String[] questionNo = questionNos.replaceAll("\\s", "").split("；");
                    for (String question : questionNo) {
                        if (question.equals(jsonObject1.getString("questionNo"))) {
                            for (AnswerEntity answerEntity : answerEntities) {
                                if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                                    dimensionScore[y] += Double.parseDouble(answerEntity.getScore());
                                    dimensionScoreMap.put(dimensionEntity.getId(),dimensionScoreMap.get(dimensionEntity.getId())==null?Double.parseDouble(answerEntity.getScore()):(dimensionScoreMap.get(dimensionEntity.getId()) +Double.parseDouble(answerEntity.getScore())));
                                }
                            }
                            break;
                        }
                    }
                }
                //计算总分
                for (AnswerEntity answerEntity : answerEntities) {
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i + 1))) {
                        if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                            score += Integer.parseInt(answerEntity.getScore());
                        }
                    }
                }
            }

            resultMap0.put("总分", String.valueOf(score));
            resultJson.put("总分", String.valueOf(score));
            if (scaleMarksEntities.size() > 0) {
                for (ScaleMarksEntity scaleMarksEntity : scaleMarksEntities) {
                    if (scaleMarksEntity.getName() != null && (scaleMarksEntity.getName().equals("无") || scaleMarksEntity.getName().equals("总分"))) {
                        if (score <= Integer.parseInt(scaleMarksEntity.getScoreEnd()) && score >= Integer.parseInt(scaleMarksEntity.getScoreStart())) {
                            if (!scaleMarksEntity.getSymptom().equals("无")) {
                                resultMap0.put("结论", scaleMarksEntity.getSymptom());
                                resultJson.put("结论", scaleMarksEntity.getSymptom());
                            }
                            if (scaleMarksEntity.getImprovementSuggestions() != null && !scaleMarksEntity.getImprovementSuggestions().equals("无")) {
                                resultMap0.put("建议", scaleMarksEntity.getImprovementSuggestions());
                                resultJson.put("建议", scaleMarksEntity.getImprovementSuggestions());
                            }
                        }
                    }
                }
            }
            if (scaleMarksEntities.size() > 0) {
                for (int y = 0; y < dimensionEntities.size(); y++) {
                    DimensionEntity dimensionEntity = dimensionEntities.get(y);
                    DecimalFormat df = new DecimalFormat("######0.00");
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]))));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]))));
                    for (ScaleMarksEntity scaleMarksEntity : scaleMarksEntities) {
                        if (scaleMarksEntity.getName() != null && scaleMarksEntity.getName().equals(dimensionEntity.getName())) {
                            if (dimensionScore[y] <= Integer.parseInt(scaleMarksEntity.getScoreEnd()) && dimensionScore[y] >= Integer.parseInt(scaleMarksEntity.getScoreStart())) {
                                if (!scaleMarksEntity.getSymptom().equals("无")) {
                                    resultMap0.put(dimensionEntity.getName() + "结论", scaleMarksEntity.getSymptom());
                                    resultJson.put(dimensionEntity.getName() + "结论", scaleMarksEntity.getSymptom());
                                }
                                if (scaleMarksEntity.getImprovementSuggestions() != null && !scaleMarksEntity.getImprovementSuggestions().equals("无")) {
                                    resultMap0.put(dimensionEntity.getName() + "建议", scaleMarksEntity.getImprovementSuggestions());
                                    resultJson.put(dimensionEntity.getName() + "建议", scaleMarksEntity.getImprovementSuggestions());
                                }
                            }
                        }
                    }
                }
                resultMapList.add(resultMap0);
            } else {
                for (int y = 0; y < dimensionEntities.size(); y++) {
                    DimensionEntity dimensionEntity = dimensionEntities.get(y);
                    DecimalFormat df = new DecimalFormat("######0.00");
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]))));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]))));
                }
                resultMapList.add(resultMap0);
            }
        } else {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                for (AnswerEntity answerEntity : answerEntities) {
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i + 1))) {
                        if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                            score += Double.parseDouble(answerEntity.getScore());
                        }
                    }
                }
            }

            resultMap0.put("总分", String.valueOf(score));
            resultJson.put("总分", String.valueOf(score));
            if (scaleMarksEntities.size() > 0) {
                for (ScaleMarksEntity scaleMarksEntity : scaleMarksEntities) {
                    if (score <= Integer.parseInt(scaleMarksEntity.getScoreEnd()) && score >= Integer.parseInt(scaleMarksEntity.getScoreStart())) {
                        if (!scaleMarksEntity.getSymptom().equals("无")) {
                            resultMap0.put("结论", scaleMarksEntity.getSymptom());
                            resultJson.put("结论", scaleMarksEntity.getSymptom());
                        }
                        if (scaleMarksEntity.getImprovementSuggestions() != null && !scaleMarksEntity.getImprovementSuggestions().equals("无")) {
                            resultMap0.put("建议", scaleMarksEntity.getImprovementSuggestions());
                            resultJson.put("建议", scaleMarksEntity.getImprovementSuggestions());
                        }
                    }
                }
            }
            resultMapList.add(resultMap0);
        }
        resultJson.put("dimensionScoreMap",dimensionScoreMap);



        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        returnJson.put("resultJson", resultJson);

        return returnJson;
    }


}
