CouponProduct.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 优惠券商品范围模型
  6. *
  7. */
  8. class CouponProduct extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'coupon_product';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 添加数据
  20. *
  21. */
  22. public function add($data)
  23. {
  24. // 时间
  25. $data['insert_time'] = time();
  26. $data['update_time'] = time();
  27. // 写入数据表
  28. $id = $this->query()->insertGetId($data);
  29. // 返回结果
  30. return $id;
  31. }
  32. /**
  33. * 添加数据
  34. *
  35. */
  36. public function edit($id,$data)
  37. {
  38. // 更新时间
  39. $data['update_time'] = time();
  40. // 写入数据表
  41. $result = $this->query()->where(['id'=>$id])->update($data);
  42. // 返回结果
  43. return $result;
  44. }
  45. /**
  46. * 查询多个优惠券的商品范围列表
  47. *
  48. * @param array $couponIds 适用产品的列表
  49. *
  50. */
  51. public function getProductList($couponIds)
  52. {
  53. // 写入数据表
  54. $result = $this->query()->whereIn('coupon_id',$couponIds)->get(['coupon_id','product_id'])->toArray();
  55. // 返回结果
  56. return $result;
  57. }
  58. /**
  59. * 查询单个优惠券的商品范围
  60. *
  61. * @param array $couponId 优惠券的
  62. *
  63. */
  64. public function getProducts($couponId)
  65. {
  66. // 写入数据表
  67. $result = $this->query()->where([['coupon_id','=',$couponId]])->pluck('product_id')->toArray();
  68. // 返回结果
  69. return $result;
  70. }
  71. /**
  72. * 查询单个优惠券的商品范围
  73. *
  74. * @param array $couponId 优惠券的
  75. *
  76. */
  77. public function getProductListByCouponIds($couponIds)
  78. {
  79. // 写入数据表
  80. $list = $this->query()
  81. ->join('product','coupon_product.product_id','=','product.id')
  82. ->whereIn('coupon_id',$couponIds)
  83. ->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'])
  84. ->toArray();
  85. // 返回结果
  86. foreach ($list as $key => $value) {
  87. // 缩略图处理
  88. $value['thumb'] = $value['thumb'] ? path_compat($value['thumb']) : '';
  89. $list[$key] = $value;
  90. }
  91. // 返回结果
  92. return $list;
  93. }
  94. }