| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace addons\OperatingNote\common\models;
- use addons\OperatingNote\common\enums\NoteEnum;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_operating_note_comment}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $note_content_id 回复的笔记id
- * @property int $level 第n级回复
- * @property int $reply_id 一级评论id
- * @property int $content 评论内容
- * @property int $is_top 是否置顶
- * @property int $top_time 置顶时间
- * @property int $status -1是删除,0是禁用 1是启用
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsOperatingNoteComment extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operating_note_comment}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'note_content_id'], 'required'],
- [['mall_id', 'user_id', 'note_content_id', 'level', 'reply_id', 'is_top', 'status', 'created_at', 'updated_at','top_time'], 'integer'],
- [['content'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'user_id' => '用户id',
- 'note_content_id' => '回复的笔记id',
- 'level' => '第n级回复',
- 'reply_id' => '一级评论id',
- 'content' => '评论内容',
- 'is_top' => '是否置顶',
- 'top_time' => '置顶时间',
- 'status' => '-1是删除,0是禁用 1是启用',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
- }
- public static function updateData($params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- if (empty($params['attributes']) || !is_array($params['attributes'])) throw new \Exception('参数有误!');
- $attributes = $params['attributes'];
- $condition = ['id' => $params['id'], 'mall_id' => $params['mall_id']];
- self::updateAll($attributes, $condition);
- $comment_list = self::lists(['where' => [['id' => $params['id']]]]);
- $note_content_id_arr = array_column($comment_list, 'note_content_id');
- $list = self::lists(['where' => [['note_content_id' => $note_content_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'count(*) nums,note_content_id', 'group' => 'note_content_id', 'index' => 'note_content_id']);
- $note_list = AddonsOperatingNoteContent::lists(['where' => [['id' => $note_content_id_arr]]], false);
- foreach ($note_list as $note) {
- $note->reply_num = $list[$note['id']]['nums']??0;
- $note->save();
- }
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- public static function setData($params)
- {
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- $content = AddonsOperatingNoteContent::getOne([['id'=>$params['note_content_id']]]);
- if($content->hidden_comment == 0){
- return false;
- }
- $params['level'] = 1;
- if (isset($params['reply_id']) && $params['reply_id'] > 0) {
- $params['level'] = 2;
- }
- $model = new self();
- if (!$model->load($params, '') || !$model->save()) {
- throw new Exception($model->getErrorMessage());
- }
- $content->reply_num++;
- $content->save();
- $transaction->commit();
- return $model->toArray();
- } catch (Exception $e) {
- $transaction->rollBack();
- var_dump($e->getMessage());
- throw new Exception($e->getMessage());
- }
- }
- public static function editComment($params)
- {
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- $detail = self::getOne([['id' => $params['note_comment_id']]]);
- if ($params['type'] == 1) {
- if ($detail->is_top == NoteEnum::STATUS_ONE) {
- $detail->is_top = NoteEnum::STATUS_ZERO;
- $detail->top_time = 0;
- } else {
- $detail->is_top = NoteEnum::STATUS_ONE;
- $detail->top_time = time();
- }
- } else {
- $detail->status = StatusEnum::DELETE;
- }
- $detail->save();
- $transaction->commit();
- return true;
- } catch (Exception $e) {
- $transaction->rollBack();
- var_dump($e->getMessage());
- throw new Exception($e->getMessage());
- }
- }
- }
|