瀏覽代碼

【Add】优惠券增加商品范围,增加优惠券发放类型

liuxiangxin 6 月之前
父節點
當前提交
3ed271828a

+ 68 - 198
app/Http/Controllers/Admin/Coupon.php

@@ -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','修改');
 		// 加载模板

+ 164 - 0
app/Http/Controllers/Admin/CouponProduct.php

@@ -0,0 +1,164 @@
+<?php namespace App\Http\Controllers\Admin;
+
+use App\Http\Requests\Admin\CouponProduct as Request;
+use App\Models\Product;
+use App\Models\CouponProduct as Model;
+use App\Models\Coupon;
+
+/**
+ * 优惠券自动发放规则产品范围
+ *
+ * @author    刘相欣
+ *
+ */
+class CouponProduct extends Auth{
+	
+	protected function _initialize(){
+		parent::_initialize();
+		$this->assign('breadcrumb1','营销管理');
+		$this->assign('breadcrumb2','优惠券商品');
+	}
+
+	/**
+	 * 列表页
+	 * 
+	 * */
+    public function index(Model $Model,Coupon $Coupon,Product $Product){
+		// 接收参数
+		$couponCode				= request('coupon_code','');
+		$productCode			= request('product_code','');
+		$productId				= $Product->codeToId($productCode);
+		$couponId				= $Coupon->codeToId($couponCode);
+		// 查询条件
+		$map 					= [];
+		// 组合条件
+		if( $couponId )			$map[] = ['coupon_product.coupon_id','=',$couponId];
+		if( $productId )		$map[] = ['product.product_id','=',$productId];
+		// 查询数据
+		$list					= $Model->query()
+									->join('product','coupon_product.product_id','=','product.id')
+									->join('coupon','coupon.id','=','coupon_product.coupon_id')
+									->where($map)
+									->orderByDesc('id')
+									->select(['coupon_product.*','product.name as product_name','coupon.name as coupon_name'])
+									->paginate(config('page_num',10));
+		// 循环处理数据
+		foreach ($list as $key => $value) {
+			$value['coupon_code'] = $Coupon->idToCode($value['coupon_id']);
+			$value['product_code'] = $Product->idToCode($value['product_id']);
+			// 重组
+			$list[$key]			= $value;
+		}
+		// 分配数据
+		$this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
+		$this->assign('list',$list);
+		// 加载模板
+		return 					$this->fetch();
+    }
+
+	/**
+	 * 添加
+	 * 
+	 * */
+	public function add(Request $request,Model $Model,Product $Product,Coupon $Coupon){
+		if( request()->isMethod('post') ){
+			// 验证参数
+			$request->scene('add')->validate();
+			// 接收数据
+			$productCode			= request('product_code','');
+			$couponCode				= request('coupon_code','');
+			$data['product_id']		= $Product->codeToId($productCode);
+			$data['coupon_id']		= $Coupon->codeToId($couponCode);
+			// 如果操作失败
+			if( !$data['product_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的产品编码']);
+			if( !$data['coupon_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
+			// 查询产品ID是否存在
+			$oldId					= $Model->query()->where([['coupon_id','=',$data['coupon_id']],['product_id','=',$data['product_id']]])->value('id');
+			// 重复提醒
+			if( $oldId ) 			return json_send(['code'=>'error','msg'=>'产品编码已存在']);
+			// 写入数据表
+			$id						= $Model->add($data);
+			// 如果操作失败
+			if( !$id ) 				return json_send(['code'=>'error','msg'=>'新增失败']);
+			// 记录行为
+			$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
+			// 告知结果
+			return					json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
+		}
+		// 分配数据
+		$this->assign('crumbs','新增');
+		// 加载模板
+		return						$this->fetch(); 
+	}
+
+	/**
+	 * 修改
+	 * 
+	 * */
+	public function edit(Request $request,Model $Model,Product $Product,Coupon $Coupon){
+		// 接收参数
+		$id							= request('id',0);
+		// 查询用户
+		$oldData					= $Model->where(['id'=>$id])->first();
+		// 修改
+		if(request()->isMethod('post')){
+			// 验证参数
+			$request->scene('edit')->validate();
+			// 接收数据
+			$productCode			= request('product_code','');
+			$couponCode				= request('coupon_code','');
+			$data['product_id']		= $Product->codeToId($productCode);
+			$data['coupon_id']		= $Coupon->codeToId($couponCode);
+			// 如果操作失败
+			if( !$data['product_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的产品编码']);
+			if( !$data['coupon_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
+			// 查询产品ID是否存在
+			$oldId					= $Model->query()->where([['coupon_id','=',$data['coupon_id']],['product_id','=',$data['product_id']]])->value('id');
+			// 重复提醒
+			if( $oldId && $oldId != $id ) return json_send(['code'=>'error','msg'=>'产品编码已存在']);
+			// 写入数据表
+			$result					= $Model->edit($id,$data);
+			// 如果操作失败
+			if( !$result ) 			return json_send(['code'=>'error','msg'=>'新增失败']);
+			// 记录行为
+			$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
+			// 告知结果
+			return					json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
+		}
+		// 错误告知
+		if( !$oldData )				return $this->error('查无数据');
+		// 产品编码
+		$oldData['product_code'] 	= $Product->idToCode($oldData['product_id']);
+		$oldData['coupon_code'] 	= $Coupon->idToCode($oldData['coupon_id']);
+		// 分配数据
+		$this->assign('oldData',$oldData);
+		$this->assign('crumbs','修改');
+		// 加载模板
+		return						$this->fetch();
+	}
+
+	/**
+	 * 修改状态
+	 * 
+	 * */
+	public function set_status(Request $request,Model $Model){
+		// 验证参数
+		$request->scene('set_status')->validate();
+		// 设置状态
+		$id				= request('id',0);
+		$status			= request('status',0);
+		// 查询用户
+		$oldData		= $Model->where(['id'=>$id])->first();
+		// 如果用户不存在
+		if( !$oldData )	return json_send(['code'=>'error','msg'=>'数据不存在']);
+		// 执行修改
+		$result			= $Model->edit($id,['status'=>$status]);		
+		// 提示新增失败
+		if( !$result )	return json_send(['code'=>'error','msg'=>'设置失败']);
+		// 记录行为
+		$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
+		// 告知结果
+		return 			json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
+	}
+
+}

+ 2 - 0
app/Http/Controllers/Admin/CouponRewardProduct.php

@@ -37,6 +37,8 @@ class CouponRewardProduct extends Auth{
 		$list					= $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
 		// 循环处理数据
 		foreach ($list as $key => $value) {
+			// 产品编码
+			$value['rule_name'] 	= $CouponRewardRule->getOne($value['reward_rule_id'],'name');
 			// 产品编码
 			$value['product_code'] = $Product->idToCode($value['product_id']);
 			// 产品编码

+ 92 - 0
app/Http/Controllers/Admin/CustomCoupon.php

@@ -4,6 +4,7 @@ use App\Models\Custom;
 use App\Models\CustomCoupon as Model;
 use App\Models\Coupon as Coupon;
 use App\Http\Requests\Admin\CustomCoupon as Request;
+use Illuminate\Support\Facades\DB;
 
 /**
  * 优惠券管理
@@ -56,6 +57,8 @@ class CustomCoupon extends Auth{
 			$value['coupon_code']= $Coupon->idToCode($value['coupon_id']);
 			// id转编号
 			$value['custom_code'] = $Custom->idToCode($value['custom_uid']);
+			// id转编号
+			$value['custom_name'] = $Custom->getValue($value['custom_uid'],'username');
 			// 如果过期时间
 			if( $value['status'] == 0 && ( $value['exp_time']>0 && $value['exp_time']< time() ) ) {
 				// 状态设置
@@ -74,6 +77,95 @@ class CustomCoupon extends Auth{
     }
 
 
+	/**
+	 * 发放优惠券
+	 * 
+	 * */
+	public function add(Request $request,Model $Model,Coupon $Coupon,Custom $Custom){
+		if( request()->isMethod('post') ){
+			// 验证参数
+			$request->scene('add')->validate();
+			// 接收数据
+			$couponCode					= request('coupon_code','');
+			$customCodes				= request('custom_codes','');
+			$couponId					= $Coupon->codeToId($couponCode);
+			// 如果操作失败
+			if( !$couponId ) 			return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
+			// 查询优惠券信息
+			$couponInfo					= $Coupon->query()->find($couponId);
+			// 提示
+			if( !$couponId )			return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
+			// 兼容逗号问题
+			$customCodes				= str_ireplace(',',',',$customCodes);
+			$customCodes				= str_ireplace("\r\n",',',$customCodes);
+			$customCodes				= str_ireplace("\n",',',$customCodes);
+			// 转数组处理
+			$customCodes				= explode(',',$customCodes);
+			// 循环处理
+			foreach ($customCodes as $key=>$value) {
+				// 转ID
+				$customUid				= $Custom->codeToId($value);
+				// id有问题
+				if( !$customUid )		return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
+				// 重组
+				$customCodes[$key]  	= $customUid;
+			}
+			// 去重
+			$customCodes				= array_unique($customCodes);
+			// 数量
+			if(  count($customCodes) > 10000 )  return json_send(['code'=>'error','msg'=>'客户编码请勿超过10000']);
+			// 排除数据
+			$customCodes				= is_array($customCodes) ? $customCodes : [];
+			// 插入列表
+			$insertList					= [];
+			// 时间
+			$time 						= time();
+			// 循环客户
+			foreach ($customCodes as $key => $value) {
+				// 是否存在已发放的客户
+				$oldId					= $Model->query()->where([['coupon_id','=',$couponId],['custom_uid','=',$value]])->value('id');
+				// 如果存在ID
+				if( $oldId )			continue;
+				// 批量写入列表
+				$insertList[]			= ['coupon_id'=>$couponId,'custom_uid'=>$value,'exp_time'=>$Coupon->getExpTime($couponInfo['exp_time']),'insert_time'=>$time,'update_time'=>$time];
+			}
+			// 查询该优惠券已经发放的数量
+			$countTotal					= $Model->query()->where([['coupon_id','=',$couponId]])->count();
+			// 如果超过数量,不允许发放
+			if( $couponInfo['issue_total'] < (count($insertList) + $countTotal) ) return json_send(['code'=>'error','msg'=>'发放超过发行数量 => '.((count($insertList) + $countTotal) - $couponInfo['issue_total'])]);
+			// 如果有客户范围
+			if( $insertList )			{
+				// 分块,每1000一批写入
+				foreach (array_chunk($insertList,1000) as $chunk) {
+					// 写入数据
+					$result 			= $Model->insert($chunk);
+					// 提示新增失败
+					if( !$result )		{
+						// 提示失败
+						return			json_send(['code'=>'error','msg'=>'指定客户写入失败']);
+					}
+				}
+			}
+			// 告知结果
+			return					json_send(['code'=>'success','msg'=>'新增成功','action'=>'add','path'=>url('admin/custom_coupon/index?'.http_build_query(['coupon_code'=>$couponCode]))]);
+		}
+		// 优惠券ID
+		$couponCode					= request('coupon_code','');
+		$couponId					= $Coupon->codeToId($couponCode);
+		// 提示
+		if( !$couponId )			return $this->error('请填写正确的优惠券编码');
+		// 查询优惠券信息
+		$couponInfo					= $Coupon->query()->find($couponId);
+		// 提示
+		if( !$couponId )			return $this->error('请填写正确的优惠券编码');
+		// 分配数据
+		$this->assign('crumbs','发放优惠券');
+		$this->assign('couponInfo',$couponInfo);
+		// 加载模板
+		return						$this->fetch(); 
+	}
+
+
 	/**
 	 * 状态
 	 * 

+ 52 - 0
app/Http/Requests/Admin/CouponProduct.php

@@ -0,0 +1,52 @@
+<?php namespace App\Http\Requests\Admin;
+
+use App\Http\Requests\BaseRequest;
+
+/**
+ * 优惠券商品验证器
+ * 
+ */
+class CouponProduct extends BaseRequest
+{
+    /**
+     * 获取应用于请求的规则
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        // 返回结果
+        return      [
+            // 有时候我们希望某个字段在第一次验证失败后就停止运行验证规则,只需要将 bail 添加到规则中:
+            // 验证字段,验证规则,提示信息
+	        'product_code' 		=> 'required',
+            'coupon_id' 	    => 'required',
+	        'id'                => 'required|integer|gt:0',
+        ];
+    }
+
+    
+    // 场景列表
+    protected   $scenes         = [
+		'add'  		            => ['name'],
+        'edit'  		        => ['id','name'],
+        'set_status'  		    => ['id'],
+	];
+
+    /**
+     * 获取已定义验证规则的错误消息
+     *
+     * @return array
+     */
+    public function messages()
+    {
+        return [
+            'product_code.required'     => '产品编码必填',
+            'coupon_id.required'	    => '优惠券必选',
+            'id.required'               => 'ID未知',
+            'id.integer'                => 'ID格式错误',
+            'id.gt'   		            => 'ID格式错误',
+        ];
+    }
+    
+}

+ 2 - 1
app/Models/Coupon.php

@@ -2,6 +2,7 @@
 
 use App\Facades\Servers\Redis\Redis;
 use App\Facades\Servers\Redis\RedisLock;
+use App\Models\Traits\Coupon\GrantType;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Carbon;
@@ -12,7 +13,7 @@ use Illuminate\Support\Carbon;
  */
 class Coupon extends Model
 {
-    use HasFactory;
+    use HasFactory,GrantType;
 
     // 与模型关联的表名
     protected $table = 'coupon';

+ 56 - 0
app/Models/Traits/Coupon/GrantType.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Models\Traits\Coupon;
+
+
+/**
+ * 奖品类型
+ * 
+ */
+trait GrantType
+{
+
+    // 奖品类型
+    private     $grantType        =   ['1'=>[
+                                        'id'            =>1,
+                                        // 类型名称
+                                        'name'          =>'客户领用',
+                                    ],'2'=>[
+                                        'id'            =>2,
+                                        // 类型名称
+                                        'name'          =>'运营发放',
+                                    ],'3'=>[
+                                        'id'            =>3,
+                                        // 类型名称
+                                        'name'          =>'规则发放',
+                                    ]];
+
+    /**
+     * 交易类型列表
+     * 
+     */
+    public function getGrantTypeList(){
+        // 返回数据
+        return                  $this->grantType;
+    }
+
+    /**
+     * 获取交易类型
+     * 
+     * @param int       $id         类型ID
+     * @param string    $field      字段
+     * 
+     */
+    public function getGrantType($id,$field=''){
+        // 获取数据
+        $list               = $this->getGrantTypeList();
+        // 获取交易类型
+        $one                = isset($list[$id]) ? $list[$id] : [];
+        // 如果存在需要的字段
+        if( $field )        return isset($one[$field]) ? $one[$field] : null;
+        // 返回结果
+        return              $one;
+    }
+
+
+}

+ 54 - 37
resources/views/admin/coupon/add.blade.php

@@ -4,11 +4,11 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 @endsection
 @section('content')
 <form class="post-form" action="" method="post" enctype="multipart/form-data">
-	<div class="form-group col-sm-12">
+	<div class="form-group col-sm-3">
 		<label class="control-label">优惠券名称 <span class="text-red">*</span></label>
 		<input class="form-control" required="required" type="text" placeholder="优惠券名称" maxlength="20" name="name" value="" />
 	</div>
-	<div class="form-group col-sm-12">
+	<div class="form-group col-sm-3">
 		<label class="control-label">优惠券类型 <span class="text-red">*</span></label>
 		<select class="form-control" required="required" name="rebate_type" id="rebate_type">
 			<option value="1" >满减券</option>
@@ -16,59 +16,76 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 			<option value="3" >赠品券</option>
 		</select>
 	</div>
-	<div class="form-group col-sm-12">
+	<div class="form-group col-sm-6">
 		<label class="control-label">优惠力度 <span class="text-red">*</span></label>
 		<div class="form-group col-sm-12">
 			<label class="control-label"> </label>
-			<div class="form-group col-sm-2">
+			<div class="form-group col-sm-6">
 				<input  required="required" class="form-control" type="text" placeholder="满多少元" name="std_pay" value="" />
 			</div>
-			<div class="form-group col-sm-2">
+			<div class="form-group col-sm-6">
 				<input  required="required" class="form-control" id="rebate" type="text" placeholder="减多少元" name="rebate" value="" />
 			</div>
 		</div>
 	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">优惠券有效期 <span class="text-red">*</span></label>
+	<div class="form-group col-sm-6">
+		<label class="control-label">发放时间 <span class="text-red">*</span></label>
 		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" checked name="exp_type" value="1"> 领取后X天有效 </label>
-				<input class="form-control" type="number" placeholder="天数" name="exp_days" value="" />
+			<div class="form-group col-sm-6">
+				<label class="control-label">开始时间 <span class="text-red">*</span></label>
+				<input  required="required" class="form-control" type="datetime-local" placeholder="开始时间" name="start_time" value="" />
 			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="exp_type"  value="2"> 固定有效期</label>
-				<input class="form-control" id="rebate" type="date" placeholder="开始时间" name="start_time" value="" />
-				<input class="form-control" id="rebate" type="date" placeholder="结束时间" name="end_time" value="" />
+			<div class="form-group col-sm-6">
+			<label class="control-label">结束时间 <span class="text-red">*</span></label>
+				<input  required="required" class="form-control" type="datetime-local" placeholder="结束时间" name="end_time" value="" />
 			</div>
 		</div>
 	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">发行数量 <span class="text-red">*</span> </label>
-		<input class="form-control" required="required" type="number" placeholder="发行数量" min="0" max="999999" name="issue_total" value="" />
-	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">适用商品</label>
+	<div class="form-group col-sm-6">
+		<label class="control-label">到期时间 <span class="text-red">*</span></label>
 		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="type_id" checked value="2"> 全部商品范围</label>
-			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="type_id"  value="1"> 指定商品范围</label>
-				<input class="form-control" type="file" placeholder="适用商品范围" maxlength="20" name="product_file" value="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
+			<div class="form-group col-sm-12">
+				<div class="form-group col-sm-6">
+					<label class="control-label"><input type="radio" checked name="exp_type" value="1" > 领取后X天有效 </label>
+					<input class="form-control" type="number" placeholder="天数" name="exp_days" value="" max="999" />
+				</div>
+				<div class="form-group col-sm-6">
+					<label class="control-label"><input type="radio" name="exp_type"  value="2"> 固定到期时间</label>
+					<input class="form-control" type="datetime-local" placeholder="到期时间" name="exp_time" value="" />
+				</div>
 			</div>
 		</div>
 	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">适用客户</label>
-		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="is_appt" checked value="0"> 全部客户</label>
-			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="is_appt"  value="1"> 指定客户范围</label>
-				<input class="form-control" type="file" placeholder="适用客户范围" name="custom_file" value="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
-			</div>
-		</div>
+	<div class="form-group col-sm-2">
+		<label class="control-label">发放类型</label>
+		<select name="grant_type" class="form-control" >
+			@foreach ($grantList as $value)
+			<option value="{{$value['id']}}" >{{$value['name']}}</option>
+			@endforeach
+		</select>
+	</div>
+	<div class="form-group col-sm-2">
+		<label class="control-label">商品范围</label>
+		<select name="type_id" class="form-control" >
+			<option value="2" >全部商品</option>
+			<option value="1" >指定商品</option>
+		</select>
+	</div>
+	<div class="form-group col-sm-2">
+		<label class="control-label">发行数量 <span class="text-red">*</span> </label>
+		<input class="form-control" required="required" type="number" placeholder="优惠券发行数量" min="0" max="999999" name="issue_total" value="" />
+	</div>
+	<div class="form-group col-sm-6">
+		<label class="control-label">活动城市</label>
+		<select name="city_ids[]" class="form-control selectpicker" data-live-search="true" data-live-search-placeholder="搜索城市" data-none-results-text="未搜索到 {0}" title="选择城市" multiple>
+			@foreach ($cityList as $group)
+			<optgroup label="{{$group['name']}}">
+				@foreach ($group['city'] as $city)
+				<option value="{{$city['id']}}" >{{$city['name']}}</option>
+				@endforeach
+			</optgroup>
+			@endforeach
+		</select>
 	</div>
 	<div class="form-group col-sm-12">
 		@csrf

+ 58 - 41
resources/views/admin/coupon/edit.blade.php

@@ -4,71 +4,88 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 @endsection
 @section('content')
 <form class="post-form" action="" method="post">
-	<div class="form-group col-sm-12">
+<div class="form-group col-sm-3">
 		<label class="control-label">优惠券名称 <span class="text-red">*</span></label>
 		<input class="form-control" required="required" type="text" placeholder="优惠券名称" maxlength="20" name="name" value="{{$oldData['name']}}" />
 	</div>
-	<div class="form-group col-sm-12">
+	<div class="form-group col-sm-3">
 		<label class="control-label">优惠券类型 <span class="text-red">*</span></label>
 		<select class="form-control" required="required" name="rebate_type" id="rebate_type">
-			<option value="1" @if ( $oldData['rebate_type'] == '1' ) selected="selected" @endif>满减券</option>
-			<option value="2" @if ( $oldData['rebate_type'] == '2' ) selected="selected" @endif>折扣券</option>
-			<option value="3" @if ( $oldData['rebate_type'] == '3' ) selected="selected" @endif>赠品券</option>
+			<option value="1" @if ( $oldData['rebate_type'] == '1' ) selected="selected" @endif >满减券</option>
+			<option value="2" @if ( $oldData['rebate_type'] == '2' ) selected="selected" @endif >折扣券</option>
+			<option value="3" @if ( $oldData['rebate_type'] == '3' ) selected="selected" @endif >赠品券</option>
 		</select>
 	</div>
-	<div class="form-group col-sm-12">
+	<div class="form-group col-sm-6">
 		<label class="control-label">优惠力度 <span class="text-red">*</span></label>
 		<div class="form-group col-sm-12">
 			<label class="control-label"> </label>
-			<div class="form-group col-sm-2">
+			<div class="form-group col-sm-6">
 				<input  required="required" class="form-control" type="text" placeholder="满多少元" name="std_pay" value="{{$oldData['std_pay']}}" />
 			</div>
-			<div class="form-group col-sm-2">
+			<div class="form-group col-sm-6">
 				<input  required="required" class="form-control" id="rebate" type="text" placeholder="减多少元" name="rebate" value="{{$oldData['rebate']}}" />
 			</div>
 		</div>
 	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">优惠券有效期 <span class="text-red">*</span></label>
+	<div class="form-group col-sm-6">
+		<label class="control-label">发放时间 <span class="text-red">*</span></label>
 		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" checked name="exp_type" value="1" @if ( $oldData['exp_type'] == 1 ) checked="" @endif > 领取后X天有效 </label>
-				<input class="form-control" type="number" placeholder="天数" name="exp_days" @if ( $oldData['exp_type'] == 1 ) value="{{intval($oldData['exp_time'])}}" @endif />
+			<div class="form-group col-sm-6">
+				<label class="control-label">开始时间 <span class="text-red">*</span></label>
+				<input  required="required" class="form-control" type="datetime-local" placeholder="开始时间" name="start_time" value="{{date('Y-m-d H:i',$oldData['start_time'])}}" />
 			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="exp_type"  value="2" @if ( $oldData['exp_type'] == 2 )  checked="" @endif > 固定有效期</label>
-				<input class="form-control" id="rebate" type="date" placeholder="开始时间" name="start_time" @if ( $oldData['exp_type'] == 2 )  value="{{date('Y-m-d',$oldData['start_time'])}}" @endif />
-				<input class="form-control" id="rebate" type="date" placeholder="结束时间" name="end_time"  @if ( $oldData['exp_type'] == 2 ) value="{{date('Y-m-d',$oldData['end_time'])}}" @endif />
+			<div class="form-group col-sm-6">
+			<label class="control-label">结束时间 <span class="text-red">*</span></label>
+				<input  required="required" class="form-control" type="datetime-local" placeholder="结束时间" name="end_time" value="{{date('Y-m-d H:i',$oldData['end_time'])}}" />
 			</div>
 		</div>
 	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">发行数量 <span class="text-red">*</span> </label>
-		<input class="form-control" required="required" type="number" placeholder="发行数量" min="0" max="999999" name="issue_total" value="{{$oldData['issue_total']}}" />
-	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">适用商品</label>
+	<div class="form-group col-sm-6">
+		<label class="control-label">到期时间 <span class="text-red">*</span></label>
 		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="type_id" value="2" @if ( $oldData['type_id'] == 2 ) checked="" @endif> 全部商品范围</label>
-			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="type_id"  value="1" @if ( $oldData['type_id'] == 1 ) checked="" @endif > 指定商品范围</label>
-				<input class="form-control" type="file" placeholder="适用商品范围" maxlength="20" name="product_file" value="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
+			<div class="form-group col-sm-12">
+				<div class="form-group col-sm-6">
+					<label class="control-label"><input type="radio" checked name="exp_type" value="1"  @if ( $oldData['exp_type'] == 1 )  checked="" @endif > 领取后X天有效 </label>
+					<input class="form-control" type="number" placeholder="天数" name="exp_days" max="999" value="{{$oldData['exp_time']<1000?$oldData['exp_time']:''}}" />
+				</div>
+				<div class="form-group col-sm-6">
+					<label class="control-label"><input type="radio" name="exp_type"  value="2"  @if ( $oldData['exp_type'] == 2 )  checked="" @endif> 固定到期时间</label>
+					<input class="form-control" type="datetime-local" placeholder="到期时间" name="exp_time" value="{{$oldData['exp_time']>1000?date('Y-m-d H:i',$oldData['exp_time']):''}}" />
+				</div>
 			</div>
 		</div>
 	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">适用客户</label>
-		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="is_appt" value="0" @if ( $oldData['is_appt'] == 0 ) checked="" @endif> 全部客户</label>
-			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="is_appt"  value="1" @if ( $oldData['is_appt'] == 1 ) checked="" @endif> 指定客户范围</label>
-				<input class="form-control" type="file" placeholder="适用客户范围" name="custom_file" value="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
-			</div>
-		</div>
+	<div class="form-group col-sm-2">
+		<label class="control-label">发放类型</label>
+		<select name="grant_type" class="form-control" >
+			@foreach ($grantList as $value)
+			<option value="{{$value['id']}}" @if ( $oldData['grant_type'] == $value['id'] ) selected="selected" @endif >{{$value['name']}}</option>
+			@endforeach
+		</select>
+	</div>
+	<div class="form-group col-sm-2">
+		<label class="control-label">商品范围</label>
+		<select name="type_id" class="form-control" >
+			<option value="2" @if ( $oldData['type_id'] == '2' ) selected="selected" @endif>全部商品</option>
+			<option value="1" @if ( $oldData['type_id'] == '1' ) selected="selected" @endif>指定商品</option>
+		</select>
+	</div>
+	<div class="form-group col-sm-2">
+		<label class="control-label">发行数量 <span class="text-red">*</span> </label>
+		<input class="form-control" required="required" type="number" placeholder="优惠券发行数量" min="0" max="999999" name="issue_total" value="{{$oldData['issue_total']}}" />
+	</div>
+	<div class="form-group col-sm-6">
+		<label class="control-label">活动城市</label>
+		<select name="city_ids[]" class="form-control selectpicker" data-live-search="true" data-live-search-placeholder="搜索城市" data-none-results-text="未搜索到 {0}" title="选择城市" multiple>
+			@foreach ($cityList as $group)
+			<optgroup label="{{$group['name']}}">
+				@foreach ($group['city'] as $city)
+				<option value="{{$city['id']}}" @if ( in_array($city['id'],$oldData['city_ids']) ) selected="selected" @endif >{{$city['name']}}</option>
+				@endforeach
+			</optgroup>
+			@endforeach
+		</select>
 	</div>
 	<div class="form-group col-sm-12">
 		@csrf
@@ -89,7 +106,7 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 			// 如果是折扣
 			if( rebateType == 2 ) $('#rebate').attr('placeholder','打几折');
 			// 如果是赠品
-			if( rebateType == 3 ) $('#rebate').attr('placeholder','产品编码');
+			if( rebateType == 3 ) $('#rebate').attr('placeholder','产品编码').val('');
 		})
 	})
 </script>

+ 24 - 15
resources/views/admin/coupon/index.blade.php

@@ -49,13 +49,13 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 					<tr>
 						<th>优惠券编码</th>
 						<th>优惠券名称</th>
-						<th>指定商品</th>
 						<th>优惠券类型</th>
+						<th>已领取/发行量</th>
+						<th>商品范围</th>
+						<th>发放类型</th>
 						<th>优惠券状态</th>
-						<th>优惠券数量</th>
-						<th>已领取数量</th>
-						<th>指定客户</th>
-						<th>创建时间</th>
+						<th>发放时间</th>
+						<th>修改时间</th>
 						<th>操作</th>
 					</tr>
 				</thead>
@@ -64,33 +64,42 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 					<tr>
 						<td> {{$a['coupon_code']}}</td>
 						<td> {{$a['name']}}</td>
-						<td> {{$a['type_id']==2?'全部商品':'指定商品'}}</td>
-						<td> 
+						<td>
 							@if ($a['rebate_type'] == 1) 满减券 @endif
 							@if ($a['rebate_type'] == 2) 折扣券 @endif
 							@if ($a['rebate_type'] == 3) 赠品券 @endif
 						</td>
+						<td> {{$a['custom_total']}}/{{$a['issue_total']}}</td>
+						<td> 
+							@if ( $a['type_id'] == 1 )
+								<a href="{{url('admin/coupon_product/index?'.http_build_query(['coupon_code'=>$a['coupon_code']]))}}">指定商品</a>
+							@else
+								全部商品
+							@endif
+						</td>
+						<td> {{$a['gant_name']}} </td>
 						<td> 
 							@if ($a['status'] == 0) 正常 @endif
 							@if ($a['status'] == 2) 暂停 @endif
 							@if ($a['status'] == 3) 过期 @endif
 							@if ($a['status'] == 4) 作废 @endif
 						</td>
-						<td> {{$a['issue_total']}}</td>
-						<td> {{$a['custom_total']}}</td>
-						<td> {{$a['is_appt']==0?'全部客户':'指定客户'}}</td>
-						<td> {{date('Y/m/d H:i:s',$a['insert_time'])}}</td>
+						<td> {{date('Y/m/d H:i',$a['start_time'])}} ~ {{date('Y/m/d H:i:s',$a['end_time'])}}</td>
+						<td> {{date('Y/m/d H:i',$a['update_time'])}}</td>
 						<td>
-							@if( check_auth('admin/custom_coupon/index') )
-							<a class="btn btn-sm btn-primary" href="{{url('admin/custom_coupon/index?'.http_build_query(['coupon_code'=>$a['coupon_code']]))}}" title="领用记录">
-								领用记录
-							</a>
+							@if( check_auth('admin/custom_coupon/add') )
+								<a href="{{url('admin/custom_coupon/add?'.http_build_query(['coupon_code'=>$a['coupon_code']]))}}" class="btn btn-primary btn-sm ">发放优惠券</a>
 							@endif
 							@if( check_auth('admin/coupon/edit') )
 							<a class="btn btn-sm btn-warning" href="{{url('admin/coupon/edit?'.http_build_query(['id'=>$a['id']]))}}" title="查看">
 								编辑
 							</a>
 							@endif
+							@if( check_auth('admin/custom_coupon/index') )
+							<a class="btn btn-sm btn-primary" href="{{url('admin/custom_coupon/index?'.http_build_query(['coupon_code'=>$a['coupon_code']]))}}" title="领用记录">
+								发放记录
+							</a>
+							@endif
 							@if( check_auth('admin/coupon/set_status') )
 								@if ( $a['status'] == 2)
 									<a class="delete btn btn-sm btn-success" data-url="{{url('admin/coupon/set_status?'.http_build_query(['id'=>$a['id'],'status'=>'0']))}}">

+ 20 - 0
resources/views/admin/coupon_product/add.blade.php

@@ -0,0 +1,20 @@
+@extends('admin.public.base')
+@section('body_class')
+style="margin: 0 auto;width: 96%;padding: 30px 0px;"
+@endsection
+@section('content')
+<form class="post-form" action="" method="post">
+	<div class="form-group col-sm-12">
+		<label class="control-label">优惠券编码</label>
+		<input class="form-control" required="required" type="text" placeholder="优惠券编码" name="coupon_code" value="{{request('coupon_code','')}}" />
+	</div>
+	<div class="form-group col-sm-12">
+		<label class="control-label">产品编码</label>
+		<input class="form-control" required="required" type="text" placeholder="产品编码" name="product_code" value="" />
+	</div>
+	<div class="form-group col-sm-12">
+		@csrf
+		<input id="send" type="submit" value="提交" class="btn btn-primary btn-block" />
+	</div>
+</form>
+@endsection

+ 21 - 0
resources/views/admin/coupon_product/edit.blade.php

@@ -0,0 +1,21 @@
+@extends('admin.public.base')
+@section('body_class')
+style="margin: 0 auto;width: 96%;padding: 30px 0px;"
+@endsection
+@section('content')
+<form class="post-form" action="" method="post">
+<div class="form-group col-sm-12">
+		<label class="control-label">优惠券编码</label>
+		<input class="form-control" required="required" type="text" placeholder="优惠券编码" name="coupon_code" value="{{$oldData['coupon_code']}}" />
+	</div>
+	<div class="form-group col-sm-12">
+		<label class="control-label">产品编码</label>
+		<input class="form-control" required="required" type="text" placeholder="产品编码" name="product_code" value="{{$oldData['product_code']}}" />
+	</div>
+	<div class="form-group col-sm-12">
+		@csrf
+		<input type="hidden" name="id" id="id" value="{{$oldData['id']}}" />
+		<input id="send" type="submit" value="提交" class="btn btn-primary btn-block" />
+	</div>
+</form>
+@endsection

+ 77 - 0
resources/views/admin/coupon_product/index.blade.php

@@ -0,0 +1,77 @@
+@extends('admin.public.base')
+@section('body_class')
+style="margin: 0 auto;width: 96%;padding: 30px 0px;"
+@endsection
+@section('content')
+
+@if(check_auth('admin/coupon_product/add'))
+	<div class="page-header">
+		<a href="{{url('admin/coupon_product/add?'.http_build_query(['coupon_code'=>request('coupon_code','')]))}}" class="btn btn-primary">新增</a>
+	</div>
+@endif
+
+<form action="" method="get" class="form-horizontal form-line">
+	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-2" style="margin-right: 2px;">
+		<input type="text" class="form-control" name="coupon_code" value="{{request('coupon_code','')}}" placeholder="请输入优惠券编码查询" />
+	</div>
+	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-2" style="margin-right: 2px;">
+		<input type="text" class="form-control" name="product_code" value="{{request('product_code','')}}" placeholder="请输入产品编码查询" />
+	</div>
+	<input type="submit" class="btn btn-sm btn-primary" value="查询"/>
+	<a href="{{url('admin/coupon_product/index')}}" class="btn btn-sm btn-default" >重置</a>
+</form>
+
+<div class="row">
+	<div class="col-xs-12">	
+		<div class="table-responsive">
+			<table class="table table-striped table-bordered table-hover">
+				<thead>
+					<tr>
+						<th>ID</th>
+						<th>优惠券编码</th>
+						<th>优惠券名称</th>
+						<th>产品编码</th>
+						<th>产品名称</th>
+						<th>状态</th>
+						<th>修改时间</th>
+						<th>操作</th>									
+					</tr>
+				</thead>
+				
+				<tbody>
+						@foreach ($list as $a)
+						<tr>
+							<th>{{$a['id']}}</th>
+							<td>{{$a['coupon_code']}}</td>
+							<td>{{$a['coupon_name']}}</td>
+							<td>{{$a['product_code']}}</td>
+							<td>{{$a['product_name']}}</td>
+							<td>{{$a['status']?'停用':'启用'}}</td>
+							<td> {{date('Y/m/d H:i:s',$a['update_time'])}}</td>
+							<td>
+								@if(check_auth('admin/coupon_product/edit'))
+								<a href="{{url('admin/coupon_product/edit?'.http_build_query(['id'=>$a['id']]))}}" class="btn btn-sm btn-warning" >编辑</a>
+								@endif
+								@if(check_auth('admin/coupon_product/set_status'))
+									@if($a['status'])
+									<a data-url="{{url('admin/coupon_product/set_status?'.http_build_query(['id'=>$a['id'],'status'=>0]))}}" class="set_status btn btn-sm btn-success" >启用</a>
+									@else
+									<a data-url="{{url('admin/coupon_product/set_status?'.http_build_query(['id'=>$a['id'],'status'=>1]))}}" class="set_status btn btn-sm btn-danger" >停用</a>
+									@endif
+								@endif
+							</td>							
+						</tr>  
+						@endforeach
+						<tr>
+							<td colspan="20" class="page">{{$list->render()}}</td>
+						</tr>
+						<tr>
+							<td colspan="20">总计 {{$list->total()}} 个商店</td>
+						</tr>
+				</tbody>
+				
+			</table>
+		</div>
+	</div>
+</div>
+@endsection

+ 1 - 1
resources/views/admin/coupon_reward_product/index.blade.php

@@ -50,7 +50,7 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 							<td>{{$a['product_code']}}</td>
 							<td>{{$a['product_name']}}</td>
 							<td>{{$a['product_units']}}</td>
-							<td>{{$a['product_units']}}</td>
+							<td>{{$a['rule_name']}}</td>
 							<td>{{$a['status']?'停用':'启用'}}</td>
 							<td> {{date('Y/m/d H:i:s',$a['update_time'])}}</td>
 							<td>

+ 5 - 77
resources/views/admin/custom_coupon/add.blade.php

@@ -6,91 +6,19 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 <form class="post-form" action="" method="post" enctype="multipart/form-data">
 	<div class="form-group col-sm-12">
 		<label class="control-label">优惠券名称 <span class="text-red">*</span></label>
-		<input class="form-control" required="required" type="text" placeholder="优惠券名称" maxlength="20" name="name" value="" />
+		<input class="form-control" required="required" type="text" placeholder="优惠券名称" maxlength="20" name="name" value="{{$couponInfo['name']}}" disabled="" />
 	</div>
 	<div class="form-group col-sm-12">
-		<label class="control-label">优惠券类型 <span class="text-red">*</span></label>
-		<select class="form-control" required="required" name="rebate_type" id="rebate_type">
-			<option value="1" >满减券</option>
-			<option value="2" >折扣券</option>
-			<option value="3" >赠品券</option>
-		</select>
+		<label class="control-label">优惠券编码 <span class="text-red">*</span></label>
+		<input class="form-control" required="required" type="text" placeholder="优惠券名称" maxlength="20" name="coupon_code" value="{{request('coupon_code','')}}" disabled="" />
 	</div>
 	<div class="form-group col-sm-12">
-		<label class="control-label">优惠力度 <span class="text-red">*</span></label>
-		<div class="form-group col-sm-12">
-			<label class="control-label"> </label>
-			<div class="form-group col-sm-2">
-				<input  required="required" class="form-control" type="text" placeholder="满多少元" name="std_pay" value="" />
-			</div>
-			<div class="form-group col-sm-2">
-				<input  required="required" class="form-control" id="rebate" type="text" placeholder="减多少元" name="rebate" value="" />
-			</div>
-		</div>
-	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">优惠券有效期 <span class="text-red">*</span></label>
-		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" checked name="exp_type" value="1"> 领取后X天有效 </label>
-				<input class="form-control" type="number" placeholder="天数" name="exp_days" value="" />
-			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="exp_type"  value="2"> 固定有效期</label>
-				<input class="form-control" id="rebate" type="date" placeholder="开始时间" name="start_time" value="" />
-				<input class="form-control" id="rebate" type="date" placeholder="结束时间" name="end_time" value="" />
-			</div>
-		</div>
-	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">发行数量 <span class="text-red">*</span> </label>
-		<input class="form-control" required="required" type="number" placeholder="发行数量" min="0" max="999999" name="issue_total" value="" />
-	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">适用商品</label>
-		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="type_id" checked value="2"> 全部商品范围</label>
-			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="type_id"  value="1"> 指定商品范围</label>
-				<input class="form-control" type="file" placeholder="适用商品范围" maxlength="20" name="product_file" value="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
-			</div>
-		</div>
-	</div>
-	<div class="form-group col-sm-12">
-		<label class="control-label">适用客户</label>
-		<div class="form-group col-sm-12">
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="is_appt" checked value="0"> 全部客户</label>
-			</div>
-			<div class="form-group col-sm-2">
-				<label class="control-label"><input type="radio" name="is_appt"  value="1"> 指定客户范围</label>
-				<input class="form-control" type="file" placeholder="适用客户范围" name="custom_file" value="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
-			</div>
-		</div>
+		<label class="control-label">发放对象</label>
+		<textarea class="form-control" name="custom_codes" rows="10" placeholder="需要发放的客户编码,多个使用换行" ></textarea>
 	</div>
 	<div class="form-group col-sm-12">
 		@csrf
 		<input id="send" type="submit" value="提交" class="btn btn-primary btn-block" />
 	</div>
 </form>
-@endsection
-
-@section('javascript')
-<script>
-	$(function(){
-		// 类型变更
-		$('#rebate_type').change(function(){
-			// 获取值
-			var rebateType = $(this).val();
-			// 如果是满减
-			if( rebateType == 1 ) $('#rebate').attr('placeholder','减多少元');
-			// 如果是折扣
-			if( rebateType == 2 ) $('#rebate').attr('placeholder','打几折');
-			// 如果是赠品
-			if( rebateType == 3 ) $('#rebate').attr('placeholder','产品编码');
-		})
-	})
-</script>
 @endsection

+ 12 - 4
resources/views/admin/custom_coupon/index.blade.php

@@ -50,10 +50,11 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 					<tr>
 						<th>优惠券编码</th>
 						<th>优惠券名称</th>
-						<th>指定商品</th>
+						<th>客户编码</th>
+						<th>客户名称</th>
+						<th>商品范围</th>
 						<th>优惠券类型</th>
 						<th>优惠券状态</th>
-						<th>客户编码</th>
 						<th>有效期限</th>
 						<th>操作</th>
 					</tr>
@@ -63,7 +64,15 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 					<tr>
 						<td> {{$a['coupon_code']}}</td>
 						<td> {{$a['coupon_name']}}</td>
-						<td> {{$a['type_id']==2?'全部商品':'指定商品'}}</td>
+						<td> {{$a['custom_code']}}</td>
+						<td> {{$a['custom_name']}}</td>
+						<td> 
+							@if ( $a['type_id'] == 1 )
+								<a href="{{url('admin/coupon_product/index?'.http_build_query(['coupon_code'=>$a['coupon_code']]))}}">指定商品</a>
+							@else
+								全部商品
+							@endif
+						</td>
 						<td> 
 							@if ($a['rebate_type'] == 1) 满减券 @endif
 							@if ($a['rebate_type'] == 2) 折扣券 @endif
@@ -76,7 +85,6 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 							@if ($a['status'] == 3) 过期 @endif
 							@if ($a['status'] == 4) 作废 @endif
 						</td>
-						<td> {{$a['custom_code']}}</td>
 						<td> {{date('Y/m/d H:i:s',$a['exp_time'])}}</td>
 						<td>
 							@if( check_auth('admin/custom_coupon/set_status') )

+ 10 - 2
routes/web.php

@@ -186,13 +186,21 @@ Route::middleware('admin')->prefix('admin')->group(function(){
     // 状态
     Route::any('coupon/set_status',[App\Http\Controllers\Admin\Coupon::class,'set_status']);
 
+    /* 优惠券发放规则-商品范围 */
+    // 列表
+    Route::any('coupon_product/index',[App\Http\Controllers\Admin\CouponProduct::class,'index']);
+    // 详情
+    Route::any('coupon_product/add',[App\Http\Controllers\Admin\CouponProduct::class,'add']);
+    // 详情
+    Route::any('coupon_product/edit',[App\Http\Controllers\Admin\CouponProduct::class,'edit']);
+    // 状态
+    Route::any('coupon_product/set_status',[App\Http\Controllers\Admin\CouponProduct::class,'set_status']);
+
     /* 优惠券 */
     // 列表
     Route::any('custom_coupon/index',[App\Http\Controllers\Admin\CustomCoupon::class,'index']);
     // 详情
     Route::any('custom_coupon/add',[App\Http\Controllers\Admin\CustomCoupon::class,'add']);
-    // 详情
-    Route::any('custom_coupon/edit',[App\Http\Controllers\Admin\CustomCoupon::class,'edit']);
     // 状态
     Route::any('custom_coupon/set_status',[App\Http\Controllers\Admin\CustomCoupon::class,'set_status']);