SendOrderService.php 15 KB

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