BackendNotice.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace common\models\backend;
  3. use common\models\base\BaseActiveRecord;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%backend_notice}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $notice_type 通知类型
  12. * @property string $title 消息标题
  13. * @property string|null $content 消息内容
  14. * @property int $is_read 是否已读,1-是,0-否
  15. * @property int $status 状态
  16. * @property int $created_at 添加时间戳
  17. * @property int $updated_at 更新时间戳
  18. */
  19. class BackendNotice extends BaseActiveRecord
  20. {
  21. const ARREARS = 1; // 商场到期通知
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%backend_notice}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'notice_type', 'is_read', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['content'], 'string'],
  37. [['title'], 'string', 'max' => 255],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'notice_type' => '通知类型',
  49. 'title' => '消息标题',
  50. 'content' => '消息内容',
  51. 'is_read' => '是否已读,1-是,0-否',
  52. 'status' => '状态',
  53. 'created_at' => '添加时间戳',
  54. 'updated_at' => '更新时间戳',
  55. ];
  56. }
  57. /**
  58. * 生成消息通知数据
  59. * @param $params
  60. * @return bool
  61. */
  62. public static function setData($params)
  63. {
  64. try {
  65. $model = new self();
  66. $model->loadDefaultValues();
  67. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  68. return true;
  69. } catch (\Exception $e) {
  70. self::setStaticError($e->getMessage());
  71. return false;
  72. }
  73. }
  74. /**
  75. * 消息阅读
  76. *
  77. * @param array $condition
  78. * @return int
  79. */
  80. public static function readNotice(int $id, int $mall_id)
  81. {
  82. $t = Yii::$app->db->beginTransaction();
  83. try {
  84. BackendUnreadCount::decUnreadNum($mall_id, BackendUnreadCount::NOTICE);
  85. static::updateAll(['is_read' => 1], compact('mall_id', 'id'));
  86. $t->commit();
  87. } catch (Exception $e) {
  88. Yii::error('消息阅读错误: ' . $e->getMessage());
  89. $t->rollBack();
  90. return false;
  91. }
  92. return true;
  93. }
  94. /**
  95. * 添加站点消息
  96. *
  97. * @param integer $mall_id
  98. * @param string $title
  99. * @param string $content
  100. * @param int $notice_type
  101. * @return boolean
  102. */
  103. public static function addMallNotice(int $mall_id, string $title, string $content, int $notice_type = self::ARREARS)
  104. {
  105. $t = Yii::$app->db->beginTransaction();
  106. try {
  107. static::setData(compact('mall_id', 'title', 'content', 'notice_type'));
  108. BackendUnreadCount::incUnreadNum($mall_id, BackendUnreadCount::NOTICE);
  109. $t->commit();
  110. } catch (Exception $e) {
  111. Yii::error('消息阅读错误: ' . $e->getMessage());
  112. $t->rollBack();
  113. return false;
  114. }
  115. return true;
  116. }
  117. }