Product.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. use App\Models\Manager\Collect\ProductKeyword as ProductKeywordModel;
  7. /**
  8. * 采集配置-商品管理
  9. * @author: 唐远望
  10. * @version: 1.0
  11. * @date: 2025-12-30
  12. */
  13. class Product extends Model
  14. {
  15. use HasFactory;
  16. // 与模型关联的表名
  17. protected $table = 'collect_product';
  18. // 是否主动维护时间戳
  19. public $timestamps = false;
  20. // 定义时间戳字段名
  21. // const CREATED_AT = 'insert_time';
  22. // const UPDATED_AT = 'update_time';
  23. /**
  24. * 平台定义
  25. * @author 唐远望
  26. * @version 1.0
  27. * @date 2025-12-31
  28. */
  29. public function platform_index_data()
  30. {
  31. //平台0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药九九,8=药易购,9=药帮忙,10=熊猫药药11=药房网
  32. $platform_data = [
  33. '1' => '淘宝',
  34. '2' => '京东',
  35. '3' => '拼多多',
  36. '4' => '美团',
  37. '5' => '药师帮',
  38. '6' => '1药城',
  39. '7' => '药九九',
  40. '8' => '药易购',
  41. '9' => '药帮忙',
  42. '10' => '熊猫药药',
  43. '11' => '药房网',
  44. ];
  45. return $platform_data;
  46. }
  47. /**
  48. * 添加
  49. * @author 唐远望
  50. * @version 1.0
  51. * @date 2025-12-30
  52. */
  53. public function addProduct_content($data)
  54. {
  55. // 计算品规数量
  56. $product_specs_data = isset($data['product_specs']) && $data['product_specs'] != '' ? explode(',', $data['product_specs']) : '';
  57. //移除数组内空值
  58. $product_specs_data = !empty($product_specs_data) ? array_filter($product_specs_data) : '';
  59. $insert_data = [
  60. 'enable_full_quantity' => isset($data['enable_full_quantity']) ? $data['enable_full_quantity'] : 0,
  61. 'platform' => $data['platform'],
  62. 'product_brand' => $data['product_brand'],
  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. 'round_number' => $data['round_number'],
  69. 'is_multiple_accounts' => isset($data['is_multiple_accounts']) ? $data['is_multiple_accounts'] : 0,
  70. 'sampling_start_time' => $data['sampling_start_time'],
  71. 'sampling_end_time' => $data['sampling_end_time'],
  72. 'company_id' => $data['company_id'],
  73. 'insert_time' => time(),
  74. ];
  75. $Product_id = $this->insertGetId($insert_data);
  76. return $Product_id;
  77. }
  78. /**
  79. * 连表查询商品关键字
  80. * @author 唐远望
  81. * @version 1.0
  82. * @date 2026-03-31
  83. */
  84. public function product_keyword(){
  85. return $this->hasMany(ProductKeywordModel::class,'collect_product_id','id')->orderBy('id','asc');;
  86. }
  87. /**
  88. * 写入数据
  89. * @author 唐远望
  90. * @version 1.0
  91. * @date 2025-12-30
  92. * @param $data
  93. * @return bool
  94. */
  95. public function addProduct($data)
  96. {
  97. DB::beginTransaction();
  98. try {
  99. // 计算品规数量
  100. $product_specs_data = isset($data['product_specs']) && $data['product_specs'] != '' ? explode(',', $data['product_specs']) : '';
  101. //移除数组内空值
  102. $product_specs_data = !empty($product_specs_data) ? array_filter($product_specs_data) : '';
  103. $insert_data = [
  104. 'enable_full_quantity' => isset($data['enable_full_quantity']) ? $data['enable_full_quantity'] : 0,
  105. 'platform' => $data['platform'],
  106. 'product_brand' => $data['product_brand'],
  107. 'product_name' => $data['product_name'],
  108. 'product_specs' => $data['product_specs'],
  109. 'product_specs_number' => $data['product_specs'] ? count($product_specs_data) : 1,
  110. 'minimum_order_quantity' => $data['minimum_order_quantity'],
  111. 'sampling_cycle' => $data['sampling_cycle'],
  112. 'round_number' => $data['round_number'],
  113. 'is_multiple_accounts' => isset($data['is_multiple_accounts']) ? $data['is_multiple_accounts'] : 0,
  114. 'sampling_start_time' => $data['sampling_start_time'],
  115. 'sampling_end_time' => $data['sampling_end_time'],
  116. 'company_id' => $data['company_id'],
  117. 'insert_time' => time(),
  118. ];
  119. $Product_id = $this->insertGetId($insert_data);
  120. // 如果存在采集配置的关键字
  121. $product_keyword = $data['product_keyword'];
  122. if (trim($product_keyword) != '') {
  123. $ProductKeywordModel = new ProductKeywordModel();
  124. $product_keyword_data = explode(',', $product_keyword);
  125. $keyword_data = [];
  126. foreach ($product_keyword_data as $key => $value) {
  127. if (trim($value) == '') continue;
  128. $keyword_data[] = [
  129. 'collect_product_id' => $Product_id,
  130. 'keyword' => $value,
  131. ];
  132. }
  133. $ProductKeywordModel->insert($keyword_data);
  134. }
  135. DB::commit();
  136. return $Product_id;
  137. // 成功处理...
  138. } catch (\Exception $e) {
  139. DB::rollBack();
  140. // 错误处理...
  141. return false;
  142. }
  143. }
  144. /**
  145. * 编辑内容
  146. * @author 唐远望
  147. * @version 1.0
  148. * @date 2025-12-30
  149. * @param $data
  150. * @return bool
  151. */
  152. public function editProduct_content($Product, $data)
  153. {
  154. DB::beginTransaction();
  155. try {
  156. // 计算品规数量
  157. $product_specs_data = isset($data['product_specs']) && $data['product_specs'] != '' ? explode(',', $data['product_specs']) : '';
  158. //移除数组内空值
  159. $product_specs_data = !empty($product_specs_data) ? array_filter($product_specs_data) : '';
  160. $Product->enable_full_quantity = isset($data['enable_full_quantity']) ? $data['enable_full_quantity'] : 0;
  161. $Product->platform = $data['platform'];
  162. $Product->product_brand = $data['product_brand'];
  163. $Product->product_name = $data['product_name'];
  164. $Product->product_specs = $data['product_specs'];
  165. $Product->product_specs_number = $product_specs_data ? count($product_specs_data) : 1;
  166. $Product->minimum_order_quantity = $data['minimum_order_quantity'];
  167. $Product->sampling_cycle = $data['sampling_cycle'];
  168. $Product->round_number = $data['round_number'];
  169. $Product->is_multiple_accounts = isset($data['is_multiple_accounts']) ? $data['is_multiple_accounts'] : 0;
  170. $Product->sampling_start_time = $data['sampling_start_time'];
  171. $Product->sampling_end_time = $data['sampling_end_time'];
  172. $Product->update_time = time();
  173. $Product->save();
  174. $ProductKeywordModel = new ProductKeywordModel();
  175. //查询是否配置了关键字,如果有则删除重新添加
  176. $keyword_count = $ProductKeywordModel->where('collect_product_id', $Product->id)->count();
  177. if ($keyword_count > 0) {
  178. $ProductKeywordModel->where('collect_product_id', $Product->id)->delete();
  179. }
  180. // 如果存在采集配置的关键字
  181. $product_keyword = $data['product_keyword'];
  182. if (trim($product_keyword) != '') {
  183. $product_keyword_data = explode(',', $product_keyword);
  184. $keyword_data = [];
  185. foreach ($product_keyword_data as $key => $value) {
  186. if (trim($value) == '') continue;
  187. $keyword_data[] = [
  188. 'collect_product_id' => $Product->id,
  189. 'keyword' => $value,
  190. ];
  191. }
  192. $ProductKeywordModel->insert($keyword_data);
  193. }
  194. DB::commit();
  195. return true;
  196. // 成功处理...
  197. } catch (\Exception $e) {
  198. DB::rollBack();
  199. // 错误处理...
  200. return false;
  201. }
  202. }
  203. /**
  204. * 更新数据
  205. * @author 唐远望
  206. * @version 1.0
  207. * @date 2025-12-30
  208. * @param $data
  209. * @return bool
  210. */
  211. public function updateProduct($where, $data)
  212. {
  213. DB::beginTransaction();
  214. try {
  215. $this->editProduct_content($where, $data);
  216. DB::commit();
  217. return true;
  218. // 成功处理...
  219. } catch (\Exception $e) {
  220. DB::rollBack();
  221. // 错误处理...
  222. return false;
  223. }
  224. }
  225. /**
  226. * 修改状态
  227. * @author 唐远望
  228. * @version 1.0
  229. * @date 2025-12-30
  230. * @param $id
  231. * @param $status
  232. * @return bool
  233. */
  234. public function changeStatus($Product, $status)
  235. {
  236. $Product->status = $status;
  237. $Product->update_time = time();
  238. $Product->save();
  239. return true;
  240. }
  241. /**
  242. * 删除数据
  243. * @author 唐远望
  244. * @version 1.0
  245. * @date 2025-12-30
  246. * @param $id
  247. * @return bool
  248. */
  249. public function deleteProduct($where)
  250. {
  251. $Product = $this->where($where)->first();
  252. if (!$Product) {
  253. return false;
  254. }
  255. DB::beginTransaction();
  256. try {
  257. $Product->delete();
  258. DB::commit();
  259. return true;
  260. // 成功处理...
  261. } catch (\Exception $e) {
  262. DB::rollBack();
  263. // 错误处理...
  264. return false;
  265. }
  266. }
  267. }