<?php
/**
 * 企业模版模块数据
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/29
 * Time: 15:30
 */

namespace JinDouYun\Model\System;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Dao\System\DEnterpriseTemplateModule;

class MEnterpriseTemplateModule
{
    private $objDEnterpriseTemplateModule;

    private $onlineUserId;

    private $onlineEnterpriseId;

    private $objMTemplateModule;

    /**
     * MEnterpriseTemplateModule constructor.
     * @param $onlineUserId
     * @param $enterpriseId
     * @throws \Exception
     */
    public function __construct($onlineUserId, $enterpriseId)
    {
        $this->onlineUserId = $onlineUserId;
        $this->onlineEnterpriseId = $enterpriseId;

        $this->objMTemplateModule = new MTemplateModule($this->onlineUserId, $this->onlineEnterpriseId);
        $this->objDEnterpriseTemplateModule = new DEnterpriseTemplateModule('default');
        $this->objDEnterpriseTemplateModule->setTable($this->objDEnterpriseTemplateModule->get_Table() . '_' . $this->onlineEnterpriseId);
    }

    /**
     * @param $params
     * @return ResultWrapper
     */
    public function add($params)
    {
        $moduleId = $this->objMTemplateModule->getModuleIdByAlias(['alias' => $params['moduleAlias'], 'templateId' => $params['templateId']]);
        if (empty($moduleId)) {
            return ResultWrapper::fail('未找到模板', ErrorCode::$dberror);
        }
        $params['moduleId'] = $moduleId;
        $dbResult = $this->objDEnterpriseTemplateModule->replace($params);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDEnterpriseTemplateModule->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 根据模版id+模块id,获取当前企业下改模块的数据
     * @param $selectParams
     * @return ResultWrapper
     */
    public function getModuleInfo($selectParams)
    {
        $moduleId = $this->objMTemplateModule->getModuleIdByAlias(['alias' => $selectParams['moduleAlias'], 'templateId' => $selectParams['templateId']]);
        if (empty($moduleId)) {
            return ResultWrapper::fail('未找到模板', ErrorCode::$dberror);
        }
        $selectParams['moduleId'] = $moduleId;
        $dbResult = $this->objDEnterpriseTemplateModule->get($selectParams);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDEnterpriseTemplateModule->error(), ErrorCode::$dberror);
        }
        if (isset($dbResult['moduleData'])) {
            $dbResult['moduleData'] = empty($dbResult['moduleData']) ? [] : json_decode($dbResult['moduleData'], true);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 编辑企业模版模块数据
     * @param $params
     * @return ResultWrapper
     */
    public function edit($params)
    {
        $moduleId = $this->objMTemplateModule->getModuleIdByAlias(['alias' => $params['moduleAlias'], 'templateId' => $params['templateId']]);
        if (empty($moduleId)) {
            return ResultWrapper::fail('未找到模板', ErrorCode::$dberror);
        }
        $params['moduleId'] = $moduleId;
        $dbResult = $this->objDEnterpriseTemplateModule->replace($params);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDEnterpriseTemplateModule->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 获取企业模版模块数据数据
     * @param $selectParams
     * @return array|bool
     */
    public function getModuleByIds($selectParams)
    {
        $dbResult = $this->objDEnterpriseTemplateModule->select($selectParams);
        if ($dbResult === false) {
            return [];
        }
        $return = [];
        foreach ($dbResult as $key => $val) {
            $return[$val['moduleAlias']] = $val;
        }
        return $return;
    }
}