package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.RechargePlanService;
import com.gxwebsoft.shop.entity.RechargePlan;
import com.gxwebsoft.shop.param.RechargePlanParam;
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 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-04-20 20:36:24
 */
@Api(tags = "会员充值套餐表管理")
@RestController
@RequestMapping("/api/shop/recharge-plan")
public class RechargePlanController extends BaseController {
    @Resource
    private RechargePlanService rechargePlanService;

    @PreAuthorize("hasAuthority('shop:rechargePlan:list')")
    @OperationLog
    @ApiOperation("分页查询会员充值套餐表")
    @GetMapping("/page")
    public ApiResult<PageResult<RechargePlan>> page(RechargePlanParam param) {
        PageParam<RechargePlan, RechargePlanParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(rechargePlanService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(rechargePlanService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:rechargePlan:list')")
    @OperationLog
    @ApiOperation("查询全部会员充值套餐表")
    @GetMapping()
    public ApiResult<List<RechargePlan>> list(RechargePlanParam param) {
        PageParam<RechargePlan, RechargePlanParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(rechargePlanService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(rechargePlanService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:rechargePlan:list')")
    @OperationLog
    @ApiOperation("根据id查询会员充值套餐表")
    @GetMapping("/{id}")
    public ApiResult<RechargePlan> get(@PathVariable("id") Integer id) {
        return success(rechargePlanService.getById(id));
        // 使用关联查询
        //return success(rechargePlanService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:rechargePlan:save')")
    @OperationLog
    @ApiOperation("添加会员充值套餐表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody RechargePlan rechargePlan) {
        // 记录当前登录用户id、租户id、商户编号
        User loginUser = getLoginUser();
        if (loginUser != null) {
          rechargePlan.setUserId(loginUser.getUserId());
          rechargePlan.setMerchantCode(getMerchantCode());
        }
        if (rechargePlanService.save(rechargePlan)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:rechargePlan:update')")
    @OperationLog
    @ApiOperation("修改会员充值套餐表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody RechargePlan rechargePlan) {
        if (rechargePlanService.updateById(rechargePlan)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @PreAuthorize("hasAuthority('shop:rechargePlan:save')")
    @OperationLog
    @ApiOperation("批量添加会员充值套餐表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<RechargePlan> list) {
        if (rechargePlanService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:rechargePlan:update')")
    @OperationLog
    @ApiOperation("批量修改会员充值套餐表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<RechargePlan> batchParam) {
        if (batchParam.update(rechargePlanService, "plan_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
