372 lines
15 KiB
PHP
372 lines
15 KiB
PHP
<?php
|
||
namespace app\common\repositories\user;
|
||
|
||
use app\common\dao\user\VipExchangeCodeDao;
|
||
use app\common\model\user\UserVipExchangeCode;
|
||
use app\common\repositories\BaseRepository;
|
||
use app\common\repositories\store\service\StoreServiceRepository;
|
||
use app\common\repositories\system\groupData\GroupDataRepository;
|
||
use app\common\repositories\system\groupData\GroupRepository;
|
||
use app\common\repositories\system\merchant\MerchantRepository;
|
||
use FormBuilder\Factory\Elm;
|
||
use think\exception\ValidateException;
|
||
use think\facade\Route;
|
||
|
||
class VipExchangeCodeRepository extends BaseRepository{
|
||
|
||
public function __construct(VipExchangeCodeDao $dao){
|
||
$this->dao = $dao;
|
||
}
|
||
/**
|
||
* Common: 公共查询模型
|
||
* Author: wu-hui
|
||
* Time: 2024/03/02 14:17
|
||
* @param $search
|
||
* @return \app\common\model\user\UserVipExchangeCode
|
||
*/
|
||
public function getSearchModel($search){
|
||
return $this->dao->searchList($search);
|
||
}
|
||
/**
|
||
* Common: 获取信息列表
|
||
* Author: wu-hui
|
||
* Time: 2024/03/02 14:18
|
||
* @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->searchList($params);
|
||
$count = $query->count();
|
||
$list = $query->page($page,$limit)->select()->toArray();
|
||
|
||
return compact('count','list');
|
||
}
|
||
/**
|
||
* Common: 生成创建表单数据
|
||
* Author: wu-hui
|
||
* Time: 2024/03/02 11:38
|
||
* @return \FormBuilder\Form
|
||
* @throws \FormBuilder\Exception\FormBuilderException
|
||
*/
|
||
public function getEditFormData(){
|
||
$formData = [];
|
||
$url = Route::buildUrl('systemUserVipExchangeCodeEditInfo')->build();
|
||
$form = Elm::createForm($url);
|
||
$rules = [
|
||
Elm::input('batch_title','当前批次名称')->required()->col(16),
|
||
Elm::radio('type', '生成方式', 0)->options([
|
||
['value' => 0, 'label' => '随机生成'],
|
||
['value' => 1, 'label' => '顺序生成'],
|
||
])->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '随机生成:使用字母和数字随机生成;顺序生成:根据指定前缀,指定初始值和结束值进行生成',
|
||
]
|
||
])->control([
|
||
[
|
||
'value' => 0,
|
||
'rule' => [
|
||
Elm::input('create_num','生成数量')->required()->col(16)
|
||
->type('number')
|
||
->min('1')
|
||
->max('999999')
|
||
->step('1'),
|
||
]
|
||
],
|
||
[
|
||
'value' => 1,
|
||
'rule' => [
|
||
Elm::input('prefix','前缀')->required()->col(16),
|
||
Elm::number('start','初始值')->required()->max(999999)->col(16),
|
||
Elm::number('end','结束值')->required()->max(999999)->col(16),
|
||
]
|
||
]
|
||
]),
|
||
];
|
||
$form->setRule($rules);
|
||
return $form->setTitle( '添加兑换码')->formData($formData);
|
||
}
|
||
/**
|
||
* Common: 兑换码生成
|
||
* Author: wu-hui
|
||
* Time: 2024/03/02 13:52
|
||
* @param $params
|
||
* @return int
|
||
*/
|
||
public function createExchangeCode($params){
|
||
// 是否允许生成兑换码
|
||
$isHas = (int)$this->dao->getSearch(['batch_title'=>$params['batch_title']])->value('id');
|
||
if($isHas > 0) throw new ValidateException('名称已经存在,请更换名称!');
|
||
// 生成批次唯一编号
|
||
$initial = substr(getFirstCharter($params['batch_title']), 0, 1);
|
||
$batchUnique = strtoupper(uniqid($initial));// 批次唯一编号
|
||
// 生成操作
|
||
$insertData = [];
|
||
if($params['type'] == 1){
|
||
// 顺序生成
|
||
if($params['start'] <= 0 || $params['end'] <= 0) throw new ValidateException('初始值和结束值必须大于0');
|
||
if($params['start'] >= $params['end']) throw new ValidateException('初始值必须小于结束值');
|
||
for($i = $params['start']; $i <= $params['end']; $i++){
|
||
// 兑换码生成
|
||
$exchangeCode = strtoupper($initial . substr(uniqid(), -8));
|
||
if(!in_array($exchangeCode, array_column($insertData,'exchange_code'))){
|
||
$insertData[] = [
|
||
'batch_title' => $params['batch_title'],
|
||
'batch_unique' => $batchUnique,
|
||
'exchange_code' => $params['prefix'] . str_pad($i, strlen($params['end']), '0', STR_PAD_LEFT),
|
||
];
|
||
}
|
||
}
|
||
}else{
|
||
// 随机生成
|
||
if($params['create_num'] <= 0) throw new ValidateException('生成数量必须大于0');
|
||
for($i = 0; count($insertData) < $params['create_num']; $i++){
|
||
// 兑换码生成
|
||
$exchangeCode = strtoupper($initial . substr(uniqid(), -8));
|
||
if(!in_array($exchangeCode, array_column($insertData,'exchange_code'))){
|
||
$insertData[] = [
|
||
'batch_title' => $params['batch_title'],
|
||
'batch_unique' => $batchUnique,
|
||
'exchange_code' => strtoupper($initial . substr(uniqid(), -8)),
|
||
];
|
||
}
|
||
}
|
||
}
|
||
|
||
return $this->dao->insertAll($insertData);
|
||
}
|
||
/**
|
||
* Common: 分配兑换码给商户表单
|
||
* Author: wu-hui
|
||
* Time: 2024/03/02 16:51
|
||
* @return \FormBuilder\Form
|
||
* @throws \FormBuilder\Exception\FormBuilderException
|
||
*/
|
||
public function allocationMerchantFormData(){
|
||
$formData = [];
|
||
// 获取商户列表
|
||
$merList = app()->make(MerchantRepository::class)
|
||
->search(['is_del' => 0,'merchant_type' => 0,'status' => 1])
|
||
->field(['mer_id as value','mer_name as label'])
|
||
->select()
|
||
->toArray();
|
||
// 获取有效的会员卡列表
|
||
$groupId = app()->make(GroupRepository::class)->getSearch(['group_key' => 'svip_pay'])->value('group_id');
|
||
$vipList = app()->make(GroupDataRepository::class)
|
||
->getSearch([])
|
||
->field(['group_data_id as value','value as label'])
|
||
->withAttr('label', function ($val) {
|
||
$value = json_decode($val, true);
|
||
|
||
return $value['svip_name'] ?? '';
|
||
})
|
||
->where('mer_id', 0)
|
||
->where('status', 1)
|
||
->where('group_id', $groupId)
|
||
->order('sort DESC,group_data_id ASC')
|
||
->select()
|
||
->toArray();
|
||
|
||
$url = Route::buildUrl('systemUserVipExchangeCodeActivateInfo')->build();
|
||
$form = Elm::createForm($url);
|
||
$rules = [
|
||
Elm::number('start_id','开始ID')->required()->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '例:输入 15 则ID大于15 小于等于结束ID的兑换码都会进行分配',
|
||
]
|
||
]),
|
||
Elm::number('end_id','结束ID')->required()->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '例:输入 80 则ID小于等于80 大于开始ID的兑换码都会进行分配',
|
||
]
|
||
]),
|
||
Elm::select('mer_id','绑定商户')->options($merList),
|
||
Elm::select('group_data_id','绑定会员卡类型')->options($vipList),
|
||
];
|
||
$form->setRule($rules);
|
||
return $form->setTitle( '分配兑换码')->formData($formData);
|
||
}
|
||
/**
|
||
* Common: 兑换码分配给商户(不进行激活操作)
|
||
* Author: wu-hui
|
||
* Time: 2024/03/02 17:09
|
||
* @param $params
|
||
* @return UserVipExchangeCode
|
||
*/
|
||
public function allocationMerchantExchangeCode($params){
|
||
// 是否允许生成兑换码
|
||
if($params['end_id'] <= 0) throw new ValidateException('结束id必须大于0');
|
||
if($params['end_id'] <= $params['start_id']) throw new ValidateException('结束id必须大于开始id');
|
||
if($params['mer_id'] <= 0) throw new ValidateException('请绑定商户');
|
||
if($params['group_data_id'] <= 0) throw new ValidateException('请绑定会员卡类型');
|
||
|
||
return UserVipExchangeCode::where('id','>',$params['start_id'])
|
||
->where('id','<=',$params['end_id'])
|
||
->where('status', 0)
|
||
->where(function($query) use ($params){
|
||
$query->where('mer_id', null)->whereOr('mer_id', '<=', 0);
|
||
})
|
||
->update([
|
||
'mer_id' => $params['mer_id'],
|
||
'group_data_id' => $params['group_data_id'],
|
||
]);
|
||
}
|
||
/**
|
||
* Common: 分配给员工并且激活表单
|
||
* Author: wu-hui
|
||
* Time: 2024/03/03 10:03
|
||
* @param $merId
|
||
* @return \FormBuilder\Form
|
||
* @throws \FormBuilder\Exception\FormBuilderException
|
||
*/
|
||
public function getActivateFormData($merId){
|
||
$formData = [];
|
||
// 获取商户员工列表
|
||
$serviceList = app()->make(StoreServiceRepository::class)
|
||
->getSearch([])
|
||
->field(['nickname as label','service_id as value'])
|
||
->where('mer_id', $merId)
|
||
->where('is_del', 0)
|
||
->select()
|
||
->toArray();
|
||
$url = Route::buildUrl('systemUserVipExchangeCodeActivateInfo')->build();
|
||
$form = Elm::createForm($url);
|
||
$rules = [
|
||
Elm::number('start_id','开始ID')->required()->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '例:输入 15 则ID大于15 小于等于结束ID的兑换码都会激活',
|
||
]
|
||
]),
|
||
Elm::number('end_id','结束ID')->required()->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '例:输入 80 则ID小于等于80 大于开始ID的兑换码都会激活',
|
||
]
|
||
]),
|
||
Elm::select('staff_id','绑定员工')->options($serviceList),
|
||
];
|
||
$form->setRule($rules);
|
||
return $form->setTitle( '分配并激活兑换码')->formData($formData);
|
||
}
|
||
/**
|
||
* Common: 分配给员工并且激活
|
||
* Author: wu-hui
|
||
* Time: 2024/03/03 10:08
|
||
* @param $params
|
||
* @param $merId
|
||
* @return UserVipExchangeCode
|
||
*/
|
||
public function activateExchangeCode($params, $merId){
|
||
// 是否允许生成兑换码
|
||
if($params['end_id'] <= 0) throw new ValidateException('结束id必须大于0');
|
||
if($params['end_id'] <= $params['start_id']) throw new ValidateException('结束id必须大于开始id');
|
||
if($params['staff_id'] <= 0) throw new ValidateException('请绑定员工');
|
||
|
||
return UserVipExchangeCode::where('id','>',$params['start_id'])
|
||
->where('id','<=',$params['end_id'])
|
||
->where('status', 0)
|
||
->where('mer_id', $merId)
|
||
->update([
|
||
'activate' => date("Y-m-d H:i:s"),
|
||
'status' => 1,
|
||
'staff_id' => $params['staff_id'],
|
||
]);
|
||
}
|
||
/**
|
||
* Common: 作废兑换码
|
||
* Author: wu-hui
|
||
* Time: 2024/03/09 17:21
|
||
* @return \FormBuilder\Form
|
||
* @throws \FormBuilder\Exception\FormBuilderException
|
||
*/
|
||
public function cancelFormData($merId){
|
||
$formData = [];
|
||
|
||
if($merId > 0) $url = Route::buildUrl('systemUserVipExchangeCodeCancelInfo')->build();
|
||
else $url = Route::buildUrl('systemUserVipExchangeCodeCancelInfo')->build();
|
||
|
||
$form = Elm::createForm($url);
|
||
$rules = [
|
||
Elm::number('start_id','开始ID')->required()->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '例:输入 15 则ID大于15 小于等于结束ID的兑换码都会作废',
|
||
]
|
||
]),
|
||
Elm::number('end_id','结束ID')->required()->appendRule('suffix',[
|
||
'type' => 'div',
|
||
'style' => ['color' => '#999999'],
|
||
'domProps' => [
|
||
'innerHTML' => '例:输入 80 则ID小于等于80 大于开始ID的兑换码都会作废',
|
||
]
|
||
]),
|
||
];
|
||
$form->setRule($rules);
|
||
return $form->setTitle( '作废兑换码')->formData($formData);
|
||
}
|
||
/**
|
||
* Common: 作废兑换码
|
||
* Author: wu-hui
|
||
* Time: 2024/03/09 17:25
|
||
* @param $params
|
||
* @param $merId
|
||
* @return UserVipExchangeCode
|
||
*/
|
||
public function cancelExchangeCode($params, $merId){
|
||
// 是否允许生成兑换码
|
||
if($params['end_id'] <= 0) throw new ValidateException('结束id必须大于0');
|
||
if($params['end_id'] <= $params['start_id']) throw new ValidateException('结束id必须大于开始id');
|
||
|
||
return UserVipExchangeCode::where('id','>',$params['start_id'])
|
||
->where('id','<=',$params['end_id'])
|
||
->where('status','<>', 2)
|
||
->when($merId > 0,function($query) use ($merId){
|
||
$query->where('mer_id', $merId);
|
||
})
|
||
->update([
|
||
'status' => 3,
|
||
]);
|
||
}
|
||
/**
|
||
* Common: 修改兑换码
|
||
* Author: wu-hui
|
||
* Time: 2024/03/30 15:44
|
||
* @param $params
|
||
* @return UserVipExchangeCode
|
||
*/
|
||
public function updateExchangeCode($params){
|
||
if(empty($params['new_exchange_code'])) throw new ValidateException('请输入新兑换码!');
|
||
if(strlen($params['new_exchange_code']) > 10) throw new ValidateException('兑换码长度不能超过10个字符!');
|
||
// 判断:是否存在信息
|
||
$info = $this->getSearchModel(['id'=>$params['id']])->findOrEmpty();
|
||
if((int)($info->id ?? 0) <= 0) throw new ValidateException('兑换码不存在!');
|
||
if((int)($info->status ?? 0) != 0) throw new ValidateException('当前兑换码不允许修改!');
|
||
// 判断:是否已经存在
|
||
$isHas = (int)$this->getSearchModel(['exchange_code'=>$params['new_exchange_code']])->value('id');
|
||
if($isHas > 0) throw new ValidateException('兑换码已经存在!');
|
||
// 执行修改
|
||
return UserVipExchangeCode::where('id',$params['id'])
|
||
->update([
|
||
'exchange_code' => $params['new_exchange_code'],
|
||
]);
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|