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

namespace JinDouYun\Controller\Manage;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Manage\MWxTemplate;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

/**
 * 微信模版
 * Class WxTemplate
 * @package JinDouYun\Controller\Manage
 */
class WxTemplate extends BaseController
{
    private $objMWxTemplate;

    /**
     * WxTemplate constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @param bool $checkToken
     * @throws \Exception
     */
    public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
        $this->objMWxTemplate = new MWxTemplate();
    }

    /**
     * 获取企业小程序模版
     */
    public function getAll()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset(isset($params['page']) ? $params['page'] : 1, isset($params['pageSize']) ? $params['pageSize'] : 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        $selectParams['enterpriseName'] = isset($params['enterpriseName']) ? $params['enterpriseName'] : '';
        $selectParams['isWeChatStatus'] = isset($params['isWeChatStatus']) ? $params['isWeChatStatus'] : '';

        $result = $this->objMWxTemplate->getAll($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => isset($params['page']) ? $params['page'] : 1,
                'pageSize'  => isset($params['pageSize']) ? $params['pageSize'] : 10,
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());

    }

    /**
     * 提交代码&&提交审核
     */
    public function submitCodeAndAudit()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $mapping = [];
        foreach ($params as $key => $val) {
            $mapping[$key] = [
                'enterpriseId'     => isset($val['enterpriseId']) ? $val['enterpriseId'] : null,
                'weixinTemplateId' => isset($val['weixinTemplateId']) ? $val['weixinTemplateId'] : null,
                'auditStatus'      => isset($val['auditStatus']) ? $val['auditStatus'] : null,
                'templateId'       => isset($val['templateId']) ? $val['templateId'] : null,
                'version'          => isset($val['version']) ? $val['version'] : null,
                'token'            => isset($val['token']) ? $val['token'] : null,
            ];

            foreach ($mapping[$key] as $k => $v) {
                if (empty($v) && $v === null) {
                    parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
                }
            }
        }
        $result = $this->objMWxTemplate->submitCodeAndAudit($mapping);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData(), 0);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 发布
     */
    public function release()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $mapping = [];
        foreach ($params as $key => $val) {
            $mapping[$key] = [
                'enterpriseId' => isset($val['enterpriseId']) ? $val['enterpriseId'] : null,
                'templateId'   => isset($val['templateId']) ? $val['templateId'] : null,
                'auditStatus'  => isset($val['auditStatus']) ? $val['auditStatus'] : null,
            ];

            foreach ($mapping[$key] as $k => $v) {
                if (empty($v) && $v === null) {
                    parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
                }
                if ($k == 'auditStatus' && $v != StatusCode::$wxAuditStatus['pass']) {
                    parent::sendOutput('企业' . $val['enterpriseId'] . '小程序暂未审核成功,请等待审核成功后发布', ErrorCode::$paramError);
                }
            }
        }
        $result = $this->objMWxTemplate->release($mapping);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData(), 0);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 撤销审核
     */
    public function revoke()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $mapping = [];
        foreach ($params as $key => $val) {
            $mapping[$key] = [
                'enterpriseId' => isset($val['enterpriseId']) ? $val['enterpriseId'] : null,
                'templateId'   => isset($val['templateId']) ? $val['templateId'] : null,
                'auditStatus'  => isset($val['auditStatus']) ? $val['auditStatus'] : null,
            ];

            foreach ($mapping[$key] as $k => $v) {
                if (empty($v) && $v === null) {
                    parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
                }
            }
        }
        $result = $this->objMWxTemplate->revoke($mapping);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData(), 0);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}