<?php
/**
 * 消息Model
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/03/31
 * Time: 15:44
 */

namespace JinDouYun\Model\Message;

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

use JinDouYun\Dao\Message\DMessage;

class MMessage
{

    private $objDMessage;
    private $userCenterId;
    private $enterpriseId;

    public function __construct($enterpriseId, $userCenterId)
    {
        $this->enterpriseId = $enterpriseId;
        $this->userCenterId = $userCenterId;
        $this->objDMessage = new DMessage('default');
        $this->objDMessage->setTable($this->objDMessage->get_Table().'_'.$enterpriseId);
    }

    /**
     * 消息添加
     * @param $params
     * @return ResultWrapper
     * @throws Exception
     */
    public function addMessage($params)
    {
        $insertData['sendId'] = isset($params['sendId']) ? $params['sendId'] : $this->userCenterId;//发送人默认为当前用户
        $insertData['receiveId'] = $params['receiveId'];
        $insertData['title'] = $params['title'];
        $insertData['content'] = $params['content'];
        $insertData['type'] = isset($params['type']) ? $params['type'] : StatusCode::$messageType['inventoryNull'];
        $insertData['receiveStatus'] = StatusCode::$delete;
        $insertData['deleteStatus'] = StatusCode::$standard;
        $insertData['sendTime'] = time();
        $insertData['createTime'] = time();
        $insertData['receiveTime'] = null;
        $dbResult = $this->objDMessage->insert($insertData);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 消息删除
     * @param $params
     * @return ResultWrapper
     */
    public function delMessage($params)
    {
        $where = ['id' => $params['id']];
        $dbResult = $this->objDMessage->update(['deleteStatus' => StatusCode::$delete], $where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }

    /**
     * 消息列表
     * @param $params
     * @param bool $type
     * @return ResultWrapper
     */
    public function getAllMessage($params)
    {
        $limit = $params['limit'];
        unset($params['limit']);
        $offset = $params['offset'];
        unset($params['offset']);

        $where = [
            'deleteStatus' => StatusCode::$standard,
        ];

        !empty($params['receiveStatus']) && $where['receiveStatus'] = $params['receiveStatus'];
        $dbResult = $this->objDMessage->select($where, '*', 'createTime desc', $limit, $offset);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
        }

        $total = $this->objDMessage->count($where);

        $return = [
            'data' => $dbResult,
            'total' => ($total) ? intval($total) : 0,
        ];

        return ResultWrapper::success($return);
    }

    /**
     * 消息详情
     * @param $params
     * @return ResultWrapper
     */
    public function getMessageInfo($params)
    {
        $where = ['id' => $params['id']];
        $dbResult = $this->objDMessage->get($where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 消息已读
     * @param $params
     * @return ResultWrapper
     */
    public function receiveMessage($params)
    {
        $where = ['id' => $params['id']];
        $dbResult = $this->objDMessage->update(['receiveStatus' => StatusCode::$standard, 'receiveTime' => time()],$where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

}