123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\Promo as Request;
- use App\Models\Promo as Model;
- use App\Models\PromoRebate;
- use App\Models\Product;
- use Illuminate\Support\Facades\DB;
- use App\Models\City;
- use App\Models\WeiBan\Tags as WeiBanTags;
- /**
- * 产品促销活动
- *
- * @author 刘相欣
- *
- */
- class Promo extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','营销管理');
- $this->assign('breadcrumb2','产品促销活动');
- }
- /**
- * 首页列表
- *
- * */
- public function index(Model $Model,City $City,PromoRebate $PromoRebate){
- // 接受参数
- $code = request('promo_code','');
- $promoName = request('promo_name','');
- $status = request('status');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- // 编码转ID
- $id = $code ? $Model->codeToId($code) : 0;
- // 查询条件
- $map = [];
- // 编码ID
- if( $id ) $map[] = ['id','=',$id];
- if( $promoName ) $map[] = ['name','=',$promoName];
- if( $startTime ) $map[] = ['start_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['end_time','<=',strtotime($endTime)];
- if( !is_null($status) ) $map[] = ['status','=',$status];
- // 查询数据
- $list = $Model->query()->where($map)->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // id转编号
- $value['promo_code']= $Model->idToCode($value['id']);
- // 发放类型
- $value['gant_name'] = $Model->getGrantType($value['grant_type'],'name');
- // 如果已经到了结束时间。活动结束
- if( $value['status'] == 0 && $value['end_time'] <= time() ) {
- // 状态设置
- $Model->edit($value['id'],['status'=>3]);
- // 状态设置
- $value['status'] = 3;
- }
- // 判断是不是可以参与
- if( $value['city_ids'] ) {
- // 解析数组
- $cityids = explode(',',$value['city_ids']);
- // 获取城市
- foreach ($cityids as $kk=>$vv) {
- // 获取值
- $vv = $City->getOne($vv,'name');
- // 获取城市名
- $cityids[$kk] = $vv;
- }
- // 城市列表
- $value['city_ids'] = implode('、',$cityids);
- }
- // 赠品
- if( $value['rebate_type'] == 3 ) {
- // 返回结果
- $value['rebate'] = (string) $PromoRebate->query()->join('product','promo_rebate.product_id','=','product.id')->where([['promo_id','=',$value['id']]])->value('product.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,City $City,PromoRebate $PromoRebate,Product $Product,WeiBanTags $WeiBanTags){
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 组合数据
- $data['name'] = request('name',0);
- $data['rebate_type'] = request('rebate_type',0);
- $data['std_pay'] = request('std_pay',0);
- $data['rebate'] = request('rebate',0);
- $data['start_time'] = request('start_time','');
- $data['end_time'] = request('end_time','');
- $data['status'] = request('status',2);
- $data['type_id'] = request('type_id',0);
- $cityIds = request('city_ids',[]);
- $tagScope = request('tag_scope',[]);
- $data['city_ids'] = implode(',',$cityIds);
- $data['tag_scope'] = implode(',',$tagScope);
- // 转换时间,默认现在现在生效
- $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
- // 转换时间,默认无结束时间
- $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
- // 验证信息
- 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'=>'活动时间必须大于当前时间']);
- // 赠品ID
- $rebateId = 0;
- // 如果是赠品表的话。处理赠品逻辑
- if( $data['rebate_type'] == 3 ) {
- // 产品编码
- $rebateId = $Product->codeToId($data['rebate']);
- // 赠品ID不存在
- if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
- // 设置为0,避免数据异常
- $data['rebate'] = 0;
- }
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- try {
- // 写入
- $id = $Model->add($data);
- // 提示新增失败
- if( !$id ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'新增失败']);
- }
- // 如果是赠品
- if( $rebateId ) {
- // 写入数据
- $result = $PromoRebate->add(['promo_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 提示失败
- return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
- }
- }
- // 提交
- DB::commit();
- // 记录行为
- $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()]]);
- }
- }
- // 获取列表
- $cityList = $City->getCityList();
- // 标签列表
- $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
- $tagList = [];
- // 循环数据
- foreach ($tagData as $value) {
- $tagList[$value['group']][] = $value['name'];
- }
- $grantList = $Model->getGrantTypeList();
- // 分配数据
- $this->assign('cityList',$cityList);
- $this->assign('grantList',$grantList);
- $this->assign('tagList',$tagList);
- $this->assign('crumbs','新增');
- // 加载模板
- return $this->fetch();
- }
-
- /**
- * 编辑
- *
- * */
- public function edit( Request $request, Model $Model,City $City,PromoRebate $PromoRebate,Product $Product,WeiBanTags $WeiBanTags){
- // 接收参数
- $id = request('id',0);
- // 查询数据
- $oldData = $Model->where(['id'=>$id])->first();
- // 修改
- if(request()->isMethod('post')){
- // 验证参数
- $request->scene('edit')->validate();
- // 组合数据
- $data['name'] = request('name',0);
- $data['rebate_type'] = request('rebate_type',0);
- $data['std_pay'] = request('std_pay',0);
- $data['rebate'] = request('rebate',0);
- $data['start_time'] = request('start_time','');
- $data['end_time'] = request('end_time','');
- $data['type_id'] = request('type_id',1);
- $cityIds = request('city_ids',[]);
- $tagScope = request('tag_scope',[]);
- $data['city_ids'] = implode(',',$cityIds);
- $data['tag_scope'] = implode(',',$tagScope);
- // 转换时间,默认现在现在生效
- $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
- // 转换时间,默认无结束时间
- $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
- // 赠品ID
- $rebateId = 0;
- // 如果是赠品表的话。处理赠品逻辑
- if( $data['rebate_type'] == 3 ) {
- // 产品编码
- $rebateId = $Product->codeToId($data['rebate']);
- // 赠品ID不存在
- if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
- // 设置为0,避免数据异常
- $data['rebate'] = 0;
- }
- // 验证信息
- 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'=>'活动时间必须大于当前时间']);
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- try {
- // 写入
- $result = $Model->edit($id,$data);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'编辑失败']);
- }
- // 如果是赠品券
- if( $rebateId ) {
- // 查询旧的
- $oldRebateId = $PromoRebate->query()->where([['promo_id','=',$id],['product_id','=',$rebateId]])->value('id');
- // 有旧的修改,无则添加新的
- $result = $oldRebateId ? $PromoRebate->edit($oldRebateId,['product_id'=>$rebateId,'rebate_num'=>1]) : $PromoRebate->add(['promo_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
- // 提示新增失败
- if( !$result ) {
- // 回滚
- DB::rollBack();
- // 提示失败
- 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()]]);
- }
- }
- // 如果是没有数据
- if( !$oldData ) return $this->error('查无数据');
- // 如果是赠品表的话。处理赠品逻辑
- if( $oldData['rebate_type'] == 3 ) {
- // 产品编码
- $rebateId = $PromoRebate->getProductByCouponId($oldData['id']);
- // 产品ID
- $rebateId = isset($rebateId[0]['product_id']) ? $Product->idToCode($rebateId[0]['product_id']) : '';
- // 设置为0,避免数据异常
- $oldData['rebate'] = $rebateId;
- }
- // 城市处理
- $oldData['city_ids'] = explode(',',$oldData['city_ids']);
- $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
- // 有效期,时间戳或者时间为0表示的时间段,否则表示领取后N天
- $oldData['exp_type'] = $oldData['exp_time'] > 1000 ? 2 : 1;
- // 获取列表
- $cityList = $City->getCityList();
- $grantList = $Model->getGrantTypeList();
- // 标签列表
- $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
- // 标签列表
- $tagList = [];
- // 循环数据
- foreach ($tagData as $value) {
- $tagList[$value['group']][] = $value['name'];
- }
- // 分配数据
- $this->assign('cityList',$cityList);
- $this->assign('grantList',$grantList);
- $this->assign('tagList',$tagList);
- $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);
- // 查询数据
- $result = $Model->edit($id,['status'=>$status]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
- }
|