package com.hfkj.service.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.RandomUtils;
import com.hfkj.common.utils.StarbucksUtil;
import com.hfkj.entity.*;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.model.order.OrderModel;
import com.hfkj.qianzhu.CinemaService;
import com.hfkj.qianzhu.StarbucksService;
import com.hfkj.service.BsProductConfigService;
import com.hfkj.service.coupon.BsOrderCouponNoService;
import com.hfkj.service.coupon.BsOrderCouponService;
import com.hfkj.service.coupon.channel.HuiLianTongCouponService;
import com.hfkj.service.goods.*;
import com.hfkj.service.hlt.HuiLianTongUnionCardService;
import com.hfkj.sysenum.GoodsVpdSourceEnum;
import com.hfkj.sysenum.order.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 创建订单业务
 * @className: OrderCreateService
 * @author: HuRui
 * @date: 2024/5/7
 **/
@Component
public class OrderCreateService {
    Logger log = LoggerFactory.getLogger(OrderCreateService.class);

    @Resource
    private BsOrderCouponService orderCouponService;
    @Resource
    private BsOrderCouponNoService orderCouponNoService;
    @Resource
    private GoodsMsgService goodsMsgService;
    @Resource
    private GoodsSpecsService goodsSpecsService;
    @Resource
    private GoodsVpdService goodsVpdService;
    @Resource
    private GoodsUserAddressService goodsUserAddressService;
    @Resource
    private BsOrderGoodsService bsOrderGoodsService;
    @Resource
    private GoodsShoppingCartService shoppingCartService;
    @Resource
    private BsProductConfigService bsProductConfigService;
    @Resource
    private BsOrderCinemaService bsOrderCinemaService;
    @Resource
    private BsOrderService bsOrderService;
    @Resource
    private GoodsJumpService goodsJumpService;
    @Resource
    private BsOrderStarbucksService orderStarbucksService;
    @Resource
    private BsOrderMemberService orderMemberService;

    /**
     * 事务产品
     * @param orderChild 子订单
     * @throws Exception
     */
    public OrderChildModel product(OrderModel order, OrderChildModel orderChild) throws Exception {
        // 查询商品
        GoodsMsg goodsMsg = goodsMsgService.queryDetail(orderChild.getProductId());
        if (goodsMsg == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品");
        }
        if (goodsMsg.getStatus() != 1) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsMsg.getTitle()+"暂无法就进行购买");
        }
        // 查询商品规格
        GoodsSpecs goodsSpecs = goodsSpecsService.queryDetail(orderChild.getProductSpecId());
        if (goodsSpecs == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品规格");
        }
        if (goodsSpecs.getStock() <= 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "库存不足");
        }
        // 减少库存
        goodsSpecsService.editStockNum(orderChild.getProductSpecId(), orderChild.getProductCount(), 2);

        JSONObject businessObj = orderChild.getBusiness()==null?new JSONObject(): new JSONObject((Map<String, Object>) orderChild.getBusiness());
        if (businessObj.getLong("shopCartId") != null) {
            shoppingCartService.delete(businessObj.getLong("shopCartId"));
        }
        orderChild.setMerId(goodsMsg.getMerId());
        orderChild.setProductName(goodsMsg.getTitle());
        orderChild.setProductImg(goodsMsg.getListImg());
        orderChild.setProductSpecName(goodsSpecs.getName());
        orderChild.setProductPrice(goodsSpecs.getSalePrice());
        // 虚拟
        // 查询商品虚拟信息
        GoodsVpd goodsVpd = goodsVpdService.queryDetailBySpecsId(goodsSpecs.getId());
        if (goodsVpd == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品虚拟规格");
        }
        // 虚拟配置
        orderChild.setBusiness(goodsVpd);
        if (goodsVpd.getSource().equals(GoodsVpdSourceEnum.type2.getCode())) {
            yuShang(order , orderChild , goodsVpd , goodsMsg , goodsSpecs , businessObj);
        } else if (goodsVpd.getSource().equals(GoodsVpdSourceEnum.type4.getCode())) {
            couponHlt(order , orderChild , goodsVpd , goodsMsg , goodsSpecs , businessObj);
        }


        return orderChild;
    }

    public void yuShang(OrderModel order,  OrderChildModel orderChild , GoodsVpd goodsVpd , GoodsMsg goodsMsg , GoodsSpecs goodsSpecs , JSONObject businessObj ) {
        // 创建娱尚订单
        BsOrderMember orderMember = new BsOrderMember();
        orderMember.setOrderNo(orderChild.getOrderNo());
        orderMember.setChildOrderNo(orderChild.getChildOrderNo());
        orderMember.setMerId(orderChild.getMerId());
        orderMember.setPlatformCode(businessObj.getString("platformCode"));
        orderMember.setUserId(order.getUserId());
        orderMember.setUserPhone(order.getUserPhone());
        orderMember.setGoodsId(goodsMsg.getId());
        orderMember.setGoodsName(goodsMsg.getTitle());
        orderMember.setGoodsTypeId(goodsMsg.getGoodsType());
        orderMember.setGoodsTypeName(goodsMsg.getGoodsTypeName());
        orderMember.setGoodsBrandId(goodsMsg.getGoodsBrand());
        orderMember.setGoodsBrandName(goodsMsg.getGoodsBrandName());
        orderMember.setGoodsSpecsId(goodsSpecs.getId());
        orderMember.setGoodsSpecsName(goodsSpecs.getName());
        orderMember.setGoodsSpescOriginalPrice(goodsSpecs.getOriginalPrice());
        orderMember.setGoodsSpescSalePrice(goodsSpecs.getSalePrice());
        orderMember.setGoodsVpdId(goodsVpd.getId());
        orderMember.setGoodsVpdKey(goodsVpd.getKey());
        orderMember.setGoodsVpdSource(goodsVpd.getSource());
        orderMember.setGoodsCount(orderChild.getProductCount());
        orderMember.setStatus(OrderChildStatusEnum.status1.getCode());
        orderMemberService.create(orderMember);

    }

    public void couponHlt(OrderModel order,  OrderChildModel orderChild , GoodsVpd goodsVpd , GoodsMsg goodsMsg , GoodsSpecs goodsSpecs , JSONObject businessObj )  throws Exception{
        // 创建卡券订单
        BsOrderCoupon orderCoupon = new BsOrderCoupon();
        orderCoupon.setOrderNo(orderChild.getOrderNo());
        orderCoupon.setChildOrderNo(orderChild.getChildOrderNo());
        orderCoupon.setMerId(orderChild.getMerId());
        orderCoupon.setPlatformCode(businessObj.getString("platformCode"));
        orderCoupon.setUserId(order.getUserId());
        orderCoupon.setUserPhone(order.getUserPhone());
        orderCoupon.setGoodsId(goodsMsg.getId());
        orderCoupon.setGoodsName(goodsMsg.getTitle());
        orderCoupon.setGoodsTypeId(goodsMsg.getGoodsType());
        orderCoupon.setGoodsTypeName(goodsMsg.getGoodsTypeName());
        orderCoupon.setGoodsBrandId(goodsMsg.getGoodsBrand());
        orderCoupon.setGoodsBrandName(goodsMsg.getGoodsBrandName());
        orderCoupon.setGoodsSpecsId(goodsSpecs.getId());
        orderCoupon.setGoodsSpecsName(goodsSpecs.getName());
        orderCoupon.setGoodsSpescOriginalPrice(goodsSpecs.getOriginalPrice());
        orderCoupon.setGoodsSpescSalePrice(goodsSpecs.getSalePrice());
        orderCoupon.setGoodsVpdId(goodsVpd.getId());
        orderCoupon.setGoodsVpdKey(goodsVpd.getKey());
        orderCoupon.setGoodsVpdSource(goodsVpd.getSource());
        orderCoupon.setGoodsCount(orderChild.getProductCount());
        orderCoupon.setStatus(OrderChildStatusEnum.status1.getCode());
        orderCouponService.editData(orderCoupon);

        for (int i = 0; i < orderCoupon.getGoodsCount(); i++) {
            // 卡券订单卡密
            BsOrderCouponNo orderCouponNo = new BsOrderCouponNo();
            orderCouponNo.setCouponOrderId(orderCoupon.getId());
            orderCouponNo.setOrderNo(orderCoupon.getId() + RandomUtils.number(6, false));
            orderCouponNo.setGoodsVpdKey(orderCoupon.getGoodsVpdKey());
            orderCouponNo.setGoodsVpdSource(orderCoupon.getGoodsVpdSource());
            orderCouponNo.setExpireStatus(false);
            orderCouponNo.setExceptionStatus(false);
            orderCouponNo.setStatus(OrderCouponNoStatusEnum.status1.getCode());

            if (goodsVpd.getSource().equals(GoodsVpdSourceEnum.type4.getCode())) {
                // 下单请求
                JSONObject jsonObject = HuiLianTongCouponService.recharge(orderCouponNo.getOrderNo(),
                        1,
                        orderCoupon.getGoodsVpdKey(),
                        orderCoupon.getUserPhone());
                if (!jsonObject.getString("respCode").equals("0000")) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "下单失败");
                }
                JSONObject response = HuiLianTongUnionCardService.resolveResponse(jsonObject.getString("data"));
                if (!response.getString("respCode").equals("0000")) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("msg"));
                }
                orderCouponNo.setChannelOrderNo(response.getJSONObject("data").getString("rechargeOrderId"));
            }
            orderCouponNoService.editData(orderCouponNo);
        }
    }

    public OrderChildModel goods(OrderModel order, OrderChildModel orderChild) throws Exception {
        // 查询商品
        GoodsMsg goodsMsg = goodsMsgService.queryDetail(orderChild.getProductId());
        if (goodsMsg == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品");
        }
        if (goodsMsg.getStatus() != 1) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsMsg.getTitle()+"暂无法就进行购买");
        }
        if (goodsMsg.getType() != 1) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前商品不属于实物产品不可下单！");
        }
        // 查询商品规格
        GoodsSpecs goodsSpecs = goodsSpecsService.queryDetail(orderChild.getProductSpecId());

        if (goodsSpecs == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品规格");
        }
        JSONObject object = new JSONObject((Map<String, Object>) orderChild.getBusiness());

        orderChild.setMerId(goodsMsg.getMerId());
        orderChild.setProductName(goodsMsg.getTitle());
        orderChild.setProductImg(goodsMsg.getListImg());
        orderChild.setProductSpecName(goodsSpecs.getName());
        orderChild.setProductPrice(goodsSpecs.getSalePrice());

        if (object == null || object.getLong("deliveryAddressId") == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请选择收货地址！");
        }

        // 查询收货地址
        GoodsUserAddress address = goodsUserAddressService.queryDetail(object.getLong("deliveryAddressId"));
        if (address == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的收货地址");
        }
        JSONObject jsonObject;
        if (orderChild.getBusiness() == null) {
            jsonObject = new JSONObject();
        } else {
            jsonObject = new JSONObject((Map<String, Object>) orderChild.getBusiness());
        }

        // 查询商品虚拟信息
        GoodsJump goodsJump = goodsJumpService.queryDetailBySpecsId(goodsSpecs.getId());

        jsonObject.put("address", address);
        jsonObject.put("goodsJump", goodsJump);

        // 创建实物订单
        BsOrderGoods orderGoods = new BsOrderGoods();
        orderGoods.setOrderNo(orderChild.getOrderNo());
        orderGoods.setChildOrderNo(orderChild.getChildOrderNo());
        orderGoods.setMerId(orderChild.getMerId());
        orderGoods.setPlatformCode(object.getString("platformCode"));
        orderGoods.setMerName(goodsMsg.getMerName());
        orderGoods.setUserId(order.getUserId());
        orderGoods.setUserPhone(order.getUserPhone());
        orderGoods.setGoodsId(goodsMsg.getId());
        orderGoods.setImg(goodsSpecs.getShowImg());
        orderGoods.setGoodsName(goodsMsg.getTitle());
        orderGoods.setGoodsTypeId(goodsMsg.getGoodsType());
        orderGoods.setGoodsTypeName(goodsMsg.getGoodsTypeName());
        orderGoods.setGoodsBrandId(goodsMsg.getGoodsBrand());
        orderGoods.setGoodsBrandName(goodsMsg.getGoodsBrandName());
        orderGoods.setGoodsSpecsId(goodsSpecs.getId());
        orderGoods.setGoodsSpecsName(goodsSpecs.getName());
        orderGoods.setGoodsSpecsOriginalPrice(goodsSpecs.getOriginalPrice());
        orderGoods.setGoodsCount(orderChild.getProductCount());
        orderGoods.setStatus(orderChild.getStatus());
        orderGoods.setDeliveryAddress(address.getAddress());
        orderGoods.setDeliveryAddressId(address.getId());
        orderGoods.setCreateTime(new Date());
        orderGoods.setUpdateTime(new Date());

        bsOrderGoodsService.create(orderGoods);

        goodsSpecsService.editStockNum(orderChild.getProductSpecId() , orderChild.getProductCount() , 2);

        jsonObject.put("orderGoodsId", orderGoods.getId());

        if (object.getLong("shopCartId") != null) {
            shoppingCartService.delete(object.getLong("shopCartId"));
        }

        orderChild.setBusiness(jsonObject);

        return orderChild;
    }

    public OrderChildModel cinema(OrderModel order, OrderChildModel orderChild) throws Exception {

        JSONObject object = new JSONObject((Map<String, Object>) orderChild.getBusiness());

        BsProductConfig productConfig = bsProductConfigService.queryDetail(OrderChildProductTypeEnum.type3.getCode());

        if (object == null ||
                object.getLong("filmId") == null  ||
                object.getLong("showId") == null  ||
                object.getInteger("drawMode") == null  ||
                object.getBoolean("acceptAdjust") == null  ||
                StringUtils.isBlank(object.getString("seats")) ||
                StringUtils.isBlank(object.getString("phone")) ||
                StringUtils.isBlank(object.getString("seatsName"))
        ) {
            throw ErrorHelp.genException(SysCode.System , ErrorCode.REQ_PARAMS_ERROR );
        }

        // 查询影片id查询影片
        Map<String, Object> params = new HashMap<>();
        params.put("filmId", object.getLong("filmId"));

        JSONObject jsonObject = CinemaService.getByFilmsId(params);
        JSONObject data = jsonObject.getJSONObject("data");

        // 查询场次
        Map<String, Object> showParams = new HashMap<>();
        showParams.put("showId", object.getLong("showId"));

        JSONObject objectShow = CinemaService.getRealTimeSeatByShowId(showParams).getJSONObject("data");
        JSONArray jsonArray = objectShow.getJSONArray("seats");

        if (jsonArray == null) {
            throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "下单失败！");
        }

        orderChild.setMerId(productConfig.getMerId());
        orderChild.setProductName(data.getString("name"));
        orderChild.setProductCount(1);
        orderChild.setProductImg(data.getString("pic"));
        orderChild.setProductSpecName(
                objectShow.getString("movieInformation") + "</br>" +
                objectShow.getString("cinemaName") + "-" +objectShow.getString("hallName") + "</br>" +
                object.getString("seatsName")
        );

        String[] seatsIdArray = object.getString("seats").split(",");

        orderChild.setProductPrice(bsOrderService.cinemaComputePrice(jsonArray , seatsIdArray , object.getInteger("drawMode")));

        // 创建电影票订单实体
        BsOrderCinema bsOrderCinema = new BsOrderCinema();
        bsOrderCinema.setOrderNo(orderChild.getOrderNo());
        bsOrderCinema.setChildOrderNo(orderChild.getChildOrderNo());
        bsOrderCinema.setMerId(productConfig.getMerId());
        bsOrderCinema.setPlatformCode(object.getString("platformCode"));
        bsOrderCinema.setUserId(order.getUserId());
        bsOrderCinema.setUserPhone(object.getString("phone"));
        bsOrderCinema.setImg(data.getString("pic"));
        bsOrderCinema.setGoodsName(data.getString("name"));
        bsOrderCinema.setGoodsSpecsName(orderChild.getProductSpecName());
        bsOrderCinema.setCinemaId(objectShow.getInteger("cinemaId"));
        bsOrderCinema.setShowId(object.getLong("showId"));
        bsOrderCinema.setFilmId(object.getInteger("filmId"));
        bsOrderCinema.setAcceptAdjust(object.getBoolean("acceptAdjust"));
        bsOrderCinema.setDrawMode(object.getInteger("drawMode"));
        bsOrderCinema.setSeatsCount(seatsIdArray.length);
        bsOrderCinema.setSeats(object.getString("seatsName"));
        bsOrderCinema.setCreateTime(new Date());
        bsOrderCinema.setUpdateTime(new Date());
        bsOrderCinema.setStatus(OrderCinemaStatusEnum.status0.getCode());

        bsOrderCinemaService.create(bsOrderCinema);

        return orderChild;
    }

    public OrderChildModel starbucks(OrderModel order, OrderChildModel orderChild) throws Exception {
        // 获取下单业务参数
        JSONObject object = new JSONObject((Map<String, Object>) orderChild.getBusiness());
        BsProductConfig productConfig = bsProductConfigService.queryDetail(OrderChildProductTypeEnum.type4.getCode());


        if (
                StringUtils.isBlank(object.getString("phone")) ||
                StringUtils.isBlank(object.getString("storeCode")) ||
                StringUtils.isBlank(object.getString("storeName")) ||
                StringUtils.isBlank(object.getString("storeAddress")) ||
                StringUtils.isBlank(object.getString("orderItems"))
        ) {
            throw ErrorHelp.genException(SysCode.System , ErrorCode.REQ_PARAMS_ERROR );
        }

        // 上传订单数据
        JSONArray orderItems = object.getJSONArray("orderItems");
        if (orderItems == null) {
            throw ErrorHelp.genException(SysCode.System , ErrorCode.REQ_PARAMS_ERROR );
        }

        // 查询星巴克产品
        Map<String , Object> map = new HashMap<>();
        map.put("pageIndex", 1);
        map.put("pageSize", 200);
        JSONObject jsonObject = StarbucksService.starbucksProducts(map);
        if (jsonObject.getJSONObject("data") == null) {
            throw ErrorHelp.genException(SysCode.System , ErrorCode.REQ_PARAMS_ERROR );
        }
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("items");
        if (jsonArray == null) {
            throw ErrorHelp.genException(SysCode.System , ErrorCode.REQ_PARAMS_ERROR );
        }

        // 组装数据
        List<JSONObject> list = new ArrayList<>();
        List<JSONObject> starbucksList;
        for (Object json : jsonArray) {
            JSONObject starbucksObject = (JSONObject) json;
            list.add(starbucksObject);
        }

        // 子订单价格
        BigDecimal childPrice = new BigDecimal(0);
        // 产品规格
        StringBuilder productSpecName = new StringBuilder();
        // 筛选数据
        for (Object orderItem : orderItems) {
            JSONObject orderItemObject = (JSONObject) orderItem;

            if (orderItemObject.getInteger("productId")  == null ||
                    orderItemObject.getInteger("num")  == null ||
                    StringUtils.isBlank(orderItemObject.getString("cupSize")) ||
                    StringUtils.isBlank(orderItemObject.getString("name")) ||
                    StringUtils.isBlank(orderItemObject.getString("temperature"))
            ) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.REQ_PARAMS_ERROR );
            }

            starbucksList = list.stream().
                    filter(o->o.getInteger("id").equals(orderItemObject.getInteger("productId"))).
                    collect(Collectors.toList());
            if (starbucksList.isEmpty()) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , orderItemObject.getString("name") + "产品缺货" );
            }

            JSONObject starbucks = starbucksList.get(0);

            BigDecimal price = StarbucksUtil.findPrice(starbucks , ((JSONObject) orderItem).getString("cupSize"));

            if (price == null) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "当前产品价格错误！" );
            }

            BsOrderStarbucks bsOrderStarbucks = new BsOrderStarbucks();
            bsOrderStarbucks.setOrderNo(orderChild.getOrderNo());
            bsOrderStarbucks.setChildOrderNo(orderChild.getChildOrderNo());
            bsOrderStarbucks.setMerId(productConfig.getMerId());
            bsOrderStarbucks.setPlatformCode(object.getString("platformCode"));
            bsOrderStarbucks.setProductId(orderItemObject.getInteger("productId"));
            bsOrderStarbucks.setUserId(order.getUserId());
            bsOrderStarbucks.setUserPhone(object.getString("phone"));
            bsOrderStarbucks.setStoreCode(object.getString("storeCode"));
            bsOrderStarbucks.setStoreName(object.getString("storeName"));
            bsOrderStarbucks.setStoreAddress(object.getString("storeAddress"));
            bsOrderStarbucks.setCreateTime(new Date());
            bsOrderStarbucks.setUpdateTime(new Date());
            bsOrderStarbucks.setPrice(StarbucksUtil.calculatePrice(productConfig.getDiscount() , price));
            bsOrderStarbucks.setName(starbucks.getString("name"));
            bsOrderStarbucks.setImg(starbucks.getString("defaultImage"));
            bsOrderStarbucks.setCream(orderItemObject.getString("cream"));
            bsOrderStarbucks.setCupSize(orderItemObject.getString("cupSize"));
            bsOrderStarbucks.setEspresso(orderItemObject.getString("espresso"));
            bsOrderStarbucks.setMilk(orderItemObject.getString("milk"));
            bsOrderStarbucks.setMilkBubble(orderItemObject.getString("milkBubble"));
            bsOrderStarbucks.setTemperature(orderItemObject.getString("temperature"));
            bsOrderStarbucks.setNum(orderItemObject.getInteger("num"));
            bsOrderStarbucks.setPlatformUniqueId(order.getUserPhone());
            bsOrderStarbucks.setStatus(OrderStarbucksStatusEnum.status0.getCode());

            orderStarbucksService.create(bsOrderStarbucks);

            childPrice = childPrice.add(bsOrderStarbucks.getPrice().multiply(orderItemObject.getBigDecimal("num")));
            productSpecName.append(starbucks.getString("name")).append("*").append(orderItemObject.getInteger("num")).append("/");
        }

        orderChild.setMerId(productConfig.getMerId());
        orderChild.setProductName("星巴克");
        orderChild.setProductCount(1);
        orderChild.setProductSpecName(productSpecName.toString());

        // 星巴克创建订单
        // 获取token
        Map<String , Object> mapToken = new HashMap<>();
        mapToken.put("platformUniqueId" , order.getOrderNo());
        mapToken.put("nickname" , order.getUserName());
        mapToken.put("mobile" , object.getString("phone"));
        StarbucksService.getTokenV2(mapToken);
        // 预下单
        Map<String , Object> mapOrder = new HashMap<>();
        mapOrder.put("storeCode", object.getString("storeCode"));
        mapOrder.put("customerMobile", object.getString("phone"));
        mapOrder.put("platformUniqueId", order.getOrderNo());
        mapOrder.put("orderItems", JSON.toJSONString(object.getJSONArray("orderItems")));
        JSONObject  starbucks =  StarbucksService.starbucksOrders(mapOrder);

        orderChild.setProductPrice(childPrice);
        orderChild.setChannelOrderNo(starbucks.getJSONObject("data").getString("orderNo"));

        return  orderChild;

    }
}
