LowPriceGoods.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. 'ultra_low_price' => $data['ultra_low_price'],
  71. 'is_ultra_low_price' => $data['is_ultra_low_price'],
  72. 'online_posting_price' => $data['online_posting_price'],
  73. 'online_posting_count' => isset($data['online_posting_count']) && is_numeric($data['online_posting_count']) ? $data['online_posting_count'] : 1,
  74. 'continuous_listing_count' => isset($data['continuous_listing_count']) && is_numeric($data['continuous_listing_count']) ? $data['continuous_listing_count'] : 1,
  75. 'social_credit_code' => $data['social_credit_code'],
  76. 'province_id' => $data['province_id'],
  77. 'province_name' => $data['province_name'],
  78. 'city_id' => $data['city_id'],
  79. 'city_name' => $data['city_name'],
  80. 'area_info' => $data['area_info'],
  81. 'link_url' => $data['link_url'],
  82. 'store_name' => $data['store_name'],
  83. 'anonymous_store_name' => isset($data['anonymous_store_name']) ? $data['anonymous_store_name'] : '',
  84. 'source_responsible_person' => $data['source_responsible_person'] ? ',' . $data['source_responsible_person'] . ',' : '',
  85. 'category_name' => $data['category_name'],
  86. 'company_category_name' => $data['company_category_name'],
  87. 'processing_status' => '1',
  88. 'insert_time' => time(),
  89. 'scrape_date' => isset($data['scrape_date']) ? $data['scrape_date'] : '',
  90. 'collection_time' => isset($data['collection_time']) ? $data['collection_time'] : '',
  91. 'shipment_province_id' => isset($data['shipment_province_id']) ? $data['shipment_province_id'] : '0', // 发货省份id
  92. 'shipment_province_name' => isset($data['shipment_province_name']) ? $data['shipment_province_name'] : '', // 发货省份
  93. 'shipment_city_id' => isset($data['shipment_city_id']) ? $data['shipment_city_id'] : '0', // 发货城市id
  94. 'shipment_city_name' => isset($data['shipment_city_name']) ? $data['shipment_city_name'] : '', // 发货城市
  95. 'merge_city_id' => $data['merge_city_id'],
  96. 'merge_province_id' => $data['merge_province_id'],
  97. 'merge_city_name' => $data['merge_city_name'],
  98. 'merge_province_name' => $data['merge_province_name'],
  99. 'snapshot_url' => isset($data['snapshot_url']) ? $data['snapshot_url'] : '', // 商品快照地址
  100. 'specify_responsible_person' => isset($data['specify_responsible_person']) ? $data['specify_responsible_person'] : '1', // 指派责任人 0=开启 1=关闭
  101. ];
  102. $LowPriceGoods_id = $this->insertGetId($insert_data);
  103. return $LowPriceGoods_id;
  104. }
  105. /**
  106. * 写入数据
  107. * @author 唐远望
  108. * @version 1.0
  109. * @date 2025-12-08
  110. * @param $data
  111. * @return bool
  112. */
  113. public function addLowPriceGoods($data, $is_import = false)
  114. {
  115. if ($is_import == false) {
  116. //检查是否重复清洗
  117. $source_data_log = $this->where([['company_id','=', $data['company_id']],['source_id','=', $data['source_id']]])->count();
  118. if(!empty($source_data_log) && $source_data_log > 0) return true;
  119. // 如果是美团跟拼多多链接为动态
  120. if (in_array($data['platform'], ['3', '4'])) {
  121. //去重规则:品牌方公司ID+平台ID+公司名称+店铺名称+品牌名称+商品名称+规格+采集日期(针对采集)
  122. $source_where[] = ['company_id', '=', $data['company_id']]; //品牌方公司ID
  123. $source_where[] = ['platform', '=', $data['platform']]; //平台ID
  124. $source_where[] = ['company_name', '=', $data['company_name']]; //公司名称
  125. $source_where[] = ['store_name', '=', $data['store_name']]; //店铺名称
  126. $source_where[] = ['product_brand', '=', $data['product_brand']]; //品牌名称
  127. $source_where[] = ['product_name', '=', $data['product_name']]; //商品名称
  128. $source_where[] = ['product_specs', '=', $data['product_specs']]; //规格
  129. $source_where[] = ['scrape_date', '=', $data['scrape_date']]; //采集日期
  130. } else {
  131. $source_where[] = ['link_url', '=', $data['link_url']]; //链接地址
  132. $source_where[] = ['scrape_date', '=', $data['scrape_date']]; //链接地址
  133. }
  134. $source_id_log = $this->where($source_where)->select(['id', 'online_posting_price'])->first();
  135. if (!empty($source_id_log)) {
  136. if ($source_id_log->online_posting_price > $data['online_posting_price']) {
  137. $is_ultra_low_price = 0; //是否超低价0=否1=是
  138. if ($data['ultra_low_price'] > 0 && $data['online_posting_price'] <= $data['ultra_low_price']) {
  139. $is_ultra_low_price = 1;
  140. }
  141. $snapshot_url = isset($data['snapshot_url']) ? $data['snapshot_url'] : ''; // 商品快照地址
  142. $this->where('id', $source_id_log->id)->update([
  143. 'online_posting_price' => $data['online_posting_price'],
  144. 'is_ultra_low_price' => $is_ultra_low_price,
  145. 'source_id' => $data['source_id'],
  146. 'snapshot_url' => $snapshot_url,
  147. 'update_time' => time()
  148. ]);
  149. }
  150. return true;
  151. }
  152. }
  153. DB::beginTransaction();
  154. try {
  155. //执行挂网数据统计
  156. $data = $this->handleCountData($data);
  157. $insert_data = [
  158. 'company_id' => $data['company_id'],
  159. 'source_id' => $data['source_id'],
  160. 'first_responsible_person' => $data['first_responsible_person'] ? ',' . $data['first_responsible_person'] . ',' : '',
  161. 'responsible_person' => $data['responsible_person'] ? ',' . $data['responsible_person'] . ',' : '',
  162. 'platform' => $data['platform'],
  163. 'company_name' => $data['company_name'],
  164. 'product_brand' => isset($data['product_brand']) ? $data['product_brand'] : '',
  165. 'product_name' => $data['product_name'],
  166. 'product_specs' => $data['product_specs'],
  167. 'suggested_price' => $data['suggested_price'],
  168. 'ultra_low_price' => $data['ultra_low_price'],
  169. 'is_ultra_low_price' => $data['is_ultra_low_price'],
  170. 'online_posting_price' => $data['online_posting_price'],
  171. 'online_posting_count' => isset($data['online_posting_count']) && is_numeric($data['online_posting_count']) ? $data['online_posting_count'] : 1,
  172. 'continuous_listing_count' => isset($data['continuous_listing_count']) && is_numeric($data['continuous_listing_count']) ? $data['continuous_listing_count'] : 1,
  173. 'social_credit_code' => $data['social_credit_code'],
  174. 'province_id' => $data['province_id'],
  175. 'province_name' => $data['province_name'],
  176. 'city_id' => $data['city_id'],
  177. 'city_name' => $data['city_name'],
  178. 'area_info' => $data['area_info'],
  179. 'link_url' => $data['link_url'],
  180. 'store_name' => $data['store_name'],
  181. 'anonymous_store_name' => isset($data['anonymous_store_name']) ? $data['anonymous_store_name'] : '',
  182. 'source_responsible_person' => $data['source_responsible_person'] ? ',' . $data['source_responsible_person'] . ',' : '',
  183. 'category_name' => $data['category_name'],
  184. 'company_category_name' => $data['company_category_name'],
  185. 'processing_status' => '1',
  186. 'insert_time' => time(),
  187. 'scrape_date' => isset($data['scrape_date']) ? $data['scrape_date'] : '',
  188. 'collection_time' => isset($data['collection_time']) ? $data['collection_time'] : '',
  189. 'shipment_province_id' => isset($data['shipment_province_id']) ? $data['shipment_province_id'] : '0', // 发货省份id
  190. 'shipment_province_name' => isset($data['shipment_province_name']) ? $data['shipment_province_name'] : '', // 发货省份
  191. 'shipment_city_id' => isset($data['shipment_city_id']) ? $data['shipment_city_id'] : '0', // 发货城市id
  192. 'shipment_city_name' => isset($data['shipment_city_name']) ? $data['shipment_city_name'] : '', // 发货城市
  193. 'merge_city_id' => $data['merge_city_id'],
  194. 'merge_province_id' => $data['merge_province_id'],
  195. 'merge_city_name' => $data['merge_city_name'],
  196. 'merge_province_name' => $data['merge_province_name'],
  197. 'snapshot_url' => isset($data['snapshot_url']) ? $data['snapshot_url'] : '', // 商品快照地址
  198. 'specify_responsible_person' => isset($data['specify_responsible_person']) ? $data['specify_responsible_person'] : '1', // 指派责任人 0=开启 1=关闭
  199. ];
  200. $LowPriceGoods_id = $this->insertGetId($insert_data);
  201. $first_responsible_persons = $data['first_responsible_person'] != '' ? explode(',', $data['first_responsible_person']) : [];
  202. $first_responsible_person_data = [];
  203. if (count($first_responsible_persons) > 0) {
  204. //去除重复的责任人
  205. $first_responsible_persons = array_unique($first_responsible_persons);
  206. //移除数组内的空值
  207. $first_responsible_persons = array_filter($first_responsible_persons);
  208. $EmployeeModel = new EmployeeModel();
  209. $employee_user_list = $EmployeeModel->whereIn('id', $first_responsible_persons)->select(['id', 'name'])->get()->toarray();
  210. $employee_user_list_name = [];
  211. if (!empty($employee_user_list)) {
  212. foreach ($employee_user_list as $key => $employee_user_info) {
  213. $employee_id = $employee_user_info['id'];
  214. $employee_user_list_name[$employee_id] = $employee_user_info['name'];
  215. }
  216. }
  217. foreach ($first_responsible_persons as $key => $employee_id) {
  218. //如果不是数字或者为空,则跳过
  219. if ($employee_id == '' || is_null($employee_id) || !is_numeric($employee_id)) continue;
  220. $first_responsible_person_data[] = [
  221. 'lowprice_product_logid' => $LowPriceGoods_id,
  222. 'employee_id' => $employee_id,
  223. 'employee_name' => isset($employee_user_list_name[$employee_id]) ? $employee_user_list_name[$employee_id] : '',
  224. 'duty_type' => 1, //责任类型1=第一责任人,2=责任人,3=溯源责任人
  225. ];
  226. }
  227. }
  228. $LowPriceGoodsMemberModel = new LowPriceGoodsMemberModel();
  229. $LowPriceGoodsMemberModel->insert($first_responsible_person_data);
  230. $responsible_persons = $data['responsible_person'] != '' ? explode(',', $data['responsible_person']) : [];
  231. $responsible_person_data = [];
  232. if (count($responsible_persons) > 0) {
  233. //去除重复的责任人
  234. $responsible_persons = array_unique($responsible_persons);
  235. //移除数组内的空值
  236. $responsible_persons = array_filter($responsible_persons);
  237. $EmployeeModel = new EmployeeModel();
  238. $employee_user_list = $EmployeeModel->whereIn('id', $responsible_persons)->select(['id', 'name'])->get()->toarray();
  239. $employee_user_list_name = [];
  240. if (!empty($employee_user_list)) {
  241. foreach ($employee_user_list as $key => $employee_user_info) {
  242. $employee_id = $employee_user_info['id'];
  243. $employee_user_list_name[$employee_id] = $employee_user_info['name'];
  244. }
  245. }
  246. foreach ($responsible_persons as $key => $employee_id) {
  247. //如果不是数字或者为空,则跳过
  248. if ($employee_id == '' || is_null($employee_id) || !is_numeric($employee_id)) continue;
  249. $responsible_person_data[] = [
  250. 'lowprice_product_logid' => $LowPriceGoods_id,
  251. 'employee_id' => $employee_id,
  252. 'employee_name' => isset($employee_user_list_name[$employee_id]) ? $employee_user_list_name[$employee_id] : '',
  253. 'duty_type' => 2, //责任类型1=第一责任人,2=责任人,3=溯源责任人
  254. ];
  255. }
  256. }
  257. $LowPriceGoodsMemberModel = new LowPriceGoodsMemberModel();
  258. $LowPriceGoodsMemberModel->insert($responsible_person_data);
  259. $source_responsible_persons = $data['source_responsible_person'] != '' ? explode(',', $data['source_responsible_person']) : [];
  260. $source_responsible_person_data = [];
  261. if (count($source_responsible_persons) > 0) {
  262. //去除重复的责任人
  263. $source_responsible_persons = array_unique($source_responsible_persons);
  264. //移除数组内的空值
  265. $source_responsible_persons = array_filter($source_responsible_persons);
  266. $EmployeeModel = new EmployeeModel();
  267. $employee_user_list = $EmployeeModel->whereIn('id', $source_responsible_persons)->select(['id', 'name'])->get()->toarray();
  268. $employee_user_list_name = [];
  269. if (!empty($employee_user_list)) {
  270. foreach ($employee_user_list as $key => $employee_user_info) {
  271. $employee_id = $employee_user_info['id'];
  272. $employee_user_list_name[$employee_id] = $employee_user_info['name'];
  273. }
  274. }
  275. foreach ($source_responsible_persons as $key => $employee_id) {
  276. //如果不是数字或者为空,则跳过
  277. if ($employee_id == '' || is_null($employee_id) || !is_numeric($employee_id)) continue;
  278. $source_responsible_person_data[] = [
  279. 'lowprice_product_logid' => $LowPriceGoods_id,
  280. 'employee_id' => $employee_id,
  281. 'employee_name' => isset($employee_user_list_name[$employee_id]) ? $employee_user_list_name[$employee_id] : '',
  282. 'duty_type' => 3, //责任类型1=第一责任人,2=责任人,3=溯源责任人
  283. ];
  284. }
  285. }
  286. $LowPriceGoodsMemberModel = new LowPriceGoodsMemberModel();
  287. $LowPriceGoodsMemberModel->insert($source_responsible_person_data);
  288. //添加通知
  289. // $this->addNotices($insert_data, $LowPriceGoods_id);
  290. DB::commit();
  291. return true;
  292. // 成功处理...
  293. } catch (\Exception $e) {
  294. DB::rollBack();
  295. Log::info('job_error', '数据清洗-新增低价商品处理记录失败', ['data' => $data, 'error' => $e->getMessage()]);
  296. // 错误处理...
  297. return false;
  298. }
  299. }
  300. function getLastSamplingInfo($currentWeek, $samplingCycle)
  301. {
  302. // 获取所有小于当前星期的配置
  303. $previousWeeks = array_filter($samplingCycle, function ($week) use ($currentWeek) {
  304. return $week < $currentWeek;
  305. });
  306. if (!empty($previousWeeks)) {
  307. // 存在小于当前星期的配置,取最大值
  308. $lastWeek = max($previousWeeks);
  309. $daysToSubtract = $currentWeek - $lastWeek;
  310. } else {
  311. // 不存在(当前是最小的),取最大的配置星期(跨周)
  312. $lastWeek = max($samplingCycle);
  313. $daysToSubtract = $currentWeek + (7 - $lastWeek);
  314. }
  315. return [
  316. 'last_week' => $lastWeek,
  317. 'days_to_subtract' => $daysToSubtract
  318. ];
  319. }
  320. /**
  321. * 处理连续挂网&累计挂网数据统计
  322. * @author 唐远望
  323. * @version 1.0
  324. * @date 2026-05-07
  325. *
  326. */
  327. public function handleCountData($product_data_info)
  328. {
  329. //查询当前商品上次连续挂网次数
  330. if (in_array($product_data_info['platform'], ['3', '4'])) {
  331. $source_last_time_where[] = ['company_id', '=', $product_data_info['company_id']]; //品牌方公司ID
  332. $source_last_time_where[] = ['platform', '=', $product_data_info['platform']]; //挂网平台ID
  333. $source_last_time_where[] = ['company_name', '=', $product_data_info['company_name']]; //店铺公司名称
  334. $source_last_time_where[] = ['store_name', '=', $product_data_info['store_name']]; //店铺名称
  335. $source_last_time_where[] = ['product_brand', '=', $product_data_info['product_brand']]; //品牌名称
  336. $source_last_time_where[] = ['product_name', '=', $product_data_info['product_name']]; //商品名称
  337. $source_last_time_where[] = ['product_specs', '=', $product_data_info['product_specs']]; //规格
  338. $source_last_time_where[] = ['scrape_date', '<', $product_data_info['scrape_date']]; //采集日期
  339. } else {
  340. $source_last_time_where[] = ['company_id', '=', $product_data_info['company_id']]; //品牌方公司ID
  341. $source_last_time_where[] = ['link_url', '=', $product_data_info['link_url']]; //链接地址
  342. $source_last_time_where[] = ['scrape_date', '<', $product_data_info['scrape_date']]; //采集日期
  343. }
  344. $online_posting_count = $this->where($source_last_time_where)->count();
  345. $product_data_info['online_posting_count'] = $online_posting_count + 1; //累计挂网次数+1
  346. //连续挂网默认值为1
  347. $product_data_info['continuous_listing_count'] = 1;
  348. $collect_config_info = isset($product_data_info['collect_config_info']) ? $product_data_info['collect_config_info'] : ''; //采集配置信息
  349. if (trim($collect_config_info) == '') return $product_data_info;
  350. $collect_config_info = json_decode($collect_config_info, true);
  351. //采集周期
  352. $sampling_cycle = isset($collect_config_info['sampling_cycle']) ? explode(',', $collect_config_info['sampling_cycle']) : '';
  353. //采集开始时间
  354. $sampling_start_time = isset($collect_config_info['sampling_start_time']) ? $collect_config_info['sampling_start_time'] : '';
  355. //采集结束时间
  356. $sampling_end_time = isset($collect_config_info['sampling_end_time']) ? $collect_config_info['sampling_end_time'] : '';
  357. if (!is_array($sampling_cycle)) return $product_data_info;
  358. $current_time = $product_data_info['collection_time'];
  359. //如果当前时间在采集开始时间结束时间范围内,则进行连续挂网和累计挂网统计
  360. if ($sampling_start_time != '' && $sampling_end_time != '' && $current_time >= $sampling_start_time && $current_time <= $sampling_end_time) {
  361. // 获取采集时间是星期几
  362. $now_week = date('w', $current_time);
  363. if ($now_week == 0) {
  364. $now_week = 7;
  365. }
  366. if (!in_array($now_week, $sampling_cycle)) {
  367. return $product_data_info;
  368. }
  369. //排序$sampling_cycle从小到大
  370. sort($sampling_cycle);
  371. // 使用
  372. $result = $this->getLastSamplingInfo($now_week, $sampling_cycle);
  373. $days_to_subtract = isset($result['days_to_subtract']) ? $result['days_to_subtract'] : 0;
  374. // echo "往前推 {$result['days_to_subtract']} 天";
  375. //查询当前商品是否在上一个周期采集中过
  376. $up_scrape_date_time = $current_time - $days_to_subtract * 24 * 3600; //查询上周这个时间点
  377. $up_scrape_date = date('Y-m-d', $up_scrape_date_time);
  378. if (in_array($product_data_info['platform'], ['3', '4'])) {
  379. $source_last_week_where[] = ['company_id', '=', $product_data_info['company_id']]; //品牌方公司ID
  380. $source_last_week_where[] = ['platform', '=', $product_data_info['platform']]; //挂网平台ID
  381. $source_last_week_where[] = ['company_name', '=', $product_data_info['company_name']]; //店铺公司名称
  382. $source_last_week_where[] = ['store_name', '=', $product_data_info['store_name']]; //店铺名称
  383. $source_last_week_where[] = ['product_brand', '=', $product_data_info['product_brand']]; //品牌名称
  384. $source_last_week_where[] = ['product_name', '=', $product_data_info['product_name']]; //商品名称
  385. $source_last_week_where[] = ['product_specs', '=', $product_data_info['product_specs']]; //规格
  386. $source_last_week_where[] = ['scrape_date', '=', $up_scrape_date]; //上个周期采集日期
  387. } else {
  388. $source_last_week_where[] = ['link_url', '=', $product_data_info['link_url']]; //链接地址
  389. $source_last_week_where[] = ['scrape_date', '=', $up_scrape_date]; //上个周期采集日期
  390. }
  391. $source_last_week_log = $this->where($source_last_week_where)->count();
  392. if ($source_last_week_log > 0) {
  393. $product_data_info['continuous_listing_count'] = $product_data_info['continuous_listing_count'] + 1; //连续挂网次数+1
  394. }
  395. } else if ($sampling_start_time != '' && $sampling_end_time == '0' && $current_time >= $sampling_start_time) {
  396. //如果当前时间在采集开始时间结束时间未设置,则进行连续挂网和累计挂网统计
  397. // 获取采集时间是星期几
  398. $collection_time = strtotime($product_data_info['collection_time']);
  399. $now_week = date('w', $collection_time);
  400. if ($now_week == 0) {
  401. $now_week = 7;
  402. }
  403. if (!in_array($now_week, $sampling_cycle)) {
  404. return $product_data_info;
  405. }
  406. //排序$sampling_cycle从小到大
  407. sort($sampling_cycle);
  408. // 使用
  409. $result = $this->getLastSamplingInfo($now_week, $sampling_cycle);
  410. $days_to_subtract = isset($result['days_to_subtract']) ? $result['days_to_subtract'] : 0;
  411. $up_scrape_date_time = $collection_time - $days_to_subtract * 24 * 3600; //查询上周这个时间点
  412. //查询当前商品上次采集记录
  413. $up_scrape_date = date('Y-m-d', $up_scrape_date_time);
  414. if (in_array($product_data_info['platform'], ['3', '4'])) {
  415. $source_last_week_where[] = ['company_id', '=', $product_data_info['company_id']]; //品牌方公司ID
  416. $source_last_week_where[] = ['platform', '=', $product_data_info['platform']]; //挂网平台ID
  417. $source_last_week_where[] = ['company_name', '=', $product_data_info['company_name']]; //店铺公司名称
  418. $source_last_week_where[] = ['store_name', '=', $product_data_info['store_name']]; //店铺名称
  419. $source_last_week_where[] = ['product_brand', '=', $product_data_info['product_brand']]; //品牌名称
  420. $source_last_week_where[] = ['product_name', '=', $product_data_info['product_name']]; //商品名称
  421. $source_last_week_where[] = ['product_specs', '=', $product_data_info['product_specs']]; //规格
  422. $source_last_week_where[] = ['scrape_date', '=', $up_scrape_date]; //上个周期采集日期
  423. }else{
  424. $source_last_week_where[] = ['link_url', '=', $product_data_info['link_url']]; //链接地址
  425. $source_last_week_where[] = ['scrape_date', '=', $up_scrape_date]; //上个周期采集日期
  426. }
  427. $source_last_week_log = $this->where($source_last_week_where)->count();
  428. if ($source_last_week_log > 0) {
  429. $product_data_info['continuous_listing_count'] = $product_data_info['continuous_listing_count'] + 1; //连续挂网次数+1
  430. }
  431. }
  432. return $product_data_info;
  433. }
  434. /**
  435. * 处理通知消息
  436. * @author 唐远望
  437. * @version 1.0
  438. * @date 2026-03-21
  439. * @param $data
  440. * @return bool
  441. */
  442. public function addNotices($low_price_data, $data_logid)
  443. {
  444. //添加通知
  445. $NoticesModel = new NoticesModel();
  446. $EmployeeModel = new EmployeeModel();
  447. $notices_data = [];
  448. if ($low_price_data['first_responsible_person'] != '' && $low_price_data['responsible_person'] != '') {
  449. //合并数据并去重
  450. $first_responsible_persons = array_unique(explode(',', $low_price_data['first_responsible_person']));
  451. $responsible_persons = array_unique(explode(',', $low_price_data['responsible_person']));
  452. $all_persons = array_merge($first_responsible_persons, $responsible_persons);
  453. //查询已已经开启通知设置的用户
  454. $employee_ids = $EmployeeModel->whereIn('id', $all_persons)->where('open_notice', 0)->pluck('id')->toArray();
  455. if (count($employee_ids) == 0) return true;
  456. foreach ($employee_ids as $key => $employee_id) {
  457. if ($employee_id == '' || is_null($employee_id)) continue;
  458. $notices_data[] = [
  459. 'company_id' => $low_price_data['company_id'],
  460. 'custom_uid' => $employee_id,
  461. 'title' => '您有一条新的低价商品待处理,请及时查看。',
  462. 'content_type' => '1', //内容类型1=低价挂网2=禁止挂网3=违规店铺
  463. 'data_logid' => $data_logid,
  464. 'insert_time' => time()
  465. ];
  466. }
  467. $NoticesModel->insert($notices_data);
  468. } else if ($low_price_data['first_responsible_person'] != '') {
  469. $first_responsible_persons = array_unique(explode(',', $low_price_data['first_responsible_person']));
  470. //查询已已经开启通知设置的用户
  471. $employee_ids = $EmployeeModel->whereIn('id', $first_responsible_persons)->where('open_notice', 0)->pluck('id')->toArray();
  472. if (count($employee_ids) == 0) return true;
  473. foreach ($employee_ids as $key => $employee_id) {
  474. if ($employee_id == '' || is_null($employee_id)) continue;
  475. $notices_data[] = [
  476. 'company_id' => $low_price_data['company_id'],
  477. 'custom_uid' => $employee_id,
  478. 'title' => '您有一条新的低价商品待处理,请及时查看。',
  479. 'content_type' => '1', //内容类型1=低价挂网2=禁止挂网3=违规店铺
  480. 'data_logid' => $data_logid,
  481. 'insert_time' => time()
  482. ];
  483. }
  484. $NoticesModel->insert($notices_data);
  485. } else if ($low_price_data['responsible_person'] != '') {
  486. $responsible_persons = array_unique(explode(',', $low_price_data['responsible_person']));
  487. //查询已已经开启通知设置的用户
  488. $employee_ids = $EmployeeModel->whereIn('id', $responsible_persons)->where('open_notice', 0)->pluck('id')->toArray();
  489. if (count($employee_ids) == 0) return true;
  490. foreach ($employee_ids as $key => $employee_id) {
  491. if ($employee_id == '' || is_null($employee_id)) continue;
  492. $notices_data[] = [
  493. 'company_id' => $low_price_data['company_id'],
  494. 'custom_uid' => $employee_id,
  495. 'title' => '您有一条新的低价商品待处理,请及时查看。',
  496. 'content_type' => '1', //内容类型1=低价挂网2=禁止挂网3=违规店铺
  497. 'data_logid' => $data_logid,
  498. 'insert_time' => time()
  499. ];
  500. }
  501. $NoticesModel->insert($notices_data);
  502. }
  503. return true;
  504. }
  505. /**
  506. * 编辑内容
  507. * @author 唐远望
  508. * @version 1.0
  509. * @date 2025-12-08
  510. * @param $data
  511. * @return bool
  512. */
  513. public function editLowPriceGoods_content($where, $data)
  514. {
  515. $LowPriceGoods = $this->where($where)->first();
  516. if (!$LowPriceGoods) {
  517. return false;
  518. }
  519. $LowPriceGoods->company_id = $data['company_id'];
  520. $LowPriceGoods->first_responsible_person = $data['first_responsible_person'];
  521. $LowPriceGoods->responsible_person = $data['responsible_person'];
  522. $LowPriceGoods->platform = $data['platform'];
  523. $LowPriceGoods->company_name = $data['company_name'];
  524. $LowPriceGoods->product_name = $data['product_name'];
  525. $LowPriceGoods->product_specs = $data['product_specs'];
  526. $LowPriceGoods->suggested_price = $data['suggested_price'];
  527. $LowPriceGoods->online_posting_price = $data['online_posting_price'];
  528. $LowPriceGoods->online_posting_count = $data['online_posting_count'];
  529. $LowPriceGoods->link_url = $data['link_url'];
  530. $LowPriceGoods->store_name = $data['store_name'];
  531. $LowPriceGoods->source_responsible_person = $data['source_responsible_person'];
  532. $LowPriceGoods->update_time = time();
  533. $LowPriceGoods->save();
  534. return true;
  535. }
  536. /**
  537. * 更新数据
  538. * @author 唐远望
  539. * @version 1.0
  540. * @date 2025-12-08
  541. * @param $data
  542. * @return bool
  543. */
  544. public function updateLowPriceGoods($where, $data)
  545. {
  546. DB::beginTransaction();
  547. try {
  548. $this->editLowPriceGoods_content($where, $data);
  549. DB::commit();
  550. return true;
  551. // 成功处理...
  552. } catch (\Exception $e) {
  553. DB::rollBack();
  554. // 错误处理...
  555. return false;
  556. }
  557. }
  558. /**
  559. * 修改状态
  560. * @author 唐远望
  561. * @version 1.0
  562. * @date 2025-12-08
  563. * @param $id
  564. * @param $status
  565. * @return bool
  566. */
  567. public function changeStatus($where, $status)
  568. {
  569. $LowPriceGoods = $this->where($where)->first();
  570. if (!$LowPriceGoods) {
  571. return false;
  572. }
  573. $LowPriceGoods->status = $status;
  574. $LowPriceGoods->update_time = time();
  575. $LowPriceGoods->save();
  576. return true;
  577. }
  578. /**
  579. * 修改处理状态
  580. * @author 唐远望
  581. * @version 1.0
  582. * @date 2025-12-08
  583. * @param $id
  584. * @param $processing_status
  585. * @return bool
  586. */
  587. public function changeProcessingStatus($where, $processing_status)
  588. {
  589. $LowPriceGoods = $this->where($where)->first();
  590. if (!$LowPriceGoods) {
  591. return false;
  592. }
  593. $LowPriceGoods->processing_status = $processing_status;
  594. $LowPriceGoods->update_time = time();
  595. $LowPriceGoods->save();
  596. return true;
  597. }
  598. /**
  599. * 删除数据
  600. * @author 唐远望
  601. * @version 1.0
  602. * @date 2025-12-08
  603. * @param $id
  604. * @return bool
  605. */
  606. public function deleteLowPriceGoods($where)
  607. {
  608. $LowPriceGoods = $this->where($where)->first();
  609. if (!$LowPriceGoods) {
  610. return false;
  611. }
  612. $LowPriceGoods->delete();
  613. return true;
  614. }
  615. }