| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace addons\Xhs\common\models;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_addons_xhs_comment".
- *
- * @property int $id ID
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $content_id 评论的种草 id
- * @property string|null $content 评论内容,保存的时候使用 json_encode 编码,拿出来的时候需要 json_decode 解码,防止评论内容中夹带表情时出错
- * @property int $score 评分
- * @property int $is_anonymous 是否匿名,1:是,0:不是
- * @property int $status -1是删除,0是禁用 1是启用
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- * @property int $is_top 是否置顶0.否|1.是
- */
- class AddonsXhsComment extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_xhs_comment}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'content_id'], 'required'],
- [['mall_id', 'user_id', 'content_id', 'score', 'is_anonymous', 'status', 'created_at', 'updated_at','is_top'], 'integer'],
- [['content'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'user_id' => '用户id',
- 'content_id' => '评论的种草 id',
- 'content' => '评论内容,保存的时候使用 json_encode 编码,拿出来的时候需要 json_decode 解码,防止评论内容中夹带表情时出错',
- 'score' => '评分',
- 'is_anonymous' => '是否匿名,1:是,0:不是',
- 'status' => '-1是删除,0是禁用 1是启用',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'is_top' => '是否置顶0.否|1.是',
- ];
- }
- 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']]]]);
- $content_id_arr = array_column($comment_list, 'content_id');
- $list = self::lists(['where' => [['content_id' => $content_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'count(*) nums,content_id', 'group' => 'content_id', 'index' => 'content_id']);
- $content_list = AddonsXhsContent::lists(['where' => [['id' => $content_id_arr]]], false);
- foreach ($content_list as $content) {
- $content->discuss_count = $list[$content['id']]['nums']??0;
- $content->save();
- }
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2021/12/13 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AddonsMaterial|bool
- */
- public static function setData(array $params){
- try{
- if (!empty($params['id'])) {
- $model = self::findOne(['and',['id'=>$params['id'],'mall_id'=>$params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if (empty($model)) throw new Exception('记录不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return $model;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|