LowPriceGoods.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace App\Models\Manager\Process;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\Manager\Process\LowPriceGoodsMember as LowPriceGoodsMemberModel;
  7. use App\Facades\Servers\Logs\Log;
  8. use App\Models\Manager\Process\Notices as NoticesModel;
  9. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  10. /**
  11. * 违规处理-低价商品模型
  12. * @author: 唐远望
  13. * @version: 1.0
  14. * @date: 2025-12-08
  15. */
  16. class LowPriceGoods extends Model
  17. {
  18. use HasFactory;
  19. // 与模型关联的表名
  20. protected $table = 'process_lowprice_product';
  21. // 是否主动维护时间戳
  22. public $timestamps = false;
  23. // 定义时间戳字段名
  24. // const CREATED_AT = 'insert_time';
  25. // const UPDATED_AT = 'update_time';
  26. /**
  27. * 平台定义
  28. * @author 唐远望
  29. * @version 1.0
  30. * @date 2025-12-31
  31. */
  32. public function platform_data()
  33. {
  34. //平台0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药九九,8=药易购,9=药帮忙,10=熊猫药药11=药房网
  35. $platform_data = [
  36. '全部' => '0',
  37. '淘宝' => '1',
  38. '京东' => '2',
  39. '拼多多' => '3',
  40. '美团' => '4',
  41. '药师帮' => '5',
  42. '1药城' => '6',
  43. '药九九' => '7',
  44. '药易购' => '8',
  45. '药帮忙' => '9',
  46. '熊猫药药' => '10',
  47. '药房网' => '11',
  48. ];
  49. return $platform_data;
  50. }
  51. /**
  52. * 添加
  53. * @author 唐远望
  54. * @version 1.0
  55. * @date 2025-12-08
  56. */
  57. public function addLowPriceGoods_content($data)
  58. {
  59. $insert_data = [
  60. 'company_id' => $data['company_id'],
  61. 'source_id' => $data['source_id'],
  62. 'first_responsible_person' => $data['first_responsible_person'] ? ',' . $data['first_responsible_person'] . ',' : '',
  63. 'responsible_person' => $data['responsible_person'] ? ',' . $data['responsible_person'] . ',' : '',
  64. 'platform' => $data['platform'],
  65. 'company_name' => $data['company_name'],
  66. 'product_brand' => isset($data['product_brand']) ? $data['product_brand'] : '',
  67. 'product_name' => $data['product_name'],
  68. 'product_specs' => $data['product_specs'],
  69. 'suggested_price' => $data['suggested_price'],
  70. 'online_posting_price' => $data['online_posting_price'],
  71. 'online_posting_count' => isset($data['online_posting_count']) && is_numeric($data['online_posting_count']) ? $data['online_posting_count'] : 1,
  72. 'continuous_listing_count' => isset($data['continuous_listing_count']) && is_numeric($data['continuous_listing_count']) ? $data['continuous_listing_count'] : 1,
  73. 'social_credit_code' => $data['social_credit_code'],
  74. 'province_id' => $data['province_id'],
  75. 'province_name' => $data['province_name'],
  76. 'city_id' => $data['city_id'],
  77. 'city_name' => $data['city_name'],
  78. 'area_info' => $data['area_info'],
  79. 'link_url' => $data['link_url'],
  80. 'store_name' => $data['store_name'],
  81. 'anonymous_store_name' => isset($data['anonymous_store_name']) ? $data['anonymous_store_name'] : '',
  82. 'source_responsible_person' => $data['source_responsible_person'] ? ',' . $data['source_responsible_person'] . ',' : '',
  83. 'category_name' => $data['category_name'],
  84. 'company_category_name' => $data['company_category_name'],
  85. 'processing_status' => '1',
  86. 'insert_time' => time(),
  87. 'scrape_date' => isset($data['scrape_date']) ? $data['scrape_date'] : '',
  88. 'collection_time' => isset($data['collection_time']) ? $data['collection_time'] : '',
  89. 'shipment_province_id' => isset($data['shipment_province_id']) ? $data['shipment_province_id'] : '0', // 发货省份id
  90. 'shipment_province_name' => isset($data['shipment_province_name']) ? $data['shipment_province_name'] : '', // 发货省份
  91. 'shipment_city_id' => isset($data['shipment_city_id']) ? $data['shipment_city_id'] : '0', // 发货城市id
  92. 'shipment_city_name' => isset($data['shipment_city_name']) ? $data['shipment_city_name'] : '', // 发货城市
  93. ];
  94. $LowPriceGoods_id = $this->insertGetId($insert_data);
  95. return $LowPriceGoods_id;
  96. }
  97. /**
  98. * 写入数据
  99. * @author 唐远望
  100. * @version 1.0
  101. * @date 2025-12-08
  102. * @param $data
  103. * @return bool
  104. */
  105. public function addLowPriceGoods($data, $is_import = false)
  106. {
  107. if ($is_import == false) {
  108. //去重规则:品牌方公司ID+店铺公司名称+店铺名称+品牌名称+商品名称+规格+盒数(针对采集)
  109. $source_where[] = ['company_id', '=', $data['company_id']]; //品牌方公司ID
  110. $source_where[] = ['company_name', '=', $data['company_name']]; //店铺公司名称
  111. $source_where[] = ['store_name', '=', $data['store_name']]; //店铺名称
  112. $source_where[] = ['product_brand', '=', $data['product_brand']]; //品牌名称
  113. $source_where[] = ['product_name', '=', $data['product_name']]; //商品名称
  114. $source_where[] = ['product_specs', '=', $data['product_specs']]; //规格
  115. $source_id_log = $this->where($source_where)->select(['id', 'online_posting_price'])->first();
  116. if (!empty($source_id_log)) {
  117. if ($source_id_log->online_posting_price > $data['online_posting_price']) {
  118. $this->where('id', $source_id_log->id)->update(['online_posting_price' => $data['online_posting_price'], 'source_id' => $data['source_id']]);
  119. }
  120. return true;
  121. }
  122. }
  123. DB::beginTransaction();
  124. try {
  125. $LowPriceGoodsMemberModel = new LowPriceGoodsMemberModel();
  126. $insert_data = [
  127. 'company_id' => $data['company_id'],
  128. 'source_id' => $data['source_id'],
  129. 'first_responsible_person' => $data['first_responsible_person'] ? ',' . $data['first_responsible_person'] . ',' : '',
  130. 'responsible_person' => $data['responsible_person'] ? ',' . $data['responsible_person'] . ',' : '',
  131. 'platform' => $data['platform'],
  132. 'company_name' => $data['company_name'],
  133. 'product_brand' => isset($data['product_brand']) ? $data['product_brand'] : '',
  134. 'product_name' => $data['product_name'],
  135. 'product_specs' => $data['product_specs'],
  136. 'suggested_price' => $data['suggested_price'],
  137. 'online_posting_price' => $data['online_posting_price'],
  138. 'online_posting_count' => isset($data['online_posting_count']) && is_numeric($data['online_posting_count']) ? $data['online_posting_count'] : 1,
  139. 'continuous_listing_count' => isset($data['continuous_listing_count']) && is_numeric($data['continuous_listing_count']) ? $data['continuous_listing_count'] : 1,
  140. 'social_credit_code' => $data['social_credit_code'],
  141. 'province_id' => $data['province_id'],
  142. 'province_name' => $data['province_name'],
  143. 'city_id' => $data['city_id'],
  144. 'city_name' => $data['city_name'],
  145. 'area_info' => $data['area_info'],
  146. 'link_url' => $data['link_url'],
  147. 'store_name' => $data['store_name'],
  148. 'anonymous_store_name' => isset($data['anonymous_store_name']) ? $data['anonymous_store_name'] : '',
  149. 'source_responsible_person' => $data['source_responsible_person'] ? ',' . $data['source_responsible_person'] . ',' : '',
  150. 'category_name' => $data['category_name'],
  151. 'company_category_name' => $data['company_category_name'],
  152. 'processing_status' => '1',
  153. 'insert_time' => time(),
  154. 'scrape_date' => isset($data['scrape_date']) ? $data['scrape_date'] : '',
  155. 'collection_time' => isset($data['collection_time']) ? $data['collection_time'] : '',
  156. 'shipment_province_id' => isset($data['shipment_province_id']) ? $data['shipment_province_id'] : '0', // 发货省份id
  157. 'shipment_province_name' => isset($data['shipment_province_name']) ? $data['shipment_province_name'] : '', // 发货省份
  158. 'shipment_city_id' => isset($data['shipment_city_id']) ? $data['shipment_city_id'] : '0', // 发货城市id
  159. 'shipment_city_name' => isset($data['shipment_city_name']) ? $data['shipment_city_name'] : '', // 发货城市
  160. ];
  161. $LowPriceGoods_id = $this->insertGetId($insert_data);
  162. $first_responsible_persons = $data['first_responsible_person'] != '' ? explode(',', $data['first_responsible_person']) : [];
  163. $first_responsible_person_data = [];
  164. if (count($first_responsible_persons) > 0) {
  165. foreach ($first_responsible_persons as $key => $employee_id) {
  166. //如果不是数字或者为空,则跳过
  167. if ($employee_id == '' || is_null($employee_id) || !is_numeric($employee_id)) continue;
  168. $first_responsible_person_data[] = [
  169. 'lowprice_product_logid' => $LowPriceGoods_id,
  170. 'employee_id' => $employee_id,
  171. 'duty_type' => 1, //责任类型1=第一责任人,2=责任人,3=溯源责任人
  172. ];
  173. }
  174. }
  175. $LowPriceGoodsMemberModel->insert($first_responsible_person_data);
  176. $responsible_persons = $data['responsible_person'] != '' ? explode(',', $data['responsible_person']) : [];
  177. $responsible_person_data = [];
  178. if (count($responsible_persons) > 0) {
  179. foreach ($responsible_persons as $key => $employee_id) {
  180. //如果不是数字或者为空,则跳过
  181. if ($employee_id == '' || is_null($employee_id) || !is_numeric($employee_id)) continue;
  182. $responsible_person_data[] = [
  183. 'lowprice_product_logid' => $LowPriceGoods_id,
  184. 'employee_id' => $employee_id,
  185. 'duty_type' => 2, //责任类型1=第一责任人,2=责任人,3=溯源责任人
  186. ];
  187. }
  188. }
  189. $LowPriceGoodsMemberModel->insert($responsible_person_data);
  190. $source_responsible_persons = $data['source_responsible_person'] != '' ? explode(',', $data['source_responsible_person']) : [];
  191. $source_responsible_person_data = [];
  192. if (count($source_responsible_persons) > 0) {
  193. foreach ($source_responsible_persons as $key => $employee_id) {
  194. //如果不是数字或者为空,则跳过
  195. if ($employee_id == '' || is_null($employee_id) || !is_numeric($employee_id)) continue;
  196. $source_responsible_person_data[] = [
  197. 'lowprice_product_logid' => $LowPriceGoods_id,
  198. 'employee_id' => $employee_id,
  199. 'duty_type' => 3, //责任类型1=第一责任人,2=责任人,3=溯源责任人
  200. ];
  201. }
  202. }
  203. $LowPriceGoodsMemberModel->insert($source_responsible_person_data);
  204. //添加通知
  205. // $this->addNotices($insert_data, $LowPriceGoods_id);
  206. DB::commit();
  207. return true;
  208. // 成功处理...
  209. } catch (\Exception $e) {
  210. DB::rollBack();
  211. Log::info('job_error', '数据清洗-新增低价商品处理记录失败', ['data' => $data, 'error' => $e->getMessage()]);
  212. // 错误处理...
  213. return false;
  214. }
  215. }
  216. /**
  217. * 处理通知消息
  218. * @author 唐远望
  219. * @version 1.0
  220. * @date 2026-03-21
  221. * @param $data
  222. * @return bool
  223. */
  224. public function addNotices($low_price_data, $data_logid)
  225. {
  226. //添加通知
  227. $NoticesModel = new NoticesModel();
  228. $EmployeeModel = new EmployeeModel();
  229. $notices_data = [];
  230. if ($low_price_data['first_responsible_person'] != '' && $low_price_data['responsible_person'] != '') {
  231. //合并数据并去重
  232. $first_responsible_persons = array_unique(explode(',', $low_price_data['first_responsible_person']));
  233. $responsible_persons = array_unique(explode(',', $low_price_data['responsible_person']));
  234. $all_persons = array_merge($first_responsible_persons, $responsible_persons);
  235. //查询已已经开启通知设置的用户
  236. $employee_ids = $EmployeeModel->whereIn('id', $all_persons)->where('open_notice', 0)->pluck('id')->toArray();
  237. if (count($employee_ids) == 0) return true;
  238. foreach ($employee_ids as $key => $employee_id) {
  239. if ($employee_id == '' || is_null($employee_id)) continue;
  240. $notices_data[] = [
  241. 'company_id' => $low_price_data['company_id'],
  242. 'custom_uid' => $employee_id,
  243. 'title' => '您有一条新的低价商品待处理,请及时查看。',
  244. 'content_type' => '1', //内容类型1=低价挂网2=禁止挂网3=违规店铺
  245. 'data_logid' => $data_logid,
  246. 'insert_time' => time()
  247. ];
  248. }
  249. $NoticesModel->insert($notices_data);
  250. } else if ($low_price_data['first_responsible_person'] != '') {
  251. $first_responsible_persons = array_unique(explode(',', $low_price_data['first_responsible_person']));
  252. //查询已已经开启通知设置的用户
  253. $employee_ids = $EmployeeModel->whereIn('id', $first_responsible_persons)->where('open_notice', 0)->pluck('id')->toArray();
  254. if (count($employee_ids) == 0) return true;
  255. foreach ($employee_ids as $key => $employee_id) {
  256. if ($employee_id == '' || is_null($employee_id)) continue;
  257. $notices_data[] = [
  258. 'company_id' => $low_price_data['company_id'],
  259. 'custom_uid' => $employee_id,
  260. 'title' => '您有一条新的低价商品待处理,请及时查看。',
  261. 'content_type' => '1', //内容类型1=低价挂网2=禁止挂网3=违规店铺
  262. 'data_logid' => $data_logid,
  263. 'insert_time' => time()
  264. ];
  265. }
  266. $NoticesModel->insert($notices_data);
  267. } else if ($low_price_data['responsible_person'] != '') {
  268. $responsible_persons = array_unique(explode(',', $low_price_data['responsible_person']));
  269. //查询已已经开启通知设置的用户
  270. $employee_ids = $EmployeeModel->whereIn('id', $responsible_persons)->where('open_notice', 0)->pluck('id')->toArray();
  271. if (count($employee_ids) == 0) return true;
  272. foreach ($employee_ids as $key => $employee_id) {
  273. if ($employee_id == '' || is_null($employee_id)) continue;
  274. $notices_data[] = [
  275. 'company_id' => $low_price_data['company_id'],
  276. 'custom_uid' => $employee_id,
  277. 'title' => '您有一条新的低价商品待处理,请及时查看。',
  278. 'content_type' => '1', //内容类型1=低价挂网2=禁止挂网3=违规店铺
  279. 'data_logid' => $data_logid,
  280. 'insert_time' => time()
  281. ];
  282. }
  283. $NoticesModel->insert($notices_data);
  284. }
  285. return true;
  286. }
  287. /**
  288. * 编辑内容
  289. * @author 唐远望
  290. * @version 1.0
  291. * @date 2025-12-08
  292. * @param $data
  293. * @return bool
  294. */
  295. public function editLowPriceGoods_content($where, $data)
  296. {
  297. $LowPriceGoods = $this->where($where)->first();
  298. if (!$LowPriceGoods) {
  299. return false;
  300. }
  301. $LowPriceGoods->company_id = $data['company_id'];
  302. $LowPriceGoods->first_responsible_person = $data['first_responsible_person'];
  303. $LowPriceGoods->responsible_person = $data['responsible_person'];
  304. $LowPriceGoods->platform = $data['platform'];
  305. $LowPriceGoods->company_name = $data['company_name'];
  306. $LowPriceGoods->product_name = $data['product_name'];
  307. $LowPriceGoods->product_specs = $data['product_specs'];
  308. $LowPriceGoods->suggested_price = $data['suggested_price'];
  309. $LowPriceGoods->online_posting_price = $data['online_posting_price'];
  310. $LowPriceGoods->online_posting_count = $data['online_posting_count'];
  311. $LowPriceGoods->link_url = $data['link_url'];
  312. $LowPriceGoods->store_name = $data['store_name'];
  313. $LowPriceGoods->source_responsible_person = $data['source_responsible_person'];
  314. $LowPriceGoods->update_time = time();
  315. $LowPriceGoods->save();
  316. return true;
  317. }
  318. /**
  319. * 更新数据
  320. * @author 唐远望
  321. * @version 1.0
  322. * @date 2025-12-08
  323. * @param $data
  324. * @return bool
  325. */
  326. public function updateLowPriceGoods($where, $data)
  327. {
  328. DB::beginTransaction();
  329. try {
  330. $this->editLowPriceGoods_content($where, $data);
  331. DB::commit();
  332. return true;
  333. // 成功处理...
  334. } catch (\Exception $e) {
  335. DB::rollBack();
  336. // 错误处理...
  337. return false;
  338. }
  339. }
  340. /**
  341. * 修改状态
  342. * @author 唐远望
  343. * @version 1.0
  344. * @date 2025-12-08
  345. * @param $id
  346. * @param $status
  347. * @return bool
  348. */
  349. public function changeStatus($where, $status)
  350. {
  351. $LowPriceGoods = $this->where($where)->first();
  352. if (!$LowPriceGoods) {
  353. return false;
  354. }
  355. $LowPriceGoods->status = $status;
  356. $LowPriceGoods->update_time = time();
  357. $LowPriceGoods->save();
  358. return true;
  359. }
  360. /**
  361. * 修改处理状态
  362. * @author 唐远望
  363. * @version 1.0
  364. * @date 2025-12-08
  365. * @param $id
  366. * @param $processing_status
  367. * @return bool
  368. */
  369. public function changeProcessingStatus($where, $processing_status)
  370. {
  371. $LowPriceGoods = $this->where($where)->first();
  372. if (!$LowPriceGoods) {
  373. return false;
  374. }
  375. $LowPriceGoods->processing_status = $processing_status;
  376. $LowPriceGoods->update_time = time();
  377. $LowPriceGoods->save();
  378. return true;
  379. }
  380. /**
  381. * 删除数据
  382. * @author 唐远望
  383. * @version 1.0
  384. * @date 2025-12-08
  385. * @param $id
  386. * @return bool
  387. */
  388. public function deleteLowPriceGoods($where)
  389. {
  390. $LowPriceGoods = $this->where($where)->first();
  391. if (!$LowPriceGoods) {
  392. return false;
  393. }
  394. $LowPriceGoods->delete();
  395. return true;
  396. }
  397. }