ConsumerController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2018/1/9
  6. * Time: 13:37
  7. */
  8. namespace App\Http\Controllers\Manage\Trade;
  9. use App\Http\Controllers\Controller;
  10. use App\Modules\Product\Models\Product;
  11. use App\Modules\Subscribe\Services\BookOrderService;
  12. use App\Modules\Subscribe\Services\ChapterOrderService;
  13. use App\Modules\Trade\Services\OrderService;
  14. use Illuminate\Http\Request;
  15. class ConsumerController extends Controller
  16. {
  17. /**
  18. * @apiDefine Trade 交易订单
  19. */
  20. /**
  21. * @apiVersion 1.0.0
  22. * @api {GET} trade/getBookConsumerInfoByUserId 获取用户的消费的书本信息
  23. * @apiGroup Trade
  24. * @apiName getBookConsumerInfoByUserId
  25. * @apiParam {Number} [userId] 用户id(可不传)
  26. * @apiParam {String} [start_time] 开始时间(可不传)
  27. * @apiParam {String} [end_time] 结束时间(可不传)
  28. *
  29. * @apiSuccess {Number} id 订购id.
  30. * @apiSuccess {Number} distribution_channel_id 渠道id
  31. * @apiSuccess {Number} bid 书本id
  32. * @apiSuccess {String} book_name 图书名称
  33. * @apiSuccess {Number} uid 用户id
  34. * @apiSuccess {Number} u u参数
  35. * @apiSuccess {Number} fee 订购费用
  36. * @apiSuccess {String} created_at 创建时间
  37. * @apiSuccess {String} updated_at 更新时间
  38. * @apiSuccess {Number} send_order_id 派单id
  39. * @apiSuccess {Number} charge_balance 充值币余额
  40. * @apiSuccess {Number} reward_balance 奖励币余额
  41. * @apiSuccess {Number} total_get_amount 累计获取书币
  42. * @apiSuccess {Number} total_consume_amount 累计消费书币
  43. * @apiSuccessExample {json} Success-Response:
  44. *
  45. * {
  46. * "code": 0,
  47. * "msg": "",
  48. * "data": {
  49. * "list": {
  50. * "current_page": 1,
  51. * "data": [
  52. * {
  53. * "id": 6,
  54. * "distribution_channel_id": 1,
  55. * "bid": 256,
  56. * "book_name": "金牌红娘",
  57. * "uid": 1,
  58. * "u": 123545,
  59. * "fee": "1.00",
  60. * "created_at": "2017-12-02 19:15:56",
  61. * "updated_at": "2017-12-01 15:26:50",
  62. * "send_order_id": 5,
  63. * "charge_balance": 44,
  64. * "reward_balance": 66
  65. * },
  66. * {
  67. * "id": 6,
  68. * "distribution_channel_id": 1,
  69. * "bid": 256,
  70. * "book_name": "金牌红娘",
  71. * "uid": 1,
  72. * "u": 123545,
  73. * "fee": "1.00",
  74. * "created_at": "2017-12-02 19:15:56",
  75. * "updated_at": "2017-12-01 15:26:50",
  76. * "send_order_id": 5,
  77. * "charge_balance": 44,
  78. * "reward_balance": 66
  79. * }
  80. * ],
  81. * "first_page_url": "http://test.manage.zhuishuyun.com/api/trade/getBookConsumerInfoByUserId?page=1",
  82. * "from": 1,
  83. * "last_page": 1,
  84. * "last_page_url": "http://test.manage.zhuishuyun.com/api/trade/getBookConsumerInfoByUserId?page=1",
  85. * "next_page_url": null,
  86. * "path": "http://test.manage.zhuishuyun.com/api/trade/getBookConsumerInfoByUserId",
  87. * "per_page": 15,
  88. * "prev_page_url": null,
  89. * "to": 1,
  90. * "total": 1
  91. * },
  92. * "total_info": {
  93. * "total_get_amount": 1111,
  94. * "total_consumer_amount": 1111
  95. * }
  96. * }
  97. * }
  98. */
  99. function getBookConsumerInfoByUserId(Request $request)
  100. {
  101. $userId = $request->has('userId') ? $request->input('userId') : '';
  102. $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Y-m-d', strtotime($request->input('end_time'))) : '';
  103. $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Y-m-d', strtotime($request->input('start_time'))) : '';
  104. if (empty($userId)) {
  105. return response()->error('PARAM_EMPTY');
  106. } else {
  107. $bookOrderResult = BookOrderService::getRecordByUid($userId, $start_time, $end_time, false);
  108. $bookOrderResult->total_get_amount = $this->getUserTotalBalanceInfo($userId, $start_time, $end_time);
  109. $bookOrderResult->total_consume_amount = $this->getUserConsumeInfo($userId, $start_time, $end_time);
  110. }
  111. return response()->success(['list' => $bookOrderResult, 'total_info' => ['total_get_amount' => $bookOrderResult->total_get_amount, 'total_consume_amount' => $bookOrderResult->total_consume_amount]]);
  112. }
  113. /**
  114. * 获取一段时间内的用户的消费总额
  115. * @param $userId 用户id
  116. * @param $start_time 开始时间
  117. * @param $end_time 结束时间
  118. * @return mixed
  119. */
  120. function getUserConsumeInfo($userId, $start_time, $end_time)
  121. {
  122. $bookOrderFeeAmount = BookOrderService::getBookTotalConsumeByUserIdAndTime($userId, $start_time, $end_time);
  123. $chapterOrderFeeAmount = ChapterOrderService::getChapterTotalConsumeByUserIdAndTime($userId, $start_time, $end_time);
  124. return $bookOrderFeeAmount + $chapterOrderFeeAmount;
  125. }
  126. /**
  127. * 获取一段时间内的用户的累计获取书币
  128. * @param $userId 用户id
  129. * @param $start_time 开始时间
  130. * @param $end_time 结束时间
  131. * @return mixed
  132. */
  133. function getUserTotalBalanceInfo($userId, $start_time, $end_time)
  134. {
  135. $params = [];
  136. $params['uid'] = $userId;
  137. $params['status'] = 'PAID';
  138. $params['order_type'] = 'RECHARGE';
  139. $params['end_time'] = $end_time;
  140. $params['begin_time'] = $start_time;
  141. $givenAmount = 0;
  142. $orderResult = OrderService::search($params, true);
  143. foreach ($orderResult as $item) {
  144. $product_id = $item['product_id'];
  145. $result = Product::getProductGivenAmount($product_id);
  146. $amount = $result['price'] * 100 + $result['given'];
  147. $givenAmount += $amount;
  148. }
  149. return $givenAmount;
  150. }
  151. /**
  152. * @apiVersion 1.0.0
  153. * @api {GET} trade/getChapterConsumerInfoByUserId 获取用户的消费的章节信息
  154. * @apiGroup Trade
  155. * @apiName getChapterConsumerInfoByUserId
  156. * @apiParam {Number} [userId] 用户id(可不传)
  157. * @apiParam {String} [start_time] 开始时间(可不传)
  158. * @apiParam {String} [end_time] 结束时间(可不传)
  159. *
  160. * @apiSuccess {Number} id 订购id.
  161. * @apiSuccess {Number} distribution_channel_id 渠道id
  162. * @apiSuccess {Number} bid 书本id
  163. * @apiSuccess {Number} cid 章节id
  164. * @apiSuccess {String} book_name 图书名称
  165. * @apiSuccess {String} chapter_name 章节名称
  166. * @apiSuccess {Number} uid 用户id
  167. * @apiSuccess {Number} u u参数
  168. * @apiSuccess {Number} fee 订购费用
  169. * @apiSuccess {String} created_at 创建时间
  170. * @apiSuccess {String} updated_at 更新时间
  171. * @apiSuccess {Number} send_order_id 派单id
  172. * @apiSuccess {Number} charge_balance 充值币余额
  173. * @apiSuccess {Number} reward_balance 奖励币余额
  174. * @apiSuccess {Number} total_get_amount 累计获取书币
  175. * @apiSuccess {Number} total_consume_amount 累计消费书币
  176. * @apiSuccessExample {json} Success-Response:
  177. *
  178. * {
  179. * "code": 0,
  180. * "msg": "",
  181. * "data": {
  182. * "list": {
  183. * "current_page": 1,
  184. * "data": [
  185. * {
  186. * "id": 6,
  187. * "distribution_channel_id": 1,
  188. * "bid": 256,
  189. * "cid": 122,
  190. * "chapter_name": "谁谁谁"
  191. * "book_name": "金牌红娘",
  192. * "uid": 1,
  193. * "u": 123545,
  194. * "fee": "1.00",
  195. * "created_at": "2017-12-02 19:15:56",
  196. * "updated_at": "2017-12-01 15:26:50",
  197. * "send_order_id": 5,
  198. * "charge_balance": 44,
  199. * "reward_balance": 66
  200. * },
  201. * {
  202. * "id": 6,
  203. * "distribution_channel_id": 1,
  204. * "bid": 256,
  205. * "cid": 122,
  206. * "chapter_name": "谁谁谁"
  207. * "book_name": "金牌红娘",
  208. * "uid": 1,
  209. * "u": 123545,
  210. * "fee": "1.00",
  211. * "created_at": "2017-12-02 19:15:56",
  212. * "updated_at": "2017-12-01 15:26:50",
  213. * "send_order_id": 5,
  214. * "charge_balance": 44,
  215. * "reward_balance": 66
  216. * }
  217. * ],
  218. * "first_page_url": "http://test.manage.zhuishuyun.com/api/trade/getBookConsumerInfoByUserId?page=1",
  219. * "from": 1,
  220. * "last_page": 1,
  221. * "last_page_url": "http://test.manage.zhuishuyun.com/api/trade/getBookConsumerInfoByUserId?page=1",
  222. * "next_page_url": null,
  223. * "path": "http://test.manage.zhuishuyun.com/api/trade/getBookConsumerInfoByUserId",
  224. * "per_page": 15,
  225. * "prev_page_url": null,
  226. * "to": 1,
  227. * "total": 1
  228. * },
  229. * "total_info": {
  230. * "total_get_amount": 1111,
  231. * "total_consumer_amount": 1111
  232. * }
  233. * }
  234. * }
  235. */
  236. function getChapterConsumerInfoByUserId(Request $request)
  237. {
  238. $userId = $request->has('userId') ? $request->input('userId') : '';
  239. $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Y-m-d', strtotime($request->input('end_time'))) : '';
  240. $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Y-m-d', strtotime($request->input('start_time'))) : '';
  241. if (empty($userId)) {
  242. return response()->error('PARAM_EMPTY');
  243. } else {
  244. $chapterOrderResult = ChapterOrderService::getRecordByUid($userId, $start_time, $end_time, false);
  245. $chapterOrderResult->total_get_amount = $this->getUserTotalBalanceInfo($userId, $start_time, $end_time);
  246. $chapterOrderResult->total_consume_amount = $this->getUserConsumeInfo($userId, $start_time, $end_time);
  247. }
  248. return response()->success(['list' => $chapterOrderResult, 'total_info' => ['total_get_amount' => $chapterOrderResult->total_get_amount, 'total_consume_amount' => $chapterOrderResult->total_consume_amount]]);
  249. }
  250. /**
  251. * 导出用户的按本订购的消费记录
  252. * @param Request $request
  253. * @return mixed
  254. */
  255. function exportUserBookConsumerInfo(Request $request)
  256. {
  257. $userId = $request->has('userId') ? $request->input('userId') : '';
  258. $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Y-m-d', strtotime($request->input('end_time'))) : '';
  259. $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Y-m-d', strtotime($request->input('start_time'))) : '';
  260. if (empty($userId)) {
  261. return response()->error('PARAM_EMPTY');
  262. } else {
  263. $bookOrderResult = BookOrderService::getRecordByUid($userId, $start_time, $end_time, true);
  264. header("Content-type:application/vnd.ms-excel");
  265. header("Content-Disposition:attachment;filename=" . "用户书本订购记录" . date("YmdHis") . ".csv");
  266. echo iconv("UTF-8", "GBK", "\"id单号\",\"渠道id\",\"书本id\",\"图书名称\",\"用户id\",\"u参数\",\"订购费用\",\"创建时间\",\"更新时间\",\"充值币余额\",\"奖励币余额\",\"派单id\"\r\n");
  267. if ($bookOrderResult) {
  268. foreach ($bookOrderResult as $item) {
  269. echo("\"" . iconv("UTF-8", "GBK", '`' . $item->id) . "\",");
  270. echo("\"" . $item->distribution_channel_id . "\",");
  271. echo("\"" . $item->bid . "\",");
  272. echo("\"" . iconv("UTF-8", "GBK", $item->book_name) . "\",");
  273. echo("\"" . $item->uid . "\",");
  274. echo("\"" . $item->u . "\",");
  275. echo("\"" . $item->fee . "\",");
  276. echo("\"" . iconv("UTF-8", "GBK", date('Y-m-d H:i:s', strtotime($item->created_at))) . "\",");
  277. echo("\"" . iconv("UTF-8", "GBK", date('Y-m-d H:i:s', strtotime($item->updated_at))) . "\",");
  278. echo("\"" . $item->charge_balance . "\",");
  279. echo("\"" . $item->reward_balance . "\",");
  280. echo("\"" . $item->send_order_id . "\"\r\n");
  281. }
  282. }
  283. exit();
  284. }
  285. }
  286. /**
  287. *
  288. * 导出用户的按章节订购的消费记录
  289. * @param Request $request
  290. * @return mixed
  291. */
  292. function exportUserChapterConsumerInfo(Request $request)
  293. {
  294. $userId = $request->has('userId') ? $request->input('userId') : '';
  295. $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Y-m-d', strtotime($request->input('end_time'))) : '';
  296. $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Y-m-d', strtotime($request->input('start_time'))) : '';
  297. if (empty($userId)) {
  298. return response()->error('PARAM_EMPTY');
  299. } else {
  300. $chapterOrderResult = ChapterOrderService::getRecordByUid($userId, $start_time, $end_time, true);
  301. header("Content-type:application/vnd.ms-excel");
  302. header("Content-Disposition:attachment;filename=" . "用户章节订购记录" . date("YmdHis") . ".csv");
  303. echo iconv("UTF-8", "GBK", "\"id单号\",\"渠道id\",\"书本id\",\"图书名称\",\"章节id\",\"章节名称\",\"用户id\",\"u参数\",\"订购费用\",\"创建时间\",\"更新时间\",\"充值币余额\",\"奖励币余额\",\"派单id\"\r\n");
  304. if ($chapterOrderResult) {
  305. foreach ($chapterOrderResult as $item) {
  306. echo("\"" . iconv("UTF-8", "GBK", '`' . $item->id) . "\",");
  307. echo("\"" . $item->distribution_channel_id . "\",");
  308. echo("\"" . $item->bid . "\",");
  309. echo("\"" . iconv("UTF-8", "GBK", $item->book_name) . "\",");
  310. echo("\"" . $item->cid . "\",");
  311. echo("\"" . iconv("UTF-8", "GBK", $item->chapter_name) . "\",");
  312. echo("\"" . $item->uid . "\",");
  313. echo("\"" . $item->u . "\",");
  314. echo("\"" . $item->fee . "\",");
  315. echo("\"" . iconv("UTF-8", "GBK", date('Y-m-d H:i:s', strtotime($item->created_at))) . "\",");
  316. echo("\"" . iconv("UTF-8", "GBK", date('Y-m-d H:i:s', strtotime($item->updated_at))) . "\",");
  317. echo("\"" . $item->charge_balance . "\",");
  318. echo("\"" . $item->reward_balance . "\",");
  319. echo("\"" . $item->send_order_id . "\"\r\n");
  320. }
  321. }
  322. exit();
  323. }
  324. }
  325. }