package com.qmth.sop.server.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.activiti.service.ActivitiService;
import com.qmth.sop.business.bean.params.FlowApproveParam;
import com.qmth.sop.business.bean.params.SopApplyParam;
import com.qmth.sop.business.bean.params.SopInfoListParam;
import com.qmth.sop.business.bean.params.SopSaveParam;
import com.qmth.sop.business.bean.result.FlowResult;
import com.qmth.sop.business.bean.result.FormWidgetMetadataResult;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.entity.TDFormWidgetMetadata;
import com.qmth.sop.business.service.TBSopInfoService;
import com.qmth.sop.business.service.TDFormWidgetMetadataService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.TFCustomTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * sop表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */
@Api(tags = "sop管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SOP)
public class TBSopInfoController {

    @Resource
    TDFormWidgetMetadataService tdFormWidgetMetadataService;

    @Resource
    TBSopInfoService tbSopInfoService;

    @Resource
    ActivitiService activitiService;

    @ApiOperation(value = "sop元数据查询")
    @RequestMapping(value = "/metadata/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = FormWidgetMetadataResult.class)})
    public Result metadataList(@ApiParam(value = "sop流程类型", required = true) @RequestParam TFCustomTypeEnum type) {
        return ResultUtil.ok(tdFormWidgetMetadataService.selectAll(type));
    }

    @ApiOperation(value = "sop元数据修改")
    @RequestMapping(value = "/metadata/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result metadataSave(@Valid @ApiParam(value = "元数据保存信息", required = true) @RequestBody List<TDFormWidgetMetadata> formWidgetMetadataList, BindingResult bindingResult) {
        return ResultUtil.ok(tdFormWidgetMetadataService.save(formWidgetMetadataList));
    }

    @ApiOperation(value = "sop申请")
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result apply(@Valid @ApiParam(value = "sop申请信息", required = true) @RequestBody SopApplyParam sopApplyParam, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbSopInfoService.sopApply(sopApplyParam));
    }

    @ApiOperation(value = "sop填报")
    @RequestMapping(value = "/approve", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result approve(@Valid @ApiParam(value = "sop填报信息", required = true) @RequestBody FlowApproveParam flowApproveParam, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbSopInfoService.sopApprove(flowApproveParam));
    }

    @ApiOperation(value = "sop列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Map.class)})
    public Result list(@Valid @ApiParam(value = "sop列表信息", required = true) @RequestBody SopInfoListParam sopInfoListParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbSopInfoService.list(sopInfoListParam));
    }

    @ApiOperation(value = "sop作废")
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result cancel(@ApiParam(value = "流程id", required = true) @RequestParam Long flowId) {
        return ResultUtil.ok(activitiService.flowEnd(flowId));
    }

    @ApiOperation(value = "sop批量作废")
    @RequestMapping(value = "/batch/cancel", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result batchCancel(@ApiParam(value = "流程id数组", required = true) @RequestBody List<Long> flowIds) {
        if (CollectionUtils.isEmpty(flowIds)) {
            throw ExceptionResultEnum.ERROR.exception("流程id数组不能为空");
        }
        for (Long l : flowIds) {
            activitiService.flowEnd(l);
        }
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "sop编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = FlowResult.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result edit(@ApiParam(value = "sopId", required = true) @RequestParam Long id) throws InterruptedException {
        return ResultUtil.ok(tbSopInfoService.sopEdit(id));
    }

    @ApiOperation(value = "sop保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result save(@Valid @ApiParam(value = "sop保存信息", required = true) @RequestBody SopSaveParam sopSaveParam, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbSopInfoService.sopSave(sopSaveParam));
    }

    @ApiOperation(value = "sopUsers")
    @RequestMapping(value = "/user/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = SysUser.class)})
    public Result userList(@ApiParam(value = "sopId", required = true) @RequestParam Long sop_info_id) {
        return ResultUtil.ok(tbSopInfoService.userList(sop_info_id));
    }
}