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

144 lines
5.9 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\MerchantShareholderLevelDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\coupon\StoreCouponRepository;
use FormBuilder\Factory\Elm;
use think\exception\ValidateException;
use think\facade\Route;
class MerchantShareholderLevelRepository extends BaseRepository{
public function __construct(MerchantShareholderLevelDao $dao){
$this->dao = $dao;
}
/**
* Common: 公共查询模型
* Author: wu-hui
* Time: 2024/06/12 11:29
* @param $search
* @return mixed
*/
public function getSearchModel($search){
return $this->dao->searchModel($search);
}
/**
* Common: 根据条件获取列表
* Author: wu-hui
* Time: 2024/06/12 13:42
* @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->dao->searchModel($params);
$count = $query->count();
$list = $query->page($page,$limit)->select()->toArray();
return compact('count','list');
}
/**
* Common: 生成创建表单
* Author: wu-hui
* Time: 2024/06/12 13:30
* @param $id
* @param $merchantType
* @return \FormBuilder\Form
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function getEditFormData($id, $merchantType){
$info = [];
if($id > 0) {
$info = $this->getSearchModel(['id'=>$id])->findOrEmpty()->toArray();
$info['coupon_ids'] = $info['coupon_ids'] ? array_map(function($couponId){ return (int)$couponId;},explode(',', $info['coupon_ids'])) : [];
}
// 获取默认权重
$defaultWeight = (int)$this->getSearchModel(['merchant_type' => $merchantType])->max('weight');
$defaultWeight = $defaultWeight + 1;
// 表单生成
$url = Route::buildUrl('systemMerchantShareholderLevelSetForm', ['id' => $id,'merchant_type' => $merchantType])->build();
$form = Elm::createForm($url);
$rules = [
Elm::input('title','等级名称')->required(),
Elm::number('weight','等级权重', $defaultWeight)->required()->col(12)->min(1),
Elm::number('price','支付金额')->required()->col(12)->min(0),
Elm::number('mer_quota','名额限制')->required()->col(24)->min(0)->appendRule('suffix',[
'type' => 'div',
'style' => ['color' => '#999999'],
'domProps' => [
'innerHTML' => '每个商户最大可绑定多少该等级的共创股东为0时则不限制',
]
]),
Elm::number('quota','赠送瓶装酒额度')->required()->col(12)->min(0),
Elm::number('vegetable_quota','赠送菜卡额度')->required()->col(12)->min(0),
Elm::number('oil_quota','赠送油卡额度')->required()->col(12)->min(0),
Elm::number('wine_quota','赠送封坛酒额度')->required()->col(12)->min(0),
Elm::select('coupon_ids', '赠送优惠券')->options(function () {
return app()->make(StoreCouponRepository::class)
->getSearch(['mer_id' => request()->merId(), 'status' => 1, 'is_del' => 0])
->column('title as label,coupon_id as value');
})->col(24)->multiple(true)->filterable(true),
];
$form->setRule($rules);
return $form->setTitle( $id > 0 ? '编辑活动' : '添加活动')->formData($info);
}
/**
* Common: 信息添加 or 编辑
* Author: wu-hui
* Time: 2024/06/12 13:40
* @param $params
* @return \app\common\dao\BaseDao|int|\think\Model
* @throws \think\db\exception\DbException
*/
public function submitEditFormData($params){
$id = $params['id'] ?? 0;
// 是否允许编辑
if(empty($params['title'])) throw new ValidateException('请输入分类名称!');
if($params['weight'] <= 0) throw new ValidateException('等级权重必须大于0');
// 数据生成
$data = [
'title' => $params['title'],
'weight' => $params['weight'],
'mer_quota' => $params['mer_quota'] ?? 0,
'price' => $params['price'] ?? 0,
'quota' => $params['quota'],
'vegetable_quota' => $params['vegetable_quota'],
'oil_quota' => $params['oil_quota'],
'wine_quota' => $params['wine_quota'],
'coupon_ids' => is_array($params['coupon_ids']) ? implode(',',$params['coupon_ids']) : $params['coupon_ids'],
'merchant_type' => $params['merchant_type'],
];
// 判断:权重值是否已经存在
$isHas = $this->getSearchModel(['merchant_type' => $params['merchant_type'],'weight' => $params['weight']])
->when($id > 0,function($query) use ($id){
$query->where('id','<>',$id);
})
->value('id');
if($isHas > 0) throw new ValidateException('权重已经存在!');
// 判断:名称是否已经存在
$titleIsHas = $this->getSearchModel(['merchant_type' => $params['merchant_type']])
->where('title', $params['title'])
->when($id > 0, function($query) use ($id){
$query->where('id', '<>', $id);
})
->value('id');
if($titleIsHas) throw new ValidateException('等级名称已经存在!');
// 编辑操作
if($id > 0) return $this->dao->update($params['id'],$data);
else return $this->dao->create($data);
}
}