调整订单佣金分润
This commit is contained in:
parent
2aba6a6d81
commit
0724fc393a
164
app/common/dao/store/consumption/CommissionDao.php
Normal file
164
app/common/dao/store/consumption/CommissionDao.php
Normal file
@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\dao\store\consumption;
|
||||
|
||||
use app\common\model\store\consumption\StoreConsumption;
|
||||
use app\common\model\store\consumption\StoreConsumptionUser;
|
||||
use app\common\model\store\order\StoreOrder;
|
||||
use app\common\model\system\merchant\Merchant;
|
||||
use app\common\model\user\User;
|
||||
use app\common\repositories\system\merchant\FinancialRecordRepository;
|
||||
use app\common\repositories\system\merchant\MerchantRepository;
|
||||
use crmeb\utils\Curl;
|
||||
use think\facade\Log;
|
||||
|
||||
class CommissionDao
|
||||
{
|
||||
|
||||
/**
|
||||
* 活动首单商户佣金 (支付成功后调用)
|
||||
* @param $order
|
||||
* @param $finance
|
||||
* @param $financeSn
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function firstOrderCommission($order, $finance, $financeSn)
|
||||
{
|
||||
$consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find();
|
||||
if (empty($consumption)) {
|
||||
return $finance;
|
||||
}
|
||||
$storeConsumptionDao = new StoreConsumptionUserDao();
|
||||
$isFirstOrder = $storeConsumptionDao->isFirstOrder($order['uid'], $consumption['start_time'], $consumption['end_time']);
|
||||
if (!$isFirstOrder) {
|
||||
return $finance;
|
||||
}
|
||||
$commission = bcmul($order['pay_price'], $consumption['config']['commission_rate'], 2);
|
||||
if ($commission > 0) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $order->user->nickname,
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => 'first_order_commission',
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn
|
||||
];
|
||||
app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission);
|
||||
}
|
||||
$redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2);
|
||||
if ($redPack > 0) {
|
||||
$userId = Merchant::where('mer_id', $order['mer_id'])->value('uid');
|
||||
$storeConsumptionDao->send($consumption, $consumption['config']['red_pack_rate'], $userId, $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO);
|
||||
$user = User::where('uid', $userId)->find();
|
||||
$user->red_pack_balance = bcadd($user->red_pack_balance, $redPack, 2);
|
||||
if (!$user->save()) {
|
||||
throw new \Exception('红包余额更新出错');
|
||||
}
|
||||
}
|
||||
$promotionCode = User::where('uid', $order['uid'])->value('promotion_code');
|
||||
if (!empty($promotionCode)) {
|
||||
$this->sendCommission($order, $promotionCode);
|
||||
}
|
||||
return $finance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动首单推广人佣金(供应商平台异步回调)
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function firstOrderBatchCommission($data)
|
||||
{
|
||||
$consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find();
|
||||
if (empty($consumption)) {
|
||||
return [];
|
||||
}
|
||||
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
|
||||
$financeSn = $financialRecordRepository->getSn();
|
||||
$users = $data['user'];
|
||||
$order = StoreOrder::where('order_id', $data['order_id'])->find();
|
||||
if (empty($order)) {
|
||||
return [];
|
||||
}
|
||||
$finance = [];
|
||||
$result = [];
|
||||
foreach ($users as $k => $user) {
|
||||
$user['user_profit'] = $user['commission'];
|
||||
$user['account'] = $user['phone'];
|
||||
$commission = bcdiv($user['user_profit'], 100, 2);
|
||||
if ($commission > 0) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $order->user->nickname,
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => $user['type'] == 3 ? 'order_commission' : 'first_order_commission',
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $order['mer_id'],
|
||||
'financial_record_sn' => $financeSn . ($k + 1)
|
||||
];
|
||||
$result[] = $user;
|
||||
}
|
||||
//用户是镇合伙人,不发放红包
|
||||
if ($user['type'] == 3) {
|
||||
continue;
|
||||
}
|
||||
$redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2);
|
||||
$user = User::where('phone', $user['account'])->find();
|
||||
if ($redPack > 0 && !empty($user)) {
|
||||
try {
|
||||
(new StoreConsumptionUserDao())->send($consumption, $consumption['config']['red_pack_rate'], $user['uid'], $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO);
|
||||
$user->red_pack_balance = bcadd($user->red_pack_balance, $redPack, 2);
|
||||
if (!$user->save()) {
|
||||
throw new \Exception('红包余额更新出错');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count($finance) > 0) {
|
||||
$financialRecordRepository->insertAll($finance);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给镇合伙人或推广人发放佣金(支付完成后调用,推广人仅首单奖励)
|
||||
* 请求发送给供应商平台后异步回调
|
||||
* @param $order
|
||||
* @param $promotionCode
|
||||
* @param int $type 类型:1=>推广人,2=>镇合伙人
|
||||
* @return void
|
||||
*/
|
||||
public function sendCommission($order, $promotionCode, $type = 1)
|
||||
{
|
||||
$curl = new Curl();
|
||||
$timestamp = time();
|
||||
$json = ['timestamp' => $timestamp, 'data' => ['order_id' => $order['order_id'], 'order_sn' => $order['order_no'], 'order_money' => bcmul($order['pay_price'], 100), 'promotion_code' => $promotionCode]];
|
||||
if ($type == 2) {
|
||||
$json['street_code'] = $promotionCode;
|
||||
} else {
|
||||
$json['promotion_code'] = $promotionCode;
|
||||
}
|
||||
$aes = new \AES();
|
||||
$iv = $aes->buildIv($timestamp);
|
||||
$encrypt = $aes->encrypt($json, $iv);
|
||||
$api = $type == 1 ? 'user_first_order_share_profit' : 'user_order_share_profit';
|
||||
$url = env('task.worker_host_url') . '/api/shop_call/' . $api;
|
||||
$curl->post($url, ['timestamp' => $timestamp, 'data' => $encrypt]);
|
||||
}
|
||||
|
||||
}
|
@ -9,9 +9,7 @@ use app\common\model\store\consumption\StoreConsumption;
|
||||
use app\common\model\store\consumption\StoreConsumptionUser;
|
||||
use app\common\model\store\order\StoreGroupOrder;
|
||||
use app\common\model\store\StoreActivityOrder;
|
||||
use app\common\model\system\merchant\Merchant;
|
||||
use app\common\model\user\User;
|
||||
use app\common\repositories\system\merchant\MerchantRepository;
|
||||
use think\facade\Db;
|
||||
|
||||
class StoreConsumptionUserDao extends BaseDao
|
||||
@ -227,53 +225,4 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
return intval($count == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动首单商户佣金
|
||||
* @param $order
|
||||
* @param $finance
|
||||
* @param $financeSn
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function firstOrderCommission($order, $finance, $financeSn)
|
||||
{
|
||||
$consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find();
|
||||
if (empty($consumption)) {
|
||||
return $finance;
|
||||
}
|
||||
$isFirstOrder = $this->isFirstOrder($order['uid'], $consumption['start_time'], $consumption['end_time']);
|
||||
if (!$isFirstOrder) {
|
||||
return $finance;
|
||||
}
|
||||
$commission = bcmul($order['pay_price'], $consumption['config']['commission_rate'], 2);
|
||||
if ($commission > 0) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $order->user->nickname,
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => 'first_order_commission',
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn
|
||||
];
|
||||
app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission);
|
||||
}
|
||||
$redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2);
|
||||
if ($redPack > 0) {
|
||||
$userId = Merchant::where('mer_id', $order['mer_id'])->value('uid');
|
||||
$this->send($consumption, $consumption['config']['red_pack_rate'], $userId, $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO);
|
||||
$user = User::where('uid', $userId)->find();
|
||||
$user->red_pack_balance = bcadd($user->red_pack_balance, $redPack, 2);
|
||||
if (!$user->save()) {
|
||||
throw new \Exception('红包余额更新出错');
|
||||
}
|
||||
}
|
||||
return $finance;
|
||||
}
|
||||
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\repositories\store\order;
|
||||
|
||||
use app\common\dao\store\consumption\CommissionDao;
|
||||
use app\common\dao\store\consumption\StoreConsumptionUserDao;
|
||||
use app\common\dao\store\order\StoreCartDao;
|
||||
use app\common\dao\store\order\StoreOrderDao;
|
||||
@ -422,8 +423,11 @@ class StoreOrderRepository extends BaseRepository
|
||||
], $order->mer_id);
|
||||
//自动打印订单
|
||||
$this->autoPrinter($order->order_id, $order->mer_id);
|
||||
// "惠农供销,谱写数字新篇章"活动首单分润
|
||||
$finance = (new StoreConsumptionUserDao())->firstOrderCommission($order, $finance, $financeSn . ($i++));
|
||||
// "惠农供销,谱写数字新篇章"活动首单分润,商户和村、小组合伙人
|
||||
$finance = (new CommissionDao())->firstOrderCommission($order, $finance, $financeSn . ($i++));
|
||||
$addressCode = explode(',', $order['user_address_code']);
|
||||
// "惠农供销,谱写数字新篇章"活动首单分润,镇合伙人
|
||||
(new CommissionDao())->sendCommission($order, $addressCode[3], 2);
|
||||
}
|
||||
//分销判断
|
||||
// if ($groupOrder->user->spread_uid) {
|
||||
|
34
app/controller/api/Open.php
Normal file
34
app/controller/api/Open.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace app\controller\api;
|
||||
|
||||
use app\common\dao\store\consumption\CommissionDao;
|
||||
use crmeb\basic\BaseController;
|
||||
|
||||
class Open extends BaseController
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取活动佣金(供应商平台异步回调)
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function activityCommission()
|
||||
{
|
||||
$timestamp = $this->request->post('timestamp');
|
||||
$data = $this->request->post('data');
|
||||
$aes = new \AES();
|
||||
$iv = !empty($timestamp) ? $aes->buildIv($timestamp) : '';
|
||||
$decrypted = $aes->decrypt($data, $iv);
|
||||
if (!empty($decrypted)) {
|
||||
$storeConsumptionUserDao = new CommissionDao();
|
||||
// "惠农供销,谱写数字新篇章"活动首单分润
|
||||
$result = $storeConsumptionUserDao->firstOrderBatchCommission($decrypted);
|
||||
return app('json')->success($result);
|
||||
}
|
||||
return app('json')->fail('解密失败');
|
||||
}
|
||||
|
||||
}
|
@ -4,21 +4,21 @@ class AES
|
||||
{
|
||||
|
||||
public $cipher = 'aes-128-cbc';
|
||||
public $secret = 've2HSq011whZYgKE';
|
||||
|
||||
/**
|
||||
* 使用对称密钥进行加密
|
||||
* @param $plainText
|
||||
* @param $secret
|
||||
* @param $iv
|
||||
* @return string
|
||||
*/
|
||||
function encrypt($plainText, $secret, $iv = null)
|
||||
function encrypt($plainText, $iv = null)
|
||||
{
|
||||
$plainText = json_encode($plainText);
|
||||
if (!empty($iv)) {
|
||||
$encryptedData = openssl_encrypt($plainText, $this->cipher, $secret, OPENSSL_RAW_DATA, $iv);
|
||||
$encryptedData = openssl_encrypt($plainText, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv);
|
||||
} else {
|
||||
$encryptedData = openssl_encrypt($plainText, $this->cipher, $secret);
|
||||
$encryptedData = openssl_encrypt($plainText, $this->cipher, $this->secret);
|
||||
}
|
||||
return base64_encode($encryptedData);
|
||||
}
|
||||
@ -26,19 +26,27 @@ class AES
|
||||
/**
|
||||
* 使用对称秘钥解密
|
||||
* @param $plainText
|
||||
* @param $secret
|
||||
* @param $iv
|
||||
* @return false|string
|
||||
*/
|
||||
function decrypt($plainText, $secret, $iv = null) {
|
||||
function decrypt($plainText, $iv = null) {
|
||||
$plainText = base64_decode($plainText);
|
||||
if (!empty($iv)) {
|
||||
$data = openssl_decrypt($plainText, $this->cipher, $secret, OPENSSL_RAW_DATA, $iv);
|
||||
$data = openssl_decrypt($plainText, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv);
|
||||
$data = json_decode($data, true);
|
||||
return $data;
|
||||
}
|
||||
return openssl_decrypt($plainText, $this->cipher, $secret);
|
||||
return openssl_decrypt($plainText, $this->cipher, $this->secret);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成iv
|
||||
* @param int $timestamp 时间戳
|
||||
* @return false|string
|
||||
*/
|
||||
public function buildIv(int $timestamp)
|
||||
{
|
||||
return substr(md5($this->secret . $timestamp), 5, 16);
|
||||
}
|
||||
|
||||
}
|
@ -644,6 +644,7 @@ Route::group('api/', function () {
|
||||
Route::resource('store/product/cloudWarehouse', 'api.store.product.CloudWarehouse');
|
||||
Route::get('store/product/town_cloud', 'api.store.product.CloudWarehouse/town');
|
||||
Route::get('storeActivity/consumption', 'api.store.StoreActivity/consumption'); //消费金列表
|
||||
Route::post('open/activityCommission', 'api.open/activityCommission'); //活动佣金回调
|
||||
})->middleware(UserTokenMiddleware::class, false);
|
||||
|
||||
//微信支付回调
|
||||
|
Loading…
x
Reference in New Issue
Block a user