| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace common\models\backend;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%backend_unread_count}}".
- *
- * @property int $id
- * @property int|null $mall_id 商城ID
- * @property int|null $type 项目类型[1=订单,2=售后订单,3=提现,4=通知]
- * @property int|null $unread_num 未读总数
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- */
- class BackendUnreadCount extends BaseActiveRecord
- {
- const ORDER = 1; // 订单
- const REFUND = 2; // 售后订单
- const WITHDRAWAL = 3; // 提现
- const NOTICE = 4; // 消息通知
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%backend_unread_count}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'type', 'unread_num', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'type' => '项目类型[1=订单,2=售后订单,3=提现,4=通知]',
- 'unread_num' => '未读总数',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '添加时间戳',
- 'updated_at' => '更新时间戳',
- ];
- }
- /**
- * 获取未读消息数量
- *
- * @param array $condition
- * @return int
- */
- public static function getUnReadCount(array $condition)
- {
- return static::find()->where($condition)->select('unread_num')->scalar();
- }
- /**
- * 根据类型获取未读数量总数
- * @param $mall_id
- * @param $type
- * @return int|null
- */
- public static function getUnreadNum($mall_id, $type)
- {
- $model = self::findOne(['mall_id' => $mall_id, 'type' => $type, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- return 0;
- }
- return $model->unread_num;
- }
- /**
- * 添加消息未读数量
- *
- * @param integer $mall_id
- * @param integer $type
- * @return boolean|int
- */
- public static function incUnreadNum(int $mall_id, int $type)
- {
- $model = static::find()->where(['mall_id' => $mall_id, 'type' => $type])->select('id')->one();
- if (empty($model)) {
- $unread_num = 1;
- $model = new static();
- $model->load(compact('mall_id', 'type', 'unread_num'), '');
- return $model->save();
- }
- return static::updateAllCounters(['unread_num' => 1], ['id' => $model->id]);
- }
- /**
- * 减少消息未读数量
- *
- * @param integer $mall_id
- * @param integer $type
- * @return int
- */
- public static function decUnreadNum(int $mall_id, int $type)
- {
- return static::updateAllCounters(['unread_num' => -1], ['mall_id' => $mall_id, 'type' => $type]);
- }
- }
|