new-admin-api/app/common/repositories/system/merchant/MerchantShareholderReposito...

325 lines
15 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\repositories\system\merchant;
use app\common\dao\system\merchant\MerchantShareholderDao;
use app\common\model\marketing\activity\Activity;
use app\common\model\system\merchant\MerchantShareholder;
use app\common\model\system\merchant\MerchantShareholderIntegral;
use app\common\model\user\ExchangeQuotaRecord;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\coupon\StoreCouponRepository;
use app\common\repositories\store\coupon\StoreCouponUserRepository;
use app\common\repositories\store\order\StoreGroupOrderRepository;
use app\common\repositories\store\order\StoreOrderCreateRepository;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\user\ExchangeQuotaRepository;
use crmeb\services\LockService;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
class MerchantShareholderRepository extends BaseRepository{
public function __construct(MerchantShareholderDao $dao){
$this->dao = $dao;
}
/**
* Common: 公共查询模型
* Author: wu-hui
* Time: 2024/06/13 11:30
* @param $search
* @return \app\common\model\system\merchant\MerchantShareholder
*/
public function getSearchModel($search){
return $this->dao->searchModel($search);
}
/**
* Common: 列表信息获取
* Author: wu-hui
* Time: 2024/06/14 16:00
* @param array $params
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList(array $params,int $page,int $limit):array{
$query = $this->getSearchModel($params);
$count = $query->count();
$list = $query->page($page,$limit)->select();
return compact('count','list');
}
/**
* Common: 处理加入信息(同步生成订单)
* Author: wu-hui
* Time: 2024/06/14 15:17
* @param $params
* @return mixed
*/
public function handleJoinInfo($params){
return Db::transaction(function() use ($params){
// 等级信息
$levelInfo = app()->make(MerchantShareholderLevelRepository::class)->getSearchModel(['id' => $params['level_id']])->findOrEmpty()->toArray();
$payMoney = $levelInfo['price'] ?? 0;
// 是否已经存在加入信息
$joinInfo = $this->getSearchModel([
'mer_id' => $params['mer_id'],
'level_id' => $params['level_id'],
'uid' => $params['uid'],
])->findOrEmpty()->toArray();
// 支付信息
$payInfo = array_intersect_key($params,array_flip((array)["pay_type","return_url"]));
$payInfo['money'] = (float)$payMoney;
$payResult = [];// 支付信息
$userInfo = $params['user_info'] ?? [];
// 判断: 信息是否已经存在
if($joinInfo){
// 信息已经存在
if((float)$payMoney > 0 && $joinInfo['status'] == 0){
// 需要支付 且未支付
$group_order_id = app()->make(StoreOrderRepository::class)->getSearch(['order_id'=>$joinInfo['order_id']])->value('group_order_id');
$groupOrder = app()->make(StoreGroupOrderRepository::class)->getSearch(['group_order_id'=>$group_order_id])->findOrEmpty();
$payResult = app()
->make(StoreOrderRepository::class)
->pay($payInfo['pay_type'],$userInfo,$groupOrder,$payInfo['return_url'],$params['is_app']);
}
else{
// 无需支付
$this->joinSuccess($joinInfo['id']);
}
}
else{
// 基本信息增加
$merchantShareholderId = $this->createJoinInfo($params);
// 判断:是否需要支付 需要支付生成订单并且获取支付信息
if((float)$payMoney > 0){
// 需要支付 发起支付
$groupOrder = app()
->make(LockService::class)
->exec('online_order.create',function() use ($payInfo,$userInfo){
$payType = array_search($payInfo['pay_type'],StoreOrderRepository::PAY_TYPE);
return app()
->make(StoreOrderCreateRepository::class)
->onlinePayment($payType,$payInfo,$userInfo, 37);
});
// 子订单只存在一个 直接查询即可
$orderId = app()->make(StoreOrderRepository::class)
->getSearch([])
->where('group_order_id',$groupOrder->group_order_id)
->value('order_id');
MerchantShareholder::update(['order_id'=>$orderId],['id'=>$merchantShareholderId]);
// 获取支付信息
$payResult = app()
->make(StoreOrderRepository::class)
->pay($payInfo['pay_type'],$userInfo,$groupOrder,$payInfo['return_url'],$params['is_app']);
}
else{
// 无需支付
$this->joinSuccess($merchantShareholderId);
}
}
return $payResult;
});
}
/**
* Common: 增加加入信息
* Author: wu-hui
* Time: 2024/06/14 13:55
* @param $params
* @return int|string
*/
public function createJoinInfo($params){
// 判断:是否已经存在
$hasId = (int)$this->getSearchModel([
'mer_id' => $params['mer_id'],
'uid' => $params['uid'],
])->value('id');
if($hasId > 0) throw new ValidateException('股东信息已经存在,请勿重复加入!');
// 判断:当前等级是否存在邀请名额限制 存在则判断是否到达限制
$levelQuotaLimit = (int)app()->make(MerchantShareholderLevelRepository::class)->getSearch(['id'=>$params['level_id']])->value('mer_quota');
if($levelQuotaLimit > 0){
$quotaUsed = (int)$this->getSearchModel(['mer_id' => $params['mer_id'],'level_id' => $params['level_id']])->count();
// 等级名额限制 小于等于 已邀请名额;禁止加入
if($levelQuotaLimit <= $quotaUsed) throw new ValidateException('本商户共创股东人数已达到上限,加入失败!');
}
// 不存在 增加
return MerchantShareholder::insertGetId([
'uid' => $params['uid'],
'mer_id' => $params['mer_id'],
'level_id' => $params['level_id'],
]);
}
/**
* Common: 加入成功后处理
* Author: wu-hui
* Time: 2024/06/14 15:17
* @param $id
*/
public function joinSuccess($id){
Db::startTrans();
try{
// 获取记录id
$info = $this->getSearchModel(['id'=>$id])->findOrEmpty()->toArray();
// 等级信息
$levelInfo = app()->make(MerchantShareholderLevelRepository::class)->getSearch(['id'=>$info['level_id']])->findOrEmpty()->toArray();
// 获取活动信息
$couponIds = $levelInfo['coupon_ids'] ? explode(',', $levelInfo['coupon_ids']) : [];
// 判断:是否赠送酒卡额度
if($levelInfo['quota'] > 0) $this->giveQuota((float)$levelInfo['quota'], (int)1, (int)$info['uid'], (int)$info['order_id']);
// 判断:是否赠送菜卡额度
if($levelInfo['vegetable_quota'] > 0) $this->giveQuota((float)$levelInfo['vegetable_quota'], (int)2, (int)$info['uid'], (int)$info['order_id']);
// 判断:是否赠送油卡额度
if($levelInfo['oil_quota'] > 0) $this->giveQuota((float)$levelInfo['oil_quota'], (int)3, (int)$info['uid'], (int)$info['order_id']);
// 判断:是否赠送封坛酒卡额度
if($levelInfo['wine_quota'] > 0) $this->giveQuota((float)$levelInfo['wine_quota'], (int)4, (int)$info['uid'], (int)$info['order_id']);
// 是否赠送优惠券
if(count($couponIds) > 0){
$uid = $info['uid'];
foreach($couponIds as $coupon_id){
$coupon = app()->make(StoreCouponRepository::class)->validSvipCouponTwo($coupon_id,$uid);
if (!$coupon) continue;//throw new ValidateException('优惠券失效');
// 获取商户id 和关联的品牌ID
$bindShop = [];
if((int)$info['mer_id'] > 0){
$bindShop = app()->make(MerchantRepository::class)->getSearch([])
->where('mer_id', $info['mer_id'])
->where('is_del', 0)
->findOrEmpty()
->toArray();
}
$params = [
'shop_mer_id' => $bindShop['mer_id'] ?? 0,
'brand_id' => $bindShop['brand_id'] ?? 0,
'user_order_id' => $info['order_id'] ?? 0,
];
app()->make(StoreCouponRepository::class)->sendCoupon($coupon, $uid,StoreCouponUserRepository::SEND_TYPE_BUY, $params);
}
}
// 是否赠送餐费积分
if((int)$levelInfo['integral_switch'] == 1) $this->giveIntegral($info,$levelInfo);
// 修改信息加入信息
MerchantShareholder::update([
'price' => $levelInfo['price'],
'quota' => $levelInfo['quota'],
'vegetable_quota' => $levelInfo['vegetable_quota'],
'oil_quota' => $levelInfo['oil_quota'],
'wine_quota' => $levelInfo['wine_quota'],
'coupon_ids' => $levelInfo['coupon_ids'],
'status' => 1,
],['id' => $id]);
Db::commit();
}catch(\Exception $e){
Db::rollback();
Log::info('共创股东支付成功 - 处理加入成功后的信息 - 错误: '.var_export([
'id' => $id,
'msg' => $e->getMessage()
],1));
}
}
/**
* Common: 赠送餐费积分
* Author: wu-hui
* Time: 2024/06/19 16:48
* @param array $info
* @param array $levelInfo
* @return bool
*/
public function giveIntegral(array $info,array $levelInfo){
$giveType = $levelInfo['integral_give_type'] ?? 0;// 赠送方式0=一次性赠送1=周期赠送
$giveNum = $levelInfo['integral_give_num'] ?? 0;// 赠送数量
$giveDay = $levelInfo['integral_give_day'] ?? 0;// 周期间隔(天)
$giveOverdueDay = $levelInfo['integral_overdue_day'] ?? 0;// 领取(解冻)后多少天过期
$cycleNum = $levelInfo['integral_cycle_num'] ?? 1;// 周期数量
// 根据赠送方式赠送积分赠送方式0=一次性赠送1=周期赠送
$insertData = [];// 赠送记录新增列表
if($giveType == 1){
// 周期赠送
$expectThawTime = date("Y-m-d H:i:s",time());// 第一期预计解冻时间
$expectOverdueTime = date("Y-m-d H:i:s",strtotime("+{$giveOverdueDay} day"));// 第一期预计过期时间
// 循环处理
for($i = 1;$i <= $cycleNum;$i++){
$insertData[] = [
'uid' => $info['uid'],
'mer_id' => $info['mer_id'],
'level_id' => $info['level_id'],
'integral_total' => $giveNum,
'status' => $i == 1 ? 1 : 0,// 第一期默认直接解冻
'expect_thaw_time' => $expectThawTime,
'real_thaw_time' => $i == 1 ? $expectThawTime : null,// 第一期默认直接解冻,
'expect_overdue_time' => $expectOverdueTime,
];
// 预计解冻和过期时间增加
$expectThawTime = date("Y-m-d H:i:s",strtotime($expectThawTime." +{$giveDay} day"));// 预计解冻时间
$expectOverdueTime = date("Y-m-d H:i:s",strtotime($expectThawTime." +{$giveOverdueDay} day"));// 预计过期时间
}
}else{
// 一次性全部赠送
$insertData[] = [
'uid' => $info['uid'],
'mer_id' => $info['mer_id'],
'level_id' => $info['level_id'],
'integral_total' => $giveNum,
'status' => 1,
'expect_thaw_time' => date("Y-m-d H:i:s",time()),
'real_thaw_time' => date("Y-m-d H:i:s",time()),
'expect_overdue_time' => $giveOverdueDay > 0 ? date("Y-m-d H:i:s",strtotime("+{$giveOverdueDay} day")) : '',
];
}
// 记录
if(count($insertData) > 0) MerchantShareholderIntegral::insertAll($insertData);
return true;
}
/**
* Common: 各种额度变更(赠送变更)
* Author: wu-hui
* Time: 2024/06/14 15:07
* @param float $num 变更数量
* @param int $quotaType 1=酒卡额度2=菜卡额度3=封坛酒额度4=加油卡额度
* @param int $uid 用户id
* @param int $orderId 订单id
*/
public function giveQuota(float $num,int $quotaType,int $uid,int $orderId){
$holdInfo = app()->make(ExchangeQuotaRepository::class)->searchModel(['uid'=>$uid,'quota_type'=>$quotaType])->findOrEmpty();
if((int)$holdInfo->uid <= 0) {
$holdInfo->uid = $uid;
$holdInfo->quota_type = $quotaType;
}
// 赠送
$changeFront = (float)$holdInfo->surplus_quota;
$holdInfo->total_quota += (float)$num;// 总额度
$holdInfo->surplus_quota += (float)$num;// 剩余额度
$holdInfo->save();
// 记录
ExchangeQuotaRecord::insert([
'uid' => $uid,
'product_id' => 0,
'order_id' => $orderId,
'order_product_id' => 0,
'change_type' => 1,
'change_quantity' => (float)$num,
'change_front' => $changeFront,
'change_after' => (float)$holdInfo->surplus_quota,
'remark' => "参加活动赠送",
'source' => 5,
'quota_type' => $quotaType,
]);
}
}