
package com.tmerclub.cloud.order.feign;

import com.tmerclub.cloud.api.feign.EsOrderFeignClient;
import com.tmerclub.cloud.api.order.feign.OrderCommFeignClient;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.order.vo.OrderItemLangVO;
import com.tmerclub.cloud.common.order.vo.OrderItemVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.order.model.OrderItem;
import com.tmerclub.cloud.order.service.OrderItemLangService;
import com.tmerclub.cloud.order.service.OrderItemService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author tmerclub
 * @date 2020/12/25
 */
@DubboService
public class OrderCommFeignController implements OrderCommFeignClient {


    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private OrderItemLangService orderItemLangService;
    @DubboReference
    private EsOrderFeignClient esOrderFeignClient;


    @Override
    public ServerResponseEntity<Long> getSpuIdByOrderItemId(Long orderItemId) {
        OrderItemVO orderItem = orderItemService.getByOrderItemId(orderItemId);
        if (Objects.isNull(orderItem)) {
            return ServerResponseEntity.showFailMsg("订单项不存在");
        }
        if (Objects.equals(orderItem.getIsComm(), Constant.IS_COMM)) {
            return ServerResponseEntity.showFailMsg("[" + orderItem.getSpuName() + "]" + "该订单项已评论，请勿重复评论");
        }
        return ServerResponseEntity.success(orderItem.getSpuId());
    }

    @Override
    public void updateOrderItemComm(OrderItemVO orderItemVO) {
        //更新订单项
        OrderItem orderItem = this.buildOrderItem(orderItemVO);
        orderItemService.update(orderItem);
        esOrderFeignClient.updateOrder(orderItemVO.getOrderId());
    }

    @Override
    public void updateOrderItemComms(List<OrderItemVO> orderItemVOList) {
        if (CollectionUtils.isEmpty(orderItemVOList)) {
            return;
        }
        // 更新订单商品
        Set<Long> orderIds = orderItemVOList.stream().map(x -> {
            OrderItem orderItem = this.buildOrderItem(x);
            orderItemService.update(orderItem);
            return x.getOrderId();
        }).collect(Collectors.toSet());
        // es更新订单
        if (CollectionUtils.isEmpty(orderIds)) {
            return;
        }
        orderIds.forEach(x -> esOrderFeignClient.updateOrder(x));
    }

    private OrderItem buildOrderItem(OrderItemVO orderItemVO) {
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderItemId(orderItemVO.getOrderItemId());
        orderItem.setIsComm(Constant.IS_COMM);
        orderItem.setCommTime(orderItemVO.getCommTime());
        orderItem.setUpdateTime(orderItemVO.getUpdateTime());
        return orderItem;
    }

    @Override
    public ServerResponseEntity<OrderItemVO> getOrderItemByOrderItemId(Long orderItemId) {
        return ServerResponseEntity.success(orderItemService.getSpuInfoByOrderItemId(orderItemId));
    }

    @Override
    public ServerResponseEntity<OrderItemVO> getByOrderItemId(Long orderItemId) {
        return ServerResponseEntity.success(orderItemService.getByOrderItemId(orderItemId));
    }

    @Override
    public ServerResponseEntity<List<OrderItemLangVO>> listOrderItemLangByIds(List<Long> orderItemIds) {
        List<OrderItemLangVO> res = orderItemLangService.listOrderItemLangByIds(orderItemIds);
        return ServerResponseEntity.success(res);
    }

    @Override
    public ServerResponseEntity<List<OrderItemVO>> getListByOrderItemId(List<Long> orderItemIds, Integer lang) {
        List<OrderItemVO> list = orderItemLangService.getListByOrderItemId(orderItemIds, lang);
        return ServerResponseEntity.success(list);
    }
}
