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

import com.github.dozermapper.core.Mapper;
import com.google.common.collect.Lists;
import com.zm.order.tool.server.component.ShardingKeyGenerator;
import com.zm.order.tool.server.dto.*;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.model.OrderIndex;
import com.zm.order.tool.server.model.OrderOperation;
import com.zm.order.tool.server.query.OrderQuery;
import com.zm.order.tool.server.request.CreateToolInterfaceRequest;
import com.zm.order.tool.server.response.*;
import com.zm.order.tool.server.service.AdminOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 管理员模块辅助 -> 类型转换
 *
 * @Author 陈标
 * @Date 2019/12/18 17:19
 */
@Component
public class AdminOrderConverter {

    private static final Logger logger = LoggerFactory.getLogger(AdminOrderConverter.class);

    @Autowired
    private Mapper mapper;
    @Autowired
    private ShardingKeyGenerator keyGenerator;


    public List<AdminOrderItemVO> convert2OrderItem(List<OrderItemDTO> orderItemDtoList) {
        if (CollectionUtils.isEmpty(orderItemDtoList)) {
            return Collections.EMPTY_LIST;
        }
        List<AdminOrderItemVO> voList = Lists.newArrayListWithCapacity(orderItemDtoList.size());
        orderItemDtoList.forEach(orderDiscountDTO -> {
            AdminOrderItemVO vo = mapper.map(orderDiscountDTO, AdminOrderItemVO.class);
            voList.add(vo);
        });
        return voList;
    }

    public List<RefundDetailVO> convert2DetailList(List<RefundDetailDTO> refundDetailDtoList) {
        if (CollectionUtils.isEmpty(refundDetailDtoList)) {
            return Collections.EMPTY_LIST;
        }
        List<RefundDetailVO> refundDetailVoList = Lists.newArrayListWithCapacity(refundDetailDtoList.size());
        refundDetailDtoList.forEach(refundDetailDTO -> {
            refundDetailVoList.add(convert(refundDetailDTO));
        });
        return refundDetailVoList;
    }

    public List<OrderOperateDTO> convertOrderOperationDTO(List<OrderOperation> orderOperations) {
        if (CollectionUtils.isEmpty(orderOperations)) {
            return Collections.EMPTY_LIST;
        }
        List<OrderOperateDTO> voList = Lists.newArrayListWithCapacity(orderOperations.size());
        orderOperations.forEach(orderOperation -> {
            OrderOperateDTO orderOperateDTO = mapper.map(orderOperation, OrderOperateDTO.class);
            voList.add(orderOperateDTO);
        });
        return voList;
    }

    public List<OrderOperateVO> convertOrderOperationVO(List<OrderOperateDTO> orderOperations) {
        if (CollectionUtils.isEmpty(orderOperations)) {
            return Collections.EMPTY_LIST;
        }
        List<OrderOperateVO> voList = Lists.newArrayListWithCapacity(orderOperations.size());
        orderOperations.forEach(orderOperateDTO -> {
            OrderOperateVO orderOperateVO = mapper.map(orderOperateDTO, OrderOperateVO.class);
            voList.add(orderOperateVO);
        });
        return voList;
    }

    public AdminOrderDetailVO convert(OrderDetailDTO orderDetailDTO) {
        return mapper.map(orderDetailDTO, AdminOrderDetailVO.class);
    }

    public List<LineUpRecordVO> convertToLineUpVO(List<LineUpRecordDTO> lineUpRecords) {
        List<LineUpRecordVO> list=new ArrayList<>();
        for (LineUpRecordDTO lineUpRecordDTO : lineUpRecords) {
            LineUpRecordVO lineUpRecordVO = mapper.map(lineUpRecordDTO, LineUpRecordVO.class);
            list.add(lineUpRecordVO);
        }
        return list;
    }

    public ToolInterfaceDTO convert(CreateToolInterfaceRequest request) {
        return mapper.map(request, ToolInterfaceDTO.class);
    }



    public List<ToolInterfaceVO> convertToToolInterfaceVO(List<ToolInterfaceDTO> toolInterfaceDtoList) {
        List<ToolInterfaceVO> list = new ArrayList<>();
        for (ToolInterfaceDTO toolInterfaceDTO : toolInterfaceDtoList) {
            ToolInterfaceVO map = mapper.map(toolInterfaceDTO, ToolInterfaceVO.class);
            list.add(map);
        }
        return list;
    }


    public List<OrderSimpleVo> convertOrderSimpleVo(List<OrderSimpleDTO> list) {
        List<OrderSimpleVo> simpleVoList = new ArrayList<>();
        for (OrderSimpleDTO orderSimpleDTO : list) {
            OrderSimpleVo simpleVo = mapper.map(orderSimpleDTO, OrderSimpleVo.class);
            simpleVoList.add(simpleVo);
        }
        return simpleVoList;
    }

    public List<OrderVO> convert(List<OrderDTO> orderDtoList) {
        if (CollectionUtils.isEmpty(orderDtoList)) {
            return Collections.EMPTY_LIST;
        }
        List<OrderVO> voList = Lists.newArrayListWithCapacity(orderDtoList.size());
        orderDtoList.forEach(orderInfoDTO -> {
            OrderVO orderVO = mapper.map(orderInfoDTO, OrderVO.class);
            voList.add(orderVO);
        });
        return voList;
    }
    public RefundDetailVO convert(RefundDetailDTO refundDetailDTO) {
        return mapper.map(refundDetailDTO, RefundDetailVO.class);
    }

    public List<OrderStatusFlowsVO> convertToOrderStatusFlowsVO(List<OrderStatusFlowsDTO> data) {
        if (CollectionUtils.isEmpty(data)) {
            return Collections.EMPTY_LIST;
        }
        List<OrderStatusFlowsVO> voList = Lists.newArrayListWithCapacity(data.size());
        data.forEach(orderStatusFlowsDTO -> {
            OrderStatusFlowsVO orderStatusFlowsVO = mapper.map(orderStatusFlowsDTO, OrderStatusFlowsVO.class);
            voList.add(orderStatusFlowsVO);
        });
        return voList;
    }

    public List<OrderSimpleVo> convertToOrderSimpleVO(List<OrderSimpleDTO> data) {
        if (CollectionUtils.isEmpty(data)) {
            return Collections.EMPTY_LIST;
        }
        List<OrderSimpleVo> voList = Lists.newArrayListWithCapacity(data.size());
        data.forEach(orderSimpleDto -> {
            OrderSimpleVo orderSimpleVo = mapper.map(orderSimpleDto, OrderSimpleVo.class);
            voList.add(orderSimpleVo);
        });
        return voList;

    }

    public OrderQuery converter(DataTransferDTO dataTransfer) {
        OrderQuery orderQuery = new OrderQuery();
        if (dataTransfer != null) {
            orderQuery.setUserId(dataTransfer.getUserId());
            orderQuery.setOrderNo(dataTransfer.getOrderNo());
            orderQuery.setBizType(dataTransfer.getBizType());
        }
        orderQuery.setPageNo(1);
        orderQuery.setPageSize(1000);
        orderQuery.setDbIndex(dataTransfer.getDbIndex());
        orderQuery.setTbIndex(dataTransfer.getTbIndex());
        return orderQuery;
    }
    
    public OrderIndex convertIndexModel(Order order) {
        OrderIndex orderIndex = new OrderIndex();
        orderIndex.setUserId(order.getUserId());
        orderIndex.setBizType(order.getBizType());
        orderIndex.setOrderNo(order.getOrderNo());
        orderIndex.setOrderId(order.getId());
        orderIndex.setId(keyGenerator.generateSnowflakeNumKey());
        return orderIndex;
    }
    
}
