AddonsXhsComment.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace addons\Xhs\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "qimall_addons_xhs_comment".
  9. *
  10. * @property int $id ID
  11. * @property int $mall_id 商城id
  12. * @property int $user_id 用户id
  13. * @property int $content_id 评论的种草 id
  14. * @property string|null $content 评论内容,保存的时候使用 json_encode 编码,拿出来的时候需要 json_decode 解码,防止评论内容中夹带表情时出错
  15. * @property int $score 评分
  16. * @property int $is_anonymous 是否匿名,1:是,0:不是
  17. * @property int $status -1是删除,0是禁用 1是启用
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 更新时间
  20. * @property int $is_top 是否置顶0.否|1.是
  21. */
  22. class AddonsXhsComment extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_xhs_comment}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'user_id', 'content_id'], 'required'],
  38. [['mall_id', 'user_id', 'content_id', 'score', 'is_anonymous', 'status', 'created_at', 'updated_at','is_top'], 'integer'],
  39. [['content'], 'string'],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => '商城id',
  50. 'user_id' => '用户id',
  51. 'content_id' => '评论的种草 id',
  52. 'content' => '评论内容,保存的时候使用 json_encode 编码,拿出来的时候需要 json_decode 解码,防止评论内容中夹带表情时出错',
  53. 'score' => '评分',
  54. 'is_anonymous' => '是否匿名,1:是,0:不是',
  55. 'status' => '-1是删除,0是禁用 1是启用',
  56. 'created_at' => '创建时间',
  57. 'updated_at' => '更新时间',
  58. 'is_top' => '是否置顶0.否|1.是',
  59. ];
  60. }
  61. public function getUser()
  62. {
  63. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  64. }
  65. public static function updateData($params)
  66. {
  67. $t = Yii::$app->db->beginTransaction();
  68. try {
  69. if (empty($params['attributes']) || !is_array($params['attributes'])) throw new \Exception('参数有误!');
  70. $attributes = $params['attributes'];
  71. $condition = ['id' => $params['id'], 'mall_id' => $params['mall_id']];
  72. self::updateAll($attributes, $condition);
  73. $comment_list = self::lists(['where' => [['id' => $params['id']]]]);
  74. $content_id_arr = array_column($comment_list, 'content_id');
  75. $list = self::lists(['where' => [['content_id' => $content_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'count(*) nums,content_id', 'group' => 'content_id', 'index' => 'content_id']);
  76. $content_list = AddonsXhsContent::lists(['where' => [['id' => $content_id_arr]]], false);
  77. foreach ($content_list as $content) {
  78. $content->discuss_count = $list[$content['id']]['nums']??0;
  79. $content->save();
  80. }
  81. $t->commit();
  82. return true;
  83. } catch (\Exception $e) {
  84. $t->rollBack();
  85. self::setStaticError($e->getMessage());
  86. return false;
  87. }
  88. }
  89. /**
  90. * 保存数据
  91. * @Author: ltm
  92. * @DateTime: 2021/12/13 0022 17:13
  93. * @Copyright: copyright (c) 2021 广东七件事集团
  94. * @param array $params
  95. * @return AddonsMaterial|bool
  96. */
  97. public static function setData(array $params){
  98. try{
  99. if (!empty($params['id'])) {
  100. $model = self::findOne(['and',['id'=>$params['id'],'mall_id'=>$params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  101. if (empty($model)) throw new Exception('记录不存在或已删除');
  102. }else{
  103. $model = new self();
  104. $model->loadDefaultValues();
  105. $model->mall_id = $params['mall_id'];
  106. }
  107. if(!$model->load($params,'') || !$model->save()){
  108. throw new Exception($model->getErrorMessage());
  109. }
  110. return $model;
  111. }catch(\Exception $e){
  112. self::$static_error = $e->getMessage();
  113. return false;
  114. }
  115. }
  116. }