CoinTransferCoin.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. <?php
  2. /**
  3. * 转账接口控制器
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\common\library\MakeReportGo;
  8. use app\common\model\MemberCoinInfo;
  9. use app\mcpsapi\controller\Guild;
  10. use app\common\model\CoinTransferCoin as CoinTransferCoinModel;
  11. use app\common\library\WeixinPay;
  12. use think\Db;
  13. use think\Config;
  14. use app\common\model\Setting;
  15. use think\Exception;
  16. use app\common\library\MakeReport;
  17. class CoinTransferCoin extends Guild
  18. {
  19. protected $nowTime;
  20. protected $payRequestLimit = 5; //重复下单的限制时间
  21. protected $redis; //redis的句柄对象
  22. /**
  23. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  24. */
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->nowTime = NOW_TIMESTAMP;
  29. $this->redis = \think\Cache::store('default')->handler();
  30. }
  31. /**
  32. * 转账管理
  33. * @return [type] [description]
  34. */
  35. public function index()
  36. {
  37. //判断当前账号是否有此功能
  38. if (!in_array($this->_channelLevel, [1, 2])) {
  39. $this->jsonResult('', 0, '您无权限使用该功能');
  40. }
  41. $list_rows = $this->input('list_rows', 10);
  42. $page = $this->input('page', 1);
  43. $type = $this->input('type', '0', 'intval'); //1(转账收入),2(转账支出)
  44. $account = $this->input('account', '', 'trim'); //收入账号
  45. $apply_begin_time = $this->input('apply_begin_time', '', 'trim');
  46. $apply_end_time = $this->input('apply_end_time', '', 'trim');
  47. $finish_begin_time = $this->input('finish_begin_time', '', 'trim');
  48. $finish_end_time = $this->input('finish_end_time', '', 'trim');
  49. $download = $this->input('download', 0, 'intval');
  50. $condition = [];
  51. if ($account) {
  52. $condition['t.det_username'] = $account;
  53. }
  54. if (!in_array($type, [1, 2])) {
  55. $this->jsonResult('', 0, '请选择交易类型');
  56. }
  57. if ($type == 1) {
  58. $condition['t.det_userid'] = $this->_channelId;
  59. if ($this->_channelLevel == 1) { //公会
  60. $condition['t.transfer_type'] = 0;
  61. } else { //子会长
  62. $condition['t.transfer_type'] = 1;
  63. }
  64. } else if ($type == 2) {
  65. $condition['t.src_channel_id'] = $this->_channelId;
  66. if ($this->_channelLevel == 1) {
  67. $condition['t.transfer_type'] = array('in', [1, 2]);
  68. } else {
  69. $condition['t.transfer_type'] = 3;
  70. }
  71. }
  72. //申请开始时间和结束时间不为空时
  73. if ($apply_begin_time != '' && $apply_end_time != '') {
  74. $condition['t.create_time'] = [
  75. ['>=', strtotime($apply_begin_time)],
  76. ['<=', strtotime($apply_end_time . ' 23:59:59')],
  77. ];
  78. } //开始时间不为空时
  79. elseif ($apply_begin_time != '') {
  80. $condition['t.create_time'] = ['>=', strtotime($apply_begin_time)];
  81. } //结束时间不为空时
  82. elseif ($apply_end_time != '') {
  83. $condition['t.create_time'] = ['<=', strtotime($apply_end_time . ' 23:59:59')];
  84. }
  85. //到账开始时间和结束时间不为空时
  86. if ($finish_begin_time != '' && $finish_end_time != '') {
  87. $condition['t.create_time'] = [
  88. ['>=', strtotime($finish_begin_time)],
  89. ['<=', strtotime($finish_end_time . ' 23:59:59')],
  90. ];
  91. } //开始时间不为空时
  92. elseif ($finish_begin_time != '') {
  93. $condition['t.create_time'] = ['>=', strtotime($finish_begin_time)];
  94. } //结束时间不为空时
  95. elseif ($finish_end_time != '') {
  96. $condition['t.create_time'] = ['<=', strtotime($finish_end_time . ' 23:59:59')];
  97. }
  98. // var_dump($condition);
  99. if ($download) {
  100. $sql = model('CoinTransferCoin')->alias('t')
  101. ->join('cy_game g', 't.game_id = g.id', 'left')
  102. ->join('nw_channel channel', 't.cuser_id = channel.id', 'left')
  103. ->field('t.*,t.amount as real_amount,t.status as finish_status,t.create_time as finish_time,g.name as game_name,channel.name as cuser_name')
  104. ->order("t.id desc")
  105. ->where($condition)
  106. ->whereRaw('src_channel_id=' . $this->_channelId . ' or det_userid=' . $this->_channelId . ' and transfer_kind=2')
  107. ->fetchSql(true)
  108. ->select();
  109. // echo $sql;
  110. // if ((new MakeReport())->addTask('guild.ChannelTransferIndex', $sql, 'cps'.session('guild_info')['id'])){
  111. if ((new MakeReportGo())->addTask('guild.ChannelTransferIndex', $sql, 'cps' . session('guild_info')['id'])) {
  112. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  113. } else {
  114. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  115. }
  116. }
  117. $transferList = model('CoinTransferCoin')->alias('t')
  118. ->join('cy_game g', 't.game_id = g.id', 'left')
  119. ->join('nw_channel channel', 't.cuser_id = channel.id', 'left')
  120. ->field('t.*,t.amount as real_amount,t.status as finish_status,t.create_time as finish_time,g.name as game_name,channel.name as cuser_name')
  121. ->order("t.id desc")
  122. ->where($condition)
  123. ->whereRaw('src_channel_id=' . $this->_channelId . ' or det_userid=' . $this->_channelId . ' and transfer_kind=2')
  124. ->paginate(['list_rows' => $list_rows, 'page' => $page])
  125. ->toArray();
  126. // echo model('CoinTransferCoin')->getLastSql()."-----lastsql------<br>";
  127. $this->jsonResult($transferList, 20000, '获取列表成功');
  128. }
  129. /**
  130. * 转账处理
  131. */
  132. public function transfer()
  133. {
  134. if ($this->request->isPost()) {
  135. $type = $this->input('type', '', 'trim'); //转账方式:single(单个转账),batch(批量转账)
  136. $transfer_kind = $this->input('transfer_kind', '', 'trim'); //收款对象:1(转给玩家),2(转给渠道)
  137. $pay_password = $this->input('pay_password', '', 'trim'); //交易密码
  138. $orderid = 'T' . makeOrderid();
  139. $batchNo = 'Batch-' . $this->_channelId . '-' . date('YmdHis');
  140. //判断当前账号是否有充值权限
  141. if (!in_array($this->_channelLevel, [1, 2])) {
  142. $this->jsonResult('', 0, '您无权限进行转账操作');
  143. }
  144. if (!in_array($type, ['single', 'batch'])) {
  145. $this->jsonResult('', 0, '请正确选择转账方式');
  146. } else {
  147. if (!in_array($transfer_kind, [1, 2])) {
  148. $this->jsonResult('', 0, '请选择收款对象');
  149. }
  150. if ($transfer_kind == 2 && $this->_channelLevel <> 1) {
  151. $this->jsonResult('', 0, '您不能转账给子会长');
  152. }
  153. }
  154. if (!$pay_password) {
  155. $this->jsonResult('', 0, '请输入交易密码');
  156. }
  157. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,pay_password,secondary_password")->where(['id' => session('guild_info')['id'], 'channel_id' => $this->_channelId])->find();
  158. if (!$adminInfo) {
  159. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  160. } else if (!$adminInfo['pay_password'] || !$adminInfo['secondary_password']) {
  161. $this->jsonResult('', 0, '请设置交易密码和二级密码后才能进行提现');
  162. } else if ($adminInfo['pay_password'] != mg_password($pay_password)) {
  163. $this->jsonResult('', 0, '交易密码错误!');
  164. }
  165. if ($type == 'single') { //单个转账
  166. $account = $this->input('account', '', 'trim'); //收款账号
  167. $amount = $this->input('amount', '', 'trim'); //转账金额
  168. $remark = $this->input('remark', '', 'trim'); //备注
  169. if (!$account) {
  170. $this->jsonResult('', 0, '请输入收款账号');
  171. }
  172. if (!$amount) {
  173. $this->jsonResult('', 0, '请输入正确的转账金额');
  174. } else if (intval($amount) <> $amount) {
  175. $this->jsonResult('', 0, '请输入正确的转账金额');
  176. }
  177. $amount = $this->input('amount', 0, 'intval'); //转账金额
  178. $channelInfo = model("Channel")->where(['id' => $this->_channelId])->find();
  179. if ($channelInfo) {
  180. if ($channelInfo['amount_coin'] < $amount) {
  181. $this->jsonResult('', 0, '您的平台币余额不足');
  182. }
  183. } else {
  184. $this->jsonResult('', 0, '您的账号有异常,请联系管理员');
  185. }
  186. if ($transfer_kind == 1) { //转给玩家
  187. $game_id = $this->input('game_id', 0, 'intval'); //游戏ID
  188. // $server_id = $this->input('server_id', '', 'trim'); //区服ID
  189. // $server_name = $this->input('server_name', '', 'trim'); //区服名称
  190. /*
  191. if(!$server_id || !$server_name){
  192. $this->jsonResult('', 0, '请选择区服信息');
  193. }
  194. */
  195. if (!$game_id) {
  196. $this->jsonResult('', 0, '请选择要转账的游戏');
  197. } else {
  198. $gameInfo = model('Game')->where(['id' => $game_id])->find();
  199. if (!$gameInfo) {
  200. $this->jsonResult('', 0, '该游戏不存在');
  201. }
  202. // 效验玩家在游戏和渠道中是否有关系
  203. $chkResult = chkTransferPlayer($this->_channelId, $account, $game_id);
  204. if ($chkResult['errorCode'] <> 1) {
  205. $this->jsonResult('', 0, $chkResult['errorMsg']);
  206. } else {
  207. $chkResultData = $chkResult['retData'];
  208. //指定时间内,禁止重复下单
  209. if (!requestDuplicateCheck('transfer_pay_duplicate_' . $this->_channelId, $this->payRequestLimit)) {
  210. $this->jsonResult('', 0, '转账请求过多,请于' . $this->payRequestLimit . 's以后,再次进行转账操作');
  211. }
  212. $transferData = array();
  213. $transferData['transfer_kind'] = $transfer_kind;
  214. $transferData['batch_no'] = $batchNo;
  215. if ($this->_channelLevel == 1) {
  216. $transferData['transfer_type'] = 2;
  217. } else {
  218. $transferData['transfer_type'] = 3;
  219. }
  220. $transferData['orderid'] = $orderid;
  221. $transferData['src_channel_id'] = $this->_channelId;
  222. $transferData['src_channel_name'] = $this->_channelName;
  223. $transferData['amount'] = $amount;
  224. $transferData['game_id'] = $game_id;
  225. $transferData['det_userid'] = $chkResultData['userid'];
  226. $transferData['det_username'] = $chkResultData['username'];
  227. $transferData['cuser_id'] = $chkResultData['cuser_id'];
  228. // $transferData['server_id'] = $server_id;
  229. // $transferData['server_name'] = $server_name;
  230. $transferData['status'] = 1;
  231. $transferData['remark'] = $remark;
  232. $transferData['create_time'] = time();
  233. // 启动事务
  234. Db::startTrans();
  235. try {
  236. $result = []; //返回值
  237. $insertTransferId = model("CoinTransferCoin")->insert($transferData);
  238. if (!$insertTransferId) {
  239. throw new Exception("创建转账订单失败! ");
  240. }
  241. $detData = array();
  242. $detData['channel_id'] = $this->_channelId;
  243. $detData['channel_name'] = $this->_channelName;
  244. $detData['change_amount'] = -$amount;
  245. $detData['account_type'] = 3; //通用账户
  246. $detData['type'] = 1; //转账支出
  247. $detData['out_orderid'] = $orderid;
  248. $detData['create_time'] = NOW_TIMESTAMP;
  249. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  250. if (!$insertDetId) {
  251. throw new Exception("添加账户变动明细失败");
  252. }
  253. $updData = array();
  254. $updData['amount_coin'] = Db::raw("amount_coin-" . $amount);
  255. $updData['update_time'] = time();
  256. $updResult = model('Channel')->where(['id' => $this->_channelId, 'amount_coin' => array('egt', $amount)])->update($updData);
  257. if (!$updResult) {
  258. throw new Exception("账户金额变动失败");
  259. }
  260. $coinData = [
  261. 'userid' => $chkResultData['userid'],
  262. 'orderid' => $orderid,
  263. 'start_amount' => $chkResultData['amount'],
  264. 'amount' => $amount,
  265. 'result_amount' => priceFormat($chkResultData['amount'] + $amount),
  266. 'type' => 1,
  267. 'remarks' => '平台币充值',
  268. 'create_time' => NOW_TIMESTAMP,
  269. ];
  270. $memberCoinInfoModel = new MemberCoinInfo();
  271. if (!$memberCoinInfoModel->insertGetId($coinData)) {
  272. throw new Exception("账户金额变动失败");
  273. }
  274. if (!model('members')->where('id', $chkResultData['userid'])->update(['amount' => priceFormat($chkResultData['amount'] + $amount)])) {
  275. throw new Exception("账户金额变动失败");
  276. }
  277. $this->redis->del('transfer_pay_duplicate_' . $this->_channelId);
  278. // 提交事务
  279. Db::commit();
  280. } catch (\Exception $e) {
  281. // 回滚事务
  282. Db::rollback();
  283. $this->jsonResult('', 0, '订单生成失败' . $e->getMessage());
  284. }
  285. $result['orderid'] = $orderid;
  286. $this->jsonResult($result, 20000, '转账成功');
  287. exit;
  288. }
  289. }
  290. } else { //转给子公会
  291. $chkResult = chkTransferChannel($this->_channelId, $account);
  292. if ($chkResult['errorCode'] <> 1) {
  293. $this->jsonResult('', 0, $chkResult['errorMsg']);
  294. } else {
  295. $chkResultData = $chkResult['retData'];
  296. //指定时间内,禁止重复下单
  297. if (!requestDuplicateCheck('transfer_pay_duplicate_' . $this->_channelId, $this->payRequestLimit)) {
  298. $this->jsonResult('', 0, '转账请求过多,请于' . $this->payRequestLimit . 's以后,再次进行转账操作');
  299. }
  300. $transferData = array();
  301. $transferData['transfer_kind'] = $transfer_kind;
  302. $transferData['batch_no'] = $batchNo;
  303. $transferData['transfer_type'] = 1;
  304. $transferData['orderid'] = $orderid;
  305. $transferData['src_channel_id'] = $this->_channelId;
  306. $transferData['src_channel_name'] = $this->_channelName;
  307. $transferData['amount'] = $amount;
  308. $transferData['det_userid'] = $chkResultData['userid'];
  309. $transferData['det_username'] = $chkResultData['username'];
  310. $transferData['status'] = 1;
  311. $transferData['remark'] = $remark;
  312. $transferData['create_time'] = time();
  313. // 启动事务
  314. Db::startTrans();
  315. try {
  316. $result = []; //返回值
  317. $insertTransferId = model("CoinTransferCoin")->insert($transferData);
  318. if (!$insertTransferId) {
  319. throw new Exception("创建转账订单失败! ");
  320. }
  321. $detData = array();
  322. $detData['channel_id'] = $this->_channelId;
  323. $detData['channel_name'] = $this->_channelName;
  324. $detData['change_amount'] = -$amount;
  325. $detData['account_type'] = 3; //通用账户
  326. $detData['type'] = 1; //转账支出
  327. $detData['out_orderid'] = $orderid;
  328. $detData['create_time'] = NOW_TIMESTAMP;
  329. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  330. if (!$insertDetId) {
  331. throw new Exception("添加账户变动明细失败");
  332. }
  333. $updData = array();
  334. $updData['amount_coin'] = Db::raw("amount_coin-" . $amount);
  335. $updData['update_time'] = time();
  336. $updResult = model('Channel')->where(['id' => $this->_channelId, 'amount_coin' => array('egt', $amount)])->update($updData);
  337. if (!$updResult) {
  338. throw new Exception("账户金额变动失败");
  339. }
  340. $detData = array();
  341. $detData['channel_id'] = $transferData['det_userid'];
  342. $detData['channel_name'] = $transferData['det_username'];
  343. $detData['change_amount'] = $transferData['amount'];
  344. $detData['account_type'] = 3; //通用账户
  345. $detData['type'] = 2; //转账收入
  346. $detData['out_orderid'] = $transferData['orderid'];
  347. $detData['create_time'] = NOW_TIMESTAMP;
  348. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  349. if (!$insertDetId) {
  350. throw new Exception("添加收款账户变动明细失败");
  351. }
  352. $updData = array();
  353. $updData['amount_coin'] = Db::raw("amount_coin+" . $transferData['amount']);
  354. $updData['update_time'] = time();
  355. $updToResult = model('Channel')->where(['id' => $transferData['det_userid']])->update($updData);
  356. if (!$updToResult) {
  357. throw new Exception("收款账户金额变动失败");
  358. }
  359. $this->redis->del('transfer_pay_duplicate_' . $this->_channelId);
  360. // 提交事务
  361. Db::commit();
  362. } catch (\Exception $e) {
  363. // 回滚事务
  364. Db::rollback();
  365. $this->jsonResult('', 0, '转账失败' . $e->getMessage());
  366. }
  367. $result['orderid'] = $orderid;
  368. $this->jsonResult($result, 20000, '转账成功');
  369. exit;
  370. }
  371. }
  372. } else { //批量转账
  373. //设置文件上传的最大限制
  374. ini_set('memory_limit', '1024M');
  375. //加载第三方类文件
  376. vendor("PHPExcel.PHPExcel");
  377. //防止乱码
  378. header("Content-type:text/html;charset=utf-8");
  379. //实例化主文件
  380. //接收前台传过来的execl文件
  381. if (!isset($_FILES['file'])) {
  382. $this->jsonResult('', 0, '请上传批量导入文件');
  383. }
  384. $file = $_FILES['file'];
  385. //截取文件的后缀名,转化成小写
  386. $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
  387. if ($extension == "xlsx") {
  388. //2007(相当于是打开接收的这个excel)
  389. $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
  390. } else {
  391. //2003(相当于是打开接收的这个excel)
  392. $objReader = \PHPExcel_IOFactory::createReader('Excel5');
  393. }
  394. $objContent = $objReader->load($file['tmp_name']);
  395. if ($objContent) {
  396. $sheetContent = $objContent->getSheet(0)->toArray();
  397. $sheetHeaderArr = $sheetContent[0];
  398. $headNameValArr = [];
  399. for ($i = 0; $i < count($sheetHeaderArr); $i++) {
  400. // echo $sheetHeaderArr[$i]."---$i---<br>";
  401. if ($transfer_kind == 1) { //转账给玩家
  402. if (trim($sheetHeaderArr[$i]) == '收款账号') {
  403. $headNameValArr['det_username'] = $i;
  404. } else if (trim($sheetHeaderArr[$i]) == '金额') {
  405. $headNameValArr['amount'] = $i;
  406. } else if (trim($sheetHeaderArr[$i]) == '游戏ID') {
  407. $headNameValArr['game_id'] = $i;
  408. } else if (trim($sheetHeaderArr[$i]) == '游戏名') {
  409. $headNameValArr['game_name'] = $i;
  410. }
  411. // else if (trim($sheetHeaderArr[$i]) == '区服ID') {
  412. // $headNameValArr['server_id'] = $i;
  413. // } else if (trim($sheetHeaderArr[$i]) == '区服名') {
  414. // $headNameValArr['server_name'] = $i;
  415. // }
  416. else if (trim($sheetHeaderArr[$i]) == '备注') {
  417. $headNameValArr['remark'] = $i;
  418. }
  419. } else { //转账给子公会
  420. if (trim($sheetHeaderArr[$i]) == '收款账号') {
  421. $headNameValArr['det_username'] = $i;
  422. } else if (trim($sheetHeaderArr[$i]) == '金额') {
  423. $headNameValArr['amount'] = $i;
  424. } else if (trim($sheetHeaderArr[$i]) == '备注') {
  425. $headNameValArr['remark'] = $i;
  426. }
  427. }
  428. }
  429. if ($transfer_kind == 1) { //转账给玩家
  430. // if (!(isset($headNameValArr['det_username']) && isset($headNameValArr['amount']) && isset($headNameValArr['game_id']) && isset($headNameValArr['game_name']) && isset($headNameValArr['server_id']) && isset($headNameValArr['server_name']) && isset($headNameValArr['remark']))) {
  431. if (!(isset($headNameValArr['det_username']) && isset($headNameValArr['amount']) && isset($headNameValArr['game_id']) && isset($headNameValArr['game_name']) && isset($headNameValArr['remark']))) {
  432. $this->jsonResult('', 0, '请选择正确的玩家批量转账导入模板 !');
  433. }
  434. } else { //转账给子公会
  435. if (!(isset($headNameValArr['det_username']) && isset($headNameValArr['amount']) && isset($headNameValArr['remark']))) {
  436. $this->jsonResult('', 0, '请选择正确的子公会批量转账导入模板!');
  437. }
  438. }
  439. unset($sheetContent[0]); //删除第一行标题
  440. /*
  441. $headNameValArr['det_username'] = 0;
  442. $headNameValArr['amount'] = 1;
  443. $headNameValArr['remark'] = 2;
  444. $sheetContent = array();
  445. $sheetContent[0] = array("B-Promoter3","10","批量转账");
  446. $sheetContent[1] = array("B-Promoter4","10","批量转账");
  447. $sheetContent[2] = array("B-Promoter5","10","批量转账");
  448. */
  449. $totalCnt = $succcessCnt = 0;
  450. $accountArrs = array();
  451. foreach ($sheetContent as $k => $v) {
  452. if ($v[0]) $totalCnt++;
  453. $arr = array();
  454. $arr['orderid'] = $orderid . "-" . ($succcessCnt + 1);
  455. $arr['src_channel_id'] = $this->_channelId;
  456. $arr['src_channel_name'] = $this->_channelName;
  457. $arr['batch_no'] = $batchNo;
  458. $arr['status'] = 1;
  459. $arr['transfer_kind'] = $transfer_kind;
  460. $arr['create_time'] = time();
  461. if (!$v{$headNameValArr['amount']}) {
  462. $this->jsonResult('', 0, '收款账号:' . trim($v{$headNameValArr['det_username']}) . ' 游戏名:' . trim($v{$headNameValArr['game_name']}) . ' 金额:' . trim($v{$headNameValArr['amount']}) . ' 请输入正确的金额');
  463. } else if (trim($v{$headNameValArr['amount']}) <> intval($v{$headNameValArr['amount']})) {
  464. $this->jsonResult('', 0, '收款账号:' . trim($v{$headNameValArr['det_username']}) . ' 游戏名:' . trim($v{$headNameValArr['game_name']}) . ' 金额:' . trim($v{$headNameValArr['amount']}) . ' 请输入正确的金额');
  465. }
  466. if ($transfer_kind == 1) { //玩家
  467. if (trim($v{$headNameValArr['det_username']}) && trim($v{$headNameValArr['amount']}) && intval($v{$headNameValArr['game_id']}) && trim($v{$headNameValArr['game_name']})) {
  468. if ($this->_channelLevel == 1) {
  469. $arr['transfer_type'] = 2;
  470. } else {
  471. $arr['transfer_type'] = 3;
  472. }
  473. $arr['amount'] = intval($v{$headNameValArr['amount']});
  474. $arr['game_id'] = intval($v{$headNameValArr['game_id']});
  475. // $arr['server_id'] = trim($v{$headNameValArr['server_id']});
  476. // $arr['server_name'] = trim($v{$headNameValArr['server_name']});
  477. $arr['remark'] = trim($v{$headNameValArr['remark']});
  478. if (!$arr['game_id']) {
  479. $this->jsonResult('', 0, '请选择要转账游戏');
  480. } else {
  481. $gameInfo = model('Game')->where(['id' => $arr['game_id']])->find();
  482. if (!$gameInfo) {
  483. $this->jsonResult('', 0, '收款账号:' . trim($v{$headNameValArr['det_username']}) . ' 游戏名:' . trim($v{$headNameValArr['game_name']}) . ' 金额:' . trim($v{$headNameValArr['amount']}) . ' 游戏不存在');
  484. } else if (trim($gameInfo['name']) <> trim($v{$headNameValArr['game_name']})) {
  485. $this->jsonResult('', 0, '收款账号:' . trim($v{$headNameValArr['det_username']}) . ' 游戏名:' . trim($v{$headNameValArr['game_name']}) . ' 金额:' . trim($v{$headNameValArr['amount']}) . ' 游戏无法匹配');
  486. } else if ($gameInfo['game_kind'] <> 1) {
  487. $this->jsonResult('', 0, '收款账号:' . trim($v{$headNameValArr['det_username']}) . ' 游戏名:' . trim($v{$headNameValArr['game_name']}) . ' 金额:' . trim($v{$headNameValArr['amount']}) . ' 游戏不能使用平台币');
  488. } else { //判断能否对该玩家进行充值
  489. $chkResult = chkTransferPlayer($this->_channelId, trim($v{$headNameValArr['det_username']}), intval($v{$headNameValArr['game_id']}));
  490. if ($chkResult['errorCode'] <> 1) {
  491. unset($arr);
  492. $this->jsonResult('', 0, $chkResult['errorMsg']);
  493. } else {
  494. $chkResultData = $chkResult['retData'];
  495. $arr['det_userid'] = $chkResultData['userid'];
  496. $arr['det_username'] = $chkResultData['username'];
  497. $arr['cuser_id'] = $chkResultData['cuser_id'];
  498. $succcessCnt++;
  499. }
  500. }
  501. }
  502. } else {
  503. unset($arr);
  504. $this->jsonResult('', 0, '收款账号:' . trim($v{$headNameValArr['det_username']}) . ' 游戏名:' . trim($v{$headNameValArr['game_name']}) . ' 金额:' . trim($v{$headNameValArr['amount']}) . ' 该记录不完整,不能进行导入!');
  505. }
  506. } else { //子公会
  507. if (trim($v{$headNameValArr['det_username']}) && trim($v{$headNameValArr['amount']})) {
  508. $arr['transfer_type'] = 1;
  509. $arr['amount'] = floatval($v{$headNameValArr['amount']});
  510. $arr['remark'] = trim($v{$headNameValArr['remark']});
  511. $chkResult = chkTransferChannel($this->_channelId, trim($v{$headNameValArr['det_username']}));
  512. if ($chkResult['errorCode'] <> 1) {
  513. unset($arr);
  514. $this->jsonResult('', 0, $chkResult['errorMsg']);
  515. } else {
  516. $chkResultData = $chkResult['retData'];
  517. $arr['det_userid'] = $chkResultData['userid'];
  518. $arr['det_username'] = $chkResultData['username'];
  519. $succcessCnt++;
  520. }
  521. } else {
  522. unset($arr);
  523. $this->jsonResult('', 0, '收款账号:' . trim($v{$headNameValArr['det_username']}) . ' 金额:' . trim($v{$headNameValArr['amount']}) . ' 该记录不完整,不能进行导入!');
  524. }
  525. }
  526. if (isset($arr)) {
  527. $accountArrs[] = $arr;
  528. }
  529. }
  530. if (!$accountArrs) {
  531. $this->jsonResult('', 0, '您未导入任何数据,请检查导入文件 !');
  532. } else {
  533. $result = []; //返回值
  534. $errorMsg = '';
  535. $totalAmount = 0;
  536. foreach ($accountArrs as $detAccount) {
  537. $totalAmount += floatval($detAccount['amount']);
  538. }
  539. reset($accountArrs);
  540. $channelInfo = model("Channel")->where(['id' => $this->_channelId])->find();
  541. if ($channelInfo) {
  542. if ($channelInfo['amount_coin'] < $totalAmount) {
  543. $this->jsonResult('', 0, '您的平台币余额不足');
  544. }
  545. } else {
  546. $this->jsonResult('', 0, '您的账号有异常,请联系管理员');
  547. }
  548. //指定时间内,禁止重复下单
  549. if (!requestDuplicateCheck('transfer_pay_duplicate_' . $this->_channelId, $this->payRequestLimit)) {
  550. $this->jsonResult('', 0, '转账请求过多,请于' . $this->payRequestLimit . 's以后,再次进行转账操作');
  551. }
  552. // 启动事务
  553. Db::startTrans();
  554. try {
  555. foreach ($accountArrs as $transferData) {
  556. if ($transfer_kind == 1) { //转给玩家
  557. $insertTransferId = model("CoinTransferCoin")->insert($transferData);
  558. if (!$insertTransferId) {
  559. throw new Exception("创建转账订单失败! ");
  560. }
  561. $detData = array();
  562. $detData['channel_id'] = $this->_channelId;
  563. $detData['channel_name'] = $this->_channelName;
  564. $detData['change_amount'] = -$transferData['amount'];
  565. $detData['account_type'] = 3; //通用账户
  566. $detData['type'] = 1; //转账支出
  567. $detData['out_orderid'] = $transferData['orderid'];
  568. $detData['create_time'] = NOW_TIMESTAMP;
  569. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  570. if (!$insertDetId) {
  571. throw new Exception("添加账户变动明细失败");
  572. }
  573. $updData = array();
  574. $updData['amount_coin'] = Db::raw("amount_coin-" . $transferData['amount']);
  575. $updData['update_time'] = time();
  576. $updResult = model('Channel')->where(['id' => $this->_channelId, 'amount_coin' => array('egt', $transferData['amount'])])->update($updData);
  577. if (!$updResult) {
  578. throw new Exception("账户金额变动失败");
  579. }
  580. $memberInfo = model('Members')->where(['id' => $transferData['det_userid']])->field('id,username,flag,amount')->find();
  581. $coinData = [
  582. 'userid' => $memberInfo['id'],
  583. 'orderid' => $transferData['orderid'],
  584. 'start_amount' => $memberInfo['amount'],
  585. 'amount' => $transferData['amount'],
  586. 'result_amount' => priceFormat($memberInfo['amount'] + $transferData['amount']),
  587. 'type' => 1,
  588. 'remarks' => '平台币充值',
  589. 'create_time' => NOW_TIMESTAMP,
  590. ];
  591. $memberCoinInfoModel = new MemberCoinInfo();
  592. if (!$memberCoinInfoModel->insertGetId($coinData)) {
  593. throw new Exception("账户金额变动失败");
  594. }
  595. if (!model('members')->where('id', $memberInfo['id'])->update(['amount' => priceFormat($memberInfo['amount'] + $transferData['amount'])])) {
  596. throw new Exception("账户金额变动失败");
  597. }
  598. $errorMsg .= '收款账号:' . $transferData['det_username'] . ' 游戏ID:' . $transferData['game_id'] . ' 金额:' . $transferData['amount'] . ' 转账成功;';
  599. } else { //转给子公会
  600. $insertTransferId = model("CoinTransferCoin")->insert($transferData);
  601. if (!$insertTransferId) {
  602. throw new Exception("创建转账订单失败! ");
  603. }
  604. $detData = array();
  605. $detData['channel_id'] = $this->_channelId;
  606. $detData['channel_name'] = $this->_channelName;
  607. $detData['change_amount'] = -$transferData['amount'];
  608. $detData['account_type'] = 3; //通用账户
  609. $detData['type'] = 1; //转账支出
  610. $detData['out_orderid'] = $transferData['orderid'];
  611. $detData['create_time'] = NOW_TIMESTAMP;
  612. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  613. if (!$insertDetId) {
  614. throw new Exception("添加账户变动明细失败");
  615. }
  616. $updData = array();
  617. $updData['amount_coin'] = Db::raw("amount_coin-" . $transferData['amount']);
  618. $updData['update_time'] = time();
  619. $updResult = model('Channel')->where(['id' => $this->_channelId, 'amount_coin' => array('egt', $transferData['amount'])])->update($updData);
  620. if (!$updResult) {
  621. throw new Exception("账户金额变动失败");
  622. }
  623. $detData = array();
  624. $detData['channel_id'] = $transferData['det_userid'];
  625. $detData['channel_name'] = $transferData['det_username'];
  626. $detData['change_amount'] = $transferData['amount'];
  627. $detData['account_type'] = 3; //通用账户
  628. $detData['type'] = 2; //转账收入
  629. $detData['out_orderid'] = $transferData['orderid'];
  630. $detData['create_time'] = NOW_TIMESTAMP;
  631. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  632. if (!$insertDetId) {
  633. throw new Exception("添加收款账户变动明细失败");
  634. }
  635. $updData = array();
  636. $updData['amount_coin'] = Db::raw("amount_coin+" . $transferData['amount']);
  637. $updData['update_time'] = time();
  638. $updToResult = model('Channel')->where(['id' => $transferData['det_userid']])->update($updData);
  639. if (!$updToResult) {
  640. throw new Exception("收款账户金额变动失败");
  641. }
  642. $errorMsg .= '收款账号:' . $transferData['det_username'] . ' 金额:' . $transferData['amount'] . ' 转账成功;';
  643. }
  644. }
  645. // 提交事务
  646. Db::commit();
  647. } catch (\Exception $e) {
  648. // 回滚事务
  649. Db::rollback();
  650. $this->jsonResult('', 0, '转账操作失败' . $e->getMessage());
  651. }
  652. $this->redis->del('transfer_pay_duplicate_' . $this->_channelId);
  653. $result['message'] = $errorMsg;
  654. $this->jsonResult($result, 20000, $errorMsg);
  655. exit;
  656. }
  657. } else {
  658. $this->jsonResult('', 0, '请上传批量导入表格 !');
  659. }
  660. }
  661. } else {
  662. $this->jsonResult('', 0, '非法请求');
  663. exit;
  664. }
  665. }
  666. public function getAmount(){
  667. $adminInfo = session('guild_info');
  668. $channel_info = Db::name('nw_channel')->where(['id' => $adminInfo['channel_id']])->field('amount,js_amount,amount_coin')->find();
  669. $this->jsonResult($channel_info, 20000, '获取成功');
  670. }
  671. }