package cn.axzo.workflow.server.controller.web.manage;

import cn.axzo.workflow.client.feign.manage.ProcessCategoryApi;
import cn.axzo.workflow.common.model.request.category.CategoryConfigCreateDTO;
import cn.axzo.workflow.common.model.request.category.CategoryConfigSearchDTO;
import cn.axzo.workflow.common.model.request.category.CategoryCreateDTO;
import cn.axzo.workflow.common.model.request.category.CategorySearchDTO;
import cn.axzo.workflow.common.model.request.category.CategoryUpdateDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.category.CategoryConfigItemVO;
import cn.axzo.workflow.common.model.response.category.CategoryItemVO;
import cn.axzo.workflow.core.service.CategoryConfigService;
import cn.axzo.workflow.core.service.CategoryService;
import cn.axzo.workflow.server.common.annotation.ErrorReporter;
import cn.axzo.workflow.server.common.annotation.RepeatSubmit;
import cn.azxo.framework.common.model.CommonResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static cn.azxo.framework.common.model.CommonResponse.success;

/**
 * 业务分类相关控制器
 *
 * @author wangli
 * @since 2023/11/9 14:01
 */
@Slf4j
@RequestMapping({"/web/v1/api/process/category", "/api/process/category"})
@RestController
@ErrorReporter
@Validated
public class ProcessCategoryController implements ProcessCategoryApi {

    @Resource
    private CategoryService categoryService;
    @Resource
    private CategoryConfigService categoryConfigService;

    /**
     * 获取指定业务分类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取指定业务分类")
    @GetMapping("/get")
    @Override
    public CommonResponse<CategoryItemVO> get(@RequestParam Long id) {
        return success(categoryService.get(id));
    }

    /**
     * 获取指定业务分类集合
     *
     * @param ids
     * @return
     */
    @Operation(summary = "获取指定业务分类集合")
    @GetMapping("/getByIds")
    @Override
    public CommonResponse<List<CategoryItemVO>> getByIds(@RequestParam List<Long> ids) {
        return success(categoryService.getIds(ids));
    }

    @Operation(summary = "通过分类值查询分类集合")
    @GetMapping("/getByValues")
    @Override
    public CommonResponse<List<CategoryItemVO>> getByValues(@RequestParam List<String> values) {
        return success(categoryService.getByValues(values));
    }

    /**
     * 更新指定分类状态
     *
     * @param id
     * @param state
     * @return
     */
    @Operation(summary = "更新指定分类状态")
    @PutMapping("/update/state")
    @Override
    public CommonResponse<Boolean> updateState(@RequestParam Long id, @RequestParam Boolean state) {
        return success(categoryService.updateState(id, state));
    }

    /**
     * 新增业务分类
     *
     * @return
     */
    @Operation(summary = "创建业务分类")
    @PostMapping("/create")
    @Override
    @RepeatSubmit
    public CommonResponse<CategoryItemVO> create(@Validated @RequestBody CategoryCreateDTO req) {
        return success(categoryService.createCategory(req));
    }

    /**
     * 更新业务分类
     *
     * @return
     */
    @Operation(summary = "更新业务分类")
    @PutMapping("/update")
    @Override
    public CommonResponse<CategoryItemVO> update(@Validated @RequestBody CategoryUpdateDTO dto) {
        return success(categoryService.updateCategory(dto));
    }

    /**
     * 删除指定业务分类
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除指定业务分类")
    @DeleteMapping("/delete")
    @Override
    @RepeatSubmit
    public CommonResponse<Boolean> delete(Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    /**
     * 获取指定业务分类集合
     *
     * @return
     */
    @Operation(summary = "搜索业务分类集合")
    @PostMapping("/list")
    @Override
    public CommonResponse<List<CategoryItemVO>> list(@Validated @RequestBody CategorySearchDTO dto) {
        return success(categoryService.list(dto));
    }

    /**
     * 搜索业务分类
     *
     * @return
     */
    @Operation(summary = "分页搜索业务分类集合")
    @PostMapping("/page/search")
    @Override
    public CommonResponse<BpmPageResult<CategoryItemVO>> search(@Validated @RequestBody CategorySearchDTO dto) {
        return success(categoryService.search(dto));
    }

    /**
     * 业务分类创建黑白名单配置项
     *
     * @param dto
     * @return
     */
    @Operation(summary = "业务分类创建黑白名单配置项")
    @PostMapping("/config/create")
    @Override
    @RepeatSubmit
    public CommonResponse<Boolean> createConfig(@Validated @RequestBody CategoryConfigCreateDTO dto) {
        categoryConfigService.create(dto);
        return success(true);
    }

    /**
     * 删除指定分类的配置项数据
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除指定分类的配置项数据")
    @DeleteMapping("/config/delete/{id}")
    @Override
    public CommonResponse<Boolean> deleteConfig(@PathVariable Long id) {
        return success(categoryConfigService.delete(id));
    }

    /**
     * 业务分类黑白名单查询
     *
     * @return
     */
    @Operation(summary = "业务分类黑白名单查询")
    @PostMapping("/config/page/search")
    @Override
    public CommonResponse<BpmPageResult<CategoryConfigItemVO>> configSearch(@Validated @RequestBody CategoryConfigSearchDTO dto) {
        return success(categoryConfigService.configSearch(dto));
    }

    /**
     * 更新业务分类的配置类型
     *
     * @param id
     * @param configType
     * @return
     */
    @Operation(summary = "更新业务分类的配置类型")
    @PostMapping("/config/type/update")
    @Override
    public CommonResponse<Boolean> updateCategoryConfigType(@RequestParam Long id, @RequestParam String configType) {
        return success(categoryService.updateRemark(id, configType));
    }

    /**
     * 用于业务方判断指定的业务分类的黑白名单配置
     *
     * @param tenantId     租户ID
     * @param categoryCode 业务分类编码
     * @return true: 可以发起创建流程实例, false: 不可用
     */
    @Operation(summary = "用于业务方判断指定的业务分类的黑白名单配置")
    @GetMapping("/check/status")
    @Override
    public CommonResponse<Boolean> checkCategoryStatus(@RequestParam Long tenantId, @RequestParam String categoryCode) {
        return success(categoryService.checkCategoryStatus(tenantId, categoryCode));
    }
}
