SendOrderService.php 16 KB

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