增加:部分商户增加补货额度管理

This commit is contained in:
wuhui_zzw 2024-05-27 14:03:56 +08:00
parent 2c10875a5e
commit d7e3e8e8f8
6 changed files with 206 additions and 2 deletions

View File

@ -0,0 +1,41 @@
<?php
namespace app\common\dao\system\merchant;
use app\common\dao\BaseDao;
use app\common\model\system\merchant\MerchantQuotaRecord;
class MerchantQuotaRecordDao extends BaseDao{
protected function getModel(): string{
return MerchantQuotaRecord::class;
}
/**
* Common: 公共搜索模型
* Author: wu-hui
* Time: 2024/05/27 13:52
* @param array $params
* @return MerchantQuotaRecord
*/
public function searchList(array $params){
return (new MerchantQuotaRecord())->when(isset($params['id']) && $params['id'] !== '',function($query) use ($params){
$query->where('id',(int)$params['id']);
})
->when(isset($params['mer_id']) && $params['mer_id'] !== '',function($query) use ($params){
$query->where('mer_id',(int)$params['mer_id']);
})
->with([
'merchant' => function($query){
$query->field('mer_id,mer_name,mer_avatar');
},
])
->order('create_time DESC,id DESC');
}
}

View File

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

View File

@ -0,0 +1,94 @@
<?php
namespace app\common\repositories\system\merchant;
use app\common\dao\system\merchant\MerchantQuotaRecordDao;
use app\common\repositories\BaseRepository;
use think\exception\ValidateException;
use think\facade\Db;
class MerchantQuotaRecordRepository extends BaseRepository{
public function __construct(MerchantQuotaRecordDao $dao){
$this->dao = $dao;
}
/**
* Common: 补货额度变更明细列表
* Author: wu-hui
* Time: 2024/05/27 13:52
* @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){
$query = $this->dao->searchList($params);
$count = $query->count();
$list = $query->page($page,$limit)->select();
return compact('count','list');
}
/**
* Common: 商户补货额度变更 并且添加变更记录
* Author: wu-hui
* Time: 2024/05/27 11:51
* @param $params
* @return array|\think\Model
*/
public function changeQuota($params){
$quantity = abs($params['change_quantity'] ?? 0);
$merId = $params['mer_id'] ?? 0;
// 是否允许生成兑换码
if(!in_array((int)$params['change_type'],[0, 1])) throw new ValidateException('非法请求,变更类型不明确!');
if($quantity <= 0) throw new ValidateException('变更数量必须大于0');
if($merId <= 0) throw new ValidateException('非法请求,商户不明确!');
// 处理
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((int)$params['change_type'] == 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('操作失败,剩余额度不足!');
}
$merInfo->save();
// 记录
$record = [
'mer_id' => $merId,
'change_type' => (int)$params['change_type'],
'change_front' => $changeFront,
'change_quantity' => $quantity,
'change_after' => $changeAfter,
];
$this->dao->create($record);
Db::commit();
return $merInfo;
}catch(\Exception $e){
Db::rollback();
throw new ValidateException($e->getMessage());
}
}
}

View File

@ -101,7 +101,8 @@ class MerchantRepository extends BaseRepository
$query->field('uid,nickname,avatar');
},
])
->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();
->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')
->select();
return compact('count', 'list');
}

View File

@ -8,6 +8,7 @@ namespace app\controller\admin\system\merchant;
use app\common\repositories\store\product\ProductCopyRepository;
use app\common\repositories\store\service\StoreServiceRepository;
use app\common\repositories\system\merchant\MerchantQuotaRecordRepository;
use app\common\repositories\system\merchant\MerchantTypeRepository;
use app\common\repositories\user\UserBillRepository;
use crmeb\basic\BaseController;
@ -375,4 +376,38 @@ class Merchant extends BaseController
$data = $this->repository->adminDetail($id);
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($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

@ -105,7 +105,7 @@ Route::group(function () {
Route::group('system/merchant', function () {
Route::get('create/form', '.Merchant/createForm')->name('systemMerchantCreateForm')->option([
'_alias' => '商户列表',
]);
]);
Route::get('count', '.Merchant/count')->name('systemMerchantCount')->option([
'_alias' => '商户列表统计',
]);
@ -159,6 +159,13 @@ Route::group(function () {
Route::get('detail/:id', '.Merchant/detail')->name('systemMerchantDetail')->option([
'_alias' => '详情',
]);
// 补货额度
Route::post('quota/change', '.Merchant/quotaChange')->name('systemMerchantQuotaChange');
Route::post('quota/change_record', '.Merchant/quotaChangeRecord')->name('systemMerchantQuotaChange');
})->prefix('admin.system.merchant')->option([
'_path' => '/merchant/list',
'_auth' => true,