1610 lines
70 KiB
PHP
1610 lines
70 KiB
PHP
<?php
|
||
|
||
namespace addon\commission\shop\controller;
|
||
|
||
use addon\commission\job\AllocationLegumesJob;
|
||
use addon\commission\job\ComputeIntegralJob;
|
||
use addon\commission\job\MerchantsSettlementJob;
|
||
use addon\commission\job\PromoterSettlementJob;
|
||
use addon\commission\model\CommissionRecord;
|
||
use addon\commission\model\Legumes;
|
||
use addon\commission\model\Partner;
|
||
use addon\commission\model\WeightValue;
|
||
use addon\fenxiao\model\Fenxiao;
|
||
use addon\weapp\model\Weapp;
|
||
use app\model\NewBaseModel;
|
||
use app\model\shop\ShopAccount;
|
||
use app\shop\controller\BaseShop;
|
||
use addon\commission\model\Setting;
|
||
use DateTime;
|
||
use DateTimeZone;
|
||
use Exception;
|
||
use think\facade\Db;
|
||
use think\facade\Queue;
|
||
|
||
class Test extends BaseShop{
|
||
|
||
public function test(){
|
||
|
||
$res=(new Legumes())->computeYesterdayLegumes();
|
||
|
||
//
|
||
// var_dump($res);die;
|
||
// $orderIds = [257,259,262,267,271];
|
||
// foreach($orderIds as $orderId){
|
||
// $this->createRecord($orderId);
|
||
// }
|
||
// $this->createRecord(354);
|
||
// die;
|
||
|
||
// $this->GiveWeightValueTest();
|
||
// $this->MerchantsSettlement();
|
||
// $this->PromoterSettlement();
|
||
// (new Partner())->settlementInit();
|
||
// $this->PartnerSettlement();
|
||
// (new Legumes())->computeYesterdayLegumes();
|
||
// $this->AllocationLegumes();
|
||
// $this->ComputeIntegral();
|
||
// $this->settlementMoney();
|
||
// $this->useLegumesIntegral();
|
||
// $this->orderRefundFinish();
|
||
|
||
|
||
/*$params = array (
|
||
'order_id' => 280,
|
||
'order_no' => '20240717100137001',
|
||
'site_id' => 5,
|
||
'site_name' => '张先生',
|
||
'website_id' => 0,
|
||
'order_name' => '0.1元买单',
|
||
'order_from' => 'cashier',
|
||
'order_from_name' => '收银台',
|
||
'order_type' => 5,
|
||
'order_type_name' => '收银订单',
|
||
'order_promotion_type' => 0,
|
||
'order_promotion_name' => '',
|
||
'promotion_id' => 0,
|
||
'out_trade_no' => '17211816855317612629',
|
||
'out_trade_no_2' => '',
|
||
'delivery_code' => '',
|
||
'order_status' => 10,
|
||
'order_status_name' => '已完成',
|
||
'order_status_action' => '{"status":10,"name":"已完成","is_allow_refund":1,"icon":"public\\/resource\\/order\\/order-icon-received.png","action":[],"member_action":[],"color":""}',
|
||
'pay_status' => 1,
|
||
'delivery_status' => 0,
|
||
'refund_status' => 0,
|
||
'pay_type' => 'wechatpay',
|
||
'pay_type_name' => '微信支付',
|
||
'delivery_type' => '0',
|
||
'delivery_type_name' => '',
|
||
'member_id' => 37,
|
||
'name' => '',
|
||
'mobile' => '',
|
||
'telephone' => '',
|
||
'province_id' => 0,
|
||
'city_id' => 0,
|
||
'district_id' => 0,
|
||
'community_id' => 0,
|
||
'address' => '',
|
||
'full_address' => '',
|
||
'longitude' => '',
|
||
'latitude' => '',
|
||
'buyer_ip' => '',
|
||
'buyer_ask_delivery_time' => '',
|
||
'buyer_message' => '',
|
||
'goods_money' => '0.10',
|
||
'delivery_money' => '0.00',
|
||
'promotion_money' => '0.00',
|
||
'coupon_id' => 0,
|
||
'coupon_money' => '0.00',
|
||
'invoice_money' => '0.00',
|
||
'order_money' => '0.10',
|
||
'adjust_money' => '0.00',
|
||
'balance_money' => '0.00',
|
||
'pay_money' => '0.10',
|
||
'create_time' => 1721181685,
|
||
'pay_time' => 1721181693,
|
||
'delivery_time' => 0,
|
||
'sign_time' => 0,
|
||
'finish_time' => 1721181693,
|
||
'close_time' => 0,
|
||
'is_lock' => 0,
|
||
'is_evaluate' => 0,
|
||
'is_delete' => 0,
|
||
'is_enable_refund' => 1,
|
||
'remark' => '',
|
||
'goods_num' => '1.000',
|
||
'delivery_store_id' => 0,
|
||
'delivery_status_name' => '',
|
||
'is_settlement' => 0,
|
||
'store_settlement_id' => 0,
|
||
'delivery_store_name' => '',
|
||
'promotion_type' => '',
|
||
'promotion_type_name' => '',
|
||
'promotion_status_name' => '',
|
||
'delivery_store_info' => NULL,
|
||
'virtual_code' => '',
|
||
'evaluate_status' => 0,
|
||
'evaluate_status_name' => '',
|
||
'refund_money' => '0.00',
|
||
'commission' => '0.00',
|
||
'is_invoice' => 0,
|
||
'invoice_type' => 1,
|
||
'invoice_title' => '',
|
||
'taxpayer_number' => '',
|
||
'invoice_rate' => '0.00',
|
||
'invoice_content' => '',
|
||
'invoice_delivery_money' => '0.00',
|
||
'invoice_full_address' => '',
|
||
'is_tax_invoice' => 0,
|
||
'invoice_email' => '',
|
||
'invoice_title_type' => 0,
|
||
'is_fenxiao' => 1,
|
||
'point_money' => '0.00',
|
||
'member_card_money' => '0.00',
|
||
'member_card_order' => 0,
|
||
'invoice_status' => 0,
|
||
'invoice_remark' => NULL,
|
||
'invoice_code' => '',
|
||
'invoice_image' => '',
|
||
'invoice_time' => 0,
|
||
'predict_delivery_time' => 0,
|
||
'is_video_number' => 0,
|
||
'is_sync_order' => 0,
|
||
'is_deduction' => 0,
|
||
'sync_msg' => '',
|
||
'close_cause' => '',
|
||
'cashier_order_type' => 'goods',
|
||
'cashier_sell_time' => 0,
|
||
'cashier_operator_id' => 0,
|
||
'cashier_operator_name' => '',
|
||
'balance' => '0.00',
|
||
'total_balance' => '0.00',
|
||
'store_id' => 8,
|
||
'reduction' => '0.00',
|
||
'round_money' => '0',
|
||
'order_scene' => 'cashier',
|
||
'store_commission_rate' => '0.00',
|
||
'store_commission' => '0.00',
|
||
'cl_id' => 0,
|
||
'cl_name' => '',
|
||
'is_community_settlement' => 0,
|
||
'arrive_time' => 0,
|
||
'delivery_id' => 0,
|
||
'commission_rate' => '0.00',
|
||
'delivery_commission' => '0.00',
|
||
'delivery_community_id' => 0,
|
||
'delivery_community' => '',
|
||
'delivery_community_info' => NULL,
|
||
'legumes_integral_use' => '0.00',
|
||
'legumes_integral_money' => '0.00',
|
||
'legumes_integral_community' => 0.0,
|
||
);*/
|
||
// (new Weapp($params['site_id']))->getRidInfo();
|
||
// debug("结果");
|
||
// (new Weapp($params['site_id']))->authSendGoods($params);
|
||
// 积分收入结算到店铺 调试
|
||
// (new ShopAccount())->orderIntegralSettlement(336);
|
||
// $this->handleOperations();
|
||
// $this->verificationLegumesPrice();
|
||
// 修改豆豆实时价格
|
||
// (new Setting())->setLegumesPrice(0, 7.3859);
|
||
|
||
// $this->afreshComputeLegumes();
|
||
// $this->afreshAllocationLegumes();
|
||
|
||
|
||
|
||
// $this->settlementAccount();
|
||
|
||
|
||
|
||
debug("调试中...". date("Y-m-d H:i:s"));
|
||
}
|
||
// 调试 - 支付成功生成抽成记录
|
||
private function createRecord($orderId){
|
||
|
||
// $orderId = (int)$data['order_id'];
|
||
// $orderId='';
|
||
$adminSet = (new Setting())->getConfig();
|
||
if($adminSet['switch'] != 1) throw new Exception('总平台未开启平台抽成');
|
||
// 当前订单是否已经处理
|
||
$isHas = (int)model('commission_record')->getValue([
|
||
['order_id', '=', $orderId]
|
||
],'id');
|
||
// if($isHas > 0) throw new Exception('当前订单已经处理');
|
||
// 获取订单商品信息
|
||
$field = 'order_goods_id,order_id,site_id,member_id,goods_id,store_id,num,(real_goods_money + legumes_integral_use) as use_money';
|
||
$orderGoodsList = model('order_goods')->getList(['order_id'=>$orderId],$field);
|
||
// 循环处理
|
||
$insertData = [];
|
||
foreach($orderGoodsList as $orderGoodsInfo){
|
||
$useMoney = $orderGoodsInfo['use_money'] ?? 0;
|
||
if($useMoney <= 0) continue;// 当前订单商品有效金额为0
|
||
// 设置获取
|
||
$set = (new Setting())->getFinalSet((int)$orderGoodsInfo['site_id'],(int)$orderGoodsInfo['goods_id']);
|
||
if($set['switch'] != 1) continue;// 当前商品不参与平台抽成
|
||
$integralRate = (float)$set['integral_rate'] ?? 0;
|
||
$operationsRate = (float)$set['operations_rate'] ?? 0;
|
||
$partnerRate = (float)$set['partner_rate'] ?? 0;
|
||
$merchantsRate = (float)$set['merchants_rate'] ?? 0;
|
||
$promoterRate = (float)$set['promoter_rate'] ?? 0;
|
||
// 根据抽成计算类型生成记录信息
|
||
$commissionType = $set['commission_type'] ?? 0;
|
||
$insertData[] = [
|
||
'member_id' => $orderGoodsInfo['member_id'],
|
||
'site_id' => $orderGoodsInfo['site_id'],
|
||
'store_id' => $orderGoodsInfo['store_id'],
|
||
'order_id' => $orderGoodsInfo['order_id'],
|
||
'order_goods_id' => $orderGoodsInfo['order_goods_id'],
|
||
'goods_id' => $orderGoodsInfo['goods_id'],
|
||
'real_goods_money' => $useMoney,
|
||
// 根据计算类型获取值 抽成类型:0=比例,1=固定金额
|
||
'integral_rate' => $commissionType == 1 ? -1 : $integralRate,
|
||
'integral_money' => $commissionType == 1 ? sprintf("%.3f",$orderGoodsInfo['num'] * $integralRate) : sprintf("%.3f",$useMoney * $integralRate / 100),
|
||
'operations_rate' => $operationsRate,
|
||
'operations_money' => sprintf("%.3f",$useMoney * $operationsRate / 100),
|
||
'partner_rate' => $commissionType == 1 ? -1 : $partnerRate,
|
||
'partner_money' => $commissionType == 1 ? sprintf("%.3f",$orderGoodsInfo['num'] * $partnerRate) : sprintf("%.3f",$useMoney * $partnerRate / 100),
|
||
'merchants_rate' => $commissionType == 1 ? -1 : $merchantsRate,
|
||
'merchants_money' => $commissionType == 1 ? sprintf("%.3f",$orderGoodsInfo['num'] * $merchantsRate) : sprintf("%.3f",$useMoney * $merchantsRate / 100),
|
||
'promoter_rate' => $commissionType == 1 ? -1 : $promoterRate,
|
||
'promoter_money' => $commissionType == 1 ? sprintf("%.3f",$orderGoodsInfo['num'] * $promoterRate) : sprintf("%.3f",$useMoney * $promoterRate / 100),
|
||
];
|
||
}
|
||
var_dump($insertData);
|
||
//
|
||
//
|
||
//
|
||
//
|
||
// if(count($insertData) > 0){
|
||
//// model('commission_record')->addList($insertData);
|
||
// // 支付成功 - 抽成记录生成后 - 招商员佣金结算
|
||
// Queue::push(MerchantsSettlementJob::class,[
|
||
// 'order_id' => (int)$orderId
|
||
// ]);
|
||
// // 支付成功 - 抽成记录生成后 - 推广员佣金结算
|
||
// Queue::push(PromoterSettlementJob::class,[
|
||
// 'order_id' => (int)$orderId,
|
||
// ]);
|
||
// }
|
||
}
|
||
// 调试 - 支付成功赠送权重值
|
||
private function GiveWeightValueTest(){
|
||
// $data = [
|
||
// 'member_id' => 21,
|
||
// 'order_id' => 224,
|
||
// 'site_id' => 2,
|
||
// ];
|
||
// try{
|
||
// // 获取当前用户 所有有效上级的 权重值持有信息
|
||
// $userHoldList = (new WeightValue())->getUserHoldList((int)$data['member_id'], (int)$data['site_id']);
|
||
//
|
||
// debug($userHoldList);
|
||
// // 获取当前订单中所有商品
|
||
// $orderGoodsField = 'order_goods_id,order_id,goods_id,(real_goods_money + point_money) as use_money';
|
||
// $orderGoodsList = model('order_goods')->getList([['order_id','=', $data['order_id']]], $orderGoodsField);
|
||
// $updateData = [];
|
||
// $insertLogData = [];
|
||
// foreach($userHoldList as $userInfo){
|
||
// foreach($orderGoodsList as $goodsInfo){
|
||
// // 判断:当前用户是否存在修改记录中 不存在添加修改信息
|
||
// if(empty($updateData[$userInfo['id']])){
|
||
// $updateData[$userInfo['id']] = [
|
||
// 'id' => $userInfo['id'],
|
||
// 'quantity' => $userInfo['quantity'],
|
||
// ];
|
||
// }
|
||
// // 持有数量增加
|
||
// $changeFront = (float)$updateData[$userInfo['id']]['quantity'];
|
||
// $updateData[$userInfo['id']]['quantity'] = (float)sprintf("%.2f",(float)$goodsInfo['use_money'] + $changeFront);
|
||
// // 记录变更记录
|
||
// $insertLogData[] = [
|
||
// 'site_id' => (int)$data['site_id'],
|
||
// 'member_id' => $userInfo['member_id'],
|
||
// 'level_id' => $userInfo['level_id'],
|
||
// 'goods_id' => $goodsInfo['goods_id'],
|
||
// 'order_id' => $goodsInfo['order_id'],
|
||
// 'order_goods_id' => $goodsInfo['order_goods_id'],
|
||
// 'change_type' => 1,
|
||
// 'change_quantity' => $goodsInfo['use_money'],
|
||
// 'change_front' => $changeFront,
|
||
// 'change_after' => (float)$updateData[$userInfo['id']]['quantity'],
|
||
// 'remark' => '下线购买商品赠送',
|
||
// ];
|
||
// }
|
||
// }
|
||
//
|
||
//
|
||
// debug($insertLogData);
|
||
// // 数据结果处理
|
||
// if(count($insertLogData) > 0){
|
||
// // 修改
|
||
// // $goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_weight_value', 'pk' => 'id']));
|
||
// // $goodsSkuModel->saveAll($updateData);
|
||
// // model('commission_weight_value_log')->addList($insertLogData);
|
||
// }
|
||
// }
|
||
// catch(\Exception $e){
|
||
// $data['error_msg'] = $e->getMessage();
|
||
// debug(['失败:调试中...'=>$data]);
|
||
// }
|
||
}
|
||
// 调试 - 招商员佣金结算
|
||
private function MerchantsSettlement(){
|
||
$orderId = (int)48;
|
||
// 当前订单
|
||
// $where = [
|
||
// // ['order_id', '=', $orderId],
|
||
// ['merchants_status', '=', 0],
|
||
// ];
|
||
// $field = 'a.id,a.site_id,a.store_id,a.order_id,a.merchants_money,s.merchants_member_id,s.store_name';
|
||
// $join = [
|
||
// ['store s', 's.store_id = a.store_id', 'LEFT'], // 关联客服
|
||
// ];
|
||
// $list = model('commission_record')->getList($where,$field,'','a',$join);
|
||
// // 循环处理
|
||
// $updateData = [];
|
||
// $insertData = [];
|
||
// foreach($list as $recordInfo){
|
||
// // 根据是否存在招商员 进行对应的处理
|
||
// if((int)$recordInfo['merchants_member_id'] > 0){
|
||
// // 存在招商员 招商员佣金结算
|
||
// $insertData[] = [
|
||
// 'site_id' => $recordInfo['site_id'],
|
||
// 'member_id' => $recordInfo['merchants_member_id'],
|
||
// 'join_id' => $recordInfo['id'],
|
||
// 'account_type' => 'merchants',
|
||
// 'account_type_name' => '招商员佣金',
|
||
// 'account_data' => $recordInfo['merchants_money'],
|
||
// 'remark' => $recordInfo['store_name'] . '的订单收入',
|
||
// ];
|
||
// // 修改信息
|
||
// $updateData[] =[
|
||
// 'id' => $recordInfo['id'],
|
||
// 'merchants_status' => 1,
|
||
// ];
|
||
// }
|
||
// else{
|
||
// // 不存在招商员
|
||
// $updateData[] =[
|
||
// 'id' => $recordInfo['id'],
|
||
// 'merchants_status' => 2,
|
||
// ];
|
||
// }
|
||
// }
|
||
// // 数据结果处理
|
||
// if(count($insertData) > 0) model('commission_account')->addList($insertData);
|
||
// if(count($updateData) > 0){
|
||
// $updateModel = (new NewBaseModel(['table_name' => 'commission_record', 'pk' => 'id']));
|
||
// $updateModel->saveAll($updateData);
|
||
// }
|
||
|
||
|
||
|
||
// debug([
|
||
// '修改内容'=> $updateData,
|
||
// '循环列表' => $list,
|
||
// '$insertData' => $insertData,
|
||
// '$updateData' => $updateData,
|
||
//
|
||
// ]);
|
||
}
|
||
// 调试 - 推广员佣金结算
|
||
private function PromoterSettlement(){
|
||
// $orderId = (int)48;
|
||
// // 当前订单
|
||
// $where = [
|
||
// // ['order_id', '=', $orderId],
|
||
// ['promoter_status', '=', 0],
|
||
// ];
|
||
// $join = [
|
||
// ['member m', 'a.member_id = m.member_id', 'LEFT'],
|
||
// ['fenxiao fx', 'm.fenxiao_id = fx.fenxiao_id', 'LEFT'],
|
||
// ];
|
||
// $field = 'a.id,a.site_id,a.member_id,a.promoter_money,m.is_fenxiao,m.nickname,fx.member_id as fenxiao_member_id,fx.parent';
|
||
// $list = model('commission_record')->getList($where,$field,'','a', $join);
|
||
// // 循环处理
|
||
// $updateData = [];
|
||
// $insertData = [];
|
||
// foreach($list as $recordInfo){
|
||
// // 判断:当前用户是否为分销 获取对应的上级分销商用户id
|
||
// if($recordInfo['is_fenxiao'] == 1){
|
||
// // 当前用户是分销商 使用parent查询上级用户id
|
||
// $parentMemberId = model('fenxiao')->getValue(['fenxiao_id'=>$recordInfo['parent']], 'member_id');
|
||
// }else{
|
||
// // 当前用户不是分销商 直接为fenxiao_member_id
|
||
// $parentMemberId = $recordInfo['fenxiao_member_id'];
|
||
// }
|
||
// // 判断:是否存在上级
|
||
// if((int)$parentMemberId > 0){
|
||
// // 存在上级
|
||
// $insertData[] = [
|
||
// 'site_id' => $recordInfo['site_id'],
|
||
// 'member_id' => $parentMemberId,
|
||
// 'join_id' => $recordInfo['id'],
|
||
// 'account_type' => 'promoter',
|
||
// 'account_type_name' => '推广员佣金',
|
||
// 'account_data' => $recordInfo['promoter_money'],
|
||
// 'remark' => $recordInfo['nickname'] . '的推广佣金',
|
||
// ];
|
||
// // 修改信息
|
||
// $updateData[] =[
|
||
// 'id' => $recordInfo['id'],
|
||
// 'promoter_status' => 1,
|
||
// ];
|
||
// }else{
|
||
// // 不存在上级
|
||
// $updateData[] =[
|
||
// 'id' => $recordInfo['id'],
|
||
// 'promoter_status' => 2,
|
||
// ];
|
||
// }
|
||
// }
|
||
// // 数据结果处理
|
||
// if(count($insertData) > 0) model('commission_account')->addList($insertData);
|
||
// if(count($updateData) > 0){
|
||
// $updateModel = (new NewBaseModel(['table_name' => 'commission_record', 'pk' => 'id']));
|
||
// $updateModel->saveAll($updateData);
|
||
// }
|
||
// debug([
|
||
// '修改内容'=> $updateData,
|
||
// '循环列表' => $list,
|
||
// '$insertData' => $insertData,
|
||
// '$updateData' => $updateData,
|
||
//
|
||
// ]);
|
||
}
|
||
// 调试 - 合伙人佣金结算
|
||
private function PartnerSettlement(){
|
||
// $data = [
|
||
// 'cycle_ids' => [
|
||
// 0 => 5,
|
||
// ],
|
||
// ];
|
||
//
|
||
// $cycleIds = $data['cycle_ids'] ?? [];
|
||
// if(count($cycleIds) <= 0) throw new Exception('无周期记录信息!');
|
||
// // 获取周期结算信息
|
||
// $cycleList = model('commission_partner_cycle')->getList([
|
||
// ['id', 'in', $cycleIds]
|
||
// ],'id,start_time,end_time');
|
||
// $cycleList = array_column($cycleList, null, 'id');
|
||
// // 获取结算记录信息
|
||
// $list = model('commission_partner')->getList([
|
||
// ['cycle_id', 'in', $cycleIds],
|
||
// ['is_settlement', '=', 0],
|
||
// ],'id,member_id,cycle_id,money,site_id');
|
||
// if(count($list) > 0){
|
||
// $insertData = [];
|
||
// foreach($list as $singleInfo){
|
||
// $cycleInfo = $cycleList[$singleInfo['cycle_id']] ?? [];
|
||
// $timePeriod = date("Y-m-d H:i:s",$cycleInfo['start_time']) . ' ~ ' . date("Y-m-d H:i:s",$cycleInfo['end_time']);
|
||
// $insertData[] = [
|
||
// 'site_id' => $singleInfo['site_id'],
|
||
// 'member_id' => $singleInfo['member_id'],
|
||
// 'join_id' => $singleInfo['id'],
|
||
// 'account_type' => 'partner',
|
||
// 'account_type_name' => '合伙人佣金',
|
||
// 'account_data' => $singleInfo['money'],
|
||
// 'remark' => $timePeriod . "权重值收入",
|
||
// ];
|
||
// }
|
||
// // 记录信息
|
||
// if(count($insertData) > 0) model('commission_account')->addList($insertData);
|
||
// // 修改结算状态
|
||
// $ids = array_column($list,'id');
|
||
// model('commission_partner')->update([ 'is_settlement' => 1],[
|
||
// ['id', 'in', $ids]
|
||
// ]);
|
||
// }
|
||
|
||
debug("结束");
|
||
}
|
||
// 调试 - 豆豆分配
|
||
private function AllocationLegumes(){
|
||
$data = [
|
||
'legumes_id' => 29,
|
||
];
|
||
|
||
// 获取周期信息
|
||
$legumesId = $data['legumes_id'];
|
||
$cycleLegumes = model('commission_legumes')->getInfo(['id' => $legumesId]);
|
||
if(!$cycleLegumes) throw new Exception('信息不存在');
|
||
// if((int)$cycleLegumes['status'] != 0) throw new Exception('当前豆豆已分配!');
|
||
// 获取时间段所有消费用户
|
||
$where = [
|
||
[ 'a.pay_time', 'between', [ $cycleLegumes['start_time'], $cycleLegumes['end_time'] ] ],// 指定时间段内
|
||
[ 'og.refund_status', 'in', [0, 3] ],// 订单商品未退款
|
||
[ 'a.order_status', 'in', [1, 3, 4, 10, 11, 12] ],//待发货、已发货、已收货、已完成、待使用、已使用
|
||
];
|
||
$join = [
|
||
[ 'order_goods og', 'og.order_id = a.order_id', 'right' ],
|
||
];
|
||
$field = 'og.order_id,og.order_goods_id,og.member_id,a.order_status,(sum(og.real_goods_money) + sum(og.legumes_integral_money)) as sum_money';
|
||
$orderList = model('order')->getList($where,$field,'','a',$join,'og.order_goods_id');
|
||
|
||
debug($where);
|
||
$totalMoney = array_sum(array_column($orderList,'sum_money'));
|
||
// debug([$totalMoney,implode(',', array_column($orderList,'order_id')),$orderList]);
|
||
// 获取平台抽成信息
|
||
$orderGoodsIds = array_column($orderList,'order_goods_id');
|
||
$commissionRecord = model('commission_record')->getList([
|
||
['order_goods_id','in',$orderGoodsIds],
|
||
],'order_goods_id,order_id,member_id,integral_money as platform_commission_money');
|
||
$totalPlatformCommission = array_sum(array_column($commissionRecord,'platform_commission_money'));
|
||
$commissionRecord = array_column($commissionRecord,null,'order_goods_id');
|
||
// 循环处理
|
||
$insertData = [];
|
||
foreach($orderList as $orderInfo){
|
||
// 获取平台抽成信息
|
||
$platformCommissionInfo = $commissionRecord[$orderInfo['order_goods_id']] ?? [];
|
||
if($platformCommissionInfo){
|
||
// 计算订单金额占比 必须进行100的偏移计算
|
||
$rate = (float)sprintf("%.3f",$platformCommissionInfo['platform_commission_money'] / $totalPlatformCommission * 100);
|
||
$getLegumes = sprintf("%.3f",$cycleLegumes['legumes_num'] * $rate / 100);
|
||
// 信息记录
|
||
$insertData[] = [
|
||
'member_id' => $orderInfo['member_id'],
|
||
'legumes_id' => $legumesId,
|
||
'order_id' => $orderInfo['order_id'],
|
||
'order_goods_id' => $orderInfo['order_goods_id'],
|
||
'cycle_total_legumes' => $cycleLegumes['legumes_num'],
|
||
'total_sales_money' => $totalMoney,
|
||
'order_money' => $orderInfo['sum_money'],
|
||
'order_money_rate' => $rate,
|
||
'get_legumes' => $getLegumes,
|
||
'status' => in_array((int)$orderInfo['order_status'],[10,12]) ? 1 : 0,
|
||
];
|
||
}
|
||
}
|
||
//
|
||
debug($insertData);
|
||
// // 添加数据
|
||
// if(count($insertData) > 0) model('commission_legumes_log')->addList($insertData);
|
||
// // 修改豆豆周期分配状态
|
||
// model('commission_legumes')->update(['status' => 1],[
|
||
// ['id', '=', $legumesId]
|
||
// ]);
|
||
// // 触发全平台豆豆转积分操作
|
||
// Queue::push(ComputeIntegralJob::class);
|
||
}
|
||
// 调试 - 全平台豆豆转积分
|
||
private function ComputeIntegral($data = []){
|
||
// // 判断:当前data不为数组时 强制为数组
|
||
// if(!is_array($data)) $data = [];
|
||
// // 并且开启处理
|
||
// $page = $data['page'] ?? 1;
|
||
// $ids = $data['ids'] ?? [];
|
||
// $limit = 1000;
|
||
// $legumesPrice = (new Setting())->getLegumesPrice();
|
||
// // 获取当前页数据 有效记录、已获取积分低于订单金额
|
||
// $where = [
|
||
// ['status', 'in', [0,1]]
|
||
// ];
|
||
// if(count($ids) > 0) $where[] = ['id', 'in', $ids];
|
||
// else $where[] = ['', 'exp', Db::raw('get_integral < order_money')];
|
||
// $field = 'id,(order_money - refund_order_money) as order_money,(get_legumes - refund_get_legumes) as get_legumes,get_integral,member_id';
|
||
// $result = model('commission_legumes_log')->pageList($where,$field,'',$page,$limit);
|
||
// $count = $result['count'] ?? 0;
|
||
// $list = $result['list'] ?? [];
|
||
// if(count($list) <= 0) throw new \Exception('无处理数据!');
|
||
// // 循环处理
|
||
// $updateData = [];
|
||
// $insertData = [];
|
||
// foreach($list as $item){
|
||
// // 计算最新本订单商品 总释放积分;总释放积分不能超过订单金额
|
||
// $getIntegral = (float)sprintf("%.2f",$item['get_legumes'] * $legumesPrice);
|
||
// $resultGetIntegral = $getIntegral >= $item['order_money'] ? $item['order_money'] : $getIntegral;
|
||
// // 修改信息记录
|
||
// $updateData[] = [
|
||
// 'id' => $item['id'],
|
||
// 'get_integral' => $resultGetIntegral
|
||
// ];
|
||
// // 增加释放记录
|
||
// $insertData[] = [
|
||
// 'member_id' => $item['member_id'],
|
||
// 'legumes_log_id' => $item['id'],
|
||
// 'change_front' => $item['get_integral'],
|
||
// 'change_quantity' => (float)sprintf("%.2f",$resultGetIntegral - $item['get_integral']),
|
||
// 'change_after' => $resultGetIntegral,
|
||
// ];
|
||
// }
|
||
//
|
||
// // 修改
|
||
// $newBaseModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
|
||
// $newBaseModel->saveAll($updateData);
|
||
// // 增加记录
|
||
// if(count($insertData) > 0) model('commission_legumes_release_log')->addList($insertData);
|
||
// // 判断:是否存在下一页
|
||
// $currentLimit = $page * $limit;
|
||
// if($currentLimit < $count){
|
||
// $data['page'] = $page + 1;
|
||
// Queue::push(ComputeIntegralJob::class,$data);
|
||
// }
|
||
}
|
||
// 调试 - 流水结算到账
|
||
private function settlementMoney(){
|
||
|
||
$data = [
|
||
// 'account_type' => 'partner',
|
||
//
|
||
'order_id' => 355,
|
||
'account_type' => 'merchants_promoter',
|
||
];
|
||
|
||
|
||
Db::startTrans();
|
||
try{
|
||
// trace($data, '平台抽成 - 流水结算到账 - 开始');
|
||
|
||
// 根据结算流水类型 获取对应条件
|
||
$where = [
|
||
['status', '=', 0]
|
||
];
|
||
if($data['account_type'] == 'partner') {
|
||
// 合伙人佣金
|
||
$where[] = ['account_type', '=', 'partner'];
|
||
}
|
||
else if($data['account_type'] == 'merchants_promoter') {
|
||
// 推广员佣金、招商员佣金
|
||
$where[] = ['account_type', 'in', ['promoter', 'merchants']];
|
||
// 仅结算指定订单相关
|
||
$joinIds = model('commission_record')->getColumn([
|
||
['order_id', '=', $data['order_id']]
|
||
],'id');
|
||
$where[] = ['join_id', 'in', $joinIds];
|
||
}
|
||
// 流水列表
|
||
// $list = model('commission_account')->getList($where,'id,sum(account_data) as total_account_data,member_id','','a',[],'member_id');
|
||
$list = Db::name('commission_account')
|
||
->field('id,sum(account_data) as total_account_data,member_id')
|
||
->where('status', 0)
|
||
->when($data['account_type'] == 'partner',function($query){
|
||
$query->where('account_type', 'partner');
|
||
},function($query) use ($data){
|
||
// 仅结算指定订单相关
|
||
$joinIds = model('commission_record')->getColumn([
|
||
['order_id', '=', $data['order_id']]
|
||
],'id');
|
||
$query->whereIn('account_type', ['promoter', 'merchants'])
|
||
->whereIn('join_id', $joinIds);
|
||
})
|
||
->group('member_id')
|
||
->select()
|
||
->toArray();
|
||
|
||
|
||
|
||
|
||
debug($list);
|
||
|
||
// trace($list, '平台抽成 - 流水结算到账 - 流水列表');
|
||
// 获取用户已经持有信息
|
||
$memberIds = array_column($list, 'member_id');
|
||
$userList = Db::name('member')
|
||
->field('commission_money,member_id')
|
||
->whereIn('member_id', $memberIds)
|
||
->select()
|
||
->toArray();
|
||
$userList = array_column($userList, null, 'member_id');
|
||
// 循环流水列表 结算信息
|
||
foreach($list as $singleInfo){
|
||
$userList[$singleInfo['member_id']]['commission_money'] += $singleInfo['total_account_data'];
|
||
}
|
||
// 修改
|
||
// $newBaseModel = (new NewBaseModel(['table_name' => 'member', 'pk' => 'member_id']));
|
||
// $userList = array_values($userList);
|
||
// $newBaseModel->saveAll($userList);
|
||
// // 已结算
|
||
// model('commission_account')->update(['status'=>1],[
|
||
// ['id', 'in', array_column($list, 'id')]
|
||
// ]);
|
||
|
||
|
||
|
||
// trace($userList, '平台抽成 - 流水结算到账 - 处理完成');
|
||
Db::commit();
|
||
|
||
debug([
|
||
'用户信息修改列表' => $userList,
|
||
'处理流水列表' => $list
|
||
]);
|
||
}
|
||
catch(\Exception $e){
|
||
Db::rollback();
|
||
$data['error_msg'] = $e->getMessage();
|
||
|
||
debug($data);
|
||
// trace($data, '平台抽成 - 流水结算到账 - 失败');
|
||
}
|
||
}
|
||
// 调试 - 订单支付成功 使用豆豆积分
|
||
private function useLegumesIntegral(){
|
||
// $data = [
|
||
// 'order_id' => 66
|
||
// ];
|
||
//
|
||
//
|
||
//
|
||
// // 获取订单信息
|
||
// $orderInfo = model('order')->getInfo([
|
||
// ['order_id','=', $data['order_id']],
|
||
// ['legumes_integral_use','>', 0]
|
||
// ],'order_id,site_id,member_id,legumes_integral_use,legumes_integral_money');
|
||
// if(!$orderInfo) throw new \Exception('信息不存在!');
|
||
// $useLegumesIntegral = (float)$orderInfo['legumes_integral_use'];
|
||
// // 查询需要使用的分配记录
|
||
// $useLegumesLogList = (new Legumes())->getUseList((int)$orderInfo['member_id'],(float)$orderInfo['legumes_integral_use']);
|
||
// // 循环处理
|
||
// $updateData = [];
|
||
// foreach($useLegumesLogList as $logInfo){
|
||
// $useIntegral = $useLegumesIntegral >= $logInfo['surplus_integral'] ? (float)$logInfo['surplus_integral'] : (float)$useLegumesIntegral;
|
||
// $useLegumesIntegral = (float)sprintf("%.2f",$useLegumesIntegral - $useIntegral);
|
||
// $updateData[] = [
|
||
// 'id' => $logInfo['id'],
|
||
// 'use_integral' => (float)sprintf("%.2f",$logInfo['use_integral'] + $useIntegral)
|
||
// ];
|
||
// if($useLegumesIntegral <= 0) break;
|
||
// }
|
||
// // 修改成功信息
|
||
// if($updateData){
|
||
// // 修改豆豆积分使用信息
|
||
// $goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
|
||
// $goodsSkuModel->saveAll($updateData);
|
||
// // 添加用户账单信息变更记录
|
||
// $mark = '购买商品使用'.$orderInfo['legumes_integral_use'].'积分抵扣'.floatval($orderInfo['legumes_integral_money']).'元';
|
||
// model('commission_account')->add([
|
||
// 'site_id' => $orderInfo['site_id'],
|
||
// 'member_id' => $orderInfo['member_id'],
|
||
// 'join_id' => $orderInfo['order_id'],
|
||
// 'account_type' => 'use_legumes_integral',
|
||
// 'account_type_name' => '积分使用',
|
||
// 'account_data' => '-'.$orderInfo['legumes_integral_use'],
|
||
// 'status' => 1,
|
||
// 'remark' => $mark,
|
||
// ]);
|
||
// }
|
||
|
||
|
||
|
||
|
||
}
|
||
// 退款处理
|
||
private function orderRefundFinish(){
|
||
/* $data = array (
|
||
'order_goods_id' => 235,
|
||
'order_id' => 218,
|
||
'order_no' => '20240513135030313361',
|
||
'site_id' => 2,
|
||
'member_id' => 21,
|
||
'goods_id' => 3,
|
||
'sku_id' => 4,
|
||
'sku_name' => '削笔机削笔器 ',
|
||
'sku_image' => 'https://aidian-1251027355.cos.ap-chengdu.myqcloud.com/upload/2/common/images/20240425/20240425030636171402879621442.jpg',
|
||
'sku_no' => '',
|
||
'is_virtual' => 0,
|
||
'goods_class' => '1',
|
||
'goods_class_name' => '实物商品',
|
||
'price' => '29.90',
|
||
'cost_price' => '0.00',
|
||
'num' => '5.000',
|
||
'goods_money' => '149.50',
|
||
'cost_money' => '0.00',
|
||
'delivery_status' => 0,
|
||
'delivery_status_name' => '未发货',
|
||
'delivery_no' => '',
|
||
'gift_flag' => 0,
|
||
'refund_no' => '202405130151256359',
|
||
'refund_status' => 3,
|
||
'refund_status_name' => '维权结束',
|
||
'refund_status_action' => '{"status":3,"name":"维权结束","action":[],"member_action":[]}',
|
||
'refund_type' => 1,
|
||
'refund_apply_money' => '100.00',
|
||
'refund_reason' => '商家对订单进行了主动退款',
|
||
'refund_real_money' => '100.00',
|
||
'refund_delivery_name' => '',
|
||
'refund_delivery_no' => '',
|
||
'refund_time' => 1715579490,
|
||
'refund_refuse_reason' => '',
|
||
'refund_action_time' => 1715579485,
|
||
'real_goods_money' => '148.88',
|
||
'refund_remark' => '商家对订单进行了主动退款',
|
||
'refund_delivery_remark' => '',
|
||
'refund_address' => '',
|
||
'is_refund_stock' => 0,
|
||
'refund_money_type' => 1,
|
||
'shop_active_refund' => 0,
|
||
'shop_refund_remark' => '',
|
||
'refund_mode' => 1,
|
||
'promotion_money' => '0.00',
|
||
'coupon_money' => '0.00',
|
||
'adjust_money' => '0.00',
|
||
'goods_name' => '削笔机削笔器',
|
||
'sku_spec_format' => '',
|
||
'is_fenxiao' => 1,
|
||
'use_point' => 0,
|
||
'point_money' => '0.00',
|
||
'refund_delivery_money' => '0.00',
|
||
'create_time' => 1715579430,
|
||
'out_aftersale_id' => '202405131350303133611715579485235',
|
||
'refund_address_id' => 0,
|
||
'refund_pay_money' => '0.00',
|
||
'store_id' => 2,
|
||
'card_item_id' => 0,
|
||
'card_promotion_money' => '0.00',
|
||
'legumes_integral_use' => '1.10',
|
||
'legumes_integral_money' => '0.62',
|
||
);
|
||
Db::startTrans();
|
||
try{
|
||
if($data['refund_status_action']){
|
||
// 判断:当前商品是否存在豆豆积分抵扣情况
|
||
if((int)$data['legumes_integral_use'] <= 0) throw new Exception('商品不存在豆豆积分抵扣');
|
||
//计算退款比例 单位:%
|
||
$refundRate = (float)sprintf("%.2f",(float)$data['refund_real_money'] / (float)$data['real_goods_money'] * 100);
|
||
// 退款成功 计算平台抽成相关退款内容
|
||
$this->handleCommissionRecord($data['order_goods_id'], $refundRate);
|
||
// 退款成功 权重值相关处理
|
||
$this->handleWeightValue($data['order_goods_id'], $refundRate);
|
||
// 招商员佣金 & 推广员佣金处理
|
||
$this->HandleCommissionMoney($data['order_goods_id'], $refundRate);
|
||
// 合伙人佣金处理
|
||
$this->HandlePartnerMoney($data['order_goods_id'], $refundRate);
|
||
// 积分和豆豆处理
|
||
$this->HandleLegumes($data['order_goods_id'], $refundRate);
|
||
// 抵扣积分退回
|
||
$this->HandleIntegral($data['order_goods_id'], $refundRate);
|
||
}
|
||
|
||
Db::commit();
|
||
}catch(Exception $e){
|
||
Db::rollback();
|
||
|
||
debug('错误:' . $e->getMessage());
|
||
}*/
|
||
}
|
||
// 退款处理 - 平台抽成记录处理
|
||
private function handleCommissionRecord($orderGoodsId, $refundRate){
|
||
$hasRefundRatio = (float)model('commission_record')->getValue(['order_goods_id'=>$orderGoodsId],'refund_ratio');
|
||
$updateData = [
|
||
'refund_ratio' => (float)sprintf("%.2f", $hasRefundRatio + $refundRate)
|
||
];
|
||
if($updateData['refund_ratio'] >= 100){
|
||
$updateData['refund_ratio'] = 100;
|
||
$updateData['partner_status'] = 3;
|
||
$updateData['merchants_status'] = 3;
|
||
$updateData['promoter_status'] = 3;
|
||
}
|
||
model('commission_record')->update($updateData,[
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
]);
|
||
|
||
|
||
return true;
|
||
}
|
||
// 退款处理 - 权重值相关处理
|
||
private function handleWeightValue($orderGoodsId, $refundRate){
|
||
$weightValueLogList = model('commission_weight_value_log')->getList([
|
||
['order_goods_id', '=', $orderGoodsId],
|
||
['change_type', '=', 1],
|
||
['source', '=', 0],
|
||
],'id,member_id,level_id,order_goods_id,change_quantity,goods_id,order_id');
|
||
$insertLogData = [];
|
||
$updateData = [];
|
||
foreach($weightValueLogList as $weightValueLogInfo){
|
||
// 获取用户持有信息
|
||
$holdInfo = model('commission_weight_value')->getInfo([
|
||
['member_id', '=', $weightValueLogInfo['member_id']],
|
||
['level_id', '=', $weightValueLogInfo['level_id']],
|
||
]);
|
||
// 获取已经减少数量
|
||
$reduced = model('commission_weight_value_log')->getSum([
|
||
['order_goods_id', '=', $weightValueLogInfo['order_goods_id']],
|
||
['change_type', '=', 0],
|
||
['source', '=', 1],
|
||
['member_id', '=', $weightValueLogInfo['member_id']],
|
||
], 'change_quantity');
|
||
// 计算减少信息,总减少数量不能超过获取数量
|
||
$reduce = (float)sprintf("%.2f",$weightValueLogInfo['change_quantity'] * $refundRate / 100);
|
||
$totalReduce = (float)sprintf("%.2f",$reduced * $reduce);
|
||
if($totalReduce > $weightValueLogInfo['change_quantity']) $reduce = (float)sprintf("%.2f",$weightValueLogInfo['change_quantity'] - $reduced);
|
||
$changeFront = $holdInfo->quantity;
|
||
$surplusQuantity = (float)sprintf("%.2f",$holdInfo['quantity'] - $reduce);
|
||
$updateData[] = [
|
||
'id' => $holdInfo['id'],
|
||
'quantity' => $surplusQuantity
|
||
];
|
||
// 记录变更记录
|
||
$insertLogData[] = [
|
||
'site_id' => $holdInfo['site_id'],
|
||
'member_id' => $weightValueLogInfo['member_id'],
|
||
'level_id' => $weightValueLogInfo['level_id'],
|
||
'goods_id' => $weightValueLogInfo['goods_id'],
|
||
'order_id' => $weightValueLogInfo['order_id'],
|
||
'order_goods_id' => $weightValueLogInfo['order_goods_id'],
|
||
'change_type' => 0,
|
||
'change_quantity' => $reduce,
|
||
'change_front' => $changeFront,
|
||
'change_after' => (float)$surplusQuantity,
|
||
'remark' => '商品退款,减少权重值',
|
||
'source' => 1,
|
||
];
|
||
}
|
||
|
||
if(count($insertLogData) > 0){
|
||
model('commission_weight_value_log')->addList($insertLogData);
|
||
}
|
||
if(count($updateData) > 0){
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_weight_value', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
}
|
||
|
||
|
||
return true;
|
||
}
|
||
// 退款处理 - 招商员佣金 & 推广员佣金处理
|
||
private function HandleCommissionMoney($orderGoodsId, $refundRate){
|
||
$where = [
|
||
['order_goods_id', '=', $orderGoodsId],
|
||
['merchants_status', '=', 1],
|
||
];
|
||
$field = [
|
||
'a.id',
|
||
'a.site_id',
|
||
'a.store_id',
|
||
'a.order_id',
|
||
'a.order_goods_id',
|
||
'a.merchants_money',
|
||
's.merchants_member_id',
|
||
's.store_name',
|
||
'a.member_id',
|
||
'a.promoter_money',
|
||
'm.is_fenxiao',
|
||
'm.nickname',
|
||
'fx.member_id as fenxiao_member_id',
|
||
'fx.parent'
|
||
];
|
||
$join = [
|
||
['store s', 's.store_id = a.store_id', 'LEFT'], // 关联客服
|
||
['member m', 'a.member_id = m.member_id', 'LEFT'],
|
||
['fenxiao fx', 'm.fenxiao_id = fx.fenxiao_id', 'LEFT'],
|
||
];
|
||
$info = model('commission_record')->getInfo($where,$field,'a',$join);
|
||
if(!$info) return false;
|
||
// 处理招商员佣金
|
||
if((int)$info['merchants_member_id'] > 0){
|
||
// 获取冻结中佣金信息
|
||
$reduce = (float)sprintf("%.3f",$info['merchants_money'] * $refundRate / 100);// 减少数量
|
||
model('commission_account')->add([
|
||
'site_id' => $info['site_id'],
|
||
'member_id' => $info['merchants_member_id'],
|
||
'join_id' => $info['id'],
|
||
'account_type' => 'merchants_refund',
|
||
'account_type_name' => '招商员佣金退款',
|
||
'account_data' => '-' . $reduce,
|
||
'remark' => '订单退款,减少收入',
|
||
'status' => 2,
|
||
]);
|
||
$memberCommissionMoney = model('member')->getValue([['member_id', '=', $info['merchants_member_id']]], 'commission_money');
|
||
model('member')->update(['commission_money' => sprintf("%.2f", $memberCommissionMoney - $reduce)],[
|
||
['member_id', '=', $info['merchants_member_id']],
|
||
]);
|
||
}
|
||
// 处理推广员佣金
|
||
if($info['is_fenxiao'] == 1) $parentMemberId = model('fenxiao')->getValue(['fenxiao_id'=>$info['parent']], 'member_id');// 当前用户是分销商 使用parent查询上级用户id
|
||
else $parentMemberId = $info['fenxiao_member_id'];// 当前用户不是分销商 直接为fenxiao_member_id
|
||
if($parentMemberId > 0){
|
||
$reduce = (float)sprintf("%.3f",$info['promoter_money'] * $refundRate / 100);// 减少数量
|
||
model('commission_account')->add([
|
||
'site_id' => $info['site_id'],
|
||
'member_id' => $parentMemberId,
|
||
'join_id' => $info['id'],
|
||
'account_type' => 'promoter_refund',
|
||
'account_type_name' => '推广员佣金退款',
|
||
'account_data' => '-' . $reduce,
|
||
'remark' => '订单退款,减少收入',
|
||
'status' => 2,
|
||
]);
|
||
$memberCommissionMoney = model('member')->getValue([['member_id', '=', $parentMemberId]], 'commission_money');
|
||
model('member')->update(['commission_money' => sprintf("%.2f", $memberCommissionMoney - $reduce)],[
|
||
['member_id', '=', $parentMemberId],
|
||
]);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
// 退款处理 - 合伙人佣金处理
|
||
private function HandlePartnerMoney($orderGoodsId, $refundRate){
|
||
$commissionRecord = model('commission_record')->getInfo([
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
],'partner_status,create_time,partner_money');
|
||
// 待结算-结算时处理,已结算-减少用户已获得佣金,2、3-无处理
|
||
if($commissionRecord['partner_status'] == 1){
|
||
// 获取:获取当前抽成结算的归属周期
|
||
$createTime = strtotime($commissionRecord['create_time']);
|
||
$cycleInfo = model('commission_partner_cycle')->getInfo([
|
||
['start_time', '<', $createTime],
|
||
['end_time', '>', $createTime],
|
||
]);
|
||
if($cycleInfo){
|
||
// 计算总退款的合伙人佣金
|
||
$reduceCommissionMoney = (float)sprintf("%.3f",$commissionRecord['partner_money'] * $refundRate / 100);// 减少数量
|
||
// 获取退款的用户列表
|
||
$userList = model('commission_partner')->getList([
|
||
['cycle_id', '=', $cycleInfo['cycle_id']],
|
||
],'id,site_id,member_id,money,proportion,cycle_id');
|
||
$insertData = [];
|
||
foreach($userList as $partnerInfo){
|
||
// 计算当前用户应扣除合伙人佣金
|
||
$reduce = (float)sprintf("%.2f",$reduceCommissionMoney * $partnerInfo['proportion'] / 100);
|
||
// 记录账单
|
||
$insertData = [
|
||
'site_id' => $partnerInfo['site_id'],
|
||
'member_id' => $partnerInfo['member_id'],
|
||
'join_id' => $partnerInfo['id'],
|
||
'account_type' => 'partner_refund',
|
||
'account_type_name' => '合伙人佣金退款',
|
||
'account_data' => '-' . $reduce,
|
||
'remark' => "订单退款,减少收入",
|
||
'status' => 2,
|
||
];
|
||
$memberCommissionMoney = model('member')->getValue([['member_id', '=', $partnerInfo['member_id']]], 'commission_money');
|
||
model('member')->update(['commission_money' => sprintf("%.2f", $memberCommissionMoney - $reduce)],[
|
||
['member_id', '=', $partnerInfo['member_id']],
|
||
]);
|
||
}
|
||
// 记录账单变化信息
|
||
if (count($insertData) > 0) model('commission_account')->addList($insertData);
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
// 退款处理 - 积分和豆豆处理
|
||
private function HandleLegumes($orderGoodsId, $refundRate){
|
||
// 获取当前抽成信息
|
||
$commissionRecord = model('commission_record')->getInfo([
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
],'id,create_time');
|
||
// 判断:当前抽成豆豆信息是否已经结算,未结算-无操作处理(结算时处理退款内容);已结算-处理退款相关内容
|
||
$maxEndTime = model('commission_legumes')->getMax([],'end_time');
|
||
$createTime = strtotime($commissionRecord['create_time']);
|
||
if($createTime < $maxEndTime){
|
||
// 已结算 - 处理退款相关内容
|
||
$legumesId = model('commission_legumes')->getValue([
|
||
['start_time', '<', $createTime],
|
||
['end_time', '>', $createTime]
|
||
], 'id');
|
||
if($legumesId > 0){
|
||
// 获取需要修改的信息列表
|
||
$correlationList = model('commission_legumes_log')->getList([
|
||
['legumes_id', '=', $legumesId]
|
||
], 'id,member_id,order_money,refund_order_money,get_legumes,refund_get_legumes,get_integral');
|
||
// 循环处理
|
||
$updateData = [];
|
||
foreach($correlationList as $correlationInfo){
|
||
// 计算 退款金额
|
||
$reduceOrderMoney = (float)sprintf("%.2f",$correlationInfo['order_money'] * $refundRate / 100);
|
||
$refundOrderMoney = (float)sprintf("%.3f", $correlationInfo['refund_order_money'] + $reduceOrderMoney);
|
||
if($refundOrderMoney > $correlationInfo['order_money']) $refundOrderMoney = $correlationInfo['order_money'];
|
||
// 计算 退款豆豆
|
||
$reduceGetLegumes = (float)sprintf("%.2f",$correlationInfo['get_legumes'] * $refundRate / 100);
|
||
$refundGetLegumes = (float)sprintf("%.3f", $correlationInfo['refund_get_legumes'] + $reduceGetLegumes);
|
||
if($refundGetLegumes > $correlationInfo['get_legumes']) $refundGetLegumes = $correlationInfo['get_legumes'];
|
||
// 记录修改信息
|
||
$updateData[] = [
|
||
'id' => $correlationInfo['id'],
|
||
'refund_order_money' => $refundOrderMoney,
|
||
'refund_get_legumes' => $refundGetLegumes
|
||
];
|
||
}
|
||
|
||
if(count($updateData) > 0) {
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
Queue::push(ComputeIntegralJob::class,[
|
||
'ids' => array_column($correlationList,'id')
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
// 退款处理 - 抵扣积分退回
|
||
private function HandleIntegral($orderGoodsId, $refundRate){
|
||
// 获取订单商品信息
|
||
$orderProductInfo = model('order_goods')->getInfo([
|
||
['order_goods_id', '=', $orderGoodsId]
|
||
],'site_id,member_id,order_id,legumes_integral_use');
|
||
$useLegumesIntegral = $orderProductInfo['legumes_integral_use'] ?? 0;
|
||
$memberId = $orderProductInfo['member_id'] ?? 0;
|
||
if($useLegumesIntegral > 0 && $memberId > 0){
|
||
// 计算退回积分
|
||
$returnIntegral = (float)sprintf("%.2f",$useLegumesIntegral * $refundRate / 100);
|
||
// 获取退回的记录列表
|
||
$useLegumesLogList = (new Legumes())->getUsedList((int)$memberId,(float)$returnIntegral);
|
||
// 循环处理
|
||
$returnComputeIntegral = $returnIntegral;// 计算用 退回积分
|
||
$updateData = [];
|
||
foreach($useLegumesLogList as $logInfo){
|
||
// 当前分配信息可退回积分 剩余可退回积分
|
||
$reduce = $returnComputeIntegral < $logInfo['use_integral'] ? $returnComputeIntegral : $logInfo['use_integral'];
|
||
$returnComputeIntegral = (float)sprintf("%.2f",$returnComputeIntegral - $reduce);
|
||
$updateData[] = [
|
||
'id' => $logInfo['id'],
|
||
'use_integral' => (float)sprintf("%.2f",$logInfo['use_integral'] - $reduce)
|
||
];
|
||
if($returnComputeIntegral <= 0) break;
|
||
}
|
||
if($updateData){
|
||
// 修改
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
// 添加用户账单信息变更记录
|
||
$mark = '订单退款 - 退回抵扣积分:'.$returnIntegral;
|
||
model('commission_account')->add([
|
||
'site_id' => $orderProductInfo['site_id'],
|
||
'member_id' => $memberId,
|
||
'join_id' => $orderProductInfo['order_id'],
|
||
'account_type' => 'refund_legumes_integral',
|
||
'account_type_name' => '积分退回',
|
||
'account_data' => $returnIntegral,
|
||
'status' => 2,
|
||
'remark' => $mark,
|
||
]);
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
// 运营抽成处理 - 区分技术和平台
|
||
private function handleOperations(){
|
||
$list = model('commission_record')->getList([
|
||
['operations_money', '>', 0]
|
||
],'id,operations_money');
|
||
$updateData = [];
|
||
foreach($list as $item){
|
||
$operationsMoney = (float)$item['operations_money'];// 运营抽成总金额
|
||
$operationsTechnologyMoney = (float)sprintf("%.3f",$operationsMoney * 0.2);// 运营抽成 - 技术公司所得(20%)
|
||
$operationsPlatformMoney = (float)sprintf("%.3f",$operationsMoney - $operationsTechnologyMoney);// 运营抽成 - 平台所得(80%)
|
||
|
||
$updateData[] = [
|
||
'id' => $item['id'],
|
||
'operations_technology_money' => $operationsTechnologyMoney,
|
||
'operations_platform_money' => $operationsPlatformMoney,
|
||
];
|
||
}
|
||
// debug($updateData);
|
||
if($updateData){
|
||
// 修改
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_record', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
}
|
||
}
|
||
// 验证 - 每日豆豆价格是否正确
|
||
private function verificationLegumesPrice(){
|
||
$list = model('commission_legumes')->getList('','','id ASC');
|
||
foreach($list as $item){
|
||
echo "<div style='width: 100vw;
|
||
display: inline-flex;
|
||
flex-direction: row;
|
||
flex-wrap: nowrap;
|
||
align-items: center;
|
||
justify-content: center;'>";
|
||
echo "<div style='margin-right: 50px'>";
|
||
echo "数据库存储数据<br />";
|
||
echo "时间段:" . date("Y-m-d H:i:s", $item['start_time']) . ' ~ ' . date("Y-m-d H:i:s", $item['end_time']) . "<br />";
|
||
echo date("Y-m-d H:i:s", $item['start_time'])."豆豆价格:" . $item['legumes_price'] . "<br />";
|
||
echo date("Y-m-d H:i:s", $item['start_time'])."产生豆豆数量:" . $item['legumes_num'] . "<br />";
|
||
echo date("Y-m-d H:i:s", $item['end_time'])."豆豆价格:" . $item['tomorrow_legumes_price'] . "<br />";
|
||
echo "</div>";
|
||
// 计算豆豆价格 豆豆价格 = 积分抽成金额(全平台总金额) ➗ 文创豆数量(全平台总数量)
|
||
echo "校验数据 重新根据抽成记录进行计算<br />";
|
||
$total_money = (float)model('commission_record')->getSum([
|
||
[ 'create_time', '<=', date("Y-m-d H:i:s", $item['start_time']) ]
|
||
], 'integral_money');// 总佣金
|
||
// 获取退款金额
|
||
$refund_money = (float)model('commission_record')->getValue([
|
||
[ 'create_time', '<=', date("Y-m-d H:i:s", $item['start_time']) ]
|
||
],'sum((integral_money * refund_ratio / 100)) as refund_money');
|
||
// 实际金额
|
||
$reality_money = (float)sprintf("%.3f",(float)$total_money - (float)$refund_money);
|
||
// 以产生豆豆
|
||
$allLegumes = model('commission_legumes')->getSum([
|
||
[ 'start_time', '<=', $item['start_time'] ]
|
||
],'legumes_num');
|
||
|
||
echo "截止".date("Y-m-d H:i:s", $item['start_time'])."<br />";
|
||
echo date("Y-m-d H:i:s", $item['start_time'])."前全平台总积分抽成佣金:{$total_money}<br />";
|
||
echo date("Y-m-d H:i:s", $item['start_time'])."前全平台总积分抽成佣金退款金额:{$refund_money}<br />";
|
||
echo date("Y-m-d H:i:s", $item['start_time'])."前全平台实际有效总积分抽成佣金:{$reality_money}<br />";
|
||
echo date("Y-m-d H:i:s", $item['start_time'])."前全平台已产生豆豆:{$allLegumes}<br />";
|
||
if($total_money > 0) echo date("Y-m-d H:i:s", $item['start_time'])."豆豆价格:".sprintf("%.4f",$reality_money / $allLegumes)."<br />";
|
||
else echo date("Y-m-d H:i:s", $item['start_time'])."豆豆价格:0.5<br />";
|
||
echo "</div><hr />";
|
||
}
|
||
|
||
|
||
debug("结束");
|
||
}
|
||
|
||
|
||
// 重新计算豆豆相关数据
|
||
private function afreshComputeLegumes(){
|
||
|
||
$legumesModel = new Legumes();
|
||
$list = model('commission_legumes')->getList([
|
||
['id', '>', 23]
|
||
]);
|
||
$legumesPrice = 6.31;//0.5;
|
||
$allLegumes = model('commission_legumes')->getSum([
|
||
['id', '<=', 23]
|
||
], 'legumes_num');
|
||
$updateData = [];
|
||
|
||
foreach($list as $legumes){
|
||
// 获取周期内积分抽成信息
|
||
$integralInfo = $legumesModel->getIntegralInfo($legumes['start_time'],$legumes['end_time']);
|
||
// if((float)$integralInfo['reality_money'] <= 0){
|
||
// echo "ID:{$legumes['id']}无积分抽成信息";
|
||
// continue;
|
||
// }
|
||
// 计算豆豆数量 豆豆数量 = 积分抽成金额(昨日总金额) * 0.2 / 豆豆当前价格
|
||
$legumesNum = (float)sprintf("%.4f",(float)$integralInfo['reality_money'] * 0.2 / $legumesPrice);
|
||
// 计算豆豆价格 下次豆豆价格 = 积分抽成金额(全平台总金额) ➗ 文创豆数量(全平台总数量)
|
||
$allIntegralInfo = $legumesModel->getIntegralInfo(1704076126, $legumes['end_time']);
|
||
// $allLegumes = model('commission_legumes')->getSum([
|
||
// [ 'end_time', 'between', [ 1704076126, $legumes['end_time'] ] ]
|
||
// ],'legumes_num');
|
||
$allLegumes += $legumesNum;
|
||
$tomorrowLegumesPrice = sprintf("%.4f",$allIntegralInfo['reality_money'] / $allLegumes);
|
||
|
||
// 2024-05-26 00:00:00
|
||
// debug(model('commission_record')->getList([
|
||
// [ 'create_time', 'between', [ date("Y-m-d H:i:s",1704076126), '2024-05-26 00:00:00' ] ]
|
||
// ], 'id,integral_money,create_time'));
|
||
|
||
echo "<pre>";
|
||
print_r([
|
||
'ID' => $legumes['id'],
|
||
'时间戳' => $legumes['start_time'] . '~' . $legumes['end_time'] ,
|
||
'时间' => date("Y-m-d H:i:s", $legumes['start_time']) . '~' . date("Y-m-d H:i:s", $legumes['end_time']) ,
|
||
date("Y-m-d", $legumes['start_time']) . '价格' => $legumesPrice,
|
||
date("Y-m-d", $legumes['end_time']) . '价格' => $tomorrowLegumesPrice,
|
||
date("Y-m-d", $legumes['start_time']) . '豆豆数量' => $legumesNum,
|
||
date("Y-m-d H:i:s", $legumes['start_time']) . '~' . date("Y-m-d H:i:s", $legumes['end_time']) . '积分抽成信息' => [
|
||
'总金额' => $integralInfo['total_money'],
|
||
'退款减少金额' => $integralInfo['refund_money'],
|
||
'实际有效' => $integralInfo['reality_money'],
|
||
],
|
||
"截至".date("Y-m-d H:i:s", $legumes['end_time']).'全平台抽成信息' => [
|
||
'总金额' => $allIntegralInfo['total_money'],
|
||
'退款减少金额' => $allIntegralInfo['refund_money'],
|
||
'实际有效' => $allIntegralInfo['reality_money'],
|
||
],
|
||
"截至".date("Y-m-d H:i:s", $legumes['end_time']).'全平台豆豆数量' => $allLegumes,
|
||
]);
|
||
|
||
|
||
|
||
$updateData[] = [
|
||
'id' => $legumes['id'],
|
||
'total_integral_money' => (float)$integralInfo['total_money'],// 当前周期积分抽成总金额
|
||
'refund_money' => (float)$integralInfo['refund_money'],// 未结算前 - 已退款金额
|
||
'reality_money' => (float)$integralInfo['reality_money'],// 实际结算时拥有的基金池总数
|
||
'legumes_price' => (float)$legumesPrice,// 豆豆价格(当日价格)
|
||
'legumes_num' => (float)$legumesNum,// 豆豆数量(当日产生数量)
|
||
'tomorrow_legumes_price' => (float)$tomorrowLegumesPrice,// 豆豆价格(明日价格)
|
||
'status' => (float)0,// 状态:0=待分配,1=已分配
|
||
];
|
||
|
||
echo "<pre><hr />";
|
||
$legumesPrice = $tomorrowLegumesPrice;
|
||
}
|
||
// 修改信息
|
||
// $goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_legumes', 'pk' => 'id']));
|
||
// $goodsSkuModel->saveAll($updateData);
|
||
//
|
||
// (new Setting())->setLegumesPrice(0, $legumesPrice);
|
||
|
||
|
||
|
||
debug("结束");
|
||
}
|
||
// 豆豆重新分配
|
||
private function afreshAllocationLegumes(){
|
||
$list = model('commission_legumes')->getList([
|
||
['id', '>', 23]
|
||
]);
|
||
|
||
$updateData = [];
|
||
|
||
foreach($list as $item){
|
||
$logList = model('commission_legumes_log')->getList([
|
||
['legumes_id', '=', $item['id']]
|
||
]);
|
||
foreach($logList as $log){
|
||
$updateData[] = [
|
||
'id' => $log['id'],
|
||
'cycle_total_legumes' => (float)$item['legumes_num'],// 当前周期豆豆总数
|
||
'get_legumes' => (float)sprintf("%.4f",$item['legumes_num'] * $log['order_money_rate'] / 100),// 当前订单实际获得豆豆
|
||
];
|
||
}
|
||
}
|
||
// 修改豆豆积分使用信息
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
|
||
|
||
// 修改豆豆周期分配状态
|
||
$ids = array_column($list, 'id');
|
||
model('commission_legumes')->update(['status' => 1],[
|
||
['id', 'in', $ids]
|
||
]);
|
||
// 触发全平台豆豆转积分操作
|
||
Queue::push(ComputeIntegralJob::class);
|
||
|
||
|
||
debug($updateData);
|
||
}
|
||
// 结算平台抽成相关流水
|
||
private function settlementAccount(){
|
||
$where = [
|
||
['a.status', '=', 0],
|
||
['a.account_type', '=', 'promoter']
|
||
];
|
||
$field = 'a.id,a.account_data,a.member_id,o.order_status,o.order_status_name';
|
||
|
||
$join = [
|
||
['commission_record cr','cr.id = a.join_id', 'left'],
|
||
['order o','o.order_id = cr.order_id', 'left'],
|
||
];
|
||
$list = model('commission_account')->getList($where,$field,'a.id DESC','a',$join);
|
||
// 获取用户已经持有信息
|
||
$memberIds = array_column($list, 'member_id');
|
||
$userList = Db::name('member')
|
||
->field('commission_money,member_id')
|
||
->whereIn('member_id', $memberIds)
|
||
->select()
|
||
->toArray();
|
||
$userList = array_column($userList, null, 'member_id');
|
||
debug($userList,false);
|
||
$ids = [];
|
||
foreach($list as $item){
|
||
if($item['order_status'] == 10){
|
||
$userList[$item['member_id']]['commission_money'] += $item['account_data'];
|
||
$ids[] = $item['id'];
|
||
}
|
||
}
|
||
// // 修改
|
||
// $newBaseModel = (new NewBaseModel(['table_name' => 'member', 'pk' => 'member_id']));
|
||
// $userList = array_values($userList);
|
||
// $newBaseModel->saveAll($userList);
|
||
// // 已结算
|
||
// model('commission_account')->update(['status'=>1],[
|
||
// ['id', 'in', $ids]
|
||
// ]);
|
||
debug($userList);
|
||
}
|
||
|
||
|
||
|
||
/******** 数据库批量处理 ***************************************/
|
||
private function sqlInit(){
|
||
// 不清除数据的表
|
||
$notCleanUp = [
|
||
// 'tk_addon
|
||
// 'tk_adv
|
||
// 'tk_album
|
||
// 'tk_album_pic
|
||
// 'tk_area
|
||
// 'tk_attachments
|
||
// 'tk_cashier_auth
|
||
// 'tk_cashier_auth_group
|
||
// 'tk_cloud_order
|
||
|
||
|
||
// 开发中 ......
|
||
|
||
|
||
// 'tk_config
|
||
// 'tk_cron
|
||
// 'tk_cron_log
|
||
// 'tk_diy_template
|
||
// 'tk_diy_template_goods
|
||
// 'tk_diy_template_goods_item
|
||
// 'tk_diy_theme
|
||
// 'tk_diy_view_util
|
||
// 'tk_document
|
||
// 'tk_express_company
|
||
// 'tk_express_company_template
|
||
// 'tk_express_deliver
|
||
// 'tk_express_delivery_package
|
||
// 'tk_express_template
|
||
// 'tk_express_template_free_shipping
|
||
// 'tk_express_template_item
|
||
// 'tk_fenxiao
|
||
// 'tk_fenxiao_account
|
||
// 'tk_fenxiao_goods_sku
|
||
// 'tk_fenxiao_level
|
||
// 'tk_fenxiao_order
|
||
// 'tk_fenxiao_withdraw
|
||
// 'tk_form
|
||
// 'tk_form_data
|
||
// 'tk_goods
|
||
// 'tk_goods_attr_class
|
||
// 'tk_goods_browse
|
||
// 'tk_goods_category
|
||
// 'tk_goods_commission
|
||
// 'tk_goods_label
|
||
// 'tk_goods_service
|
||
// 'tk_goods_sku
|
||
// 'tk_group
|
||
// 'tk_link
|
||
// 'tk_local
|
||
// 'tk_manage_account
|
||
// 'tk_manage_level
|
||
// 'tk_manage_order
|
||
// 'tk_manage_user
|
||
// 'tk_member
|
||
// 'tk_member_account
|
||
// 'tk_member_address
|
||
// 'tk_member_bank_account
|
||
// 'tk_member_level
|
||
// 'tk_member_level_records
|
||
// 'tk_member_recommend
|
||
// 'tk_menu
|
||
// 'tk_message
|
||
// 'tk_message_template
|
||
// 'tk_message_variable
|
||
// 'tk_order
|
||
// 'tk_order_goods
|
||
// 'tk_order_log
|
||
// 'tk_order_refund_log
|
||
// 'tk_pay
|
||
// 'tk_pay_notify_log
|
||
// 'tk_pay_refund
|
||
// 'tk_pay_shop
|
||
// 'tk_promotion_bundling
|
||
// 'tk_promotion_coupon
|
||
// 'tk_promotion_coupon_type
|
||
// 'tk_promotion_groupbuy
|
||
// 'tk_promotion_jielong
|
||
// 'tk_promotion_pinfan
|
||
// 'tk_promotion_pinfan_group
|
||
// 'tk_promotion_topic
|
||
// 'tk_service_category
|
||
// 'tk_shop
|
||
// 'tk_site
|
||
// 'tk_site_diy_template
|
||
// 'tk_site_diy_view
|
||
// 'tk_site_group
|
||
// 'tk_sms_template
|
||
// 'tk_stat_shop
|
||
// 'tk_stat_shop_hour
|
||
// 'tk_stat_store
|
||
// 'tk_stat_store_hour
|
||
// 'tk_stock_document
|
||
// 'tk_stock_document_goods
|
||
// 'tk_stock_inventory
|
||
// 'tk_stock_inventory_goods
|
||
// 'tk_store
|
||
// 'tk_store_category
|
||
// 'tk_store_goods
|
||
// 'tk_store_goods_sku
|
||
// 'tk_store_label
|
||
// 'tk_store_member
|
||
// 'tk_supply_goods_warehousing
|
||
// 'tk_supply_order
|
||
// 'tk_supply_sub_order
|
||
// 'tk_uni_fans
|
||
// 'tk_user
|
||
// 'tk_user_group
|
||
// 'tk_user_log
|
||
// 'tk_verifier
|
||
// 'tk_verify
|
||
// 'tk_verify_record
|
||
// 'tk_website
|
||
// 'tk_wechat_replay_rule
|
||
|
||
|
||
|
||
];
|
||
// 获取全部数据库列表
|
||
$tables = $this->getAllTable();
|
||
// 获取存在数据的表
|
||
// $hasTable = $this->getHasSpecifyDataTable($tables,$notCleanUp);
|
||
$hasTable = $this->getHasDataTable($tables,$notCleanUp);
|
||
|
||
debug($hasTable);
|
||
// 执行清除操作
|
||
// foreach($tables as $name){
|
||
// if(!in_array($name,$notCleanUp)){
|
||
// // 执行清除操作
|
||
// $res = Db::query('TRUNCATE TABLE '.$name.';');
|
||
// }
|
||
// }
|
||
|
||
debug("处理完成");
|
||
}
|
||
// 数据库处理 - 获取全部数据库
|
||
private function getAllTable(){
|
||
$database = Db::query('SELECT DATABASE() as database_name')[0]['database_name'];
|
||
$tables = Db::query('SHOW TABLES');
|
||
return array_column($tables,'Tables_in_'.$database);
|
||
}
|
||
// 数据库处理 - 获取存在数据的表(任意数据)
|
||
private function getHasDataTable($tables,$notCleanUp){
|
||
$tableList = [];
|
||
foreach($tables as $tableName){
|
||
if(!in_array($tableName,$notCleanUp)){
|
||
// echo "{$tableName}<br />\n\r";
|
||
// 获取字段信息
|
||
$result = Db::query("select count(*) as count_data from {$tableName}");
|
||
$countData = $result[0]['count_data'] ?? 0;
|
||
if($countData > 0){
|
||
$tableList[] = $tableName;
|
||
}
|
||
}
|
||
}
|
||
|
||
return $tableList;
|
||
}
|
||
// 数据库处理 - 获取存在指定数据的表
|
||
private function getHasSpecifyDataTable($tables,$notCleanUp){
|
||
$tableList = [];
|
||
foreach($tables as $tableName){
|
||
if(!in_array($tableName,$notCleanUp)){
|
||
echo "{$tableName}\n\r";
|
||
// 获取字段信息
|
||
$allFieldDesc = Db::query("show columns from {$tableName}");
|
||
$allField = array_column($allFieldDesc,'Field');
|
||
// if($this->isHasSpecifyData($allField,$name)) $tableList[] = $name;
|
||
|
||
|
||
$res = $this->isHasSpecifyData($allField,$tableName);
|
||
echo $res ? "存在\n\r" : "不存在\n\r";
|
||
if($res){
|
||
// 批量替换
|
||
$query = "UPDATE {$tableName} SET ";
|
||
$handleFieldNum = 0;
|
||
foreach($allFieldDesc as $fieldInfo){
|
||
if(strpos($fieldInfo['Type'], 'int') !== false || $fieldInfo['Key'] == 'PRI') continue;
|
||
|
||
$fieldName = $fieldInfo['Field'];
|
||
$query .= "`{$fieldName}` = REPLACE(`{$fieldName}`,'bt.test.cdlfjy.com','zc.test.cdlfjy.com'),";
|
||
++$handleFieldNum;
|
||
}
|
||
$query = trim($query);
|
||
$query = trim($query,',');
|
||
$query = trim($query);
|
||
echo "SQL({$handleFieldNum}):{$query}\n";
|
||
if($handleFieldNum > 0) Db::query($query);
|
||
}
|
||
|
||
echo "\n\n\n";
|
||
}
|
||
}
|
||
|
||
return $tableList;
|
||
}
|
||
// 数据库处理 - 判断当前表是否具有指定内容
|
||
private function isHasSpecifyData($allField,$tableName){
|
||
$query = "select * from {$tableName} where (";
|
||
foreach($allField as $fieldName){
|
||
$query .= "`{$fieldName}` like '%bt.test.cdlfjy.com%' OR ";
|
||
}
|
||
$query = trim($query);
|
||
$query = trim($query,'OR');
|
||
$query = trim($query) . ")";
|
||
|
||
$result = Db::query($query);
|
||
if($result) return true;
|
||
|
||
return false;
|
||
}
|
||
|
||
}
|