DealGameChannelDivide.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\crontab\controller;
  3. use think\Db;
  4. use think\Env;
  5. use think\Log;
  6. class DealGameChannelDivide
  7. {
  8. private $data;
  9. // 执行过期回收操作
  10. public function doAction()
  11. {
  12. //命令行
  13. if (request()->isCli()) {
  14. Log::info("-----处理游戏渠道分成结算单开始-------");
  15. $isJobDone = $this->doJob();
  16. Log::info("-----处理游戏渠道分成结算单结束-------");
  17. die;
  18. }
  19. else{
  20. echo '非命令行请求,请求被禁止';
  21. }
  22. }
  23. /**
  24. * 自动处理财务结算源数据
  25. */
  26. private function doJob()
  27. {
  28. //钉钉通知地址
  29. $this->_ddurl = Env::get('dingtalk.warning_url');
  30. set_time_limit(0);
  31. $executionTime = input('executionTime');
  32. echo $executionTime."---executionTime-----<br>";
  33. // 获取日期
  34. $this->_date = $executionTime ? $executionTime : date("Y-m-d");
  35. if(date('Y-m-d',strtotime($this->_date))<>$this->_date){
  36. $templateMsg = "公会游戏分成脚本Start Time:".date("Y-m-d H:i:s")."\r\n";
  37. $templateMsg .= "日期错误,请确认输入正确的日期! time:".$this->_date."\r\n";
  38. $this->curlDD($templateMsg, $this->_ddurl,true);
  39. echo 'date error,please confirm!'; die();
  40. }
  41. //如果是星期一则跑(开始时间上周一,结束时间上周日)
  42. if(date('w',strtotime($this->_date)) != 1){
  43. $templateMsg = "公会游戏分成脚本Start Time:".date("Y-m-d H:i:s")."\r\n";
  44. $templateMsg .= "请确认所有公会都已经配置好相关游戏的分成比例,以免造成结算单无法生成!! time:".$this->_date."\r\n";
  45. $this->curlDD($templateMsg, $this->_ddurl,true);
  46. echo 'not monday,no data to run!!!'; die();
  47. }
  48. $templateMsg = "公会游戏分成脚本Start Time:".date("Y-m-d H:i:s")."\r\n";
  49. $this->startTime = strtotime($this->_date) - 3600*24*7; //上周开始时间
  50. $this->lastTime = strtotime($this->_date) - 1; //上周结束时间
  51. echo $this->startTime."-----startTime-----".$this->lastTime."-----lastTime----------<br>";
  52. $this->startDateTime = date('Y-m-d H:i:s',$this->startTime); //上周开始时间
  53. $this->toDateTime = date('Y-m-d H:i:s',$this->lastTime); //上周结束时间
  54. echo $this->startDateTime."----startDateTime----".$this->toDateTime."----toDateTime-------<br>";
  55. $this->currentWeek = date("oW",$this->startTime); //归属第几周
  56. //删除掉重新统计
  57. Db::startTrans();
  58. $sql = "delete from nw_game_channel_divide_data where week='".$this->currentWeek."'";
  59. Db::execute($sql);
  60. $game_divides = Db::table('nw_game_channel_divide')->select();
  61. while(list($key,$val)=@each($game_divides)){
  62. $GameId = $val['game_id'];
  63. $ChannelId = $val['channel_id'];
  64. $DivideId = $val['id'];
  65. $where = array();
  66. $where['status'] = 1;
  67. $where['create_time'] = array(['>=', $this->startTime ],['<=', $this->lastTime]);
  68. $where['gameid'] = $GameId;
  69. //所有下级渠道(包括自己)
  70. $channelIds = get_child_channel_arr($ChannelId);
  71. array_push($channelIds,$ChannelId);
  72. $where['channel_id'] = ['in',$channelIds];
  73. // var_dump($where);
  74. $GameChannelData = Db::table('cy_pay')
  75. ->field("gameid AS game_id,count(*) as pay_cnt,SUM(amount) AS pay_amt,SUM(real_amount) AS pay_real_amt,SUM(real_ptb) AS pay_real_ptb")
  76. ->where($where)
  77. ->find();
  78. echo Db::table('cy_pay')->getLastSql()."----lastsql----2222222222-------<br>";
  79. if(intval($GameChannelData['pay_cnt'])){
  80. $AddData = array();
  81. $AddData['week'] = $this->currentWeek;
  82. $AddData['divide_id'] = $DivideId;
  83. $AddData['begin_time'] = $this->startDateTime;
  84. $AddData['end_time'] = $this->toDateTime;
  85. $AddData['game_id'] = $GameId;
  86. $AddData['channel_id'] = $ChannelId;
  87. $AddData['ratio'] = $val['ratio'];
  88. $AddData['pay_cnt'] = intval($GameChannelData['pay_cnt']);
  89. $AddData['pay_amt'] = floatval($GameChannelData['pay_amt']);
  90. $AddData['divide_amt'] = floatval(intval($GameChannelData['pay_amt']*$val['ratio'])/100);
  91. $AddData['pay_real_amt'] = floatval($GameChannelData['pay_real_amt']);
  92. $AddData['pay_real_ptb'] = floatval($GameChannelData['pay_real_ptb']);
  93. $AddData['update_time'] = time();
  94. var_dump($AddData);
  95. $insertId = Db::table('nw_game_channel_divide_data')->insertGetId($AddData);
  96. if($insertId){
  97. $UpdData = array();
  98. $UpdData['divide_channel_id'] = $ChannelId;
  99. $UpdData['divide_data_id'] = $insertId ;
  100. $UpdData['divide_update_time'] = time();
  101. var_dump($UpdData);
  102. $updResult = Db::table('cy_pay')->where($where)->update($UpdData);
  103. }
  104. }
  105. }
  106. Db::commit();
  107. $templateMsg .= "公会游戏分成脚本End Time:".date("Y-m-d H:i:s")."\r\n";
  108. $this->curlDD($templateMsg, $this->_ddurl,true);
  109. echo $templateMsg."----templateMsg------";die();
  110. }
  111. /**
  112. * 调用钉钉机器人函数
  113. *
  114. */
  115. function curlDD($msg,$url,$all=false){
  116. return true;
  117. if($all){
  118. $postData = [
  119. 'msgtype' => 'text',
  120. 'text' => [
  121. 'content' =>mb_convert_encoding($msg, "UTF-8", "auto"),
  122. ],
  123. 'at' => [
  124. 'isAtAll' => true
  125. ]
  126. ];
  127. }
  128. else{
  129. $postData = [
  130. 'msgtype' => 'text',
  131. 'text' => [
  132. 'content' =>mb_convert_encoding($msg, "UTF-8", "auto"),
  133. ]
  134. ];
  135. }
  136. $postString = json_encode($postData);
  137. $ch = curl_init();
  138. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  139. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  140. curl_setopt($ch, CURLOPT_URL, $url);
  141. curl_setopt($ch, CURLOPT_POST, true);
  142. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  143. curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
  144. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  145. "Content-Type: application/json"
  146. ));
  147. $result = curl_exec($ch);
  148. $HTTP_CODE= curl_getinfo($ch,CURLINFO_HTTP_CODE);
  149. curl_close($ch);
  150. if($HTTP_CODE<>200){
  151. $arr=array("state"=>-100);
  152. echo json_encode($arr);
  153. exit;
  154. }elseif($result==""){
  155. $arr=array("state"=>0,"errMsg"=>"接口返回为空");
  156. echo json_encode($arr);
  157. exit;
  158. }
  159. return $result;
  160. }
  161. }