BackendUnreadCount.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace common\models\backend;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\ActiveRecord;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%backend_unread_count}}".
  10. *
  11. * @property int $id
  12. * @property int|null $mall_id 商城ID
  13. * @property int|null $type 项目类型[1=订单,2=售后订单,3=提现,4=通知]
  14. * @property int|null $unread_num 未读总数
  15. * @property int $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int $created_at 添加时间戳
  17. * @property int $updated_at 更新时间戳
  18. */
  19. class BackendUnreadCount extends BaseActiveRecord
  20. {
  21. const ORDER = 1; // 订单
  22. const REFUND = 2; // 售后订单
  23. const WITHDRAWAL = 3; // 提现
  24. const NOTICE = 4; // 消息通知
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%backend_unread_count}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'type', 'unread_num', 'status', 'created_at', 'updated_at'], 'integer'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => '商城ID',
  49. 'type' => '项目类型[1=订单,2=售后订单,3=提现,4=通知]',
  50. 'unread_num' => '未读总数',
  51. 'status' => '状态[-1:删除;0:禁用;1启用]',
  52. 'created_at' => '添加时间戳',
  53. 'updated_at' => '更新时间戳',
  54. ];
  55. }
  56. /**
  57. * 获取未读消息数量
  58. *
  59. * @param array $condition
  60. * @return int
  61. */
  62. public static function getUnReadCount(array $condition)
  63. {
  64. return static::find()->where($condition)->select('unread_num')->scalar();
  65. }
  66. /**
  67. * 根据类型获取未读数量总数
  68. * @param $mall_id
  69. * @param $type
  70. * @return int|null
  71. */
  72. public static function getUnreadNum($mall_id, $type)
  73. {
  74. $model = self::findOne(['mall_id' => $mall_id, 'type' => $type, 'status' => StatusEnum::ENABLED]);
  75. if (empty($model)) {
  76. return 0;
  77. }
  78. return $model->unread_num;
  79. }
  80. /**
  81. * 添加消息未读数量
  82. *
  83. * @param integer $mall_id
  84. * @param integer $type
  85. * @return boolean|int
  86. */
  87. public static function incUnreadNum(int $mall_id, int $type)
  88. {
  89. $model = static::find()->where(['mall_id' => $mall_id, 'type' => $type])->select('id')->one();
  90. if (empty($model)) {
  91. $unread_num = 1;
  92. $model = new static();
  93. $model->load(compact('mall_id', 'type', 'unread_num'), '');
  94. return $model->save();
  95. }
  96. return static::updateAllCounters(['unread_num' => 1], ['id' => $model->id]);
  97. }
  98. /**
  99. * 减少消息未读数量
  100. *
  101. * @param integer $mall_id
  102. * @param integer $type
  103. * @return int
  104. */
  105. public static function decUnreadNum(int $mall_id, int $type)
  106. {
  107. return static::updateAllCounters(['unread_num' => -1], ['mall_id' => $mall_id, 'type' => $type]);
  108. }
  109. }