package com.mortals.xhx.busiz.service.impl;

import com.alibaba.fastjson.JSON;
import com.mortals.framework.idempotent.annotation.Idempotent;
import com.mortals.xhx.busiz.entity.OrderEntity;
import com.mortals.xhx.busiz.service.TestIdempotentAnnotationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * TestIdempotentAnnotationService实现
 *
 * @author wenpan 2023/01/07 10:15
 */
@Slf4j
@Service
public class TestIdempotentAnnotationServiceImpl implements TestIdempotentAnnotationService {

    /**
     * 用orderEntity对象里的orderNumber作为业务幂等键
     */
    @Idempotent(operationType = "'addOrder'", source = "#orderEntity.platformCode",
            determineCurrentLookupKey = "mySqlIdempotentRepository", businessKey = "#orderEntity.orderNumber")
    @Override
    public OrderEntity addOrder(OrderEntity orderEntity) {
        log.info("TestIdempotentAnnotationServiceImpl#addOrder orderEntity is : {}", JSON.toJSONString(orderEntity));
        // 模拟修改了订单一些信息
        orderEntity.setCreateDate(LocalDateTime.now());
        orderEntity.setLastModifyDate(LocalDateTime.now());
        orderEntity.setOrderStatus(orderEntity.getOrderStatus() + 1);
        // 新增订单逻辑....

        return orderEntity;
    }

    /**
     * 用orderEntity对象里的orderLineEntityList集合里的第一个元素的skuCode作为业务幂等键
     */
    @Idempotent(operationType = "'modifyOrder'",
            source = "#orderEntity.platformCode",
            businessKey = "#orderEntity.orderLineEntityList[0].skuCode")
    @Override
    public OrderEntity modifyOrder(OrderEntity orderEntity) {
        log.info("TestIdempotentAnnotationServiceImpl#modifyOrder orderEntity is : {}", JSON.toJSONString(orderEntity));
        // 模拟修改了订单一些信息
        orderEntity.setCreateDate(LocalDateTime.now());
        orderEntity.setLastModifyDate(LocalDateTime.now());
        orderEntity.setOrderStatus(orderEntity.getOrderStatus() + 1);
        // 修改订单逻辑....

        return orderEntity;
    }
}
