package org.springblade.modules.api.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderGoods;
import org.springblade.modules.finance.service.IOrderGoodsService;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.finance.vo.MerchantPageOrderVO;
import org.springblade.modules.finance.vo.OrderGoodsVO;
import org.springblade.modules.finance.wrapper.OrderGoodsWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author pangqijun
 * Date 2024/7/18
 * Description
 */
@RestController
@AllArgsConstructor
@RequestMapping(CommonConstant.API_URL + "/orderGoods")
@Api(value = "移动端商品订单接口", tags = "移动端商品订单接口")
public class ApiOrderGoodsController {

    private final IOrderGoodsService iOrderGoodsService;
    private final IOrderService iOrderService;

    @GetMapping("/user")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取用户订单")
    public R<IPage<OrderGoodsVO>> getUserOrder(Query query, OrderGoodsVO orderGoodsVO) {
        orderGoodsVO.setUserId(AuthUtil.getUserId());
        IPage<OrderGoodsVO> orderGoodsVOIPage = iOrderGoodsService.selectUserOrder(Condition.getPage(query), orderGoodsVO);
        return R.data(orderGoodsVOIPage);
    }

    @ApiLog("订单列表(订单管理)")
    @PostMapping("/getMerchantOrder")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "通用下单")
    public R<IPage<MerchantPageOrderVO>> getMerchantOrder(MerchantPageOrderVO merchantPageOrderVO, Query query) {
        merchantPageOrderVO.setUserId(AuthUtil.getUserId());
        IPage<MerchantPageOrderVO> pages = iOrderGoodsService.getMerchantOrder(Condition.getPage(query), merchantPageOrderVO);
        return R.data(pages);
    }

    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取订单详情")
    public R<OrderGoodsVO> detail(Long orderId) {
        Order order = iOrderService.getById(orderId);
        Assert.notNull(order, "未查询到订单信息");
        OrderGoods orderGoods = iOrderGoodsService.getOne(Wrappers.lambdaQuery(OrderGoods.class)
                .eq(OrderGoods::getOrderId, orderId));
        Assert.notNull(orderGoods, "未查询到商品订单信息");
        OrderGoodsVO orderGoodsVO = OrderGoodsWrapper.build().entityVO(orderGoods);
        orderGoodsVO.setOrderState(order.getOrderState());
        orderGoodsVO.setOrderNo(order.getOrderNo());
        orderGoodsVO.setPaymentAmount(order.getPaymentAmount());
        orderGoodsVO.setTotalAmount(order.getTotalAmount());
        orderGoodsVO.setDiscountAmount(order.getDiscountAmount());
        return R.data(orderGoodsVO);
    }

}
