/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2023 . All rights reserved.
*
*/
package net.northking.cctp.scriptcase.db.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import net.northking.cctp.scriptcase.api.scenescript.bean.SceneScript;
import net.northking.cctp.scriptcase.api.scenescript.bean.Variable;
import net.northking.cctp.scriptcase.constants.ScriptConstant;
import net.northking.cctp.scriptcase.db.dao.AtuScriptInputSetDao;
import net.northking.cctp.scriptcase.db.service.AtuScriptInputSetService;
import net.northking.cctp.scriptcase.db.entity.AtuScriptInputSet;

import net.northking.cctp.common.db.BasicDao;
import net.northking.cctp.common.db.PaginationService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

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


/**
 * 脚本输入项数据集关联草稿表
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2023-08-29 10:41:44  <br>
 * @author:  maven-cctp-plugin  <br>
 * @since:  1.0 <br>
 */
@Service
@ConditionalOnMissingBean(name = "AtuScriptInputSetServiceImpl")
public class AtuScriptInputSetServiceImpl extends PaginationService<AtuScriptInputSet> implements AtuScriptInputSetService
{
private static final Logger logger = LoggerFactory.getLogger(AtuScriptInputSetServiceImpl.class);

  private final AtuScriptInputSetDao atuScriptInputSetDao;

  public AtuScriptInputSetServiceImpl(AtuScriptInputSetDao atuScriptInputSetDao) {this.atuScriptInputSetDao = atuScriptInputSetDao;}

  public BasicDao<AtuScriptInputSet> getDao() {return atuScriptInputSetDao;}

  public AtuScriptInputSet createEntity() { return new AtuScriptInputSet();}

  @Override
  protected void beforeInsert(AtuScriptInputSet record) {

  }

  @Override
  protected void beforeUpdate(AtuScriptInputSet record) {

  }

    @Override
    public JSONObject createDefaultInputSet(String scriptJson) {
      JSONObject jsonObject = new JSONObject();
      List<Map<String, Object>> tableContent = new ArrayList<>();
      List<String> tableHeaders = new ArrayList<>();
      tableHeaders.add(ScriptConstant.SCRIPT_TEST_PURPOSE_NAME);
      Map<String, Object> map = new HashMap<>();
      map.put(ScriptConstant.SCRIPT_TEST_PURPOSE_ID, IdUtil.simpleUUID());
      map.put(ScriptConstant.SCRIPT_TEST_PURPOSE_NAME, ScriptConstant.DEFAULT_PURPOSE_NAME);
      // 增加脚本中的输入项变量
      SceneScript sceneScript = JSONUtil.toBean(scriptJson, SceneScript.class);
      if (sceneScript != null){
        List<Variable> variables = sceneScript.getVariables();
        if (CollUtil.isNotEmpty(variables)){
          for (Variable variable : variables) {
            if (variable.isInput()){
              map.put(variable.getName(), variable.getDefaultValue() == null ? "" : variable.getDefaultValue());
              tableHeaders.add(variable.getName());
            }
          }
        }
      }
      tableContent.add(map);
      Map<String,Object> variableMap = new HashMap<>();
      List<Map<String,Object>> variableList = new ArrayList<>();
      variableMap.put(ScriptConstant.INPUT_VARIABLE_DATA_TYPE,ScriptConstant.INPUT_VARIABLE_DEFAULT_TYPE);
      variableMap.put(ScriptConstant.INPUT_VARIABLE_NAME,ScriptConstant.SCRIPT_TEST_PURPOSE_NAME);
      variableMap.put(ScriptConstant.INPUT_VARIABLE_DEFAULT_VALUE,null);
      variableMap.put(ScriptConstant.INPUT_VARIABLE_MEMO,null);
      variableList.add(variableMap);

      jsonObject.put(ScriptConstant.SCRIPT_INPUT_TABLE_CONTENT, tableContent);
      jsonObject.put(ScriptConstant.SCRIPT_INPUT_TABLE_HEADERS, tableHeaders);
      jsonObject.put(ScriptConstant.SCRIPT_INPUT_VARIABLES,variableList);
      return jsonObject;
    }







}

