BusinessSettleFlow.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * 商务结算流水统计定时器脚本
  4. *
  5. */
  6. namespace app\crontab;
  7. set_time_limit(0);
  8. use think\console\Command;
  9. use think\console\Input;
  10. use think\console\Output;
  11. use think\console\Input\Argument;
  12. use think\Db;
  13. class BusinessSettleFlow extends Command {
  14. protected $_output;
  15. protected function configure() {
  16. $this->addArgument('execMonth', Argument::OPTIONAL); //指定统计的日期时,格式Y-m
  17. $this->setName('BusinessSettleFlow')->setDescription('商务结算流水统计脚本');
  18. }
  19. protected function execute(Input $input, Output $output) {
  20. ini_set('memory_limit', '1024M');
  21. $now_time = time(); //当前时间
  22. $month = $input->getArgument('execMonth'); //指定的执行月份
  23. $this->_output = $output;
  24. //指定统计的月份时
  25. if(!empty($month)){
  26. $start_time = mktime(0,0,0,date('m',strtotime($month)),01);
  27. $end_time = mktime(0,0,0,date('m',strtotime($month))+1,01)-1;
  28. }
  29. //未指定统计的月份时
  30. else{
  31. $start_time = mktime(0,0,0,date('m')-1,01); //上一个自然月的第一天
  32. $end_time = mktime(0,0,0,date('m'),01)-1; //上一个自然月的最后一天
  33. }
  34. $output->writeln(date('Y-m-d H:i:s')." BusinessSettleFlow start\r\n");
  35. //上一个自然月分成合同
  36. $contract_list = Db::table('nw_point_contract')->field('id,gameid,start_time,end_time')->where(['delete_time'=>0,'start_time'=>['<=',$end_time],'end_time'=>['>',$start_time]])->select();
  37. $output->writeln("contract_list sql: ".Db::table('nw_point_contract')->getLastSql()."\r\n");
  38. if(!empty($contract_list)){
  39. $arr = [];
  40. foreach($contract_list as $key=>$value){
  41. //总流水
  42. $total_amount = 0;
  43. //结算月份的开始时间
  44. $settle_start_time = ($value['start_time']>=$start_time ? $value['start_time'] : $start_time);
  45. //结算月份的结束时间
  46. $settle_end_time = ($value['end_time']>=$end_time ? $end_time : $value['end_time']);
  47. $pay_info = Db::table('cy_pay force index(idx_pay_time)')->field('sum(amount) as amount')
  48. ->where(['status'=>1,'pay_time'=>[['>=', $settle_start_time],['<=', $settle_end_time]],'gameid'=>$value['gameid']])->find();
  49. $complex_info = Db::table('nw_complex_pay force index(idx_pay_time)')->field('sum(amount) as amount')
  50. ->where(['status'=>1,'pay_time'=>[['>=', $settle_start_time],['<=', $settle_end_time]],'gameid'=>$value['gameid']])->find();
  51. if(!empty($pay_info))
  52. $total_amount = $total_amount+$pay_info['amount'];
  53. if(!empty($pay_info))
  54. $total_amount = $total_amount+$complex_info['amount'];
  55. $arr[$key][0] = $value['gameid'];
  56. $arr[$key][1] = $value['id'];
  57. $arr[$key][2] = $total_amount;
  58. $arr[$key][3] = $settle_start_time;
  59. $arr[$key][4] = $settle_end_time;
  60. $arr[$key][5] = $now_time;
  61. $arr[$key][6] = $now_time;
  62. /*
  63. Db::table('nw_business_settle')->insert(['gameid' =>$value['gameid'],
  64. 'point_contract_id' =>$value['id'],
  65. 'pay_amount' =>$total_amount,
  66. 'settle_start_time' =>($value['start_time']>=$start_time ? $value['start_time'] : $start_time),
  67. 'settle_end_time' =>($value['end_time']>=$end_time ? $end_time : $value['end_time']),
  68. 'create_time' =>$now_time,
  69. 'update_time' =>$now_time
  70. ]);*/
  71. }
  72. $arr_key = array('gameid','point_contract_id','pay_amount','settle_start_time','settle_end_time','create_time','update_time');
  73. $arr_update = array('pay_amount','update_time');
  74. $execInfo = $this->multArray2InsertExec('nw_business_settle',$arr_key,$arr,$arr_update);
  75. if ($execInfo['code'] == 0) {
  76. $output->writeln("insert error:".json_encode($execInfo)." time:".date('Y-m-d H:i:s')."\r\n");
  77. }
  78. }
  79. $output->writeln(date('Y-m-d H:i:s')." BusinessSettleFlow end\r\n");
  80. exit;
  81. }
  82. /**
  83. * 多条数据同时转化成插入SQL语句,并执行SQL语句,支持批量更新和新增
  84. *
  85. * @param string $table 表名
  86. * @param array $arr_key 是表字段名的key:$arr_key=array('field1','field2','field3')
  87. * @param array $arr 是字段值 数组示例 arrat(('a','b','c'), ('bbc','bbb','caaa'),('add','bppp','cggg'))
  88. * @param array $arr_list 是需要更新的表字段名的key $arr_list=array('field1','field2','field3')
  89. * @param string $split
  90. *
  91. * @return array
  92. */
  93. private function multArray2InsertExec($table,$arr_key,$arr,$arr_list,$split='`')
  94. {
  95. $arrValues=array();
  96. $arrListValues = array();
  97. if ( empty($table) || !is_array($arr_key) || !is_array($arr) || !is_array($arr_list)) {
  98. return ['code'=> 0, 'msg' => 'multArray2Insert param ERROR '];
  99. }
  100. $sql = "INSERT INTO %s( %s ) values %s on duplicate key update %s";
  101. foreach ($arr as $k => $v) {
  102. $arrValues[$k] = "'".implode("','", array_values($v))."'";
  103. }
  104. foreach ($arr_list as $k => $v) {
  105. $arrListValues[$k] = $v . "=values(".$v.')';
  106. }
  107. $sql = sprintf($sql,$table,"{$split}" .implode("{$split} ,{$split}",$arr_key) . "{$split}", "(". implode("),(",array_values($arrValues)) . ")",implode(',',$arrListValues));
  108. $this->_output->writeln(" insert sql: {$sql} \r\n");
  109. if($sql){
  110. try {
  111. $result = Db::execute($sql);
  112. } catch (\think\exception\PDOException $e) {
  113. return ['code'=> 0, 'msg' => 'InsertSql ERROR '.$e->getMessage()];
  114. }
  115. return ['code'=> 1, 'msg' => 'SUCCESS'];
  116. }else{
  117. return ['code'=> 0, 'msg' => 'InsertSql Null'];
  118. }
  119. }
  120. }