package com.artfess.rescue.patrol.controller;


import com.artfess.base.annotation.ApiGroup;
import com.artfess.base.constants.ApiGroupConsts;
import com.artfess.base.constants.SystemConstants;
import com.artfess.base.context.BaseContext;
import com.artfess.base.controller.BaseController;
import com.artfess.base.enums.ResponseErrorEnums;
import com.artfess.base.model.CommonResult;
import com.artfess.base.query.PageList;
import com.artfess.base.query.QueryFilter;
import com.artfess.base.util.BeanUtils;
import com.artfess.base.valid.AddGroup;
import com.artfess.base.valid.UpdateGroup;
import com.artfess.rescue.patrol.manager.BizInspectionPlanManager;
import com.artfess.rescue.patrol.model.BizInspectionPlan;
import com.artfess.rescue.utils.UserControlUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.quartz.SchedulerException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 巡检计划表：用于生成巡检任务的表 前端控制器
 *
 * @company 阿特菲斯信息技术有限公司
 * @author 系统管理员
 * @since 2024-08-02
 */
@RestController
@RequestMapping("/patrol/bizInspectionPlan/v1/")
@Api(tags = "巡检计划")
@ApiGroup(group = {ApiGroupConsts.GROUP_BIZ_RESCUE})
public class BizInspectionPlanController extends BaseController<BizInspectionPlanManager, BizInspectionPlan> {
    @Resource
    BaseContext baseContext;
    @Resource
    UserControlUtils userControlUtils;

    @PostMapping("/saveOrUpdate")
    @ApiOperation("添加或修改巡检任务")
    public CommonResult<String> saveOrUpdate(@ApiParam(name="model", value="巡检任务") @Validated({AddGroup.class, UpdateGroup.class}) @RequestBody BizInspectionPlan trainTaskConf) throws Exception {
        boolean rtn = BeanUtils.validClass(trainTaskConf.getPlanClass());
        if (rtn) {
            boolean result = baseService.saveOrUpdateInfo(trainTaskConf);
            if(!result) {
                return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "操作失败!");
            }
        } else {
            return new CommonResult(false, "操作失败!", null);
        }

        return new CommonResult<>();
    }

    @Override
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除")
    public CommonResult<String> deleteById(@ApiParam(name="id", value="实体id") @PathVariable String id) {
        try {
            boolean result = false;
            result = baseService.deleteById(id);
            if(!result) {
                return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "删除实体失败");
            }
            return new CommonResult<>("删除成功！");
        } catch (Exception e) {
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION,  e.getMessage());
        }
    }

    @PostMapping(value = "/toggleTriggerRun")
    @ApiOperation(value = "启用或停止巡检任务", httpMethod = "POST", notes = "启用或停止巡检任务")
    public CommonResult<String> start(@ApiParam(name = "id", value = "配置id", required = false) @RequestParam String id) throws Exception {
        boolean result = baseService.toggleTriggerRun(id);
        if(result){
            return new CommonResult<>("操作成功！");
        }else{
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "操作失败！");
        }
    }
    @PostMapping(value = "/executeJob")
    @ApiOperation(value = "执行巡检任务", httpMethod = "POST", notes = "执行巡检任务")
    public CommonResult<String> executeJob(@ApiParam(name = "id", value = "任务id", required = true) @RequestParam String id) throws SchedulerException {
        boolean result = baseService.executeJob(Collections.singletonList(id));
        if(result){
            return new CommonResult<>("执行巡检任务成功！");
        }else{
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "执行巡检任务失败！");
        }
    }

    @Override
    @GetMapping("/{id}")
    @ApiOperation("根据id查询实体")
    public BizInspectionPlan getById(@ApiParam(name = "id", value = "实体id") @PathVariable String id) {
        return baseService.findById(id);
    }

    @Override
    @PostMapping(value="/query", produces={"application/json; charset=utf-8" })
    @ApiOperation("分页查询结果")
    public PageList<BizInspectionPlan> query(@ApiParam(name="queryFilter", value="分页查询信息") @RequestBody QueryFilter<BizInspectionPlan> queryFilter) {
        // 非系统管理员自能查询自己部门的任务，本级的人员可以看全部
        if (!baseContext.getCurrentOrgId().equals(SystemConstants.SYS_ORG_ID) && !baseContext.isAdmin()) {
            userControlUtils.addDeptPermission(queryFilter);
        }
        return baseService.findByPage(queryFilter);
    }


    @GetMapping("/push")
    @ApiOperation("发布巡检任务")
    public CommonResult<String> push(@ApiParam(name = "id", value = "实体id") @RequestParam String id) {
        boolean result = baseService.push(id);
        if (!result) {
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "发布巡检任务失败");
        }
        return new CommonResult<>();
    }

    @PostMapping(value = "/executeJob/list")
    @ApiOperation(value = "执行巡检任务-批量执行", httpMethod = "POST", notes = "执行巡检任务-批量执行")
    public CommonResult<String> executeJobList(@ApiParam(name = "ids", value = "任务ids", required = true) @RequestParam List<String> ids) throws SchedulerException {
        boolean result = baseService.executeJob(ids);
        if (result) {
            return new CommonResult<>("执行巡检任务成功！");
        } else {
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "执行巡检任务失败！");
        }
    }
}
