package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.DesignSignUpService;
import com.gxwebsoft.cms.entity.DesignSignUp;
import com.gxwebsoft.cms.param.DesignSignUpParam;
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-sign-up")
public class DesignSignUpController extends BaseController {
    @Resource
    private DesignSignUpService designSignUpService;

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

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

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

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

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

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

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

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

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

}
