package com.qxgmat.service.extend;

import com.alibaba.fastjson.JSONObject;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.dao.entity.QuestionNo;
import com.qxgmat.data.dao.entity.Setting;
import com.qxgmat.data.relation.entity.QuestionNoRelation;
import com.qxgmat.service.inline.SettingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ToolsService {

    @Resource
    private SettingService settingService;

    /**
     * 根据练习或者模考时间设置计算考题考试时间
     *      setting: {struct: {difficult: ""}}
     * @param settingKey
     * @param relationList
     * @return
     */
    public Integer computerTime(SettingKey settingKey, List<QuestionNoRelation> relationList){
        Setting setting = settingService.getByKey(settingKey);
        JSONObject value = setting.getValue();

        Integer time = 0;
        for(QuestionNoRelation relation:relationList){
            JSONObject difficultMap = null;
            for (Integer struct : relation.getModuleStruct()){
                // 判断结构体
                difficultMap = value.getJSONObject(String.valueOf(struct));
                if (difficultMap != null) break;
            }
            if (difficultMap == null) continue;
            // 判断难度
            String t = difficultMap.getString(relation.getQuestion().getDifficult());
            if (t == null || t.isEmpty()) continue;
            time += Integer.valueOf(t);
        }
        return time;
    }
}