204 lines
10 KiB
PHP
204 lines
10 KiB
PHP
<?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),
|
||
// 餐费积分
|
||
Elm::radio('integral_switch','是否赠送餐费积分',0)
|
||
->setOptions([['value' => 0,'label' => '关闭'],['value' => 1,'label' => '开启']])
|
||
->control([
|
||
[
|
||
'value' => 1,
|
||
'rule' => [
|
||
Elm::radio('integral_give_type','赠送方式',0)
|
||
->setOptions([['value' => 0,'label' => '一次性全部赠送'],['value' => 1,'label' => '周期赠送']])
|
||
->control([
|
||
[
|
||
'value' => 0,
|
||
'rule' => [
|
||
Elm::number('integral_give_num','赠送数量')->required()->min(0)->step(0.01)->precision(2)->col(24),
|
||
Elm::number('integral_overdue_day','过期时间(天)')->required()->min(0)->max(50000)->col(24)->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '赠送后多少天过期!为0则永不过期',
|
||
]
|
||
]),
|
||
]
|
||
],
|
||
[
|
||
'value' => 1,
|
||
'rule' => [
|
||
Elm::number('integral_cycle_num','周期数量')->required()->min(1)->max(100)->col(12),
|
||
Elm::number('integral_give_num','每期赠送数量')->required()->min(0)->step(0.01)->precision(2)->col(12)->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '每个周期赠送的数量!',
|
||
]
|
||
]),
|
||
Elm::number('integral_give_day','周期间隔(天)')->required()->min(0)->max(100)->col(24)->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '多少天为一个周期;例:填写10,则10天为一个周期;7月1日12时12分成为共创股东并且支付成功,则在7月11日12时12分解冻第二笔餐费积分',
|
||
]
|
||
]),
|
||
Elm::number('integral_overdue_day','过期时间')->required()->min(0)->max(50000)->col(24)->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '每个周期解冻后多少天过期!为0则永不过期',
|
||
]
|
||
]),
|
||
]
|
||
],
|
||
]),
|
||
]
|
||
],
|
||
]),
|
||
];
|
||
$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'],
|
||
'integral_switch' => $params['integral_switch'],
|
||
'integral_give_type' => $params['integral_give_type'],
|
||
'integral_give_num' => $params['integral_give_num'],
|
||
'integral_give_day' => $params['integral_give_day'],
|
||
'integral_overdue_day' => $params['integral_overdue_day'],
|
||
'integral_cycle_num' => $params['integral_cycle_num']
|
||
];
|
||
// 判断:权重值是否已经存在
|
||
$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);
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|