package com.cpop.mall.framework.task;

import com.alibaba.fastjson.JSONObject;
import com.cpop.core.base.enums.OrderSource;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.utils.SpringUtils;
import com.cpop.jambox.business.entity.StoreExtend;
import com.cpop.jambox.business.service.StoreExtendService;
import com.cpop.jambox.framework.constant.JamboxCloudUrl;
import com.cpop.mall.business.entity.Order;
import com.cpop.mall.business.entity.OrderDetail;
import com.cpop.mall.business.entity.Product;
import com.cpop.mall.business.entity.ProductRecord;
import com.cpop.mall.business.service.ProductRecordService;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.cpop.jambox.business.entity.table.StoreExtendTableDef.STORE_EXTEND;

/**
 * @author DB
 * @createTime 2023/11/10 17:11
 * @description 订单详情异步任务
 */
@Component
public class OrderDetailAsyncTask {

    /**
     * 云地址
     */
    @Value("${mall.cloudBaseUrl}")
    private String cloudBaseUrl;

    /**
     * @descriptions 订单详情异步任务
     * @author DB
     * @date 2023/11/13 15:37
     * @param order 订单
     * @param loginUserInfo 用户详情
     * @param orderDetails 订单详情
     * @param productList 产品列表
     * @return: void
     */
    @Async("customAsyncThreadPool")
    public void asyncProcessingOrderDetails(Order order, String phoneNumber,String nickName, List<OrderDetail> orderDetails, List<Product> productList) {
        Map<String, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getId, item -> item));
        List<ProductRecord> productRecords = SpringUtils.getBean(ProductRecordService.class).listByIds(orderDetails.stream().map(OrderDetail::getProductRecordId).collect(Collectors.toSet()));
        Map<String, ProductRecord> productRecordMap = productRecords.stream().collect(Collectors.toMap(ProductRecord::getId, item -> item));
        //获取云校区id
        List<StoreExtend> storeExtendList = SpringUtils.getBean(StoreExtendService.class).queryChain()
                .where(STORE_EXTEND.STORE_ID.in(orderDetails.stream().map(OrderDetail::getStoreId).collect(Collectors.toSet())))
                .list();
        Map<String, String> storeCloudMap = storeExtendList.stream().collect(Collectors.toMap(StoreExtend::getStoreId, StoreExtend::getStoreCloudId));
        orderDetails.forEach(item -> {
            //获取当前订单记录的商品信息
            ProductRecord productRecord = productRecordMap.get(item.getProductRecordId());
            Product product = productMap.get(productRecord.getProductId());
            //课卡
            if (product.getProductType() == 0) {
                //课卡信息
                JSONObject jsonBody = new JSONObject();
                jsonBody.put("_type", "addPeriod");
                //订单id
                jsonBody.put("mallOrderId", order.getId());
                //办卡实收金额
                jsonBody.put("money", productRecord.getRecordPrice());
                //店铺/校区
                jsonBody.put("storeId", storeCloudMap.get(item.getStoreId()));
                //手机号
                jsonBody.put("phone", phoneNumber);
                //客户名称
                jsonBody.put("customerName", nickName);
                //模板id
                jsonBody.put("templateId", product.getCardTemplateId());
                //订单来源
                jsonBody.put("orderSource", OrderSource.MALL.toString());
                //模板对应实际地址
                jsonBody.put("templateUrl", cloudBaseUrl);
                //获取课卡信息
                SpringUtils.getBean(RestTemplate.class).postForObject(JamboxCloudUrl.COMMON_CARD_URL, jsonBody, JSONObject.class);
            }
        });
    }
}
