Product.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. 'company_id' => $data['company_id'],
  64. 'insert_time' => time(),
  65. ];
  66. $Product_id = $this->insertGetId($insert_data);
  67. DB::commit();
  68. return $Product_id;
  69. // 成功处理...
  70. } catch (\Exception $e) {
  71. DB::rollBack();
  72. // 错误处理...
  73. return false;
  74. }
  75. }
  76. /**
  77. * 编辑内容
  78. * @author 唐远望
  79. * @version 1.0
  80. * @date 2025-12-30
  81. * @param $data
  82. * @return bool
  83. */
  84. public function editProduct_content($Product, $data)
  85. {
  86. DB::beginTransaction();
  87. try {
  88. $Product->platform = $data['platform'];
  89. $Product->product_name = $data['product_name'];
  90. $Product->product_specs = $data['product_specs'];
  91. $Product->minimum_order_quantity = $data['minimum_order_quantity'];
  92. $Product->sampling_cycle = $data['sampling_cycle'];
  93. $Product->sampling_start_time = $data['sampling_start_time'];
  94. $Product->sampling_end_time = $data['sampling_end_time'];
  95. $Product->update_time = time();
  96. $Product->save();
  97. DB::commit();
  98. return true;
  99. // 成功处理...
  100. } catch (\Exception $e) {
  101. DB::rollBack();
  102. // 错误处理...
  103. return false;
  104. }
  105. }
  106. /**
  107. * 更新数据
  108. * @author 唐远望
  109. * @version 1.0
  110. * @date 2025-12-30
  111. * @param $data
  112. * @return bool
  113. */
  114. public function updateProduct($where, $data)
  115. {
  116. DB::beginTransaction();
  117. try {
  118. $this->editProduct_content($where, $data);
  119. DB::commit();
  120. return true;
  121. // 成功处理...
  122. } catch (\Exception $e) {
  123. DB::rollBack();
  124. // 错误处理...
  125. return false;
  126. }
  127. }
  128. /**
  129. * 修改状态
  130. * @author 唐远望
  131. * @version 1.0
  132. * @date 2025-12-30
  133. * @param $id
  134. * @param $status
  135. * @return bool
  136. */
  137. public function changeStatus($Product, $status)
  138. {
  139. $Product->status = $status;
  140. $Product->update_time = time();
  141. $Product->save();
  142. return true;
  143. }
  144. /**
  145. * 删除数据
  146. * @author 唐远望
  147. * @version 1.0
  148. * @date 2025-12-30
  149. * @param $id
  150. * @return bool
  151. */
  152. public function deleteProduct($where)
  153. {
  154. $Product = $this->where($where)->first();
  155. if (!$Product) {
  156. return false;
  157. }
  158. DB::beginTransaction();
  159. try {
  160. $Product->delete();
  161. DB::commit();
  162. return true;
  163. // 成功处理...
  164. } catch (\Exception $e) {
  165. DB::rollBack();
  166. // 错误处理...
  167. return false;
  168. }
  169. }
  170. }