|
@@ -0,0 +1,211 @@
|
|
|
+<?php namespace App\Http\Controllers\Admin;
|
|
|
+
|
|
|
+use App\Http\Requests\Admin\CouponRewardRule as Request;
|
|
|
+use App\Models\Coupon;
|
|
|
+use App\Models\CouponRewardRule as Model;
|
|
|
+use App\Models\Custom;
|
|
|
+use App\Models\City;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 优惠券自动发放规则
|
|
|
+ *
|
|
|
+ * @author 刘相欣
|
|
|
+ *
|
|
|
+ */
|
|
|
+class CouponRewardRule extends Auth{
|
|
|
+
|
|
|
+ protected function _initialize(){
|
|
|
+ parent::_initialize();
|
|
|
+ $this->assign('breadcrumb1','优惠券发放');
|
|
|
+ $this->assign('breadcrumb2','规则配置');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表页
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function index(Model $Model,Coupon $Coupon){
|
|
|
+ // 查询条件
|
|
|
+ $map = [];
|
|
|
+ // 查询数据
|
|
|
+ $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
|
|
|
+ // 循环处理数据
|
|
|
+ foreach ($list as $key => $value) {
|
|
|
+ // 优惠券名称
|
|
|
+ $value['coupon_name'] = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('name');
|
|
|
+ // 重组
|
|
|
+ $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,Coupon $Coupon,Custom $Custom,City $City){
|
|
|
+ if( request()->isMethod('post') ){
|
|
|
+ // 验证参数
|
|
|
+ $request->scene('add')->validate();
|
|
|
+ // 接收数据
|
|
|
+ $couponCode = request('coupon_code','');
|
|
|
+ $data['coupon_id'] = $Coupon->codeToId($couponCode);
|
|
|
+ $data['name'] = request('name','');
|
|
|
+ $data['std_num'] = request('std_num',0);
|
|
|
+ $data['start_time'] = request('start_time','');
|
|
|
+ $data['end_time'] = request('end_time','');
|
|
|
+ $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
|
|
|
+ $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
|
|
|
+ $data['status'] = 1;
|
|
|
+ $cityIds = request('city_ids',[]);
|
|
|
+ $data['city_ids'] = implode(',',$cityIds);
|
|
|
+ $removeCustom = request('remove_custom','');
|
|
|
+ // 循环处理
|
|
|
+ if( $removeCustom ) {
|
|
|
+ // 兼容逗号问题
|
|
|
+ $removeCustom = str_ireplace(',',',',$removeCustom);
|
|
|
+ // 转数组处理
|
|
|
+ $removeCustom = explode(',',$removeCustom);
|
|
|
+ // 循环处理
|
|
|
+ foreach ($removeCustom as $key=>$value) {
|
|
|
+ // 转ID
|
|
|
+ $customUid = $Custom->codeToId($value);
|
|
|
+ // id有问题
|
|
|
+ if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
|
|
|
+ // 重组
|
|
|
+ $removeCustom[$key] = $customUid;
|
|
|
+ }
|
|
|
+ // 去重
|
|
|
+ $removeCustom = array_unique($removeCustom);
|
|
|
+ }
|
|
|
+ // 排除数据
|
|
|
+ $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
|
|
|
+ // 写入数据表
|
|
|
+ $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']);
|
|
|
+ }
|
|
|
+ // 获取列表
|
|
|
+ $cityList = $City->getCityList();
|
|
|
+ // 分配数据
|
|
|
+ $this->assign('cityList',$cityList);
|
|
|
+ $this->assign('crumbs','新增');
|
|
|
+ // 加载模板
|
|
|
+ return $this->fetch();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function edit(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City){
|
|
|
+ // 接收参数
|
|
|
+ $id = request('id',0);
|
|
|
+ // 查询用户
|
|
|
+ $oldData = $Model->where(['id'=>$id])->first();
|
|
|
+ // 修改
|
|
|
+ if(request()->isMethod('post')){
|
|
|
+ // 验证参数
|
|
|
+ $request->scene('edit')->validate();
|
|
|
+ // 接收数据
|
|
|
+ $couponCode = request('coupon_code','');
|
|
|
+ $data['coupon_id'] = $Coupon->codeToId($couponCode);
|
|
|
+ $data['name'] = request('name','');
|
|
|
+ $data['std_num'] = request('std_num',0);
|
|
|
+ $data['start_time'] = request('start_time','');
|
|
|
+ $data['end_time'] = request('end_time','');
|
|
|
+ $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
|
|
|
+ $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
|
|
|
+ $cityIds = request('city_ids',[]);
|
|
|
+ $data['city_ids'] = implode(',',$cityIds);
|
|
|
+ $removeCustom = request('remove_custom','');
|
|
|
+ // 循环处理
|
|
|
+ if( $removeCustom ) {
|
|
|
+ // 兼容逗号问题
|
|
|
+ $removeCustom = str_ireplace(',',',',$removeCustom);
|
|
|
+ // 转数组处理
|
|
|
+ $removeCustom = explode(',',$removeCustom);
|
|
|
+ // 循环处理
|
|
|
+ foreach ($removeCustom as $key=>$value) {
|
|
|
+ // 转ID
|
|
|
+ $customUid = $Custom->codeToId($value);
|
|
|
+ // id有问题
|
|
|
+ if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
|
|
|
+ // 重组
|
|
|
+ $removeCustom[$key] = $customUid;
|
|
|
+ }
|
|
|
+ // 去重
|
|
|
+ $removeCustom = array_unique($removeCustom);
|
|
|
+ }
|
|
|
+ // 排除数据
|
|
|
+ $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
|
|
|
+ // 写入数据表
|
|
|
+ $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['coupon_code'] = $Coupon->idToCode($oldData['coupon_id']);
|
|
|
+ // 排除客户
|
|
|
+ if( $oldData['remove_custom'] ) {
|
|
|
+ // 列表
|
|
|
+ $removeCustom = [];
|
|
|
+ // 循环处理
|
|
|
+ foreach (explode(',',$oldData['remove_custom']) as $key => $value) {
|
|
|
+ // id转编码下发
|
|
|
+ $removeCustom[] = $Custom->idToCode($value);
|
|
|
+ }
|
|
|
+ // 排除用户
|
|
|
+ $oldData['remove_custom'] = implode(',',$removeCustom);
|
|
|
+ }
|
|
|
+ // 获取城市ID
|
|
|
+ $oldData['city_ids'] = explode(',',$oldData['city_ids']);
|
|
|
+ // 获取列表
|
|
|
+ $cityList = $City->getCityList();
|
|
|
+ // 分配数据
|
|
|
+ $this->assign('cityList',$cityList);
|
|
|
+ $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'=>'']);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|