123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 优惠券商品范围模型
- *
- */
- class CouponProduct extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'coupon_product';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['id'=>$id])->update($data);
- // 返回结果
- return $result;
- }
- /**
- * 查询多个优惠券的商品范围列表
- *
- * @param array $couponIds 适用产品的列表
- *
- */
- public function getProductList($couponIds)
- {
- // 写入数据表
- $result = $this->query()->whereIn('coupon_id',$couponIds)->get(['coupon_id','product_id'])->toArray();
- // 返回结果
- return $result;
- }
- /**
- * 查询单个优惠券的商品范围
- *
- * @param array $couponId 优惠券的
- *
- */
- public function getProducts($couponId)
- {
- // 写入数据表
- $result = $this->query()->where([['coupon_id','=',$couponId]])->pluck('product_id')->toArray();
- // 返回结果
- return $result;
- }
- /**
- * 查询单个优惠券的商品范围
- *
- * @param array $couponId 优惠券的
- *
- */
- public function getProductListByCouponIds($couponIds)
- {
- // 写入数据表
- $list = $this->query()
- ->join('product','coupon_product.product_id','=','product.id')
- ->whereIn('coupon_id',$couponIds)
- ->get(['product.name as product_name','product.id as product_id','product.thumb as thumb','product.price as price','product.market_price as market_price','product.stock as stock'])
- ->toArray();
- // 返回结果
- foreach ($list as $key => $value) {
- // 缩略图处理
- $value['thumb'] = $value['thumb'] ? path_compat($value['thumb']) : '';
- $list[$key] = $value;
- }
- // 返回结果
- return $list;
- }
- }
|