package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.DesignCategoryService;
import com.gxwebsoft.cms.entity.DesignCategory;
import com.gxwebsoft.cms.param.DesignCategoryParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 设计征集报名分类控制器
 *
 * @author 科技小王子
 * @since 2024-10-10 09:23:25
 */
@Api(tags = "设计征集报名分类管理")
@RestController
@RequestMapping("/api/cms/design-category")
public class DesignCategoryController extends BaseController {
    @Resource
    private DesignCategoryService designCategoryService;

    @ApiOperation("分页查询设计征集报名分类")
    @GetMapping("/page")
    public ApiResult<PageResult<DesignCategory>> page(DesignCategoryParam param) {
        // 使用关联查询
        return success(designCategoryService.pageRel(param));
    }

    @ApiOperation("查询全部设计征集报名分类")
    @GetMapping()
    public ApiResult<List<DesignCategory>> list(DesignCategoryParam param) {
        // 使用关联查询
        return success(designCategoryService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:designCategory:list')")
    @ApiOperation("根据id查询设计征集报名分类")
    @GetMapping("/{id}")
    public ApiResult<DesignCategory> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(designCategoryService.getByIdRel(id));
    }

    @ApiOperation("添加设计征集报名分类")
    @PostMapping()
    public ApiResult<?> save(@RequestBody DesignCategory designCategory) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         designCategory.setUserId(loginUser.getUserId());
        }
        if (designCategoryService.save(designCategory)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改设计征集报名分类")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DesignCategory designCategory) {
        if (designCategoryService.updateById(designCategory)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除设计征集报名分类")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (designCategoryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加设计征集报名分类")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<DesignCategory> list) {
        if (designCategoryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改设计征集报名分类")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<DesignCategory> batchParam) {
        if (batchParam.update(designCategoryService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除设计征集报名分类")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (designCategoryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
