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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.ObjectEntity;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.scale.dao.model.PsychologicalCapitalEntity;
import com.rf.psychological.scale.dao.model.UserRecordEntity;
import com.rf.psychological.scale.resultBusiness.scaleResult.BaseScale;
import com.rf.psychological.user.dao.model.UserEntity;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.ExcelUtil;
import com.rf.psychological.utils.Utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zsy
 * @description:心理资本
 * @date 2021/7/20 15:55
 */
public class PSYCAPITALCognize extends BaseScale {

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

    public  JSONObject scaleCalculate() throws Exception {
        List<Map<String, String>> resultMapList = new ArrayList<>();
        JSONObject jsonObject = resultJson.getJSONObject("jsonObject");
        String fileName = resultJson.getString("fileName");
        String name;

        JSONObject resultDBJson = new JSONObject(true);

        name = Constant.SHEET_NAME_PSY_CAPITAL;
        List<PsychologicalCapitalEntity> psyCapitalList = (List<PsychologicalCapitalEntity>) resultJson.get("psyCapitalList");
        List<UserRecordEntity> userRecordEntities = (List<UserRecordEntity>) resultJson.get("userRecordEntities");
        for (PsychologicalCapitalEntity psyCapital : psyCapitalList) {
            if (psyCapital.getHierarchy().equals("1.0")){
                JSONObject information = new JSONObject(true);
                information = JSONObject.parseObject(psyCapital.getRecordId());
                for (String key : information.keySet()) {
                    resultDBJson.put(key,information.get(key));
                }
            }else {
                for (UserRecordEntity userRecordEntity : userRecordEntities){
                    if (psyCapital.getRecordId().equals(userRecordEntity.getId())){
                        String testresult = userRecordEntity.getTestResults();
                        String[] testresults = testresult.split(";");
                        String[] scores = testresults[0].split(":");
                        if (psyCapital.getHierarchy().equals("1.1")){
                            resultDBJson.put("1.1"+userRecordEntity.getName()+"总分",scores[1]);
                        }else if (psyCapital.getHierarchy().equals("1.2")){
                            resultDBJson.put("1.2"+userRecordEntity.getName()+"总分",scores[1]);
                        }else if (psyCapital.getHierarchy().equals("1.3")){
                            resultDBJson.put("1.3"+userRecordEntity.getName()+"总分",scores[1]);
                            String keyA = testresults[1].split(":")[0];
                            String valueA = testresults[1].split(":")[1];
                            resultDBJson.put("1.3"+userRecordEntity.getName()+keyA,valueA);
                            String keyB = testresults[3].split(":")[0];
                            String valueB = testresults[3].split(":")[1];
                            resultDBJson.put("1.3"+userRecordEntity.getName()+keyB,valueB);
                            String keyC = testresults[5].split(":")[0];
                            String valueC = testresults[5].split(":")[1];
                            resultDBJson.put("1.3"+userRecordEntity.getName()+keyC,valueC);
                            String keyD = testresults[7].split(":")[0];
                            String valueD = testresults[7].split(":")[1];
                            resultDBJson.put("1.3"+userRecordEntity.getName()+keyD,valueD);
                            if(psyCapital.getGroupIndex().equals("1")){
                                resultDBJson.put("心理资本","高心理资本");
                            }else {
                                resultDBJson.put("心理资本","低心理资本");
                            }
                            if(psyCapital.getStrongType().equals("1")){
                                resultDBJson.put("说服信息","强说服信息");
                            }else {
                                resultDBJson.put("说服信息","弱说服信息");
                            }
                        }else if (psyCapital.getHierarchy().equals("2.1")){
                            resultDBJson.put("2.1"+userRecordEntity.getName()+"总分",scores[1]);
                        }else {
                            resultDBJson.put("2.2"+userRecordEntity.getName()+"总分",scores[1]);
                        }
                    }

                }

            }
        }

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("fileName",fileName);
        returnJson.put("name", name);
        returnJson.put("resultJson",resultDBJson);
        returnJson.put("datas","");

        return returnJson;
    }


}
