SendOrderService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/12/2
  6. * Time: 15:36
  7. */
  8. namespace App\Modules\SendOrder\Services;
  9. use App\Modules\SendOrder\Models\QuickAppSendOrder;
  10. use App\Modules\SendOrder\Models\SendOrder;
  11. use App\Modules\SendOrder\Models\SendOrderExtraStat;
  12. use App\Modules\Statistic\Services\WapVisitStatService;
  13. use DB;
  14. use Redis;
  15. class SendOrderService
  16. {
  17. public static function getSendOrderStatic(int $id)
  18. {
  19. return SendOrder::find($id);
  20. }
  21. public function getQuickAppSendOrder(int $send_order_id)
  22. {
  23. return QuickAppSendOrder::where('send_order_id', $send_order_id)->first();
  24. }
  25. /**
  26. * 更新派单的备注
  27. * @param $id 派单id
  28. * @param $distribution_channel_id 渠道id
  29. * @param $remark 备注
  30. * @return mixed
  31. */
  32. static function updateRemark($id, $distribution_channel_id, $remark)
  33. {
  34. return SendOrder::updateRemark($id, $distribution_channel_id, $remark);
  35. }
  36. /**
  37. * 更新派单的星数
  38. * @param $id 派单id
  39. * @param $distribution_channel_id 渠道id
  40. * @param $starNum 星数
  41. * @return mixed
  42. */
  43. static function updateStarNum($id, $distribution_channel_id, $starNum)
  44. {
  45. return SendOrder::updateStarNum($id, $distribution_channel_id, $starNum);
  46. }
  47. /**
  48. * 更新派单的星数、备注
  49. * @param $id 派单id
  50. * @param $distribution_channel_id 渠道id
  51. * @param $starNum 星数
  52. * @return mixed
  53. */
  54. static function updateStarNumAndRemark($id, $distribution_channel_id, $starNum, $remark)
  55. {
  56. return SendOrder::updateStarNumAndRemark($id, $distribution_channel_id, $starNum, $remark);
  57. }
  58. /**
  59. * 更新派单
  60. * @param $id 派单id
  61. * @param $distribution_channel_id 渠道id
  62. * @param $name 派单名称
  63. * @param $cost 成本
  64. * @param $channel_type 派单渠道类型.(允许值: AUTHENTICATED, UNAUTHENTICATED)
  65. * @return mixed
  66. */
  67. static function updateSendOrderInfo($id, $distribution_channel_id, $name, $pre_send_date, $channel_type, $cost, $promotion_type, $subscribe_chapter = [])
  68. {
  69. return SendOrder::updateSendOrderInfo($id, $distribution_channel_id, $name, $pre_send_date, $channel_type, $cost, $promotion_type, $subscribe_chapter);
  70. }
  71. /**
  72. * 通过bid获取该书的推广次数
  73. * @param $bid 书本id
  74. */
  75. static function getPromotionCountByBid($bid, $channelId)
  76. {
  77. return SendOrder::getPromotionCountByBid($bid, $channelId);
  78. }
  79. /**
  80. * 通过渠道id获取该渠道的推广次数
  81. * @param $channelId 渠道id
  82. */
  83. static function getPromotionCountByChannelId($channelId)
  84. {
  85. return SendOrder::getPromotionCountByChannelId($channelId);
  86. }
  87. /**
  88. * 获取渠道下某时间段内推广次数
  89. * @param $channelId 渠道id
  90. * @param $begin_time 开始日期
  91. * @param $end_time 结束日期
  92. */
  93. static function getChannelPromotionCount($channelId, $begin_time, $end_time)
  94. {
  95. return SendOrder::getChannelPromotionCount($channelId, $begin_time, $end_time);
  96. }
  97. /**
  98. * 获取渠道下某时间段内实际推广次数
  99. * @param $channelId 渠道id
  100. * @param $begin_time 开始日期
  101. * @param $end_time 结束日期
  102. */
  103. static function getChannelRealPromotionCount($channelId, $begin_time, $end_time)
  104. {
  105. return SendOrder::getChannelRealPromotionCount($channelId, $begin_time, $end_time);
  106. }
  107. /**
  108. * 获取实际推广次数
  109. * @param $channelId 渠道id
  110. * @param $begin_time 开始日期
  111. * @param $end_time 结束日期
  112. */
  113. static function getRealPromotionCount($params)
  114. {
  115. return SendOrder::getRealPromotionCount($params);
  116. }
  117. /**
  118. * 获取渠道下某时间段内推广次数
  119. * @param $params begin_time、end_time、distribution_channel_id
  120. */
  121. static function getPromotionCount($params)
  122. {
  123. return SendOrder::getPromotionCount($params);
  124. }
  125. /**
  126. * 通过渠道id下所有的推广次数
  127. * @param $channelId 渠道id
  128. */
  129. static function getTotalPromotionCountByChannelIds($channelIds)
  130. {
  131. return SendOrder::getTotalPromotionCountByChannelIds($channelIds);
  132. }
  133. /**
  134. * 通过区间内的渠道id获取该渠道的推广次数
  135. * @param $channelId 渠道id
  136. * @param $start_time 开始时间
  137. * @param $end_time 结束时间
  138. */
  139. static function getDuraingPromotionCountByChannelId($channelId, $start_time = '', $end_time = '')
  140. {
  141. return SendOrder::getDuraingPromotionCountByChannelId($channelId, $start_time, $end_time);
  142. }
  143. /**
  144. * 获取派单信息
  145. * @param $bookId 推广书籍id
  146. * @param $distribution_channel_id 推广渠道id
  147. * @param $name 派单名称
  148. * @param $bookName 书名
  149. * @param $sendOrderId 派单id
  150. * @param string $start_date 开始时间
  151. * @param string $end_date 结束时间
  152. * @param string $isAll 是否获取所有
  153. * @return mixed
  154. */
  155. static function getSendOrders($bookId, $distribution_channel_id, $name = null, $bookName = null, $sendOrderId, $start_time = '', $end_time = '', $isAll = false)
  156. {
  157. return SendOrder::getSendOrders($bookId, $distribution_channel_id, $name, $bookName, $sendOrderId, $start_time, $end_time, $isAll);
  158. }
  159. /**
  160. * 管理后台获取派单信息
  161. * @param $bookId 推广书籍id
  162. * @param $distribution_channel_id 推广渠道id
  163. * @param $name 派单名称
  164. * @param $bookName 书名
  165. * @param $sendOrderId 派单id
  166. * @param string $start_date 开始时间
  167. * @param string $end_date 结束时间
  168. * @param string $isAll 是否获取所有
  169. * @return mixed
  170. */
  171. static function getManageSendOrders($params = [], $is_all = false)
  172. {
  173. return SendOrder::getManageSendOrders($params, $is_all);
  174. }
  175. /**
  176. * 创建推广派单(章节)
  177. * @param array ['name', 'channel_type', 'promotion_type','sex_preference', 'book_id', 'cost','book_name','qr_code_id', 'chapter_id', 'chapter_name', 'document_end_chapter_seq', 'original_guide_id', 'headline_id', 'body_template_id', 'document_cover_id', 'subscribe_chapter_id', 'subscribe_chapter_name', 'subscribe_chapter_seq', 'distribution_channel_id', 'redirect_url']
  178. */
  179. static function createFromChapter($data)
  180. {
  181. return SendOrder::createFromChapter($data);
  182. }
  183. /**
  184. * 创建推广派单(推广页面)
  185. * @param array ['name', 'channel_type','promotion_type', 'page_id', 'entrance','cost', 'qr_code_id','distribution_channel_id', 'redirect_url', 'subscribe_chapter_id', 'subscribe_chapter_name', 'subscribe_chapter_seq']
  186. */
  187. static function createFromPage($data)
  188. {
  189. return SendOrder::createFromPage($data);
  190. }
  191. /**
  192. * 创建推广派单(目录)
  193. * @param array ['name', 'channel_type', 'promotion_type','book_id', 'book_name','cost', 'chapter_id','qr_code_id', 'chapter_name', 'distribution_channel_id', 'redirect_url', 'subscribe_chapter_id', 'subscribe_chapter_name', 'subscribe_chapter_seq']
  194. */
  195. static function createFromDirectory($data)
  196. {
  197. return SendOrder::createFromDirectory($data);
  198. }
  199. /**
  200. * 获取Pv
  201. * @param $id 派单id
  202. * @return int PV
  203. */
  204. static function getPv($id)
  205. {
  206. return WapVisitStatService::getSendOrderTotalPv($id);
  207. //return (float)Redis::hget('send_order_pv_' . $id, 'total');
  208. }
  209. static function getBrowserUvAndPv($id)
  210. {
  211. return WapVisitStatService::getBrowserSendOrderTotalPvAndUv($id);
  212. }
  213. /**
  214. * 获取Uv
  215. * @param $id 派单id
  216. * @return int UV
  217. */
  218. static function getUv($id)
  219. {
  220. return WapVisitStatService::getSendOrderTotalUv($id);
  221. //return (float)Redis::hget('send_order_uv_' . $id, 'total');
  222. }
  223. /**
  224. * 获取每一天的Uv信息列表
  225. * @param $id 派单id
  226. * @return int UV
  227. */
  228. static function getUvInfo($id)
  229. {
  230. return WapVisitStatService::getSendOrderUv($id);
  231. //return Redis::hgetAll('send_order_uv_' . $id);
  232. }
  233. /**
  234. * 获取每一天的pv信息列表
  235. * @param $id 派单id
  236. * @return int PV
  237. */
  238. static function getPvInfo($id)
  239. {
  240. return WapVisitStatService::getSendOrderPv($id);
  241. //return Redis::hgetAll('send_order_pv_' . $id);
  242. }
  243. /**
  244. * 删除派单
  245. * @param $id 派单id
  246. * @param $distribution_channel_id 渠道id
  247. * @return
  248. */
  249. static function removeSendOrder($id, $distribution_channel_id)
  250. {
  251. return SendOrder::removeSendOrder($id, $distribution_channel_id);
  252. }
  253. /**
  254. * 获取重定向url
  255. * @param $id
  256. * @return mixed
  257. */
  258. static function getRedirectUrlById($id)
  259. {
  260. return SendOrder::where('id', $id)->select('redirect_url', 'book_id', 'send_time', 'distribution_channel_id', 'name', 'promotion_point', 'force_show_qrcode', 'promotion_type')->first();
  261. }
  262. /**
  263. * 根据id获取信息
  264. * @param $id
  265. * @return mixed
  266. */
  267. static function getById($id)
  268. {
  269. return SendOrder::find($id);
  270. }
  271. //获取推广总uv、pv
  272. static function getChannelPromotionTotalUvPv($distribution_channel_id, $date)
  273. {
  274. //return WapVisitStatService::getChannelPromotionTotalUvPv($distribution_channel_id,$date);
  275. $send_orders = SendOrder::getSendOrders(null, $distribution_channel_id, null, null, '', '', '', true);
  276. $uv = 0;
  277. $pv = 0;
  278. foreach ($send_orders as $send_order) {
  279. $uv += (float) Redis::hget('send_order_uv_' . $send_order->id, $date);
  280. $pv += (float) Redis::hget('send_order_pv_' . $send_order->id, $date);
  281. }
  282. return compact('uv', 'pv');
  283. }
  284. /**
  285. * 获取date的实际派单数
  286. */
  287. static function getActualSendOrderNum($distribution_channel_id, $date)
  288. {
  289. return SendOrder::whereBetween('send_time', [$date, date('Y-m-d', strtotime($date) + 86400)])->where('distribution_channel_id', $distribution_channel_id)->count();
  290. }
  291. /**
  292. * 获取继续阅读的uv
  293. * @param $distribution_channel_id
  294. * @param $date
  295. * @return array
  296. */
  297. static function getContinueTotalReadUv($send_order_id)
  298. {
  299. return (float) Redis::hget('send_order:continue:' . $send_order_id, 'total');
  300. }
  301. /**
  302. * 设置成本
  303. * @param $id 派单id
  304. * @param $distribution_channel_id 渠道id
  305. * @param $cost 成本
  306. * @return mixed
  307. */
  308. static function setSendOrderCost($id, $distribution_channel_id, $cost)
  309. {
  310. return SendOrder::setSendOrderCost($id, $distribution_channel_id, $cost);
  311. }
  312. /**
  313. * 获取派单下的首充用户数
  314. * $id 派单id
  315. * @param $id
  316. */
  317. static function getFirstChargeUserNum($id)
  318. {
  319. return SendOrder::getFirstChargeUserNum($id);
  320. }
  321. /**
  322. * 获取派单下的非首充用户数
  323. * $id 派单id
  324. * @param $id
  325. */
  326. static function getRepetitiousChargeUserNum($id)
  327. {
  328. return SendOrder::getRepetitiousChargeUserNum($id);
  329. }
  330. /**
  331. * 获取派单uv大于20的时间
  332. * @param $sendOrderId 派单id
  333. * @return float|int 时间
  334. */
  335. static function getUv20Time($sendOrderId)
  336. {
  337. $uv20Time = '';
  338. $periodTotalUV = 0;
  339. $uvInfos = SendOrderService::getUvInfo($sendOrderId);
  340. if (isset($uvInfos['total'])) {
  341. unset($uvInfos['total']);
  342. }
  343. ksort($uvInfos);
  344. foreach ($uvInfos as $key => $v) {
  345. $periodTotalUV += $v;
  346. //如果uv总数大于20
  347. if ($periodTotalUV > 20) {
  348. $uv20Time = $key;
  349. break;
  350. }
  351. }
  352. return $uv20Time;
  353. }
  354. /**
  355. * 新获取派单列表
  356. * @param $bookId 推广书籍id
  357. * @param $distribution_channel_id 推广渠道id
  358. * @param $name 派单名称
  359. * @param $bookName 书名
  360. * @param $sendOrderId 派单id
  361. * @param string $start_date 开始时间
  362. * @param string $end_date 结束时间
  363. * @param string $isAll 是否获取所有
  364. * @return mixed
  365. */
  366. static function search($params, $isAll = false)
  367. {
  368. return SendOrder::search($params, $isAll);
  369. }
  370. /*
  371. * 更新派单时间
  372. */
  373. static function updateSendOrderTime($send_order_id)
  374. {
  375. return SendOrder::where('id', $send_order_id)->update(['send_time' => date('Y-m-d H:i:s')]);
  376. }
  377. /**
  378. * 获取实际派单数
  379. */
  380. static function getRealSendOrderCount($distribution_channel_id = '', $date)
  381. {
  382. return SendOrder::getSendOrderCount($distribution_channel_id, $date);
  383. }
  384. static function getChannelPromotionBooks($distribution_channel_id = [], $endData, $book_name = '', $channel_name = '', $isAll = false)
  385. {
  386. $obj = SendOrder::select('send_orders.book_name', "companies.name as nickname", DB::raw("GROUP_CONCAT(distinct send_orders.distribution_channel_id) as distribution_channel_id"), DB::raw("count(*) as count"))
  387. ->join("distribution_channels", 'send_orders.distribution_channel_id', "=", "distribution_channels.id")
  388. ->join("channel_users", 'distribution_channels.channel_user_id', "=", "channel_users.id")
  389. ->join('companies', 'companies.id', "=", "channel_users.company_id")
  390. ->where("send_orders.created_at", "<=", $endData)
  391. ->groupBy('send_orders.book_name')
  392. ->groupBy('distribution_channels.channel_user_id');
  393. if ($book_name) {
  394. $obj = $obj->where('send_orders.book_name', $book_name);
  395. }
  396. if ($channel_name) {
  397. $obj = $obj->where('companies.name', 'like', '%' . $channel_name . '%');
  398. }
  399. if ($distribution_channel_id) {
  400. $obj = $obj->whereIn('send_orders.distribution_channel_id', $distribution_channel_id);
  401. }
  402. if ($isAll) {
  403. return $obj->get();
  404. } else {
  405. return $obj->paginate();
  406. }
  407. }
  408. static function getExtraStat($send_order_id)
  409. {
  410. return SendOrderExtraStat::getBySendOrderId($send_order_id);
  411. }
  412. static function getPeriodActualSendOrdersNum($channel_id, $start_time, $end_time)
  413. {
  414. return SendOrder::where([
  415. ['distribution_channel_id', '=', $channel_id],
  416. ['send_time', '>=', $start_time],
  417. ['send_time', '<=', $end_time],
  418. ])->count();
  419. }
  420. static function getCompanyPromotionBooks($company_id, $start_time, $end_time)
  421. {
  422. $res = DB::select("select * from (select bcs.bid,bcs.book_name,count(so.id) as promotion_times
  423. from channel_users cu
  424. left join distribution_channels dc on dc.channel_user_id=cu.id
  425. left join send_orders so on so.distribution_channel_id=dc.id
  426. left join book_configs bcs on bcs.bid=so.book_id
  427. where cu.company_id=" . $company_id . " and so.book_id is not null and so.send_time between '" . $start_time . "' and '" . $end_time . "'
  428. group by so.book_id) tmp order by tmp.promotion_times desc");
  429. return $res;
  430. }
  431. //成本统计
  432. static function getCostStats($distribution_channels)
  433. {
  434. return SendOrder::whereIn('distribution_channel_id', $distribution_channels)
  435. ->where('is_enable', 1)
  436. ->whereNotNull('send_time')
  437. ->sum('cost');
  438. }
  439. }