123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\CouponRewardRule as Request;
- use App\Models\Business;
- 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,Business $Business){
- // 查询条件
- $map = [];
- // 查询数据
- $list = $Model->query()->where($map);
- $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');
- // 重组
- $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,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'=>'']);
- }
- }
|