ChannelSettle.php.bak 26 KB

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