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

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.category.CategoryItemVO;
import cn.axzo.workflow.core.service.CategoryService;
import cn.azxo.framework.common.model.CommonResponse;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

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

@Tag(name = "运维管理 - 字典分类")
@Slf4j
@RequestMapping("/web/v1/api/category")
@RestController
@Validated
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Operation(summary = "创建分类")
    @PostMapping("/create")
    public CommonResponse<Boolean> create(@Validated @RequestBody CategoryCreateDTO dto) {
        log.info("创建Category===>>>参数：{}", JSON.toJSONString(dto));
        categoryService.createCategory(dto);
        return success(true);
    }


    @Operation(summary = "更新分类")
    @PutMapping("/update")
    public CommonResponse<Boolean> update(@Validated @RequestBody CategoryUpdateDTO dto) {
        log.info("更新Category===>>>参数：{}", JSON.toJSONString(dto));
        categoryService.updateCategory(dto);
        return success(true);
    }


    @Operation(summary = "分类列表")
    @GetMapping("/list")
    public CommonResponse<IPage<CategoryItemVO>> list(@Validated @RequestBody CategorySearchDTO dto) {
        log.info("查询Category===>>>列表接口参数，{}", JSON.toJSONString(dto));
        Page<CategoryItemVO> bpmCategory = categoryService.findCategory(dto);
        return success(bpmCategory);
    }

    @Operation(summary = "更新分类状态")
    @PutMapping("/state/update")
    public CommonResponse<Boolean> updateState(@NotBlank(message = "分类 ID 不能为空") @RequestParam Long id,
                                               @NotBlank(message = "状态不能为空") @RequestParam Boolean state) {
        log.info("更新状态Category===>>>参数，{}", JSON.toJSONString(id));
        return success(categoryService.updateState(id, state));
    }


    @Operation(summary = "删除分类")
    @DeleteMapping("/delete")
    public CommonResponse<Boolean> delete(@NotBlank(message = "分类 ID 不能为空") @RequestParam Long id) {
        log.info("删除Category===>>>参数，{}", id);
        categoryService.deleteCategory(id);
        return success(true);
    }


}
