package com.gxwebsoft.love.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.love.entity.UserPlanIcon;
import com.gxwebsoft.love.param.UserPlanIconParam;
import com.gxwebsoft.love.service.UserPlanIconService;
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-25 18:09:34
 */
@Api(tags = "会员套餐权益表管理")
@RestController
@RequestMapping("/api/love/user-plan-icon")
public class UserPlanIconController extends BaseController {
    @Resource
    private UserPlanIconService userPlanIconService;

    @PreAuthorize("hasAuthority('love:userPlanIcon:list')")
    @OperationLog
    @ApiOperation("分页查询会员套餐权益表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserPlanIcon>> page(UserPlanIconParam param) {
        PageParam<UserPlanIcon, UserPlanIconParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(userPlanIconService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(userPlanIconService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:list')")
    @OperationLog
    @ApiOperation("查询全部会员套餐权益表")
    @GetMapping()
    public ApiResult<List<UserPlanIcon>> list(UserPlanIconParam param) {
        PageParam<UserPlanIcon, UserPlanIconParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(userPlanIconService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(userPlanIconService.listRel(param));
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:list')")
    @OperationLog
    @ApiOperation("根据id查询会员套餐权益表")
    @GetMapping("/{id}")
    public ApiResult<UserPlanIcon> get(@PathVariable("id") Integer id) {
        return success(userPlanIconService.getById(id));
        // 使用关联查询
        //return success(userPlanIconService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:save')")
    @OperationLog
    @ApiOperation("添加会员套餐权益表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserPlanIcon userPlanIcon) {
        if (userPlanIconService.save(userPlanIcon)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:update')")
    @OperationLog
    @ApiOperation("修改会员套餐权益表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserPlanIcon userPlanIcon) {
        if (userPlanIconService.updateById(userPlanIcon)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:remove')")
    @OperationLog
    @ApiOperation("删除会员套餐权益表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userPlanIconService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:save')")
    @OperationLog
    @ApiOperation("批量添加会员套餐权益表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserPlanIcon> list) {
        if (userPlanIconService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:update')")
    @OperationLog
    @ApiOperation("批量修改会员套餐权益表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserPlanIcon> batchParam) {
        if (batchParam.update(userPlanIconService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanIcon:remove')")
    @OperationLog
    @ApiOperation("批量删除会员套餐权益表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userPlanIconService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
