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.BpmCategoryParam;
import com.njcn.bpm.pojo.po.BpmCategory;
import com.njcn.bpm.pojo.vo.BpmCategoryVO;
import com.njcn.bpm.service.IBpmCategoryService;
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 com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


/**
 * @author hongawen
 */
@RestController
@RequestMapping("/bpm/category")
@Validated
@Slf4j
@Api(tags = "流程分类控制器")
@RequiredArgsConstructor
public class BpmCategoryController extends BaseController {

    private final IBpmCategoryService categoryService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("创建流程分类")
    @ApiImplicitParam(name = "bpmCategoryParam", value = "流程分类数据", required = true)
    public HttpResult<String> add(@Valid @RequestBody BpmCategoryParam bpmCategoryParam) {
        String methodDescribe = getMethodDescribe("add");
        String categoryId = categoryService.createCategory(bpmCategoryParam);
        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 BpmCategoryParam.BpmCategoryUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        categoryService.updateCategory(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));
        categoryService.deleteCategory(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @GetMapping("/getById")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获得流程分类")
    @Parameter(name = "id", description = "编号", required = true)
    public HttpResult<BpmCategory> getById(String id) {
        String methodDescribe = getMethodDescribe("getById");
        BpmCategory bpmCategory = categoryService.getById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, bpmCategory, methodDescribe);
    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询流程分类数据")
    @ApiImplicitParam(name = "bpmCategoryQueryParam", value = "查询参数", required = true)
    public HttpResult<Page<BpmCategoryVO>> list(@RequestBody BpmCategoryParam.BpmCategoryQueryParam bpmCategoryQueryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询流程分类数据为：{}", methodDescribe, bpmCategoryQueryParam);
        Page<BpmCategoryVO> result = categoryService.getCategoryPage(bpmCategoryQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


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

}