<?php
/**
 * 消息Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/03/31
 * Time: 16:00
 */

namespace JinDouYun\Controller\Message;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Message\MMessage;

class Message extends BaseController
{
    private $objMMessage;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMMessage = new MMessage($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 消息添加
     * @throws Exception
     */
    public function addMessage()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'receiveId' => isset($params['receiveId']) ? $params['receiveId'] : '',
            'title' => isset($params['title']) ? $params['title'] : '',
            'content' => isset($params['content']) ? $params['content'] : '',
        ];

        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMMessage->addMessage($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 消息删除
     */
    public function delMessage()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMMessage->delMessage($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 消息列表
     */
    public function getAllMessage()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset(isset($params['page']) ? $params['page'] : 1, isset($params['pageSize']) ? $params['pageSize'] : 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        isset($params['receiveStatus']) && $selectParams['receiveStatus'] = $params['receiveStatus'];

        $result = $this->objMMessage->getAllMessage($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 消息详情
     */
    public function getMessageInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMMessage->getMessageInfo($params);

        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 消息已读
     */
    public function receiveMessage()
    {
        $params['id'] = $this->request->param('request_id');
        if(!$params['id']){
            $data = $this->request->getRawJson();
            $params['id'] = isset($data['id']) ? $data['id'] : '';
        }
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMMessage->receiveMessage($params);

        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

}