194 lines
6.8 KiB
PHP
194 lines
6.8 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\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 getActivateFormData(){
|
|
$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 activateExchangeCode($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)
|
|
->update([
|
|
'status' => 1,
|
|
'activate' => date("Y-m-d H:i:s",time()),
|
|
'mer_id' => $params['mer_id'],
|
|
'group_data_id' => $params['group_data_id'],
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|