package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.DesignCollectService;
import com.gxwebsoft.cms.entity.DesignCollect;
import com.gxwebsoft.cms.param.DesignCollectParam;
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-08 15:21:56
 */
@Api(tags = "设计征集管理")
@RestController
@RequestMapping("/api/cms/design-collect")
public class DesignCollectController extends BaseController {
    @Resource
    private DesignCollectService designCollectService;

    @ApiOperation("分页查询设计征集")
    @GetMapping("/page")
    public ApiResult<PageResult<DesignCollect>> page(DesignCollectParam param) {
        // 使用关联查询
        return success(designCollectService.pageRel(param));
    }

    @ApiOperation("查询全部设计征集")
    @GetMapping()
    public ApiResult<List<DesignCollect>> list(DesignCollectParam param) {
        // 使用关联查询
        return success(designCollectService.listRel(param));
    }

    @ApiOperation("根据id查询设计征集")
    @GetMapping("/{id}")
    public ApiResult<DesignCollect> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(designCollectService.getByIdRel(id));
    }

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

    @ApiOperation("修改设计征集")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DesignCollect designCollect) {
        if (designCollectService.updateById(designCollect)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
