MMessage.Class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * 消息Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2020/03/31
  7. * Time: 15:44
  8. */
  9. namespace JinDouYun\Model\Message;
  10. use Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\Message\DMessage;
  15. class MMessage
  16. {
  17. private $objDMessage;
  18. private $userCenterId;
  19. private $enterpriseId;
  20. public function __construct($enterpriseId, $userCenterId)
  21. {
  22. $this->enterpriseId = $enterpriseId;
  23. $this->userCenterId = $userCenterId;
  24. $this->objDMessage = new DMessage('default');
  25. $this->objDMessage->setTable($this->objDMessage->get_Table().'_'.$enterpriseId);
  26. }
  27. /**
  28. * 消息添加
  29. * @param $params
  30. * @return ResultWrapper
  31. * @throws Exception
  32. */
  33. public function addMessage($params)
  34. {
  35. $insertData['sendId'] = isset($params['sendId']) ? $params['sendId'] : $this->userCenterId;//发送人默认为当前用户
  36. $insertData['receiveId'] = $params['receiveId'];
  37. $insertData['title'] = $params['title'];
  38. $insertData['content'] = $params['content'];
  39. $insertData['type'] = isset($params['type']) ? $params['type'] : StatusCode::$messageType['inventoryNull'];
  40. $insertData['receiveStatus'] = StatusCode::$delete;
  41. $insertData['deleteStatus'] = StatusCode::$standard;
  42. $insertData['sendTime'] = time();
  43. $insertData['createTime'] = time();
  44. $insertData['receiveTime'] = null;
  45. $dbResult = $this->objDMessage->insert($insertData);
  46. if($dbResult === false){
  47. return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
  48. }
  49. return ResultWrapper::success($dbResult);
  50. }
  51. /**
  52. * 消息删除
  53. * @param $params
  54. * @return ResultWrapper
  55. */
  56. public function delMessage($params)
  57. {
  58. $where = ['id' => $params['id']];
  59. $dbResult = $this->objDMessage->update(['deleteStatus' => StatusCode::$delete], $where);
  60. if($dbResult === false){
  61. return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
  62. }else{
  63. return ResultWrapper::success($dbResult);
  64. }
  65. }
  66. /**
  67. * 消息列表
  68. * @param $params
  69. * @param bool $type
  70. * @return ResultWrapper
  71. */
  72. public function getAllMessage($params)
  73. {
  74. $limit = $params['limit'];
  75. unset($params['limit']);
  76. $offset = $params['offset'];
  77. unset($params['offset']);
  78. $where = [
  79. 'deleteStatus' => StatusCode::$standard,
  80. ];
  81. !empty($params['receiveStatus']) && $where['receiveStatus'] = $params['receiveStatus'];
  82. $dbResult = $this->objDMessage->select($where, '*', 'createTime desc', $limit, $offset);
  83. if ($dbResult === false) {
  84. return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
  85. }
  86. $total = $this->objDMessage->count($where);
  87. $return = [
  88. 'data' => $dbResult,
  89. 'total' => ($total) ? intval($total) : 0,
  90. ];
  91. return ResultWrapper::success($return);
  92. }
  93. /**
  94. * 消息详情
  95. * @param $params
  96. * @return ResultWrapper
  97. */
  98. public function getMessageInfo($params)
  99. {
  100. $where = ['id' => $params['id']];
  101. $dbResult = $this->objDMessage->get($where);
  102. if($dbResult === false){
  103. return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
  104. }
  105. return ResultWrapper::success($dbResult);
  106. }
  107. /**
  108. * 消息已读
  109. * @param $params
  110. * @return ResultWrapper
  111. */
  112. public function receiveMessage($params)
  113. {
  114. $where = ['id' => $params['id']];
  115. $dbResult = $this->objDMessage->update(['receiveStatus' => StatusCode::$standard, 'receiveTime' => time()],$where);
  116. if($dbResult === false){
  117. return ResultWrapper::fail($this->objDMessage->error(), ErrorCode::$dberror);
  118. }
  119. return ResultWrapper::success($dbResult);
  120. }
  121. }