package cn.newfeifan.mall.module.trade.controller.admin.order;

import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.module.member.api.user.MemberUserApi;
import cn.newfeifan.mall.module.member.api.user.dto.MemberUserRespDTO;
import cn.newfeifan.mall.module.pay.dal.dataobject.order.PayOrderDO;
import cn.newfeifan.mall.module.pay.service.order.PayOrderService;
import cn.newfeifan.mall.module.trade.controller.admin.aftersale.vo.AfterSaleBaseVO;
import cn.newfeifan.mall.module.trade.controller.admin.order.vo.*;
import cn.newfeifan.mall.module.trade.controller.admin.order.vo.detailvo.TradeOrderDeliveryVO;
import cn.newfeifan.mall.module.trade.controller.admin.order.vo.detailvo.TradeOrderPayVo;
import cn.newfeifan.mall.module.trade.controller.app.order.vo.AppTradeOrderDetailRespVO;
import cn.newfeifan.mall.module.trade.convert.aftersale.AfterSaleConvert;
import cn.newfeifan.mall.module.trade.convert.order.TradeOrderConvert;
import cn.newfeifan.mall.module.trade.dal.dataobject.aftersale.AfterSaleDO;
import cn.newfeifan.mall.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.newfeifan.mall.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.newfeifan.mall.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.newfeifan.mall.module.trade.dal.dataobject.order.TradeOrderLogDO;
import cn.newfeifan.mall.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.newfeifan.mall.module.trade.framework.order.config.TradeOrderProperties;
import cn.newfeifan.mall.module.trade.service.aftersale.AfterSaleService;
import cn.newfeifan.mall.module.trade.service.delivery.DeliveryExpressService;
import cn.newfeifan.mall.module.trade.service.order.TradeOrderLogService;
import cn.newfeifan.mall.module.trade.service.order.TradeOrderQueryService;
import cn.newfeifan.mall.module.trade.service.order.TradeOrderUpdateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 订单详情")
@RestController
@RequestMapping("/trade/order-detail")
@Validated
@Slf4j
public class TradeOrderDetailController {

    @Resource
    private TradeOrderUpdateService tradeOrderUpdateService;
    @Resource
    private TradeOrderQueryService tradeOrderQueryService;
    @Resource
    private TradeOrderLogService tradeOrderLogService;
    @Resource
    private TradeOrderProperties tradeOrderProperties;

    @Resource
    private PayOrderService payOrderService;

    @Resource
    private MemberUserApi memberUserApi;
    @Resource
    private AfterSaleService afterSaleService;

    @Resource
    private DeliveryExpressService deliveryExpressService;

    @GetMapping("/get-detail")
    @Operation(summary = "订单基本信息")
    @Parameter(name = "id", description = "订单编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<TradeOrderDetailRespVO> getOrderDetail(@RequestParam("id") Long id) {
        // 查询订单
        TradeOrderDO order = tradeOrderQueryService.getOrder(id);
        if (order == null) {
            return success(null);
        }
        // 查询订单项
        List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(id);

        // 拼接数据
        MemberUserRespDTO user = memberUserApi.getUser(order.getUserId());
        MemberUserRespDTO brokerageUser = order.getBrokerageUserId() != null ?
                memberUserApi.getUser(order.getBrokerageUserId()) : null;
        List<TradeOrderLogDO> orderLogs = tradeOrderLogService.getOrderLogListByOrderId(id);
        return success(TradeOrderConvert.INSTANCE.convert(order, orderItems, orderLogs, user, brokerageUser));
    }

    @GetMapping("/after_sale")
    @Operation(summary = "新增接口20240229-售后信息")
    @Parameter(name = "id", description = "订单编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<? extends List<? extends AfterSaleBaseVO>> afterSale(@RequestParam("id") Long id) {
        // 查询订单
        TradeOrderDO order = tradeOrderQueryService.getOrder(id);
        if (order == null) {
            return success(null);
        }
        // 查询售后订单
        List<AfterSaleDO> afterSales = afterSaleService.getAfterSaleByOrderId(order.getId());
        return success(AfterSaleConvert.INSTANCE.convertList2(afterSales));
    }

    @GetMapping("/delivery")
    @Operation(summary = "新增接口20240229-物流信息")
    @Parameter(name = "id", description = "订单编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<TradeOrderDeliveryVO> delivery(@RequestParam("id") Long id) {
        // 查询订单
        TradeOrderDO order = tradeOrderQueryService.getOrder(id);
        if (order == null) {
            return success(null);
        }
        TradeOrderDeliveryVO build = TradeOrderDeliveryVO.builder()
                .logisticsNo(order.getLogisticsNo())
                .deliveryType(DeliveryTypeEnum.getNameByValue(order.getDeliveryType()))
                .deliveryPrice(order.getDeliveryPrice())
                .logisticsId(order.getLogisticsId())
                .remark(order.getRemark()).build();
        return success(build);
    }

    @GetMapping("/payInfo")
    @Operation(summary = "新增接口20240229-支付信息")
    @Parameter(name = "id", description = "订单编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<TradeOrderPayVo> payInfo(@RequestParam("id") Long id) {
        // 查询订单
        TradeOrderDO order = tradeOrderQueryService.getOrder(id);
        if (order == null) {
            return success(null);
        }
        // 查询支付状态
        PayOrderDO payOrderDO = payOrderService.getOrderByTradeOrderId(id);
        TradeOrderPayVo build = TradeOrderPayVo.builder()
                .userRemark(order.getUserRemark())
                .payChannelCode(order.getPayChannelCode())
                .path("")
                .payPrice(order.getPayPrice())
                .build();
        if (payOrderDO == null){
            build.setPayStatus("未支付");
        }else {
            build.setPayStatus(payOrderDO.getStatus() == 0 ? "未支付" : "已支付");
        }
        return success(null);
    }

    @GetMapping("/productInfo")
    @Operation(summary = "新增接口20240229-商品订单信息信息")
    @Parameter(name = "id", description = "订单编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<AppTradeOrderDetailRespVO> productInfo(@RequestParam("id") Long id) {
        // 查询订单
        TradeOrderDO order = tradeOrderQueryService.getOrder(id);
        if (order == null) {
            return success(null);
        }

        // 查询订单项
        List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(order.getId());
        // 查询物流公司
        DeliveryExpressDO express = order.getLogisticsId() != null && order.getLogisticsId() > 0 ?
                deliveryExpressService.getDeliveryExpress(order.getLogisticsId()) : null;
        // 最终组合
        return success(TradeOrderConvert.INSTANCE.convert02(order, orderItems, tradeOrderProperties, express));
    }


}
