package com.cpop.mall.business.controller.mini;

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.mall.business.bo.*;
import com.cpop.mall.business.entity.OrderEvaluate;
import com.cpop.mall.business.service.OrderEvaluateService;
import com.cpop.mall.business.service.OrderRefundService;
import com.cpop.mall.business.service.OrderService;
import com.cpop.mall.business.vo.OrderInfoVo;
import com.cpop.mall.business.vo.OrderPageVo;
import com.cpop.mall.business.vo.OrderRefundPageVo;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.cpop.mall.business.entity.table.OrderTableDef.ORDER;

/**
 * @author DB
 * @createTime 2023/10/25 9:19
 * @description
 */
@RestController
@Api(tags = "小程序商城-订单管理")
@RequestMapping("/mini/order")
public class MiniOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * @descriptions 分页查询商城-商品
     * @author DB
     * @date 2023/10/23 11:56
     * @param bo 分页参数
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getOrderPage")
    @ApiOperation("小程序-我的订单列表")
    public R<Page<OrderPageVo>> getOrderPage(@ApiParam("分页参数") OrderPageBo bo) {
        Page<OrderPageVo> page = orderService.getOrderPage(bo);
        return R.ok(page);
    }

    /**
     * @descriptions 生成预订单
     * @author DB
     * @date 2023/11/06 11:12
     * @param bo 请求参数
     * @return: com.cpop.core.base.R<java.lang.Object>
     */
    @PostMapping("/createAdvanceOrder")
    @ApiOperation("商城-订单-生成预订单")
    public R<String> createAdvanceOrder(@RequestBody @Validated @ApiParam("商城-预订单参数") AdvanceOrderBo bo) {
        return R.ok(orderService.createAdvanceOrder(bo));
    }

    /**
     * @descriptions 商城-订单-下单
     * @author DB
     * @date 2023/10/23 12:15
     * @param bo 下单请求对象
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PostMapping("/placeOrder")
    @ApiOperation("商城-订单-下单")
    public R<Object> placeOrder(@RequestBody @Validated @ApiParam("商城-订单") PlaceOrderBo bo) {
        return R.ok(orderService.placeOrder(bo));
    }

    /**
     * @param bo 评价订单对象
     * @descriptions 评价订单
     * @author DB
     * @date 2023/10/23 12:15
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PostMapping("/orderEvaluate")
    @ApiOperation("评价订单")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> orderEvaluate(@RequestBody @Validated @ApiParam("评价订单对象") OrderEvaluateBo bo) {
        SpringUtils.getBean(OrderEvaluateService.class).save(BeanUtils.mapToClass(bo, OrderEvaluate.class));
        //修改订单
        orderService.updateChain().set(ORDER.HAVE_EVALUATE, true).where(ORDER.ID.eq(bo.getOrderId())).update();
        return R.ok();
    }

    /**
     * @descriptions 申请退款
     * @author DB
     * @date 2023/10/26 17:22
     * @param bo 申请退款对象
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PostMapping("/applyRefund")
    @ApiOperation("申请退款")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> applyRefund(@RequestBody @Validated @ApiParam("申请退款对象") OrderApplyRefundBo bo) {
        orderService.applyRefund(bo);
        return R.ok();
    }

    /**
     * @descriptions 取消订单
     * @author DB
     * @date 2023/10/27 17:16
     * @param orderId 商城-订单Id
     * @return: com.cpop.core.base.R<java.lang.Object>
     */
    @PutMapping("/cancelOrder")
    @ApiOperation("取消订单")
    public R<Void> cancelOrder(@RequestParam("orderId") @ApiParam("商城-订单Id") String orderId) {
        orderService.cancelOrder(orderId);
        return R.ok();
    }

    /**
     * @descriptions 小程序-重新支付订单
     * @author DB
     * @date 2023/11/03 15:53
     * @param orderId 订单id
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.vo.OrderPageVo>>
     */
    @Deprecated
    @PutMapping("/rePayOrder/{orderId}")
    @ApiOperation("小程序-重新支付订单")
    public R<Object> rePayOrder(@PathVariable @ApiParam(value = "订单id", required = true) String orderId) {
        return R.ok(orderService.rePayOrder(orderId));
    }

    /**
     * @descriptions 小程序-获取订单详情
     * @author DB
     * @date 2023/11/06 14:25
     * @param orderId 订单id
     * @return: com.cpop.core.base.R<java.lang.Object>
     */
    @GetMapping("/getOrderInfo/{orderId}")
    @ApiOperation("小程序-获取订单详情")
    public R<OrderInfoVo> getOrderInfo(@PathVariable @ApiParam(value = "订单id", required = true) String orderId) {
        return R.ok(orderService.getOrderInfo(orderId));
    }

}
