Product.php 5.8 KB

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