123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\PromoProduct as Request;
- use App\Models\Product;
- use App\Models\PromoProduct as Model;
- use App\Models\Promo;
- /**
- * 优惠券自动发放规则产品范围
- *
- * @author 刘相欣
- *
- */
- class PromoProduct extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','营销管理');
- $this->assign('breadcrumb2','促销活动商品');
- }
- /**
- * 列表页
- *
- * */
- public function index(Model $Model,Promo $Promo,Product $Product){
- // 接收参数
- $promoCode = request('promo_code','');
- $productCode = request('product_code','');
- $productId = $Product->codeToId($productCode);
- $promoId = $Promo->codeToId($promoCode);
- // 查询条件
- $map = [];
- // 组合条件
- if( $promoId ) $map[] = ['promo_product.promo_id','=',$promoId];
- if( $productId ) $map[] = ['product.id','=',$productId];
- // 查询数据
- $list = $Model->query()
- ->join('product','promo_product.product_id','=','product.id')
- ->join('promo','promo.id','=','promo_product.promo_id')
- ->where($map)
- ->orderByDesc('id')
- ->select(['promo_product.*','product.name as product_name','promo.name as promo_name'])
- ->paginate(config('page_num',10));
- // 循环处理数据
- foreach ($list as $key => $value) {
- $value['promo_code'] = $Promo->idToCode($value['promo_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,Promo $Promo){
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 接收数据
- $productCode = request('product_code','');
- $promoCode = request('promo_code','');
- $data['product_id'] = $Product->codeToId($productCode);
- $data['promo_id'] = $Promo->codeToId($promoCode);
- // 如果操作失败
- if( !$data['product_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的产品编码']);
- if( !$data['promo_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的促销活动编码']);
- // 查询产品ID是否存在
- $oldId = $Model->query()->where([['promo_id','=',$data['promo_id']],['product_id','=',$data['product_id']]])->value('id');
- // 重复提醒
- if( $oldId ) return json_send(['code'=>'error','msg'=>'产品编码已存在']);
- //查询产品店铺是否与活动一致
- $productInfo = $Product->getOne($data['product_id']);
- $promoInfo = $Promo->getOne($data['promo_id']);
- if ($productInfo['business_id'] !== $promoInfo['business_id']) {
- 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,Promo $Promo){
- // 接收参数
- $id = request('id',0);
- // 查询用户
- $oldData = $Model->where(['id'=>$id])->first();
- // 修改
- if(request()->isMethod('post')){
- // 验证参数
- $request->scene('edit')->validate();
- // 接收数据
- $productCode = request('product_code','');
- $promoCode = request('promo_code','');
- $data['product_id'] = $Product->codeToId($productCode);
- $data['promo_id'] = $Promo->codeToId($promoCode);
- // 如果操作失败
- if( !$data['product_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的产品编码']);
- if( !$data['promo_id'] ) return json_send(['code'=>'error','msg'=>'请填写正确的活动编码']);
- // 查询产品ID是否存在
- $oldId = $Model->query()->where([['promo_id','=',$data['promo_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['promo_code'] = $Promo->idToCode($oldData['promo_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'=>'']);
- }
- }
|