Special.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\wap\model\special;
  12. use app\admin\model\special\SpecialSource;
  13. use app\wap\model\store\StoreOrder;
  14. use app\wap\model\store\StorePink;
  15. use app\wap\model\user\User;
  16. use basic\ModelBasic;
  17. use service\SystemConfigService;
  18. use think\Url;
  19. use traits\ModelTrait;
  20. use think\Db;
  21. class Special extends ModelBasic
  22. {
  23. use ModelTrait;
  24. public function profile()
  25. {
  26. return $this->hasOne('SpecialContent', 'special_id', 'id')->field('content');
  27. }
  28. //动态赋值
  29. public static function getPinkStrarTimeAttr($value)
  30. {
  31. return $value ? date('Y-m-d H:i:s', $value) : '';
  32. }
  33. public static function getPinkEndTimeAttr($value)
  34. {
  35. return $value ? date('Y-m-d H:i:s', $value) : '';
  36. }
  37. public static function getAddTimeAttr($value)
  38. {
  39. return date('Y-m-d H:i:s', $value);
  40. }
  41. public static function getBannerAttr($value)
  42. {
  43. return is_string($value) ? json_decode($value, true) : $value;
  44. }
  45. public static function getLabelAttr($value)
  46. {
  47. return is_string($value) ? json_decode($value, true) : $value;
  48. }
  49. /**
  50. * 设置专题显示条件
  51. * @param string $alias 别名
  52. * @param null $model model
  53. * @param bool $isAL 是否起别名,默认执行
  54. * @return $this
  55. */
  56. public static function PreWhere($alias = '', $model = null, $isAL = false)
  57. {
  58. self::setPinkSpecial();
  59. if (is_null($model)) $model = new self();
  60. if ($alias) {
  61. $isAL || $model = $model->alias($alias);
  62. $alias .= '.';
  63. }
  64. return $model->where(["{$alias}is_del" => 0]);
  65. }
  66. /**
  67. * 获取拼团详情页的专题详情和分享连接
  68. * @param string $order_id 订单id
  69. * @param int $pinkId 当前拼团id
  70. * @param int $uid 当前用户id
  71. * @return array
  72. * */
  73. public static function getPinkSpecialInfo($order_id, $pinkId, $uid)
  74. {
  75. $special = self::PreWhere()->where('id', StoreOrder::where('order_id', $order_id)->value('cart_id'))
  76. ->field(['image', 'title', 'abstract', 'money', 'label', 'id', 'is_pink', 'pink_money'])->find();
  77. if (!$special) return [];
  78. $special['image'] = get_oss_process($special['image'], 4);
  79. $special['link'] = SystemConfigService::get('site_url') . Url::build('special/details') . '?id=' . $special['id'] . '&pinkId=' . $pinkId . '&partake=1#partake';
  80. $special['abstract'] = self::HtmlToMbStr($special['abstract']);
  81. return $special;
  82. }
  83. public static function getPinkList($pink_id)
  84. {
  85. }
  86. /**
  87. * 设置拼团到时间的专题
  88. * */
  89. public static function setPinkSpecial()
  90. {
  91. self::where('pink_strar_time', '<', time())->where('pink_end_time', '<', time())->update([
  92. 'is_pink' => 0,
  93. 'pink_strar_time' => 0,
  94. 'pink_end_time' => 0
  95. ]);
  96. }
  97. /**
  98. * 获取单个专题的详细信息,拼团信息,拼团用户信息
  99. * @param $uid 用户id
  100. * @param $id 专题id
  101. * @param $pinkId 拼团id
  102. * */
  103. public static function getOneSpecial($uid, $id, $pinkId)
  104. {
  105. $special = self::PreWhere()->find($id);
  106. if (!$special) return self::setErrorInfo('您要查看的专题不存在!');
  107. if ($special->is_show==0) return self::setErrorInfo('您要查看的专题已下架!');
  108. self::where('id',$id)->setInc('browse_count');
  109. if($uid){
  110. $time=strtotime('today');
  111. $res=Db::name('learning_records')->where(['uid'=>$uid,'special_id'=>$id,'add_time'=>$time])->find();
  112. if(!$res){
  113. Db::name('learning_records')->insert(['uid'=>$uid,'special_id'=>$id,'add_time'=>$time]);
  114. if($special->lecturer_id){
  115. Lecturer::where('id',$special->lecturer_id)->setInc('study');
  116. }
  117. }
  118. }
  119. $title = $special->title;
  120. $pinkUser = StorePink::getPinkAttend($id);
  121. $pinkUserFase = StorePink::getPinkAttendFalse($id);
  122. $pinkUser = array_merge($pinkUser, $pinkUserFase);
  123. $pinkIngList = StorePink::getPinkAll($id, $pinkId, 3);
  124. foreach ($pinkIngList as &$item) {
  125. $item['difftime'] = [];
  126. $pinkAll = StorePink::getPinkMember($item['k_id'] ? $item['k_id'] : $item['id']);
  127. $pinkAll = StorePink::getPinkTFalseList($pinkAll, $item['k_id'] ? $item['k_id'] : $item['id'], $id);
  128. $pinkAllCount = count($pinkAll);
  129. $pinkT = $item['k_id'] ? StorePink::getPinkUserOne($item['k_id']) : $item;
  130. $item['num'] = bcsub($pinkT['people'],bcadd($pinkAllCount , 1,0),0);
  131. }
  132. $special->fake_sales += StoreOrder::where(['paid' => 1, 'cart_id' => $id, 'refund_status' => 0,'type'=>0,'combination_id'=>$id])->count();
  133. $special->collect = self::getDb('special_relation')->where(['link_id' => $id, 'type' => 0, 'uid' => $uid, 'category' => 1])->count() ? true : false;
  134. $special->content = htmlspecialchars_decode($special->profile->content);
  135. $special->profile->content = '';
  136. $swiperlist = json_encode($special->banner);
  137. $special = json_encode($special->toArray());
  138. $pinkUser = json_encode($pinkUser);
  139. $pinkIngList = json_encode($pinkIngList);
  140. return compact('swiperlist', 'special', 'title', 'pinkUser', 'pinkIngList');
  141. }
  142. /**
  143. * 我的课程和我的收藏
  144. * @param int $type 1=收藏,0=我的购买
  145. * @param int $page 页码
  146. * @param int $limit 每页显示条数
  147. * @param int $uid 用户uid
  148. * @return array
  149. * */
  150. public static function getGradeList($type, $page, $limit, $uid, $search = '')
  151. {
  152. if ($type)
  153. $list = self::PreWhere('a')->where('s.uid', $uid)->order('a.sort desc')->join('__SPECIAL_RELATION__ s', 'a.id=s.link_id')->field('a.*')->page($page, $limit)->select();
  154. else {
  155. $model = self::PreWhere('s', SpecialBuy::where('s.uid', $uid)->where('s.is_del', 0)->order('a.sort desc,s.add_time desc')->alias('s'), true)->join('__SPECIAL__ a', 'a.id=s.special_id');
  156. if ($search) {
  157. $model = $model->where('a.title|a.abstract', 'like', "%$search%");
  158. }
  159. $list = $model->field('a.*,a.type as types,s.*')->page($page, $limit)->select();
  160. }
  161. $list=count($list)>0 ? $list->toArray() :[];
  162. foreach ($list as &$item) {
  163. $item['image'] = get_oss_process($item['image'], 4);
  164. if (is_string($item['label'])) $item['label'] = json_decode($item['label'], true);
  165. $item['subject_name'] = SpecialSubject::where('id', $item['subject_id'])->value('name');
  166. if($type) $id=$item['id'];
  167. else $id=$item['special_id'];
  168. $item['s_id'] =$id;
  169. $specialSourceId = SpecialSource::getSpecialSource($id);
  170. if($specialSourceId) $item['count']=count($specialSourceId);
  171. else $item['count']=0;
  172. }
  173. $page += 1;
  174. return compact('list', 'page');
  175. }
  176. /**
  177. * 获取某个专题的详细信息
  178. * @param int $id 专题id
  179. * @return array
  180. * */
  181. public static function getSpecialInfo($id)
  182. {
  183. $special = self::PreWhere()->find($id);
  184. if (!$special) return self::setErrorInfo('没有找到此专题');
  185. $special->abstract = self::HtmlToMbStr($special->abstract);
  186. return $special->toArray();
  187. }
  188. /**
  189. * 获取推广专题列表
  190. * @param array $where 查询条件
  191. * @param int $uid 用户uid
  192. * @return array
  193. * */
  194. public static function getSpecialSpread($where, $uid)
  195. {
  196. $is_promoter = User::where('uid', $uid)->value('is_promoter');
  197. $store_brokerage_ratio = SystemConfigService::get('store_brokerage_ratio');
  198. $store_brokerage_two = SystemConfigService::get('store_brokerage_two');
  199. $store_brokerage_ratio = bcdiv($store_brokerage_ratio, 100, 2);
  200. $store_brokerage_two = bcdiv($store_brokerage_two, 100, 2);
  201. $ids = SpecialSubject::where('a.is_show', 1)->alias('a')->join('__SPECIAL__ s', 's.subject_id=a.id')->column('a.id');
  202. $subjectIds = [];
  203. foreach ($ids as $item) {
  204. if (self::PreWhere()->where('is_show', 1)->where('subject_id', $item)->count()) array_push($subjectIds, $item);
  205. }
  206. $model = SpecialSubject::where('is_show', 1)->order('sort desc')->field('id,name');
  207. if ($where['grade_id']) $model = $model->where('grade_id', $where['grade_id']);
  208. $list = $model->where('id', 'in', $subjectIds)->page((int)$where['page'], (int)$where['limit'])->select();
  209. $data = count($list) ? $list->toArray() : [];
  210. foreach ($data as &$item) {
  211. $item['list'] = self::PreWhere()->where('is_show', 1)->where('subject_id', $item['id'])->field([
  212. 'image', 'id', 'title', 'money'
  213. ])->order('sort desc')->select();
  214. if (count($item['list'])) $item['list'] = $item['list']->toArray();
  215. foreach ($item['list'] as &$value) {
  216. $value['image'] = get_oss_process($value['image'], 4);
  217. $value['spread_money'] = bcmul($value['money'], $store_brokerage_ratio, 2);
  218. }
  219. }
  220. $page = (int)$where['page'] + 1;
  221. return compact('data', 'page');
  222. }
  223. /**
  224. * 设置查询条件
  225. * @param $where
  226. * @return $this
  227. */
  228. public static function setWhere($where)
  229. {
  230. if ($where['type']) {
  231. $model = self::PreWhere('a');
  232. if ($where['subject_id']) {
  233. $model = $model->where('a.subject_id', $where['subject_id']);
  234. }
  235. if ($where['search']) {
  236. $model = $model->where('a.title|a.abstract', 'LIKE', "%$where[search]%");
  237. }
  238. return $model->order('a.sort desc,a.id desc')->where('a.is_show', 1)
  239. ->join('special_record r', 'r.special_id = a.id')
  240. ->group('a.id')->where('uid', $where['uid']);
  241. } else {
  242. $model = self::PreWhere();
  243. if ($where['subject_id']) {
  244. $model = $model->where('subject_id', $where['subject_id']);
  245. }
  246. if ($where['search']) {
  247. $model = $model->where('title|abstract', 'LIKE', "%$where[search]%");
  248. }
  249. return $model->where('is_show', 1)->order('sort desc,id desc');
  250. }
  251. }
  252. /**
  253. * 获取专题列表
  254. * @param $where
  255. * @return mixed
  256. */
  257. public static function getSpecialList($where)
  258. {
  259. if ($where['type']) {
  260. $alias = 'a.';
  261. $field = [$alias . 'id', $alias . 'browse_count', $alias . 'image', $alias . 'title',$alias . 'type', $alias . 'money', $alias . 'pink_money', $alias . 'is_pink', $alias . 'subject_id', $alias . 'label', 'r.number'];
  262. } else {
  263. $field = ['browse_count', 'image', 'title','type', 'money', 'pink_money', 'is_pink', 'subject_id', 'label', 'id'];
  264. }
  265. $list = self::setWhere($where)
  266. ->field($field)
  267. ->page($where['page'], $where['limit'])
  268. ->select();
  269. $list = count($list) ? $list->toArray() : [];
  270. foreach ($list as &$item) {
  271. $specialSourceId = SpecialSource::getSpecialSource($item['id']);
  272. if($specialSourceId) $item['count']=count($specialSourceId);
  273. else $item['count']=0;
  274. $item['subject_name'] = SpecialSubject::where('id', $item['subject_id'])->value('name');
  275. }
  276. return $list;
  277. }
  278. /**讲师名下课程
  279. * @return array
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. * @throws \think\exception\DbException
  283. */
  284. public static function getLecturerSpecialList($lecturer_id=0,$page=1,$limit=10)
  285. {
  286. $field = ['browse_count', 'image', 'title','type', 'money', 'pink_money', 'is_pink', 'subject_id', 'label', 'id','is_show','is_del','lecturer_id'];
  287. $model = self::PreWhere();
  288. $model = $model->where(['is_show'=>1,'lecturer_id'=>$lecturer_id])->order('sort desc,id desc');
  289. $list =$model->field($field)
  290. ->page($page, $limit)
  291. ->select();
  292. $list = count($list) ? $list->toArray() : [];
  293. foreach ($list as &$item) {
  294. $specialSourceId = SpecialSource::getSpecialSource($item['id']);
  295. if($specialSourceId) $item['count']=count($specialSourceId);
  296. else $item['count']=0;
  297. $item['subject_name'] = SpecialSubject::where('id', $item['subject_id'])->value('name');
  298. }
  299. return $list;
  300. }
  301. /**拼团专题
  302. * @param int $page
  303. * @param int $limit
  304. */
  305. public static function getPinkSpecialList($page=1,$limit=10)
  306. {
  307. $field = ['browse_count', 'image', 'title','type', 'money', 'pink_money', 'is_pink', 'subject_id', 'label', 'id','is_show','is_del','lecturer_id'];
  308. $model = self::PreWhere();
  309. $model = $model->where(['is_show'=>1,'is_pink'=>1])->order('sort desc,id desc');
  310. $list =$model->field($field)
  311. ->page($page, $limit)
  312. ->select();
  313. $list = count($list) ? $list->toArray() : [];
  314. foreach ($list as &$item) {
  315. $specialSourceId = SpecialSource::getSpecialSource($item['id']);
  316. if($specialSourceId) $item['count']=count($specialSourceId);
  317. else $item['count']=0;
  318. $item['subject_name'] = SpecialSubject::where('id', $item['subject_id'])->value('name');
  319. }
  320. return $list;
  321. }
  322. }