SendOrder.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. 'force_show_qrcode'
  45. ];
  46. /**
  47. * 通过bid获取该书的推广次数
  48. * @param $bid 书本id
  49. * @param $channelId 渠道id
  50. */
  51. static function getPromotionCountByBid($bid, $channelId)
  52. {
  53. return self::where('book_id', $bid)->where('distribution_channel_id', $channelId)->count();
  54. }
  55. /**
  56. * 通过bid获取该书的推广次数
  57. * @param $bid 书本id
  58. * @param $channelId 渠道id
  59. */
  60. static function getDuringPromotionCountByBid($bid, $begin_time, $end_time)
  61. {
  62. $search_obj = self::orderBy('id', 'desc')->where('is_enable', 1);
  63. if ($bid) {
  64. $search_obj->where('book_id', $bid);
  65. }
  66. if ($begin_time) {
  67. $search_obj->where('send_time', '>=', $begin_time);
  68. }
  69. if ($end_time) {
  70. $search_obj->where('send_time', '<=', $end_time);
  71. }
  72. return $search_obj->count();
  73. }
  74. /**
  75. * 获取渠道下某时间段内推广次数
  76. * @param $channelId 渠道id
  77. * @param $begin_time 开始日期
  78. * @param $end_time 结束日期
  79. */
  80. static function getChannelPromotionCount($channelId, $begin_time, $end_time)
  81. {
  82. return self::where('created_at', '>=', $begin_time)->where('created_at', '<=', $end_time)->where('distribution_channel_id', $channelId)->count();
  83. }
  84. /**
  85. * 获取渠道下某时间段内实际推广次数
  86. * @param $channelId 渠道id
  87. * @param $begin_time 开始日期
  88. * @param $end_time 结束日期
  89. */
  90. static function getChannelRealPromotionCount($channelId, $begin_time, $end_time)
  91. {
  92. return self::where('send_time', '>=', $begin_time)->where('send_time', '<=', $end_time)->where('distribution_channel_id', $channelId)->count();
  93. }
  94. /**
  95. * 获取实际推广次数
  96. * @param $params
  97. */
  98. static function getRealPromotionCount($params)
  99. {
  100. $search_obj = self::orderBy('id', 'desc');
  101. if (isset($params['begin_time'])) $search_obj->where('send_time', '>=', $params['begin_time']);
  102. if (isset($params['end_time'])) $search_obj->where('send_time', '<=', $params['end_time']);
  103. if (isset($params['distribution_channel_id'])) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  104. return $search_obj->count();
  105. }
  106. /**
  107. * 获取推广次数
  108. * @param $params
  109. */
  110. static function getPromotionCount($params)
  111. {
  112. $search_obj = self::orderBy('id', 'desc');
  113. if (isset($params['begin_time'])) $search_obj->where('created_at', '>=', $params['begin_time']);
  114. if (isset($params['end_time'])) $search_obj->where('created_at', '<=', $params['end_time']);
  115. if (isset($params['distribution_channel_id'])) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  116. if (isset($params['channels'])) $search_obj->whereIn('distribution_channel_id', $params['channels']);
  117. if (isset($params['book_id'])) $search_obj->where('book_id', $params['book_id']);
  118. return $search_obj->count();
  119. }
  120. /**
  121. * 通过渠道id获取该渠道的推广次数
  122. * @param $channelId 渠道id
  123. */
  124. static function getPromotionCountByChannelId($channelId)
  125. {
  126. return self::where('distribution_channel_id', $channelId)->count();
  127. }
  128. /**
  129. * 获取渠道id下所有的推广次数
  130. * @param $channelId 渠道id
  131. */
  132. static function getTotalPromotionCountByChannelIds($channelIds)
  133. {
  134. return self::whereIn('distribution_channel_id', $channelIds)->count();
  135. }
  136. /**
  137. * 通过区间内的渠道id获取该渠道的推广次数
  138. * @param $channelId 渠道id
  139. * @param $start_time 开始时间
  140. * @param $end_time 结束时间
  141. */
  142. static function getDuraingPromotionCountByChannelId($distribution_channel_id, $start_time = '', $end_time = '')
  143. {
  144. $search_object = self::where('is_enable', 1);
  145. if ($distribution_channel_id) {
  146. $search_object->where('distribution_channel_id', $distribution_channel_id);
  147. }
  148. if ($start_time) {
  149. $search_object->where('created_at', '>=', $start_time);
  150. }
  151. if ($end_time) {
  152. $search_object->where('created_at', '<=', $end_time);
  153. }
  154. return $search_object->count();
  155. }
  156. /**
  157. * 获取派单信息
  158. * @param $bookId 推广书籍id
  159. * @param $distribution_channel_id 推广渠道id
  160. * @param $name 派单名称
  161. * @param $bookName 书名
  162. * @param $sendOrderId 派单id
  163. * @param string $start_date 开始时间
  164. * @param string $end_date 结束时间
  165. * @param string $isAll 是否获取所有
  166. * @return mixed
  167. */
  168. static function getSendOrders($bookId, $distribution_channel_id, $name = null, $bookName = null, $sendOrderId, $start_time = '', $end_time = '', $isAll = false)
  169. {
  170. $search_object = self::orderBy('created_at', 'desc');
  171. if ($bookId) {
  172. $search_object->where('book_id', $bookId);
  173. }
  174. if ($distribution_channel_id) {
  175. $search_object->where('distribution_channel_id', $distribution_channel_id);
  176. }
  177. if ($bookName) {
  178. $search_object->where('book_name', $bookName);
  179. }
  180. if ($sendOrderId) {
  181. $search_object->where('id', $sendOrderId);
  182. }
  183. if ($name) {
  184. $search_object->where('name', 'like', '%' . $name . '%');
  185. }
  186. if ($start_time) {
  187. $search_object->where('created_at', '>=', $start_time);
  188. }
  189. if ($end_time) {
  190. $search_object->where('created_at', '<=', $end_time . ' 23:59:59');
  191. }
  192. $search_object->where('is_enable', 1);
  193. if ($isAll) {
  194. return $search_object->get();
  195. } else {
  196. return $search_object->paginate();
  197. }
  198. }
  199. /**
  200. * 获取派单信息
  201. * @param $bookId 推广书籍id
  202. * @param $distribution_channel_id 推广渠道id
  203. * @param $name 派单名称
  204. * @param $bookName 书名
  205. * @param $sendOrderId 派单id
  206. * @param string $start_date 开始时间
  207. * @param string $end_date 结束时间
  208. * @param string $isAll 是否获取所有
  209. * @return mixed
  210. */
  211. static function getManageSendOrders($params = [], $is_all = false)
  212. {
  213. $search_object = self::orderBy('send_orders.created_at', 'desc')->where('is_enable', 1)
  214. ->join('books', 'books.id', '=', 'send_orders.book_id')
  215. ->join('book_categories', 'book_categories.id', '=', 'books.category_id');
  216. if (isset($params['id']) && $params['id']) $search_object->where('send_orders.id', $params['id']);
  217. if (isset($params['book_id']) && $params['book_id']) $search_object->where('book_id', $params['book_id']);
  218. if (isset($params['name']) && $params['name']) $search_object->where('name', 'like', '%' . $params['name'] . '%');
  219. if (isset($params['book_name']) && $params['book_name']) $search_object->where('book_name', 'like', '%' . $params['book_name'] . '%');
  220. if (isset($params['start_time']) && $params['start_time']) $search_object->where('send_orders.created_at', '>=', $params['start_time']);
  221. if (isset($params['end_time']) && $params['end_time']) $search_object->where('send_orders.created_at', '<=', $params['end_time'] . ' 23:59:59');
  222. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_object->where('send_orders.distribution_channel_id', $params['distribution_channel_id']);
  223. if (isset($params['gender']) && $params['gender']) $search_object->where('book_categories.pid', '=', $params['gender']);
  224. $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',
  225. 'body_template_id', 'document_cover_id', 'original_guide_id', 'subscribe_chapter_id', 'subscribe_chapter_name', 'qr_code_id',
  226. 'subscribe_chapter_seq', 'cost', 'sex_preference', 'is_enable', 'entrance', 'domain', 'redirect_url', 'send_time', 'charge_type', 'channel_name',
  227. DB::raw('(select ifnull(count(*),0) from force_subscribe_users where send_order_id=send_orders.id and is_subscribed=1) as fansNum'),
  228. 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'),
  229. 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'));
  230. //判断内外部派单
  231. /* if (isset($params['order_status']) && $params['order_status']) {
  232. if (1 == $params['order_status']) {
  233. // $search_object->whereExists('fansNum', '<', 20);
  234. $search_object->whereExists(function ($query) {
  235. $query->from('force_subscribe_users')
  236. ->whereRaw('send_order_id=send_orders.id and is_subscribed=1')->where(DB::raw('count(*)'), '<', 20);
  237. });
  238. } elseif (2 == $params['order_status']) {
  239. // $search_object->having('fansNum', '>=', 20);
  240. $search_object->whereExists(function ($query) {
  241. $query->from('force_subscribe_users')
  242. ->whereRaw('send_order_id=send_orders.id and is_subscribed=1')->where(DB::raw('count(*)'), '>=', 20);
  243. });
  244. }
  245. }*/
  246. if (isset($params['orderBy']) && $params['orderBy']) {
  247. $orderByType = 'desc';
  248. $orderColum = 'send_orders.created_at'; //排序的列
  249. if (isset($params['orderByType']) && $params['orderByType']) {
  250. if (2 == $params['orderByType']) {
  251. $orderByType = 'asc';
  252. }
  253. }
  254. //12小时充值
  255. if (1 == $params['orderBy']) {
  256. $orderColum = 'half_day_charge_amount';
  257. //7天充值充值
  258. } elseif (2 == $params['orderBy']) {
  259. $orderColum = 'weekend_charge_amount';
  260. //充值总额
  261. }
  262. $search_object->orderBy($orderColum, $orderByType);
  263. }
  264. if ($is_all) {
  265. return $search_object->get();
  266. } else {
  267. return $search_object->paginate();
  268. }
  269. }
  270. /**
  271. * 获取派单信息
  272. * @param $bookId 推广书籍id
  273. * @param $distribution_channel_id 推广渠道id
  274. * @param $name 派单名称
  275. * @param $bookName 书名
  276. * @param $sendOrderId 派单id
  277. * @param string $start_date 开始时间
  278. * @param string $end_date 结束时间
  279. * @param string $isAll 是否获取所有
  280. * @return mixed
  281. */
  282. static function search($params, $isAll = false)
  283. {
  284. $search_object = self::where('is_enable', 1)->orderBy('id', 'desc');
  285. if (isset($params['book_id']) && $params['book_id']) {
  286. $search_object->where('send_orders.book_id', $params['book_id']);
  287. }
  288. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
  289. $search_object->where('send_orders.distribution_channel_id', $params['distribution_channel_id']);
  290. }
  291. if (isset($params['book_name']) && $params['book_name']) {
  292. $book_ids = BookConfig::getIdByName($params['book_name']);
  293. if ($book_ids) {
  294. $bids = [];
  295. foreach ($book_ids as $item) {
  296. $bids[] = $item->bid;
  297. }
  298. $search_object->whereIn('send_orders.book_id', $bids);
  299. }
  300. }
  301. if (isset($params['import_company_name']) && $params['import_company_name']) {
  302. $search_object->where('send_orders.import_company_name', 'like', '%' . $params['import_company_name'] . '%');
  303. }
  304. if (isset($params['name']) && $params['name']) {
  305. $search_object->where('send_orders.name', 'like', '%' . $params['name'] . '%');
  306. }
  307. if (isset($params['start_time']) && $params['start_time']) {
  308. $search_object->where('send_orders.created_at', '>=', $params['start_time']);
  309. }
  310. if (isset($params['end_time']) && $params['end_time']) {
  311. $search_object->where('send_orders.created_at', '<=', $params['end_time']);
  312. }
  313. if (isset($params['send_time_start_time']) && $params['send_time_start_time']) {
  314. $search_object->where('send_orders.send_time', '>=', $params['send_time_start_time']);
  315. }
  316. if (isset($params['send_time_end_time']) && $params['send_time_end_time']) {
  317. $search_object->where('send_orders.send_time', '<=', $params['send_time_end_time']);
  318. }
  319. if (isset($params['pre_send_date_end']) && $params['pre_send_date_end']) {
  320. $search_object->where('send_orders.pre_send_date', '<=', $params['pre_send_date_end']);
  321. }
  322. if (isset($params['pre_send_date_start']) && $params['pre_send_date_start']) {
  323. $search_object->where('send_orders.pre_send_date', '>=', $params['pre_send_date_start']);
  324. }
  325. if (isset($params['start_send_time']) && $params['start_send_time']) {
  326. $search_object->where('send_orders.send_time', '>=', $params['start_send_time']);
  327. }
  328. if (isset($params['end_send_time']) && $params['end_send_time']) {
  329. $search_object->where('send_orders.send_time', '<=', $params['end_send_time']);
  330. }
  331. if (isset($params['promotion_type']) && $params['promotion_type']) {
  332. $search_object->where('send_orders.promotion_type', $params['promotion_type']);
  333. }
  334. if (isset($params['id']) && $params['id']) {
  335. $search_object->where('send_orders.id', $params['id']);
  336. }
  337. if (isset($params['promotion_point']) && $params['promotion_point']) {
  338. $search_object->where('send_orders.promotion_point', $params['promotion_point']);
  339. }
  340. // \Log::info('my_sql:'.($search_object->toSql()));
  341. if ($isAll) {
  342. return $search_object->get();
  343. } else {
  344. return $search_object->paginate();
  345. }
  346. }
  347. /**
  348. * 更新派单的备注
  349. * @param $id 派单id
  350. * @param $distribution_channel_id 渠道id
  351. * @param $remark 备注
  352. * @return mixed
  353. */
  354. static function updateRemark($id, $distribution_channel_id, $remark)
  355. {
  356. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['remark' => $remark]);
  357. }
  358. /**
  359. * 更新派单的星数
  360. * @param $id 派单id
  361. * @param $distribution_channel_id 渠道id
  362. * @param $starNum 星数
  363. * @return mixed
  364. */
  365. static function updateStarNum($id, $distribution_channel_id, $starNum)
  366. {
  367. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['star_num' => $starNum]);
  368. }
  369. /**
  370. * 更新派单的星数、备注
  371. * @param $id 派单id
  372. * @param $distribution_channel_id 渠道id
  373. * @param $starNum 星数
  374. * @param $remark 备注
  375. * @return mixed
  376. */
  377. static function updateStarNumAndRemark($id, $distribution_channel_id, $starNum, $remark)
  378. {
  379. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['star_num' => $starNum, 'remark' => $remark]);
  380. }
  381. /**
  382. * 更新派单
  383. * @param $id 派单id
  384. * @param $distribution_channel_id 渠道id
  385. * @param $name 派单名称
  386. * @param $cost 成本
  387. * @param $channel_type 派单渠道类型.(允许值: AUTHENTICATED, UNAUTHENTICATED)
  388. * @param $promotion_type 派单类型.(允许值: INTERNAL, EXTERNAL)
  389. * @return mixed
  390. */
  391. static function updateSendOrderInfo($id, $distribution_channel_id, $name, $pre_send_date, $channel_type, $cost, $promotion_type, $subscribe_chapter = [])
  392. {
  393. $object = self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id);
  394. $params = [];
  395. if ($name) {
  396. $params['name'] = $name;
  397. }
  398. if ($channel_type) {
  399. $params['channel_type'] = $channel_type;
  400. }
  401. if ($cost) {
  402. $params['cost'] = $cost;
  403. }
  404. if ($promotion_type) {
  405. $params['promotion_type'] = $promotion_type;
  406. }
  407. if ($pre_send_date) {
  408. $params['pre_send_date'] = $pre_send_date;
  409. }
  410. if ($subscribe_chapter) {
  411. $params['subscribe_chapter_id'] = $subscribe_chapter['subscribe_chapter_id'];
  412. $params['subscribe_chapter_seq'] = $subscribe_chapter['subscribe_chapter_seq'];
  413. $params['subscribe_chapter_name'] = $subscribe_chapter['subscribe_chapter_name'];
  414. }
  415. return $object->update($params);
  416. }
  417. /**
  418. * 创建推广派单(章节)
  419. */
  420. static function createFromChapter($data)
  421. {
  422. return self::create($data);
  423. }
  424. /**
  425. * 创建推广派单(页面)
  426. */
  427. static function createFromPage($data)
  428. {
  429. return self::create($data);
  430. }
  431. /**
  432. * 创建推广派单(目录)
  433. */
  434. static function createFromDirectory($data)
  435. {
  436. return self::create($data);
  437. }
  438. /**
  439. * 删除派单
  440. * @param $id 派单id
  441. * @param $distribution_channel_id 渠道id
  442. * @return
  443. */
  444. static function removeSendOrder($id, $distribution_channel_id)
  445. {
  446. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['is_enable' => 0]);
  447. }
  448. /**
  449. * 设置成本
  450. * @param $id 派单id
  451. * @param $distribution_channel_id 渠道id
  452. * @param $cost 成本
  453. * @return mixed
  454. */
  455. static function setSendOrderCost($id, $distribution_channel_id, $cost)
  456. {
  457. return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['cost' => $cost]);
  458. }
  459. /**
  460. * 获取派单下的首充用户数
  461. * @param $id 派单id
  462. */
  463. static function getFirstChargeUserNum($id)
  464. {
  465. $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");
  466. return ($result && isset($result[0]->num)) ? $result[0]->num : 0;
  467. }
  468. /**
  469. * 获取派单下的非首充用户数
  470. * @param $id 派单id
  471. */
  472. static function getRepetitiousChargeUserNum($id)
  473. {
  474. $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");
  475. return ($result && isset($result[0]->num)) ? $result[0]->num : 0;
  476. }
  477. /**
  478. * 获取实际派单数
  479. */
  480. static function getSendOrderCount($distribution_channel_id = '', $date)
  481. {
  482. $search_object = self::where('send_time', '>=', $date)->where('send_time', '<=', date('Y-m-d', strtotime($date) + 86400));
  483. if ($distribution_channel_id) {
  484. $search_object->where('distribution_channel_id', $distribution_channel_id);
  485. }
  486. return $search_object->count();
  487. }
  488. }