重构:管理后台补货额度管理 列表显示内容、额度变更操作重构

This commit is contained in:
wuhui_zzw 2024-06-04 17:01:10 +08:00
parent 234a9b084b
commit a386776429
8 changed files with 293 additions and 77 deletions

View File

@ -0,0 +1,65 @@
<?php
namespace app\common\dao\system\merchant;
use app\common\dao\BaseDao;
use app\common\model\system\merchant\MerchantQuota;
use crmeb\services\VicWordService;
class MerchantQuotaDao extends BaseDao{
protected function getModel(): string{
return MerchantQuota::class;
}
/**
* Common: 公共搜索模型
* Author: wu-hui
* Time: 2024/06/04 14:44
* @param array $params
* @param string|array $field
* @return MerchantQuota
*/
public function searchList(array $params, $field = '*'){
return (new MerchantQuota())
->alias('mqr')
->join('merchant m','m.mer_id = mqr.mer_id','right')
->field($field)
->where('is_del', 0)
->where('status', 1)
->when(isset($params['id']) && $params['id'] !== '',function($query) use ($params){
$query->where('mqr.id',(int)$params['id']);
})
->when(isset($params['mer_id']) && $params['mer_id'] !== '',function($query) use ($params){
$query->where('mqr.mer_id',(int)$params['mer_id']);
})
->when(isset($params['merchant_type']) && $params['merchant_type'] !== '',function($query) use ($params){
$query->where('m.merchant_type',(int)$params['merchant_type']);
})
->when(isset($params['keyword']) && $params['keyword'] !== '',function($query) use ($params){
if (is_numeric($params['keyword'])) {
$query->whereLike('mer_name|mer_keyword|mer_phone', "%{$params['keyword']}%");
} else {
$word = app()->make(VicWordService::class)->getWord($params['keyword']);
$query->where(function ($query) use ($word, $params) {
foreach ($word as $item) {
if(mb_strlen($item) > 1) $query->whereOr('mer_name', 'LIKE', "%$item%");
}
$query->whereOr('mer_name|mer_keyword', 'LIKE', "%{$params['keyword']}%");
});
}
})
->with([
'merchant' => function($query){
$query->field('mer_id,mer_name,mer_avatar');
},
])
->order('m.sort DESC,m.create_time DESC');
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace app\common\model\system\merchant;
use app\common\model\BaseModel;
class MerchantQuota extends BaseModel{
public static function tablePk(): string{
return 'id';
}
public static function tableName(): string{
return 'merchant_quota';
}
public function merchant(){
return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
}
}

View File

@ -34,53 +34,83 @@ class MerchantQuotaRecordRepository extends BaseRepository{
/**
* Common: 商户补货额度变更 并且添加变更记录
* Author: wu-hui
* Time: 2024/05/27 15:56
* @param int $merId 商户id
* @param float $quantity 变更数量
* @param int $changeType 变更类型0=减少1=增加
* @param int $source 变更来源0=后台手动处理1=补货减少
* @param int $orderId 订单id
* @return mixed
* Time: 2024/06/04 16:05
* @param array $params
* @return array
*/
public function changeQuota(int $merId, float $quantity,int $changeType,int $source = 0,int $orderId = 0){
// 是否允许生成兑换码
if(!in_array($changeType,[0, 1])) throw new ValidateException('非法请求,变更类型不明确!');
if($quantity <= 0) throw new ValidateException('变更数量必须大于0');
if($merId <= 0) throw new ValidateException('非法请求,商户不明确!');
// 处理
public function changeQuota(array $params){
/**
* 参数:
* mer_id 商户id
* change_type 变更类型0=减少1=增加
* quota_type 额度类型0=冠名品牌额度上限1=其他品牌额度上限
* quantity 变更数量
* source 变更来源0=后台手动处理1=补货减少
* order_id 订单id
*/
Db::startTrans();
try{
// 获取商户信息
$merInfo = app()->make(MerchantRepository::class)->getSearch(['mer_id'=>$merId])->field('quota_total,quota_used,quota_surplus')->findOrEmpty();
$changeFront = $merInfo->quota_surplus ?? 0;// 变更前数量
// 根据操作类型进行处理 变更类型0=减少1=增加
if($changeType == 1){
// 增加
$changeAfter = sprintf("%.2f", $changeFront + $quantity);
$merInfo->quota_total += $quantity;
$merInfo->quota_surplus += $quantity;
}else{
// 减少
$changeAfter = sprintf("%.2f", $changeFront - $quantity);
$merInfo->quota_surplus -= $quantity;
if($merInfo->quota_surplus < 0) throw new ValidateException('操作失败,剩余额度不足!');
// 获取额度信息
$info = app()->make(MerchantQuotaRepository::class)->getSearch(['mer_id'=>$params['mer_id']])->findOrEmpty();
if((int)$info->mer_id <= 0) $info->mer_id = $params['mer_id'];
// 根据变更类型进行处理
$recordData = [];
if($params['quota_type'] == 0){
// 冠名品牌额度
$changeFront = $info->title_brand_limit ?? 0;// 变更前数量
if($params['change_type'] == 1){
// 增加
$info->title_brand_limit = sprintf("%.2f", (float)$changeFront + (float)$params['quantity']);
}else{
// 减少
$info->title_brand_limit = sprintf("%.2f", (float)$changeFront - (float)$params['quantity']);
// 判断:减少总额度后 如果总额度小于已使用额度 则等于已使用额度
if($info->title_brand_limit < $info->title_brand_used) $info->title_brand_limit = $info->title_brand_used;
}
// 记录
$recordData = [
'mer_id' => $params['mer_id'],
'change_type' => (int)$params['change_type'],
'change_front' => $changeFront,
'change_quantity' => (float)$params['quantity'],
'change_after' => $info->title_brand_limit,
'source' => $params['source'] ?? 0,
'order_id' => $params['order_id'] ?? 0,
'quota_type' => $params['quota_type'] ?? 0
];
}
$merInfo->save();
// 记录
$record = [
'mer_id' => $merId,
'change_type' => (int)$changeType,
'change_front' => $changeFront,
'change_quantity' => $quantity,
'change_after' => $changeAfter,
'source' => $source,
'order_id' => $orderId
];
$this->dao->create($record);
else if($params['quota_type'] == 1){
// 其他品牌额度
$changeFront = $info->other_brand_limit ?? 0;// 变更前数量
if($params['change_type'] == 1){
// 增加
$info->other_brand_limit = sprintf("%.2f", (float)$changeFront + (float)$params['quantity']);
}else{
// 减少
$info->other_brand_limit = sprintf("%.2f", (float)$changeFront - (float)$params['quantity']);
// 判断:减少总额度后 如果总额度小于已使用额度 则等于已使用额度
if($info->other_brand_limit < $info->other_brand_used) $info->other_brand_limit = $info->other_brand_used;
}
// 记录
$recordData = [
'mer_id' => $params['mer_id'],
'change_type' => (int)$params['change_type'],
'change_front' => $changeFront,
'change_quantity' => (float)$params['quantity'],
'change_after' => $info->other_brand_limit,
'source' => $params['source'] ?? 0,
'order_id' => $params['order_id'] ?? 0,
'quota_type' => $params['quota_type'] ?? 0
];
}
// 保存修改
$info->save();
// 增加记录
if(count($recordData) > 0) $this->dao->create($recordData);
Db::commit();
return $merInfo;
return $info->toArray();
}catch(\Exception $e){
Db::rollback();
throw new ValidateException($e->getMessage());

View File

@ -0,0 +1,52 @@
<?php
namespace app\common\repositories\system\merchant;
use app\common\dao\system\merchant\MerchantQuotaDao;
use app\common\repositories\BaseRepository;
class MerchantQuotaRepository extends BaseRepository{
public function __construct(MerchantQuotaDao $dao){
$this->dao = $dao;
}
/**
* Common: 列表获取
* Author: wu-hui
* Time: 2024/06/04 14:59
* @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){
$field = [
'm.mer_id',
'm.mer_name',
'm.merchant_type',
'mqr.title_brand_limit',
'mqr.title_brand_used',
'mqr.title_brand_total',
'mqr.other_brand_limit',
'mqr.other_brand_used',
'mqr.other_brand_total',
];
$query = $this->dao->searchList($params,$field);
$count = $query->count();
$list = $query->page($page,$limit)->select();
return compact('count','list');
}
}

View File

@ -101,7 +101,7 @@ class MerchantRepository extends BaseRepository
$query->field('uid,nickname,avatar');
},
])
->field('quota_total,quota_used,quota_surplus,resource_shareholders_uid,merchant_type,sort,mer_id,mer_name,real_name,mer_phone,mer_address,mark,status,create_time,is_best,is_trader,type_id,category_id,copy_product_num,export_dump_num,is_margin,margin,ot_margin,mer_avatar,margin_remind_time,agent_id,shop_mer_id,brand_id')
->field('resource_shareholders_uid,merchant_type,sort,mer_id,mer_name,real_name,mer_phone,mer_address,mark,status,create_time,is_best,is_trader,type_id,category_id,copy_product_num,export_dump_num,is_margin,margin,ot_margin,mer_avatar,margin_remind_time,agent_id,shop_mer_id,brand_id')
->select();
return compact('count', 'list');
}

View File

@ -377,38 +377,4 @@ class Merchant extends BaseController
return app('json')->success($data);
}
/**
* Common: 补货额度 - 变更
* Author: wu-hui
* Time: 2024/05/27 11:51
* @return mixed
*/
public function quotaChange(){
$params = $this->request->params([
'change_type',
['change_quantity', 0],
['mer_id', 0]
]);
app()->make(MerchantQuotaRecordRepository::class)
->changeQuota((int)$params['mer_id'],(float)$params['change_quantity'],(int)$params['change_type']);
return app('json')->success('变更成功');
}
/**
* Common: 补货额度 - 变更记录明细
* Author: wu-hui
* Time: 2024/05/27 13:53
* @return mixed
*/
public function quotaChangeRecord(){
[$page, $limit] = $this->getPage();
$params = $this->request->params([
['mer_id', 0]
]);
$result = app()->make(MerchantQuotaRecordRepository::class)->getList((array)$params,(int)$page,(int)$limit);
return app('json')->success($result);
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace app\controller\admin\system\merchant;
use app\common\repositories\system\merchant\MerchantQuotaRecordRepository;
use app\common\repositories\system\merchant\MerchantQuotaRepository;
use crmeb\basic\BaseController;
use think\App;
class Quota extends BaseController{
protected $repository;
public function __construct(App $app, MerchantQuotaRepository $repository){
parent::__construct($app);
$this->repository = $repository;
}
/**
* Common: 额度列表
* Author: wu-hui
* Time: 2024/06/04 15:10
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function quotaList(){
[$page, $limit] = $this->getPage();
$params = $this->request->params([
['mer_id', ''],
'keyword',
'merchant_type'
]);
$result = $this->repository->getList((array)$params,(int)$page,(int)$limit);
return app('json')->success($result);
}
/**
* Common: 变更
* Author: wu-hui
* Time: 2024/05/27 11:51
* @return mixed
*/
public function quotaChange(){
$params = $this->request->params([
'change_type',
'quota_type',
['quantity', 0],
['mer_id', 0]
]);
app()->make(MerchantQuotaRecordRepository::class)->changeQuota($params);
return app('json')->success('变更成功');
}
/**
* Common: 变更记录明细
* Author: wu-hui
* Time: 2024/05/27 13:53
* @return mixed
*/
public function quotaChangeRecord(){
[$page, $limit] = $this->getPage();
$params = $this->request->params([
['mer_id', 0]
]);
$result = app()->make(MerchantQuotaRecordRepository::class)->getList((array)$params,(int)$page,(int)$limit);
return app('json')->success($result);
}
}

View File

@ -160,8 +160,9 @@ Route::group(function () {
'_alias' => '详情',
]);
// 补货额度
Route::post('quota/change', '.Merchant/quotaChange')->name('systemMerchantQuotaChange');
Route::post('quota/change_record', '.Merchant/quotaChangeRecord')->name('systemMerchantQuotaChange');
Route::post('quota/list', '.Quota/quotaList')->name('systemMerchantQuotaList');
Route::post('quota/change', '.Quota/quotaChange')->name('systemMerchantQuotaChange');
Route::post('quota/change_record', '.Quota/quotaChangeRecord')->name('systemMerchantQuotaChange');