FinanceController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Models\Mongo\PayLogsModel;
  5. use App\Models\Mysql\WithDrawalLogModel;
  6. use App\Repositories\AgentBankInfoRepository;
  7. use App\Repositories\AgentRepository;
  8. use App\Repositories\BalanceLogRepository;
  9. use App\Repositories\Criterias\FinanceRechargeCriteria;
  10. use App\Repositories\Criterias\FinanceWithdrawCriteria;
  11. use App\Repositories\Criterias\OrderLimitCriteria;
  12. use App\Repositories\PayLogRepository;
  13. use App\Repositories\WithDrawalLogRepository;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Validator;
  17. use Modules\ManageApi\Responses\CommonSuccessUidResponse;
  18. use Modules\ManageApi\Responses\ConfigSuccessResponse;
  19. use Modules\ManageApi\Responses\FinanceRechargePageResponse;
  20. use Modules\ManageApi\Responses\FinanceWithdrawResponse;
  21. class FinanceController extends BaseController
  22. {
  23. /**
  24. * 充值记录
  25. *
  26. * @return \Illuminate\Http\JsonResponse|FinanceRechargePageResponse
  27. */
  28. public function rechargeLists()
  29. {
  30. /**
  31. * @var Validator $validatorFactory
  32. */
  33. $validatorFactory = app('validator');
  34. /**
  35. * @var PayLogRepository $payRepo
  36. */
  37. $payRepo = app(PayLogRepository::class);
  38. $inputData = $this->getJsonData();
  39. // 针对layui table自动分页请求参数,优先GET值.
  40. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  41. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  42. if (!is_null($queryPage)) {
  43. $inputData['page'] = $queryPage;
  44. }
  45. if (!is_null($queryPageSize)) {
  46. $inputData['page_size'] = $queryPageSize;
  47. }
  48. $rules = [
  49. 'page' => 'nullable|integer',
  50. 'page_size' => 'nullable|integer',
  51. 'user_uid' => 'nullable|string',
  52. 'user_name' => 'nullable|string',
  53. 'pay_type' => 'nullable|string',
  54. 'order_status' => 'nullable|integer',
  55. 'mom_app_uid' => 'nullable|string',
  56. 'order_sn' => 'nullable|string',
  57. 'start_date' => 'nullable|string',
  58. 'end_date' => 'nullable|string',
  59. ];
  60. $validator = $validatorFactory->make($inputData, $rules);
  61. if ($validator->fails()) {
  62. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  63. return json_fail($validator->errors()->first());
  64. }
  65. $orders = [
  66. ['created_at', 'DESC'],
  67. ];
  68. if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
  69. $pageSize = CommonConstant::PAGE_SIZE;
  70. } else {
  71. $pageSize = $inputData['page_size'];
  72. }
  73. $page = format_page($inputData['page'] ?? 0);
  74. $field = ['order_sn', 'user_uid', 'mom_app_uid', 'nick', 'agent_uid', 'real_amount', 'order_status', 'pay_channel', 'order_sn', 'created_at', 'paid_at', 'pay_type'];
  75. $res = $payRepo->pushCriteria(new FinanceRechargeCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, $field, $page);
  76. $recTotal = $this->rechargeTotal($inputData, $res);
  77. return new FinanceRechargePageResponse($res, $recTotal);
  78. }
  79. /**
  80. * 提现列表
  81. *
  82. * @return \Illuminate\Http\JsonResponse|FinanceWithdrawResponse
  83. */
  84. public function withdrawList()
  85. {
  86. /**
  87. * @var Validator $validatorFactory
  88. */
  89. $validatorFactory = app('validator');
  90. /**
  91. * @var WithDrawalLogRepository $witRepo
  92. */
  93. $witRepo = app(WithDrawalLogRepository::class);
  94. $inputData = $this->getJsonData();
  95. // 针对layui table自动分页请求参数,优先GET值.
  96. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  97. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  98. if (!is_null($queryPage)) {
  99. $inputData['page'] = $queryPage;
  100. }
  101. if (!is_null($queryPageSize)) {
  102. $inputData['page_size'] = $queryPageSize;
  103. }
  104. $rules = [
  105. 'page' => 'nullable|integer',
  106. 'page_size' => 'nullable|integer',
  107. 'agent_username' => 'nullable|string',
  108. 'status' => 'nullable|integer',
  109. 'type' => 'nullable|integer',
  110. 'start_date' => 'nullable|string',
  111. 'end_date' => 'nullable|string',
  112. 'order' => 'nullable|string',
  113. ];
  114. $validator = $validatorFactory->make($inputData, $rules);
  115. if ($validator->fails()) {
  116. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  117. return json_fail($validator->errors()->first());
  118. }
  119. if (empty($inputData['order'])) {
  120. $orders = [
  121. ['id', 'DESC']
  122. ];
  123. } else {
  124. $orderMap = explode('-', $inputData['order']);
  125. $orders = [[$orderMap[0], $orderMap[1]]];
  126. }
  127. if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
  128. $pageSize = CommonConstant::PAGE_SIZE;
  129. } else {
  130. $pageSize = $inputData['page_size'];
  131. }
  132. $page = format_page($inputData['page'] ?? 0);
  133. $res = $witRepo->pushCriteria(new FinanceWithdrawCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  134. $withdrawTotal = $this->withdrawTotal($inputData, $res);
  135. return new FinanceWithdrawResponse($res, $withdrawTotal);
  136. }
  137. /**
  138. * 提现操作
  139. *
  140. * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse
  141. * @throws \Exception
  142. */
  143. public function applyWithdraw()
  144. {
  145. $inputData = $this->getJsonData();
  146. $rules = [
  147. 'order_uid' => 'required|string',
  148. 'status' => 'required|integer|in:1,3'
  149. ];
  150. $validator = Validator::make($inputData, $rules);
  151. if ($validator->fails()) {
  152. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  153. return json_fail($validator->errors()->first());
  154. }
  155. /**
  156. * @var WithDrawalLogRepository $witRepo
  157. */
  158. $witRepo = app(WithDrawalLogRepository::class);
  159. /**
  160. * @var AgentRepository $witRepo
  161. */
  162. $agentRepo = app(AgentRepository::class);
  163. /**
  164. * @var BalanceLogRepository $bLogRepo
  165. */
  166. $bLogRepo = app(BalanceLogRepository::class);
  167. $witOrder = $witRepo->getOrderFirst($inputData['order_uid']);
  168. if (empty($witOrder)) {
  169. return json_fail('订单状态异常');
  170. }
  171. $agent = $agentRepo->findBy('uid', $witOrder->agent_uid);
  172. if (empty($agent)) {
  173. return json_fail('代理账户不存在');
  174. }
  175. $oldAgent = clone $agent;
  176. //开启事物
  177. DB::beginTransaction();
  178. if ($inputData['status'] == 1) { //拒绝
  179. $agent->balance += $witOrder->amount;
  180. }
  181. $agent->frozen -= $witOrder->amount;
  182. $res = $agent->save();
  183. //修改订单状态
  184. $res1 = $witRepo->update(['status' => $inputData['status'], 'paid_at' => date('Y-m-d H:i:s')], $inputData['order_uid'], 'uid');
  185. if (!$res || !$res1) {
  186. DB::rollBack();
  187. return json_fail('操作失败');
  188. }
  189. $res2 = $bLogRepo->record($agent, $oldAgent);
  190. if (!$res2) {
  191. DB::rollBack();
  192. return json_fail('帐变写入失败');
  193. }
  194. DB::commit();
  195. return new ConfigSuccessResponse();
  196. }
  197. /**
  198. * 删除提现银行卡
  199. *
  200. * @param AgentBankInfoRepository $bankRepo
  201. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  202. */
  203. public function withdrawBankDel(AgentBankInfoRepository $bankRepo)
  204. {
  205. $inputData = $this->getJsonData();
  206. $rules = [
  207. 'uid' => 'required|string',
  208. ];
  209. $validator = Validator::make($inputData, $rules);
  210. if ($validator->fails()) {
  211. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  212. return json_fail($validator->errors()->first());
  213. }
  214. $bankInfo = $bankRepo->findBy('uid', $inputData['uid']);
  215. if (empty($bankInfo)) {
  216. return json_fail(trans('common.err_resource_not_found'));
  217. }
  218. $oldBankInfo = clone $bankInfo;
  219. $result = $bankInfo->delete();
  220. if (!$result) {
  221. return json_fail('删除失败');
  222. } else {
  223. $logSubject = '删除一个提现银行卡,uid:' . $bankInfo->uid;
  224. $logSvalue = $bankInfo->uid;
  225. $logDbData = [
  226. 'after_items' => [
  227. ],
  228. 'before_items' => [
  229. $oldBankInfo->toArray(),
  230. ],
  231. ];
  232. $this->log($logSubject, $logSvalue, $logDbData);
  233. return new CommonSuccessUidResponse($bankInfo->uid);
  234. }
  235. }
  236. /**
  237. * 提现统计计算
  238. *
  239. * @param $inputData
  240. * @param $res
  241. * @return array
  242. */
  243. private function withdrawTotal($inputData, $res)
  244. {
  245. unset($inputData['page']);
  246. unset($inputData['page_size']);
  247. if (empty($inputData)) {
  248. $witTotal['unpaid_amount'] = WithDrawalLogModel::where('status', '<>', 3)->sum('amount');
  249. } else {
  250. $data = $res->toArray()['data'];
  251. $unpaidAmount = 0;
  252. foreach ($data as $val) {
  253. $unpaidAmount += $val['amount'];
  254. }
  255. $witTotal['today_amount'] = $unpaidAmount;
  256. }
  257. $witTotal['today_amount'] = WithDrawalLogModel::query()->where('status', 3)->whereBetween('created_at', [Carbon::parse(date('Y-m-d') . ' 00:00:00'), Carbon::parse(date('Y-m-d') . ' 23:59:59')])->sum('amount');
  258. return $witTotal;
  259. }
  260. /**
  261. * 充值统计计算(如果没有查询条件就统计当日的数据)
  262. *
  263. * @param $inputData
  264. * @param $res
  265. */
  266. private function rechargeTotal($inputData, $res)
  267. {
  268. unset($inputData['page']);
  269. unset($inputData['page_size']);
  270. $data = $res->toArray()['data'];
  271. if (empty($inputData)) {
  272. $field = ['order_status', 'real_amount'];
  273. $data = PayLogsModel::query()->whereBetween('created_at', [Carbon::parse(date('Y-m-d') . ' 00:00:00'), Carbon::parse(date('Y-m-d') . ' 23:59:59')])->get($field)->toArray();
  274. }
  275. $succInt = 0;
  276. $orderInt = 0;
  277. $recTotal['total_money'] = 0;
  278. $recTotal['succ_money'] = 0;
  279. foreach ($data as $v) {
  280. if ($v['order_status'] == 1) {
  281. $succInt++;
  282. $recTotal['succ_money'] += $v['real_amount'];
  283. }
  284. $recTotal['total_money'] += $v['real_amount'];
  285. $orderInt ++;
  286. }
  287. $recTotal['succ_int'] = $succInt;
  288. $recTotal['order_int'] = $orderInt;
  289. // 成功率 = 成功的订单 / 总订单
  290. if ($succInt == 0) {
  291. $recTotal['succ_rate'] = 0;
  292. } else {
  293. //精度问题 2 / 14 * 100
  294. $recTotal['succ_rate'] = floor(round($succInt / $orderInt, 2) * 100);
  295. }
  296. return $recTotal;
  297. }
  298. }