diff --git a/app/common/dao/system/merchant/MerchantQuotaDao.php b/app/common/dao/system/merchant/MerchantQuotaDao.php new file mode 100644 index 0000000..8797e84 --- /dev/null +++ b/app/common/dao/system/merchant/MerchantQuotaDao.php @@ -0,0 +1,65 @@ +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'); + } + + + + + + + +} diff --git a/app/common/model/system/merchant/MerchantQuota.php b/app/common/model/system/merchant/MerchantQuota.php new file mode 100644 index 0000000..953b9d0 --- /dev/null +++ b/app/common/model/system/merchant/MerchantQuota.php @@ -0,0 +1,26 @@ +hasOne(Merchant::class, 'mer_id', 'mer_id'); + } + + + + + +} diff --git a/app/common/repositories/system/merchant/MerchantQuotaRecordRepository.php b/app/common/repositories/system/merchant/MerchantQuotaRecordRepository.php index f21479f..9ff964e 100644 --- a/app/common/repositories/system/merchant/MerchantQuotaRecordRepository.php +++ b/app/common/repositories/system/merchant/MerchantQuotaRecordRepository.php @@ -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()); diff --git a/app/common/repositories/system/merchant/MerchantQuotaRepository.php b/app/common/repositories/system/merchant/MerchantQuotaRepository.php new file mode 100644 index 0000000..5e7ec12 --- /dev/null +++ b/app/common/repositories/system/merchant/MerchantQuotaRepository.php @@ -0,0 +1,52 @@ +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'); + } + + + + + + +} diff --git a/app/common/repositories/system/merchant/MerchantRepository.php b/app/common/repositories/system/merchant/MerchantRepository.php index 33357cc..664a9db 100644 --- a/app/common/repositories/system/merchant/MerchantRepository.php +++ b/app/common/repositories/system/merchant/MerchantRepository.php @@ -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'); } diff --git a/app/controller/admin/system/merchant/Merchant.php b/app/controller/admin/system/merchant/Merchant.php index 38359e8..712d897 100644 --- a/app/controller/admin/system/merchant/Merchant.php +++ b/app/controller/admin/system/merchant/Merchant.php @@ -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); - } - - } diff --git a/app/controller/admin/system/merchant/Quota.php b/app/controller/admin/system/merchant/Quota.php new file mode 100644 index 0000000..a6ccf7b --- /dev/null +++ b/app/controller/admin/system/merchant/Quota.php @@ -0,0 +1,76 @@ +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); + } + + + + +} diff --git a/route/admin/merchant.php b/route/admin/merchant.php index 5a9817f..d610bbd 100644 --- a/route/admin/merchant.php +++ b/route/admin/merchant.php @@ -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');