OfficialAccountBills.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers\Channel\Finance;
  3. use App\Http\Controllers\Channel\Finance\Transformers\OfficialAccountBillsTransformer;
  4. use App\Modules\Finance\Services\OfficialAccountBillsService;
  5. use DB;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Storage;
  8. class OfficialAccountBills extends BaseController
  9. {
  10. /**
  11. * @apiDefine Finance 结算提现模块
  12. */
  13. /**
  14. * @apiVersion 1.0.0
  15. * @apiDescription 服务号结算详情
  16. * @api {GET} OfficialAccountBills/getList 服务号结算详情
  17. * @apiGroup Finance
  18. * @apiName OfficialAccountBills-getList
  19. * @apiParam {String} start_time 开始时间.
  20. * @apiParam {String} end_time 结束时间.
  21. * @apiParam {String} export 导出(值给1就行export=1)
  22. * @apiSuccess {String} data.list.recharge_amount 充值金额.
  23. * @apiSuccess {String} data.list.date 日期.
  24. * @apiSuccess {String} data.list.nickname 服务号
  25. * @apiSuccess {String} data.meta.channel_recharge_amount total 渠道累计充值
  26. * @apiSuccess {String} data.meta.recharge_amount_in_30_days 30日内充值
  27. * @apiSuccess {String} data.meta.recharge_amount_in_60_days 60日内充值
  28. * @apiSuccess {String} data.meta.recharge_amount_in_90_days 90日内充值
  29. * @apiSuccess {String} data.meta.new_fans_num 新关粉丝数
  30. * @apiSuccess {String} data.meta.per_page per_page
  31. * @apiSuccess {String} data.meta.current_page current_page
  32. * @apiSuccess {String} data.meta.last_page last_page
  33. * @apiSuccess {String} data.meta.next_page_url next_page_url
  34. * @apiSuccess {String} data.meta.prev_page_url prev_page_url
  35. * @apiSuccessExample {json} Success-Response:
  36. *
  37. * {
  38. * "code": 0,
  39. * "msg": "",
  40. * "data":{
  41. * list:[
  42. * {
  43. * recharge_amount: "3145.00",
  44. * date: "2018-04-13",
  45. * appid: "wx1d6855c00d8e6500",
  46. * nickname: "南梦繁花",
  47. * channel_recharge_amount: "27680.00",
  48. * recharge_amount_in_30_days: 111,
  49. * recharge_amount_in_60_days: 111,
  50. * recharge_amount_in_90_days: 111,
  51. * new_fans_num: 40
  52. * },...
  53. * ]
  54. * meta:{}
  55. * }
  56. *
  57. */
  58. public function getList(Request $request)
  59. {
  60. $start_time = $request->input('start_time', date('Y-m-d'));
  61. $end_time = $request->input('end_time', date('Y-m-d', time() + 86400));
  62. $distribution_channel_id = $this->getChannelId();
  63. $official = $this->getOfficialAccount($distribution_channel_id);
  64. if (!$official)
  65. return response()->success();
  66. if ($request->input('export')) {
  67. $data = OfficialAccountBillsService::getBillsByOfficialAcount($distribution_channel_id, $start_time, $end_time, true);
  68. // $data = OrderService::OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time,500);
  69. } else {
  70. // $data = OrderService::OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time);
  71. $data = OfficialAccountBillsService::getBillsByOfficialAcount($distribution_channel_id, $start_time, $end_time, false);
  72. }
  73. // if ($data) {
  74. // foreach ($data as &$v) {
  75. // $v->nickname = isset($official[$v->appid]) ? $official[$v->appid] : '未知';
  76. // $amount = OrderService::OfficialAccountOrderSum($distribution_channel_id, $start_time, $end_time, $v->appid);
  77. // $v->total_amount = $amount;
  78. // }
  79. // }
  80. if ($request->input('export')) {
  81. $filename = date('YmdHis') . '.csv';
  82. Storage::append($filename, mb_convert_encoding("日期,服务号,充值金额,总额,新关粉丝数,30天内充值,60天内充值,90天内充值", 'gbk'));
  83. $str = '';
  84. if ($data) {
  85. foreach ($data as $val) {
  86. $val->new_fans_num = $val->new_fans_num ? $val->new_fans_num : 0;
  87. $val->recharge_amount_in_30_days = $val->recharge_amount_in_30_days ? $val->recharge_amount_in_30_days : 0;
  88. $val->recharge_amount_in_60_days = $val->recharge_amount_in_60_days ? $val->recharge_amount_in_60_days : 0;
  89. $val->recharge_amount_in_90_days = $val->recharge_amount_in_90_days ? $val->recharge_amount_in_90_days : 0;
  90. $str .= "{$val->date},{$val->nickname},{$val->recharge_amount},{$val->channel_recharge_amount},{$val->new_fans_num},{$val->recharge_amount_in_30_days},{$val->recharge_amount_in_60_days},{$val->recharge_amount_in_90_days}\r\n";
  91. }
  92. }
  93. Storage::append($filename, mb_convert_encoding($str, 'gbk'));
  94. return response()->download(storage_path('app/' . $filename))->deleteFileAfterSend(true);
  95. }
  96. return response()->pagination(new OfficialAccountBillsTransformer(), $data);
  97. }
  98. private function getOfficialAccount($distribution_channel_id)
  99. {
  100. if (empty($distribution_channel_id)) return [];
  101. $res = DB::table('official_accounts')->select('nickname', 'appid')->where('distribution_channel_id', $distribution_channel_id)->get();
  102. if ($res) {
  103. $data = [];
  104. foreach ($res as $v) {
  105. $data[$v->appid] = $v->nickname;
  106. }
  107. return $data;
  108. }
  109. return [];
  110. }
  111. }