package com.njcn.bpm.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.bpm.pojo.param.BpmSignParam;
import com.njcn.bpm.pojo.po.BpmSign;
import com.njcn.bpm.pojo.vo.BpmSignVO;
import com.njcn.bpm.service.IBpmSignService;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * BPM 流程标识 前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2024-05-13
 */
@RestController
@RequestMapping("/bpmSign")
@Validated
@Slf4j
@Api(tags = "流程标识控制器")
@RequiredArgsConstructor
public class BpmSignController extends BaseController {

    private final IBpmSignService bpmSignService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("创建流程标识")
    @ApiImplicitParam(name = "bpmSignParam", value = "流程标识数据", required = true)
    public HttpResult<String> add(@Valid @RequestBody BpmSignParam bpmSignParam) {
        String methodDescribe = getMethodDescribe("add");
        String categoryId = bpmSignService.createSign(bpmSignParam);
        if (StrUtil.isNotBlank(categoryId)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, categoryId, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("更新流程标识")
    @ApiImplicitParam(name = "updateParam", value = "流程标识数据", required = true)
    public HttpResult<Object> update(@RequestBody @Validated BpmSignParam.BpmSignUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        bpmSignService.updateSign(updateParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/delete")
    @ApiOperation("删除流程标识")
    @ApiImplicitParam(name = "ids", value = "流程标识索引", required = true, dataTypeClass = List.class)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，流程标识ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        bpmSignService.deleteSign(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询流程标识数据")
    @ApiImplicitParam(name = "bpmSignQueryParam", value = "查询参数", required = true)
    public HttpResult<Page<BpmSignVO>> list(@RequestBody BpmSignParam.BpmSignQueryParam bpmSignQueryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询流程标识数据为：{}", methodDescribe, bpmSignQueryParam);
        Page<BpmSignVO> result = bpmSignService.getSignPage(bpmSignQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @GetMapping("/simpleList")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获得动态表单的精简列表")
    public HttpResult<List<BpmSign>> getSignSimpleList() {
        String methodDescribe = getMethodDescribe("getCategorySimpleList");
        List<BpmSign> list = bpmSignService.getSignSimpleList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


}

