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.BpmFormParam;
import com.njcn.bpm.pojo.po.BpmForm;
import com.njcn.bpm.pojo.vo.BpmFormVO;
import com.njcn.bpm.service.IBpmFormService;
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/form")
@Validated
@Slf4j
@Api(tags = "流程表单控制器")
@RequiredArgsConstructor
public class BpmFormController extends BaseController {


    private final IBpmFormService formService;


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询流程表单数据")
    @ApiImplicitParam(name = "bpmFormQueryParam", value = "查询参数", required = true)
    public HttpResult<Page<BpmFormVO>> list(@RequestBody BpmFormParam.BpmFormQueryParam bpmFormQueryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询流程表单数据为：{}", methodDescribe, bpmFormQueryParam);
        Page<BpmFormVO> result = formService.getFormPage(bpmFormQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增流程表单")
    @ApiImplicitParam(name = "bpmFormParam", value = "流程表单数据", required = true)
    public HttpResult<String> add(@Valid @RequestBody BpmFormParam bpmFormParam) {
        String methodDescribe = getMethodDescribe("add");
        String wfFormId = formService.createForm(bpmFormParam);
        if (StrUtil.isNotBlank(wfFormId)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, wfFormId, 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 BpmFormParam.BpmFormUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        formService.updateForm(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));
        formService.deleteForm(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }



    @GetMapping("/getById")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获得动态表单")
    @Parameter(name = "id", description = "编号", required = true)
    public HttpResult<BpmForm> getById(String id) {
        String methodDescribe = getMethodDescribe("getById");
        BpmForm form = formService.getById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, form, methodDescribe);
    }

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


}
