ChannelSettle.php 26 KB

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