Product.php 5.0 KB

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