package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.SwiperService;
import com.gxwebsoft.shop.entity.Swiper;
import com.gxwebsoft.shop.param.SwiperParam;
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/swiper")
public class SwiperController extends BaseController {
    @Resource
    private SwiperService swiperService;

    @ApiOperation("分页查询轮播图")
    @GetMapping("/page")
    public ApiResult<PageResult<Swiper>> page(SwiperParam param) {
        // 使用关联查询
        return success(swiperService.pageRel(param));
    }

    @ApiOperation("查询全部轮播图")
    @GetMapping()
    public ApiResult<List<Swiper>> list(SwiperParam param) {
        // 使用关联查询
        return success(swiperService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:swiper:list')")
    @ApiOperation("根据id查询轮播图")
    @GetMapping("/{id}")
    public ApiResult<Swiper> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(swiperService.getByIdRel(id));
    }

    @ApiOperation("添加轮播图")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Swiper swiper) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         swiper.setUserId(loginUser.getUserId());
        }
        if (swiperService.save(swiper)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改轮播图")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Swiper swiper) {
        if (swiperService.updateById(swiper)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除轮播图")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (swiperService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加轮播图")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Swiper> list) {
        if (swiperService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改轮播图")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Swiper> batchParam) {
        if (batchParam.update(swiperService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除轮播图")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (swiperService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
