<?php

namespace App\System\Task;

use App\System\Basic\beModel;
use App\System\Model\SettingModel;
use App\System\Vendor\beSms;
use \Exception;

class SendSMSTask {
    public function run_task($task){
        $smslist = beModel::beTable("be_sms as s")->beSelect("sid,s.uid,smessage,ui.uidial,ui.uiphone")->beWhere("sstatus","0")->beJoin("INNER JOIN be_user_info as ui ON ui.uid = s.uid")->beLimit(5)->beOrder("sid","ASC")->beGetAll();
        $sendedsms = [];
        foreach($smslist as $sms){
            $response = beSms::beSendSms($sms["uidial"],$sms["uiphone"],$sms["smessage"]);
            if($response["status"]){
                $sendedsms[] = $sms["sid"];
            }
            else{
                beModel::beUpdate(["sstatus" => -1],"be_sms")->beWhere("sid",$sms["sid"])->beExecute();
            }
        }
        if($sendedsms){
            beModel::beUpdate(["sstatus" => 1],"be_sms")->beWhere("sid",$sendedsms,"IN")->beExecute();
        }
    }
}
?>