| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace common\models\backend;
- use common\models\base\BaseActiveRecord;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%backend_notice}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $notice_type 通知类型
- * @property string $title 消息标题
- * @property string|null $content 消息内容
- * @property int $is_read 是否已读,1-是,0-否
- * @property int $status 状态
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- */
- class BackendNotice extends BaseActiveRecord
- {
- const ARREARS = 1; // 商场到期通知
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%backend_notice}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'notice_type', 'is_read', 'status', 'created_at', 'updated_at'], 'integer'],
- [['content'], 'string'],
- [['title'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'notice_type' => '通知类型',
- 'title' => '消息标题',
- 'content' => '消息内容',
- 'is_read' => '是否已读,1-是,0-否',
- 'status' => '状态',
- 'created_at' => '添加时间戳',
- 'updated_at' => '更新时间戳',
- ];
- }
- /**
- * 生成消息通知数据
- * @param $params
- * @return bool
- */
- public static function setData($params)
- {
- try {
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 消息阅读
- *
- * @param array $condition
- * @return int
- */
- public static function readNotice(int $id, int $mall_id)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- BackendUnreadCount::decUnreadNum($mall_id, BackendUnreadCount::NOTICE);
- static::updateAll(['is_read' => 1], compact('mall_id', 'id'));
- $t->commit();
- } catch (Exception $e) {
- Yii::error('消息阅读错误: ' . $e->getMessage());
- $t->rollBack();
- return false;
- }
- return true;
- }
- /**
- * 添加站点消息
- *
- * @param integer $mall_id
- * @param string $title
- * @param string $content
- * @param int $notice_type
- * @return boolean
- */
- public static function addMallNotice(int $mall_id, string $title, string $content, int $notice_type = self::ARREARS)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- static::setData(compact('mall_id', 'title', 'content', 'notice_type'));
- BackendUnreadCount::incUnreadNum($mall_id, BackendUnreadCount::NOTICE);
- $t->commit();
- } catch (Exception $e) {
- Yii::error('消息阅读错误: ' . $e->getMessage());
- $t->rollBack();
- return false;
- }
- return true;
- }
- }
|