AddonsOperatingNoteUserLike.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace addons\OperatingNote\common\models;
  3. use addons\OperatingNote\common\enums\NoteEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_operating_note_user_like}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $user_id 用户id
  12. * @property int $note_content_id 点赞笔记id
  13. * @property int $is_like 是否点赞 1/是 0/否
  14. * @property int $status
  15. * @property int $created_at
  16. * @property int $updated_at
  17. */
  18. class AddonsOperatingNoteUserLike extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_operating_note_user_like}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'user_id', 'note_content_id', 'is_like'], 'required'],
  34. [['mall_id', 'user_id', 'note_content_id', 'is_like', 'status', 'created_at', 'updated_at'], 'integer'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => '商城ID',
  45. 'user_id' => '用户id',
  46. 'note_content_id' => '点赞笔记id',
  47. 'is_like' => '是否点赞 1/是 0/否',
  48. 'status' => 'Status',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. ];
  52. }
  53. public static function setData($id, $user_id, $mall_id)
  54. {
  55. $transaction = \Yii::$app->db->beginTransaction();
  56. try {
  57. $msg = '';
  58. $model = self::getOne([['user_id' => $user_id, 'note_content_id' => $id]]);
  59. $note = AddonsOperatingNoteContent::getOne([['id' => $id]]);
  60. if (!$model) {
  61. $model = new self();
  62. $model->user_id = $user_id;
  63. $model->mall_id = $mall_id;
  64. $model->note_content_id = $id;
  65. $model->is_like = NoteEnum::STATUS_ONE;
  66. $msg = '点赞';
  67. $note->likes_num++;
  68. } else {
  69. if ($model->is_like == 1) {
  70. $model->is_like = 0;
  71. $msg = '取消点赞';
  72. if ($note->likes_num >= 1) {
  73. $note->likes_num--;
  74. }
  75. } else {
  76. $model->is_like = 1;
  77. $msg = '点赞';
  78. $note->likes_num++;
  79. }
  80. }
  81. $note->save();
  82. $model->save();
  83. $transaction->commit();
  84. return $msg;
  85. } catch (Exception $e) {
  86. $transaction->rollBack();
  87. var_dump($e->getMessage());
  88. throw new Exception($e->getMessage());
  89. }
  90. }
  91. }