package com.icloud.server.payutils.notify;

import com.icloud.server.payutils.dto.PayNotifyTaskDO;
import com.icloud.server.payutils.order.PayOrderService;
import com.icloud.server.utils.time.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * 支付通知 Core Service 实现类
 *
 * @author
 */
@Service
@Valid
@Slf4j
public class PayNotifyServiceImpl implements PayNotifyService {

    /**
     * 通知超时时间，单位：秒
     */
    public static final int NOTIFY_TIMEOUT = 120;
    /**
     * {@link #NOTIFY_TIMEOUT} 的毫秒
     */
    public static final long NOTIFY_TIMEOUT_MILLIS = 120 * DateUtils.SECOND_MILLIS;

    @Resource
    @Lazy // 循环依赖，避免报错
    private PayOrderService orderService;
//    @Resource
//    @Lazy // 循环依赖，避免报错
//    private PayRefundService refundService;



    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor; // TODO ：未来提供独立的线程池

    @Resource
    private PayNotifyLockRedisDAO payNotifyLockCoreRedisDAO;



    @Override
    public void createPayNotifyTask(PayNotifyTaskCreateReqDTO reqDTO) {
        PayNotifyTaskDO task = new PayNotifyTaskDO();
        task.setType(reqDTO.getType()).setDataId(reqDTO.getDataId());
        task.setStatus(PayNotifyStatusEnum.WAITING.getStatus()).setNextNotifyTime(new Date())
                .setNotifyTimes(0).setMaxNotifyTimes(PayNotifyTaskDO.NOTIFY_FREQUENCY.length + 1);
        // 补充 merchantId + appId + notifyUrl 字段
        if (Objects.equals(task.getType(), PayNotifyTypeEnum.ORDER.getType())) {


           //保存支付单通知
        } else if (Objects.equals(task.getType(), PayNotifyTypeEnum.REFUND.getType())) {
            //保存退款单通知
        }

        // 保存订单



    }


}
