聚合财务1

master
chenzhi 5 years ago
parent 744cbcf7ea
commit 95cd9993e2

@ -0,0 +1,320 @@
<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 后台总览聚合
* @author cz
*/
class AggregateFinanceSetController extends Controller {
// public $beginTime;
// public $endTime;
// public $date;
// public $nowdata;
// public $UserModel;
// public $ChartModel;
// public $LoginModel;
// public $SpendModel;
// public $PromoteModel;
// public $GameCountModel;
// public $addid;
// public $adddata;
// public $gameid;
// public $gamedata;
// public $reset=false;
public $apihost;
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'];
//初始化
// $this->UserModel =M("User","tab_");
// $this->LoginModel =M("user_login_record","tab_");
// $this->SpendModel =M("spend","tab_");
// $this->PromoteModel =M("promote","tab_");
// $this->model =M();
// $this->PromoteCountModel =M("PromoteCount","tab_");
// $this->GameCountModel =M("GameCount","tab_");
// $this->nowdata =time();
// $group_concat_max_len = M()->execute("SET SESSION group_concat_max_len = 4294967295");
// echo "set group_concat_max_len ok,beging count".PHP_EOL;
// if(I("reset")) $this->reset = true;
}
public function setDaliyCount()
{
$w = (int)date("w");
$d = (int)date("d");
$this->setDateCount($begin);
}
public function setWeekCount()//进行周结
{
echo $this->apihost;
# code...
}
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 > time()){
die("结束时间不能包含今日");
}
if($begin == $end){
$this->setDateCount($begin);
}else{
$starttime = $begin?strtotime($begin):mktime(0,0,0,date('m'),date('d')-1,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->setDateCount($datelist[$i]);
}
}
}
public function setDateCount($date)
{
$this->date = $date;
$tarry = explode('-',$date);
$this->beginTime=mktime(0,0,0,$tarry[1],$tarry[2],$tarry[0]);
$this->endTime=mktime(0,0,0,$tarry[1],$tarry[2]-0+1,$tarry[0])-1;
set_time_limit(0);
$t1 = microtime(true);
//判断是否存在
$PromoteRes = $this->PromoteCountModel->where("date = '{$this->beginTime}'")->find();
$GameRes = $this->GameCountModel->where("date = '{$this->beginTime}'")->find();
$reset='';
if(!empty($PromoteRes) || !empty($GameRes) ){
if($this->reset){
$this->PromoteCountModel->where("date = '{$this->beginTime}'")->delete();
$this->GameCountModel->where("date = '{$this->beginTime}'")->delete();
$reset = "reset and creat ";
}else{
echo ("{$this->date} 已统计,请勿重复提交".PHP_EOL);
return;
}
}
$this->model->startTrans();
$this->addid = [];
$this->adddata = [];
$this->promoteNew();
$this->promotePayMoeny();
$this->promoteActive();
$this->promoteCreateDb();
// unset($this->addid);
// unset($this->adddata);
$this->gameid = [];
$this->gamedata = [];
$this->gameNew();
$this->gamePayMoeny();
$this->gameActive();
$this->gameCreateDb();
$this->model->commit();
$t2 = microtime(true);
echo ("{$this->date} {$reset}success runtime:".round($t2-$t1,3).'s'.PHP_EOL);
# code...
}
//=======================聚合推广员========================
//1聚合新增
public function promoteNew()
{
$map = ['register_time'=>['between',[$this->beginTime,$this->endTime]],"puid"=>0,"promote_id"=>["gt",0]];
$promoteNew = $this->UserModel->field('COUNT(1) AS new_user_count,promote_id,promote_account')
->where($map)
->group("promote_id")
->select();
$count = count($promoteNew);
for ($i=0; $i < $count; $i++) {
$this->addid[] = $promoteNew[$i]["promote_id"];
$this->adddata[$promoteNew[$i]["promote_id"]] = $promoteNew[$i];
}
unset($promoteNew);
}
//2聚合付费
public function promotePayMoeny()
{
$map = ['pay_time'=>['between',[$this->beginTime,$this->endTime]],"pay_status"=>1,"promote_id"=>["gt",0]];//1支付成功
$promotePayMoeny = $this->SpendModel->field('sum(pay_amount) AS pay_money_count,promote_id,promote_account')
->where($map)
->group("promote_id")
->select();
$count = count($promotePayMoeny);
for ($i=0; $i < $count; $i++) {
if (in_array($promotePayMoeny[$i]["promote_id"],$this->addid)) {
//存在
$this->adddata[$promotePayMoeny[$i]["promote_id"]]["pay_money_count"] = $promotePayMoeny[$i]["pay_money_count"];
}else{
$this->addid[] = $promotePayMoeny[$i]["promote_id"];
$this->adddata[$promotePayMoeny[$i]["promote_id"]] = $promotePayMoeny[$i];
}
}
unset($promotePayMoeny);
}
//3聚合活跃
public function promoteActive()
{
$map = ['login_time'=>['between',[$this->beginTime,$this->endTime]],"promote_id"=>["gt",0]];//0不是小号
$promoteActive = $this->LoginModel->field('COUNT(DISTINCT user_id) AS active_user_count,GROUP_CONCAT(DISTINCT user_id) active_user_list,promote_id')
->where($map)
->group("promote_id")
->select();
$count = count($promoteActive);
for ($i=0; $i < $count; $i++) {
if (in_array($promoteActive[$i]["promote_id"],$this->addid)) {
//存在
$this->adddata[$promoteActive[$i]["promote_id"]]["active_user_count"] = $promoteActive[$i]["active_user_count"];
$this->adddata[$promoteActive[$i]["promote_id"]]["active_user_list"] = $promoteActive[$i]["active_user_list"];
}else{
$this->addid[] = $promoteActive[$i]["promote_id"];
$this->adddata[$promoteActive[$i]["promote_id"]] = $promoteActive[$i];
}
}
unset($promoteActive);
}
//4加入数据库
public function promoteCreateDb()
{
$count = count($this->addid);
if ($count > 0) {
foreach ($this->adddata as $key => $value) {
$value['date']=$this->beginTime;
$value['create_time']=$this->nowdata;
$value['active_user_list'] = @json_encode(array_map("intFun",explode(",",$value['active_user_list'])));
if(empty($value['promote_account']) && $value['promote_id']){
if($value['promote_id'] == 0){
$value['promote_account'] = "官方渠道";
}else{
$value['promote_account'] = $this->PromoteModel->field("account")->where("id = {$value['promote_id']}")->find()['account'];
if(!$value['promote_account']){
$value['promote_account']="未知推广员";
}
}
}
$tempdbres = $this->PromoteCountModel->add($value);
if($tempdbres === false){
$this->model->rollback();
die("error");
}
}
}
}
//=======================聚合游戏========================
//1聚合新增
public function gameNew()
{
$map = ['register_time'=>['between',[$this->beginTime,$this->endTime]],"puid"=>0,"fgame_id"=>["gt",0]];//0不是小号
$gameNew = $this->UserModel->field('COUNT(1) AS new_user_count,fgame_id game_id,fgame_name game_name')
->where($map)
->group("fgame_id")
->select();
$count = count($gameNew);
for ($i=0; $i < $count; $i++) {
$this->gameid[] = $gameNew[$i]["game_id"];
$this->gamedata[$gameNew[$i]["game_id"]] = $gameNew[$i];
}
unset($gameNew);
}
//2聚合付费
public function gamePayMoeny()
{
$map = ['pay_time'=>['between',[$this->beginTime,$this->endTime]],"pay_status"=>1,"game_id"=>["gt",0]];//1支付成功
$gamePayMoeny = $this->SpendModel->field('sum(pay_amount) AS pay_money_count,game_id,game_name')
->where($map)
->group("game_id")
->select();
$count = count($gamePayMoeny);
for ($i=0; $i < $count; $i++) {
if (in_array($gamePayMoeny[$i]["game_id"],$this->gameid)) {
//存在
$this->gamedata[$gamePayMoeny[$i]["game_id"]]["pay_money_count"] = $gamePayMoeny[$i]["pay_money_count"];
}else{
$this->gameid[] = $gamePayMoeny[$i]["game_id"];
$this->gamedata[$gamePayMoeny[$i]["game_id"]] = $gamePayMoeny[$i];
}
}
unset($gamePayMoeny);
}
//3聚合活跃
public function gameActive()
{
$map = ['login_time'=>['between',[$this->beginTime,$this->endTime]],"game_id"=>["gt",0]];//0不是小号
$gameActive = $this->LoginModel->field('COUNT(DISTINCT user_id) AS active_user_count,GROUP_CONCAT(DISTINCT user_id) active_user_list,game_id,game_name')
->where($map)
->group("game_id")
->select();
$count = count($gameActive);
for ($i=0; $i < $count; $i++) {
if (in_array($gameActive[$i]["game_id"],$this->gameid)) {
//存在
$this->gamedata[$gameActive[$i]["game_id"]]["active_user_count"] = $gameActive[$i]["active_user_count"];
$this->gamedata[$gameActive[$i]["game_id"]]["active_user_list"] = $gameActive[$i]["active_user_list"];
}else{
$this->gameid[] = $gameActive[$i]["game_id"];
$this->gamedata[$gameActive[$i]["game_id"]] = $gameActive[$i];
}
}
unset($gameActive);
}
//4加入数据库
public function gameCreateDb()
{
$count = count($this->gameid);
if ($count > 0) {
foreach ($this->gamedata as $key => $value) {
$value['date']=$this->beginTime;
$value['create_time']=$this->nowdata;
$value['active_user_list'] = @json_encode(array_map("intFun",explode(",",$value['active_user_list'])));
$tempdbres = $this->GameCountModel->add($value);
if($tempdbres === false){
$this->model->rollback();
die("error");
}
}
}
}
}

@ -1605,3 +1605,39 @@ CREATE TABLE `tab_finance_compare_info` (
KEY `pay_way` (`pay_way`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='财务导入对账详情表';
--kv
CREATE TABLE `sys_kv` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`key` varchar(50) COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'key',
`value` varchar(1000) COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'value',
`type` varchar(50) DEFAULT NULL COMMENT '类型说明',
`remark` varchar(1000) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`),
UNIQUE KEY `key_name` (`key`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='系统简单kv储存表';
INSERT INTO `platform`.`sys_kv` (`key`, `value`, `type`, `remark`) VALUES ('aggregate_finance_api', 'http://admin.cz.com/index.php?g=api&m=FinanceWeekCount&a=returnFinanceAccountsDataList', 'string', '聚合下游结算Api地址');
CREATE TABLE `tab_aggregate_statement` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`statement_type` tinyint(2) DEFAULT '0' COMMENT '对账类型(0-周结,1月结,2补点)',
`company_id` int(11) DEFAULT '0' COMMENT '对账公司id',
`company_name` varchar(60) NOT NULL COMMENT '对账公司名称',
`link_phone` varchar(24) DEFAULT NULL COMMENT '对账公司联系人电话',
`statement_begin_time` int(11) NOT NULL DEFAULT '0' COMMENT '对账开始时间',
`statement_end_time` int(11) NOT NULL DEFAULT '0' COMMENT '对账截止时间',
`create_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建对账单时间',
`statement_money` decimal(10,2) DEFAULT '0.00' COMMENT '对账金额',
`pay_amount` decimal(10,2) DEFAULT '0.00' COMMENT '原始金额合计',
`ext_field` longtext COMMENT '凭证地址',
`first_party_info` longtext NOT NULL COMMENT '甲方相关信息json',
`second_party_info` longtext NOT NULL COMMENT '乙方相关信息json',
`statement_info` longtext NOT NULL COMMENT '订单相关信息json',
`verify_status` tinyint(2) DEFAULT '0' COMMENT '审核状态 0:未审核 1:审核通过 2:审核拒绝',
PRIMARY KEY (`id`),
KEY `statement_type` (`statement_type`) USING BTREE,
KEY `company_id` (`company_id`) USING BTREE,
KEY `link_phone` (`link_phone`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT='cp/公会对账单';
Loading…
Cancel
Save