assign('breadcrumb1','优惠券发放');
$this->assign('breadcrumb2','规则配置');
}
/**
* 列表页
*
* */
public function index(Model $Model,Coupon $Coupon,Business $Business){
// 查询条件
$map = [];
$session = session('userRule');
if ($session){
$map[] = ['company_id','=',$session['company_id']];
if ($session['business_id']){
$map[] = ['business_id','=',$session['business_id']];
}
if ($session['menu_type'] == 1 && $session['data_type'] == 2){
$shopIds = Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray();
}
}
// 查询数据
$list = $Model->query()->where($map);
if (isset($shopIds)) $list->whereIn('business_id',$shopIds);
$list = $list->orderByDesc('id')->paginate(config('page_num',10));
// 循环处理数据
foreach ($list as $key => $value) {
// 优惠券名称
$value['coupon_name'] = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('name');
$value['business_name'] = Business::query()->where([['id','=',$value['business_id']]])->value('name');
// 重组
$list[$key] = $value;
}
// 分配数据
$this->assign('empty', '
~~暂无数据 |
');
$this->assign('list',$list);
// 加载模板
return $this->fetch();
}
/**
* 添加
*
* */
public function add(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City,Business $Business){
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;
$data['business_id'] = request('business_id',0);
$session = session('userRule');
if ($session){
$data['company_id'] = $session['company_id'];
}
$cityIds = request('city_ids',[]);
$data['city_ids'] = implode(',',$cityIds);
$removeCustom = request('remove_custom','');
//店铺优惠卷是否存在
$res = $Coupon::query()->where(['id'=>$data['coupon_id'],'business_id'=>$data['business_id']])->first();
if (!$res) return json_send(['code'=>'error','msg'=>'新增失败,店铺优惠卷编码错误']);
// 循环处理
if( $removeCustom ) {
// 兼容逗号问题
$removeCustom = str_ireplace(',',',',$removeCustom);
$removeCustom = str_ireplace("\r\n",',',$removeCustom);
$removeCustom = str_ireplace("\n",',',$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();
$businessList = $Business->getList();
// 分配数据
$this->assign('cityList',$cityList);
$this->assign('businessList',$businessList);
$this->assign('crumbs','新增');
// 加载模板
return $this->fetch();
}
/**
* 修改
*
* */
public function edit(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City,Business $Business){
// 接收参数
$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;
$data['business_id'] = request('business_id',0);
$cityIds = request('city_ids',[]);
$data['city_ids'] = implode(',',$cityIds);
$removeCustom = request('remove_custom','');
// 循环处理
if( $removeCustom ) {
// 兼容逗号问题
$removeCustom = str_ireplace(',',',',$removeCustom);
$removeCustom = str_ireplace("\r\n",',',$removeCustom);
$removeCustom = str_ireplace("\n",',',$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("\n",$removeCustom);
}
// 获取城市ID
$oldData['city_ids'] = explode(',',$oldData['city_ids']);
// 获取列表
$cityList = $City->getCityList();
$businessList = $Business->getList();
// 分配数据
$this->assign('cityList',$cityList);
$this->assign('businessList',$businessList);
$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'=>'']);
}
}