优化聚合cp获取接口

master
chenzhi 5 years ago
parent 36ab480d50
commit c59a314afe

@ -9,18 +9,15 @@ use Think\Controller;
*/
class AggregateFinanceSetController extends Controller {
public $apihost;
public $cpapihost;
public $token = "LYHTQDJS";
public function _initialize(){
$apihost = M("Kv")->field("value")->where("`key` = 'aggregate_finance_api'")->find();
$cpapihost = M("Kv")->field("value")->where("`key` = 'aggregate_cp_settlement_api'")->find();
if(empty($apihost) || empty($cpapihost)){
echo "请先设置请求接口aggregate_finance_api和aggregate_cp_settlement_api的值".PHP_EOL;die;
if(empty($apihost)){
echo "请先设置请求接口aggregate_finance_api的值".PHP_EOL;die;
}
$this->apihost = $apihost['value'];
$this->cpapihost = $cpapihost['value'];
}
public function setFreeDateCount($begin,$end='')
{
@ -66,15 +63,12 @@ class AggregateFinanceSetController extends Controller {
if($w == 1){
echo $nowdate.":".PHP_EOL;
echo "--周结begin".PHP_EOL;
// $this->setWeekCount($stime);
$this->cpStatement(0,$stime);
$this->setWeekCount($stime);
}
if($d == 1){
echo $nowdate.":".PHP_EOL;
echo "--月结&补点begin".PHP_EOL;
// $this->setMonthCount($stime);
$this->cpStatement(1,$stime);
// $this->cpStatement(2,$stime);
$this->setMonthCount($stime);
}
}else{
echo $nowdate."非周一和月初,无需任何处理".PHP_EOL;
@ -235,144 +229,5 @@ class AggregateFinanceSetController extends Controller {
}
}
return true;
}
/**
* 上游结算
* @param [string] $type 0:周结 1:月结 2周结补点
*/
public function cpStatement($type,$stime)
{
if($type == 0){
$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;
}else{
$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;//上个月的月末时间戳
}
//获取哪些要结算
if($type == 1){
$cpDbRes = M("Partner","tab_")->where("settlement_type = 2")->select();
}else{
$cpDbRes = M("Partner","tab_")->where("settlement_type = 1")->select();
}
$cpList=[];
foreach ($cpDbRes as $k => $v) {
$cpList[$v['id']] =$v;
}
$cp_id = implode(",",array_flip(array_flip(array_column($cpDbRes,'id'))));
//获取本地数据
$locals = $this->getLocalStatementData($begintime,$endtime,$cp_id);
//获取聚合数据
$sign = md5($begintime.$endtime."1".$this->token);
$arr = [
"begintime"=>$begintime,
"endtime"=>$endtime,
"type"=>1,
"sign"=>$sign,
"cp_id"=>$cp_id
];
$dataurl .= $this->cpapihost."&".http_build_query($arr);
$html = file_get_contents($dataurl);
$rsp = json_decode($html,true);
if($rsp['code'] != 1){
echo $rsp['error'].PHP_EOL;die;
}else{
$rsp = $rsp['data'];
}
//整合数据本地
foreach($locals as $k=>$v){
$cpList[$k]["list"] = $v["list"];
}
//整合数据,聚合
foreach($rsp as $k=>$v){
if(isset($cpList[$k]["list"])){
//已经存在
foreach($rsp[$k]["list"] as $ke=>$va){
if(isset($cpList[$k]["list"][$ke])){
$cpList[$k]["list"][$ke]['pay_money'] += $va['pay_money'];
}else{
$cpList[$k]["list"][$ke] = ["pay_money"=>$va['pay_money']];
}
}
}else{
$cpList[$k]["list"] = $v["list"];
}
}
//比例计算及保存
foreach($cpList as $v){
dd($v);
}
# code...
}
/**
* 获取本地上游结算
*/
public function getLocalStatementData($begintime,$endtime,$cp_id)
{
$game_where = "partner_id in ({$cp_id})";
$game = M('game','tab_');
$gameDbRes = $game->field("id,relation_game_name as game_name,partner_id as cp_id,original_package_name")->where($game_where)->select();
$gameList=[];
foreach ($gameDbRes as $k => $v) {
$gameList[$v['id']] =$v;
}
//获取所有id
$gmstr = implode(",",array_flip(array_flip(array_column($gameDbRes,'id'))));
//获取支付记录
$paywhere = [
"pay_status"=>1,
"payed_time"=>["BETWEEN",[$begintime,$endtime]],
"game_id"=>["in",$gmstr]
];
$paydb = M('spend','tab_');
$paylist = $paydb->field("game_id,SUM(pay_amount) pay_amount")->where($paywhere)->group("game_id")->select();
if(empty($paylist)){
return [];
}
//绑定价格
foreach ($paylist as $k => $v) {
$gameList[$v['game_id']]['pay_money'] =$v["pay_amount"];
}
unset($paylist);
//按游戏名称及cp整理数据
$cplList = [];
foreach ($gameList as $k => $v) {
if($v['pay_money'] <= 0) continue; //0值不发送
if(empty($v['original_package_name'])){
$game_name = $v['game_name'];
}else{
$game_name = $v['original_package_name'];
}
$cp_id = $v['cp_id'];
unset($v['cp_id']);
$ymoney = $cplList[$cp_id]['list'][$game_name]["pay_money"]?:0;
$cplList[$cp_id]['list'][$game_name]["pay_money"] =$ymoney+$v["pay_money"];
// if($type==2){
// $cplList[$cp_id]['list'][$game_name]["list"][] =$v; //传递列表详情
// }
}
return $cplList;
}
}
}

@ -0,0 +1,216 @@
<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 聚合渠道结算接口对接
* @author cz
*/
class CompanyStatementSetController extends Controller {
public $cpapihost;
public $token = "LYHTQDJS";
public function _initialize(){
$cpapihost = M("Kv")->field("value")->where("`key` = 'aggregate_cp_settlement_api'")->find();
if(empty($cpapihost)){
echo "请先设置请求接口aggregate_cp_settlement_api的值".PHP_EOL;die;
}
$this->cpapihost = $cpapihost['value'];
}
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("结束时间不能大于当前");
}
$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]);
}
}
public function setDailyCount($stime="now")
{
if(!IS_CLI){
// die("只支持脚本访问");
}
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->cpStatement(0,$stime);
}
if($d == 1){
echo $nowdate.":".PHP_EOL;
echo "--月结&补点begin".PHP_EOL;
$this->cpStatement(1,$stime);
// $this->cpStatement(2,$stime);
}
}else{
echo $nowdate."非周一和月初,无需任何处理".PHP_EOL;
}
}
/**
* TODO:以下为上下游结算
* 上游结算
* @param [string] $type 0:周结 1:月结 2周结补点
*/
public function cpStatement($type,$stime)
{
if($type == 0){
$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;
}else{
$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;//上个月的月末时间戳
}
//获取哪些要结算
if($type == 1){
$cpDbRes = M("Partner","tab_")->where("settlement_type = 2")->select();
}else{
$cpDbRes = M("Partner","tab_")->where("settlement_type = 1")->select();
}
$cpList=[];
foreach ($cpDbRes as $k => $v) {
$cpList[$v['id']] =$v;
}
$cp_id = implode(",",array_flip(array_flip(array_column($cpDbRes,'id'))));
//获取本地数据
$locals = $this->getLocalStatementData($begintime,$endtime,$cp_id);
//获取聚合数据
$sign = md5($begintime.$endtime."1".$this->token);
$arr = [
"begintime"=>$begintime,
"endtime"=>$endtime,
"type"=>1,
"sign"=>$sign,
"cp_id"=>$cp_id
];
$dataurl .= $this->cpapihost."&".http_build_query($arr);
$html = file_get_contents($dataurl);
$rsp = json_decode($html,true);
if($rsp['code'] != 1){
echo $rsp['error'].PHP_EOL;die;
}else{
$rsp = $rsp['data'];
}
//整合数据本地
foreach($locals as $k=>$v){
$cpList[$k]["list"] = $v["list"];
}
//整合数据,聚合
foreach($rsp as $k=>$v){
if(isset($cpList[$k]["list"])){
//已经存在
foreach($rsp[$k]["list"] as $ke=>$va){
if(isset($cpList[$k]["list"][$ke])){
$cpList[$k]["list"][$ke]['pay_money'] += $va['pay_money'];
}else{
$cpList[$k]["list"][$ke] = ["pay_money"=>$va['pay_money']];
}
}
}else{
$cpList[$k]["list"] = $v["list"];
}
}
//比例计算及保存
foreach($cpList as $v){
dd($v);
}
# code...
}
/**
* 获取本地上游结算
*/
public function getLocalStatementData($begintime,$endtime,$cp_id)
{
$game_where = "partner_id in ({$cp_id})";
$game = M('game','tab_');
$gameDbRes = $game->field("id,relation_game_name as game_name,partner_id as cp_id,original_package_name")->where($game_where)->select();
$gameList=[];
foreach ($gameDbRes as $k => $v) {
$gameList[$v['id']] =$v;
}
//获取所有id
$gmstr = implode(",",array_flip(array_flip(array_column($gameDbRes,'id'))));
//获取支付记录
$paywhere = [
"pay_status"=>1,
"payed_time"=>["BETWEEN",[$begintime,$endtime]],
"game_id"=>["in",$gmstr]
];
$paydb = M('spend','tab_');
$paylist = $paydb->field("game_id,SUM(pay_amount) pay_amount")->where($paywhere)->group("game_id")->select();
if(empty($paylist)){
return [];
}
//绑定价格
foreach ($paylist as $k => $v) {
$gameList[$v['game_id']]['pay_money'] =$v["pay_amount"];
}
unset($paylist);
//按游戏名称及cp整理数据
$cplList = [];
foreach ($gameList as $k => $v) {
if($v['pay_money'] <= 0) continue; //0值不发送
if(empty($v['original_package_name'])){
$game_name = $v['game_name'];
}else{
$game_name = $v['original_package_name'];
}
$cp_id = $v['cp_id'];
unset($v['cp_id']);
$ymoney = $cplList[$cp_id]['list'][$game_name]["pay_money"]?:0;
$cplList[$cp_id]['list'][$game_name]["pay_money"] =$ymoney+$v["pay_money"];
// if($type==2){
// $cplList[$cp_id]['list'][$game_name]["list"][] =$v; //传递列表详情
// }
}
return $cplList;
}
}

@ -1846,3 +1846,5 @@ CREATE TABLE `tab_company_relation_log` (
KEY `settlement_type` (`settlement_type`) USING BTREE,
KEY `status` (`status`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='公司关系绑定申请记录';

Loading…
Cancel
Save