package com.ruoyi.database.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.util.SySMS;
import com.ruoyi.database.domain.BaseFlowLog;
import com.ruoyi.database.mapper.BaseFlowLogMapper;
import com.ruoyi.database.service.BaseFlowLogService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (流程日志记录)ServiceImpl
 *
 * @author makejava
 * @since 2024-09-02 15:36:54
 */
@Service
public class BaseFlowLogServiceImpl extends ServiceImpl<BaseFlowLogMapper, BaseFlowLog> implements BaseFlowLogService {

    @Override
    public void selectSMSStatus() {
        List<BaseFlowLog> list = lambdaQuery().eq(BaseFlowLog::getSendType, 2)
                .eq(BaseFlowLog::getSendResult, 0)
                .list();
        for (BaseFlowLog baseFlowLog : list){
            long time = System.currentTimeMillis() / 1000L;
            long cTime = 60 * 15L;
            if (time - baseFlowLog.getOperTime() > cTime){
                baseFlowLog.setSendResult(2);
                baseFlowLog.setErrReason("超时未反馈");
                updateById(baseFlowLog);
            }
            String smsId = baseFlowLog.getSmsId();
            if (!"".equals(smsId)){
                Map<String, String> map = SySMS.sendMesStatus(smsId);
                if (map.isEmpty()){
                    continue;
                }
                String sentresult = map.get("sentresult");
                String smsstatus = map.get("smsstatus");
                if ("3002".equals(sentresult)){
                    baseFlowLog.setSendResult(2);
                    baseFlowLog.setErrReason(smsstatus);
                }else {
                    baseFlowLog.setSendResult(1);
                }
            }
            updateById(baseFlowLog);
        }
    }
}
