CoinTransfer.php 35 KB

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