package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.shop.entity.UserGradeLog;
import com.gxwebsoft.shop.param.UserGradeLogParam;
import com.gxwebsoft.shop.service.UserGradeLogService;
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 2023-06-20 16:33:06
 */
@Api(tags = "用户会员等级变更记录表管理")
@RestController
@RequestMapping("/api/shop/user-grade-log")
public class UserGradeLogController extends BaseController {
    @Resource
    private UserGradeLogService userGradeLogService;

    @PreAuthorize("hasAuthority('shop:userGradeLog:list')")
    @OperationLog
    @ApiOperation("分页查询用户会员等级变更记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserGradeLog>> page(UserGradeLogParam param) {
        // 使用关联查询
        return success(userGradeLogService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:list')")
    @OperationLog
    @ApiOperation("查询全部用户会员等级变更记录表")
    @GetMapping()
    public ApiResult<List<UserGradeLog>> list(UserGradeLogParam param) {
        // 使用关联查询
        return success(userGradeLogService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:list')")
    @OperationLog
    @ApiOperation("根据id查询用户会员等级变更记录表")
    @GetMapping("/{id}")
    public ApiResult<UserGradeLog> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(userGradeLogService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:save')")
    @OperationLog
    @ApiOperation("添加用户会员等级变更记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserGradeLog userGradeLog) {
        if (userGradeLogService.save(userGradeLog)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:update')")
    @OperationLog
    @ApiOperation("修改用户会员等级变更记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserGradeLog userGradeLog) {
        if (userGradeLogService.updateById(userGradeLog)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:remove')")
    @OperationLog
    @ApiOperation("删除用户会员等级变更记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userGradeLogService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:save')")
    @OperationLog
    @ApiOperation("批量添加用户会员等级变更记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserGradeLog> list) {
        if (userGradeLogService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:update')")
    @OperationLog
    @ApiOperation("批量修改用户会员等级变更记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserGradeLog> batchParam) {
        if (batchParam.update(userGradeLogService, "log_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:userGradeLog:remove')")
    @OperationLog
    @ApiOperation("批量删除用户会员等级变更记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userGradeLogService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
