<?php
/**
 * 企业使用模版
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/29
 * Time: 16:36
 */

namespace JinDouYun\Controller\System;

use Mall\Framework\Core\ErrorCode;

use JinDouYun\Cache\EnterpriseCache;
use JinDouYun\Controller\BaseController;

use JinDouYun\Model\System\MEnterpriseBindTemplate;
use Mall\Framework\Core\StatusCode;

class EnterpriseBindTemplate extends BaseController
{
    private $objMEnterpriseBindTemplate;

    /**
     * SystemTemplate constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMEnterpriseBindTemplate = new MEnterpriseBindTemplate($this->onlineUserId, $this->onlineEnterpriseId);
    }

    /**
     * 企业模版 (启用/停用)
     */
    public function bindTemplate()
    {
        $params = $this->request->getRawJson();
        $data = [
            'templateId'       => $this->request->param('request_id'),
            'enableStatus'     => isset($params['enableStatus']) ? $params['enableStatus'] : '',
            'weixinTemplateId' => isset($params['weixinTemplateId']) ? $params['weixinTemplateId'] : '',
            'enterpriseId'     => $this->onlineEnterpriseId,
            'version'          => isset($params['version']) ? $params['version'] : '',
        ];
        foreach ($data as $key => $value) {
            if (empty($value) && $value != 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMEnterpriseBindTemplate->bindTemplate($data, $this->token);
        if ($result->isSuccess()) {
            $objEnterpriseCache = new EnterpriseCache();
            $objEnterpriseCache->EnterpriseDataCache($this->onlineEnterpriseId, 'weixinTemplateId', $data['weixinTemplateId']);
            $objEnterpriseCache->EnterpriseDataCache($this->onlineEnterpriseId, 'templateId', $data['templateId']);
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 获取企业正在使用的模版
     */
    public function getAllTemplate()
    {
        $dbResult = $this->objMEnterpriseBindTemplate->getAllTemplate();
        if ($dbResult->isSuccess()) {
            $returnData = $dbResult->getData();
            parent::sendOutput($returnData['data'], 0);
        }
        parent::sendOutput($dbResult->getData(), ErrorCode::$dberror);
    }



    /**
     * 2-1,查询审核状态
     */
    public function getAuditStatus()
    {
        $params = $this->request->getRawJson();
        $paramsData = [
            'templateId'   => isset($params['templateId']) ? $params['templateId'] : '',
            'enterpriseId' => $this->onlineEnterpriseId,
            'auditId'      => isset($params['auditId']) ? $params['auditId'] : '',
        ];
        foreach ($paramsData as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数为空!', ErrorCode::$paramError);
            }
        }
        $dbResult = $this->objMEnterpriseBindTemplate->getAuditStatus($paramsData);
        if (!$dbResult->isSuccess()) {
            parent::sendOutput($dbResult->getData(), ErrorCode::$dberror);
        }
        parent::sendOutput($dbResult->getData());
    }


    /**
     * 小程序提交代码
     */
    public function submitCode()
    {
        $params = $this->request->getRawJson();
        $data = [
            'templateId'       => $this->request->param('request_id'),
            'enableStatus'     => StatusCode::$standard,
            'weixinTemplateId' => isset($params['weixinTemplateId']) ? $params['weixinTemplateId'] : '',
            'enterpriseId'     => $this->onlineEnterpriseId,
            'version'          => isset($params['version']) ? $params['version'] : '',
        ];
//        var_dump( $data);
//        die;
        foreach ($data as $key => $value) {
            if (empty($value) && $value != 0) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMEnterpriseBindTemplate->submitCode($data, $this->token);
        if ($result->isSuccess()) {
            $objEnterpriseCache = new EnterpriseCache();
            $objEnterpriseCache->EnterpriseDataCache($this->onlineEnterpriseId, 'weixinTemplateId', $data['weixinTemplateId']);
            $objEnterpriseCache->EnterpriseDataCache($this->onlineEnterpriseId, 'templateId', $data['templateId']);
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }


    /**
     * 2.提交审核
     */
    public function submitAudit()
    {
        $params = $this->request->getRawJson();
        $paramsData = [
            'templateId'   => isset($params['templateId']) ? $params['templateId'] : '',
            'enterpriseId' => $this->onlineEnterpriseId,
        ];
        foreach ($paramsData as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数为空!', ErrorCode::$paramError);
            }
        }
        $dbResult = $this->objMEnterpriseBindTemplate->submitAudit($paramsData);

        if (!$dbResult->isSuccess()) {
            parent::sendOutput($dbResult->getData(), ErrorCode::$dberror);
        }
        parent::sendOutput($dbResult->getData());
    }

    /**
     * 3.发布
     */
    public function release()
    {
        $params = $this->request->getRawJson();
        $paramsData = [
            'templateId'   => isset($params['templateId']) ? $params['templateId'] : '',
            'enterpriseId' => $this->onlineEnterpriseId,
        ];
        foreach ($paramsData as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数为空!', ErrorCode::$paramError);
            }
        }
        $dbResult = $this->objMEnterpriseBindTemplate->release($paramsData);
        if (!$dbResult->isSuccess()) {
            parent::sendOutput($dbResult->getData(), ErrorCode::$dberror);
        }
        parent::sendOutput($dbResult->getData());
    }

    /**
     * 获取当前提交代码之后,小程序的状态
     */
    public function getWxStatus()
    {
        $dbResult = $this->objMEnterpriseBindTemplate->getWxStatus();
        if (!$dbResult->isSuccess()) {
            parent::sendOutput($dbResult->getData(), ErrorCode::$dberror);
        }
        parent::sendOutput($dbResult->getData());
    }
}