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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.EATEntity;
import com.rf.psychological.dao.model.STFSEntity;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.scale.resultBusiness.scaleResult.BaseScale;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.ExcelUtil;

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

/**
 * @author zsy
 * @description:神分裂症语义距离度量
 * @date 2021/7/20 15:55
 */
public class STFSCognize extends BaseScale {

    public STFSCognize(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);

        JSONArray jsonArray = jsonObject.getJSONArray("userResponseRecords");
        //对数据按照同一人物的同一表情进行排序
        //jsonArray.sort(Comparator.comparing(obj->((JSONObject)obj).getString("name"),Comparator.nullsFirst(Comparator.naturalOrder())));
        List<STFSEntity> STFSEntityList = jsonArray.toJavaList(STFSEntity.class);

        fileName = fileName + "-" + Constant.SHEET_NAME_STFS + ".xlsx";
        name = Constant.SHEET_NAME_STFS;
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(STFSEntity.class, STFSEntityList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_STFS), fileName, name);

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

        return returnJson;
    }


}
