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.

518 lines
19 KiB
PHTML

5 years ago
<?php
namespace Admin\Controller;
class FinanceCompareController extends ThinkController
{
5 years ago
public $zfb = [1];//官方支付宝渠道id
public $wx = [2,3];//微信渠道id
public $ybzf = [17];//易宝渠道id
5 years ago
public $erro_size = 20000;//商家不存在的错误阈值,到达就不执行
5 years ago
public $page_size = 300;//每次执行比较的数量
public $payway = [
"1"=>"支付宝",
"2"=>"微信",
"3"=>"易宝"
];
5 years ago
public $checktype=[
"1"=>"金额不符",
"2"=>"商家数据缺失",
"3"=>"平台数据缺失",
"4"=>"状态不符",
"5"=>"渠道不符"
];
public $readtype=[
"0"=>"未处理",
"1"=>"已处理"
];
5 years ago
public function index($p=1)
{
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$arraypage = $page;
if (isset($_REQUEST['row'])) {
$row = $_REQUEST['row'];
} else {
$row = 10;
}
$map=["check_status"=>1];
if ($_REQUEST['pay_way'] != '') {
$map['pay_way'] = $_REQUEST['pay_way'];
}
5 years ago
$listdata = M("finance_compare","tab_")->where($map)->order("create_time desc");
if(isset($_REQUEST['export'])){
5 years ago
$listdata = $listdata->select();
$count = M("finance_compare","tab_")->where($map)->count();
}else{
5 years ago
$listdata = $listdata->page($page,$row)->select();
$count = count($listdata);
}
foreach ($listdata as $k => &$v) {
$v['begin_time']=date("Y-m-d H:i:s",$v['begin_time']);
$v['end_time']=date("Y-m-d H:i:s",$v['end_time']);
$v['compare_time'] = $v['begin_time']." 至 ".$v['end_time'];
$v['create_time']=date("Y-m-d H:i:s",$v['create_time']);
$v['pay_way']=$this->payway[$v['pay_way']];
}
if(isset($_REQUEST['export'])){
$GetData = $_GET;
unset($GetData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"统计-数据校验-订单校验-导出"]);
data2csv($listdata,'订单校验',array(
"id"=>"序号",
"compare_time"=>"校验时间范围",
"pay_way"=>"支付渠道",
"check_count"=>"校验数量",
"diff_count"=>"异常数量",
"diff_money"=>"异常金额",
"create_time"=>"校验时间"
));
}
$this->checkListOrCountAuthRestMap($map,[]);
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->assign('list_data',$listdata);
$this->assign('pay_way_list',$this->payway);
$this->display();
}
5 years ago
public function add()
{
5 years ago
$this->assign("page_size",$this->page_size);
5 years ago
$this->display();
}
5 years ago
public function list($p=1)
{
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$arraypage = $page;
if (isset($_REQUEST['row'])) {
$row = $_REQUEST['row'];
} else {
$row = 10;
}
if (isset($_REQUEST['compare_id'])) {
$compare_id = $_REQUEST['compare_id'];
}else{
$this->error("参数错误");
}
$map=["type"=>["gt",0],"compare_id"=>$compare_id];
if ($_REQUEST['pay_order_number'] != '') {
$map['pay_order_number'] = $_REQUEST['pay_order_number'];
}
if ($_REQUEST['is_read'] != '') {
$map['is_read'] = $_REQUEST['is_read'];
}
if ($_REQUEST['type'] != '') {
$map['type'] = $_REQUEST['type'];
}
$listdata = M("finance_compare_info","tab_")->where($map);
if(isset($_REQUEST['export'])){
$listdata = $listdata->select();
}else{
$listdata = $listdata->page($page,$row)->select();
}
foreach ($listdata as $k => &$v) {
$v['pay_way']=$this->payway[$v['pay_way']];
$v['type']=$this->checktype[$v['type']];
$v['read_str']=$this->readtype[$v['is_read']];
}
if(isset($_REQUEST['export'])){
$GetData = $_GET;
unset($GetData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"统计-数据校验-订单校验-订单校验详情-导出"]);
data2csv($listdata,'订单校验详情',array(
"pay_order_number"=>"订单号",
"pay_way"=>"支付渠道",
"read_str"=>"状态",
"pt_pay_amount"=>"平台支付金额",
"sj_pay_amount"=>"商家订单金额",
"diff_money"=>"相差金额",
"type"=>"原因分类"
));
}
$count = M("finance_compare_info","tab_")
->field("count(*) count,
SUM(CASE WHEN is_read = 0 THEN pt_pay_amount ELSE 0 END) as noread_pt_pay_amount,
SUM(CASE WHEN is_read = 0 THEN sj_pay_amount ELSE 0 END) as noread_sj_pay_amount,
SUM(CASE WHEN is_read = 0 THEN diff_money ELSE 0 END) as noread_diff_money,
SUM(pt_pay_amount) pt_pay_amount,
SUM(sj_pay_amount) sj_pay_amount,
SUM(diff_money) diff_money")
->where($map)
->find();
$page = set_pagination($count['count'],$row);
if($page) {$this->assign('_page', $page);}
$this->assign('list_data',$listdata);
$this->assign('count_data',$count);
$this->assign('read_type',$this->readtype);
$this->assign('check_type',$this->checktype);
$this->display();
}
public function changeRead()
{
if (isset($_REQUEST['id']) && isset($_REQUEST['is_read']) && isset($_REQUEST['compare_id'])) {
$id = $_REQUEST['id'];
$is_read = $_REQUEST['is_read'];
$compare_id = $_REQUEST['compare_id'];
}else{
$this->error("参数错误");
}
//处理数据
$info = M("finance_compare_info","tab_")->where("id={$id}")->find();
if($info['is_read'] != $is_read){
$this->ajaxReturn(array("error"=>"此单已标记成功请勿重复点击","code"=>40001));
}
//获取
$fc = M("finance_compare","tab_")->where("id={$compare_id}")->find();
if($is_read == 0){
//改为已处理
$fcsavedata = [
"diff_count"=>$fc['diff_count']-1,
"diff_money"=>$fc['diff_money']-$info['diff_money'],
];
$isavedata = [
"is_read"=>1
];
}else{
//改为未处理
$fcsavedata = [
"diff_count"=>$fc['diff_count']-0+1,
"diff_money"=>$fc['diff_money']-0+$info['diff_money'],
];
$isavedata = [
"is_read"=>0
];
}
//保存
M("finance_compare","tab_")->where("id={$compare_id}")->save($fcsavedata);
M("finance_compare_info","tab_")->where("id={$id}")->save($isavedata);
$this->ajaxReturn(array("success"=>"处理成功","code"=>0));
}
/**
* 创建比较基础数据
*/
5 years ago
public function creatCompare()
{
5 years ago
$adddata = I("post.");
5 years ago
5 years ago
$adddata['create_time'] = time();
$adddata['begin_time'] = strtotime($adddata['begin_time']);
$adddata['end_time'] = strtotime($adddata['end_time'])+ 86399;
$res = M("finance_compare","tab_")->add($adddata);
if($res){
$this->ajaxReturn(array("success"=>"ok","code"=>0,"data"=>["id"=>$res]));
}else{
$this->ajaxReturn(array("error"=>"creat error","code"=>4000));
}
5 years ago
}
5 years ago
/**
* 循环比较数据
*/
5 years ago
public function compareData()
{
5 years ago
$checkdata = I('POST.');
$keys = $checkdata['keys'];
$compare_id = $checkdata['compare_id'];
$pay_way = $checkdata['pay_way'];//1支付宝 2微信 17易宝
$data = $checkdata['checkarr'];
5 years ago
//过滤数据
5 years ago
$sp=[];
$pf=[];
$er=[];
foreach($data as $k=>$v){
5 years ago
$v[$keys[0]] = trim($v[$keys[0]]);
$v[$keys[1]] = trim($v[$keys[1]]);
5 years ago
$pay_where = substr($v[$keys[0]], 0, 2);
$tmp = [
5 years ago
"pt_pay_amount"=>0,
5 years ago
"diff_money"=>$v[$keys[1]],
5 years ago
"sj_pay_amount"=>$v[$keys[1]],
5 years ago
"type"=>3,
"pay_order_number"=>$v[$keys[0]],
"compare_id"=>$compare_id,
"pay_way"=>$pay_way,
"is_read"=>0
];
5 years ago
5 years ago
if($pay_where == 'SP'){
5 years ago
$tmp['order_type']=1;
5 years ago
$sp[$v[$keys[0]]] = $tmp;
}elseif($pay_where == 'PF'){
5 years ago
$tmp['order_type']=2;
5 years ago
$pf[$v[$keys[0]]] = $tmp;
}else{
5 years ago
$tmp['order_type']=0;
5 years ago
$er[$v[$keys[0]]] = $tmp;
}
}
//判断游戏流水
if(count($sp) > 0){
$keys = implode(",",array_keys($sp));
$where = ["pay_order_number"=>["in",$keys]];
$sres = M("Spend","tab_")->field("pay_order_number,pay_amount,pay_status,pay_way")->where($where)->select();
if($sres){
$this->setDiffData($sres,$sp);
}
5 years ago
$saveres = $this->saveCheckData($sp);
if($saveres == false){
5 years ago
$this->delCompare($compare_id);
5 years ago
$this->ajaxReturn(array("error"=>"添加失败","code"=>40001));
}
}
//判断平台币流水
if(count($pf) > 0){
$keys = implode(",",array_keys($pf));
$where = ["pay_order_number"=>["in",$keys]];
$sres = M("Deposit","tab_")->field("pay_order_number,pay_amount,pay_status,pay_way")->where($where)->select();
if($sres){
$this->setDiffData($sres,$pf);
}
$saveres = $this->saveCheckData($pf);
if($saveres == false){
5 years ago
$this->delCompare($compare_id);
5 years ago
$this->ajaxReturn(array("error"=>"添加失败","code"=>40002));
}
}
//不存在订单添加
if(count($er) > 0){
$saveerrores = $this->saveCheckData($er);
if($saveerrores == false){
5 years ago
$this->delCompare($compare_id);
5 years ago
$this->ajaxReturn(array("error"=>"添加失败","code"=>40003));
}
}
$this->ajaxReturn(array("success"=>"ok","code"=>0,"data"=>[]));
}
/**
* 验证平台游戏流水
*/
public function checkSpendData()
{
$checkdata = I('POST.');
if ($checkdata['pay_way'] == 1) {
$check_arr = $this->zfb;
}elseif($checkdata['pay_way'] == 2){
$check_arr = $this->wx;
}elseif($checkdata['pay_way'] == 3){
$check_arr = $this->ybzf;
}
$check_arr = implode(",",$check_arr);
//组件sql
$fmap = [
"compare_id"=>$checkdata['compare_id'],
"order_type"=>1
];
5 years ago
//改为join
$fcisql = M("finance_compare_info","tab_")->field("pay_order_number,compare_id")->where($fmap)->select(false);
5 years ago
$map = [
5 years ago
// "pay_order_number"=>["not in",$fcisql],
"s.pay_time" => ['between', [strtotime($checkdata['begin_time']), strtotime($checkdata['end_time']) + 86399]],
"s.pay_status"=>1,
"s.pay_way"=>["in",$check_arr]
5 years ago
];
5 years ago
$res = M("Spend s","tab_")
->field("s.pay_order_number,s.pay_amount,s.pay_status,s.pay_way,ifnull(fc.compare_id,0) compare_id")
->join("( $fcisql ) fc on s.pay_order_number = fc.pay_order_number","left")
->where($map)
->having("compare_id = 0")
->limit($this->erro_size)
->select();
5 years ago
if(count($res) >= $this->erro_size){
5 years ago
$this->delCompare($checkdata['compare_id']);
5 years ago
$this->ajaxReturn(array("error"=>"商家不存在的数据大于{$this->erro_size}条,没有比较的意义,请确认表格的渠道和时间是否正确","code"=>40004));
5 years ago
}
$saveres = true;
if(count($res) > 0){
$add = [];
foreach($res as $k=>$v){
$tmp = [
"pt_pay_amount"=>$v["pay_amount"],
"diff_money"=>$v["pay_amount"],
"sj_pay_amount"=>0,
"type"=>2,
"pay_order_number"=>$v["pay_order_number"],
"compare_id"=>$checkdata['compare_id'],
"pay_way"=>$checkdata['pay_way'],
"order_type"=>1,
"is_read"=>0
];
$add[] = $tmp;
}
unset($res);
$saveres = M("finance_compare_info","tab_")->addAll($add);
}
if($saveres){
$this->ajaxReturn(array("success"=>"ok","code"=>0,"data"=>[]));
}else{
5 years ago
$this->delCompare($checkdata['compare_id']);
5 years ago
$this->ajaxReturn(array("error"=>"比对数据添加失败","code"=>40005));
}
}
/**
* 验证平台币流水
*/
public function checkDepositData()
{
$checkdata = I('POST.');
if ($checkdata['pay_way'] == 1) {
$check_arr = $this->zfb;
}elseif($checkdata['pay_way'] == 2){
$check_arr = $this->wx;
}elseif($checkdata['pay_way'] == 3){
$check_arr = $this->ybzf;
}
$check_arr = implode(",",$check_arr);
//组件sql
$fmap = [
"compare_id"=>$checkdata['compare_id'],
"order_type"=>2
];
5 years ago
$fcisql = M("finance_compare_info","tab_")->field("pay_order_number,compare_id")->where($fmap)->select(false);
5 years ago
$map = [
"create_time" => ['between', [strtotime($checkdata['begin_time']), strtotime($checkdata['end_time']) + 86399]],
"pay_status"=>1,
"pay_way"=>["in",$check_arr]
];
5 years ago
$res = M("Deposit s","tab_")
->field("s.pay_order_number,s.pay_amount,s.pay_status,s.pay_way,ifnull(fc.compare_id,0) compare_id")
->join("( $fcisql ) fc on s.pay_order_number = fc.pay_order_number","left")
->where($map)
->having("compare_id = 0")
->limit($this->erro_size)
->select();
5 years ago
if(count($res) >= $this->erro_size){
5 years ago
$this->delCompare($checkdata['compare_id']);
5 years ago
$this->ajaxReturn(array("error"=>"商家不存在的数据大于{$this->erro_size}条,没有比较的意义,请确认表格的渠道和时间是否正确","code"=>40004));
5 years ago
}
$saveres = true;
if(count($res) > 0){
$add = [];
foreach($res as $k=>$v){
$tmp = [
"pt_pay_amount"=>$v["pay_amount"],
"diff_money"=>$v["pay_amount"],
"sj_pay_amount"=>0,
"type"=>2,
"pay_order_number"=>$v["pay_order_number"],
"compare_id"=>$checkdata['compare_id'],
"pay_way"=>$checkdata['pay_way'],
"order_type"=>2,
"is_read"=>0
];
$add[] = $tmp;
}
unset($res);
$saveres = M("finance_compare_info","tab_")->addAll($add);
}
if($saveres){
$this->ajaxReturn(array("success"=>"ok","code"=>0,"data"=>[]));
}else{
5 years ago
$this->delCompare($checkdata['compare_id']);
5 years ago
$this->ajaxReturn(array("error"=>"比对数据添加失败","code"=>40005));
5 years ago
}
}
5 years ago
/**
* 生成最终比较统计
*/
public function creatCompareCount()
{
$checkdata = I('POST.');
$compare_id = $checkdata['compare_id'];
//统计
$imap = [
"compare_id"=>$compare_id,
"type"=>["gt",0]
];
5 years ago
$ires = M("finance_compare_info","tab_")->field("IFNULL(sum(diff_money),0) diff_money,IFNULL(count(*),0) count")->where($imap)->find();
5 years ago
$savedata = [
"diff_count"=>$ires['count'],
"diff_money"=>$ires['diff_money'],
"check_status"=>1
];
$res = M("finance_compare","tab_")->where("id='{$compare_id}'")->save($savedata);
if($res){
$this->ajaxReturn(array("success"=>"ok","code"=>0,"data"=>[]));
}else{
5 years ago
$this->delCompare($compare_id);
5 years ago
$this->ajaxReturn(array("error"=>"save error","code"=>40006));
}
5 years ago
5 years ago
}
5 years ago
//比较数据库与原数据
protected function setDiffData($dbres,&$ydata)
5 years ago
{
//type 1:金额不符 2商家数据缺失 3:平台数据缺失 4:状态不符5:渠道不符
foreach ($dbres as $k => $v) {
if(array_key_exists($v['pay_order_number'],$ydata)){
//存在
$flag = true;
5 years ago
$ydata[$v['pay_order_number']]['pt_pay_amount'] = $v['pay_amount'];
5 years ago
$ydata[$v['pay_order_number']]['diff_money'] = abs($ydata[$v['pay_order_number']]['pt_pay_amount']-$ydata[$v['pay_order_number']]['sj_pay_amount']);
5 years ago
if($ydata[$v['pay_order_number']]['diff_money'] > 0){//金额不符
5 years ago
$ydata[$v['pay_order_number']]['type'] = 1;
$flag = false;
continue;
}
if($v['pay_status'] != 1){//状态不符合
$ydata[$v['pay_order_number']]['type'] = 4;
$ydata[$v['pay_order_number']]['diff_money'] = $v['pay_amount'];
$flag = false;
continue;
}
//判断渠道
5 years ago
$way = $ydata[$v['pay_order_number']]['pay_way'];
if ($way == 1) {
5 years ago
$checkarr = $this->zfb;
5 years ago
}elseif($way == 2){
5 years ago
$checkarr = $this->wx;
5 years ago
}elseif($way == 3){
5 years ago
$checkarr = $this->ybzf;
5 years ago
}
if(!in_array($way,$checkarr)){//渠道不符
$ydata[$v['pay_order_number']]['type'] = 5;
$flag = false;
5 years ago
continue;
5 years ago
}
if($flag){//正常
5 years ago
$ydata[$v['pay_order_number']]['type'] = 0;
}
}
}
5 years ago
}
//添加详情
protected function saveCheckData(&$data){
$arr = [];
foreach ($data as $k => $v) {
$arr[] = $v;
}
unset($data);
$res = M("finance_compare_info","tab_")->addAll($arr);
unset($arr);
return $res;
5 years ago
}
5 years ago
//回退删除
protected function delCompare($compare_id){
M("finance_compare_info","tab_")->where("compare_id = '{$compare_id}'")->delete();
M("finance_compare","tab_")->where("id = '{$compare_id}'")->delete();
}
5 years ago
}