package com.css.simulation.resource.server.adapter.controller.system_manage.scene_upload;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.system.SceneImportPageParam;
import api.common.pojo.po.system.SceneImportPO;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.SceneEvaluationRuleService;
import com.css.simulation.resource.server.infra.oauth.AuthorizationHolder;
import com.css.simulation.resource.server.infra.feign.service.SceneService;
import com.css.simulation.resource.server.app.service.SceneImportService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/sceneImport")
@PreAuthorize("@AuthorityCheck.admin()")
public class SceneUploadController {

    @Resource
    private SceneImportService sceneImportService;

    @Resource
    private SceneService sceneService;

    @Resource
    private SceneEvaluationRuleService sceneEvaluationRuleService;

//    @RequestMapping("/batchDeleteTask")
//    public ResponseBodyVO<String> batchDeleteTask(@RequestBody SceneImportPO sceneImportPO) {
//        List<String> idList = sceneImportPO.getIdList();
//        for (String taskId : idList) {
//            sceneImportService.deleteTask(taskId);
//        }
//        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
//    }

    //* -------------------------------- 删除任务 --------------------------------

    @RequestMapping("/deleteTask")
    public ResponseBodyVO<String> deleteTask(@RequestBody SceneImportPO sceneImportPO) {
        String taskId = sceneImportPO.getId();
        sceneImportService.deleteTask(taskId);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    /**
     * 保存上传任务
     */
    @RequestMapping("/saveTask")
    public ResponseBodyVO<SceneImportPO> saveTask(@RequestBody SceneImportPO po, HttpServletRequest request) {
        if (ObjectUtil.isNull(po)) {
            return new ResponseBodyVO<>(false, 500, "参数错误！", null);
        }
        String sceneType = po.getSceneType();
        if (ObjectUtil.isNull(sceneType) || DictConstants.SCENE_GENERAL.equals(sceneType)) {
            return new ResponseBodyVO<>(false, 500, "参数错误！", null);
        }
        // 判断是否选择复杂度脚本
        String sceneEvaluationRuleId = po.getSceneEvaluationRuleId();
        if (ObjectUtil.isNotNull(sceneEvaluationRuleId) && !sceneEvaluationRuleService.checkSceneEvaluationScript(sceneEvaluationRuleId)) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "复杂度脚本类型错误，请检查脚本类型.");
        }
        // 保存场景导入任务
        po = sceneImportService.saveTask(po);
        if (ObjectUtil.isNull(po.getId())) {
            return new ResponseBodyVO<>(false, 500, "任务名称重复！", null);
        }

        ResponseBodyVO<SceneImportPO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(po);
        //异步-启动上传
        String authorization = request.getHeader("Authorization");
        AuthorizationHolder.setAuthorization(authorization);
        po.setCreateTime(null);//防止类型转换异常
        po.setModifyTime(null);//防止类型转换异常
        //* -------------------------------- 启动导入 --------------------------------
        sceneService.startImport(po);
        return response;
    }

    //查询列表
    @RequestMapping("/getSceneImporPagetList")
    public ResponseBodyVO<PageInfo<SceneImportPO>> getSceneImporPagetList(@RequestBody SceneImportPageParam param) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, sceneImportService.getSceneImporPagetList(param));
    }

}
