|
@@ -10,6 +10,7 @@ use App\Models\CustomCoupon;
|
|
|
use App\Models\Product;
|
|
|
use App\Models\FilesManager;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use App\Models\City;
|
|
|
|
|
|
/**
|
|
|
* 优惠券管理
|
|
@@ -29,7 +30,7 @@ class Coupon extends Auth{
|
|
|
* 首页列表
|
|
|
*
|
|
|
* */
|
|
|
- public function index(Model $Model,AdminUser $AdminUser,CustomCoupon $CustomCoupon){
|
|
|
+ public function index(Model $Model,CustomCoupon $CustomCoupon){
|
|
|
// 接受参数
|
|
|
$code = request('coupon_code','');
|
|
|
$status = request('status');
|
|
@@ -52,15 +53,9 @@ class Coupon extends Auth{
|
|
|
foreach ($list as $key => $value) {
|
|
|
// id转编号
|
|
|
$value['coupon_code']= $Model->idToCode($value['id']);
|
|
|
- // id转编号
|
|
|
- $value['admin_name'] = $AdminUser->getOne($value['admin_uid'],'username');
|
|
|
+ $value['gant_name'] = $Model->getGrantType($value['grant_type'],'name');
|
|
|
// 如果过期时间
|
|
|
- if( $value['status'] == 0 && ( $value['end_time']>0 && $value['end_time']< time() ) ) {
|
|
|
- // 设置过期状态
|
|
|
- $Model->setStatusByExpire();
|
|
|
- // 状态设置
|
|
|
- $value['status'] =3;
|
|
|
- }
|
|
|
+ if( $value['end_time'] < time() ) $value['status'] = 3;
|
|
|
// id转编号
|
|
|
$value['custom_total'] = isset($customTotal[$value['id']]) ? $customTotal[$value['id']] : 0;
|
|
|
// 重组
|
|
@@ -77,7 +72,7 @@ class Coupon extends Auth{
|
|
|
* 添加
|
|
|
*
|
|
|
* */
|
|
|
- public function add( Request $request, Model $Model,Product $Product,Custom $Custom,FilesManager $FilesManager,CouponRebate $CouponRebate,CustomCoupon $CustomCoupon,CouponProduct $CouponProduct){
|
|
|
+ public function add( Request $request, Model $Model,City $City,CouponRebate $CouponRebate,Product $Product){
|
|
|
if( request()->isMethod('post') ){
|
|
|
// 验证参数
|
|
|
$request->scene('add')->validate();
|
|
@@ -87,20 +82,25 @@ class Coupon extends Auth{
|
|
|
$data['std_pay'] = request('std_pay',0);
|
|
|
$data['rebate'] = request('rebate',0);
|
|
|
$data['issue_total'] = request('issue_total',0);
|
|
|
- $data['start_time'] = request('start_time',date('Y-m-d'));
|
|
|
+ $data['start_time'] = request('start_time','');
|
|
|
$data['end_time'] = request('end_time','');
|
|
|
$data['type_id'] = request('type_id',1);
|
|
|
- $data['is_appt'] = request('is_appt',1);
|
|
|
- // 根据是否上传了商品范围文件
|
|
|
- if( $data['type_id'] == 1 && !request()->hasFile('product_file') ) return json_send(['code'=>'error','msg'=>'请上传指定商品范围']);
|
|
|
- // 根据是否上传了客户范围文件
|
|
|
- if( $data['is_appt'] == 1 && !request()->hasFile('custom_file') ) return json_send(['code'=>'error','msg'=>'请上传指定客户范围']);
|
|
|
+ $data['grant_type'] = request('grant_type',1);
|
|
|
+ $cityIds = request('city_ids',[]);
|
|
|
+ $data['city_ids'] = implode(',',$cityIds);
|
|
|
// 转换时间,默认现在现在生效
|
|
|
$data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
|
|
|
// 转换时间,默认无结束时间
|
|
|
- $data['end_time'] = $data['end_time'] ? strtotime($data['end_time'].' 23:59:59') : 0;
|
|
|
+ $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
|
|
|
+ // 转换时间,默认无结束时间
|
|
|
+ $data['exp_time'] = request('exp_time',0) ? strtotime(request('exp_time','')) : 0;
|
|
|
// 判断有效时间类型。获取不同的过期时间
|
|
|
- $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['end_time'];
|
|
|
+ $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['exp_time'];
|
|
|
+ // 验证信息
|
|
|
+ if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
|
|
|
+ if( $data['start_time'] < 0 ) return json_send(['code'=>'error','msg'=>'请填写活动开始时间']);
|
|
|
+ if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动时间必须大于当前时间']);
|
|
|
+ if( $data['exp_time'] <= 0 ) return json_send(['code'=>'error','msg'=>'请填写领用后有效期']);
|
|
|
// 赠品ID
|
|
|
$rebateId = 0;
|
|
|
// 如果是赠品表的话。处理赠品逻辑
|
|
@@ -112,125 +112,59 @@ class Coupon extends Auth{
|
|
|
// 设置为0,避免数据异常
|
|
|
$data['rebate'] = 0;
|
|
|
}
|
|
|
- // 商品范围
|
|
|
- $products = [];
|
|
|
- // 如果是商品范围
|
|
|
- if( $data['type_id'] == 1 ) {
|
|
|
- // 读取文件获取指定的商品范围
|
|
|
- $products = $FilesManager->excelToCode(request()->file('product_file'),$Product);
|
|
|
- // 产品范围不存在
|
|
|
- if( !$products ) return json_send(['code'=>'error','msg'=>'请上传可用的商品范围']);
|
|
|
- // 去重
|
|
|
- $products = array_values(array_unique($products));
|
|
|
- // 商品数量
|
|
|
- if( count($products) > 1000 ) return json_send(['code'=>'error','msg'=>'可用的商品范围请勿超过1000']);
|
|
|
- }
|
|
|
- // 客户范围
|
|
|
- $customs = [];
|
|
|
- // 如果是客户范围
|
|
|
- if( $data['is_appt'] == 1 ) {
|
|
|
- // 读取文件获取指定的客户范围
|
|
|
- $customs = $FilesManager->excelToCode(request()->file('custom_file'),$Custom);
|
|
|
- // 客户范围不存在
|
|
|
- if( !$customs ) return json_send(['code'=>'error','msg'=>'请上传可用的客户范围']);
|
|
|
- // 去重
|
|
|
- $customs = array_values(array_unique($customs));
|
|
|
- // 客户数量
|
|
|
- if( count($customs) > 10000 ) return json_send(['code'=>'error','msg'=>'客户范围请勿超过10000']);
|
|
|
- // 如果超过数量,不允许发放
|
|
|
- if( $data['issue_total'] < count($customs) ) return json_send(['code'=>'error','msg'=>'客户数量请勿超过发行数量']);
|
|
|
- }
|
|
|
// 组合数据,写入订单表,子表
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
// 写入
|
|
|
- $id = $Model->add($data);
|
|
|
+ $id = $Model->add($data);
|
|
|
// 提示新增失败
|
|
|
- if( !$id ) {
|
|
|
+ if( !$id ) {
|
|
|
// 回滚
|
|
|
DB::rollBack();
|
|
|
- // 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'新增失败']);
|
|
|
- }
|
|
|
- // 当前时间
|
|
|
- $time = time();
|
|
|
- // 如果有商品范围
|
|
|
- if( $products ) {
|
|
|
- // 循环商品
|
|
|
- foreach ($products as $key => $value) {
|
|
|
- // 时间处理
|
|
|
- $value = ['coupon_id'=>$id,'product_id'=>$value,'insert_time'=>$time,'update_time'=>$time];
|
|
|
- // 重组
|
|
|
- $products[$key] = $value;
|
|
|
- }
|
|
|
- // 写入数据
|
|
|
- $result = $CouponProduct->insert($products);
|
|
|
- // 提示新增失败
|
|
|
- if( !$result ) {
|
|
|
- // 回滚
|
|
|
- DB::rollBack();
|
|
|
- // 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'商品范围写入失败']);
|
|
|
- }
|
|
|
+ // 提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'新增失败']);
|
|
|
}
|
|
|
// 如果是赠品
|
|
|
- if( $rebateId ) {
|
|
|
+ if( $rebateId ) {
|
|
|
// 写入数据
|
|
|
- $result = $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
|
|
|
+ $result = $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
|
|
|
// 提示新增失败
|
|
|
- if( !$result ) {
|
|
|
+ if( !$result ) {
|
|
|
// 回滚
|
|
|
DB::rollBack();
|
|
|
// 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
|
|
|
- }
|
|
|
- }
|
|
|
- // 如果有客户范围
|
|
|
- if( $customs ) {
|
|
|
- // 分块,每1000一批写入
|
|
|
- foreach (array_chunk($customs,1000) as $chunk) {
|
|
|
- // 循环客户
|
|
|
- foreach ($chunk as $key => $value) {
|
|
|
- // 时间处理
|
|
|
- $value = ['coupon_id'=>$id,'custom_uid'=>$value,'exp_time'=>$Model->getExpTime($data['exp_time']),'insert_time'=>$time,'update_time'=>$time];
|
|
|
- // 重组
|
|
|
- $chunk[$key] = $value;
|
|
|
- }
|
|
|
- // 写入数据
|
|
|
- $result = $CustomCoupon->insert($chunk);
|
|
|
- // 提示新增失败
|
|
|
- if( !$result ) {
|
|
|
- // 回滚
|
|
|
- DB::rollBack();
|
|
|
- // 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'客户范围写入失败']);
|
|
|
- }
|
|
|
+ return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
|
|
|
}
|
|
|
}
|
|
|
// 提交
|
|
|
DB::commit();
|
|
|
- } catch (\Throwable $th) {
|
|
|
+ // 记录行为
|
|
|
+ $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
|
|
|
+ // 告知结果
|
|
|
+ return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
|
|
|
+ } catch (\Throwable $th) {
|
|
|
// 回滚
|
|
|
DB::rollBack();
|
|
|
// 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
|
|
|
+ return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
|
|
|
}
|
|
|
- // 记录行为
|
|
|
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
|
|
|
- // 告知结果
|
|
|
- return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
|
|
|
}
|
|
|
+ // 获取列表
|
|
|
+ $cityList = $City->getCityList();
|
|
|
+ $grantList = $Model->getGrantTypeList();
|
|
|
// 分配数据
|
|
|
+ $this->assign('cityList',$cityList);
|
|
|
+ $this->assign('grantList',$grantList);
|
|
|
$this->assign('crumbs','新增');
|
|
|
// 加载模板
|
|
|
- return $this->fetch();
|
|
|
+ return $this->fetch();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 编辑
|
|
|
*
|
|
|
* */
|
|
|
- public function edit( Request $request, Model $Model,Product $Product,Custom $Custom,FilesManager $FilesManager,CouponRebate $CouponRebate,CustomCoupon $CustomCoupon,CouponProduct $CouponProduct){
|
|
|
+ public function edit( Request $request, Model $Model,City $City,CouponRebate $CouponRebate,Product $Product){
|
|
|
// 接收参数
|
|
|
$id = request('id',0);
|
|
|
// 查询数据
|
|
@@ -245,18 +179,20 @@ class Coupon extends Auth{
|
|
|
$data['std_pay'] = request('std_pay',0);
|
|
|
$data['rebate'] = request('rebate',0);
|
|
|
$data['issue_total'] = request('issue_total',0);
|
|
|
- $data['start_time'] = request('start_time',date('Y-m-d'));
|
|
|
+ $data['start_time'] = request('start_time','');
|
|
|
$data['end_time'] = request('end_time','');
|
|
|
$data['type_id'] = request('type_id',1);
|
|
|
- $data['is_appt'] = request('is_appt',1);
|
|
|
+ $data['grant_type'] = request('grant_type',1);
|
|
|
+ $cityIds = request('city_ids',[]);
|
|
|
+ $data['city_ids'] = implode(',',$cityIds);
|
|
|
// 转换时间,默认现在现在生效
|
|
|
$data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
|
|
|
// 转换时间,默认无结束时间
|
|
|
- $data['end_time'] = $data['end_time'] ? strtotime($data['end_time'].' 23:59:59') : 0;
|
|
|
+ $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
|
|
|
+ // 转换时间,默认无结束时间
|
|
|
+ $data['exp_time'] = request('exp_time',0) ? strtotime(request('exp_time','')) : 0;
|
|
|
// 判断有效时间类型。获取不同的过期时间
|
|
|
- $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['end_time'];
|
|
|
- // 折扣不能超过10
|
|
|
- if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
|
|
|
+ $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['exp_time'];
|
|
|
// 赠品ID
|
|
|
$rebateId = 0;
|
|
|
// 如果是赠品表的话。处理赠品逻辑
|
|
@@ -268,32 +204,11 @@ class Coupon extends Auth{
|
|
|
// 设置为0,避免数据异常
|
|
|
$data['rebate'] = 0;
|
|
|
}
|
|
|
- // 商品范围
|
|
|
- $products = [];
|
|
|
- // 如果是商品范围,并且上传了文件
|
|
|
- if( $data['type_id'] == 1 && request()->hasFile('product_file')) {
|
|
|
- // 读取文件获取指定的商品范围
|
|
|
- $products = $FilesManager->excelToCode(request()->file('product_file'),$Product);
|
|
|
- // 产品范围不存在
|
|
|
- if( !$products ) return json_send(['code'=>'error','msg'=>'请上传可用的商品范围']);
|
|
|
- // 去重
|
|
|
- $products = array_values(array_unique($products));
|
|
|
- // 商品数量
|
|
|
- if( count($products) > 1000 ) return json_send(['code'=>'error','msg'=>'可用的商品范围请勿超过1000']);
|
|
|
- }
|
|
|
- // 客户范围
|
|
|
- $customs = [];
|
|
|
- // 如果是客户范围,并且上传了文件
|
|
|
- if( $data['is_appt'] == 1 && request()->hasFile('custom_file') ) {
|
|
|
- // 客户范围不存在
|
|
|
- if( !$customs ) return json_send(['code'=>'error','msg'=>'请上传可用的客户范围']);
|
|
|
- // 去重
|
|
|
- $customs = array_values(array_unique($customs));
|
|
|
- // 客户数量
|
|
|
- if( count($customs) > 10000 ) return json_send(['code'=>'error','msg'=>'客户范围请勿超过10000']);
|
|
|
- // 如果超过数量,不允许发放
|
|
|
- if( $data['issue_total'] < count($customs) ) return json_send(['code'=>'error','msg'=>'客户数量请勿超过发行数量']);
|
|
|
- }
|
|
|
+ // 验证信息
|
|
|
+ if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
|
|
|
+ if( $data['start_time'] < 0 ) return json_send(['code'=>'error','msg'=>'请填写活动开始时间']);
|
|
|
+ if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动时间必须大于当前时间']);
|
|
|
+ if( $data['exp_time'] <= 0 ) return json_send(['code'=>'error','msg'=>'请填写领用后有效期']);
|
|
|
// 组合数据,写入订单表,子表
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
@@ -303,92 +218,40 @@ class Coupon extends Auth{
|
|
|
if( !$result ) {
|
|
|
// 回滚
|
|
|
DB::rollBack();
|
|
|
- // 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'新增失败']);
|
|
|
+ // 提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'新增失败']);
|
|
|
}
|
|
|
// 如果是赠品券
|
|
|
if( $rebateId ) {
|
|
|
// 查询旧的
|
|
|
- $oldRebateId = $CouponRebate->query()->where([['coupon_id','=',$id],['product_id','=',$rebateId]])->value('id');
|
|
|
+ $oldRebateId = $CouponRebate->query()->where([['coupon_id','=',$id],['product_id','=',$rebateId]])->value('id');
|
|
|
// 如果旧的与新的不一致
|
|
|
if( $oldRebateId != $rebateId ) {
|
|
|
// 删除旧的
|
|
|
$CouponRebate->query()->where([['coupon_id','=',$id],['product_id','=',$rebateId]])->delete();
|
|
|
// 有旧的修改,无则添加新的
|
|
|
- $result = $oldRebateId ? $CouponRebate->edit($oldRebateId,['product_id'=>$rebateId,'rebate_num'=>1]) : $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
|
|
|
- // 提示新增失败
|
|
|
- if( !$result ) {
|
|
|
- // 回滚
|
|
|
- DB::rollBack();
|
|
|
- // 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- // 当前时间
|
|
|
- $time = time();
|
|
|
- // 如果有商品范围
|
|
|
- if( $products ) {
|
|
|
- // 循环商品
|
|
|
- foreach ($products as $key => $value) {
|
|
|
- // 重复的产品移除
|
|
|
- $oldProduct = $CouponProduct->query()->where([['coupon_id','=',$id],['product_id','=',$value]])->value('product_id');
|
|
|
- // 存在产品,移除
|
|
|
- if( $oldProduct ) {
|
|
|
- unset($products[$key]);
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 时间处理
|
|
|
- $value = ['coupon_id'=>$id,'product_id'=>$value,'insert_time'=>$time,'update_time'=>$time];
|
|
|
- // 重组
|
|
|
- $products[$key] = $value;
|
|
|
- }
|
|
|
- // 格式化
|
|
|
- $products = array_values($products);
|
|
|
- // 写入数据
|
|
|
- $result = $CouponProduct->insert($products);
|
|
|
- // 提示新增失败
|
|
|
- if( !$result ) {
|
|
|
- // 回滚
|
|
|
- DB::rollBack();
|
|
|
- // 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'商品范围写入失败']);
|
|
|
- }
|
|
|
- }
|
|
|
- // 如果有客户范围
|
|
|
- if( $customs ) {
|
|
|
- // 分块,每1000一批写入
|
|
|
- foreach (array_chunk($customs,1000) as $chunk) {
|
|
|
- // 循环客户
|
|
|
- foreach ($chunk as $key => $value) {
|
|
|
- // 时间处理
|
|
|
- $value = ['coupon_id'=>$id,'custom_uid'=>$value,'exp_time'=>$Model->getExpTime($data['exp_time']),'insert_time'=>$time,'update_time'=>$time];
|
|
|
- // 重组
|
|
|
- $chunk[$key] = $value;
|
|
|
- }
|
|
|
- // 写入数据
|
|
|
- $result = $CustomCoupon->insert($chunk);
|
|
|
+ $result = $oldRebateId ? $CouponRebate->edit($oldRebateId,['product_id'=>$rebateId,'rebate_num'=>1]) : $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
|
|
|
// 提示新增失败
|
|
|
if( !$result ) {
|
|
|
// 回滚
|
|
|
DB::rollBack();
|
|
|
// 提示失败
|
|
|
- return json_send(['code'=>'error','msg'=>'客户范围写入失败']);
|
|
|
+ return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 提交
|
|
|
DB::commit();
|
|
|
+ // 记录行为
|
|
|
+ $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
|
|
|
+ // 告知结果
|
|
|
+ return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
|
|
|
} catch (\Throwable $th) {
|
|
|
// 回滚
|
|
|
DB::rollBack();
|
|
|
// 提示失败
|
|
|
return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
|
|
|
}
|
|
|
- // 记录行为
|
|
|
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
|
|
|
- // 告知结果
|
|
|
- return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
|
|
|
}
|
|
|
// 如果是没有数据
|
|
|
if( !$oldData ) return $this->error('查无数据');
|
|
@@ -401,9 +264,16 @@ class Coupon extends Auth{
|
|
|
// 设置为0,避免数据异常
|
|
|
$oldData['rebate'] = $rebateId;
|
|
|
}
|
|
|
+ // 城市处理
|
|
|
+ $oldData['city_ids'] = explode(',',$oldData['city_ids']);
|
|
|
// 有效期,时间戳或者时间为0表示的时间段,否则表示领取后N天
|
|
|
$oldData['exp_type'] = $oldData['exp_time'] > 1000 ? 2 : 1;
|
|
|
+ // 获取列表
|
|
|
+ $cityList = $City->getCityList();
|
|
|
+ $grantList = $Model->getGrantTypeList();
|
|
|
// 分配数据
|
|
|
+ $this->assign('cityList',$cityList);
|
|
|
+ $this->assign('grantList',$grantList);
|
|
|
$this->assign('oldData',$oldData);
|
|
|
$this->assign('crumbs','修改');
|
|
|
// 加载模板
|