Special.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\special;
  12. use app\admin\model\live\LiveGoods;
  13. use app\admin\model\live\LiveStudio;
  14. use app\admin\model\order\StoreOrder;
  15. use app\admin\model\system\RecommendRelation;
  16. use think\cache\driver\Redis;
  17. use traits\ModelTrait;
  18. use basic\ModelBasic;
  19. /**
  20. * Class Special 专题
  21. * @package app\admin\model\special
  22. */
  23. class Special extends ModelBasic
  24. {
  25. use ModelTrait;
  26. protected static function init()
  27. {
  28. self::afterUpdate(function () {
  29. del_redis_hash("wap_index_has","recommend_list");
  30. });
  31. }
  32. public function profile()
  33. {
  34. return $this->hasOne('SpecialContent', 'special_id', 'id')->field('content');
  35. }
  36. public static function PreWhere($alert = '')
  37. {
  38. $alert = $alert ? $alert . '.' : '';
  39. return self::where([$alert . 'is_show' => 1, $alert . 'is_del' => 0]);
  40. }
  41. //动态赋值
  42. public static function getPinkStrarTimeAttr($value)
  43. {
  44. return $value ? date('Y-m-d H:i:s', $value) : '';
  45. }
  46. public static function getPinkEndTimeAttr($value)
  47. {
  48. return $value ? date('Y-m-d H:i:s', $value) : '';
  49. }
  50. public static function getAddTimeAttr($value)
  51. {
  52. return date('Y-m-d H:i:s', $value);
  53. }
  54. public static function getBannerAttr($value)
  55. {
  56. return is_string($value) ? json_decode($value, true) : $value;
  57. }
  58. public static function getLabelAttr($value)
  59. {
  60. return is_string($value) ? json_decode($value, true) : $value;
  61. }
  62. //end
  63. //设置
  64. public static function getBannerKeyAttr($banner)
  65. {
  66. if (is_string($banner)) $banner = json_decode($banner, true);
  67. if ($banner === false) return [];
  68. $value = [];
  69. foreach ($banner as $item) {
  70. $value[] = [
  71. 'is_show' => false,
  72. 'pic' => $item
  73. ];
  74. }
  75. return $value;
  76. }
  77. public static function saveFieldByWhere(array $where, array $data)
  78. {
  79. if (!$where || !$data) return false;
  80. return parent::saveFieldByWhere($where, $data);
  81. }
  82. //获取单个专题
  83. public static function getOne($id, $is_live = false)
  84. {
  85. $special = self::get($id);
  86. if (!$special) return false;
  87. if ($special->is_del) return false;
  88. $special->banner = self::getBannerKeyAttr($special->banner);
  89. $special->profile->content = htmlspecialchars_decode($special->profile->content);
  90. if ($is_live) {
  91. $liveInfo = LiveStudio::where('special_id', $special->id)->find();
  92. if (!$liveInfo) return self::setErrorInfo('暂未查到直播间');
  93. if ($liveInfo->is_del) return self::setErrorInfo('直播间已删除无法编辑');
  94. $liveInfo->live_duration = (strtotime($liveInfo->stop_play_time) - strtotime($liveInfo->start_play_time)) / 60;
  95. $liveInfo = $liveInfo->toArray();
  96. } else
  97. $liveInfo = [];
  98. return [$special->toArray(), $liveInfo];
  99. }
  100. //设置条件
  101. public static function setWhere($where, $alert = '', $model = null)
  102. {
  103. $model = $model === null ? new self() : $model;
  104. if ($alert) $model = $model->alias($alert);
  105. $alert = $alert ? $alert . '.' : '';
  106. if ($where['order'])
  107. $model = $model->order($alert . self::setOrder($where['order']));
  108. else
  109. $model = $model->order($alert.'sort desc,'.$alert.'id desc');
  110. if (isset($where['subject_id']) && $where['subject_id']) $model = $model->where($alert . 'subject_id', $where['subject_id']);
  111. if (isset($where['store_name']) && $where['store_name']!='') $model = $model->where($alert . 'title|' . $alert . 'abstract|' . $alert . 'phrase|'. $alert . 'id', "LIKE", "%$where[store_name]%");
  112. if ($where['is_show'] !== '') $model = $model->where($alert . 'is_show', $where['is_show']);
  113. if (isset($where['type']) && $where['type']) $model = $model->where($alert . 'type', $where['type']);
  114. if (isset($where['special_type']) && $where['special_type'] !== '') {
  115. $model = $model->where($alert . 'type', $where['special_type']);
  116. }
  117. if (isset($where['admin_id']) && $where['admin_id']) $model = $model->where($alert . 'admin_id', $where['admin_id']);
  118. if (isset($where['start_time']) && $where['start_time'] && isset($where['end_time']) && $where['end_time']) $model = $model->whereTime($alert . 'add_time', 'between', [strtotime($where['start_time']), strtotime($where['end_time'])]);
  119. return $model->where($alert .'is_del', 0);
  120. }
  121. /**
  122. * @param $where
  123. */
  124. public static function getPinkList($where)
  125. {
  126. $data = self::setWhere($where, 'A')->field('A.*,T.content,S.name as subject_name')
  127. ->join('__SPECIAL_CONTENT__ T', 'T.special_id=A.id','LEFT')->join('__SPECIAL_SUBJECT__ S', 'S.id=A.subject_id','LEFT')
  128. ->page((int)$where['page'], (int)$where['limit'])->where('A.is_pink',1)->select();
  129. $data = count($data) ? $data->toArray() : [];
  130. foreach ($data as &$item) {
  131. $item['recommend'] = RecommendRelation::where('a.link_id', $item['id'])->where('a.type', 'in', [0, 2])->alias('a')
  132. ->join('__RECOMMEND__ r', 'a.recommend_id=r.id')->column('a.id,r.title');
  133. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  134. $item['sales'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  135. $liveGoods = LiveGoods::getOne(['special_id' => $item['id'], 'is_delete' => 0]);
  136. $item['is_live_goods'] = 0;
  137. $item['live_goods_id'] = 0;
  138. if ($liveGoods) {
  139. $item['live_goods_id'] = $liveGoods->id;
  140. if ($liveGoods->is_show == 1) {
  141. $item['is_live_goods'] = 1;
  142. }
  143. }
  144. //查看拼团状态,如果已结束关闭拼团
  145. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  146. self::update(['is_pink' => 0], ['id' => $item['id']]);
  147. $item['is_pink'] = 0;
  148. }
  149. if(!$item['is_pink']){
  150. $item['pink_money'] = 0;
  151. }
  152. $item['stream_name'] = LiveStudio::where('special_id', $item['id'])->value('stream_name');
  153. $oldTaskCount = SpecialTask::where('special_id', $item['id'])->where('is_show', 1)->count();
  154. $newTaskCount = SpecialSource::where('special_id', $item['id'])->count();
  155. $item['task_count'] = $newTaskCount + $oldTaskCount;
  156. $item['live_id'] = LiveStudio::where('special_id', $item['id'])->value('id');
  157. $item['is_play'] =0;
  158. if ($item['live_id']) {
  159. $item['online_num'] = LiveStudio::where('id', $item['live_id'])->value('online_num');
  160. $item['is_play'] = LiveStudio::where('id', $item['live_id'])->value('is_play') ? 1 : 0;
  161. }
  162. $item['buy_user_num'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id']])->count('id');
  163. $item['start_play_time'] = LiveStudio::where('special_id', $item['id'])->value('start_play_time');
  164. }
  165. $count = self::setWhere($where)->count();
  166. return compact('data', 'count');
  167. }
  168. //查找专题列表
  169. public static function getSpecialList($where)
  170. {
  171. $data = self::setWhere($where, 'A')->field('A.*,T.content,S.name as subject_name')
  172. ->join('__SPECIAL_CONTENT__ T', 'T.special_id=A.id','LEFT')->join('__SPECIAL_SUBJECT__ S', 'S.id=A.subject_id','LEFT')
  173. ->page((int)$where['page'], (int)$where['limit'])->select();
  174. $data = count($data) ? $data->toArray() : [];
  175. foreach ($data as &$item) {
  176. $item['recommend'] = RecommendRelation::where('a.link_id', $item['id'])->where('a.type', 'in', [0, 2])->alias('a')
  177. ->join('__RECOMMEND__ r', 'a.recommend_id=r.id')->column('a.id,r.title');
  178. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  179. $item['sales'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  180. $liveGoods = LiveGoods::getOne(['special_id' => $item['id'], 'is_delete' => 0]);
  181. $item['is_live_goods'] = 0;
  182. $item['live_goods_id'] = 0;
  183. if ($liveGoods) {
  184. $item['live_goods_id'] = $liveGoods->id;
  185. if ($liveGoods->is_show == 1) {
  186. $item['is_live_goods'] = 1;
  187. }
  188. }
  189. //查看拼团状态,如果已结束关闭拼团
  190. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  191. self::update(['is_pink' => 0], ['id' => $item['id']]);
  192. $item['is_pink'] = 0;
  193. }
  194. if(!$item['is_pink']){
  195. $item['pink_money'] = 0;
  196. }
  197. if ($where['type'] == 4) $item['stream_name'] = LiveStudio::where('special_id', $item['id'])->value('stream_name');
  198. $oldTaskCount = SpecialTask::where('special_id', $item['id'])->where('is_show', 1)->count();
  199. $newTaskCount = SpecialSource::where('special_id', $item['id'])->count();
  200. $item['task_count'] = $newTaskCount + $oldTaskCount;
  201. $item['live_id'] = LiveStudio::where('special_id', $item['id'])->value('id');
  202. $item['is_play'] =0;
  203. if ($item['live_id']) {
  204. $item['online_num'] = LiveStudio::where('id', $item['live_id'])->value('online_num');
  205. $item['is_play'] = LiveStudio::where('id', $item['live_id'])->value('is_play') ? 1 : 0;
  206. }
  207. $item['buy_user_num'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id']])->count('id');
  208. $item['start_play_time'] = LiveStudio::where('special_id', $item['id'])->value('start_play_time');
  209. }
  210. $count = self::setWhere($where)->count();
  211. return compact('data', 'count');
  212. }
  213. }