Product.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php namespace App\Models;
  2. use App\Models\Traits\MultipUpdate;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. /**
  7. * 产品模型
  8. *
  9. */
  10. class Product extends Model
  11. {
  12. use HasFactory,MultipUpdate;
  13. // 与模型关联的表名
  14. protected $table = 'product';
  15. // 是否主动维护时间戳
  16. public $timestamps = false;
  17. // 定义时间戳字段名
  18. // const CREATED_AT = 'insert_time';
  19. // const UPDATED_AT = 'update_time';
  20. /**
  21. * 添加数据
  22. *
  23. */
  24. public function add($data)
  25. {
  26. // 时间
  27. $data['insert_time'] = time();
  28. $data['update_time'] = time();
  29. // 写入数据表
  30. $id = $this->query()->insertGetId($data);
  31. // 返回结果
  32. return $id;
  33. }
  34. /**
  35. * 添加数据
  36. *
  37. */
  38. public function edit($id,$data)
  39. {
  40. // 更新时间
  41. $data['update_time'] = time();
  42. // 写入数据表
  43. $result = $this->query()->where(['id'=>$id])->update($data);
  44. // 返回结果
  45. return $result;
  46. }
  47. /**
  48. * 编码转id
  49. *
  50. * @param string $code 编码
  51. *
  52. */
  53. public function codeToId($code){
  54. return intval(str_ireplace('klcp','',$code));
  55. }
  56. /**
  57. * id转编码
  58. *
  59. * @param int $id 编码
  60. *
  61. */
  62. public function idToCode($id){
  63. return 'klcp'. str_pad($id, 9, '0', STR_PAD_LEFT);;
  64. }
  65. /**
  66. * 描述文件更新
  67. *
  68. */
  69. public function updateDesc($id,$desc)
  70. {
  71. // 路径
  72. $path = public_path('uploads/product/detail/');
  73. // 如果路径不存在
  74. if( !is_dir($path) ) mkdir($path,0755,true);
  75. // 写入文件
  76. $result = file_put_contents($path.$id.'.html',$desc);
  77. // 返回结果
  78. return $result;
  79. }
  80. /**
  81. * 描述文件更新
  82. *
  83. */
  84. public function getDesc($id)
  85. {
  86. // 路径
  87. $path = public_path('uploads/product/detail/');
  88. // 如果路径不存在
  89. if( !is_dir($path) ) mkdir($path,0755,true);
  90. // 不存在文件
  91. if( !file_exists($path.$id.'.html') ) file_put_contents($path.$id.'.html','');
  92. // 写入文件
  93. $result = (string) file_get_contents($path.$id.'.html');
  94. // 返回结果
  95. return $result;
  96. }
  97. /**
  98. * 获取产品列表
  99. *
  100. * @param array $productIds 产品ID列表
  101. *
  102. */
  103. public function pluckList($productIds){
  104. // 查询数据
  105. $data = $this->query()->where([['status','=',0]])->whereIn('id',$productIds)->get(['id','name','price','market_price','stock'])->toArray();
  106. // 以id为主重组
  107. $list = [];
  108. // 循环
  109. foreach ($data as $value) {
  110. $list[$value['id']] = $value;
  111. }
  112. // 返回结果
  113. return $list;
  114. }
  115. /**
  116. * 库存更新
  117. *
  118. * @param int $productId 产品ID
  119. * @param int $incr 增减数量
  120. */
  121. public function updateStock($productId,$incr=1){
  122. // 返回结果
  123. return $this->query()->where([['id','=',$productId]])->update(['stock'=>DB::raw('stock+'.$incr)]);
  124. }
  125. /**
  126. * 获取产品信息
  127. *
  128. * @param int $id 产品ID
  129. * @param string $field 指定字段
  130. *
  131. */
  132. public function getOne($id,$field=''){
  133. // 返回结果
  134. $one = $this->query()->where([['id','=',$id]])->first(['id','name','spec','price','market_price','stock']);
  135. // 获取数据
  136. $one = $one ? $one->toArray() : [];
  137. // 返回值
  138. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  139. }
  140. /**
  141. * 获取多条数据
  142. *
  143. */
  144. public function getListByIds($productIds)
  145. {
  146. // 写入数据表
  147. $data = $this->query()->whereIn('id',$productIds)->where([['status','=',0]])->get(['id','name as product_name','thumb as product_thumb','spec as sku_attr_names','price','stock','status','business_id','quota','min_quota','quota_start','quota_end','tag_scope','tag_exclude'])->toArray();
  148. // 列表
  149. $list = [];
  150. // 循环处理
  151. foreach ($data as $value) {
  152. // 重组数据
  153. $list[$value['id']] = $value;
  154. }
  155. // 返回结果
  156. return $list;
  157. }
  158. }