package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.RechargeOrderPlanService;
import com.gxwebsoft.shop.entity.RechargeOrderPlan;
import com.gxwebsoft.shop.param.RechargeOrderPlanParam;
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-order-plan")
public class RechargeOrderPlanController extends BaseController {
    @Resource
    private RechargeOrderPlanService rechargeOrderPlanService;

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

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

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

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

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

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

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

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

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

}
