RechargeLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. ];
  114. foreach ($list as &$value) {
  115. $name = [];
  116. if (!empty($value->gear)) {
  117. $gear = \GuzzleHttp\json_decode($value->gear, true);
  118. if (is_array($gear)) {
  119. foreach ($gear as $val) {
  120. // 新格式:支付方式位掩码
  121. if (isset($val['name']) && isset($val['status']) && $val['status'] == 1) {
  122. $name[] = $payMethodsNames[$val['name']] ?? $val['name'];
  123. }
  124. // 旧格式:支付渠道ID(兼容)
  125. elseif (isset($val['id']) && ($val['status'] ?? 0) == 1) {
  126. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  127. }
  128. }
  129. }
  130. }
  131. $value->gear = implode(', ', $name);
  132. }
  133. // dd($list);
  134. // // 首充档位配置
  135. // $first_pay = DB::table('agent.dbo.recharge_gear')
  136. // ->where('first_pay', 1)
  137. // ->first();
  138. // $name = [];
  139. // if (!empty($first_pay->gear)) {
  140. // $gear = \GuzzleHttp\json_decode($first_pay->gear, true);
  141. // foreach ($gear as $val) {
  142. // if ($val['status'] == 1) {
  143. // $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  144. // }
  145. // }
  146. // }
  147. // $first_pay->gear = implode(',', $name);
  148. $first_pay = [];
  149. $RecomendarRecharge = [];
  150. $monthCard = [];
  151. // 推荐充值档位
  152. // $RecomendarRecharge = DB::table(TableName::QPAccountsDB() . 'SystemStatusInfo')
  153. // ->where('StatusName', 'RecomendarRecharge')
  154. // ->value('StatusValue');
  155. // // 周卡
  156. // $channels = DB::table('agent.dbo.admin_configs')->where([
  157. // 'type' => 'pay',
  158. // ])->pluck('id', 'name')->toArray();
  159. // $monthCard = MonthCard::query()->where('CardType', 1)->get();
  160. // foreach ($monthCard as $k => $v) {
  161. // $monthCard[$k]->FirstReward = $v->FirstReward/100;
  162. // $monthCard[$k]->DayReward = $v->DayReward/100;
  163. // $cardChannels = array_filter($v->gear, function ($item) {
  164. // return $item['status'] == 1;
  165. // });
  166. // $ids = array_column($cardChannels, 'id');
  167. // $v['channels'] = array_filter($channels, function ($item) use ($ids) {
  168. // return in_array($item, $ids);
  169. // });
  170. // }
  171. return compact('list', 'first_pay', 'RecomendarRecharge', 'monthCard');
  172. }
  173. public function channel_switch($id, $type)
  174. {
  175. $first_pay_status = DB::table('agent.dbo.recharge_gear')->where('id', $id)->value('first_pay');
  176. switch ($type) {
  177. case 'off': // 关闭
  178. $query = DB::table('agent.dbo.recharge_gear')->where('status', 1)->count();
  179. if ($query <= 1) {
  180. $this->error = '至少开启一个充值档位';
  181. return false;
  182. }
  183. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  184. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  185. 'status' => -1,
  186. ]);
  187. }else{
  188. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  189. 'status' => -1,
  190. ]);
  191. }
  192. break;
  193. case 'on': // 开启
  194. if ($first_pay_status == 2) { // 引导付费 共用一个开关状态
  195. $res = DB::table('agent.dbo.recharge_gear')->where('first_pay', 2)->update([
  196. 'status' => 1,
  197. ]);
  198. }else{
  199. $res = DB::table('agent.dbo.recharge_gear')->where('id', $id)->update([
  200. 'status' => 1,
  201. ]);
  202. }
  203. break;
  204. default:
  205. $this->error = '类型错误';
  206. return false;
  207. }
  208. if (isset($res) && $res) {
  209. return true;
  210. } else {
  211. $this->error = '操作失败';
  212. return false;
  213. }
  214. }
  215. public function add($money, $status, $config_ids, $favorable_price, $give, $pay_methods = 0)
  216. {
  217. // 构建支付方式数组
  218. $gearData = [];
  219. $payMethodsMap = [
  220. 1 => 'cashapp',
  221. 2 => 'paypal',
  222. 4 => 'applepay',
  223. 8 => 'googlepay'
  224. ];
  225. foreach ($payMethodsMap as $value => $name) {
  226. if ($pay_methods & $value) {
  227. $gearData[] = [
  228. 'name' => $name,
  229. 'value' => $value,
  230. 'status' => 1
  231. ];
  232. }
  233. }
  234. $add_data = [
  235. 'money' => round($money, 2),
  236. 'status' => $status,
  237. 'gear' => \GuzzleHttp\json_encode($gearData), // 存储到gear字段
  238. 'created_at' => date('Y-m-d H:i:s'),
  239. 'favorable_price' => round($favorable_price, 2),
  240. 'give' => round($give, 2)
  241. ];
  242. DB::table('agent.dbo.recharge_gear')->insert($add_data);
  243. return true;
  244. }
  245. public function update($id)
  246. {
  247. $info = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  248. $gear = \GuzzleHttp\json_decode($info->gear, true);
  249. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  250. $new_arr = [];
  251. foreach ($gear as &$value) {
  252. if ($value['status'] == 1) {
  253. $new_arr[] = $value['id'];
  254. }
  255. }
  256. $info->gear = $new_arr;
  257. return compact('info', 'names');
  258. }
  259. public function gear_switch($id, $config_id, $type)
  260. {
  261. $query = DB::table('agent.dbo.recharge_gear')->where('id', $id)->first();
  262. $gear = \GuzzleHttp\json_decode($query->gear, true);
  263. $off = 0;
  264. $data = [];
  265. $new_arr = [];
  266. switch ($type) {
  267. case 'on':
  268. foreach ($gear as &$value) {
  269. $data[] = $value['id'];
  270. if ($value['id'] == $config_id) {
  271. $value['status'] = 1;
  272. }
  273. }
  274. if (!in_array($config_id, $data)) {
  275. $new_arr['id'] = $config_id;
  276. $new_arr['status'] = 1;
  277. }
  278. break;
  279. case 'off':
  280. foreach ($gear as &$value) {
  281. $data[] = $value['id'];
  282. if ($value['id'] == $config_id) {
  283. $value['status'] = -1;
  284. }
  285. if ($value['status'] == 1) {
  286. $off += 1;
  287. }
  288. }
  289. if (!in_array($config_id, $data)) {
  290. $new_arr['id'] = $config_id;
  291. $new_arr['status'] = -1;
  292. }
  293. if ($off < 1) {
  294. $this->error = '至少开启一个通道';
  295. return false;
  296. }
  297. break;
  298. default:
  299. $this->error = '类型错误';
  300. return false;
  301. }
  302. !empty($new_arr) && $gear[] = $new_arr;
  303. $gear = \GuzzleHttp\json_encode($gear);
  304. DB::table('agent.dbo.recharge_gear')->where('id', $id)->update(['gear' => $gear]);
  305. }
  306. // 首充礼包
  307. public function gift()
  308. {
  309. $where = [];
  310. $where[] = ['first_pay', '>=', 1];
  311. $list = DB::table('agent.dbo.recharge_gear')
  312. ->where($where)
  313. ->orderByDesc('created_at')
  314. ->get();
  315. $names = DB::table('agent.dbo.admin_configs')->where('type', 'pay')->pluck('name', 'id');
  316. foreach ($list as &$value) {
  317. $name = [];
  318. if (!empty($value->gear)) {
  319. $gear = \GuzzleHttp\json_decode($value->gear, true);
  320. foreach ($gear as $val) {
  321. if ($val['status'] == 1) {
  322. $name[] = isset($names[$val['id']]) ? $names[$val['id']] : '';
  323. }
  324. }
  325. }
  326. $value->gear = implode(',', $name);
  327. }
  328. // 旧首充
  329. // 开关状态
  330. $oldRechargeStatus = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  331. ->where('StatusName', 'FirstChargeGiftBagStatus')
  332. ->first();
  333. // 首充
  334. $oldRecharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  335. ->where('StatusName', 'FirstChargeGiftBag')
  336. ->first();
  337. $oldRechargeChannel = $names[$oldRecharge->StatusValue] ?? '';
  338. $pay_list = $names;
  339. return compact('list', 'oldRecharge', 'oldRechargeStatus','oldRechargeChannel','pay_list');
  340. }
  341. }