<?php
/**
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2020/5/8
 * Time: 15:01
 */

namespace JinDouYun\Controller\Cron;


use JinDouYun\Dao\System\DEnterpriseBindTemplate;
use JinDouYun\Model\System\MEnterpriseBindTemplate;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Factory;

/**
 * 获取小程序审核状态
 * Class GetWxAuditStatus
 * @package JinDouYun\Controller\Cron
 */
class GetWxAuditStatus
{
    private $oplatformConfigData;

    private $objDEnterpriseBindTemplate;

    public function __construct()
    {
        echo date('Y-m-d H:i:s') . ': S-获取小程序审核状态' . PHP_EOL;
        $this->objDEnterpriseBindTemplate = new DEnterpriseBindTemplate();
        $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
        if (empty($oplatformConfigData)) {
            exit('配置错误');
        }
        $this->oplatformConfigData = $oplatformConfigData;
    }

    public function pullWxAuditStatus()
    {
        //获取正在审核中小程序的审核状态
        $dbResult = $this->objDEnterpriseBindTemplate->select(['enableStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$wxAuditStatus['inAudit']]);
        if ($dbResult === false) {
            file_put_contents('/www/wwwroot/logs/api.junhailan.com/cron.log', date('Y-m-d H:i:s') . '查询审核中模版失败: ' . var_export($this->objDEnterpriseBindTemplate->error(), true) . PHP_EOL, FILE_APPEND);
            die;
        }
        if (empty($dbResult)) {
            file_put_contents('/www/wwwroot/logs/api.junhailan.com/cron.log', date('Y-m-d H:i:s') . '暂时没有审核中的模版' . PHP_EOL, FILE_APPEND);
            die;
        }

        foreach ($dbResult as $value) {
            $objMEnterpriseBindTemplate = new MEnterpriseBindTemplate(null, $value['enterpriseId']);
            $dbResult = $objMEnterpriseBindTemplate->getAuditStatus([
                'templateId'   => $value['templateId'],
                'enterpriseId' => $value['enterpriseId'],
                'auditId'      => $value['auditId'],
            ]);
            if (!$dbResult->isSuccess()){
                file_put_contents('/www/wwwroot/logs/api.junhailan.com/cron.log', date('Y-m-d H:i:s') . '查询审核状态失败: ' . var_export($dbResult->getData(), true) . PHP_EOL, FILE_APPEND);
                continue;
            }
        }
    }

    public function __destruct()
    {
        echo date('Y-m-d H:i:s') . ': E-获取小程序审核状态' . PHP_EOL;
        // TODO: Implement __destruct() method.
    }
}