package com.hai.service.impl;

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.config.QianZhuConfig;
import com.hai.entity.HighChildOrder;
import com.hai.entity.HighOrder;
import com.hai.entity.HighUser;
import com.hai.service.HighOrderService;
import com.hai.service.HighQzOrderService;
import com.hai.service.HighUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;

@Service("highQzOrderService")
public class HighQzOrderServiceImpl implements HighQzOrderService {

    @Resource
    private HighUserService highUserService;

    @Resource
    private HighOrderService highOrderService;

    @Override
    public HighOrder assemblyQzOrder(String orderNo,String orderType) throws Exception {
        HighOrder highOrder = null;
        JSONObject orderDetail = null;
        if (orderType.equals("KFC")) {
            orderDetail = QianZhuConfig.getKfcOrderByOrderNo(orderNo);
        }

        if (orderType.equals("CINEMA")) {
            orderDetail = QianZhuConfig.getCinemaOrderByOrderNo(orderNo);
        }

        if (orderType.equals("MOBILE")) {
            orderDetail = QianZhuConfig.getMobileOrderByOrderNo(orderNo);
        }

        if (orderDetail != null && orderDetail.getBoolean("success") == true) {
            highOrder = new HighOrder();
            JSONObject data = orderDetail.getJSONObject("data");
            HighOrder order = highOrderService.getOrderByOrderNo(orderNo);
            if (order != null) {
                return order;
            }
            HighUser highUser = highUserService.findByUnionId(data.getString("platformUniqueId"));
            if (highUser == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户");
            }
            highOrder.setHighChildOrderList(new ArrayList<>());
            highOrder.setOrderNo(orderNo);
            highOrder.setMemId(highUser.getId());
            highOrder.setMemName(data.getString("userName"));
            highOrder.setMemPhone(data.getString("userMobile"));
            highOrder.setPayModel(2);
            highOrder.setPayType(2);
            highOrder.setPayPrice(data.getBigDecimal("totalPrice"));
            highOrder.setTotalPrice(data.getBigDecimal("totalPrice"));
            highOrder.setDeductionPrice(new BigDecimal("0"));
            highOrder.setOrderStatus(1);
            highOrder.setCreateTime(data.getDate("createTime"));

            HighChildOrder highChildOrder;
            if (orderType.equals("KFC")) {
                // 商品信息
                JSONObject kfcPlaceOrder = data.getJSONObject("kfcPlaceOrder");
                JSONArray items = kfcPlaceOrder.getJSONArray("items");
                for (Object goodsObject : items) {
                    JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(goodsObject), JSONObject.class);
                    highChildOrder = new HighChildOrder();
                    highChildOrder.setMemId(highOrder.getMemId());
                    highChildOrder.setGoodsType(4);
                    highChildOrder.setStoreName(kfcPlaceOrder.getString("storeName"));
                    highChildOrder.setStoreAddress(kfcPlaceOrder.getString("storeAddress"));
                    highChildOrder.setGoodsName(object.getString("productName"));
                    highChildOrder.setGoodsImg(object.getString("imageUrl"));
                    highChildOrder.setGoodsSpecName("默认");
                    highChildOrder.setGoodsPrice(new BigDecimal(object.getString("originPrice")));
                    highChildOrder.setGoodsActualPrice(new BigDecimal(object.getString("price")));
                    highChildOrder.setSaleCount(object.getInteger("quantity"));
                    highChildOrder.setTotalPrice(new BigDecimal(highChildOrder.getSaleCount().toString()).multiply(highChildOrder.getGoodsPrice()));
                    highChildOrder.setGiveawayType(false);
                    highChildOrder.setChildOrdeStatus(1);
                    highChildOrder.setPraiseStatus(0);

                   /* // 计算原始价格
                    highChildOrder.getGoodsPrice().multiply(new BigDecimal(highChildOrder.getSaleCount().toString()));
                    highOrder.setTotalPrice();*/

                    // 1:堂食 2:外带 3：外卖
                    highChildOrder.setExt1(kfcPlaceOrder.getString("eatType"));
                    highOrder.getHighChildOrderList().add(highChildOrder);
                }
            }

            if (orderType.equals("CINEMA")) {
                highChildOrder = new HighChildOrder();
                highChildOrder.setMemId(highOrder.getMemId());
                highChildOrder.setGoodsType(5);
                //highChildOrder.setStoreId(data.getLong("hallId"));
                highChildOrder.setStoreName(data.getString("cinemaName"));
                highChildOrder.setStoreAddress(data.getString("cinemaAddr"));
                //highChildOrder.setGoodsId(data.getLong("filmId"));
                highChildOrder.setGoodsName(data.getString("filmName"));
                highChildOrder.setGoodsDesc(data.getString("seatsDesc"));
                highChildOrder.setGoodsImg(data.getString("pic"));
                highChildOrder.setGoodsSpecName("默认");
                highChildOrder.setGoodsPrice(new BigDecimal(data.getString("marketUnitPrice")));
                highChildOrder.setGoodsActualPrice(new BigDecimal(data.getString("unitPrice")));
                highChildOrder.setSaleCount(data.getInteger("seatsCount"));
                highChildOrder.setTotalPrice(new BigDecimal(highChildOrder.getSaleCount().toString()).multiply(highChildOrder.getGoodsPrice()));
                highChildOrder.setGiveawayType(false);
                highChildOrder.setChildOrdeStatus(1);
                highChildOrder.setPraiseStatus(0);

                /*// 计算原始价格
                highOrder.setTotalPrice(highChildOrder.getGoodsPrice().multiply(new BigDecimal(highChildOrder.getSaleCount().toString())));*/

                // 观看时间
                JSONObject showTimeObject = new JSONObject();
                showTimeObject.put("showTime", data.getString("showTime"));
                showTimeObject.put("showEndTime", data.getString("showEndTime"));
                highChildOrder.setExt1(showTimeObject.toJSONString());

                // 影厅名称
                highChildOrder.setExt2(data.getString("hallName"));

                // 影片语言
                highChildOrder.setExt3(data.getString("language"));

                highOrder.getHighChildOrderList().add(highChildOrder);
            }

            if (orderType.equals("MOBILE")) {
                highChildOrder = new HighChildOrder();
                highChildOrder.setMemId(highOrder.getMemId());
                highChildOrder.setGoodsType(6);
                highChildOrder.setGoodsName("手机充值"+data.getString("rechargeDenomination")+"元");
                highChildOrder.setGoodsSpecName(data.getString("rechargeMobile"));
                highChildOrder.setGoodsPrice(new BigDecimal(data.getString("rechargeDenomination")));
                highChildOrder.setGoodsActualPrice(new BigDecimal(data.getString("unitPrice")));
                highChildOrder.setSaleCount(data.getInteger("quantity"));
                highChildOrder.setTotalPrice(new BigDecimal(highChildOrder.getSaleCount().toString()).multiply(highChildOrder.getGoodsPrice()));
                highChildOrder.setGiveawayType(false);
                highChildOrder.setChildOrdeStatus(1);
                highChildOrder.setPraiseStatus(0);
               /* // 计算原始价格
                highOrder.setTotalPrice(highChildOrder.getGoodsPrice().multiply(new BigDecimal(highChildOrder.getSaleCount().toString())));*/
                highOrder.getHighChildOrderList().add(highChildOrder);
            }

            highOrderService.insertOrder(highOrder);
        }
        return highOrder;
    }
}
