Alliance.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /**
  3. * 公会联盟控制器
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\guildapi\controller\Guild;
  8. use think\Db;
  9. use think\Config;
  10. use app\common\model\Setting;
  11. use think\Exception;
  12. use app\common\library\MakeReport;
  13. use app\common\library\MakeReportGo;
  14. use think\Env;
  15. class Alliance extends Guild {
  16. protected $nowTime;
  17. protected $redis; //redis的句柄对象
  18. /**
  19. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  20. */
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->nowTime = NOW_TIMESTAMP;
  25. $this->redis = \think\Cache::store('default')->handler();
  26. }
  27. /**
  28. * 公会联盟结算列表
  29. * @return [type] [description]
  30. */
  31. public function settle()
  32. {
  33. //判断当前账号是否有此功能
  34. if(!in_array($this->_channelLevel,[0])){
  35. $this->jsonResult('', 0, '您无权限使用该功能');
  36. }
  37. $list_rows = $this->input('list_rows',10);
  38. $page = $this->input('page',1);
  39. $begin_time = $this->input('begin_time','','trim');
  40. $end_time = $this->input('end_time','','trim');
  41. $orderid = $this->input('orderid','','trim'); //收入账号
  42. $channel_name = $this->input('channel_name','','trim'); //渠道名
  43. $status = $this->input('status',0,'intval');
  44. $download = $this->input('download',0,'intval');
  45. $condition = [];
  46. $condition['c.parent_id'] = $this->_channelId;
  47. $condition['c.level'] = 1;
  48. $condition['a.settle_type'] = 1;
  49. if($orderid){
  50. $condition['a.orderid'] = $orderid;
  51. }
  52. if($channel_name){
  53. $condition['a.channel_name'] = $channel_name;
  54. }
  55. //申请开始时间和结束时间不为空时
  56. if ($begin_time != '' && $end_time != '') {
  57. $condition['a.create_time'] = [
  58. ['>=', strtotime($begin_time)],
  59. ['<=', strtotime($end_time . ' 23:59:59')],
  60. ];
  61. } //开始时间不为空时
  62. elseif ($begin_time != '') {
  63. $condition['a.create_time'] = ['>=', strtotime($begin_time)];
  64. } //结束时间不为空时
  65. elseif ($end_time != '') {
  66. $condition['a.create_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  67. }
  68. // var_dump($condition);
  69. $whereRaw = '';
  70. if(input('status')<>''){
  71. if($status == 1){
  72. $whereRaw .= ' a.first_audit_status=1 and a.second_audit_status=1 ';
  73. }
  74. else if($status == 2){
  75. $whereRaw .= ' a.first_audit_status=2 or a.second_audit_status=2 ';
  76. }
  77. else if($status == 0){
  78. $whereRaw .= ' a.first_audit_status<>2 and a.second_audit_status=0 ';
  79. }
  80. }
  81. if($download){
  82. if($whereRaw){
  83. $sql = model('ChannelDivideSettle')->alias('a')
  84. ->join('nw_channel c','a.channel_id=c.id','left')
  85. ->where($condition)
  86. ->whereRaw($whereRaw)
  87. ->field("a.id,a.orderid,a.channel_id,a.channel_name,a.total_amount,a.total_divide_amt,a.remark,a.create_time,a.first_audit_status,a.second_audit_status")
  88. ->order("a.id desc")
  89. ->fetchSql(true)
  90. ->select();
  91. }
  92. else{
  93. $sql = model('ChannelDivideSettle')->alias('a')
  94. ->join('nw_channel c','a.channel_id=c.id','left')
  95. ->where($condition)
  96. ->field("a.id,a.orderid,a.channel_id,a.channel_name,a.total_amount,a.total_divide_amt,a.remark,a.create_time,a.first_audit_status,a.second_audit_status")
  97. ->order("a.id desc")
  98. ->fetchSql(true)
  99. ->select();
  100. }
  101. // echo $sql;
  102. // if ((new MakeReport())->addTask('guild.AllianceSettle', $sql, 'cps'.session('guild_info')['id'])){
  103. if ((new MakeReportGo())->addTask('guild.AllianceSettle', $sql, 'cps'.session('guild_info')['id'])){
  104. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  105. }
  106. else{
  107. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  108. }
  109. }
  110. if($whereRaw){
  111. $settleList = model('ChannelDivideSettle')->alias('a')
  112. ->join('nw_channel c','a.channel_id=c.id','left')
  113. ->where($condition)
  114. ->whereRaw($whereRaw)
  115. ->field("a.id,a.orderid,a.channel_id,a.channel_name,a.total_amount,a.total_divide_amt,a.remark,a.create_time,a.first_audit_status,a.second_audit_status")
  116. ->order("a.id desc")
  117. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  118. ->toArray();
  119. }
  120. else{
  121. $settleList = model('ChannelDivideSettle')->alias('a')
  122. ->join('nw_channel c','a.channel_id=c.id','left')
  123. ->where($condition)
  124. ->field("a.id,a.orderid,a.channel_id,a.channel_name,a.total_amount,a.total_divide_amt,a.remark,a.create_time,a.first_audit_status,a.second_audit_status")
  125. ->order("a.id desc")
  126. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  127. ->toArray();
  128. }
  129. foreach ( $settleList["data"] as &$val ) {
  130. if($val['first_audit_status']==1 && $val['second_audit_status']==1){ //审核通过
  131. $val['status'] = 1;
  132. }
  133. else if($val['first_audit_status']==2 || $val['second_audit_status']==2){ //审核不通过
  134. $val['status'] = 2;
  135. }
  136. else{ //审核中
  137. $val['status'] = 0;
  138. }
  139. unset($val['first_audit_status']); unset($val['second_audit_status']);
  140. }
  141. $this->jsonResult($settleList, 20000, '获取列表成功');
  142. }
  143. /**
  144. * 提现列表
  145. * @return [type] [description]
  146. */
  147. public function withdraw()
  148. {
  149. //判断当前账号是否有此功能
  150. if(!in_array($this->_channelLevel,[0])){
  151. $this->jsonResult('', 0, '您无权限使用该功能');
  152. }
  153. $list_rows = $this->input('list_rows',10);
  154. $page = $this->input('page',1);
  155. $begin_time = $this->input('begin_time','','trim');
  156. $end_time = $this->input('end_time','','trim');
  157. $orderid = $this->input('orderid','','trim');
  158. $channel_name = $this->input('channel_name','','trim');
  159. $status = $this->input('status','0','intval');
  160. $download = $this->input('download','0','intval');
  161. $condition = [];
  162. $condition['c.parent_id'] = $this->_channelId;
  163. $condition['c.level'] = 1;
  164. if($orderid){
  165. $condition['a.orderid'] = $orderid;
  166. }
  167. if($channel_name){
  168. $condition['a.channel_name'] = $channel_name;
  169. }
  170. if(input('status')<>''){
  171. $condition['a.status'] = intval($status);
  172. }
  173. //申请开始时间和结束时间不为空时
  174. if ($begin_time != '' && $end_time != '') {
  175. $condition['a.create_time'] = [
  176. ['>=', strtotime($begin_time)],
  177. ['<=', strtotime($end_time . ' 23:59:59')],
  178. ];
  179. } //开始时间不为空时
  180. elseif ($begin_time != '') {
  181. $condition['a.create_time'] = ['>=', strtotime($begin_time)];
  182. } //结束时间不为空时
  183. elseif ($end_time != '') {
  184. $condition['a.create_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  185. }
  186. // var_dump($condition);
  187. if($download){
  188. $sql = model('ChannelWithdraw')->alias('a')
  189. ->join('nw_channel c','a.channel_id=c.id','left')
  190. ->where($condition)
  191. ->field("a.id,a.orderid,a.channel_id,a.channel_name,a.withdraw_amt,a.remark,a.create_time,a.status")
  192. ->order("a.id desc")
  193. ->fetchSql(true)
  194. ->select();
  195. // echo $sql;
  196. // if ((new MakeReport())->addTask('guild.AllianceWithdraw', $sql, 'cps'.session('guild_info')['id'])){
  197. if ((new MakeReportGo())->addTask('guild.AllianceWithdraw', $sql, 'cps'.session('guild_info')['id'])){
  198. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  199. }
  200. else{
  201. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  202. }
  203. }
  204. $withdrawList = model('ChannelWithdraw')->alias('a')
  205. ->join('nw_channel c','a.channel_id=c.id','left')
  206. ->where($condition)
  207. ->field("a.id,a.orderid,a.channel_id,a.channel_name,a.withdraw_amt,a.remark,a.create_time,a.status")
  208. ->order("a.id desc")
  209. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  210. ->toArray();
  211. // echo model('ChannelWithdraw')->getLastSql()."-----lastsql------<br>";
  212. $this->jsonResult($withdrawList, 20000, '获取列表成功');
  213. }
  214. /**
  215. * 公会结算数据(外放渠道)
  216. */
  217. public function getChanelSettle()
  218. {
  219. if ($this->request->isPost()) {
  220. //判断当前账号是否有此功能
  221. if(!in_array($this->_channelLevel,[0])){
  222. $this->jsonResult('', 0, '您无权限使用该功能');
  223. }
  224. //只有外放的商务才能为公会结算
  225. if (!(isset(session('guild_info')['roles'][0]) && session('guild_info')['roles'][0] == 5)) {
  226. $this->jsonResult('', 0, '您无权限使用该功能');
  227. }
  228. else{
  229. $channel_id = $this->input('channel_id',0,'intval'); //公会ID
  230. if(!$channel_id){
  231. $this->jsonResult('', 0, '请选择要进行结算的公会');
  232. }
  233. $channelInfoApply = model("ChannelInfo")->field("id,channel_id,real_name,apply_status,type")->where(['channel_id'=>$this->_channelId,'apply_status'=>1])->find();
  234. if(empty($channelInfoApply)){
  235. $this->jsonResult('', 0, '您的身份尚未认证通过,不能进行结算');
  236. }
  237. $parentChannelInfo = model("Channel")->field("id,name,level,parent_id,status,channel_tg_type")->where(['id'=>$this->_channelId])->find();
  238. if(empty($parentChannelInfo)){
  239. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  240. }
  241. else if($parentChannelInfo['channel_tg_type'] <> 1){
  242. $this->jsonResult('', 0, '您无权限使用该功能!');
  243. }
  244. $channelInfo = model("Channel")->field("id,name,level,parent_id,cps_settle_period,mcps_settle_period,status")->where(['id'=>$channel_id,'level'=>1])->find();
  245. if(empty($channelInfo)){
  246. $this->jsonResult('', 0, '您要进行结算的公会不存在');
  247. }
  248. else if($channelInfo['parent_id'] <> $this->_channelId){
  249. $this->jsonResult('', 0, '非您所属公会,您不能为他进行结算操作');
  250. }
  251. if($channelInfo['cps_settle_period']==2){
  252. $this->lastTime = strtotime(date('Y-m-d')) - 1;
  253. }
  254. else{
  255. $this->lastTime = strtotime(date('Y-m-d',(time()-((date('w',time())==0?7:date('w',time()))-1)*24*3600))) - 1;
  256. }
  257. $list_rows = input('list_rows',10);
  258. $page = input('page',1);
  259. $where = array();
  260. $where['p.status'] = 1;
  261. $where['p.create_time'] = array('elt', $this->lastTime);
  262. //所有下级渠道(包括自己)
  263. $channelIds = get_child_channel_arr($channel_id);
  264. array_push($channelIds,$channel_id);
  265. $where['p.channel_id'] = ['in',$channelIds];
  266. $where['p.settle_id'] = array('eq',0);
  267. // var_dump($where);
  268. $GameChannelData = Db::table('cy_pay')->alias('p')
  269. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1','inner')
  270. ->join('nw_game_channel_divide d','p.gameid = d.game_id and d.channel_id='.$channel_id,'left')
  271. ->field("p.gameid AS game_id,g.name as game_name,g.channel_split_ratio,count(*) as pay_cnt,SUM(p.amount) AS pay_amt,d.id as ratio_id,d.ratio,SUM(p.real_amount) AS pay_real_amt,SUM(p.real_ptb) AS pay_real_ptb")
  272. ->where($where)
  273. ->group('p.gameid')
  274. ->select();
  275. $retData = array();
  276. $total_amt = $divide_amt = $total_cnt = 0;
  277. foreach ( $GameChannelData as &$det ) {
  278. if(intval($det['ratio_id'])){
  279. $ratio_id = $det['ratio_id'];
  280. $ratio = floatval($det['ratio']);
  281. }
  282. else{
  283. $ratio_id = 0;
  284. $ratio = floatval($det['channel_split_ratio']);
  285. }
  286. if(!$ratio){
  287. $this->jsonResult('', 0, '您的该公会下尚有游戏未配置分成比例,请先联系平台设置');
  288. }
  289. else{
  290. $det['ratio_id'] = $ratio_id;
  291. $det['ratio'] = $ratio;
  292. $total_cnt += floatval($det['pay_cnt']);
  293. $total_amt += floatval($det['pay_amt']);
  294. $divide_amt += floatval(intval($det['pay_amt'] * $det['ratio']) / 100);
  295. $det['divide_amt'] = floatval(intval($det['pay_amt'] * $det['ratio']) / 100);
  296. }
  297. }
  298. $settle = array();
  299. $settle['total_amount'] = $total_amt;
  300. $settle['divide_amount'] = $divide_amt;
  301. $settle['total_cnt'] = $total_cnt;
  302. $detPaylist = Db::table('cy_pay')->alias('p')
  303. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1','inner')
  304. ->join('nw_game_channel_divide d','p.gameid = d.game_id and d.channel_id='.$channel_id,'left')
  305. ->field("p.orderid AS orderid,p.gameid AS game_id,g.name as game_name,p.amount AS amount,round(p.amount*(case when d.id then d.ratio else g.channel_split_ratio end)/100,2) AS divide_amt,case when d.id then d.id else '0' end as ratio_id,case when d.id then d.ratio else g.channel_split_ratio end as ratio,p.real_amount AS real_amount,p.real_ptb AS real_ptb,p.serverid,p.servername,p.rolename,p.userid,p.username,p.create_time")
  306. ->where($where)
  307. ->order("p.id desc")
  308. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  309. ->toArray();
  310. // var_dump($detPaylist);
  311. $retData['settle'] = $settle;
  312. $retData['settleDet'] = $GameChannelData;
  313. $retData['detPaylist'] = $detPaylist;
  314. $this->jsonResult($retData, 20000, '获取结算列表成功');
  315. }
  316. exit;
  317. }
  318. else{
  319. $this->jsonResult('', 0, '非法请求');
  320. exit;
  321. }
  322. }
  323. /**
  324. * 公会结算处理(外放公会)
  325. */
  326. public function doChannelSettle()
  327. {
  328. if ($this->request->isPost()) {
  329. $channel_id = $this->input('channel_id',0,'intval'); //公会ID
  330. $total_amount = $this->input('total_amount',0,'floatval'); //流水
  331. $divide_amount = $this->input('divide_amount',0,'floatval'); //分成金额
  332. $total_cnt = $this->input('total_cnt',0,'intval'); //结算订单笔数
  333. $remark = '外放商务结算申请'; //备注
  334. if(!in_array($this->_channelLevel,[0])){
  335. $this->jsonResult('', 0, '您无权限使用该功能');
  336. }
  337. //只有外放的商务才能为公会结算
  338. if (!(isset(session('guild_info')['roles'][0]) && session('guild_info')['roles'][0] == 5)) {
  339. $this->jsonResult('', 0, '您无权限使用该功能');
  340. }
  341. if(!$channel_id){
  342. $this->jsonResult('', 0, '请选择要进行结算的公会');
  343. }
  344. if(!$total_amount || !$divide_amount || !$total_cnt){
  345. $this->jsonResult('', 0, '没有可结算单的订单');
  346. }
  347. /*
  348. else if($divide_amount < 100){
  349. $this->jsonResult('', 0, '结算金额不能小于100元!');
  350. }
  351. */
  352. $channelInfoApply = model("ChannelInfo")->field("id,channel_id,real_name,apply_status,type")->where(['channel_id'=>$this->_channelId,'apply_status'=>1])->find();
  353. if(empty($channelInfoApply)){
  354. $this->jsonResult('', 0, '您的身份尚未认证通过,不能进行结算');
  355. }
  356. $parentChannelInfo = model("Channel")->field("id,name,level,parent_id,status,channel_tg_type")->where(['id'=>$this->_channelId])->find();
  357. if(empty($parentChannelInfo)){
  358. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  359. }
  360. else if($parentChannelInfo['channel_tg_type'] <> 1){
  361. $this->jsonResult('', 0, '您无权限使用该功能!');
  362. }
  363. $channelInfo = model("Channel")->field("id,name,level,parent_id,cps_settle_period,mcps_settle_period,status")->where(['id'=>$channel_id,'level'=>1])->find();
  364. if(empty($channelInfo)){
  365. $this->jsonResult('', 0, '您要进行结算的公会不存在');
  366. }
  367. else if($channelInfo['parent_id'] <> $this->_channelId){
  368. $this->jsonResult('', 0, '非您所属公会,您不能为他进行结算操作');
  369. }
  370. $orderid = 'ZFS'.makeOrderid();
  371. if($channelInfo['cps_settle_period']==2){ //日结
  372. $this->lastTime = strtotime(date('Y-m-d')) - 1;
  373. if(!(date('Hi') >= '02:00' && date('Hi') < '23:59')){
  374. $this->jsonResult('', 0, '不在结算时间内,请在每日02:00 ~ 23:59分内提交结算申请!');
  375. }
  376. }
  377. else{ //周结
  378. $this->lastTime = strtotime(date('Y-m-d',(time()-((date('w',time())==0?7:date('w',time()))-1)*24*3600))) - 1;
  379. if(date("w")<>1 || !(date('Hi') >= '02:00' && date('Hi') < '23:59')){
  380. $this->jsonResult('', 0, '不在结算时间内,请在每周一02:00~23:59 内提交结算申请!');
  381. }
  382. }
  383. $where = array();
  384. $where['p.status'] = 1;
  385. $where['p.create_time'] = array('elt', $this->lastTime);
  386. //所有下级渠道(包括自己)
  387. $channelIds = get_child_channel_arr($channel_id);
  388. array_push($channelIds,$channel_id);
  389. $where['p.channel_id'] = ['in',$channelIds];
  390. $where['p.settle_id'] = array('eq',0);
  391. // var_dump($where);
  392. $GameChannelData = Db::table('cy_pay')->alias('p')
  393. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1','inner')
  394. ->join('nw_game_channel_divide d','p.gameid = d.game_id and d.channel_id='.$channel_id,'left')
  395. ->field("p.gameid AS game_id,g.name as game_name,g.channel_split_ratio,min(p.create_time) as begin_time,max(p.create_time) as end_time,count(*) as pay_cnt,SUM(p.amount) AS pay_amt,d.id as ratio_id,d.ratio,SUM(p.real_amount) AS pay_real_amt,SUM(p.real_ptb) AS pay_real_ptb")
  396. ->where($where)
  397. ->group('p.gameid')
  398. ->select();
  399. $retData = array();
  400. // echo Db::table('cy_pay')->getLastSql()."----sql-----------<br>";
  401. $totalAmount = $divideAmount = $totalCnt = 0;
  402. $beginTime = time();
  403. $endTime = 0;
  404. foreach ( $GameChannelData as &$det ) {
  405. if(intval($det['ratio_id'])){
  406. $ratio_id = $det['ratio_id'];
  407. $ratio = floatval($det['ratio']);
  408. }
  409. else{
  410. $ratio_id = 0;
  411. $ratio = floatval($det['channel_split_ratio']);
  412. }
  413. if(!$ratio){
  414. $this->jsonResult('', 0, '您下属的该公会尚有游戏未配置分成比例,不能进行结算,请先联系平台设置');
  415. }
  416. else{
  417. $det['ratio_id'] = $ratio_id;
  418. $det['ratio'] = $ratio;
  419. $totalCnt += floatval($det['pay_cnt']);
  420. $totalAmount += floatval($det['pay_amt']);
  421. $divideAmount += floatval(intval($det['pay_amt'] * $det['ratio']) / 100);
  422. $det['divide_amt'] = floatval(intval($det['pay_amt'] * $det['ratio']) / 100);
  423. if($beginTime > $det['begin_time']){
  424. $beginTime = $det['begin_time'];
  425. }
  426. if($endTime < $det['end_time']){
  427. $endTime = $det['end_time'];
  428. }
  429. }
  430. }
  431. reset($GameChannelData);
  432. if(!$totalAmount || !$divideAmount || !$totalCnt){
  433. $this->jsonResult('', 0, '您已没有可结算的订单');
  434. }
  435. if(abs($totalAmount-$total_amount)>0.01 || $totalCnt<>$total_cnt || abs($divideAmount-$divide_amount)>0.01){
  436. $this->jsonResult('', 0, '结算数据与后台计算数据不一致,请重新核对后再提交');
  437. }
  438. //指定时间内,禁止重复下单
  439. if(!requestDuplicateCheck('channel_settle_duplicate_'.$channel_id,10)){
  440. $this->jsonResult('', 0, '结算请求过多,请于10s以后,再次进行结算操作');
  441. }
  442. $detPaylist = Db::table('cy_pay')->alias('p')
  443. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1','inner')
  444. ->join('nw_game_channel_divide d','p.gameid = d.game_id and d.channel_id='.$channel_id,'left')
  445. ->field("p.id as payid,p.orderid AS orderid,p.gameid AS game_id,p.userid,p.username,g.name as game_name,g.channel_split_ratio,p.channel_id,p.amount AS amount,(p.amount*(case when d.id then d.ratio else g.channel_split_ratio end)/100) AS divide_amt,case when d.id then d.id else '0' end as ratio_id,case when d.id then d.ratio else g.channel_split_ratio end as ratio,p.real_amount AS real_amount,p.real_ptb AS real_ptb,p.create_time,p.serverid,p.servername,p.rolename")
  446. ->where($where)
  447. ->select();
  448. // 启动事务
  449. Db::startTrans();
  450. try{
  451. $settleData = array();
  452. $settleData['orderid'] = $orderid;
  453. $settleData['settle_type'] = 1;
  454. $settleData['channel_id'] = $channel_id;
  455. $settleData['channel_name'] = $channelInfo['name'];
  456. $settleData['total_amount'] = $totalAmount;
  457. $settleData['total_divide_amt'] = $divideAmount;
  458. $settleData['begin_time'] = $beginTime;
  459. $settleData['end_time'] = $endTime;
  460. $settleData['settle_method'] = 2; //外放结算方式
  461. $settleData['remark'] = $remark;
  462. $settleData['create_time'] = time();
  463. $settleData['first_audit_status'] = 1;
  464. $settleData['first_audit_time'] = time();
  465. if($channelInfo['cps_settle_period']==2){
  466. $settleData['settle_period'] = 2;
  467. }
  468. else{
  469. $settleData['settle_period'] = 1;
  470. }
  471. $insertSettleId = model('ChannelDivideSettle')->insertGetId($settleData);
  472. if ( !$insertSettleId) {
  473. throw new Exception("添加结算单失败");
  474. }
  475. while(list($key,$val)=@each($GameChannelData)){
  476. $detData = array();
  477. $detData['settle_id'] = $insertSettleId;
  478. $detData['divide_id'] = $val['ratio_id'];
  479. $detData['begin_time'] = $val['begin_time'];
  480. $detData['end_time'] = $val['end_time'];
  481. $detData['game_id'] = $val['game_id'];
  482. $detData['channel_id'] = $channel_id;
  483. $detData['ratio'] = $val['ratio'];
  484. $detData['pay_cnt'] = $val['pay_cnt'];
  485. $detData['pay_amt'] = floatval($val['pay_amt']);
  486. $detData['pay_real_amt'] = floatval($val['pay_real_amt']);
  487. $detData['pay_real_ptb'] = floatval($val['pay_real_ptb']);
  488. $detData['divide_amt'] = floatval($val['divide_amt']);
  489. $detData['create_time'] = time();
  490. ${'insertDet'.$val['game_id'].'ID'} = model('ChannelDivideSettleDet')->insertGetId($detData);
  491. if ( !${'insertDet'.$val['game_id'].'ID'}) {
  492. throw new Exception("添加结算单明细失败");
  493. }
  494. }
  495. $orderIds = array();
  496. while(list($key,$val)=@each($detPaylist)){
  497. // echo ${'insertDet'.$val['game_id'].'ID'}."---insertDet".$val['game_id']."ID'--------<br>";
  498. $orderIds[] = $val['payid'];
  499. $detPayData['settle_id'] = $insertSettleId;
  500. $detPayData['settle_det_id'] = ${'insertDet'.$val['game_id'].'ID'};
  501. $detPayData['orderid'] = $val['orderid'];
  502. $detPayData['game_id'] = $val['game_id'];
  503. $detPayData['channel_id'] = $val['channel_id'];
  504. $detPayData['ratio'] = $val['ratio'];
  505. $detPayData['userid'] = $val['userid'];
  506. $detPayData['username'] = $val['username'];
  507. $detPayData['amount'] = floatval($val['amount']);
  508. $detPayData['real_amount'] = floatval($val['real_amount']);
  509. $detPayData['real_ptb'] = floatval($val['real_ptb']);
  510. $detPayData['divide_amt'] = floatval($val['divide_amt']);
  511. $detPayData['serverid'] = trim($val['serverid']);
  512. $detPayData['servername'] = trim($val['servername']);
  513. $detPayData['rolename'] = trim($val['rolename']);
  514. $detPayData['create_time'] = $val['create_time'];
  515. $detPayData['add_time'] = time();
  516. // var_dump($detPayData);
  517. $insertDetPayId = model('ChannelDivideSettleDetPay')->insertGetId($detPayData);
  518. if ( !$insertDetPayId) {
  519. throw new Exception("添加结算订单明细失败");
  520. }
  521. }
  522. $updPayData = array();
  523. $updPayData['settle_channel_id'] = $channel_id;
  524. $updPayData['settle_id'] = $insertSettleId;
  525. $updPayData['settle_update_time'] = time();
  526. /*
  527. $gameids = model('Game')->where(['game_kind'=>1])->column('id');
  528. $where['p.gameid']= array('IN',$gameids);
  529. */
  530. $where['p.id']= array('IN',$orderIds);
  531. $updResult = model('Pay')->alias('p')->where($where)->update($updPayData);
  532. if ( !$updResult) {
  533. throw new Exception("更新订单为已结算失败");
  534. }
  535. $this->redis->del('channel_settle_duplicate_'.$channel_id);
  536. // 提交事务
  537. Db::commit();
  538. } catch (\Exception $e) {
  539. // 回滚事务
  540. Db::rollback();
  541. $this->jsonResult('', 0, '结算申请生成失败'.$e->getMessage());
  542. }
  543. $result['orderid'] = $orderid;
  544. $template = '有新的专服结算申请:商务"'.$this->_channelName.'"为下属公会"'.$channelInfo['name'].'" 发起结算申请,订单号:'.$orderid.' 。请及时安排处理,时间:'.date('Y-m-d H:i:s');
  545. $ddurl = Env::get('operat_url');
  546. curlDD($template, $ddurl,true);
  547. $this->jsonResult($result, 20000, '结算申请生成成功');
  548. exit;
  549. }
  550. else{
  551. $this->jsonResult('', 0, '非法请求');
  552. exit;
  553. }
  554. }
  555. }