Product.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Models\manager\Collect;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. /**
  7. * 采集配置-商品管理
  8. * @author: 唐远望
  9. * @version: 1.0
  10. * @date: 2025-12-30
  11. */
  12. class Product extends Model
  13. {
  14. use HasFactory;
  15. // 与模型关联的表名
  16. protected $table = 'collect_product';
  17. // 是否主动维护时间戳
  18. public $timestamps = false;
  19. // 定义时间戳字段名
  20. // const CREATED_AT = 'insert_time';
  21. // const UPDATED_AT = 'update_time';
  22. /**
  23. * 添加
  24. * @author 唐远望
  25. * @version 1.0
  26. * @date 2025-12-30
  27. */
  28. public function addProduct_content($data)
  29. {
  30. $insert_data = [
  31. 'platform' => $data['platform'],
  32. 'product_name' => $data['product_name'],
  33. 'product_specs' => $data['product_specs'],
  34. 'minimum_order_quantity' => $data['minimum_order_quantity'],
  35. 'sampling_cycle' => $data['sampling_cycle'],
  36. 'sampling_start_time' => $data['sampling_start_time'],
  37. 'sampling_end_time' => $data['sampling_end_time'],
  38. 'insert_time' => time(),
  39. ];
  40. $Product_id = $this->insertGetId($insert_data);
  41. return $Product_id;
  42. }
  43. /**
  44. * 写入数据
  45. * @author 唐远望
  46. * @version 1.0
  47. * @date 2025-12-30
  48. * @param $data
  49. * @return bool
  50. */
  51. public function addProduct($data)
  52. {
  53. DB::beginTransaction();
  54. try {
  55. $insert_data = [
  56. 'platform' => $data['platform'],
  57. 'product_name' => $data['product_name'],
  58. 'product_specs' => $data['product_specs'],
  59. 'minimum_order_quantity' => $data['minimum_order_quantity'],
  60. 'sampling_cycle' => $data['sampling_cycle'],
  61. 'sampling_start_time' => $data['sampling_start_time'],
  62. 'sampling_end_time' => $data['sampling_end_time'],
  63. 'insert_time' => time(),
  64. ];
  65. $Product_id = $this->insertGetId($insert_data);
  66. DB::commit();
  67. return $Product_id;
  68. // 成功处理...
  69. } catch (\Exception $e) {
  70. DB::rollBack();
  71. // 错误处理...
  72. return false;
  73. }
  74. }
  75. /**
  76. * 编辑内容
  77. * @author 唐远望
  78. * @version 1.0
  79. * @date 2025-12-30
  80. * @param $data
  81. * @return bool
  82. */
  83. public function editProduct_content($Product, $data)
  84. {
  85. DB::beginTransaction();
  86. try {
  87. $Product->platform = $data['platform'];
  88. $Product->product_name = $data['product_name'];
  89. $Product->product_specs = $data['product_specs'];
  90. $Product->minimum_order_quantity = $data['minimum_order_quantity'];
  91. $Product->sampling_cycle = $data['sampling_cycle'];
  92. $Product->sampling_start_time = $data['sampling_start_time'];
  93. $Product->sampling_end_time = $data['sampling_end_time'];
  94. $Product->update_time = time();
  95. $Product->save();
  96. DB::commit();
  97. return true;
  98. // 成功处理...
  99. } catch (\Exception $e) {
  100. DB::rollBack();
  101. // 错误处理...
  102. return false;
  103. }
  104. }
  105. /**
  106. * 更新数据
  107. * @author 唐远望
  108. * @version 1.0
  109. * @date 2025-12-30
  110. * @param $data
  111. * @return bool
  112. */
  113. public function updateProduct($where, $data)
  114. {
  115. DB::beginTransaction();
  116. try {
  117. $this->editProduct_content($where, $data);
  118. DB::commit();
  119. return true;
  120. // 成功处理...
  121. } catch (\Exception $e) {
  122. DB::rollBack();
  123. // 错误处理...
  124. return false;
  125. }
  126. }
  127. /**
  128. * 修改状态
  129. * @author 唐远望
  130. * @version 1.0
  131. * @date 2025-12-30
  132. * @param $id
  133. * @param $status
  134. * @return bool
  135. */
  136. public function changeStatus($Product, $status)
  137. {
  138. $Product->status = $status;
  139. $Product->update_time = time();
  140. $Product->save();
  141. return true;
  142. }
  143. /**
  144. * 删除数据
  145. * @author 唐远望
  146. * @version 1.0
  147. * @date 2025-12-30
  148. * @param $id
  149. * @return bool
  150. */
  151. public function deleteProduct($where)
  152. {
  153. $Product = $this->where($where)->first();
  154. if (!$Product) {
  155. return false;
  156. }
  157. DB::beginTransaction();
  158. try {
  159. $Product->delete();
  160. DB::commit();
  161. return true;
  162. // 成功处理...
  163. } catch (\Exception $e) {
  164. DB::rollBack();
  165. // 错误处理...
  166. return false;
  167. }
  168. }
  169. }