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.SCEntity;
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.text.DecimalFormat;
import java.util.*;

/**
 * @author zsy
 * @description:面孔情绪评价任务
 * @date 2021/7/20 15:55
 */
public class EATCognize extends BaseScale {

    public EATCognize(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("results");
        //对数据按照同一人物的同一表情进行排序
        jsonArray.sort(Comparator.comparing(obj->((JSONObject)obj).getString("name"),Comparator.nullsFirst(Comparator.naturalOrder())));
        List<EATEntity> EATEntityList = jsonArray.toJavaList(EATEntity.class);

        fileName = fileName + "-" + Constant.SHEET_NAME_EAT + ".xlsx";
        name = Constant.SHEET_NAME_EAT;
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(EATEntity.class, EATEntityList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_EAT), 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;
    }


}
