SendOrder.php 21 KB

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