You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.2 KiB
PHTML

5 years ago
<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 后台总览聚合
* @author cz
*/
class AggregateFinanceSetController extends Controller {
public $apihost;
5 years ago
public $token = "LYHTQDJS";
5 years ago
public function _initialize(){
$apihost = M("Kv")->field("value")->where("`key` = 'aggregate_finance_api'")->find();
if(empty($apihost)){
echo "请先设置请求接口aggregate_finance_api的值".PHP_EOL;die;
}
$this->apihost = $apihost['value'];
5 years ago
}
public function setFreeDateCount($begin,$end='')
{
if($end == '') $end = $begin;
//判断日期格式
$patten = "/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/";
if (!preg_match($patten, $begin)) {
die("开始时间格式错误");
}
if (!preg_match($patten, $end)) {
die("结束时间格式错误");
}
if(strtotime($end) < strtotime($begin)){
die("结束时间不能比开始时间小");
}
if(strtotime($end)+86399-24*3600 > time()){
die("结束时间不能大于当前");
}
5 years ago
5 years ago
$starttime = $begin?strtotime($begin):mktime(0,0,0,date('m'),date('d'),date('Y'));
$endtime = $end?strtotime($end)+86399:$starttime+86399;
$datelist = get_date_list($starttime,$endtime,1);
$countdate = count($datelist);
for($i=0;$i<$countdate;$i++){
$this->setDailyCount($datelist[$i]);
}
5 years ago
}
5 years ago
public function setDailyCount($stime="now")
5 years ago
{
5 years ago
if($stime=="now"){
$stime=time();
}else{
$stime = strtotime($stime);
}
$nowdate = date("Y-m-d",$stime);
$w = (int)date("w",$stime);
$d = (int)date("d",$stime);
if($w == 1 || $d == 1){
if($w == 1){
echo $nowdate.":".PHP_EOL;
echo "--周结begin".PHP_EOL;
$this->setWeekCount($stime);
}
if($d == 1){
echo $nowdate.":".PHP_EOL;
echo "--月结&补点begin".PHP_EOL;
$this->setMonthCount($stime);
}
}else{
echo $nowdate."非周一和月初,无需任何处理".PHP_EOL;
}
}
public function setWeekCount($stime)//进行周结
{
$begintime = mktime(0,0,0,date('m',$stime),date('d',$stime)-7,date('Y',$stime));
$endtime = mktime(0,0,0,date('m',$stime),date('d',$stime),date('Y',$stime))-1;
$this->getAndSaveData(0,$begintime,$endtime);
}
public function setMonthCount($stime)//进行周结
{
$thismonth = date('m',$stime);
$thisyear = date('Y',$stime);
if ($thismonth == 1) {
$lastmonth = 12;
$lastyear = $thisyear - 1;
} else {
$lastmonth = $thismonth - 1;
$lastyear = $thisyear;
}
$lastStartDay = $lastyear . '-' . $lastmonth . '-1';
$lastEndDay = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay));
$begintime = strtotime($lastStartDay);//上个月的月初时间戳
$endtime = strtotime($lastEndDay)+24*3600-1;//上个月的月末时间戳
//普通月结
$this->getAndSaveData(1,$begintime,$endtime);
//补点
$this->getAndSaveData(2,$begintime,$endtime);
5 years ago
}
5 years ago
protected function getAndSaveData($type,$begintime,$endtime)
5 years ago
{
5 years ago
$sign = md5($begintime.$endtime.$type.$this->token);
$arr = [
"begintime"=>$begintime,
"endtime"=>$endtime,
"type"=>$type,
"sign"=>$sign
];
$dataurl .= $this->apihost."&".http_build_query($arr);
$html = file_get_contents($dataurl);
$rsp = json_decode($html,true);
if($rsp['code'] != 1){
echo $rsp['error'].PHP_EOL;die;
}
//成功插入
$Aggregate = M("aggregate_statement","tab_");
$begintimestr = date("Y-m-d",$begintime);
$endtimestr = date("Y-m-d",$endtime);
if($rsp['count'] > 0){
//插入数据
foreach ($rsp['data'] as $k => $v) {
foreach ($v["game_list"] as $ke => &$va) {
$va['begintime'] = $begintimestr;
$va['endtime'] = $endtimestr;
$va['fax_ratio'] = 0;
}
$v["statement_info"] = json_encode($v["game_list"],JSON_UNESCAPED_UNICODE);
unset($v["game_list"]);
$v['begintime'] = $begintime;
$v['endtime'] = $endtime;
$v['create_time'] = time();
5 years ago
$v['admin_name'] = "system";
$v['admin_id'] = 0;
5 years ago
$v['second_party_info'] = json_encode([
"partner"=>$v['channel_name']
],JSON_UNESCAPED_UNICODE);
5 years ago
$Aggregate->add($v);
}
}
if($type == 0){
echo "----周结统计ok".PHP_EOL;
}elseif($type == 1){
echo "----月结统计ok".PHP_EOL;
}elseif($type == 2){
echo "----补点统计ok".PHP_EOL;
}
5 years ago
}
5 years ago
}