package org.springblade.modules.api.controller;

import cn.hutool.core.bean.BeanUtil;
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.MerchantDetailOrderVO;
import org.springblade.modules.finance.vo.MerchantPageOrderVO;
import org.springframework.context.annotation.Lazy;
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;

/**
 * 商家端
 */
@RestController
@AllArgsConstructor
@RequestMapping(CommonConstant.API_URL + "/orderShop")
@Api(value = "商品订单接口-商家", tags = "商品订单接口-商家")
public class ApiMerchantOrderController {

    @Lazy
    private final IOrderGoodsService iOrderGoodsService;

    private final IOrderService iOrderService;

    @ApiLog("订单列表(订单管理)")
    @PostMapping("/pageOrder")
    @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<MerchantDetailOrderVO> 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, "未查询到商品订单信息");
        MerchantDetailOrderVO merchantDetailOrderVO = new MerchantDetailOrderVO();
        BeanUtil.copyProperties(order,merchantDetailOrderVO);
        return R.data(merchantDetailOrderVO);
    }


}
