package com.qmth.sop.server.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.params.SopQuickSearchParam;
import com.qmth.sop.business.bean.result.EditResult;
import com.qmth.sop.business.service.TBSopQuickSearchService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.SopQuickSearchTypeEnum;
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.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * sop快捷搜索表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */
@Api(tags = "sop快捷搜索Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SOP_QUICK)
public class TBSopQuickController {

    @Resource
    TBSopQuickSearchService tbSopQuickSearchService;

    @ApiOperation(value = "sop快捷搜索列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @OperationLog(logType = LogTypeEnum.SEARCH)
    @ApiResponses({ @ApiResponse(code = 200, message = "sop快捷搜索信息", response = EditResult.class) })
    public Result list(@ApiParam(value = "流程类型", required = true) @RequestParam TFCustomTypeEnum type,
            @ApiParam(value = "内容类型") @RequestParam(required = false) SopQuickSearchTypeEnum contentType) {
        return ResultUtil.ok(tbSopQuickSearchService.list(type, contentType));
    }

    @ApiOperation(value = "sop快捷搜索保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperationLog(logType = LogTypeEnum.UPDATE)
    @ApiResponses({ @ApiResponse(code = 200, message = "sop快捷搜索信息", response = EditResult.class) })
    public Result save(
            @Valid @ApiParam(value = "sop快捷搜索参数信息", required = true) @RequestBody SopQuickSearchParam sopQuickSearchParam,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbSopQuickSearchService.save(sopQuickSearchParam));
    }

    @ApiOperation(value = "sop快捷搜索删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @OperationLog(logType = LogTypeEnum.DELETE)
    @ApiResponses({ @ApiResponse(code = 200, message = "sop快捷搜索信息", response = EditResult.class) })
    public Result delete(@ApiParam(value = "id", required = true) @RequestParam Long id) {
        return ResultUtil.ok(tbSopQuickSearchService.delete(id));
    }
}