package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.SplashService;
import com.gxwebsoft.shop.entity.Splash;
import com.gxwebsoft.shop.param.SplashParam;
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-09-30 22:07:41
 */
@Api(tags = "开屏广告管理")
@RestController
@RequestMapping("/api/shop/splash")
public class SplashController extends BaseController {
    @Resource
    private SplashService splashService;

    @ApiOperation("分页查询开屏广告")
    @GetMapping("/page")
    public ApiResult<PageResult<Splash>> page(SplashParam param) {
        // 使用关联查询
        return success(splashService.pageRel(param));
    }

    @ApiOperation("查询全部开屏广告")
    @GetMapping()
    public ApiResult<List<Splash>> list(SplashParam param) {
        // 使用关联查询
        return success(splashService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:splash:list')")
    @ApiOperation("根据id查询开屏广告")
    @GetMapping("/{id}")
    public ApiResult<Splash> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(splashService.getByIdRel(id));
    }

    @ApiOperation("添加开屏广告")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Splash splash) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         splash.setUserId(loginUser.getUserId());
        }
        if (splashService.save(splash)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改开屏广告")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Splash splash) {
        if (splashService.updateById(splash)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除开屏广告")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (splashService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加开屏广告")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Splash> list) {
        if (splashService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改开屏广告")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Splash> batchParam) {
        if (batchParam.update(splashService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除开屏广告")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (splashService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
