
package com.tmerclub.cloud.order.feign;

import cn.hutool.core.bean.BeanUtil;
import com.tmerclub.cloud.api.order.feign.OrderAddrFeignClient;
import com.tmerclub.cloud.common.leaf.manager.SegmentManager;
import com.tmerclub.cloud.common.order.vo.OrderAddrVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.constant.DistributedIdKey;
import com.tmerclub.cloud.order.model.OrderAddr;
import com.tmerclub.cloud.order.service.OrderAddrService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @Author lth
 * @Date 2021/11/22 9:21
 */
@DubboService
public class OrderAddrFeignController implements OrderAddrFeignClient {

    @Autowired
    private OrderAddrService orderAddrService;
    @Autowired
    private SegmentManager segmentManager;

    @Override
    public ServerResponseEntity<OrderAddrVO> getOrderAddrByOrderId(Long orderId) {
        OrderAddrVO orderAddrVO = orderAddrService.getByOrderId(orderId);
        return ServerResponseEntity.success(orderAddrVO);
    }

    @Override
    public ServerResponseEntity<Long> addCueAddr(OrderAddrVO orderAddrVO) {
        OrderAddr orderAddr = BeanUtil.toBean(orderAddrVO, OrderAddr.class);
        Long addrId = segmentManager.getSegmentIdWithDateTime(DistributedIdKey.tmerclub_ORDER_ADDR, orderAddr.getUserId());
        orderAddr.setOrderAddrId(addrId);
        orderAddr.setCreateTime(new Date());
        orderAddrService.save(orderAddr);
        return ServerResponseEntity.success(addrId);
    }
}
