SendOrder.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/22
  6. * Time: 14:15
  7. */
  8. namespace App\Modules\SendOrder\Models;
  9. use App\Modules\Book\Models\BookConfig;
  10. use DB;
  11. use Illuminate\Database\Eloquent\Model;
  12. class SendOrder extends Model
  13. {
  14. protected $table = 'send_orders';
  15. protected $fillable = [
  16. 'name',
  17. 'channel_type',
  18. 'promotion_type',
  19. 'distribution_channel_id',
  20. 'book_id',
  21. 'book_name',
  22. 'chapter_id',
  23. 'chapter_name',
  24. 'headline_id',
  25. 'body_template_id',
  26. 'document_cover_id',
  27. 'original_guide_id',
  28. 'subscribe_chapter_id',
  29. 'import_company_name',
  30. 'fan_num',
  31. 'subscribe_chapter_name',
  32. 'qr_code_id',
  33. 'subscribe_chapter_seq',
  34. 'cost',
  35. 'sex_preference',
  36. 'is_enable',
  37. 'entrance',
  38. 'domain',
  39. 'redirect_url',
  40. 'send_time',
  41. 'charge_type',
  42. 'pre_send_date',
  43. 'promotion_point'
  44. ];
  45. static function searchForDaliy($params, $isAll = false)
  46. {
  47. $search_object = self::where('is_enable', 1);
  48. $fields = ['send_orders.id', 'book_id', 'name', 'promotion_type', 'send_orders.created_at', 'remark', 'import_company_name', 'send_time', 'promotion_point', 'pre_send_date', 'book_name', 'distribution_channel_id', 'cost'];
  49. $search_object->select($fields);
  50. if (isset($params['distribution_channel_ids']) && $params['distribution_channel_ids']) {
  51. $search_object->whereIn('distribution_channel_id', $params['distribution_channel_ids']);
  52. }
  53. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
  54. $search_object->where('distribution_channel_id', $params['distribution_channel_id']);
  55. }
  56. if (isset($params['start_time']) && $params['start_time']) {
  57. $search_object->where('send_time', '>=', $params['start_time']);
  58. }
  59. if (isset($params['end_time']) && $params['end_time']) {
  60. $search_object->where('send_time', '<=', $params['end_time']);
  61. }
  62. //按预计日期
  63. if (isset($params['pre_date']) && $params['pre_date']) {
  64. $search_object->where('pre_send_date', '=', $params['pre_date']);
  65. }
  66. if (isset($params['promotion_type']) && $params['promotion_type']) {
  67. $search_object->where('promotion_type', $params['promotion_type']);
  68. }
  69. if (isset($params['is_single_site']) && $params['is_single_site']) {
  70. $search_object->orderBy('pre_send_date');
  71. } else {
  72. $search_object->orderBy('distribution_channel_id');
  73. }
  74. if ($isAll) {
  75. return $search_object->get();
  76. } else {
  77. return $search_object->paginate();
  78. }
  79. }
  80. public static function getDuringCostByChannelIds($distribution_channels, $begin_time, $end_time)
  81. {
  82. $search_obj = self::where('is_enable', 1)
  83. ->where('pre_send_date', '>=', $begin_time)
  84. ->where('pre_send_date', '<=', $end_time)
  85. ->whereIn('distribution_channel_id', $distribution_channels);
  86. return $search_obj->sum('cost');
  87. }
  88. /**
  89. * 通过bid获取该书的推广次数
  90. * @param $bid 书本id
  91. * @param $channelId 渠道id
  92. */
  93. static function getPromotionCountByBid($bid, $channelId)
  94. {
  95. return self::where('book_id', $bid)->where('distribution_channel_id', $channelId)->count();
  96. }
  97. /**
  98. * 通过bid获取该书的推广次数
  99. * @param $bid 书本id
  100. * @param $channelId 渠道id
  101. */
  102. static function getDuringPromotionCountByBid($bid, $begin_time, $end_time)
  103. {
  104. $search_obj = self::orderBy('id', 'desc')->where('is_enable', 1);
  105. if ($bid) {
  106. $search_obj->where('book_id', $bid);
  107. }
  108. if ($begin_time) {
  109. $search_obj->where('send_time', '>=', $begin_time);
  110. }
  111. if ($end_time) {
  112. $search_obj->where('send_time', '<=', $end_time);
  113. }
  114. return $search_obj->count();
  115. }
  116. /**
  117. * 获取渠道下某时间段内推广次数
  118. * @param $channelId 渠道id
  119. * @param $begin_time 开始日期
  120. * @param $end_time 结束日期
  121. */
  122. static function getChannelPromotionCount($channelId, $begin_time, $end_time)
  123. {
  124. return self::where('created_at', '>=', $begin_time)->where('created_at', '<=', $end_time)->where('distribution_channel_id', $channelId)->count();
  125. }
  126. /**
  127. * 获取渠道下某时间段内实际推广次数
  128. * @param $channelId 渠道id
  129. * @param $begin_time 开始日期
  130. * @param $end_time 结束日期
  131. */
  132. static function getChannelRealPromotionCount($channelId, $begin_time, $end_time)
  133. {
  134. return self::where('send_time', '>=', $begin_time)->where('send_time', '<=', $end_time)->where('distribution_channel_id', $channelId)->count();
  135. }
  136. /**
  137. * 获取实际推广次数
  138. * @param $params
  139. */
  140. static function getRealPromotionCount($params)
  141. {
  142. $search_obj = self::orderBy('id', 'desc');
  143. if (isset($params['begin_time'])) $search_obj->where('send_time', '>=', $params['begin_time']);
  144. if (isset($params['end_time'])) $search_obj->where('send_time', '<=', $params['end_time']);
  145. if (isset($params['distribution_channel_id'])) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  146. return $search_obj->count();
  147. }
  148. /**
  149. * 获取推广次数
  150. * @param $params
  151. */
  152. static function getPromotionCount($params)
  153. {
  154. $search_obj = self::orderBy('id', 'desc');
  155. if (isset($params['begin_time'])) $search_obj->where('created_at', '>=', $params['begin_time']);
  156. if (isset($params['end_time'])) $search_obj->where('created_at', '<=', $params['end_time']);
  157. if (isset($params['distribution_channel_id'])) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  158. if (isset($params['channels'])) $search_obj->whereIn('distribution_channel_id', $params['channels']);
  159. if (isset($params['book_id'])) $search_obj->where('book_id', $params['book_id']);
  160. return $search_obj->count();
  161. }
  162. /**
  163. * 通过渠道id获取该渠道的推广次数
  164. * @param $channelId 渠道id
  165. */
  166. static function getPromotionCountByChannelId($channelId)
  167. {
  168. return self::where('distribution_channel_id', $channelId)->count();
  169. }
  170. /**
  171. * 获取渠道id下所有的推广次数
  172. * @param $channelId 渠道id
  173. */
  174. static function getTotalPromotionCountByChannelIds($channelIds)
  175. {
  176. return self::whereIn('distribution_channel_id', $channelIds)->count();
  177. }
  178. /**
  179. * 通过区间内的渠道id获取该渠道的推广次数
  180. * @param $channelId 渠道id
  181. * @param $start_time 开始时间
  182. * @param $end_time 结束时间
  183. */
  184. static function getDuraingPromotionCountByChannelId($distribution_channel_id, $start_time = '', $end_time = '')
  185. {
  186. $search_object = self::where('is_enable', 1);
  187. if ($distribution_channel_id) {
  188. $search_object->where('distribution_channel_id', $distribution_channel_id);
  189. }
  190. if ($start_time) {
  191. $search_object->where('created_at', '>=', $start_time);
  192. }
  193. if ($end_time) {
  194. $search_object->where('created_at', '<=', $end_time);
  195. }
  196. return $search_object->count();
  197. }
  198. /**
  199. * 获取派单信息
  200. * @param $bookId 推广书籍id
  201. * @param $distribution_channel_id 推广渠道id
  202. * @param $name 派单名称
  203. * @param $bookName 书名
  204. * @param $sendOrderId 派单id
  205. * @param string $start_date 开始时间
  206. * @param string $end_date 结束时间
  207. * @param string $isAll 是否获取所有
  208. * @return mixed
  209. */
  210. static function getSendOrders($bookId, $distribution_channel_id, $name = null, $bookName = null, $sendOrderId, $start_time = '', $end_time = '', $isAll = false)
  211. {
  212. $search_object = self::orderBy('id', 'desc');
  213. if ($bookId) {
  214. $search_object->where('book_id', $bookId);
  215. }
  216. if ($distribution_channel_id) {
  217. $search_object->where('distribution_channel_id', $distribution_channel_id);
  218. }
  219. if ($bookName) {
  220. $search_object->where('book_name', $bookName);
  221. }
  222. if ($sendOrderId) {
  223. $search_object->where('id', $sendOrderId);
  224. }
  225. if ($name) {
  226. $search_object->where('name', 'like', '%' . $name . '%');
  227. }
  228. if ($start_time) {
  229. $search_object->where('created_at', '>=', $start_time);
  230. }
  231. if ($end_time) {
  232. $search_object->where('created_at', '<=', $end_time . ' 23:59:59');
  233. }
  234. $search_object->where('is_enable', 1);
  235. if ($isAll) {
  236. return $search_object->get();
  237. } else {
  238. return $search_object->paginate();
  239. }
  240. }
  241. /**
  242. * 获取派单信息
  243. * @param $bookId 推广书籍id
  244. * @param $distribution_channel_id 推广渠道id
  245. * @param $name 派单名称
  246. * @param $bookName 书名
  247. * @param $sendOrderId 派单id
  248. * @param string $start_date 开始时间
  249. * @param string $end_date 结束时间
  250. * @param string $isAll 是否获取所有
  251. * @return mixed
  252. */
  253. static function getManageSendOrders($params = [], $is_all = false)
  254. {
  255. $search_object = self::orderBy('send_orders.created_at', 'desc')->where('is_enable', 1)
  256. ->join('books', 'books.id', '=', 'send_orders.book_id')
  257. ->join('book_categories', 'book_categories.id', '=', 'books.category_id');
  258. if (isset($params['id']) && $params['id']) $search_object->where('send_orders.id', $params['id']);
  259. if (isset($params['book_id']) && $params['book_id']) $search_object->where('book_id', $params['book_id']);
  260. if (isset($params['name']) && $params['name']) $search_object->where('name', 'like', '%' . $params['name'] . '%');
  261. if (isset($params['book_name']) && $params['book_name']) $search_object->where('book_name', 'like', '%' . $params['book_name'] . '%');
  262. if (isset($params['start_time']) && $params['start_time']) $search_object->where('send_orders.created_at', '>=', $params['start_time']);
  263. if (isset($params['end_time']) && $params['end_time']) $search_object->where('send_orders.created_at', '<=', $params['end_time'] . ' 23:59:59');
  264. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_object->where('send_orders.distribution_channel_id', $params['distribution_channel_id']);
  265. if (isset($params['gender']) && $params['gender']) $search_object->where('book_categories.pid', '=', $params['gender']);
  266. $search_object->select('send_orders.id', 'send_orders.name', 'send_orders.channel_type', 'send_orders.distribution_channel_id', 'send_orders.created_at', 'send_orders.updated_at', 'book_id', 'book_name', 'chapter_id', 'chapter_name', 'headline_id',
  267. 'body_template_id', 'document_cover_id', 'original_guide_id', 'subscribe_chapter_id', 'subscribe_chapter_name', 'qr_code_id',
  268. 'subscribe_chapter_seq', 'cost', 'sex_preference', 'is_enable', 'entrance', 'domain', 'redirect_url', 'send_time', 'charge_type', 'channel_name',
  269. DB::raw('(select ifnull(count(*),0) from force_subscribe_users where send_order_id=send_orders.id and is_subscribed=1) as fansNum'),
  270. DB::raw('(select ifnull(sum(price),0) from orders where send_order_id=send_orders.id and status="PAID" and pay_end_at BETWEEN send_orders.send_time and DATE_ADD(send_orders.send_time,interval 12 HOUR)) as half_day_charge_amount'),
  271. DB::raw('(select ifnull(sum(price),0) from orders where send_order_id=send_orders.id and status="PAID" and pay_end_at BETWEEN send_orders.send_time and DATE_ADD(send_orders.send_time,interval 7 DAY)) as weekend_charge_amount'));
  272. //判断内外部派单
  273. /* if (isset($params['order_status']) && $params['order_status']) {
  274. if (1 == $params['order_status']) {
  275. // $search_object->whereExists('fansNum', '<', 20);
  276. $search_object->whereExists(function ($query) {
  277. $query->from('force_subscribe_users')
  278. ->whereRaw('send_order_id=send_orders.id and is_subscribed=1')->where(DB::raw('count(*)'), '<', 20);
  279. });
  280. } elseif (2 == $params['order_status']) {
  281. // $search_object->having('fansNum', '>=', 20);
  282. $search_object->whereExists(function ($query) {
  283. $query->from('force_subscribe_users')
  284. ->whereRaw('send_order_id=send_orders.id and is_subscribed=1')->where(DB::raw('count(*)'), '>=', 20);
  285. });
  286. }
  287. }*/
  288. if (isset($params['orderBy']) && $params['orderBy']) {
  289. $orderByType = 'desc';
  290. $orderColum = 'send_orders.created_at'; //排序的列
  291. if (isset($params['orderByType']) && $params['orderByType']) {
  292. if (2 == $params['orderByType']) {
  293. $orderByType = 'asc';
  294. }
  295. }
  296. //12小时充值
  297. if (1 == $params['orderBy']) {
  298. $orderColum = 'half_day_charge_amount';
  299. //7天充值充值
  300. } elseif (2 == $params['orderBy']) {
  301. $orderColum = 'weekend_charge_amount';
  302. //充值总额
  303. }
  304. $search_object->orderBy($orderColum, $orderByType);
  305. }
  306. if ($is_all) {
  307. return $search_object->get();
  308. } else {
  309. return $search_object->paginate();
  310. }
  311. }
  312. /**
  313. * 获取派单信息
  314. * @param $bookId 推广书籍id
  315. * @param $distribution_channel_id 推广渠道id
  316. * @param $name 派单名称
  317. * @param $bookName 书名
  318. * @param $sendOrderId 派单id
  319. * @param string $start_date 开始时间
  320. * @param string $end_date 结束时间
  321. * @param string $isAll 是否获取所有
  322. * @return mixed
  323. */
  324. static function search($params, $isAll = false)
  325. {
  326. $search_object = self::where('is_enable', 1)->orderBy('id', 'desc');
  327. if (isset($params['book_id']) && $params['book_id']) {
  328. $search_object->where('send_orders.book_id', $params['book_id']);
  329. }
  330. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
  331. $search_object->where('send_orders.distribution_channel_id', $params['distribution_channel_id']);
  332. }
  333. if (isset($params['book_name']) && $params['book_name']) {
  334. $book_ids = BookConfig::getIdByName($params['book_name']);
  335. if ($book_ids) {
  336. $bids = [];
  337. foreach ($book_ids as $item) {
  338. $bids[] = $item->bid;
  339. }
  340. $search_object->whereIn('send_orders.book_id', $bids);
  341. }
  342. }
  343. if (isset($params['import_company_name']) && $params['import_company_name']) {
  344. $search_object->where('send_orders.import_company_name', 'like', '%' . $params['import_company_name'] . '%');
  345. }
  346. if (isset($params['name']) && $params['name']) {
  347. $search_object->where('send_orders.name', 'like', '%' . $params['name'] . '%');
  348. }
  349. if (isset($params['start_time']) && $params['start_time']) {
  350. $search_object->where('send_orders.created_at', '>=', $params['start_time']);
  351. }
  352. if (isset($params['end_time']) && $params['end_time']) {
  353. $search_object->where('send_orders.created_at', '<=', $params['end_time']);
  354. }
  355. if (isset($params['send_time_start_time']) && $params['send_time_start_time']) {
  356. $search_object->where('send_orders.send_time', '>=', $params['send_time_start_time']);
  357. }
  358. if (isset($params['send_time_end_time']) && $params['send_time_end_time']) {
  359. $search_object->where('send_orders.send_time', '<=', $params['send_time_end_time']);
  360. }
  361. if (isset($params['pre_send_date_end']) && $params['pre_send_date_end']) {
  362. $search_object->where('send_orders.pre_send_date', '<=', $params['pre_send_date_end']);
  363. }
  364. if (isset($params['pre_send_date_start']) && $params['pre_send_date_start']) {
  365. $search_object->where('send_orders.pre_send_date', '>=', $params['pre_send_date_start']);
  366. }
  367. if (isset($params['start_send_time']) && $params['start_send_time']) {
  368. $search_object->where('send_orders.send_time', '>=', $params['start_send_time']);
  369. }
  370. if (isset($params['end_send_time']) && $params['end_send_time']) {
  371. $search_object->where('send_orders.send_time', '<=', $params['end_send_time']);
  372. }
  373. if (isset($params['promotion_type']) && $params['promotion_type']) {
  374. $search_object->where('send_orders.promotion_type', $params['promotion_type']);
  375. }
  376. if (isset($params['id']) && $params['id']) {
  377. $search_object->where('send_orders.id', $params['id']);
  378. }
  379. if (isset($params['promotion_point']) && $params['promotion_point']) {
  380. $search_object->where('send_orders.promotion_point', $params['promotion_point']);
  381. }
  382. // \Log::info('my_sql:'.($search_object->toSql()));
  383. if ($isAll) {
  384. return $search_object->get();
  385. } else {
  386. return $search_object->paginate();
  387. }
  388. }
  389. static function searchForDaily($params, $isAll = false)
  390. {
  391. $search_object = self::where('is_enable', 1);
  392. $fields = ['send_orders.id', 'book_id', 'name', 'promotion_type', 'send_orders.created_at', 'remark', 'import_company_name', 'send_time', 'promotion_point', 'pre_send_date', 'book_name', 'distribution_channel_id', 'cost'];
  393. $search_object->select($fields);
  394. if (isset($params['distribution_channel_ids']) && $params['distribution_channel_ids']) {
  395. $search_object->whereIn('distribution_channel_id', $params['distribution_channel_ids']);
  396. }
  397. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
  398. $search_object->where('distribution_channel_id', $params['distribution_channel_id']);
  399. }
  400. if (isset($params['start_time']) && $params['start_time']) {
  401. $search_object->where('send_time', '>=', $params['start_time']);
  402. }
  403. if (isset($params['end_time']) && $params['end_time']) {
  404. $search_object->where('send_time', '<=', $params['end_time']);
  405. }
  406. //按预计日期
  407. if (isset($params['pre_date']) && $params['pre_date']) {
  408. $search_object->where('pre_send_date', '=', $params['pre_date']);
  409. }
  410. if (isset($params['promotion_type']) && $params['promotion_type']) {
  411. $search_object->where('promotion_type', $params['promotion_type']);
  412. }
  413. if (isset($params['is_single_site']) && $params['is_single_site']) {
  414. $search_object->orderBy('pre_send_date');
  415. } else {
  416. $search_object->orderBy('distribution_channel_id');
  417. }
  418. if ($isAll) {
  419. return $search_object->get();
  420. } else {
  421. return $search_object->paginate();
  422. }
  423. }
  424. /**
  425. * 更新派单的备注
  426. * @param $id 派单id
  427. * @param $distribution_channel_id 渠道id
  428. * @param $remark 备注
  429. * @return mixed
  430. */
  431. static function updateRemark($id, $distribution_channel_id, $remark)
  432. {
  433. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['remark' => $remark]);
  434. }
  435. /**
  436. * 更新派单的星数
  437. * @param $id 派单id
  438. * @param $distribution_channel_id 渠道id
  439. * @param $starNum 星数
  440. * @return mixed
  441. */
  442. static function updateStarNum($id, $distribution_channel_id, $starNum)
  443. {
  444. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['star_num' => $starNum]);
  445. }
  446. /**
  447. * 更新派单的星数、备注
  448. * @param $id 派单id
  449. * @param $distribution_channel_id 渠道id
  450. * @param $starNum 星数
  451. * @param $remark 备注
  452. * @return mixed
  453. */
  454. static function updateStarNumAndRemark($id, $distribution_channel_id, $starNum, $remark)
  455. {
  456. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['star_num' => $starNum, 'remark' => $remark]);
  457. }
  458. /**
  459. * 更新派单
  460. * @param $id 派单id
  461. * @param $distribution_channel_id 渠道id
  462. * @param $name 派单名称
  463. * @param $cost 成本
  464. * @param $channel_type 派单渠道类型.(允许值: AUTHENTICATED, UNAUTHENTICATED)
  465. * @param $promotion_type 派单类型.(允许值: INTERNAL, EXTERNAL)
  466. * @return mixed
  467. */
  468. static function updateSendOrderInfo($id, $distribution_channel_id, $name, $pre_send_date, $channel_type, $cost, $promotion_type, $subscribe_chapter = [])
  469. {
  470. $object = self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id);
  471. $params = [];
  472. if ($name) {
  473. $params['name'] = $name;
  474. }
  475. if ($channel_type) {
  476. $params['channel_type'] = $channel_type;
  477. }
  478. if ($cost) {
  479. $params['cost'] = $cost;
  480. }
  481. if ($promotion_type) {
  482. $params['promotion_type'] = $promotion_type;
  483. }
  484. if ($pre_send_date) {
  485. $params['pre_send_date'] = $pre_send_date;
  486. }
  487. if ($subscribe_chapter) {
  488. $params['subscribe_chapter_id'] = $subscribe_chapter['subscribe_chapter_id'];
  489. $params['subscribe_chapter_seq'] = $subscribe_chapter['subscribe_chapter_seq'];
  490. $params['subscribe_chapter_name'] = $subscribe_chapter['subscribe_chapter_name'];
  491. }
  492. return $object->update($params);
  493. }
  494. /**
  495. * 创建推广派单(章节)
  496. */
  497. static function createFromChapter($data)
  498. {
  499. return self::create($data);
  500. }
  501. /**
  502. * 创建推广派单(页面)
  503. */
  504. static function createFromPage($data)
  505. {
  506. return self::create($data);
  507. }
  508. /**
  509. * 创建推广派单(目录)
  510. */
  511. static function createFromDirectory($data)
  512. {
  513. return self::create($data);
  514. }
  515. /**
  516. * 删除派单
  517. * @param $id 派单id
  518. * @param $distribution_channel_id 渠道id
  519. * @return
  520. */
  521. static function removeSendOrder($id, $distribution_channel_id)
  522. {
  523. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['is_enable' => 0]);
  524. }
  525. /**
  526. * 设置成本
  527. * @param $id 派单id
  528. * @param $distribution_channel_id 渠道id
  529. * @param $cost 成本
  530. * @return mixed
  531. */
  532. static function setSendOrderCost($id, $distribution_channel_id, $cost)
  533. {
  534. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['cost' => $cost]);
  535. }
  536. /**
  537. * 获取派单下的首充用户数
  538. * @param $id 派单id
  539. */
  540. static function getFirstChargeUserNum($id)
  541. {
  542. $result = DB::select("SELECT COUNT(*) as num FROM ( SELECT (SELECT IFNULL(COUNT(*),0) as isss FROM orders WHERE uid=a.uid and `status` = 'PAID' and pay_end_at < a.end LIMIT 1) as temp FROM (SELECT uid,MIN(pay_end_at) as 'end' FROM orders WHERE status='PAID' and send_order_id ='{$id}' GROUP BY uid) a ) b WHERE temp = 0");
  543. return ($result && isset($result[0]->num)) ? $result[0]->num : 0;
  544. }
  545. /**
  546. * 获取派单下的非首充用户数
  547. * @param $id 派单id
  548. */
  549. static function getRepetitiousChargeUserNum($id)
  550. {
  551. $result = DB::select("SELECT COUNT(*) as num FROM (SELECT (SELECT IFNULL(COUNT(*),0) as isss FROM orders WHERE uid=a.uid and `status` = 'PAID' and pay_end_at < a.end LIMIT 1) as temp FROM (SELECT uid,MIN(pay_end_at) as 'end' FROM orders WHERE status='PAID' and send_order_id ='{$id}' GROUP BY uid) a ) b WHERE temp = 1");
  552. return ($result && isset($result[0]->num)) ? $result[0]->num : 0;
  553. }
  554. /**
  555. * 获取实际派单数
  556. */
  557. static function getSendOrderCount($distribution_channel_id = '', $date)
  558. {
  559. $search_object = self::where('send_time', '>=', $date)->where('send_time', '<=', date('Y-m-d', strtotime($date) + 86400));
  560. if ($distribution_channel_id) {
  561. $search_object->where('distribution_channel_id', $distribution_channel_id);
  562. }
  563. return $search_object->count();
  564. }
  565. static function getBookdSendNum($start_date,$end_date)
  566. {
  567. return self::select(DB::raw('book_id,count(1) num'))->where('send_time','>=',$start_date)->where('send_time','<',$end_date)->groupBy('book_id')->get()->pluck('num','book_id');
  568. }
  569. public static function getSendOrdersByIds($ids)
  570. {
  571. $result = self::whereIn('id', $ids)->get();
  572. return $result ? $result->toArray() : [];
  573. }
  574. }