SendOrderService.php 16 KB

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