添加活动首单商户佣金
This commit is contained in:
parent
1016037ac3
commit
f675551008
@ -9,7 +9,9 @@ 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
|
||||
@ -65,7 +67,7 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
}
|
||||
Db::startTrans();
|
||||
try {
|
||||
$isFirstOrder = $this->isFirstOrder($userId, $consumption['start_time']);
|
||||
$isFirstOrder = $this->isFirstOrder($userId, $consumption['start_time'], $consumption['end_time']);
|
||||
$storeActivityOrderDao = new StoreActivityOrderDao();
|
||||
$storeActivityOrderDao->save($groupOrder, $spreadUserId, $isFirstOrder);
|
||||
if ($consumption['type'] == StoreConsumption::TYPE_OWNER_CONSUMPTION) {
|
||||
@ -108,7 +110,7 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
}
|
||||
Db::startTrans();
|
||||
try {
|
||||
$isFirstOrder = $this->isFirstOrder($userId, $spreadConsumption['start_time']);
|
||||
$isFirstOrder = $this->isFirstOrder($userId, $spreadConsumption['start_time'], $spreadConsumption['end_time']);
|
||||
$storeActivityOrderDao = new StoreActivityOrderDao();
|
||||
$storeActivityOrderDao->save($groupOrder, $spreadUserId, $isFirstOrder);
|
||||
if ($spreadConsumption['type'] == StoreConsumption::TYPE_PULL_CONSUMPTION && $isFirstOrder) {
|
||||
@ -178,7 +180,7 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
|
||||
/**
|
||||
* 发放消费金
|
||||
* @param array $consumption 消费金类型
|
||||
* @param $consumption 消费金类型
|
||||
* @param float $rate 红包发放比例
|
||||
* @param int $userId 用户id
|
||||
* @param string $groupOrderIds 订单id集合
|
||||
@ -187,7 +189,7 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send(array $consumption, float $rate, int $userId, string $groupOrderIds, float $amount, $status = -2)
|
||||
public function send($consumption, float $rate, int $userId, string $groupOrderIds, float $amount, $status = -2, $type = 1)
|
||||
{
|
||||
$model = new StoreConsumptionUser();
|
||||
$model->coupon_id = $consumption['coupon_id'];
|
||||
@ -196,10 +198,11 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
$model->order_id_set = $groupOrderIds;
|
||||
$model->coupon_price = bcmul($amount, $rate, 2);
|
||||
$model->balance = $model->coupon_price;
|
||||
$model->order_amount = $amount;
|
||||
$model->create_time = date('Y-m-d H:i:s');
|
||||
$model->start_time = date('Y-m-d H:i:s', time() + 7 * 86400);
|
||||
$model->end_time = date('Y-m-d H:i:s', time() + 7 * 86400 + 365 * 86400);
|
||||
$model->type = 'send';
|
||||
$model->type = $type;
|
||||
$model->status = $status;
|
||||
if (!$model->save()) {
|
||||
throw new \Exception('发放失败');
|
||||
@ -210,17 +213,67 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
* 判断是否是首单
|
||||
* @param int $userId 用户id
|
||||
* @param string $startTime 活动开始时间
|
||||
* @param string $endTime 活动结束时间
|
||||
* @return int
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function isFirstOrder(int $userId, string $startTime)
|
||||
public function isFirstOrder(int $userId, string $startTime, string $endTime)
|
||||
{
|
||||
$isNewUser = User::where('uid', $userId)->where('create_time', '>=', $startTime)->count();
|
||||
$isNewUser = User::where('uid', $userId)->whereBetweenTime('create_time', $startTime, $endTime)->count();
|
||||
if ($isNewUser == 0) {
|
||||
return 0;
|
||||
}
|
||||
$count = StoreGroupOrder::where('uid', $userId)->where('paid', 1)->where('pay_time', '>=', $startTime)->count();
|
||||
$count = StoreGroupOrder::where('uid', $userId)->where('paid', 1)->whereBetweenTime('pay_time', $startTime, $endTime)->count();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -14,6 +14,7 @@ class StoreConsumption extends BaseModel
|
||||
|
||||
const TYPE_OWNER_CONSUMPTION = 13; //个人消费金
|
||||
const TYPE_PULL_CONSUMPTION = 14; //拉新消费金
|
||||
const TYPE_FIRST_ORDER_COMMISSION = 15; //首单佣金
|
||||
|
||||
public static function tablePk(): string
|
||||
{
|
||||
|
@ -12,6 +12,8 @@ class StoreConsumptionUser extends BaseModel
|
||||
const STATUS_UNUSED = 0; //未使用
|
||||
const STATUS_USED = 1; //已使用
|
||||
const STATUS_OVERDUE = 2; //过期的
|
||||
const TYPE_ONE = 1; //实物通用红包
|
||||
const TYPE_TWO = 2; //现金抵扣红包
|
||||
|
||||
public static function tablePk(): string
|
||||
{
|
||||
|
@ -11,6 +11,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\repositories\store\order;
|
||||
|
||||
use app\common\dao\store\consumption\StoreConsumptionUserDao;
|
||||
use app\common\dao\store\order\StoreCartDao;
|
||||
use app\common\dao\store\order\StoreOrderDao;
|
||||
use app\common\model\store\order\StoreGroupOrder;
|
||||
@ -421,6 +422,8 @@ class StoreOrderRepository extends BaseRepository
|
||||
], $order->mer_id);
|
||||
//自动打印订单
|
||||
$this->autoPrinter($order->order_id, $order->mer_id);
|
||||
// "惠农供销,谱写数字新篇章"活动首单分润
|
||||
$finance = (new StoreConsumptionUserDao())->firstOrderCommission($order, $finance, $financeSn . ($i++));
|
||||
}
|
||||
//分销判断
|
||||
// if ($groupOrder->user->spread_uid) {
|
||||
|
44
extend/AES.php
Normal file
44
extend/AES.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
class AES
|
||||
{
|
||||
|
||||
public $cipher = 'aes-128-cbc';
|
||||
|
||||
/**
|
||||
* 使用对称密钥进行加密
|
||||
* @param $plainText
|
||||
* @param $secret
|
||||
* @param $iv
|
||||
* @return string
|
||||
*/
|
||||
function encrypt($plainText, $secret, $iv = null)
|
||||
{
|
||||
$plainText = json_encode($plainText);
|
||||
if (!empty($iv)) {
|
||||
$encryptedData = openssl_encrypt($plainText, $this->cipher, $secret, OPENSSL_RAW_DATA, $iv);
|
||||
} else {
|
||||
$encryptedData = openssl_encrypt($plainText, $this->cipher, $secret);
|
||||
}
|
||||
return base64_encode($encryptedData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用对称秘钥解密
|
||||
* @param $plainText
|
||||
* @param $secret
|
||||
* @param $iv
|
||||
* @return false|string
|
||||
*/
|
||||
function decrypt($plainText, $secret, $iv = null) {
|
||||
$plainText = base64_decode($plainText);
|
||||
if (!empty($iv)) {
|
||||
$data = openssl_decrypt($plainText, $this->cipher, $secret, OPENSSL_RAW_DATA, $iv);
|
||||
$data = json_decode($data, true);
|
||||
return $data;
|
||||
}
|
||||
return openssl_decrypt($plainText, $this->cipher, $secret);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user