package com.fzm.mall.server.admin.goods_center.controller;


import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.goods_center.entity.dto.CategoryDTO;
import com.fzm.mall.server.admin.goods_center.entity.dto.CategorySaveDTO;
import com.fzm.mall.server.admin.goods_center.entity.dto.CategoryUpdateDTO;
import com.fzm.mall.server.admin.goods_center.service.ICategoryService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 商品类别 前端控制器
 * </p>
 *
 * @author fzm
 * @since 2021-03-04
 */
@Api(tags = "商品中心")
@RestController
@RequestMapping("/category/categoryPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CategoryController {

    private final ICategoryService categoryService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "商品中心-商品类别-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "商品中心-商品类别-新增", module = OperateModuleEnum.GOODS_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid CategorySaveDTO categorySaveDTO) {

        categoryService.save(categorySaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "商品中心-商品类别-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "商品中心-商品类别-编辑", module = OperateModuleEnum.GOODS_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid CategoryUpdateDTO categoryUpdateDTO) {

        categoryService.update(categoryUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "商品中心-商品类别-删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "商品中心-商品类别-删除", module = OperateModuleEnum.GOODS_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{categoryId}")
    public ResponseVO delete(
            @ApiParam(value = "系列编号", required = true) @PathVariable(value = "categoryId") String categoryId) {
        categoryService.delete(categoryId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "商品中心-商品类别-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO treeList() {
        List<CategoryDTO> result = categoryService.treeList();
        return resFac.getSuccessResponseWithData(result);
    }
}
