shop-php/app/common/dao/store/consumption/CommissionDao.php
2024-01-23 16:55:26 +08:00

163 lines
6.9 KiB
PHP
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
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);
}
$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) {
$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=>村合伙人3=>镇合伙人4=>配送员
* @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_sn'], 'order_money' => bcmul($order['pay_price'], 100), 'promotion_code' => $promotionCode]];
if ($type == 3) {
$json['street_code'] = $promotionCode;
} elseif ($type == 4) {
$json['courier_phone'] = $promotionCode;
} else {
$json['promotion_code'] = $promotionCode;
}
$aes = new \AES();
$iv = $aes->buildIv($timestamp);
$encrypt = $aes->encrypt($json, $iv);
$api = in_array($type, [1, 2]) ? 'user_first_order_share_profit' : 'user_order_share_profit';
$url = env('task.worker_host_url') . '/api/shop_call/' . $api;
$result = $curl->post($url, ['timestamp' => $timestamp, 'data' => $encrypt]);
$result = json_decode($result, true);
if ($result['code'] != 1) {
Log::error('发送佣金失败:', $result);
}
}
}