LowPriceGoods.php 34 KB

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