package com.yc.ship.module.trade.service.notifylog;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.module.trade.dal.dataobject.notifylog.NotifyLogDO;
import com.yc.ship.module.trade.dal.mysql.notifylog.NotifyLogMapper;
import com.yc.ship.module.trade.framework.mq.TradePublishUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Map;


/**
 * 消费通知记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class NotifyLogServiceImpl implements NotifyLogService {

    @Resource
    private NotifyLogMapper notifyLogMapper;
    @Autowired
    private TradePublishUtils tradePublishUtils;


    @Override
    public Integer saveNotifyLog(Map detailInfo, String requestUrl, String requestData, String responseData, Integer notityStatus) {
        int execNum = 1;
        Long checkId = MapUtil.getLong(detailInfo, "check_id");//逾期核销为空
        String voucherCode = MapUtil.getStr(detailInfo, "voucherCode");
        //查询数据库

        NotifyLogDO notifyLog = notifyLogMapper.selectOne(NotifyLogDO::getNotityStatus,1,NotifyLogDO::getVoucherCode,voucherCode); // 查询是否包含推送成功的的数据
        if (notifyLog != null) {
            return 1;
        }

        notifyLog = notifyLogMapper.selectOne(NotifyLogDO::getNotityStatus,0,NotifyLogDO::getVoucherCode,voucherCode); // 查询是否包含推送失败的数据
        if (notifyLog == null) {
            notifyLog = new NotifyLogDO();
        }
        // 通知状态：0失败，1成功
        notifyLog.setNotityStatus(notityStatus);

        // 票号
        notifyLog.setVoucherCode(voucherCode);
        // 检票记录主键
        notifyLog.setCheckId(checkId);
        // 请求地址
        notifyLog.setRequestUrl(requestUrl);
        // 请求内容
        notifyLog.setRequestData(requestData);
        // 响应参数
        notifyLog.setResponseData(responseData);
        //保存数据
        // 更新
        if (null !=notifyLog.getId()) {
            execNum = notifyLog.getExecNum() + 1;
            // 执行次数
            notifyLog.setExecNum(execNum);
            this.updateRefundBack(notifyLog);

        } else {
            // 主键
            long id = IdWorker.getId(notifyLog);
            notifyLog.setId(id);
            // 执行次数
            notifyLog.setExecNum(execNum);
            notifyLogMapper.insert(notifyLog);
        }

        //推送失败 执行次数 <= 6，进行再次排队推送。
        if (execNum < 6 && StringUtils.isNotBlank(checkId.toString()) && notityStatus == 0) {
            // 发送检票通知
//            try {
//                //延迟60秒后重新推送
//                Thread.sleep(60000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            publishUtils.publish(PublishUtils.tkCheck, checkId);
            tradePublishUtils.publishCheckNotifyDelayMsg(checkId.toString(),  60 * 1000);
            // 出票通知推送失败重推
        } else if(execNum < 6 && voucherCode.startsWith("SP")) {
            // 查询订单

        } else if(execNum>=6) {
            //封装内容

        }
        return execNum;
    }


    public int updateRefundBack(NotifyLogDO notifyLog) {
        LambdaUpdateWrapper<NotifyLogDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(NotifyLogDO::getCheckId, notifyLog.getCheckId());
        updateWrapper.set(NotifyLogDO::getRequestUrl, notifyLog.getRequestUrl());
        updateWrapper.set(NotifyLogDO::getRequestData, notifyLog.getRequestData());
        updateWrapper.set(NotifyLogDO::getResponseData, notifyLog.getResponseData());
        updateWrapper.set(NotifyLogDO::getExecNum, notifyLog.getExecNum());
        updateWrapper.set(NotifyLogDO::getNotityStatus, notifyLog.getNotityStatus());
        updateWrapper.eq(NotifyLogDO::getId, notifyLog.getId());
        updateWrapper.eq(NotifyLogDO::getVoucherCode, notifyLog.getVoucherCode());
        return notifyLogMapper.update(updateWrapper);
    }
}