AddonsXhsContent.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace addons\Xhs\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\goods\Goods;
  5. use common\models\user\User;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "qimall_addons_xhs_content".
  10. *
  11. * @property int $id ID
  12. * @property int $mall_id 商城ID
  13. * @property int $user_id 用户ID
  14. * @property int $goods_id 商品ID
  15. * @property int $cate_id 内容所属的分类的id
  16. * @property int $views_count 浏览数
  17. * @property int $stars_count 点赞数
  18. * @property int $shares_count 被分享数
  19. * @property int $discuss_count 评论数
  20. * @property int|null $order_count 订单数
  21. * @property int|null $goods_views_count 商品浏览数量
  22. * @property int $content_type 内容类型,1:图片+文字,2:视频+文字
  23. * @property int $check_status 内容状态,1:在审核中,2:审核通过,3:审核未通过
  24. * @property int|null $check_time 审核是时间
  25. * @property string|null $check_remark 审核原因
  26. * @property string $title 标题
  27. * @property string $main_text 正文
  28. * @property int $time_length 视频时长,单位 秒,如果是图文,时长为 0
  29. * @property string $source 此字段保存资源相关的数据,即图片或者视频的保存路径。数据保存的结构形式如,[{url: "/path/to/11.mp4", front_cover: "/path/to/11.jpg"}].其中 url 的值是图片或者视频的保存路径, front_cover 是封面图片的保存路径
  30. * @property int|null $status 状态,-1是删除,0是禁用,1是启用
  31. * @property int $created_at 创建时间
  32. * @property int $updated_at 更新ID
  33. * @property int $video_wide 视频宽
  34. * @property int $video_long 视频长
  35. * @property int $is_top 是否置顶0.否|1.是
  36. */
  37. class AddonsXhsContent extends \common\models\base\BaseActiveRecord
  38. {
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%addons_xhs_content}}';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['mall_id', 'user_id', 'cate_id', 'main_text', 'source'], 'required'],
  53. [['mall_id', 'user_id', 'goods_id', 'cate_id', 'views_count', 'stars_count', 'shares_count', 'discuss_count', 'order_count', 'goods_views_count', 'content_type', 'check_status', 'check_time', 'time_length', 'status', 'created_at', 'updated_at', 'video_wide', 'video_long', 'is_top'], 'integer'],
  54. [['main_text', 'source'], 'string'],
  55. [['check_remark'], 'string', 'max' => 255],
  56. [['title'], 'string', 'max' => 50],
  57. ];
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. 'mall_id' => '商城ID',
  67. 'user_id' => '用户ID',
  68. 'goods_id' => '商品ID',
  69. 'cate_id' => '内容所属的分类的id',
  70. 'views_count' => '浏览数',
  71. 'stars_count' => '点赞数',
  72. 'shares_count' => '被分享数',
  73. 'discuss_count' => '评论数',
  74. 'order_count' => '订单数',
  75. 'goods_views_count' => '商品浏览数量',
  76. 'content_type' => '内容类型,1:图片+文字,2:视频+文字',
  77. 'check_status' => '内容状态,1:在审核中,2:审核通过,3:审核未通过',
  78. 'check_time' => '审核是时间',
  79. 'check_remark' => '审核原因',
  80. 'title' => '标题',
  81. 'main_text' => '正文',
  82. 'time_length' => '视频时长,单位 秒,如果是图文,时长为 0',
  83. 'source' => '此字段保存资源相关的数据,即图片或者视频的保存路径。数据保存的结构形式如,[{url: \"/path/to/11.mp4\", front_cover: \"/path/to/11.jpg\"}].其中 url 的值是图片或者视频的保存路径, front_cover 是封面图片的保存路径',
  84. 'status' => '状态,-1是删除,0是禁用,1是启用',
  85. 'created_at' => '创建时间',
  86. 'updated_at' => '更新ID',
  87. 'video_wide' => '视频宽',
  88. 'video_long' => '视频长',
  89. 'is_top' => '是否置顶0.否|1.是',
  90. ];
  91. }
  92. public function getUser()
  93. {
  94. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  95. }
  96. public function getLike()
  97. {
  98. return $this->hasOne(AddonsXhsUserLike::class, ['content_id' => 'id'])->select('id,is_likes,content_id,user_id');
  99. }
  100. //管理商品
  101. public function getGoods()
  102. {
  103. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,cover_pic,sales_num,virtual_sales');
  104. }
  105. //管理商品
  106. public function getGoodsstatistics()
  107. {
  108. return $this->hasOne(AddonsXhsGoodsStatistics::class, ['goods_id' => 'goods_id'])->select('id,total_amount,order_num,goods_id,bought_count');
  109. }
  110. /**
  111. * 获取分类
  112. * @Author: ltm
  113. * @DateTime: 2021/12/13 0022 17:13
  114. * @Copyright: copyright (c) 2021 广东七件事集团
  115. * @param array $params
  116. * @return AddonsMaterial|bool
  117. */
  118. public function getCate(){
  119. return $this->hasOne(AddonsXhsCate::class, ['id' => 'cate_id'])->andWhere(['<>','status',StatusEnum::DELETE]);
  120. }
  121. /**
  122. * 保存数据
  123. * @Author: ltm
  124. * @DateTime: 2021/12/13 0022 17:13
  125. * @Copyright: copyright (c) 2021 广东七件事集团
  126. * @param array $params
  127. * @return string
  128. */
  129. public static function setData(array $params){
  130. try{
  131. if (!empty($params['id'])) {
  132. $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
  133. if (empty($model)) throw new Exception('记录不存在或已删除');
  134. }else{
  135. $model = new self();
  136. $model->loadDefaultValues();
  137. }
  138. $model->mall_id = $params['mall_id'];
  139. if(!$model->load($params,'') || !$model->save()){
  140. throw new Exception($model->getErrorMessage());
  141. }
  142. return $model;
  143. }catch(\Exception $e){
  144. self::$static_error = $e->getMessage();
  145. return false;
  146. }
  147. }
  148. /**
  149. *
  150. */
  151. public static function handle($mall_id,$id,$type,$user_id){
  152. $find_data = AddonsXhsContent::findOne(['id' => $id]);
  153. $params['id'] = $id;
  154. $params['mall_id'] = $mall_id;
  155. switch ($type){
  156. case 'views';
  157. $params['views_count'] = $find_data ['views_count']+ 1;
  158. $statistics_data['views_count'] = 1;
  159. break;
  160. case 'stars';
  161. $res = self::likeLog($mall_id,$id,$user_id);
  162. if($res == StatusEnum::ENABLED) { //点赞
  163. $params['stars_count'] = $find_data ['stars_count'] + 1;
  164. $statistics_data['stars_count'] = 1;
  165. }else { //取消点赞
  166. $params['stars_count'] = $find_data ['stars_count'] - 1;
  167. $statistics_data['stars_count'] = "-1";
  168. }
  169. break;
  170. case 'shares';
  171. $params['shares_count'] = $find_data ['shares_count']+ 1;
  172. $statistics_data['shares_count'] = 1;
  173. break;
  174. case 'goods_views';
  175. $params['goods_views_count'] = $find_data ['goods_views_count']+ 1;
  176. if($find_data ['goods_id']){
  177. $statistics_data['goods_views_count'] = 1;
  178. self::goodsViewsLog($mall_id,$id,$user_id,$find_data['goods_id']);
  179. }
  180. break;
  181. case 'discuss';//评论
  182. $params['discuss_count'] = $find_data ['discuss_count']+ 1;
  183. $statistics_data['discuss_count'] = 1;
  184. break;
  185. case 'push_count'; //发布信息
  186. $statistics_data['push_count'] = 1;
  187. }
  188. $statistics_data['mall_id'] = $mall_id;
  189. if($id){
  190. if(!$find_data->load($params,'') || !$find_data->save()){
  191. throw new Exception($find_data->getErrorMessage());
  192. }
  193. }
  194. AddonsXhsStatistics::setData($statistics_data);
  195. AddonsXhsAllStatistics::setData($statistics_data);
  196. return true;
  197. }
  198. //添加商品访问记录
  199. public static function goodsViewsLog($mall_id,$id,$user_id,$goods_id){
  200. $like_data['mall_id'] = $mall_id;
  201. $like_data['content_id'] = $id;
  202. $like_data['user_id'] = $user_id;
  203. $like_data['goods_id'] = $goods_id;
  204. AddonsXhsGoodsVisit::setData($like_data);
  205. $goods_data['goods_id'] = $goods_id;
  206. $goods_data['goods_views_count'] = 1;
  207. $goods_data['mall_id'] = $mall_id;
  208. AddonsXhsGoodsStatistics::setData($goods_data);
  209. }
  210. //用户喜欢记录
  211. public static function likeLog($mall_id,$id,$user_id){
  212. $like_data['mall_id'] = $mall_id;
  213. $like_data['content_id'] = $id;
  214. $like_data['user_id'] = $user_id;
  215. return AddonsXhsUserLike::setData($like_data);
  216. }
  217. }