package com.zm.order.tool.server.helper;

import com.alibaba.fastjson.JSON;
import com.github.dozermapper.core.Mapper;
import com.zm.order.tool.server.component.ShardingKeyGenerator;
import com.zm.order.tool.server.cy.CyOrderRelationsDao;
import com.zm.order.tool.server.dao.OrderDao;
import com.zm.order.tool.server.dao.OrderIndexDao;
import com.zm.order.tool.server.dao.OrderItemDao;
import com.zm.order.tool.server.manager.RedisManager;
import com.zm.order.tool.server.manager.SearchManager;
import com.zm.order.tool.server.model.*;
import com.zm.order.tool.server.query.OrderItemQuery;
import com.zm.order.tool.server.query.OrderQuery;
import com.zm.order.tool.server.request.OrderDataReviseRequest;
import com.zm.order.tool.server.tempdb.TempOrderDao;
import com.zm.order.tool.server.tempdb.TempOrderItemDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author NianChang
 * @ClassName OrderScriptHelper
 * @date 2021/02/21 2:54 下午
 */
@Component
public class OrderScriptHelper {

    @Autowired
    private Mapper mapper;

    @Autowired
    private TempOrderDao tempOrderDao;

    @Autowired
    private TempOrderItemDao tempOrderItemDao;

    @Autowired
    private RedisManager redisManager;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderIndexDao orderIndexDao;

    @Autowired
    private SearchManager searchManager;

    @Autowired
    private CyOrderRelationsDao cyRelationsDao;

    @Autowired
    private OrderItemDao orderItemDao;

    @Autowired
    private ShardingKeyGenerator shardingKeyGenerator;

    /**
     * 数据迁移
     * @return:
     * @Date: 2020/12/10 2:29 PM
     */
    public OrderQuery buildQueryByTransfer(OrderDataReviseRequest request) {
        OrderQuery orderQuery = new OrderQuery();
        orderQuery.setSize(200);
        orderQuery.setOrderNo(request.getOrderNo());
        return orderQuery;
    }

    public List<OrderItem> queryItemList(List<TempOrder> tempOrderList) {
        List<String> orderNoList = tempOrderList.stream().map(TempOrder::getOrderNo).collect(Collectors.toList());
        OrderItemQuery itemQuery = new OrderItemQuery();
        itemQuery.setOrderNoList(orderNoList);
        List<TempOrderItem> tempOrderItems = tempOrderItemDao.selectList(itemQuery);
        List<OrderItem> itemList = new ArrayList<>();
        tempOrderItems.forEach(item -> {
            itemList.add(mapper.map(item, OrderItem.class));
        });
        return itemList;
    }

    public CyOrderRelations getOrderRelations(TempOrder tempOrder) {
        CyOrderRelations relations = new CyOrderRelations();
        relations.setOrderNo(tempOrder.getOrderNo());
        // 订单下单时间小于2020-02-18 00:04:16(1581955456000) 需要在id上 + 424237643681759233
        if (tempOrder.getOrderAt().getTime() < 1581955456000L) {
            relations.setOrderId(tempOrder.getId() + 424237643681759233L);
        } else {
            relations.setOrderId(tempOrder.getId());
        }
        return relations;
    }

    public OrderIndex getOrderIndex(TempOrder tempOrder) {
        OrderIndex orderIndex = new OrderIndex();
        orderIndex.setOrderNo(tempOrder.getOrderNo());
        orderIndex.setUserId(tempOrder.getUserId());
        orderIndex.setOrderId(tempOrder.getId());
        orderIndex.setBizType(tempOrder.getBizType());
        return orderIndex;
    }

    public Order getOrder(long beforeTime, TempOrder tempOrder, Map<String, List<OrderItem>> itemMap) {
        Order order = mapper.map(tempOrder, Order.class);
        if (tempOrder.getOrderAt().getTime() < beforeTime) {
            order.setItemInfo("TL");
        } else {
            List<OrderItem> itemList = itemMap.get(tempOrder.getOrderNo());
            if (!CollectionUtils.isEmpty(itemList)) {
                List<InnerOrderItem> items = new ArrayList<>(itemList.size());
                itemList.forEach(item -> {
                    items.add(mapper.map(item, InnerOrderItem.class));
                });
                order.setItemInfo(JSON.toJSONString(items));
            } else {
                order.setItemInfo("TL");
            }
        }
        return order;
    }

    public OrderPayDetails buildOrderPayDetails(Order order) {
        OrderPayDetails orderPayDetails = new OrderPayDetails();
        orderPayDetails.setId(shardingKeyGenerator.generateSnowflakeNumKey());
        orderPayDetails.setOrderNo(order.getOrderNo());
        orderPayDetails.setUserId(order.getUserId());
        orderPayDetails.setSellerId(order.getSellerId());
        orderPayDetails.setStoreId(order.getStoreId());
        orderPayDetails.setPayType(order.getPayType());
        orderPayDetails.setCreatedAt(order.getPayAt());
        orderPayDetails.setUpdatedAt(order.getPayAt());
        orderPayDetails.setPayNo(!StringUtils.isEmpty(order.getPayNo()) ? order.getPayNo() : "");
        orderPayDetails.setThirdPayNo(!StringUtils.isEmpty(order.getThirdPayNo()) ? order.getThirdPayNo() : "");
        orderPayDetails.setOrderAt(order.getOrderAt());
        orderPayDetails.setPayAt(Objects.nonNull(order.getPayAt()) ? order.getPayAt() : order.getOrderAt());
        orderPayDetails.setSource(order.getSource());
        orderPayDetails.setOrderType(order.getOrderType());
        orderPayDetails.setAmount(order.getActualAmount());

        String payInfo = order.getPayInfo();
        if (!StringUtils.isEmpty(payInfo)) {
            Map<String, Object> jsonMap = JSON.parseObject(payInfo);
            Object walletPayDetail = jsonMap.get("walletPayDetail");
            if (!StringUtils.isEmpty(walletPayDetail)) {
                Map<String, Object> extraMap = new HashMap<>();
                extraMap.put("extraMap", walletPayDetail);
                orderPayDetails.setFeatures(JSON.toJSONString(extraMap));
            }
        }
        return orderPayDetails;
    }
}
