
package com.tmerclub.cloud.search.feign;


import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.feign.SearchOrderFeignClient;
import com.tmerclub.cloud.api.vo.EsPageVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.order.dto.OrderSearchDTO;
import com.tmerclub.cloud.common.order.vo.EsOrderVO;
import com.tmerclub.cloud.common.order.vo.OrderItemVO;
import com.tmerclub.cloud.common.order.vo.UserOrderStatisticVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.search.manager.OrderRefundSearchManager;
import com.tmerclub.cloud.search.manager.OrderSearchManager;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * 订单搜索feign连接
 *
 * @author YXF
 * @date 2020/12/07
 */
@DubboService
public class SearchOrderFeignController implements SearchOrderFeignClient {

    @Autowired
    private OrderSearchManager orderSearchManager;
    @Autowired
    private OrderRefundSearchManager orderRefundSearchManager;


    @Override
    public ServerResponseEntity<List<UserOrderStatisticVO>> countOrderByUserIds(List<Long> userIds) {
        List<UserOrderStatisticVO> list = orderSearchManager.countOrderByUserIds(userIds);
        return ServerResponseEntity.success(list);
    }

    @Override
    public ServerResponseEntity<EsPageVO<EsOrderVO>> pageOrder(OrderSearchDTO orderSearchDTO) {
        return ServerResponseEntity.success(orderSearchManager.pageSearchResult(orderSearchDTO));
    }

    @Override
    public ServerResponseEntity<Long> countOrder(OrderSearchDTO orderSearchDTO) {
        orderSearchDTO.setPageSize(1);
        orderSearchDTO.setPageNum(1);
        EsPageVO<EsOrderVO> esOrderPage = orderSearchManager.pageSearchResult(orderSearchDTO);
        return ServerResponseEntity.success(esOrderPage.getTotal());
    }

    @Override
    public ServerResponseEntity<List<EsOrderVO>> listOrderById(List<Long> orderIds) {
        if (CollUtil.isEmpty(orderIds)) {
            return ServerResponseEntity.success(new ArrayList<>(0));
        }
        List<EsOrderVO> orderList = orderSearchManager.listByOrderIds(orderIds);
        return ServerResponseEntity.success(orderList);
    }

    @Override
    public ServerResponseEntity<List<EsOrderVO>> listOrder(OrderSearchDTO orderSearchDTO) {
        EsPageVO<EsOrderVO> orderPage = orderSearchManager.pageSearchResult(orderSearchDTO);
        return ServerResponseEntity.success(orderPage.getList());
    }

    @Override
    public ServerResponseEntity<List<OrderItemVO>> getOrderItems(List<Long> orderItemIds) {
        if (CollUtil.isEmpty(orderItemIds)) {
            return ServerResponseEntity.success();
        }
        OrderSearchDTO orderSearchDTO = new OrderSearchDTO();
        orderSearchDTO.setOrderItemIds(orderItemIds);
        orderSearchDTO.setPageSize(orderItemIds.size());
        orderSearchDTO.setPageNum(1);
        orderSearchDTO.setFetchSource(new String[]{"orderId", "orderItems", "actualTotal"});
        EsPageVO<EsOrderVO> orderPage = orderSearchManager.pageSearchResult(orderSearchDTO);
        List<OrderItemVO> orderItemList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (EsOrderVO esOrderVO : orderPage.getList()) {
            orderItemList.addAll(BeanUtil.mapAsList(esOrderVO.getOrderItems(), OrderItemVO.class));
        }
        return ServerResponseEntity.success(orderItemList);
    }

    @Override
    public ServerResponseEntity<List<EsOrderVO>> listEsOrder(OrderSearchDTO orderSearchDTO) {
        List<EsOrderVO> esOrders = orderSearchManager.listSearchResult(orderSearchDTO);
        return ServerResponseEntity.success(esOrders);
    }
}
