package com.hai.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.AESEncodeUtil;
import com.hai.config.*;
import com.hai.dao.OutRechargeOrderMapper;
import com.hai.entity.*;
import com.hai.enum_type.*;
import com.hai.goods.service.GoodsDetailService;
import com.hai.goods.service.GoodsPresentService;
import com.hai.goods.service.GoodsSkuService;
import com.hai.goods.service.ShoppingCartService;
import com.hai.model.GasPayPriceModel;
import com.hai.model.OutRechargePriceModel;
import com.hai.order.model.CreateOrderChildModel;
import com.hai.order.model.CreateOrderModel;
import com.hai.order.service.OrderCreateHandleService;
import com.hai.order.service.OrderService;
import com.hai.order.service.OrderServiceExt;
import com.hai.order.type.OrderChildGoodsType;
import com.hai.order.type.OrderChildStatus;
import com.hai.order.type.OrderOilStatus;
import com.hai.order.utils.OrderUtil;
import com.hai.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * 创建订单-业务校验
 * @className: CreateOrderCheckService
 * @author: HuRui
 * @date: 2022/8/25
 **/
@Service("createOrderHandleService")
public class OrderCreateHandleServiceImpl implements OrderCreateHandleService {

    private static Logger log = LoggerFactory.getLogger(OrderCreateHandleServiceImpl.class);

    @Resource
    private CommonService commonService;



    @Resource
    private BsCompanyService companyService;

    @Resource
    private SecConfigService secConfigService;

    @Resource
    private HighUserService userService;

    @Resource
    private OrderServiceExt orderServiceExt;

    @Resource
    private HighCouponService couponService;

    @Resource
    private HighMerchantStoreService storeService;


    @Resource
    private HighMerchantService merchantService;

    @Resource
    private HighDiscountCouponRelService discountCouponRelService;

    @Resource
    private OutRechargeOrderMapper outRechargeOrderMapper;

    @Resource
    private HighCouponHandselService highCouponHandselService;

    @Resource
    private HighMerchantAccountMarketingService merchantAccountMarketingService;

    @Resource
    private OutRechargePriceService outRechargePriceService;

    @Resource
    private HighGasOrderService gasOrderService;

    @Resource
    private HighGasClassGroupTaskService gasClassGroupTaskService;

    @Resource
    private HighGasStaffService gasStaffService;

    @Resource
    private HighGasDiscountOilPriceService gasDiscountOilPriceService;

    @Resource
    private HighTyAgentOilStationService gasTyAgentOilStationService;

    @Resource
    private JinZhuJiaYouService gasJinZhuJiaYouService;

    @Resource
    private ThirdProductConfig thirdProductConfig;

    @Resource
    private HighUserService highUserService;

    @Resource
    private ApiMemberProductService apiMemberProductService;

    @Resource
    private HighDiscountPackageService discountPackageService;

    @Resource
    private HighDiscountPackageActualService discountPackageActualService;


    @Resource
    private GoodsDetailService goodsDetailService;

    @Resource
    private GoodsSkuService goodsSkuService;

    @Resource
    private GoodsPresentService presentService;


    @Resource
    private ShoppingCartService shoppingCartService;

    @Override
    public List<HighChildOrder> couponHandle(HighDiscountUserRel discountUserRel, CreateOrderChildModel createOrderChild) throws Exception {
            if (createOrderChild.getSaleCount() != 1) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "卡卷只能购买一张");
            }
            // 查询卡卷信息
            HighCoupon coupon = couponService.getCouponDetail(createOrderChild.getGoodsId());
            if (coupon == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, "");
            }
            // 是否需要预约
            if (coupon.getReserveStatus() == true) {
                if (createOrderChild.getStoreId() == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未选择门店");
                }
                // 查询门店信息
                HighMerchantStore merchantStore = storeService.getDetailById(createOrderChild.getStoreId());
                if (merchantStore == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到门店信息");
                }
                if (!merchantStore.getMerchantId().equals(coupon.getMerchantId())) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "卡券无法在此门店消费");
                }
            }

            // 是否达到限购数量
            if (orderServiceExt.userBuyCouponNum(createOrderChild.getMemId(), coupon.getId()) > coupon.getLimitNumber()) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, coupon.getCouponName() + ",已达到限购数量");
            }

            // 生成子订单编号
            String childOrderNo = OrderUtil.generateChildOrderNo();

            // 如果不是来源不是贵州中石化，需要校验库存
            if (!coupon.getCouponSource().equals(4) && !coupon.getCouponSource().equals(5) && !coupon.getCouponSource().equals(6)) {
                if (couponService.assignOrderStock(coupon.getId(), childOrderNo) == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "库存不足");
                }
            }

            List<HighChildOrder> childOrderList = new ArrayList<>();
            HighChildOrder childOrder;
            childOrder = new HighChildOrder();
            childOrder.setMemId(createOrderChild.getMemId());
            childOrder.setMemName(createOrderChild.getMemName());
            childOrder.setMemPhone(createOrderChild.getMemPhone());
            childOrder.setChildOrderNo(childOrderNo);
            childOrder.setGoodsType(OrderChildGoodsType.TYPE1.getNumber());
            childOrder.setGoodsId(coupon.getId());
            childOrder.setGoodsName(coupon.getCouponName());
            childOrder.setGoodsImg(coupon.getCouponImg());
            childOrder.setGoodsSpecName("默认");
            childOrder.setGoodsPrice(coupon.getSalesPrice());
            childOrder.setGoodsActualPrice(coupon.getDiscountPrice());
            childOrder.setSaleCount(createOrderChild.getSaleCount());
            childOrder.setGiveawayType(false);
            childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
            childOrder.setPraiseStatus(0);
            childOrder.setCreateTime(new Date());

            // 贵州中石化
            if (coupon.getCouponSource().equals(4)) {
                if (secConfigService.isConfig("GZ_SINOPEC_STATUS", "true") == true) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "卡券维护中，请稍后重试！");
                }
                // GZ_SINOPEC_QUOTA  单日限购数量
                SecConfig gzSinopecQuota = secConfigService.findByCodeType("GZ_SINOPEC_QUOTA");
                if (gzSinopecQuota != null
                        && couponService.userBuySourceLimitNumber(childOrder.getMemId(),4) >= Integer.parseInt(gzSinopecQuota.getCodeValue())) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂时无法下单，已达单日限购数量");
                }
                // 下单请求
                JSONObject jsonObject = HuiLianTongConfig.recharge(createOrderChild.getOrderNo(),
                        childOrder.getSaleCount(),
                        coupon.getCouponKey(),
                        createOrderChild.getMemPhone());
                if (!jsonObject.getString("respCode").equals("0000")) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "下单失败");
                }
                JSONObject response = HuiLianTongUnionCardConfig.resolveResponse(jsonObject.getString("data"));
                if (!response.getString("respCode").equals("0000")) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("msg"));
                }
                childOrder.setChannelOrderNo(response.getJSONObject("data").getString("rechargeOrderId"));
            } else if (coupon.getCouponSource().equals(5)) {
                // 重庆中石油业务状态
                SecConfig cqCnpcStatus = secConfigService.findByCodeType("CQ_CNPC_STATUS_" + coupon.getCouponKey());
                if (cqCnpcStatus != null && StringUtils.isNotBlank(cqCnpcStatus.getCodeValue())) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, cqCnpcStatus.getCodeValue());
                }
            }

            // 校验优惠券是否可用  如果使用优惠券就只能使用卡券的原价
            if (discountUserRel != null) {
                List<HighDiscountCouponRel> discountCouponRelList = discountCouponRelService.getRelByCoupon(coupon.getId());
                List<HighDiscountCouponRel> collect = discountCouponRelList.stream().filter(o -> o.getDiscountId().equals(discountUserRel.getDiscountId())).collect(Collectors.toList());
                if (collect == null || collect.size() == 0) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法使用优惠券");
                }
                childOrder.setGoodsActualPrice(coupon.getSalesPrice());
            }

            childOrder.setTotalPrice(childOrder.getGoodsPrice().multiply(new BigDecimal(childOrder.getSaleCount().toString())));
            childOrder.setTotalActualPrice(childOrder.getGoodsActualPrice().multiply(new BigDecimal(childOrder.getSaleCount().toString())));
            childOrder.setTotalDeductionPrice(childOrder.getGoodsPrice().subtract(childOrder.getTotalActualPrice()));
            childOrderList.add(childOrder);

            // 是否赠送卡卷
            if (coupon.getIsPresent() == true) {
                // 查询赠送卡券信息
                List<HighCouponHandsel> handselCouponList = highCouponHandselService.getHandselListByCoupon(childOrder.getGoodsId());
                for (HighCouponHandsel handsel : handselCouponList) {
                    // 查询卡卷信息
                    HighCoupon handselCoupon = couponService.getCouponDetail(handsel.getHandselCouponId());
                    if (handselCoupon == null) {
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, "");
                    }
                    String handselChildOrderNo = OrderUtil.generateChildOrderNo();

                    // 赠送卡卷是否有库存，没有就不赠送
                    if (couponService.assignOrderStock(handselCoupon.getId(), handselChildOrderNo) != null) {
                        // 生成交易子订单
                        childOrder= new HighChildOrder();
                        childOrder.setMemId(createOrderChild.getMemId());
                        childOrder.setMemName(createOrderChild.getMemName());
                        childOrder.setMemPhone(createOrderChild.getMemPhone());
                        childOrder.setChildOrderNo(handselChildOrderNo);
                        childOrder.setGoodsType(OrderChildGoodsType.TYPE1.getNumber());
                        childOrder.setGoodsId(handselCoupon.getId());
                        childOrder.setGoodsName(handselCoupon.getCouponName());
                        childOrder.setGoodsImg(handselCoupon.getCouponImg());
                        childOrder.setGoodsSpecName("默认");
                        childOrder.setGoodsPrice(new BigDecimal("0"));
                        childOrder.setGoodsActualPrice(new BigDecimal("0"));
                        childOrder.setSaleCount(1);
                        childOrder.setGiveawayType(true);
                        childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
                        childOrder.setPraiseStatus(0);
                        childOrder.setTotalPrice(childOrder.getGoodsPrice().multiply(new BigDecimal(childOrder.getSaleCount().toString())));
                        childOrder.setTotalActualPrice(new BigDecimal("0"));
                        childOrder.setTotalDeductionPrice(new BigDecimal("0"));
                        childOrder.setCreateTime(new Date());
                        childOrderList.add(childOrder);
                    }
                }
            }

            return childOrderList;
    }

    @Override
    public HighChildOrder integralRecharge(HighOrder order , CreateOrderChildModel createOrderChild) {
        // 查询用户信息
        HighUser user = userService.getDetailDataByUser(createOrderChild.getGoodsId());
        if (user == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户信息");
        }
        HighChildOrder childOrder = new HighChildOrder();
        childOrder.setOrderNo(order.getOrderNo());
        childOrder.setMemId(createOrderChild.getMemId());
        childOrder.setMemName(createOrderChild.getMemName());
        childOrder.setMemPhone(createOrderChild.getMemPhone());
        childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
        childOrder.setGoodsType(OrderChildGoodsType.TYPE2.getNumber());
        childOrder.setGoodsId(user.getId());
        childOrder.setGoodsName(user.getName());
        childOrder.setGoodsImg(user.getHeaderImg());
        childOrder.setGoodsSpecName("默认");
        childOrder.setGoodsPrice(createOrderChild.getGoodsPrice());
        childOrder.setGoodsActualPrice(createOrderChild.getGoodsPrice());
        childOrder.setSaleCount(1);
        childOrder.setTotalPrice(childOrder.getGoodsPrice().multiply(new BigDecimal(childOrder.getSaleCount().toString())));
        childOrder.setTotalActualPrice(childOrder.getGoodsActualPrice().multiply(new BigDecimal(childOrder.getSaleCount().toString())));
        childOrder.setTotalDeductionPrice(new BigDecimal("0"));
        childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
        childOrder.setGiveawayType(false);
        childOrder.setPraiseStatus(0);
        return childOrder;
    }

    @Override
    public HighChildOrder oilHandle(HighOrder order, HighDiscountUserRel discountUserRel, CreateOrderModel createOrderModel, CreateOrderChildModel createOrderChild) throws Exception {
        // 查询油品类型
        SecDictionary gasOilType = commonService.mappingSysCode("GAS_OIL_TYPE", createOrderChild.getGasOilNo());
        if (gasOilType == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知油品类型");
        }
        // 加油限制
        if (gasOilType.getExt1().equals("1")) {
            SecDictionary refuelLimit = commonService.mappingSysName("REFUEL_LIMIT", "汽油");
            if (refuelLimit != null && createOrderChild.getGoodsPrice().compareTo(new BigDecimal(refuelLimit.getCodeValue()))  == 1) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "汽油加油金额不能超过" + refuelLimit.getCodeValue()+"元");
            }
        } else if (gasOilType.getExt1().equals("2")) {
            SecDictionary refuelLimit = commonService.mappingSysName("REFUEL_LIMIT", "柴油");
            if (refuelLimit != null && createOrderChild.getGoodsPrice().compareTo(new BigDecimal(refuelLimit.getCodeValue()))  == 1) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "柴油加油金额不能超过" + refuelLimit.getCodeValue()+"元");
            }
        }
        // 查询油站
        HighMerchantStore store = storeService.getDetailById(createOrderChild.getGoodsId());
        if (store == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的油站");
        }
        // 查询商户
        HighMerchant merchant = merchantService.getDetailById(store.getMerchantId());
        if (merchant == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的油站");
        }
        // 查询分公司
        BsCompany company = companyService.getCompanyById(merchant.getCompanyId());
        if (company == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的分公司");
        }

        // 营销减免的金额
        BigDecimal marketingPrice = merchantAccountMarketingService.getPrice(store.getMerchantId(), MerAccountMarketingType.type3.getType(), Integer.valueOf(createOrderChild.getGasOilNo()));

        // 计算价格
        GasPayPriceModel priceModel = gasDiscountOilPriceService.oilPriceDiscountCompute(createOrderChild.getGoodsPrice(), createOrderChild.getGoodsId(), createOrderChild.getGasOilNo(), createOrderChild.getIsTyAgent());

        // 生成交易子订单
        HighChildOrder childOrder = new HighChildOrder();
        childOrder.setOrderNo(order.getOrderNo());
        childOrder.setMemId(createOrderChild.getMemId());
        childOrder.setMemName(createOrderChild.getMemName());
        childOrder.setMemPhone(createOrderChild.getMemPhone());
        childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
        childOrder.setGoodsType(OrderChildGoodsType.TYPE3.getNumber());
        childOrder.setGoodsId(store.getId());
        childOrder.setGoodsName(store.getStoreName());
        childOrder.setGoodsImg(store.getStoreLogo());
        childOrder.setGoodsSpecName(createOrderChild.getGasOilNo()+"#");
        childOrder.setGoodsPrice(priceModel.getOilingPrice());
        childOrder.setGoodsActualPrice(priceModel.getPayPrice());
        childOrder.setSaleCount(createOrderChild.getSaleCount());
        childOrder.setGiveawayType(false);
        childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
        childOrder.setTotalPrice(new BigDecimal(childOrder.getSaleCount().toString()).multiply(childOrder.getGoodsPrice()));
        childOrder.setTotalActualPrice(new BigDecimal(childOrder.getSaleCount().toString()).multiply(childOrder.getGoodsActualPrice()));
        childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
        childOrder.setPraiseStatus(0);
        childOrder.setCreateTime(new Date());

        // 生成加油订单
        HighGasOrder gasOrder = new HighGasOrder();
        gasOrder.setChannelType(store.getSourceType());
        gasOrder.setOrderNo(order.getOrderNo());
        gasOrder.setChildOrderNo(childOrder.getChildOrderNo());
        gasOrder.setCompanyId(company.getId());
        gasOrder.setCompanyName(company.getName());
        gasOrder.setMemId(childOrder.getMemId());
        gasOrder.setMemPhone(childOrder.getMemPhone());
        gasOrder.setMerId(store.getMerchantId());
        gasOrder.setMerName(merchant.getMerchantName());
        gasOrder.setStoreId(store.getId());
        gasOrder.setStoreName(store.getStoreName());
        gasOrder.setStoreAddress(store.getAddress());
        gasOrder.setGasRefuelPrice(childOrder.getGoodsPrice());
        gasOrder.setGasOilNo(createOrderChild.getGasOilNo());
        gasOrder.setGasGunNo(createOrderChild.getGasGunNo());
        gasOrder.setGasOilType(Integer.parseInt(gasOilType.getExt1()));
        gasOrder.setStatus(OrderOilStatus.STATUS1.getNumber());
        gasOrder.setGasDiscount(new BigDecimal("0"));
        gasOrder.setTotalDeductionPrice(new BigDecimal("0"));
        gasOrder.setDeductionCouponPrice(new BigDecimal("0"));
        gasOrder.setDeductionProductPrice(new BigDecimal("0"));

        // 成本价 油站价 减 营销策略价
        gasOrder.setGasPriceCost(priceModel.getPriceGun().subtract(marketingPrice));
        if (marketingPrice.compareTo(new BigDecimal("0")) <= 0) {
            gasOrder.setGasPriceCostTotal(gasOrder.getGasRefuelPrice());
        } else {
            gasOrder.setGasPriceCostTotal(gasOrder.getGasRefuelPrice().multiply(gasOrder.getGasOilLiters()).setScale(2, BigDecimal.ROUND_HALF_DOWN));
        }

        // 使用了优惠券
        if (discountUserRel != null) {
            // 支付价格
            childOrder.setGoodsActualPrice(childOrder.getGoodsPrice());

            // 优惠券类型  1：满减  2：抵扣   3：折扣
            if (discountUserRel.getHighDiscount().getDiscountType() == 1) {
                // 如果订单总额 小于 满减价格
                if (gasOrder.getGasRefuelPrice().compareTo(discountUserRel.getHighDiscount().getDiscountCondition()) > 1) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.UN_MEMBER_ERROR, "订单未达到"+discountUserRel.getHighDiscount().getDiscountCondition()+"元,无法使用此优惠券");
                }
                gasOrder.setDeductionCouponPrice(discountUserRel.getHighDiscount().getDiscountPrice());
            } else if (discountUserRel.getHighDiscount().getDiscountType() == 2) {
                gasOrder.setDeductionCouponPrice(discountUserRel.getHighDiscount().getDiscountPrice());

            } else if (discountUserRel.getHighDiscount().getDiscountType() == 3) {
                // 加油金额 * 折扣
                BigDecimal deductionPrice = gasOrder.getGasRefuelPrice()
                        .multiply(discountUserRel.getHighDiscount().getDiscountPrice())
                        .setScale(2, BigDecimal.ROUND_HALF_DOWN);
                gasOrder.setDeductionCouponPrice(gasOrder.getGasRefuelPrice().subtract(deductionPrice));
            }
            gasOrder.setPayablePrice(gasOrder.getGasRefuelPrice().subtract(gasOrder.getTotalDeductionPrice()));
            if (gasOrder.getPayablePrice().compareTo(new BigDecimal("0")) < 0) {
                gasOrder.setPayablePrice(new BigDecimal("0"));
            }

            // 油价信息
            gasOrder.setGasPriceGun(priceModel.getPriceGun());
            gasOrder.setGasPriceVip(priceModel.getPriceGun());
            gasOrder.setGasPriceOfficial(new BigDecimal("0"));
            gasOrder.setGasPricePlatform(priceModel.getPriceGun());
            gasOrder.setGasOilLiters(priceModel.getOilLiters());
            gasOrder.setGasDiscount(new BigDecimal("0"));
            gasOrder.setGasOilSubsidy(new BigDecimal("0"));
            gasOrder.setGasLitersPreferences(new BigDecimal("0"));
            gasOrder.setGasPricePreferences(priceModel.getPriceGun());
        } else {
            // 支付价格
            childOrder.setGoodsActualPrice(priceModel.getPayPrice());
            gasOrder.setDeductionProductPrice(priceModel.getTotalPreferences());
            gasOrder.setPayablePrice(priceModel.getPayPrice());

            // 油价信息
            gasOrder.setGasPriceGun(priceModel.getPriceGun());
            gasOrder.setGasPriceVip(priceModel.getPriceVip());
            gasOrder.setGasPriceOfficial(priceModel.getPriceOfficial());
            gasOrder.setGasPricePlatform(priceModel.getPricePlatform());
            gasOrder.setGasOilLiters(priceModel.getOilLiters());
            gasOrder.setGasDiscount(priceModel.getDiscount());
            gasOrder.setGasOilSubsidy(priceModel.getPreferentialMargin());
            gasOrder.setGasLitersPreferences(priceModel.getLitersPreferences());
            gasOrder.setGasPricePreferences(priceModel.getPricePreferences());
        }
        childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
        gasOrder.setTotalDeductionPrice(gasOrder.getDeductionProductPrice().add(gasOrder.getDeductionCouponPrice()));
        gasOrder.setPayGold(createOrderModel.getPayGold()!=null?createOrderModel.getPayGold():0);

        if (store.getSourceType().equals(MerchantStoreSourceType.type4.getNumber())) {
            // 查询金猪油品数据
            JinZhuJiaYouOilNo jzOilNo = JinZhuJiaYouOilNo.getDataByOilNo(Integer.valueOf(gasOrder.getGasOilNo()));
            if (jzOilNo == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂时无法提交订单，未知的油品");
            }
            // 请求金猪接口 - 创建金猪订单
            JSONObject jzOrderObject = gasJinZhuJiaYouService.createOrder(gasOrder.getOrderNo(),
                    gasOrder.getGasRefuelPrice(),
                    store.getStoreKey(),
                    jzOilNo.getOilNoCode(),
                    gasOrder.getGasGunNo(),
                    gasOrder.getMemPhone()
            );
            gasOrder.setChannelOrderNo(jzOrderObject.getJSONObject("data").getString("systemOrderNo"));
            gasOrder.setGasPriceChannelPay(jzOrderObject.getJSONObject("data").getBigDecimal("paymentAmount"));
        }

        // 代理商
        if (createOrderChild.getIsTyAgent() != null && createOrderChild.getIsTyAgent().equals(true)) {
            // 查询加油站
            HighTyAgentOilStation oilStation = gasTyAgentOilStationService.getDetailByOilStationId(createOrderChild.getGoodsId());
            if (oilStation != null) {
                gasOrder.setGasSalesmanId(oilStation.getTySalesmanId());
                gasOrder.setGasSalesmanName(oilStation.getTySalesmanName());
                gasOrder.setGasAgentId(oilStation.getTyAgentId());
                gasOrder.setGasAgentName(oilStation.getTyAgentName());
                gasOrder.setGasOrgId(oilStation.getOrganizationId());
                gasOrder.setGasOrgName(oilStation.getOrganizationName());
            }
        }

        // 加油员
        if (StringUtils.isNotBlank(createOrderChild.getGasStaffCode())) {
            String qrCodeSignKey = commonService.getDictionaryCodeName("QR_CODE_SIGN_KEY", "1");
            if (StringUtils.isBlank(qrCodeSignKey)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知秘钥");
            }
            String staffId;
            try {
                staffId = AESEncodeUtil.aesDecryptByBytes(AESEncodeUtil.base64Decode(URLDecoder.decode(createOrderChild.getGasStaffCode(),"UTF-8")), qrCodeSignKey);
            } catch (Exception e) {
                log.error("HighOrderController --> addOrder() error!", "加油员code解析失败");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "收银员code解析失败");
            }
            // 查询员工
            HighGasStaff gasStaff = gasStaffService.getStaffDetailById(Long.parseLong(staffId));
            if (gasStaff == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知加油员");
            }
            if (gasStaff.getMerchantStoreId().equals(childOrder.getGoodsId())) {
                gasOrder.setGasStaffId(gasStaff.getId());
                gasOrder.setGasStaffName(gasStaff.getName());
            }
        }

        // 油站是否开启班组
        HighGasClassGroupTask groupTask = gasClassGroupTaskService.getCurrentTaskByStoreId(gasOrder.getStoreId());
        if (groupTask != null) {
            gasOrder.setGasClassGroupId(groupTask.getGasClassGroupId());
            gasOrder.setGasClassGroupName(groupTask.getGasClassGroupName());
            gasOrder.setGasClassGroupTaskId(groupTask.getId());
        }

        gasOrderService.addGasOrder(gasOrder);
        return childOrder;
    }

    @Override
    public HighChildOrder couponPackage(HighOrder order , CreateOrderChildModel createOrderChild) {
        // 查询优惠券包
        HighDiscountPackage discountPackage = discountPackageService.findDiscountPackageById(createOrderChild.getGoodsId().intValue());
        if (discountPackage == null) {
            log.error("HighOrderController --> addOrder() error!", "");
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到优惠券包");
        }
        if (discountPackage.getStatus() != 1) {
            log.error("HighOrderController --> addOrder() error!", "");
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券包不可用");
        }
        if (discountPackageActualService.getStockByPackageId(discountPackage.getId()) == 0) {
            log.error("HighOrderController --> addOrder() error!", "");
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "库存数量不足");
        }
        if (discountPackage.getPurchaseNum() < discountPackageActualService.userPurchaseByUserId(createOrderChild.getMemId().intValue(), discountPackage.getId())) {
            log.error("HighOrderController --> addOrder() error!", "");
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "超过购买限制");
        }
        // 生成交易子订单
        HighChildOrder childOrder = new HighChildOrder();
        childOrder.setMemId(createOrderChild.getMemId());
        childOrder.setMemName(createOrderChild.getMemName());
        childOrder.setMemPhone(createOrderChild.getMemPhone());
        childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
        childOrder.setGoodsType(OrderChildGoodsType.TYPE7.getNumber());
        childOrder.setGoodsId(discountPackage.getId().longValue());
        childOrder.setGoodsName(discountPackage.getTitle());
        childOrder.setGoodsImg(discountPackage.getListImg());
        childOrder.setOrderNo(order.getOrderNo());
        childOrder.setGoodsSpecName("默认");
        childOrder.setGoodsPrice(discountPackage.getPrice());
        childOrder.setGoodsActualPrice(discountPackage.getPrice());
        childOrder.setSaleCount(createOrderChild.getSaleCount());
        childOrder.setGiveawayType(false);
        childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
        childOrder.setTotalPrice(new BigDecimal(childOrder.getSaleCount().toString()).multiply(childOrder.getGoodsPrice()));
        childOrder.setTotalActualPrice(new BigDecimal(childOrder.getSaleCount().toString()).multiply(childOrder.getGoodsActualPrice()));
        childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
        childOrder.setPraiseStatus(0);
        childOrder.setCreateTime(new Date());
        return childOrder;
    }

    @Override
    public HighChildOrder hltUnionCardRecharge(HighOrder order, CreateOrderChildModel createOrderChild) {
        if (createOrderChild.getGoodsPrice() == null) {
            log.error("HighOrderController --> hltUnionCardRecharge() error!", "请设置充值金额");
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请设置充值金额");
        }
        if (StringUtils.isBlank(createOrderChild.getGoodsSpecName()) ) {
            log.error("HighOrderController --> hltUnionCardRecharge() error!", "请设置充值卡号");
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请设置充值卡号");
        }
        // 查询用户信息
        HighUser user = highUserService.findByUserId(createOrderChild.getMemId());
        if (user == null) {
            log.error("HighOrderController --> hltUnionCardRecharge() error!", "未找到用户");
            throw ErrorHelp.genException(SysCode.System, ErrorCode.UN_MEMBER_ERROR, "");
        }
        // 生成交易子订单
        HighChildOrder childOrder = new HighChildOrder();
        childOrder.setMemId(createOrderChild.getMemId());
        childOrder.setMemName(createOrderChild.getMemName());
        childOrder.setMemPhone(createOrderChild.getMemPhone());
        childOrder.setOrderNo(order.getOrderNo());
        childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
        childOrder.setGoodsType(OrderChildGoodsType.TYPE8.getNumber());
        childOrder.setGoodsId(user.getId());
        childOrder.setGoodsName("贵州汇联通工会卡充值");
        childOrder.setGoodsImg(CommonSysConst.getSysConfig().getHsgDomainName()+"/filesystem/wxApplets/hlt_logo.png");
        childOrder.setGoodsSpecName(createOrderChild.getGoodsSpecName());
        childOrder.setGoodsPrice(createOrderChild.getGoodsPrice());
        childOrder.setGoodsActualPrice(createOrderChild.getGoodsPrice());
        childOrder.setSaleCount(createOrderChild.getSaleCount());
        childOrder.setGiveawayType(false);
        childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
        childOrder.setTotalPrice(new BigDecimal(childOrder.getSaleCount().toString()).multiply(childOrder.getGoodsPrice()));
        childOrder.setTotalActualPrice(new BigDecimal(childOrder.getSaleCount().toString()).multiply(childOrder.getGoodsActualPrice()));
        childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
        childOrder.setPraiseStatus(0);
        childOrder.setCreateTime(new Date());
        return childOrder;
    }

    @Override
    public List<HighChildOrder> thirdPartyOrder(HighOrder order , JSONObject object) throws Exception {

        JSONObject jsonObject;

        List<HighChildOrder> childOrderList = new ArrayList<>();

        // 千猪返回订单号
        String orderNo;

        // 查询详单订单的实际
        JSONObject productDetail = thirdProductConfig.getThirdPartyByDetail(object.getInteger("platformId"), order.getProductType(), object.getLong("companyId") , object.getLong("productId") , null , "");

        // 获取token 千猪请求接口token
        JSONObject userToken = QianZhuConfig.getTokenV2("19940218", order.getMemName(), order.getMemPhone());

        if (!userToken.getBoolean("success")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, userToken.getString("message"));
        }

        // 1 星巴克 2 肯德基 3 会员充值
        if (order.getProductType() == 1) {

            List<String> List = new ArrayList<>();

            JSONArray array = object.getJSONArray("orderItems");

            for (Object o : array) {
                JSONObject object1 = (JSONObject) o;
                Map<String, Object> contentMap = new LinkedHashMap<>();
                if (!object1.getString("productId").equals("")) {
                    contentMap.put("productId", object1.getString("productId"));
                }
                if (!object1.getString("cupSize").equals("")) {
                    contentMap.put("cupSize", object1.getString("cupSize"));
                }
                if (!object1.getString("temperature").equals("")) {
                    contentMap.put("temperature", object1.getString("temperature"));
                }
                if (!object1.getString("cream").equals("")) {
                    contentMap.put("cream", object1.getString("cream"));
                }
                if (!object1.getString("espresso").equals("")) {
                    contentMap.put("espresso", object1.getString("espresso"));
                }
                if (!object1.getString("milk").equals("")) {
                    contentMap.put("milk", object1.getString("milk"));
                }
                if (!object1.getString("milkBubble").equals("")) {
                    contentMap.put("milkBubble", object1.getString("milkBubble"));
                }
                if (!"".equals(object1.getString("num"))) {
                    contentMap.put("num", object1.getString("num"));
                }
                List.add(JSON.toJSONString(contentMap));
            }

            // 下单
            jsonObject = QianZhuConfig.starbucksOrders("19940218", object.getString("storeCode"), List.toString().replace(" ", ""), object.getString("customerMobile"));

            // 判断下单是否成功
            if (!Objects.equals(jsonObject.getString("code"), "200")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, jsonObject.getString("message"));
            }

            if (!jsonObject.getBoolean("success")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, jsonObject.getString("message"));
            }

            // 定义千猪返回订单号
            orderNo = jsonObject.getJSONObject("data").getString("orderNo");

            // 根据订单号查询订单详情
            JSONObject orderObject = QianZhuConfig.starbucksOrderByOrderNo(orderNo);

            // 判断是否成功
            if (!orderObject.getBoolean("success")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, jsonObject.getString("message"));
            }


            // 获取星巴克订单内容
            JSONArray starbucksOrder = orderObject.getJSONObject("data").getJSONArray("orderItems");

            for (Object starbucksObject : starbucksOrder) {
                JSONObject childObject = (JSONObject) starbucksObject;
                HighChildOrder childOrder = new HighChildOrder();
                childOrder.setChildOrderStatus(1);
                childOrder.setGoodsType(9);
                childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
                childOrder.setStoreId(object.getLong("storeCode"));
                childOrder.setChannelOrderNo(orderNo);
                childOrder.setGoodsId(object.getLong("productId"));
                childOrder.setMemId(order.getMemId());
                childOrder.setOrderNo(order.getOrderNo());
                childOrder.setMemName(order.getMemName());
                childOrder.setMemPhone(order.getMemPhone());
                childOrder.setStoreAddress(orderObject.getJSONObject("data").getString("storeAddress"));
                childOrder.setStoreName(orderObject.getJSONObject("data").getString("storeName"));
                childOrder.setGoodsName(childObject.getString("productName"));
                childOrder.setGoodsImg(childObject.getString("imgUrl"));
                childOrder.setSaleCount(childObject.getInteger("quantity"));
                childOrder.setGoodsPrice(childObject.getBigDecimal("marketPrice"));
                childOrder.setGiveawayType(false);
                childOrder.setCreateTime(new Date());

                // 计算利润
                BigDecimal profitPrice = childObject.getBigDecimal("marketPrice").multiply(productDetail.getBigDecimal("priceDiscount").divide(new BigDecimal(100)));
                // 计算子订单支付金额
                BigDecimal childPrice = childObject.getBigDecimal("unitPrice").add(profitPrice).setScale(2 , RoundingMode.HALF_UP);
                if (childPrice.compareTo(childObject.getBigDecimal("marketPrice")) > 0) {
                    childPrice = childObject.getBigDecimal("marketPrice");
                }

                // 配置实际支付金额
                childOrder.setGoodsActualPrice(childPrice);

                // 计算市场价金额
                childOrder.setTotalPrice(childObject.getBigDecimal("marketPrice").multiply(childObject.getBigDecimal("quantity")));
                childOrder.setTotalActualPrice(childPrice.multiply(childObject.getBigDecimal("quantity")));
                childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
                childOrderList.add(childOrder);
            }


        } else if (order.getProductType() == 2) {
            if (object.getInteger("eatType") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请选择就餐方式！");
            }
            // 下单
            jsonObject = QianZhuConfig.createKfcOrder(object.getInteger("eatType"), object.getString("storeCode"), object.getString("customerMobile"), object.getString("orderItems"), "19940218");

            // 判断下单是否成功
            if (!Objects.equals(jsonObject.getString("code"), "10000")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, jsonObject.getString("message"));
            }
            // 定义千猪返回订单号
            orderNo = jsonObject.getJSONObject("data").getString("orderNo");

            // 根据订单号查询订单详情
            JSONObject orderObject = QianZhuConfig.getKfcOrderByOrderNo(orderNo);
            // 判断下单是否成功
            if (!orderObject.getBoolean("success")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, jsonObject.getString("message"));
            }

            // 获取肯德基订单内容
            JSONArray kfcOrder = orderObject.getJSONObject("data").getJSONObject("kfcPlaceOrder").getJSONArray("items");
            for (Object kfcObject : kfcOrder) {
                JSONObject childObject = (JSONObject) kfcObject;
                HighChildOrder childOrder = new HighChildOrder();
                childOrder.setChildOrderStatus(1);
                childOrder.setGoodsType(4);
                childOrder.setMemId(order.getMemId());
                childOrder.setMemName(order.getMemName());
                childOrder.setMemPhone(order.getMemPhone());
                childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
                childOrder.setOrderNo(order.getOrderNo());
                childOrder.setGoodsId(object.getLong("productId"));
                childOrder.setChannelOrderNo(orderNo);
                childOrder.setStoreAddress(orderObject.getJSONObject("data").getJSONObject("kfcPlaceOrder").getString("storeAddress"));
                childOrder.setStoreName(orderObject.getJSONObject("data").getJSONObject("kfcPlaceOrder").getString("storeName"));
                childOrder.setGoodsName(childObject.getString("productName"));
                childOrder.setGoodsImg(childObject.getString("imageUrl"));
                childOrder.setSaleCount(childObject.getInteger("quantity"));
                childOrder.setGoodsPrice(childObject.getBigDecimal("originPrice"));
                childOrder.setGiveawayType(false);
                childOrder.setCreateTime(new Date());
                // 计算利润
                BigDecimal profitPrice = childObject.getBigDecimal("originPrice").multiply(productDetail.getBigDecimal("priceDiscount").divide(new BigDecimal(100)));
                // 计算子订单支付金额
                BigDecimal childPrice = childObject.getBigDecimal("price").add(profitPrice).setScale(2 , RoundingMode.HALF_UP);
                if (childPrice.compareTo(childObject.getBigDecimal("originPrice")) > 0) {
                    childPrice = childObject.getBigDecimal("originPrice");
                }

                // 配置实际支付金额
                childOrder.setGoodsActualPrice(childPrice);

                // 计算市场价金额
                childOrder.setTotalPrice(childObject.getBigDecimal("originPrice").multiply(childObject.getBigDecimal("quantity")));
                childOrder.setTotalActualPrice(childPrice.multiply(childObject.getBigDecimal("quantity")));
                childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
                childOrderList.add(childOrder);

            }
        } else if (order.getProductType() == 3) {
            ApiMemberProduct memberProduct = apiMemberProductService.findById(object.getLong("goodsId"));

            if (memberProduct == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前产品不可购买");
            }
            object.put("productId", memberProduct.getProductId());

            // 计算实际支付金额
            BigDecimal childPrice = memberProduct.getCostPrice().add(memberProduct.getPrice().multiply(productDetail.getBigDecimal("priceDiscount").divide(new BigDecimal(100)))).setScale(2, RoundingMode.HALF_UP);
            if (childPrice.compareTo(memberProduct.getPrice()) > 0) {
                childPrice = memberProduct.getPrice();
            }

            HighChildOrder childOrder = new HighChildOrder();
            childOrder.setChildOrderStatus(1);
            childOrder.setGoodsType(10);
            childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
            childOrder.setOrderNo(order.getOrderNo());
            childOrder.setStoreId(object.getLong("storeCode"));
            childOrder.setGoodsId(memberProduct.getId());
            childOrder.setGoodsName(memberProduct.getName());
            childOrder.setGoodsSpecName(memberProduct.getName());
            childOrder.setGiveawayType(false);
            childOrder.setSaleCount(1);
            childOrder.setMemId(order.getMemId());
            childOrder.setMemName(order.getMemName());
            childOrder.setMemPhone(order.getMemPhone());
            childOrder.setGoodsPrice(memberProduct.getPrice());
            childOrder.setTotalPrice(memberProduct.getPrice());
            childOrder.setTotalActualPrice(childPrice);
            childOrder.setCreateTime(new Date());
            childOrder.setTotalDeductionPrice(memberProduct.getPrice().subtract(childPrice));

            // 配置实际支付金额
            childOrder.setGoodsActualPrice(childPrice);

            childOrderList.add(childOrder);

        }

        return childOrderList;
    }

    @Override
    public HighChildOrder mobileRecharge(HighOrder order , JSONObject object) throws Exception {
        // 初始化话费订单对象
        OutRechargeOrder outRechargeOrder = new OutRechargeOrder();
        // 查询用户信息
        // 产品id
        Long goodsId = object.getLong("goodsId");
        // 充值内容
        String rechargeContent = object.getString("rechargeContent");

        // 查询产品详情
        OutRechargePriceModel outRechargePrice = outRechargePriceService.findById(goodsId, null);


        // 判断充值系统是否关闭
        if (!secConfigService.isConfig("RECHARGE", "1")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.RECHARGE_CLOSE, "");
        }

        // 生成交易子订单
        HighChildOrder childOrder = new HighChildOrder();
        childOrder.setOrderNo(order.getOrderNo());
        childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
        childOrder.setGoodsType(OrderChildGoodsType.TYPE6.getNumber());
        childOrder.setGoodsId(goodsId);
        childOrder.setGoodsName(rechargeContent + OperatorEnum.getNameByType(outRechargePrice.getOperatorType()) + "话费充值" + outRechargePrice.getRechargePrice() + "元");
        childOrder.setGoodsSpecName(outRechargePrice.getRechargePrice() + "元");
        childOrder.setGoodsPrice(outRechargePrice.getRechargePrice());
        childOrder.setGoodsActualPrice(outRechargePrice.getPayPrice());
        childOrder.setSaleCount(1);
        childOrder.setGiveawayType(false);
        childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
        childOrder.setTotalPrice(outRechargePrice.getRechargePrice());
        childOrder.setTotalActualPrice(outRechargePrice.getPayPrice());
        childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
        childOrder.setPraiseStatus(0);
        childOrder.setCreateTime(new Date());

        // 生成充值订单
        outRechargeOrder.setUserId(order.getMemId());
        outRechargeOrder.setRechargeContent(rechargeContent);
        outRechargeOrder.setCompanyId(outRechargePrice.getCompanyId());
        outRechargeOrder.setUserName(order.getMemName());
        outRechargeOrder.setRechargeType(outRechargePrice.getRechargeType());
        outRechargeOrder.setUserPhone(order.getMemPhone());
        outRechargeOrder.setOrderNo(order.getOrderNo());
        outRechargeOrder.setChildOrderNo(childOrder.getChildOrderNo());
        outRechargeOrder.setCreateTimed(new Date());
        outRechargeOrder.setGoodsId(goodsId);
        outRechargeOrder.setRechargePrice(outRechargePrice.getRechargePrice());
        outRechargeOrder.setOrderPrice(outRechargePrice.getRechargePrice());
        outRechargeOrder.setType(1);
        outRechargeOrder.setOperatorName(OperatorEnum.getNameByType(outRechargePrice.getOperatorType()));
        outRechargeOrder.setOperatorType(outRechargePrice.getOperatorType());
        outRechargeOrderMapper.insert(outRechargeOrder);
        return childOrder;
    }

    @Override
    public HighChildOrder goods(HighOrder order, CreateOrderChildModel createOrderChild) throws Exception{


        GoodsDetail goods = goodsDetailService.findGoodsDetailById(createOrderChild.getGoodsId());
        GoodsSku goodsSku = goodsSkuService.findGoodsSkuById(createOrderChild.getSkuId());

        Map<String , Object> map = new HashMap<>();
        map.put("goodsId" , createOrderChild.getGoodsId());
        map.put("id" , createOrderChild.getSkuId());

        List<GoodsSku> goodsSkuList = goodsSkuService.getGoodsSkuList(map);

        if (goodsSkuList.size() == 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "商品信息错误！ ");
        }

        // 生成交易子订单
        HighChildOrder childOrder = new HighChildOrder();
        childOrder.setOrderNo(order.getOrderNo());
        childOrder.setMemId(order.getMemId());
        childOrder.setMemName(order.getMemName());
        childOrder.setMemPhone(order.getMemPhone());
        childOrder.setChildOrderNo(OrderUtil.generateChildOrderNo());
        childOrder.setGoodsType(OrderChildGoodsType.TYPE11.getNumber());
        childOrder.setGoodsId(createOrderChild.getGoodsId());
        childOrder.setGoodsName(goods.getName());
        childOrder.setSecUserId(goods.getOpId());
        childOrder.setSkuId(createOrderChild.getSkuId());
        childOrder.setGoodsSpecName(goodsSku.getName());
        childOrder.setGoodsPrice(goodsSku.getOriginalPrice());
        childOrder.setGoodsActualPrice(goodsSku.getPrice());
        childOrder.setGoodsImg(goodsSku.getShowImg());
        childOrder.setSaleCount(createOrderChild.getSaleCount());
        childOrder.setGiveawayType(false);
        childOrder.setChildOrderStatus(OrderChildStatus.STATUS1.getNumber());
        childOrder.setTotalPrice(childOrder.getGoodsPrice().multiply(new BigDecimal(childOrder.getSaleCount())));
        childOrder.setTotalActualPrice(childOrder.getGoodsActualPrice().multiply(new BigDecimal(childOrder.getSaleCount())));
        childOrder.setTotalDeductionPrice(childOrder.getTotalPrice().subtract(childOrder.getTotalActualPrice()));
        childOrder.setPraiseStatus(0);
        childOrder.setCreateTime(new Date());
        childOrder.setExt5(goods.getOpId().toString());

        if (createOrderChild.getShopCartId() != null) {
            shoppingCartService.deleteShoppingCart(createOrderChild.getShopCartId());
        }

        goodsSkuService.decreaseStockNum(createOrderChild.getSkuId(),  createOrderChild.getSaleCount());

        presentService.preGiveUser(childOrder);

        return childOrder;
    }
}
