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

import com.google.common.collect.Maps;
import com.zm.order.tool.server.component.ShardingKeyGenerator;
import com.zm.order.tool.server.dto.OperateOrderDTO;
import com.zm.order.tool.server.enums.RoleEnum;
import com.zm.order.tool.server.model.BaseIndexOrder;
import com.zm.order.tool.server.model.OrderOperation;
import com.zm.order.tool.server.query.OrderQuery;
import com.zm.order.tool.server.request.TemporaryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @Author 陈标
 * @Date 2020/3/20 18:20
 */
@Component
public class AdminBuilder {

    @Autowired
    private ShardingKeyGenerator shardingKeyGenerator;


    public OrderQuery buildOrderQuery(TemporaryRequest request) {
        OrderQuery query = new OrderQuery();
        if (Objects.nonNull(request.getStartId())) {
            query.setStartId(Long.valueOf(request.getStartId()));
        }
        if (Objects.nonNull(request.getEndId())) {
            query.setEndId(Long.valueOf(request.getEndId()));
        }
        query.setDbIndex(request.getDbIndex());
        query.setTbIndex(request.getTbIndex());
        return query;
    }

    public OrderOperation buildOrderOperation(OperateOrderDTO operateOrderDTO, BaseIndexOrder baseIndexOrder) {

        OrderOperation orderOperation = new OrderOperation();
        orderOperation.setId(shardingKeyGenerator.generateSnowflakeNumKey());
        orderOperation.setUserId(baseIndexOrder.getUserId());
        orderOperation.setOrderNo(baseIndexOrder.getOrderNo());
        orderOperation.setRole(RoleEnum.ORDER_CENTER_TOOL.getValue());
        orderOperation.setOperator(8L);
        orderOperation.setOperation(RoleEnum.ORDER_CENTER_TOOL.getDesc());
        orderOperation.setBeforeStatus(baseIndexOrder.getStatus());
        orderOperation.setAfterStatus(Objects.isNull(operateOrderDTO.getStatus()) ? baseIndexOrder.getStatus() : operateOrderDTO.getStatus());
        Map<String, Object> extraMap = Maps.newHashMap();
        extraMap.put("operateOrder", operateOrderDTO);
        orderOperation.setExtraMap(extraMap);
        return orderOperation;

    }

    public OrderOperation buildOrderOperation(BaseIndexOrder baseIndexOrder) {
        OrderOperation orderOperation = new OrderOperation();
        orderOperation.setId(shardingKeyGenerator.generateSnowflakeNumKey());
        orderOperation.setUserId(baseIndexOrder.getUserId());
        orderOperation.setOrderNo(baseIndexOrder.getOrderNo());
        orderOperation.setRole(RoleEnum.ORDER_CENTER_TOOL.getValue());
        orderOperation.setOperator(8L);
        orderOperation.setOperation(RoleEnum.ORDER_CENTER_TOOL.getDesc());
        orderOperation.setBeforeStatus(baseIndexOrder.getStatus());
        orderOperation.setAfterStatus(5);
        Map<String, Object> extraMap = Maps.newHashMap();
        extraMap.put("operateOrder", "修改订单完成时间");
        orderOperation.setExtraMap(extraMap);
        return orderOperation;

    }
    
}
