package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.OrderRenewService;
import com.gxwebsoft.shop.entity.OrderRenew;
import com.gxwebsoft.shop.param.OrderRenewParam;
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-13 10:34:35
 */
@Api(tags = "续费订单记录表管理")
@RestController
@RequestMapping("/api/shop/order-renew")
public class OrderRenewController extends BaseController {
    @Resource
    private OrderRenewService orderRenewService;

    @PreAuthorize("hasAuthority('shop:orderRenew:list')")
    @OperationLog
    @ApiOperation("分页查询续费订单记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<OrderRenew>> page(OrderRenewParam param) {
        PageParam<OrderRenew, OrderRenewParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(orderRenewService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(orderRenewService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:list')")
    @OperationLog
    @ApiOperation("查询全部续费订单记录表")
    @GetMapping()
    public ApiResult<List<OrderRenew>> list(OrderRenewParam param) {
        PageParam<OrderRenew, OrderRenewParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(orderRenewService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(orderRenewService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:list')")
    @OperationLog
    @ApiOperation("根据id查询续费订单记录表")
    @GetMapping("/{id}")
    public ApiResult<OrderRenew> get(@PathVariable("id") Integer id) {
        return success(orderRenewService.getById(id));
        // 使用关联查询
        //return success(orderRenewService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:save')")
    @OperationLog
    @ApiOperation("添加续费订单记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OrderRenew orderRenew) {
        // 记录当前登录用户id、租户id、商户编号
        User loginUser = getLoginUser();
        if (loginUser != null) {
          orderRenew.setUserId(loginUser.getUserId());
          orderRenew.setMerchantCode(getMerchantCode());
        }
        if (orderRenewService.save(orderRenew)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:update')")
    @OperationLog
    @ApiOperation("修改续费订单记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OrderRenew orderRenew) {
        if (orderRenewService.updateById(orderRenew)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:remove')")
    @OperationLog
    @ApiOperation("删除续费订单记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderRenewService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:save')")
    @OperationLog
    @ApiOperation("批量添加续费订单记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OrderRenew> list) {
        if (orderRenewService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:update')")
    @OperationLog
    @ApiOperation("批量修改续费订单记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OrderRenew> batchParam) {
        if (batchParam.update(orderRenewService, "order_renew_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRenew:remove')")
    @OperationLog
    @ApiOperation("批量删除续费订单记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderRenewService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
