| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace addons\OperatingNote\common\models;
- use addons\OperatingNote\common\enums\NoteEnum;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_operating_note_user_like}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $user_id 用户id
- * @property int $note_content_id 点赞笔记id
- * @property int $is_like 是否点赞 1/是 0/否
- * @property int $status
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsOperatingNoteUserLike extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operating_note_user_like}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'note_content_id', 'is_like'], 'required'],
- [['mall_id', 'user_id', 'note_content_id', 'is_like', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户id',
- 'note_content_id' => '点赞笔记id',
- 'is_like' => '是否点赞 1/是 0/否',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData($id, $user_id, $mall_id)
- {
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- $msg = '';
- $model = self::getOne([['user_id' => $user_id, 'note_content_id' => $id]]);
- $note = AddonsOperatingNoteContent::getOne([['id' => $id]]);
- if (!$model) {
- $model = new self();
- $model->user_id = $user_id;
- $model->mall_id = $mall_id;
- $model->note_content_id = $id;
- $model->is_like = NoteEnum::STATUS_ONE;
-
- $msg = '点赞';
- $note->likes_num++;
- } else {
- if ($model->is_like == 1) {
- $model->is_like = 0;
- $msg = '取消点赞';
- if ($note->likes_num >= 1) {
- $note->likes_num--;
- }
- } else {
- $model->is_like = 1;
- $msg = '点赞';
- $note->likes_num++;
- }
- }
- $note->save();
- $model->save();
- $transaction->commit();
- return $msg;
- } catch (Exception $e) {
- $transaction->rollBack();
- var_dump($e->getMessage());
- throw new Exception($e->getMessage());
- }
- }
- }
|