package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.entity.AdRecord;
import com.gxwebsoft.cms.param.AdRecordParam;
import com.gxwebsoft.cms.service.AdRecordService;
import com.gxwebsoft.common.core.web.*;
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-03-04 21:14:15
 */
@Api(tags = "广告图片")
@RestController
@RequestMapping("/api/cms/ad-record")
public class AdRecordController extends BaseController {
    @Resource
    private AdRecordService adRecordService;

    @ApiOperation("分页查询广告图片记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<AdRecord>> page(AdRecordParam param) {
        // 使用关联查询
        return success(adRecordService.pageRel(param));
    }

    @ApiOperation("查询全部广告图片记录表")
    @GetMapping()
    public ApiResult<List<AdRecord>> list(AdRecordParam param) {
        PageParam<AdRecord, AdRecordParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(adRecordService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(adRecordService.listRel(param));
    }

    @ApiOperation("根据id查询广告图片记录表")
    @GetMapping("/{id}")
    public ApiResult<AdRecord> get(@PathVariable("id") Integer id) {
        return success(adRecordService.getById(id));
        // 使用关联查询
        //return success(adRecordService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:adRecord:list')")
    @ApiOperation("添加广告图片记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AdRecord adRecord) {
        if (adRecordService.save(adRecord)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:adRecord:update')")
    @ApiOperation("修改广告图片记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AdRecord adRecord) {
        if (adRecordService.updateById(adRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:adRecord:remove')")
    @ApiOperation("删除广告图片记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (adRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:adRecord:save')")
    @ApiOperation("批量添加广告图片记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AdRecord> list) {
        if (adRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:adRecord:update')")
    @ApiOperation("批量修改广告图片记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<AdRecord> batchParam) {
        if (batchParam.update(adRecordService, "ad_record_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:adRecord:remove')")
    @ApiOperation("批量删除广告图片记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (adRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
