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

import com.alibaba.fastjson.JSON;
import com.github.dozermapper.core.Mapper;
import com.google.common.collect.Maps;
import com.zm.order.tool.server.constants.CommonConstants;
import com.zm.order.tool.server.dto.OrderAddrDTO;
import com.zm.order.tool.server.dto.OrderCreateDTO;
import com.zm.order.tool.server.enums.CommonOperationEnum;
import com.zm.order.tool.server.enums.RoleEnum;
import com.zm.order.tool.server.request.CreateOrderRequest;
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.Date;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @packageName: com.zm.order.tool.server.convert
 * @className: BussinessOrderConverter
 * @author zhuxiaofeng
 * @date 2021/11/23 11:17
 */
@Component
public class BussinessOrderConverter {

    @Autowired
    private Mapper mapper;

    public OrderCreateDTO convert(CreateOrderRequest request) {
        if (Objects.isNull(request)) {
            return null;
        }
        OrderCreateDTO orderCreateDTO = buildBaseInfo(request);
        buildMapInfo(request, orderCreateDTO);
        return orderCreateDTO;
    }

    private OrderCreateDTO buildBaseInfo(CreateOrderRequest request) {
        OrderCreateDTO orderCreateDTO = mapper.map(request, OrderCreateDTO.class);
        orderCreateDTO.setPerformanceInfo(convert(request.getPerformanceInfoMap()));
        orderCreateDTO.setPayInfo(convert(request.getPayInfoMap()));
        orderCreateDTO.setActivityInfo(convert(request.getActivityInfoMap()));
        orderCreateDTO.setSourceInfo(convert(request.getSourceInfoMap()));
        orderCreateDTO.setOperation(CommonOperationEnum.CREATE.getOperation());
        orderCreateDTO.setRole(RoleEnum.SYSTEM.getValue());
        orderCreateDTO.setOperator(request.getUserId());
        if (Objects.isNull(request.getOrderAt())) {
            orderCreateDTO.setOrderAt(new Date());
        }
        if (Objects.isNull(request.getStatus())) {
            orderCreateDTO.setStatus(0);
        }
        return orderCreateDTO;
    }

    private void buildMapInfo(CreateOrderRequest request, OrderCreateDTO orderCreateDTO) {
        if (!CollectionUtils.isEmpty(request.getExtraMap())) {
            Map<String, Object> extraMap = request.getExtraMap();

        }
        if (request.getUserId() == 0L) {
            orderCreateDTO.setUserId(0 - request.getStoreId());
        }
        // 这里做个转换
        OrderAddrDTO orderAddrDTO = orderCreateDTO.getAddr();
        if (Objects.nonNull(orderAddrDTO) && !StringUtils.isEmpty(orderAddrDTO.getMobile())) {
            Map<String, Object> performanceInfoMap = request.getPerformanceInfoMap();
            if (CollectionUtils.isEmpty(performanceInfoMap)) {
                performanceInfoMap = Maps.newHashMap();
            }
            performanceInfoMap.put(CommonConstants.RECEIVER_MOBILE, orderAddrDTO.getMobile());
            orderCreateDTO.setPerformanceInfo(convert(performanceInfoMap));
        }
    }

    private String convert(Map<String, Object> map) {
        try {
            if (!CollectionUtils.isEmpty(map)) {
                return JSON.toJSONString(map);
            }
        } catch (Throwable throwable) {
        }
        return null;
    }
}
