package com.gxwebsoft.love.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.love.entity.UserPlanPrice;
import com.gxwebsoft.love.param.UserPlanPriceParam;
import com.gxwebsoft.love.service.UserPlanPriceService;
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 18:07:50
 */
@Api(tags = "会员套餐定价表管理")
@RestController
@RequestMapping("/api/love/user-plan-price")
public class UserPlanPriceController extends BaseController {
    @Resource
    private UserPlanPriceService userPlanPriceService;

    @PreAuthorize("hasAuthority('love:userPlanPrice:list')")
    @OperationLog
    @ApiOperation("分页查询会员套餐定价表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserPlanPrice>> page(UserPlanPriceParam param) {
        // 使用关联查询
        return success(userPlanPriceService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('love:userPlanPrice:list')")
    @OperationLog
    @ApiOperation("查询全部会员套餐定价表")
    @GetMapping()
    public ApiResult<List<UserPlanPrice>> list(UserPlanPriceParam param) {
        // 使用关联查询
        return success(userPlanPriceService.listRel(param));
    }

    @PreAuthorize("hasAuthority('love:userPlanPrice:list')")
    @OperationLog
    @ApiOperation("根据id查询会员套餐定价表")
    @GetMapping("/{id}")
    public ApiResult<UserPlanPrice> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(userPlanPriceService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('love:userPlanPrice:save')")
    @OperationLog
    @ApiOperation("添加会员套餐定价表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserPlanPrice userPlanPrice) {
        if (userPlanPriceService.save(userPlanPrice)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanPrice:update')")
    @OperationLog
    @ApiOperation("修改会员套餐定价表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserPlanPrice userPlanPrice) {
        if (userPlanPriceService.updateById(userPlanPrice)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
