<?php
/**
 * 系统推送消息
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2021/05/07
 * Time: 15:38
 */

namespace JinDouYun\Controller\System;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\System\MSystemPushMessage;

class SystemPushMessage extends BaseController
{
    private $objMSystemPushMessage;

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

    /**
     * 添加,编辑系统模版
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'title' => isset($params['title']) ? $params['title'] : '',
        ];
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $data['enableStatus'] = isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$delete;
        return $data;
    }

    /**
     * 添加系统模版
     * @throws \Exception
     */
    public function add()
    {
        $data = $this->commonFieldFilter();
        $result = $this->objMSystemPushMessage->add($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 推送渠道启用/停用
     */
    public function updatePushEnableStatus()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $updateData = [
            'id' => getArrayItem($params, 'id', 0),
            'weixinEnabledStatus' => getArrayItem($params, 'weixinEnabledStatus', 0),
            'smsEnabledStatus' => getArrayItem($params, 'smsEnabledStatus', 0),
        ];
        foreach ($updateData as $key => $value) {
            if (empty($value)) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMSystemPushMessage->updatePushEnableStatus($updateData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }


    /**
     * 推送消息启用/停用
     */
    public function updateEnableStatus()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $updateData = [
            'id' => getArrayItem($params, 'id', 0),
            'enabledStatus' => getArrayItem($params, 'enabledStatus', 0),
        ];
        foreach ($updateData as $key => $value) {
            if (empty($value)) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMSystemPushMessage->updateEnableStatus($updateData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 获取系统内置推送消息模板列表
     */
    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'],
            'offset' => $pageParams['offset'],
            'type'   => getArrayItem($params, 'type', 0),
            'enabledStatus' => getArrayItem($params, 'enabledStatus', 0),
            'keyword' => getArrayItem($params, 'keyword', ''),
        ];
        $dbResult = $this->objMSystemPushMessage->getAll($selectParams);
        if ($dbResult->isSuccess()) {
            $returnData = $dbResult->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($dbResult->getData(), ErrorCode::$dberror);
    }

    /**
     * 配置详情
     */
    public function settingDetail()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMSystemPushMessage->settingDetail($id);
        if($result->isSuccess()){
            $this->sendOutput($result->getData());
        }else{
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 编辑系统模版
     */
    public function edit()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $updateData = [
            'id' => getArrayItem($params, 'id', 0),
            'weixinEnabledStatus' => getArrayItem($params, 'weixinEnabledStatus', 0),
            'smsEnabledStatus' => getArrayItem($params, 'smsEnabledStatus', 0),
            'weixinTemplateId' => getArrayItem($params, 'weixinTemplateId', 0),
        ];
        foreach ($updateData as $key => $value) {
            if (empty($value)) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMSystemPushMessage->edit($updateData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 删除系统模版
     */
    public function del()
    {
        $id = $this->request->param('request_id');
        if (!$id) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        if (!is_array($id)) {
            $id = [$id];
        }
        $result = $this->objMSystemPushMessage->del($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 自动创建微信模板消息
     */
    public function autoCreateWeiXinTemplateId()
    {
        $id = $this->request->param('request_id');
        if (!$id) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMSystemPushMessage->autoCreateWeiXinTemplateId($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());

    }
}