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

import cn.qmai.data.encrypt.service.DataEncryptService;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.model.OrderDataHandleRequest;
import com.zm.order.tool.server.utils.DateUtils;
import net.sf.jsqlparser.JSQLParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author zhuxiaofeng
 * @date 2021/9/16 11:42
 */
@Component
public class OrderDataHandleHelper {

    @Autowired
    private DataEncryptService encryptService;

    public Order buildOrderUpdateData(Order oldOrder, OrderDataHandleRequest request) throws JSQLParserException {
        Order order = new Order();
        order.setId(oldOrder.getId());
        order.setUserId(oldOrder.getUserId());
        if (Objects.nonNull(request.getStatus())) {
            order.setStatus(request.getStatus());
        }
        if (Objects.nonNull(request.getPayStatus())) {
            order.setPayStatus(request.getPayStatus());
        }
        if (Objects.nonNull(request.getBizType())) {
            order.setBizType(request.getBizType());
        }
        if (Objects.nonNull(request.getPayAt())) {
            order.setPayAt(request.getPayAt());
        }
        if (Objects.nonNull(request.getStoreOrderNo())) {
            order.setStoreOrderNo(request.getStoreOrderNo());
        }
        if (Objects.nonNull(request.getOrderType())) {
            order.setOrderType(request.getOrderType());
        }
        if (Objects.nonNull(request.getMobile())) {
            order.setMobile(request.getMobile());
            order.setMobileCipher(encryptService.encrypt(request.getMobile()));
        }
        if (Objects.nonNull(request.getOrderSubType())) {
            order.setOrderSubType(request.getOrderSubType());
        }
        if (Objects.nonNull(request.getStoreId())) {
            order.setStoreId(request.getStoreId());
        }
        if (Objects.nonNull(request.getSourceNo())) {
            order.setSourceNo(request.getSourceNo());
        }
        if (Objects.nonNull(request.getTotalAmount())) {
            order.setTotalAmount(request.getTotalAmount());
        }
        if (Objects.nonNull(request.getCostAmount())) {
            order.setCostAmount(request.getCostAmount());
        }
        if (Objects.nonNull(request.getPackAmount())) {
            order.setPackAmount(request.getPackAmount());
        }
        if (Objects.nonNull(request.getItemAmount())) {
            order.setItemAmount(request.getItemAmount());
        }
        if (Objects.nonNull(request.getIsDeleted())) {
            order.setIsDeleted(request.getIsDeleted());
        }
        if (Objects.nonNull(request.getFreightAmount())) {
            order.setFreightAmount(request.getFreightAmount());
        }
        if (Objects.nonNull(request.getDiscountAmount())) {
            order.setDiscountAmount(request.getDiscountAmount());
        }
        if (Objects.nonNull(request.getActualAmount())) {
            order.setActualAmount(request.getActualAmount());
        }
        buildOrderUpdateDataExtra(order, request, oldOrder);
        return order;
    }

    private void buildOrderUpdateDataExtra(Order order, OrderDataHandleRequest request, Order oldOrder) {
        if (Objects.nonNull(request.getChangeAmount())) {
            order.setChangeAmount(request.getChangeAmount());
        }
        if (Objects.nonNull(request.getPayType())) {
            order.setPayType(request.getPayType());
        }
        if (Objects.nonNull(request.getPayNo())) {
            order.setPayNo(request.getPayNo());
        }
        if (Objects.nonNull(request.getThirdPayNo())) {
            order.setThirdPayNo(request.getThirdPayNo());
        }
        if (Objects.nonNull(request.getPerformanceStatus())) {
            order.setPerformanceStatus(request.getPerformanceStatus());
        }
        if (Objects.nonNull(request.getPerformanceType())) {
            order.setPerformanceType(request.getPerformanceType());
        }
        if (Objects.nonNull(request.getCommentStatus())) {
            order.setCommentStatus(request.getCommentStatus());
        }
        if (Objects.nonNull(request.getCompletedAt())) {
            order.setCompletedAt(request.getCompletedAt());
        }
        if (Objects.nonNull(request.getAfterSaleStatus())) {
            order.setAfterSaleStatus(request.getAfterSaleStatus());
        }
        if (Objects.nonNull(request.getReceiveStatus())) {
            order.setReceiveStatus(request.getReceiveStatus());
        }
        if (Objects.nonNull(request.getBuyerRemarks())) {
            order.setBuyerRemarks(request.getBuyerRemarks());
        }
        if (Objects.nonNull(request.getSellerRemarks())) {
            order.setSellerRemarks(request.getSellerRemarks());
        }
        if (Objects.nonNull(request.getSellerId())) {
            order.setSellerId(request.getSellerId());
        }
        if (Objects.nonNull(request.getExtraMap())) {
            Map<String, Object> extra = oldOrder.getExtraMap();
            Map<String, Object> requestMap = JSON.parseObject(request.getExtraMap(), Map.class);
            // 扩展字段原数据没有的key 添加，没有的key 添加，不删除key
            for (Map.Entry<String, Object> entry : requestMap.entrySet()) {
                extra.put(entry.getKey(), entry.getValue());
            }
            order.setExtraMap(extra);
        }
        if (Objects.nonNull(request.getPayInfo())) {
            Map<String, Object> payInfoMap = Maps.newHashMap();
            if (!StringUtils.isEmpty(oldOrder.getPayInfo())) {
                payInfoMap = JSON.parseObject(oldOrder.getPayInfo(), Map.class);
            }
            Map<String, Object> payInfoNewMap = JSON.parseObject(request.getPayInfo(), Map.class);
            // payInfo原数据没有的key 添加,不删除key
            for (Map.Entry<String, Object> entry : payInfoNewMap.entrySet()) {
                payInfoMap.put(entry.getKey(), entry.getValue());
            }
            order.setPayInfo(JSON.toJSONString(payInfoMap));
        }
    }

    public static SplitResult splitList(List<Order> list) {
        List<Order> firstAndLastTen = new ArrayList<>();
        List<Order> remaining = new ArrayList<>();

        int size = list.size();
        // 提取前 10 个元素
        for (int i = 0; i < Math.min(10, size); i++) {
            firstAndLastTen.add(list.get(i));
        }
        // 提取后 10 个元素
        for (int i = Math.max(0, size - 10); i < size; i++) {
            firstAndLastTen.add(list.get(i));
        }

        // 找出剩下的元素
        for (int i = 10; i < Math.max(0, size - 10); i++) {
            remaining.add(list.get(i));
        }

        return new SplitResult(firstAndLastTen, remaining);
    }


    /**
     * 过滤订单下单时间在365天之前的订单
     *
     * @param list
     */
    public List<Order> filterUpdateAt365(List<Order> list) {
        for (int i = 0; i < list.size(); i++) {
            Order order = list.get(i);
            //如果更新时间在365天之前，则删除该订单
            if (DateUtils.before(new Date(), Calendar.YEAR, 1).before(order.getUpdatedAt())) {
                list.remove(i);
            }
        }
        return list;
    }
}
