Special.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\special;
  12. use app\merchant\model\live\LiveGoods;
  13. use app\merchant\model\live\LiveStudio;
  14. use app\merchant\model\order\StoreOrder;
  15. use app\merchant\model\special\SpecialSource;
  16. use think\cache\driver\Redis;
  17. use traits\ModelTrait;
  18. use basic\ModelBasic;
  19. use app\merchant\model\special\SpecialBuy;
  20. /**
  21. * Class Special 专题
  22. * @package app\merchant\model\special
  23. */
  24. class Special extends ModelBasic
  25. {
  26. use ModelTrait;
  27. protected static function init()
  28. {
  29. self::afterUpdate(function () {
  30. $subjectUrl = getUrlToDomain();
  31. del_redis_hash($subjectUrl . "wap_index_has", "recommend_list");
  32. del_redis_hash($subjectUrl . "web_index_has", "recommend_list");
  33. });
  34. }
  35. public function profile()
  36. {
  37. return $this->hasOne('SpecialContent', 'special_id', 'id')->field('content');
  38. }
  39. public function singleProfile()
  40. {
  41. return $this->hasOne('SpecialContent', 'special_id', 'id')->field('is_try,try_content,try_time,link,videoId,file_name,file_type,content');
  42. }
  43. public static function PreWhere($alert = '')
  44. {
  45. $alert = $alert ? $alert . '.' : '';
  46. return self::where([$alert . 'is_show' => 1, $alert . 'is_del' => 0]);
  47. }
  48. //动态赋值
  49. public static function getPinkStrarTimeAttr($value)
  50. {
  51. return $value ? date('Y-m-d H:i:s', $value) : '';
  52. }
  53. public static function getPinkEndTimeAttr($value)
  54. {
  55. return $value ? date('Y-m-d H:i:s', $value) : '';
  56. }
  57. public static function getAddTimeAttr($value)
  58. {
  59. return date('Y-m-d H:i:s', $value);
  60. }
  61. public static function getBannerAttr($value)
  62. {
  63. return is_string($value) ? json_decode($value, true) : $value;
  64. }
  65. public static function getLabelAttr($value)
  66. {
  67. return is_string($value) ? json_decode($value, true) : $value;
  68. }
  69. //end
  70. //设置
  71. public static function getBannerKeyAttr($banner)
  72. {
  73. if (is_string($banner)) $banner = json_decode($banner, true);
  74. if ($banner === false) return [];
  75. $value = [];
  76. foreach ($banner as $item) {
  77. $value[] = [
  78. 'is_show' => false,
  79. 'pic' => $item
  80. ];
  81. }
  82. return $value;
  83. }
  84. //获取单个专题
  85. public static function getOne($id, $is_live = false)
  86. {
  87. $special = self::get($id);
  88. if (!$special) return false;
  89. if ($special->is_del) return false;
  90. $special->banner = self::getBannerKeyAttr($special->banner);
  91. $special->profile->content = htmlspecialchars_decode($special->profile->content);
  92. if ($is_live) {
  93. $liveInfo = LiveStudio::where('special_id', $special->id)->find();
  94. if (!$liveInfo) return self::setErrorInfo('暂未查到直播间');
  95. if ($liveInfo->is_del) return self::setErrorInfo('直播间已删除无法编辑');
  96. $liveInfo->live_duration = (strtotime($liveInfo->stop_play_time) - strtotime($liveInfo->start_play_time)) / 60;
  97. $liveInfo = $liveInfo->toArray();
  98. } else
  99. $liveInfo = [];
  100. return [$special->toArray(), $liveInfo];
  101. }
  102. //获取单个轻专题
  103. public static function getsingleOne($id)
  104. {
  105. $special = self::get($id);
  106. if (!$special) return false;
  107. if ($special->is_del) return false;
  108. $special->singleProfile->content = htmlspecialchars_decode($special->singleProfile->content);
  109. return $special->toArray();
  110. }
  111. //设置条件
  112. public static function setWhere($where, $alert = '', $model = null)
  113. {
  114. $model = $model === null ? new self() : $model;
  115. if ($alert) $model = $model->alias($alert);
  116. $alert = $alert ? $alert . '.' : '';
  117. if (isset($where['order']) && $where['order'])
  118. $model = $model->order($alert . self::setOrder($where['order']));
  119. else
  120. $model = $model->order($alert . 'sort desc,' . $alert . 'id desc');
  121. if (isset($where['subject_id']) && $where['subject_id']) $model = $model->where($alert . 'subject_id', $where['subject_id']);
  122. if (isset($where['store_name']) && $where['store_name'] != '') $model = $model->where($alert . 'title|' . $alert . 'abstract|' . $alert . 'phrase|' . $alert . 'id', "LIKE", "%$where[store_name]%");
  123. if (isset($where['is_show']) && $where['is_show'] !== '') $model = $model->where($alert . 'is_show', $where['is_show']);
  124. if (isset($where['type']) && $where['type'] && $where['type'] < 7) {
  125. $model = $model->where($alert . 'type', $where['type']);
  126. } else if (isset($where['type']) && $where['type'] && $where['type'] == 7) {
  127. $where['is_light'] = 1;
  128. }
  129. if (isset($where['status']) && $where['status'] != '') {
  130. $model = $model->where($alert . 'status', $where['status']);
  131. } else {
  132. $model = $model->where($alert . 'status', 'in', [1, -1, 0]);
  133. }
  134. if (isset($where['is_light']) && $where['is_light']) $model = $model->where($alert . 'is_light', $where['is_light']);
  135. if (isset($where['special_type']) && $where['special_type'] !== '') {
  136. $model = $model->where($alert . 'type', $where['special_type']);
  137. }
  138. if (isset($where['mer_id']) && $where['mer_id']) $model = $model->where($alert . 'mer_id', $where['mer_id']);
  139. 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'])]);
  140. return $model->where($alert . 'is_del', 0);
  141. }
  142. /**拼团专题列表
  143. * @param $where
  144. */
  145. public static function getPinkList($where)
  146. {
  147. $data = self::setWhere($where, 'A')->field('A.*,S.name as subject_name')
  148. ->join('__SPECIAL_SUBJECT__ S', 'S.id=A.subject_id', 'LEFT')
  149. ->page((int)$where['page'], (int)$where['limit'])->where('A.is_pink', 1)->select();
  150. $data = count($data) ? $data->toArray() : [];
  151. foreach ($data as &$item) {
  152. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  153. $item['sales'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  154. $liveGoods = LiveGoods::getOne(['special_id' => $item['id'], 'is_delete' => 0]);
  155. $item['is_live_goods'] = 0;
  156. $item['live_goods_id'] = 0;
  157. if ($liveGoods) {
  158. $item['live_goods_id'] = $liveGoods->id;
  159. if ($liveGoods->is_show == 1) {
  160. $item['is_live_goods'] = 1;
  161. }
  162. }
  163. //查看拼团状态,如果已结束关闭拼团
  164. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  165. self::update(['is_pink' => 0], ['id' => $item['id']]);
  166. $item['is_pink'] = 0;
  167. }
  168. if (!$item['is_pink']) {
  169. $item['pink_money'] = 0;
  170. }
  171. $item['stream_name'] = LiveStudio::where('special_id', $item['id'])->value('stream_name');
  172. $item['live_id'] = LiveStudio::where('special_id', $item['id'])->value('id');
  173. $item['is_play'] = 0;
  174. if ($item['live_id']) {
  175. $item['online_num'] = LiveStudio::where('id', $item['live_id'])->value('online_num');
  176. $item['is_play'] = LiveStudio::where('id', $item['live_id'])->value('is_play') ? 1 : 0;
  177. }
  178. $item['buy_user_num'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id']])->count('id');
  179. $item['start_play_time'] = LiveStudio::where('special_id', $item['id'])->value('start_play_time');
  180. }
  181. $count = self::setWhere($where)->where('is_pink', 1)->count();
  182. return compact('data', 'count');
  183. }
  184. //查找专题列表
  185. public static function getSpecialList($where)
  186. {
  187. $data = self::setWhere($where, 'A')->field('A.*,S.name as subject_name')
  188. ->join('__SPECIAL_SUBJECT__ S', 'S.id=A.subject_id', 'LEFT')
  189. ->page((int)$where['page'], (int)$where['limit'])->select();
  190. $data = count($data) ? $data->toArray() : [];
  191. foreach ($data as &$item) {
  192. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  193. $item['sales'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  194. $liveGoods = LiveGoods::getOne(['special_id' => $item['id'], 'is_delete' => 0]);
  195. if (!$item['quantity']) {
  196. $quantity = SpecialSource::getSpecialSourceCount($item['id']);
  197. $item['quantity'] = $quantity;
  198. $dat['quantity'] = $quantity;
  199. self::edit($dat, $item['id'], 'id');
  200. }
  201. if ($item['quantity'] > 0 && $item['sum'] == 0 || $item['quantity'] > 0 && $item['sum'] > 0 && $item['quantity'] > $item['sum']) {
  202. $item['sum'] = $item['quantity'];
  203. $dat['sum'] = $item['quantity'];
  204. self::edit($dat, $item['id'], 'id');
  205. }
  206. $item['is_live_goods'] = 0;
  207. $item['live_goods_id'] = 0;
  208. if ($liveGoods) {
  209. $item['live_goods_id'] = $liveGoods->id;
  210. if ($liveGoods->is_show == 1) {
  211. $item['is_live_goods'] = 1;
  212. }
  213. }
  214. //查看拼团状态,如果已结束关闭拼团
  215. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  216. self::update(['is_pink' => 0], ['id' => $item['id']]);
  217. $item['is_pink'] = 0;
  218. }
  219. if (!$item['is_pink']) {
  220. $item['pink_money'] = 0;
  221. }
  222. if ($where['type'] == 4) $item['stream_name'] = LiveStudio::where('special_id', $item['id'])->value('stream_name');
  223. $item['live_id'] = LiveStudio::where('special_id', $item['id'])->value('id');
  224. $item['is_play'] = 0;
  225. if ($item['live_id']) {
  226. $item['online_num'] = LiveStudio::where('id', $item['live_id'])->value('online_num');
  227. $item['is_play'] = LiveStudio::where('id', $item['live_id'])->value('is_play') ? 1 : 0;
  228. }
  229. $item['buy_user_num'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id']])->count('id');
  230. $item['start_play_time'] = LiveStudio::where('special_id', $item['id'])->value('start_play_time');
  231. }
  232. $count = self::setWhere($where)->count();
  233. return compact('data', 'count');
  234. }
  235. /**审核条件筛选
  236. * @param $where
  237. * @param string $alert
  238. * @param null $model
  239. * @return Special
  240. */
  241. public static function setExamineWhere($where, $alert = '', $model = null)
  242. {
  243. $model = $model === null ? new self() : $model;
  244. if ($alert) $model = $model->alias($alert);
  245. $alert = $alert ? $alert . '.' : '';
  246. if (isset($where['order']) && $where['order'])
  247. $model = $model->order($alert . self::setOrder($where['order']));
  248. else
  249. $model = $model->order($alert . 'sort desc,' . $alert . 'id desc');
  250. if (isset($where['subject_id']) && $where['subject_id']) $model = $model->where($alert . 'subject_id', $where['subject_id']);
  251. if (isset($where['store_name']) && $where['store_name'] != '') $model = $model->where($alert . 'title|' . $alert . 'abstract|' . $alert . 'phrase|' . $alert . 'id', "LIKE", "%$where[store_name]%");
  252. if (isset($where['type']) && $where['type']) {
  253. $model = $model->where($alert . 'type', $where['type']);
  254. }
  255. if (isset($where['status']) && $where['status'] != '') {
  256. $model = $model->where($alert . 'status', $where['status']);
  257. } else {
  258. $model->where($alert . 'status', 'in', [1, 0, -1]);
  259. }
  260. if (isset($where['mer_id']) && $where['mer_id']) $model = $model->where($alert . 'mer_id', $where['mer_id']);
  261. 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'])]);
  262. return $model->where($alert . 'is_del', 0);
  263. }
  264. /**专题审核列表
  265. * @param $where
  266. * @return array
  267. * @throws \think\Exception
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\ModelNotFoundException
  270. * @throws \think\exception\DbException
  271. */
  272. public static function getSpecialExamineList($where)
  273. {
  274. $data = self::setExamineWhere($where, 'A')->field('A.*,S.name as subject_name')
  275. ->join('__SPECIAL_SUBJECT__ S', 'S.id=A.subject_id', 'LEFT')
  276. ->page((int)$where['page'], (int)$where['limit'])->select();
  277. $data = count($data) ? $data->toArray() : [];
  278. foreach ($data as $key => &$value) {
  279. $value['fail_time'] = date('Y-m-d H:i:s', $value['fail_time']);
  280. }
  281. $count = self::setExamineWhere($where)->count();
  282. return compact('data', 'count');
  283. }
  284. /**获取试题关联的专题
  285. * @param $relation_ids
  286. * @return array
  287. * @throws \think\Exception
  288. * @throws \think\db\exception\DataNotFoundException
  289. * @throws \think\db\exception\ModelNotFoundException
  290. * @throws \think\exception\DbException
  291. */
  292. public static function getKnowledgePoints($relation_ids)
  293. {
  294. $data = self::PreWhere()->where('id', 'in', $relation_ids)->select();
  295. $data = count($data) > 0 ? $data->toArray() : [];
  296. $count = self::PreWhere()->where('id', 'in', $relation_ids)->count();
  297. return compact('data', 'count');
  298. }
  299. public static function getUserWhere($where)
  300. {
  301. return self::alias('s')->join('SpecialBuy b', 's.id=b.special_id')
  302. ->where(['b.uid' => $where['uid'], 's.is_del' => 0, 'b.is_del' => 0, 'b.type' => 3])
  303. ->field('s.title,s.type,s.id,s.image');
  304. }
  305. /**已获得专题
  306. * @param $where
  307. * @return array
  308. * @throws \think\Exception
  309. */
  310. public static function getUserSpecialList($where)
  311. {
  312. $data = self::getUserWhere($where)->page($where['page'], $where['limit'])->select();
  313. $count = self::getUserWhere($where)->count();
  314. return compact('data', 'count');
  315. }
  316. public static function getUserSpecialLists($where, $special_source, $type)
  317. {
  318. if ($type == 0) {
  319. $data = Special::setWhere($where)->where('id', 'not in', $special_source)->whereIn('type', [SPECIAL_IMAGE_TEXT, SPECIAL_AUDIO, SPECIAL_VIDEO, SPECIAL_COLUMN, SPECIAL_OTHER])->page((int)$where['page'], (int)$where['limit'])->select();
  320. $data = count($data) ? $data->toArray() : [];
  321. $count = Special::setWhere($where)->where('id', 'not in', $special_source)->whereIn('type', [SPECIAL_IMAGE_TEXT, SPECIAL_AUDIO, SPECIAL_VIDEO, SPECIAL_COLUMN, SPECIAL_OTHER])->count();
  322. } elseif ($type == 1) {
  323. $data = Special::setWhere($where)->where('id', 'not in', $special_source)->whereIn('type', [SPECIAL_LIVE])->page((int)$where['page'], (int)$where['limit'])->select();
  324. $data = count($data) ? $data->toArray() : [];
  325. $count = Special::setWhere($where)->where('id', 'not in', $special_source)->whereIn('type', [SPECIAL_LIVE])->count();
  326. }
  327. return compact('data', 'count');
  328. }
  329. }