RechargeLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace App\Http\logic\admin;
  3. use App\Facade\TableName;
  4. use App\Http\logic\api\BaseApiLogic;
  5. use App\Models\Platform\MonthCard;
  6. use Illuminate\Support\Facades\DB;
  7. class RechargeLogic extends BaseApiLogic
  8. {
  9. public function config($method)
  10. {
  11. $list = DB::connection('write')->table('agent.dbo.admin_configs as ac')
  12. ->leftJoin('agent.dbo.admin_users as u', 'ac.admin_id', '=', 'u.id')
  13. ->select('ac.*', 'u.account')
  14. ->where('ac.type', 'pay')
  15. ->where('ac.new_pay_type', $method)
  16. ->get();
  17. // 支付渠道列表
  18. $pay_list = DB::connection('write')->table('agent.dbo.admin_configs')
  19. ->where('status', 1)
  20. ->where('type', 'pay')
  21. ->pluck('name', 'id');
  22. // 开关状态
  23. $first = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  24. ->where('StatusName', 'FirstChargeGiftBagStatus')
  25. ->first();
  26. // 首充
  27. $firstCharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  28. ->where('StatusName', 'FirstChargeGiftBag')
  29. ->first();
  30. $poll = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  31. ->where('StatusName', 'PollSwitch')
  32. ->first();
  33. return compact('list', 'pay_list', 'first', 'firstCharge', 'poll');
  34. }
  35. // 控制开关
  36. public function switch_control($id, $type, $admin_id)
  37. {
  38. switch ($type) {
  39. case 'off': // 关闭
  40. // 不能全部关闭,至少保留一个
  41. $query = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->where('status', 1)->count();
  42. if ($query <= 1) {
  43. $this->error = '至少开启一个支付渠道';
  44. return false;
  45. }
  46. $res = DB::table('agent.dbo.admin_configs')->where('id', $id)->update([
  47. 'status' => -1,
  48. 'updated_at' => date('Y-m-d H:i:s'),
  49. 'admin_id' => $admin_id
  50. ]);
  51. $list = DB::table('agent.dbo.recharge_gear')->get();
  52. foreach ($list as &$value) {
  53. try {
  54. $gear = \GuzzleHttp\json_decode($value->gear, true);
  55. foreach ($gear as &$val) {
  56. if ($val['id'] == $id) {
  57. $val['status'] = -1;
  58. }
  59. }
  60. $gear = \GuzzleHttp\json_encode($gear);
  61. DB::table('agent.dbo.recharge_gear')->where('id', $value->id)->update(['gear' => $gear]);
  62. }catch (\Exception $e) {
  63. }
  64. }
  65. break;
  66. case 'on': // 开启
  67. $res = DB::table('agent.dbo.admin_configs')->where('id', $id)->update([
  68. 'status' => 1,
  69. 'updated_at' => date('Y-m-d H:i:s'),
  70. 'admin_id' => $admin_id
  71. ]);
  72. $list = DB::table('agent.dbo.recharge_gear')->get();
  73. foreach ($list as &$value) {
  74. try {
  75. $gear = \GuzzleHttp\json_decode($value->gear, true);
  76. foreach ($gear as &$val) {
  77. if ($val['id'] == $id) {
  78. $val['status'] = 1;
  79. }
  80. }
  81. $gear = \GuzzleHttp\json_encode($gear);
  82. DB::table('agent.dbo.recharge_gear')->where('id', $value->id)->update(['gear' => $gear]);
  83. }catch (\Exception $e){
  84. }
  85. }
  86. break;
  87. default:
  88. $this->error = '类型错误';
  89. return false;
  90. }
  91. if (isset($res) && $res) {
  92. return true;
  93. } else {
  94. $this->error = '操作失败';
  95. return false;
  96. }
  97. }
  98. // 充值档位
  99. public function gear_list($first_pay_status = 0)
  100. {
  101. $where = [];
  102. $where[] = ['first_pay', $first_pay_status];
  103. $list = DB::table('agent.dbo.recharge_gear')
  104. ->where($where)
  105. ->orderBy('money')
  106. ->get();
  107. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  108. $payMethodsNames = [
  109. 'cashapp' => 'CashApp',
  110. 'paypal' => 'PayPal',
  111. 'applepay' => 'ApplePay',
  112. 'googlepay' => 'GooglePay',
  113. 'usdt-trc20' => 'USDT-TRC20',
  114. 'usdt-erc20' => 'USDT-ERC20',
  115. 'usdc-trc20' => 'USDC-TRC20',
  116. 'usdc-erc20' => 'USDC-ERC20',
  117. 'btcpay' => 'BTCPay',
  118. ];
  119. foreach ($list as &$value) {
  120. $name = [];
  121. if (!empty($value->gear)) {
  122. $gear = \GuzzleHttp\json_decode($value->gear, true);
  123. if (is_array($gear)) {
  124. foreach ($gear as $val) {
  125. // 新格式:支付方式位掩码
  126. if (isset($val['name']) && isset($val['status']) && $val['status'] == 1) {
  127. $name[] = $payMethodsNames[$val['name']] ?? $val['name'];
  128. }
  129. // 旧格式:支付渠道ID(兼容)
  130. elseif (isset($val['id']) && ($val['status'] ?? 0) == 1) {
  131. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  132. }
  133. }
  134. }
  135. }
  136. $value->gear = implode(', ', $name);
  137. }
  138. // dd($list);
  139. // // 首充档位配置
  140. // $first_pay = DB::table('agent.dbo.recharge_gear')
  141. // ->where('first_pay', 1)
  142. // ->first();
  143. // $name = [];
  144. // if (!empty($first_pay->gear)) {
  145. // $gear = \GuzzleHttp\json_decode($first_pay->gear, true);
  146. // foreach ($gear as $val) {
  147. // if ($val['status'] == 1) {
  148. // $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  149. // }
  150. // }
  151. // }
  152. // $first_pay->gear = implode(',', $name);
  153. $first_pay = [];
  154. $RecomendarRecharge = [];
  155. $monthCard = [];
  156. // 推荐充值档位
  157. // $RecomendarRecharge = DB::table(TableName::QPAccountsDB() . 'SystemStatusInfo')
  158. // ->where('StatusName', 'RecomendarRecharge')
  159. // ->value('StatusValue');
  160. // // 周卡
  161. // $channels = DB::table('agent.dbo.admin_configs')->where([
  162. // 'type' => 'pay',
  163. // ])->pluck('id', 'name')->toArray();
  164. // $monthCard = MonthCard::query()->where('CardType', 1)->get();
  165. // foreach ($monthCard as $k => $v) {
  166. // $monthCard[$k]->FirstReward = $v->FirstReward/100;
  167. // $monthCard[$k]->DayReward = $v->DayReward/100;
  168. // $cardChannels = array_filter($v->gear, function ($item) {
  169. // return $item['status'] == 1;
  170. // });
  171. // $ids = array_column($cardChannels, 'id');
  172. // $v['channels'] = array_filter($channels, function ($item) use ($ids) {
  173. // return in_array($item, $ids);
  174. // });
  175. // }
  176. return compact('list', 'first_pay', 'RecomendarRecharge', 'monthCard');
  177. }
  178. public function channel_switch($id, $type)
  179. {
  180. $first_pay_status = DB::table('agent.dbo.recharge_gear')->where('id', $id)->value('first_pay');
  181. switch ($type) {
  182. case 'off': // 关闭
  183. $query = DB::table('agent.dbo.recharge_gear')->where('status', 1)->count();
  184. if ($query <= 1) {
  185. $this->error = '至少开启一个充值档位';
  186. return false;
  187. }
  188. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  189. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  190. 'status' => -1,
  191. ]);
  192. }else{
  193. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  194. 'status' => -1,
  195. ]);
  196. }
  197. break;
  198. case 'on': // 开启
  199. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  200. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  201. 'status' => 1,
  202. ]);
  203. }else{
  204. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  205. 'status' => 1,
  206. ]);
  207. }
  208. break;
  209. default:
  210. $this->error = '类型错误';
  211. return false;
  212. }
  213. if (isset($res) && $res) {
  214. return true;
  215. } else {
  216. $this->error = '操作失败';
  217. return false;
  218. }
  219. }
  220. public function add($money, $status, $config_ids, $favorable_price, $give, $pay_methods = 0, $in_shop = 1)
  221. {
  222. // 构建支付方式数组
  223. $gearData = [];
  224. $payMethodsMap = [
  225. 1 => 'cashapp',
  226. 2 => 'paypal',
  227. 4 => 'applepay',
  228. 8 => 'googlepay',
  229. 64 => 'usdt-trc20',
  230. 128 => 'usdt-erc20',
  231. 256 => 'usdc-trc20',
  232. 512 => 'usdc-erc20'
  233. ];
  234. foreach ($payMethodsMap as $value => $name) {
  235. if ($pay_methods & $value) {
  236. $gearData[] = [
  237. 'name' => $name,
  238. 'value' => $value,
  239. 'status' => 1
  240. ];
  241. }
  242. }
  243. $add_data = [
  244. 'money' => round($money, 2),
  245. 'status' => $status,
  246. 'in_shop' => (int)$in_shop,
  247. 'gear' => \GuzzleHttp\json_encode($gearData), // 存储到gear字段
  248. 'created_at' => date('Y-m-d H:i:s'),
  249. 'favorable_price' => round($favorable_price, 2),
  250. 'give' => round($give, 2)
  251. ];
  252. DB::table('agent.dbo.recharge_gear')->insert($add_data);
  253. return true;
  254. }
  255. public function update($id)
  256. {
  257. $info = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  258. $gear = \GuzzleHttp\json_decode($info->gear, true);
  259. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  260. $new_arr = [];
  261. foreach ($gear as &$value) {
  262. if ($value['status'] == 1) {
  263. $new_arr[] = $value['id'];
  264. }
  265. }
  266. $info->gear = $new_arr;
  267. return compact('info', 'names');
  268. }
  269. public function gear_switch($id, $config_id, $type)
  270. {
  271. $query = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  272. $gear = \GuzzleHttp\json_decode($query->gear, true);
  273. $off = 0;
  274. $data = [];
  275. $new_arr = [];
  276. switch ($type) {
  277. case 'on':
  278. foreach ($gear as &$value) {
  279. $data[] = $value['id'];
  280. if ($value['id'] == $config_id) {
  281. $value['status'] = 1;
  282. }
  283. }
  284. if (!in_array($config_id, $data)) {
  285. $new_arr['id'] = $config_id;
  286. $new_arr['status'] = 1;
  287. }
  288. break;
  289. case 'off':
  290. foreach ($gear as &$value) {
  291. $data[] = $value['id'];
  292. if ($value['id'] == $config_id) {
  293. $value['status'] = -1;
  294. }
  295. if ($value['status'] == 1) {
  296. $off += 1;
  297. }
  298. }
  299. if (!in_array($config_id, $data)) {
  300. $new_arr['id'] = $config_id;
  301. $new_arr['status'] = -1;
  302. }
  303. if ($off < 1) {
  304. $this->error = '至少开启一个通道';
  305. return false;
  306. }
  307. break;
  308. default:
  309. $this->error = '类型错误';
  310. return false;
  311. }
  312. !empty($new_arr) && $gear[] = $new_arr;
  313. $gear = \GuzzleHttp\json_encode($gear);
  314. DB::table('agent.dbo.recharge_gear')->where('id', $id)->update(['gear' => $gear]);
  315. }
  316. // 首充礼包
  317. public function gift()
  318. {
  319. $where = [];
  320. $where[] = ['first_pay', '>=', 1];
  321. $list = DB::table('agent.dbo.recharge_gear')
  322. ->where($where)
  323. ->orderByDesc('created_at')
  324. ->get();
  325. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  326. foreach ($list as &$value) {
  327. $name = [];
  328. if (!empty($value->gear)) {
  329. $gear = \GuzzleHttp\json_decode($value->gear, true);
  330. foreach ($gear as $val) {
  331. if ($val['status'] == 1) {
  332. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  333. }
  334. }
  335. }
  336. $value->gear = implode(',', $name);
  337. }
  338. // 旧首充
  339. // 开关状态
  340. $oldRechargeStatus = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  341. ->where('StatusName', 'FirstChargeGiftBagStatus')
  342. ->first();
  343. // 首充
  344. $oldRecharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  345. ->where('StatusName', 'FirstChargeGiftBag')
  346. ->first();
  347. $oldRechargeChannel = $names[$oldRecharge->StatusValue] ?? '';
  348. $pay_list = $names;
  349. return compact('list', 'oldRecharge', 'oldRechargeStatus','oldRechargeChannel','pay_list');
  350. }
  351. }