304 lines
12 KiB
PHP
304 lines
12 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::input('create_num','生成数量')->required()->col(16)
|
|
->type('number')
|
|
->min('1')
|
|
->max('999999')
|
|
->step('1'),
|
|
];
|
|
$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){
|
|
// 是否允许生成兑换码
|
|
if($params['create_num'] <= 0) throw new ValidateException('生成数量必须大于0');
|
|
$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 = [];
|
|
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,
|
|
]);
|
|
}
|
|
|
|
|
|
}
|