BookPromotionController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace App\Http\Controllers\Manage\Book;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Manage\Book\Transformers\BookPromotionCheckedTransformer;
  5. use App\Http\Controllers\Manage\Book\Transformers\BookPromotionTransformer;
  6. use App\Modules\Book\Models\BookConfig;
  7. use App\Modules\Book\Models\BookPromotionChecked;
  8. use App\Modules\Book\Services\BookConfigService;
  9. use App\Modules\Book\Services\BookPromotionCheckedService;
  10. use App\Modules\Channel\Models\Channel;
  11. use App\Modules\Channel\Services\ChannelService;
  12. use App\Modules\Manage\Services\ManageService;
  13. use App\Modules\SendOrder\Models\SendOrder;
  14. use App\Modules\SendOrder\Models\SendOrderStat;
  15. use App\Modules\SendOrder\Services\SendOrderService;
  16. use App\Modules\SendOrder\Services\SendOrderStatService;
  17. use Cache;
  18. use Cookie;
  19. use DB;
  20. use Illuminate\Http\Request;
  21. use Storage;
  22. class BookPromotionController extends Controller
  23. {
  24. /**
  25. * @apiDefine BookPromotion 书籍推广
  26. */
  27. /**
  28. * 获取书籍推广数据
  29. * @param Request $request
  30. */
  31. public function getBookPromotionInfo(Request $request)
  32. {
  33. $result = [];
  34. $end_date = $request->input('end_date');
  35. $book_name = $request->input('book_name');
  36. $begin_date = $request->input('begin_date');
  37. $orderBy = $request->input('orderBy');
  38. $company_name = $request->input('company_name');
  39. $manageId = $request->session()->get('manage_auth');
  40. if ($manageId) {
  41. $manageModel = ManageService::getById($manageId);
  42. if ($manageModel) {
  43. $role = $manageModel->role;
  44. $channels = ($role == 'admin' || $role == 'business_leader') ? [] : ChannelService::getChannelIdList($manageId);
  45. $param = compact('company_name', 'book_name', 'begin_date', 'end_date', 'orderBy');
  46. $result = SendOrderStatService::getChannelPromotionBook($channels, $param, false);
  47. foreach ($result as &$item) {
  48. $company_id = $item->company_id;
  49. $channels = Channel::getChanneIdByCompanyId($company_id);
  50. $params = [
  51. 'channels' => $channels,
  52. 'book_id' => $item->bid,
  53. 'begin_time' => $item->date,
  54. 'end_time' => $item->date . ' 23:59:59'
  55. ];
  56. if (!$item->uv_three_day) {
  57. $item->uv_three_day = 0;
  58. }
  59. if (!$item->percent_recharge_devide_uv_three_day) {
  60. $item->percent_recharge_devide_uv_three_day = 0;
  61. }
  62. $item->send_order_count = SendOrderService::getPromotionCount($params);
  63. $item->percent_recharge_devide_uv = number_format($item->percent_recharge_devide_uv, 2);
  64. }
  65. }
  66. }
  67. return response()->pagination(new BookPromotionTransformer(), $result);
  68. }
  69. /**
  70. * 设置当期推广的书籍的信息
  71. * @param Request $request
  72. * @return mixed
  73. */
  74. public function setPromotionStatus_v1(Request $request)
  75. {
  76. $cacheKey = 'setPromotionStatus';
  77. $book_id = $request->input('book_id');
  78. $promotion_status = $request->input('promotion_status', 0);
  79. $tempBookString = '';
  80. $exist = Cache::get($cacheKey, '');
  81. if (1 == $promotion_status) {
  82. $tempBookString = $exist . ',' . $book_id;
  83. Cache::put($cacheKey, $tempBookString, 3 * 24 * 60);
  84. } else {
  85. $book_ids = explode(',', $exist);
  86. foreach ($book_ids as $book_id_item) {
  87. if (!empty($book_id_item) && $book_id_item != $book_id) {
  88. $tempBookString = $tempBookString . ',' . $book_id_item;
  89. }
  90. if ($book_id_item == $book_id) {
  91. Cache::forget('infos' . $book_id);
  92. }
  93. }
  94. Cache::put($cacheKey, $tempBookString, 3 * 24 * 60);
  95. }
  96. return response()->success(['promotion_status' => $promotion_status, 'tempBookString' => $tempBookString]);
  97. }
  98. public function setPromotionStatus(Request $request)
  99. {
  100. //$cacheKey = 'setPromotionStatus';
  101. $book_id = $request->input('book_id');
  102. $promotion_status = $request->input('promotion_status', 0);
  103. if (1 == $promotion_status) {
  104. BookConfig::where('bid', $book_id)->update(['is_current_week_promotion' => 1]);
  105. } else {
  106. BookConfig::where('bid', $book_id)->update(['is_current_week_promotion' => 0]);
  107. }
  108. return response()->success(['promotion_status' => $promotion_status]);
  109. }
  110. public function commitPromotionStatus(Request $request)
  111. {
  112. $cacheKey = 'setPromotionStatus';
  113. $bidString = Cache::get($cacheKey, '');
  114. $book_ids = explode(',', $bidString);
  115. $period = BookPromotionCheckedService::getMaxPeriod();
  116. foreach ($book_ids as $book_id) {
  117. if ($book_id) {
  118. $book = Cache::get('infos' . $book_id);
  119. if ($book) {
  120. $book = json_decode($book, true);
  121. $book['onshelf_date'] = $book['created_at'];
  122. unset($book['created_at']);
  123. $book['periods'] = $period;
  124. BookPromotionCheckedService::create($book);
  125. Cache::forget('infos' . $book_id);
  126. }
  127. }
  128. }
  129. Cache::forget($cacheKey);
  130. return response()->success();
  131. }
  132. public function getPromotionBooksUnCheck(Request $request)
  133. {
  134. $res = [];
  135. $exist = Cache::get('setPromotionStatus', '');
  136. $operator = unserialize($request->session()->get('manage_user'))->nickname;
  137. $book_ids = explode(',', $exist);
  138. foreach ($book_ids as $book_id) {
  139. $book_info = Cache::get('infos' . $book_id);
  140. if ($book_info) {
  141. $book_info = json_decode($book_info, true);
  142. } else {
  143. $book_info = $this->getBookPromotionData($operator, $book_id);
  144. }
  145. if ($book_info) {
  146. $res[] = $book_info;
  147. }
  148. }
  149. return response()->success($res);
  150. }
  151. public function getPromotionBooksChecked(Request $request)
  152. {
  153. $periods = $request->input('periods');
  154. $result = BookPromotionChecked::getInfo($periods, false);
  155. return response()->pagination(new BookPromotionCheckedTransformer(), $result);
  156. }
  157. private function getBookPromotionData($operator, $book_id)
  158. {
  159. $book_config = BookConfigService::getBookById($book_id);
  160. if ($book_config) {
  161. $data = [];
  162. $data['book_id'] = $book_id;
  163. $data['operator'] = $operator;
  164. $data['size'] = $book_config->size;
  165. $data['status'] = $book_config->status;
  166. $data['author'] = $book_config->author;
  167. $data['book_name'] = $book_config->book_name;
  168. $data['cp_source'] = $book_config->cp_source;
  169. $data['created_at'] = date("Y-m-d H:i:s", strtotime($book_config->created_at));
  170. $data['is_on_shelf'] = $book_config->is_on_shelf;
  171. $now_time = date('Y-m-d H:i:s', strtotime(time()));
  172. $send_30_day_time = date('Y-m-d H:i:s', strtotime(time()) - 86400 * 30);
  173. $recharge_amount_in_24h_outside = SendOrderStat::getRechargeAmountByBookId(['book_id' => $book_id, 'start_time' => $send_30_day_time, 'end_time' => $now_time], true, true);
  174. $recharge_amount_in_24h_inside = SendOrderStat::getRechargeAmountByBookId(['book_id' => $book_id, 'start_time' => $send_30_day_time, 'end_time' => $now_time], false, true);
  175. $recharge_amount_in_72h_inside = SendOrderStat::getRechargeAmountByBookId(['book_id' => $book_id, 'start_time' => $send_30_day_time, 'end_time' => $now_time], false, false);
  176. $recharge_amount_in_72h_outside = SendOrderStat::getRechargeAmountByBookId(['book_id' => $book_id, 'start_time' => $send_30_day_time, 'end_time' => $now_time], true, false);
  177. //获取一个月内的总派单书
  178. $promotionCount = SendOrder::getDuringPromotionCountByBid($book_id, $send_30_day_time, $now_time);
  179. if (is_numeric($promotionCount) && $promotionCount > 0) {
  180. $recharge_amount_in_24h_outside = $recharge_amount_in_24h_outside / $promotionCount;
  181. $recharge_amount_in_24h_inside = $recharge_amount_in_24h_inside / $promotionCount;
  182. $recharge_amount_in_72h_inside = $recharge_amount_in_72h_inside / $promotionCount;
  183. $recharge_amount_in_72h_outside = $recharge_amount_in_72h_outside / $promotionCount;
  184. }
  185. $data['recharge_amount_in_24h_outside'] = $recharge_amount_in_24h_outside ? $recharge_amount_in_24h_outside : 0;
  186. $data['recharge_amount_in_24h_inside'] = $recharge_amount_in_24h_inside ? $recharge_amount_in_24h_inside : 0;
  187. $data['recharge_amount_in_72h_inside'] = $recharge_amount_in_72h_inside ? $recharge_amount_in_72h_inside : 0;
  188. $data['recharge_amount_in_72h_outside'] = $recharge_amount_in_72h_outside ? $recharge_amount_in_72h_outside : 0;
  189. Cache::put("infos" . $book_id, json_encode($data), 3 * 24 * 60);
  190. return $data;
  191. } else {
  192. return null;
  193. }
  194. }
  195. /**
  196. * 导出书籍推广数据
  197. * @param Request $request
  198. */
  199. public function exportBookPromotionInfo(Request $request)
  200. {
  201. set_time_limit(0);
  202. $end_date = $request->input('end_date');
  203. $book_name = $request->input('book_name');
  204. $begin_date = $request->input('begin_date');
  205. $orderBy = $request->input('orderBy');
  206. $company_name = $request->input('company_name');
  207. $manage_id = $request->session()->get('manage_auth');
  208. if ($manage_id) {
  209. $manageModel = ManageService::getById($manage_id);
  210. if ($manageModel) {
  211. $role = $manageModel->role;
  212. $channels = ($role == 'admin' || $role == 'business_leader') ? [] : ChannelService::getChannelIdList($manage_id);
  213. $param = compact('company_name', 'book_name', 'begin_date', 'end_date', 'orderBy');
  214. $result = SendOrderStatService::getChannelPromotionBook($channels, $param, true);
  215. header("Content-type:application/vnd.ms-excel");
  216. header("Content-Disposition:attachment;filename=" . "明细" . date("YmdHis") . ".csv");
  217. echo iconv("UTF-8", "GBK", "\"日期\",\"公司名称\",\"书本名称\",\"书本id\",\"24小时充值\",\"72小时充值\",\"注册用户数\",\"24小时UV\",\"24小时充值/24小时UV\",\"72小时UV\",\"72小时充值/72小时UV\",\"当日创建派单数\"\r\n");
  218. foreach ($result as $item) {
  219. $company_id = $item->company_id;
  220. $channels = Channel::getChanneIdByCompanyId($company_id);
  221. $params = [
  222. 'channels' => $channels,
  223. 'book_id' => $item->bid,
  224. 'begin_time' => $item->date,
  225. 'end_time' => $item->date . ' 23:59:59'
  226. ];
  227. $item->uv_three_day = $item->uv_three_day ? $item->uv_three_day : 0;
  228. if (!$item->percent_recharge_devide_uv_three_day) {
  229. $item->percent_recharge_devide_uv_three_day = 0;
  230. }
  231. $item->send_order_count = SendOrderService::getPromotionCount($params);
  232. $item->percent_recharge_devide_uv = number_format($item->percent_recharge_devide_uv, 2);
  233. echo("\"" . mb_convert_encoding($item->date, "GBK", "UTF-8") . "\",");
  234. echo("\"" . mb_convert_encoding($item->company_name, "GBK", "UTF-8") . "\",");
  235. echo("\"" . mb_convert_encoding($item->book_name, "GBK", "UTF-8") . "\",");
  236. echo("\"" . mb_convert_encoding($item->bid, "GBK", "UTF-8") . "\",");
  237. echo("\"" . mb_convert_encoding($item->recharge_amount_in_one_day, "GBK", "UTF-8") . "\",");
  238. echo("\"" . mb_convert_encoding($item->recharge_amount_in_three_days, "GBK", "UTF-8") . "\",");
  239. echo("\"" . mb_convert_encoding($item->register_user_num, "GBK", "UTF-8") . "\",");
  240. echo("\"" . mb_convert_encoding($item->uv_one_day, "GBK", "UTF-8") . "\",");
  241. echo("\"" . mb_convert_encoding($item->percent_recharge_devide_uv, "GBK", "UTF-8") . "\",");
  242. echo("\"" . mb_convert_encoding($item->uv_three_day, "GBK", "UTF-8") . "\",");
  243. echo("\"" . mb_convert_encoding($item->percent_recharge_devide_uv_three_day, "GBK", "UTF-8") . "\",");
  244. echo("\"" . $item->send_order_count . "\"\r\n");
  245. }
  246. }
  247. }
  248. exit();
  249. }
  250. }