dlh_test_server/app/common/logic/PayNotifyLogic.php

1248 lines
58 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载:https://gitee.com/likeshop_gitee
// | github下载:https://github.com/likeshop-github
// | 访问官网:https://www.likeshop.cn
// | 访问社区:https://home.likeshop.cn
// | 访问手册:http://doc.likeshop.cn
// | 微信公众号:likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\common\logic;
use app\admin\logic\distribution\DistributionLevelLogic;
use app\api\logic\PayLogic;
use app\common\enum\IntegralGoodsEnum;
use app\common\enum\IntegralOrderEnum;
use app\common\enum\NoticeEnum;
use app\common\model\{AccountLog,
distribution\Distribution,
distribution\DistributionOrderGoods,
goods\Goods,
integral\IntegralGoods,
integral\IntegralOrder,
order\OrderGoods,
RechargeOrder,
user\UserDistribution,
shop\Shop};
use app\common\enum\OrderEnum;
use app\common\enum\OrderLogEnum;
use app\common\enum\PayEnum;
use app\common\model\order\Order;
use app\common\model\order\OrderTrade;
use app\common\server\DistributionServer;
use app\common\model\order\OrderLog;
use app\common\model\user\User;
use app\common\server\ConfigServer;
use think\facade\Db;
use think\Exception;
use think\facade\Log;
/**
* 支付成功后处理订单状态
* Class PayNotifyLogic
* @package app\api\logic
*/
class PayNotifyLogic
{
/**
* @notes 回调处理
* @param $action
* @param $order_sn
* @param array $extra
* @return bool|string
* @throws \think\exception\PDOException
* @author suny
* @date 2021/7/13 6:32 下午
*/
public static function handle($action, $order_sn, $extra = [])
{
Db::startTrans();
try {
self::$action($order_sn, $extra);
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
$record = [
__CLASS__, __FUNCTION__, $e->getFile(), $e->getLine(), $e->getMessage()
];
Log::record(implode('-', $record));
return $e->getMessage();
}
}
/**
* @notes 添加订单日志表
* @param $order_id
* @param $user_id
* @param $shop_id
* @return array
* @author suny
* @date 2021/7/13 6:32 下午
*/
public static function getOrderLogData($order_id, $user_id, $shop_id)
{
$order_log_data = [];
$order_log_data['type'] = OrderLogEnum::TYPE_USER;
$order_log_data['channel'] = OrderLogEnum::USER_PAID_ORDER;
$order_log_data['order_id'] = $order_id;
$order_log_data['handle_id'] = $user_id;
$order_log_data['shop_id'] = $shop_id;
$order_log_data['content'] = OrderLogEnum::getLogDesc(OrderLogEnum::USER_PAID_ORDER);
$order_log_data['create_time'] = time();
return $order_log_data;
}
/**
* @notes 父订单回调
* @param $order_sn
* @param array $extra
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author suny
* @date 2021/7/13 6:33 下午
*/
private static function trade($order_sn, $extra = [])
{
//根据父订单号查找父订单
$trade = OrderTrade::where(['t_sn' => $order_sn])->find();
if($trade == null){
// $d['transaction_id'] = $extra['transaction_id'];
// $d['pay_time'] = date("Y-m-d H:i:s");
// $d['pay_status'] = 1;
// $r = Db::name("local_pay")->where("order_sn",$order_sn)->update($d);
$rest = self::rechargeLocalPay($order_sn, $extra);
if($rest){
return Db::name("local_store_order")->where("order_no",$order_sn)->update(['state'=>1]);
}
}
//根据父订单id查找子订单
$orders = Order::with(['order_goods', 'shop'])
->where('trade_id', $trade['id'])
->select()->toArray();
//修改用户消费累计额度
$user = User::find($trade['user_id']);
$user->total_order_amount = ['inc', $trade['order_amount']];
$user->save();
//赠送成长值
$growth_ratio = ConfigServer::get('transaction', 'money_to_growth', 0);
if ($growth_ratio > 0) {
$able_get_growth = floor($trade['total_amount'] / $growth_ratio);
$user->where('id', $trade['user_id'])
->inc('user_growth', $able_get_growth)
->update();
AccountLogLogic::AccountRecord($trade['user_id'], $able_get_growth, 1, AccountLog::order_give_growth, '', $trade['id'], $order_sn);
}
// 生成分销订单
PayLogic::distributionOrderGoods(array_column($orders, 'id'));
// 更新分销会员等级
DistributionLevelLogic::updateDistributionLevel($trade['user_id']);
foreach ($orders as $item) {
//赠送积分
$open_award = ConfigServer::get('order_award', 'open_award', 0);
if ($open_award == 1) {
$award_event = ConfigServer::get('order_award', 'award_event', 0);
$award_ratio = ConfigServer::get('order_award', 'award_ratio', 0);
if ($award_ratio > 0) {
$award_integral = floor($item['order_amount'] * ($award_ratio / 100));
}
}
//更新订单状态
$data = [
'pay_status' => PayEnum::ISPAID,
'pay_time' => time(),
'order_status' => Order::STATUS_WAIT_DELIVERY,
'award_integral_status' => $award_event ?? 0,
'award_integral' => $award_integral ?? 0
];
//如果返回了第三方流水号
if (isset($extra['transaction_id'])) {
$data['transaction_id'] = $extra['transaction_id'];
}
Order::update($data, ['id' => $item['id']]);
// 增加一条订单日志
$order_log_add_data = self::getOrderLogData($item['id'], $item['user_id'], $item['shop_id']);
$order_log_datas_insert[] = $order_log_add_data;
OrderLog::insertAll($order_log_datas_insert);
// if ($item['order_type'] == order::NORMAL_ORDER){
// DistributionServer::commission($item['id']);
// }
//通知用户
event('Notice', [
'scene' => NoticeEnum::ORDER_PAY_NOTICE,
'mobile' => $user['mobile'],
'params' => ['order_id' => $item['id'], 'user_id' => $item['user_id']]
]);
//通知商家
if (!empty($item['shop']['mobile'])) {
event('Notice', [
'scene' => NoticeEnum::USER_PAID_NOTICE_SHOP,
'mobile' => $item['shop']['mobile'],
'params' => ['order_id' => $item['id'], 'user_id' => $item['user_id']]
]);
}
event('Printer', [
'order_id' => $item['id'],
]);
}
//如果返回了第三方流水号
if (isset($extra['transaction_id'])) {
$trade->transaction_id = $extra['transaction_id'];
$trade->save();
}
$order_ids = array_column($orders, 'id');
// 虚拟商品更新订单信息
GoodsVirtualLogic::afterPayVirtualDelivery($order_ids);
// 更新商品销量
self::updateGoodsSales($order_ids);
}
// 本地商家支付回调
public static function rechargeLocalPay($order_sn, $extra){
if (isset($extra['transaction_id'])) {
$d['transaction_id'] = $extra['transaction_id'];
}
$result = Db::name("local_pay")->where("order_sn",$order_sn)->find();
if($result['pay_status'] == 0){
$local_store = Db::name('local_store')->where(['lstore_id' => $result['local_id']])->find();
if($result['local_id'] == 138){
$price = round($result['order_amount']*0.72,2);
}else{
$price = round($result['order_amount']*0.7,2);
}
if ($local_store['empowerPhone']) {
event('Notice', [
'scene' => NoticeEnum::LOCAL_SHOP_COLLECTION_NOTICE,
'mobile' => $local_store['empowerPhone'],
'params' => [
'user_id' => $local_store['user_id'],
'money' => $price,
'time' => date("Y-m-d H:i:s"),
'order' => $order_sn,
]
]);
}
if($result != null){
$d['pay_time'] = date("Y-m-d H:i:s");
$d['pay_status'] = 1;
$order['lstore_id']= $result['local_id'];
$r = Db::name("local_pay")->where("id",$result['id'])->update($d);
Db::name('local_goods_order')->where("lsg_order_id",$result['lsg_order_id'])->update([
'state'=>1
]);
}
$order['id']= $result['id'];
$order['type']= 4;
$order['order_amount']= $result['order_amount'];
$order['order_sn']= $order_sn;
$type = "local_pay";
$account_log = new AccountLog();
$account_log->log_sn = createSn('account_log','log_sn','',4);
$account_log->user_id = $result['user_id'];
$account_log->source_type = AccountLog::local_pay;
$account_log->source_id = $result['id'];
$account_log->source_sn = $result['order_sn'];
$account_log->change_amount = $result['order_amount'];
$account_log->left_amount = 0;
$account_log->remark = "本地商家微信支付";
$account_log->change_type = 0;
$account_log->create_time = time();
$account_log->save();
return self::recharge_billions($order, $result['user_id'],$type,$result['local_id']);
}
}
/**
* @notes 子订单回调
* @param $order_sn
* @param array $extra
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author suny
* @date 2021/7/13 6:33 下午
*/
private static function order($order_sn, $extra = [])
{
$time = time();
$order = Order::with(['order_goods', 'shop'])
->where('order_sn', $order_sn)
->find()->toArray();
//赠送积分
$open_award = ConfigServer::get('order_award', 'open_award', 0);
if ($open_award == 1) {
$award_event = ConfigServer::get('order_award', 'award_event', 0);
$award_ratio = ConfigServer::get('order_award', 'award_ratio', 0);
if ($award_ratio > 0) {
$award_integral = floor($order['order_amount'] * ($award_ratio / 100));
}
}
//更新订单状态
$data = [
'pay_status' => PayEnum::ISPAID,
'pay_time' => time(),
'order_status' => Order::STATUS_WAIT_DELIVERY,
'award_integral_status' => $award_event ?? 0,
'award_integral' => $award_integral ?? 0
];
//如果返回了第三方流水号
if (isset($extra['transaction_id'])) {
$data['transaction_id'] = $extra['transaction_id'];
}
Order::update($data, ['id' => $order['id']]);
// 增加商品销量
// 增加一条订单日志
$order_log_add_data = self::getOrderLogData($order['id'], $order['user_id'], $order['shop_id']);
$order_log_datas_insert[] = $order_log_add_data;
OrderLog::insertAll($order_log_datas_insert);
//修改用户消费累计额度
$user = User::find($order['user_id']);
$user->total_order_amount = ['inc', $order['order_amount']];
$user->save();
//赠送成长值
$growth_ratio = ConfigServer::get('transaction', 'money_to_growth', 0);
if ($growth_ratio > 0) {
$able_get_growth = floor($order['order_amount'] / $growth_ratio);
$user->where('id', $order['user_id'])
->inc('user_growth', $able_get_growth)
->update();
AccountLogLogic::AccountRecord($order['user_id'], $able_get_growth, 1, AccountLog::order_give_growth, '', $order['id'], $order_sn);
}
// 生成分销订单
PayLogic::distributionOrderGoods([$order['id']]);
// 更新分销会员等级
DistributionLevelLogic::updateDistributionLevel($order['user_id']);
// //拼购,砍价的订单不参与分销分佣
// if ($order['order_type'] == order::NORMAL_ORDER){
// DistributionServer::commission($order['id']);
// }
// 虚拟商品更新订单信息
GoodsVirtualLogic::afterPayVirtualDelivery($order['id']);
// 更新商品销量
self::updateGoodsSales($order['id']);
//通知用户
event('Notice', [
'scene' => NoticeEnum::ORDER_PAY_NOTICE,
'mobile' => $user['mobile'],
'params' => ['order_id' => $order['id'], 'user_id' => $order['user_id']]
]);
//通知商家
if (!empty($order['shop']['mobile'])) {
event('Notice', [
'scene' => NoticeEnum::USER_PAID_NOTICE_SHOP,
'mobile' => $order['shop']['mobile'],
'params' => ['order_id' => $order['id'], 'user_id' => $order['user_id']]
]);
}
event('Printer', [
'order_id' => $order['id'],
]);
}
/**
* @notes 充值回调
* @param $order_sn
* @param array $extra
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author suny
* @date 2021/7/13 6:33 下午
*/
private static function recharge($order_sn, $extra = [])
{
Log::write('微信支付异步回调: ' . $order_sn);
$new = time();
$recharge_order = new RechargeOrder();
$order = $recharge_order->where(['order_sn' => $order_sn])->find();
if ($order['pay_status'] != PayEnum::ISPAID) {
if($order['transaction_id'] == null ) {
$update_data['pay_time'] = $new;
$update_data['pay_status'] = PayEnum::ISPAID;
if (isset($extra['transaction_id'])) {
$update_data['transaction_id'] = $extra['transaction_id'];
}
$recharge_order->where(['id' => $order['id']])->update($update_data);
$user = User::find($order['user_id']);
$total_money = $order['order_amount'] + $order['give_money'];
$total_integral = $order['give_integral'];
$user->user_money = ['inc', $total_money];
$user->user_integral = ['inc', $total_integral];
$user->user_growth = ['inc', $order['give_growth']];
$user->total_recharge_amount = ['inc', $total_money];
$user->save();
$remark = "余额充值";
//记录余额
$total_money > 0 && AccountLogLogic::AccountRecord($user->id, $total_money, 1, AccountLog::recharge_money, $remark, $order['id'], $order_sn);
//记录积分
$total_integral > 0 && AccountLogLogic::AccountRecord($user->id, $total_integral, 1, AccountLog::recharge_give_integral);
//记录成长值
$order['give_growth'] > 0 && AccountLogLogic::AccountRecord($user->id, $order['give_growth'], 1, AccountLog::recharge_give_growth);
$type = "balance_rechange";
$order['type'] = 2;
// 余额充值参与百亿补贴
self::recharge_billions($order, $order['user_id'],$type,"");
}
}
}
private static function recharge_billions_old($order, $user_id,$type="")
{
if(isset($order['id'])){
$check_order = Db::name("user_billions")
->where("order_id",$order['id'])
->where("type",$order['type'])
->find();
if($check_order != null){
return true;
}
}
$user_subsidy = 2; // 自我补贴
$all_user_subsidy = 18; //全用户获取的比例
$user_start_num = 1; // 补贴前几位数量
$user_start_subsidy = 30; //补贴前几位比例 全用户比例中扣除
$user_end_subsidy = 70; //补贴剩余部分比例
$user_first_leader = 30; // 推广者排队补贴
$first_leader = 2; //分销推广者补贴
$store_subsidy = 1; // 店铺推广者补贴 如没有补贴给店铺拥有者
$donglonghui = 2; //东龙汇服务费
$other_leader = 3; //分公司收入 如没有则并入东龙汇
$shop_com = 70; // 商家获取收入
$user = Db::name("user")->where(['id' => $user_id])->find();
$first_leader_user = Db::name("user")->where("id", $user['first_leader'])->find();
$is_d = Distribution::where('user_id', $user['first_leader'] )->where('is_distribution', 1 )->find();
$data['order_price'] = $order['order_amount']; // 改为充值金额
$data['user_id'] = $user_id;
$data['goods_id'] = 0; // 充值接口不需要该参数
$data['shop_id'] = 0; // 充值接口不需要该参数
$data['goods_num'] = 1; // 充值接口设置为1
$data['order_id'] = $order['id'];
$data['dtime'] = date("Y-m-d H:i:s");
$data['type'] = $order['type']; // 类型是充值
$data['subsidy_price'] = round($order['order_amount'] * $user_subsidy / 100,8);
$res = Db::name("user_billions")->save($data);
$subsidy = $order['order_amount'] * $all_user_subsidy / 100;
if($is_d != null){
$ufl = round($subsidy*$user_first_leader/100,8);
$subsidy_ufl = round($subsidy - $ufl,8);
$first_billions_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray();
if(count($first_billions_msg) > 0){
foreach($first_billions_msg as $k=>$v){
if($ufl > 0){
if($v['order_price'] <= $ufl+$v['subsidy_price']){
// 查询用户总余额账户
$first_user_all_account = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->find();
if($first_user_all_account != null){
// 判断账户是否可用
if($first_user_all_account['state'] == 0 || $first_user_all_account['state'] == 2){
return -2;
}
// 更新主表
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->inc("total_amount",$v['order_price'])->update();
if(!$first_update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $first_user_all_account['id'];
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($first_save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$first_save_record){
return -5;
}
}else{
$d['user_id'] = $v['user_id'];
$d['total_amount'] = $v['order_price'];
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d);
if(!$first_update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $first_update_subsidy_withdrawal;
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($first_save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$first_save_record){
return -5;
}
}
// 更新排队状态
$first_update_user_billions = Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$v['order_price'],"state"=>2]);
if(!$first_update_user_billions){
return -3;
}
// 剩余金额
$ufl = round($ufl+$v['subsidy_price']-$v['order_price'],8);
}else{
$subsidy_price_sum = round($v['subsidy_price']+$ufl,8);
Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$subsidy_price_sum]);
// Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$ufl)->update();
$ufl = 0;
}
}
}
$subsidy = round($subsidy_ufl + $ufl,8);
}
}
$billions_msg = Db::name("user_billions")->where("state",0)->select()->toArray();
// $first_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray();
$count = count($billions_msg);
$arr = $arr1 = array();
// 数量超出前几位补贴用户
if($count > $user_start_num) {
$start_subsidy = $subsidy * $user_start_subsidy / 100;
$average = round($start_subsidy / $user_start_num, 8);
$last = round($start_subsidy - $average * ($user_start_num - 1), 8);
for ($i = 0; $i < $user_start_num; $i++) {
if ($i + 1 == $user_start_num) {
$arr[$i] = $last;
} else {
$arr[$i] = $average;
}
}
// 剩余人数补贴
$end_average = round(($subsidy - $start_subsidy)/ ($count-$user_start_num), 8);
$end_last = round($subsidy - $start_subsidy - $end_average * ($count-$user_start_num - 1), 8);
for ($y = 0; $y < $count-$user_start_num; $y++) {
if ($y + 1 == $count-$user_start_num) {
$arr1[$y] = $end_last;
} else {
$arr1[$y] = $end_average;
}
}
}else{
$user_start_num = $count;
// 数量不超出前几位补贴用户,按全部金额计算
$average = round($subsidy/$count, 8);
$last = round($subsidy - $average * ($count - 1), 8);
for ($i = 0; $i < $count; $i++) {
if ($i + 1 == $count) {
$arr[$i] = $last;
} else {
$arr[$i] = $average;
}
}
}
$billion_arr = $billion_arr_id = "";
foreach($billions_msg as $k=>$v){
if($k + 1 <= $user_start_num){
// var_dump($v['order_price']);
// var_dump($arr[$k]);
// var_dump($v['subsidy_price']);
if($v['order_price'] <= $arr[$k] + $v['subsidy_price']){
// 查询用户总余额账户
$user_all_account = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->find();
if($user_all_account != null){
// 判断账户是否可用
if($user_all_account['state'] == 0 || $user_all_account['state'] == 2){
return -2;
}
// 更新主表
$update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->inc("total_amount", $v['order_price'])->update();
if(!$update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $user_all_account['id'];
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$save_record){
return -5;
}
}else{
$d['user_id'] = $v['user_id'];
$d['total_amount'] = $v['order_price'];
$update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d);
if(!$update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $update_subsidy_withdrawal;
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['is_recharge'] = 1;
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$save_record){
return -5;
}
}
// 更新排队状态
$update_user_billions = Db::name("user_billions")->where("billions_id", $v['billions_id'])->update(['subsidy_price' => $v['order_price'], "state" => 2]);
if(!$update_user_billions){
return -3;
}
// 剩余数量
$residue_count = $user_start_num - $k - 1;
if($residue_count!=0){
// 剩余金额
$residue_price = round($arr[$k]+$v['subsidy_price']-$v['order_price'],8);
foreach($arr as $k1=>$v1){
if($k1 > $k){
$residue_price+=$arr[$k1];
}
}
$average = round($residue_price/$residue_count,8);
$last = round($residue_price - $average*($residue_count-1),8);
foreach($arr as $k1=>$v1){
if($k1+1 == $user_start_num){
$arr[$k1] = $last;
}else{
if($k1 > $k){
$arr[$k1] = $average;
}
}
}
}
}else{
// $subp = round($v['subsidy_price']+$arr[$k],6);
// $billion_arr.=" when ".$v['billions_id']." then '".$subp."'";
// $billion_arr_id.=$v['billions_id'].",";
Db::name("user_billions")->where("billions_id", $v['billions_id'])->inc("subsidy_price", $arr[$k])->update();
}
}else{
if($v['order_price'] <= $arr1[$k - $user_start_num] + $v['subsidy_price']){
// 查询用户总余额账户
$user_all_account = Db::name("subsidy_withdrawal")->where("user_id", $v['user_id'])->find();
if($user_all_account == null){
$d['user_id'] = $v['user_id'];
$d['total_amount'] = $v['order_price'];
$update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d);
if(!$update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $update_subsidy_withdrawal;
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['is_recharge'] = 1;
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$save_record){
return -5;
}
}else{
// 判断账户是否可用
if($user_all_account['state'] == 0 || $user_all_account['state'] == 2){
return -2;
}
// 更新主表
$update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->inc("total_amount",$v['order_price'])->update();
if(!$update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $user_all_account['id'];
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['is_recharge'] = 1;
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$save_record){
return -5;
}
}
// 更新排队状态
$update_user_billions = Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$v['order_price'],"state"=>2]);
if(!$update_user_billions){
return -3;
}
// 剩余数量
$residue_count = $count-$user_start_num - $k+$user_start_num - 1;
if($residue_count!=0){
// 剩余金额
$residue_price = round($arr1[$k-$user_start_num]+$v['subsidy_price']-$v['order_price'],8);
foreach($arr1 as $k1=>$v1){
if($k1 > $k - $user_start_num){
$residue_price+=$arr1[$k1];
}
}
$average = round($residue_price/$residue_count,8);
$last = round($residue_price - $average*($residue_count-1),8);
foreach($arr1 as $k1=>$v1){
if($k1+1 == $count-$user_start_num){
$arr1[$k1] = $last;
}else{
if($k1 > $k){
$arr[$k1] = $average;
}
}
}
}
}else{
// $subp = round($v['subsidy_price']+$arr1[$k-$user_start_num],6);
// $billion_arr.=" when ".$v['billions_id']." then '".$subp."'";
// $billion_arr_id.=$v['billions_id'].",";
Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$arr1[$k-$user_start_num])->update();
}
}
}
// $billion_arr_id = explode(",",$billion_arr_id);
// array_pop($billion_arr_id);
// // print_r("<pre>");
// // var_dump($billion_arr_id);die;
// $billion_arr_id = implode(",",$billion_arr_id);
// $sql = "UPDATE `ls_user_billions`
// SET subsidy_price = CASE billions_id
// $billion_arr
// END
// WHERE billions_id IN ($billion_arr_id)";
// $query = Db::query($sql);
// 本地生活微信支付 推广商家收益
}
private static function recharge_billions($order, $user_id,$type="",$lstore_id="")
{
if(isset($order['id'])){
$check_order = Db::name("user_billions")
->where("order_id",$order['id'])
->where("type",$order['type'])
->find();
if($check_order != null){
return true;
}
}
$user_subsidy = 2; // 自我补贴
$all_user_subsidy = 20;
$user_start_num = 1; // 补贴前几位数量
$user_start_subsidy = 30; //补贴前几位比例 全用户比例中扣除
$user_end_subsidy = 70; //补贴剩余部分比例
$user_first_leader = 30; // 推广者排队补贴
$first_leader = 2; //分销推广者补贴
$store_subsidy = 1; // 店铺推广者补贴 如没有补贴给店铺拥有者
$donglonghui = 2; //东龙汇服务费
$other_leader = 3; //分公司收入 如没有则并入东龙汇
$shop_com = 70; // 商家获取收入
if($lstore_id == 138){
$all_user_subsidy = 18; //全用户获取的比例
}
if($lstore_id == 140){
$first_leader=0;
$other_leader = 0;
$all_user_subsidy = 20; //全用户获取的比例
}
$user = Db::name("user")->where(['id' => $user_id])->find();
$first_leader_user = Db::name("user")->where("id", $user['first_leader'])->find();
$is_d = Distribution::where('user_id', $user['first_leader'] )->where('is_distribution', 1 )->find();
$data['order_price'] = $order['order_amount']; // 改为充值金额
$data['user_id'] = $user_id;
$data['goods_id'] = 0; // 充值接口不需要该参数
$data['shop_id'] = 0; // 充值接口不需要该参数
$data['goods_num'] = 1; // 充值接口设置为1
$data['order_id'] = $order['id'];
$data['dtime'] = date("Y-m-d H:i:s");
$data['type'] = $order['type']; // 类型是充值
$data['subsidy_price'] = round($order['order_amount'] * $user_subsidy / 100,8);
$res = Db::name("user_billions")->insertGetId($data);
$subsidy = $order['order_amount'] * $all_user_subsidy / 100;
if($is_d != null){
$ufl = round($subsidy*$user_first_leader/100,8);
$subsidy_ufl = round($subsidy - $ufl,8);
$first_billions_msg = Db::name("user_billions")->where("user_id",$user['first_leader'])->where("state",0)->select()->toArray();
if(count($first_billions_msg) > 0){
foreach($first_billions_msg as $k=>$v){
if($ufl > 0){
if($v['order_price'] <= $ufl+$v['subsidy_price']){
// 查询用户总余额账户
$first_user_all_account = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->find();
if($first_user_all_account != null){
// 判断账户是否可用
if($first_user_all_account['state'] == 0 || $first_user_all_account['state'] == 2){
return -2;
}
// 更新主表
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->where("user_id",$v['user_id'])->inc("total_amount",$v['order_price'])->update();
if(!$first_update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $first_user_all_account['id'];
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($first_save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$first_save_record){
return -5;
}
}else{
$d['user_id'] = $v['user_id'];
$d['total_amount'] = $v['order_price'];
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d);
if(!$first_update_subsidy_withdrawal){
return -4;
}
$dat['subsidy_withdrawal_id'] = $first_update_subsidy_withdrawal;
$dat['user_id'] = $v['user_id'];
$dat['billions_id'] = $v['billions_id'];
$dat['type'] = 1;
$dat['money'] = $v['order_price'];
$dat['create_time'] = date("Y-m-d H:i:s");
// 插入提现记录
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
if ($first_save_record) {
$send = array(
'user_id' => $v['user_id'],
'money' => $v['order_price']
);
sendBillionsSubsidySMS($send);
}
if(!$first_save_record){
return -5;
}
}
// 更新排队状态
$first_update_user_billions = Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$v['order_price'],"state"=>2]);
if(!$first_update_user_billions){
return -3;
}
// 剩余金额
$ufl = round($ufl+$v['subsidy_price']-$v['order_price'],8);
}else{
$subsidy_price_sum = round($v['subsidy_price']+$ufl,8);
Db::name("user_billions")->where("billions_id",$v['billions_id'])->update(['subsidy_price'=>$subsidy_price_sum]);
// Db::name("user_billions")->where("billions_id",$v['billions_id'])->inc("subsidy_price",$ufl)->update();
$ufl = 0;
}
}
}
$subsidy = round($subsidy_ufl + $ufl,8);
}
self::atcl($order,$store_subsidy,$type,$user,$first_leader,$other_leader);
}
$billions_count = Db::name("user_billions")->where("state",0)->count();
$data_surplus['billions_id'] = $res;
$data_surplus['subsidy_all_price'] = $subsidy;
$data_surplus['user_start_num'] = $user_start_num;
$data_surplus['user_start_subsidy'] = $user_start_subsidy;
$data_surplus['user_start_price'] = round($subsidy*$user_start_subsidy/100,8);
$data_surplus['user_start_avg_price'] = round($data_surplus['user_start_price']/$user_start_num,8);
$data_surplus['residue_price'] = $subsidy-$data_surplus['user_start_price'];
if($billions_count>$user_start_num){
$data_surplus['user_price'] = round(($subsidy-$data_surplus['user_start_price'])/($billions_count-$user_start_num),8);
}
$data_surplus['dtime'] = date("Y-m-d H:i:s");
$rest = Db::name("user_billions_surplus")->save($data_surplus);
return $rest;
}
public static function atcl($order,$store_subsidy,$type,$user,$first_leader,$other_leader){
if(isset($order['lstore_id'])){
$local_msg = Db::name("local_store")
->where("lstore_id",$order['lstore_id'])
->where("is_delete",0)
->where("store_state",1)
->find();
if($local_msg != null){
$local_first_leader = Db::name("user")
->where("id",$local_msg['user_id'])
->where("del",0)
->where("disable",0)
->find();
if($local_first_leader != null){
if($local_first_leader['first_leader']!=0){
$is_local_d = Distribution::where('user_id', $local_first_leader['first_leader'] )->where('is_distribution', 1 )->find();
if($is_local_d!=null){
$local_earnings = round($order['order_amount']*$store_subsidy/100,2);
User::where('id', $local_first_leader['first_leader'])
->inc('earnings', $local_earnings)->update();
$local_earnings_buy = AccountLog::where('user_id', $local_first_leader['first_leader'])->order("create_time desc")->limit(1)->value('left_amount');
$sn = createSn('account_log', 'log_sn', '', 4);
AccountLog::create([
'log_sn' => $sn,
'user_id' => $local_first_leader['first_leader'],
'source_type' => AccountLog::local_inc_earnings, //todo 改为充值类型
'source_id' => 0, //todo 改为充值订单主键
'source_sn' => 0, // todo 改为充值订单编号
'change_amount' => $local_earnings,
'left_amount' => $local_earnings_buy + $local_earnings,
'change_type' => 1,
'remark' => '推广本地商家收款佣金增加'
]);
}
}
}
}
}
$remark = $remark1 =$remark2= "";
$source_type = $source_type1 =$source_type2= 0;
if($type == "balance_rechange"){
$remark = "用户充值佣金";
$source_type = AccountLog::recharge_inc_earnings;
$remark1 = "无极充值佣金";
$source_type1 = AccountLog::electrodeless_rechange_inc_earnings;
$remark2 = "无极公司充值佣金";
$source_type2 = AccountLog::company_electrodeless_rechange_inc_earnings;
}
if($type == "local_pay"){
$remark = "本地生活用户佣金";
$source_type = AccountLog::local_user_inc_earnings;
$remark1 = "无极本地生活用户佣金";
$source_type1 = AccountLog::electrodeless_local_inc_earnings;
$remark2 = "无极公司本地生活用户佣金";
$source_type2 = AccountLog::company_electrodeless_local_inc_earnings;
}
if($type == "billions_rechange"){
$remark = "返补充值佣金";
$source_type = AccountLog::billions_rechange_inc_earnings;
$remark1 = "无极返补充值佣金";
$source_type1 = AccountLog::electrodeless_billions_rechange_inc_earnings;
$remark2 = "无极公司返补充值佣金";
$source_type2 = AccountLog::company_electrodeless_billions_rechange_inc_earnings;
}
if($type == "pay_goods"){
$remark = "消费佣金";
$source_type = AccountLog::pay_money;
$remark1 = "无极消费佣金";
$source_type1 = AccountLog::electrodeless_pay_money;
$remark2 = "无极公司消费佣金";
$source_type2 = AccountLog::company_electrodeless_pay_money;
}
if($user['first_leader']>0){
$first_leader_percent = round($order['order_amount'] * $first_leader / 100, 8);
// Order::where('id', $order['id'])
// ->inc('distribution_money',$first_leader_percent)->update();
UserDistribution::where('user_id', $user['first_leader'])
->inc('order_amount', $first_leader_percent)->update();
UserDistribution::where('user_id', $user['first_leader'])
->inc('order_num')->update();
User::where('id', $user['first_leader'])
->inc('earnings', $first_leader_percent)->update();
$earnings_buy = AccountLog::where('user_id', $user['first_leader'])->order("create_time desc")->limit(1)->value('left_amount');
$sn = createSn('account_log', 'log_sn', '', 4);
AccountLog::create([
'log_sn' => $sn,
'user_id' => $user['first_leader'],
'source_type' => $source_type, //todo 改为充值类型
'source_id' => 0, //todo 改为充值订单主键
'source_sn' => 0, // todo 改为充值订单编号
'change_amount' => $first_leader_percent,
'left_amount' => $earnings_buy + $first_leader_percent,
'change_type' => 1,
'remark' => $remark
]);
DistributionOrderGoods::create([
'sn' => $sn,
'user_id' => $user['first_leader'],
'ratio' => $first_leader,
'order_id' => 0, //todo 改为充值订单主键
'order_goods_id' => 0, //todo 因为是充值所以改为0
'goods_num' => 1, // todo 因为是充值所以改为1
'money' => $first_leader_percent,
'status' => 2,
'create_time' => time(),
'shop_id' => 0, // todo 因为是充值所以改为默认0
'is_recharge' => 1, // todo 因为是充值所以改为1
'settlement_time' => time()
]);
}
// 1%给运营人员
$divide_second_user_msg = Db::name("user")->where("id",$user['divide_second_user_id'])->find();
$regional_state = 0;
if($divide_second_user_msg != null){
$regional_state = $user['regional_state'];
}
$performance_person = 0;
$performance = 0;
if($regional_state == 1){
$performance = $user['regional_percent'];
$performance_person = round($order['order_amount'] * $performance / 100, 8);
// Order::where('id', $order['id'])
// ->inc('distribution_money',$performance_person)->update();
// $shopDistribution_msg = UserShopDistribution::where('shop_id', $value['shop_id'])
// ->find();
User::where('id', $user['divide_second_user_id'])
->inc('earnings', $performance_person)->update();
$earnings_buy = AccountLog::where('user_id', $user['divide_second_user_id'])->order("create_time desc")->limit(1)->value('left_amount');
$sn = createSn('account_log', 'log_sn', '', 4);
AccountLog::create([
'log_sn' => $sn,
'user_id' => $user['divide_second_user_id'],
'source_type' => $source_type1,
'source_id' => 0,
'source_sn' => 0,
'change_amount' => $performance_person,
'left_amount' => $earnings_buy + $performance_person,
'change_type' => 1,
'remark' => $remark1
]);
DistributionOrderGoods::create([
'sn' => $sn,
'user_id' => $user['divide_second_user_id'],
'ratio' => $performance,
'order_id' => 0,
'order_goods_id' => 0,
'goods_num' => 1,
'money' => $performance_person,
'status' => 2,
'create_time' => time(),
'shop_id' => 0,
'settlement_time' => time()
]);
}
// 3%对公司
$role_user_msg = Db::name("user")->where("id",$user['divide_role_user_id'])->find();
$state = 0;
if($role_user_msg != null){
$state = $user['divide_state'];
}
if($user['divide_role_user_id'] > 0&&$state>0 ){
$divide_percent = round($order['order_amount'] * ($other_leader-$performance) / 100, 8);
// Order::where('id', $order['id'])
// ->inc('distribution_money',$divide_percent)->update();
// $shopDistribution_msg = UserShopDistribution::where('shop_id', $value['shop_id'])
// ->find();
User::where('id', $user['divide_role_user_id'])
->inc('earnings', $divide_percent)->update();
$earnings_buy = AccountLog::where('user_id', $user['divide_role_user_id'])->order("create_time desc")->limit(1)->value('left_amount');
$sn = createSn('account_log', 'log_sn', '', 4);
AccountLog::create([
'log_sn' => $sn,
'user_id' => $user['divide_role_user_id'],
'source_type' => $source_type2, //todo 改为充值类型
'source_id' => 0, //todo 改为充值订单主键
'source_sn' => $order['order_sn'], // todo 改为充值订单编号
'change_amount' => $divide_percent,
'left_amount' => $earnings_buy + $divide_percent,
'change_type' => 1,
'remark' => $remark2
]);
DistributionOrderGoods::create([
'sn' => $sn,
'user_id' => $user['divide_role_user_id'],
'ratio' => $other_leader-$performance,
'order_id' => $order['id'], //todo 改为充值订单主键
'order_goods_id' => 0, //todo 因为是充值所以改为0
'goods_num' => 1, // todo 因为是充值所以改为1
'money' => $divide_percent,
'status' => 2,
'create_time' => time(),
'shop_id' => 0, // todo 因为是充值所以改为默认0
'is_recharge' => 1, // todo 因为是充值所以改为1
'settlement_time' => time()
]);
}
return true;
}
/**
* @notes 积分订单回调
* @param $order_sn
* @param array $extra
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/3/1 14:36
*/
private static function integral($order_sn, $extra = [])
{
$order = IntegralOrder::where(['order_sn' => $order_sn])->findOrEmpty();
$goods = $order['goods_snap'];
// 更新订单状态
$data = [
'order_status' => IntegralOrderEnum::ORDER_STATUS_DELIVERY,
'pay_status' => PayEnum::ISPAID,
'pay_time' => time(),
];
// 红包类型 或者 无需物流 支付完即订单完成
if ($goods['type'] == IntegralGoodsEnum::TYPE_BALANCE || $goods['delivery_way'] == IntegralGoodsEnum::DELIVERY_NO_EXPRESS) {
$data['order_status'] = IntegralOrderEnum::ORDER_STATUS_COMPLETE;
$data['confirm_time'] = time();
}
// 第三方流水号
if (isset($extra['transaction_id'])) {
$data['transaction_id'] = $extra['transaction_id'];
}
IntegralOrder::update($data, ['id' => $order['id']]);
// 更新商品销量
IntegralGoods::where([['id', '=', $goods['id']], ['stock', '>=', $order['total_num']]])
->dec('stock', $order['total_num'])
->inc('sales', $order['total_num'])
->update();
// 红包类型,直接增加余额
if ($goods['type'] == IntegralGoodsEnum::TYPE_BALANCE) {
$reward = round($goods['balance'] * $order['total_num'], 2);
User::where(['id' => $order['user_id']])
->inc('user_money', $reward)
->update();
AccountLogLogic::AccountRecord(
$order['user_id'],
$reward, 1,
AccountLog::integral_order_inc_balance,
'', $order['id'], $order['order_sn']
);
}
}
/**
* @notes 更新商品销量
* @param $order_id
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/10/14 10:16
*/
private static function updateGoodsSales($order_ids)
{
if (!is_array($order_ids)) {
$order_ids = [$order_ids];
}
$order_goods = OrderGoods::whereIn('order_id', $order_ids)
->select()
->toArray();
if (empty($order_goods)) {
return false;
}
foreach ($order_goods as $item) {
// 增加商品销量
Goods::where('id', $item['goods_id'])
->inc('sales_actual', $item['goods_num'])
->update();
}
return true;
}
}