SubNoticeJobs.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace App\Jobs\Manager\Process;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Facades\Servers\Logs\Log;
  10. use App\Models\Manager\Process\LowPriceGoodsMember as LowPriceGoodsMemberModel;
  11. use App\Models\Manager\Process\LowPriceGoods as LowPriceGoodsModel;
  12. use App\Models\Manager\Process\ViolationProductMember as ViolationProductMemberModel;
  13. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  14. use App\Models\Manager\Process\ViolationStore as ViolationStoreModel;
  15. use App\Models\Manager\Process\ViolationStoreMember as ViolationStoreMemberModel;
  16. use Illuminate\Support\Carbon;
  17. use App\Facades\Servers\Sms\VerifyCode as Sms;
  18. use App\Servers\Email\VerifyCode as EmailVerifyCode;
  19. use App\Models\Manager\Personnel\NoticeConfig as NoticeConfigModel;
  20. use App\Models\Manager\Process\SubNoticeLog as SubNoticeLogModel;
  21. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  22. /**
  23. * 订阅通知
  24. * @author 唐远望
  25. * @version 1.0
  26. * @date 2026-03-30
  27. */
  28. class SubNoticeJobs implements ShouldQueue
  29. {
  30. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  31. protected $message_data;
  32. protected $user_data;
  33. /**
  34. * Create a new job instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(array $message_data)
  39. {
  40. $this->message_data = $message_data;
  41. }
  42. /**
  43. * Execute the job.
  44. *
  45. * @return void
  46. */
  47. public function handle()
  48. {
  49. try {
  50. $notice_config_data = $this->get_statistics_time_rule($this->message_data['company_id']);
  51. if ($notice_config_data['statistics_start_time'] == '' || $notice_config_data['statistics_end_time'] == '' || $notice_config_data['statistics_day'] == 0) {
  52. //记录日志
  53. // Log::info('job_info', '订阅未设置,不进行推送', ['data' => $this->message_data, 'notice_config_data' => $notice_config_data]);
  54. return true;
  55. }
  56. $statistics_start_time = $notice_config_data['statistics_start_time'];
  57. $statistics_end_time = $notice_config_data['statistics_end_time'];
  58. $push_time = $notice_config_data['push_time'];
  59. $EmailVerifyCode = new EmailVerifyCode();
  60. $EmployeeModel = new EmployeeModel();
  61. $sms_tpl_id = config('verifycode.aliyun_process_merge_notice.sms_tpl');
  62. $company_id = $this->message_data['company_id'];
  63. $action_one = $this->send_low_price_goods_notice($company_id, $statistics_start_time, $statistics_end_time);
  64. $action_two = $this->send_violation_product($company_id, $statistics_start_time, $statistics_end_time);
  65. $action_three = $this->send_violation_store($company_id, $statistics_start_time, $statistics_end_time);
  66. if (empty($this->user_data)) return true;
  67. $SubNoticeLogModel = new SubNoticeLogModel();
  68. if ($action_one && $action_two && $action_three) {
  69. foreach ($this->user_data as $key => $value) {
  70. $mobile = $value['mobile'];
  71. $email_to = $value['email'];
  72. $user_id = $key;
  73. //查询已已经开启通知设置的用户
  74. $employee_ids = $EmployeeModel->where('id', $user_id)->where('open_notice', 0)->get();
  75. if (empty($employee_ids)) continue;
  76. $number1 = isset($this->user_data[$key]['lowprice_product_logids']) ? count($this->user_data[$key]['lowprice_product_logids']) : 0;
  77. $number2 = isset($this->user_data[$key]['violation_product_logids']) ? count($this->user_data[$key]['violation_product_logids']) : 0;
  78. $number3 = isset($this->user_data[$key]['violation_store_logids']) ? count($this->user_data[$key]['violation_store_logids']) : 0;
  79. //统计总条数
  80. $totle_number = $number1 + $number2 + $number3;
  81. $sms_config_data = [];
  82. if (trim($mobile) != '') {
  83. $sms_config_data = ['parameter' => ['totle_number' => $totle_number, 'number1' => $number1, 'number2' => $number2, 'number3' => $number3], 'sms_tpl_id' => $sms_tpl_id];
  84. // $res_msg = Sms::sendContent($mobile, ['totle_number' => $totle_number, 'number1' => $number1, 'number2' => $number2, 'number3' => $number3], $sms_tpl_id);
  85. // Log::info('job_send_sms', '订阅短信通知推送队列记录', ['email' => $mobile, 'sms_tpl_id' => $sms_tpl_id, 'msg' => $res_msg]);
  86. }
  87. $header_date = '';
  88. $statistics_start_time_string = date('Y-m-d', strtotime($statistics_start_time));
  89. $statistics_end_time_string = date('Y-m-d', strtotime($statistics_end_time));
  90. if ($statistics_start_time_string == $statistics_end_time_string) {
  91. $header_date = $statistics_start_time;
  92. } else {
  93. $header_date = $statistics_start_time_string . '至' . $statistics_end_time_string;
  94. }
  95. $email_title = '';
  96. $email_content = '';
  97. if (trim($email_to) != '') {
  98. $email_title = '智价云违规数据通知提醒';
  99. $email_content = "智价云提醒:" . $header_date . "采集总数据共" . $totle_number . "条,其中低价挂网商品共" . $number1 . "条、禁止挂网商品共" . $number2 . "条、违规挂网店铺共" . $number3 . "条。";
  100. // $res_msg = $EmailVerifyCode->sendSmtpEmail($email_to, $email_title, $email_content);
  101. // Log::info('job_send_email', '订阅邮件通知推送队列记录', ['email' => $email_to, 'email_content' => $email_content, 'msg' => $res_msg]);
  102. }
  103. $internal_notice_content = $header_date . "采集总数据共" . $totle_number . "条,其中低价挂网商品共" . $number1 . "条、禁止挂网商品共" . $number2 . "条、违规挂网店铺共" . $number3 . "条。";
  104. $ext_data = [
  105. 'company_id' => $company_id,
  106. 'lowprice_product_number' => $number1,
  107. 'violation_product_number' => $number2,
  108. 'violation_store_number' => $number3,
  109. 'statistics_start_time' => $statistics_start_time,
  110. 'statistics_end_time' => $statistics_end_time,
  111. ];
  112. // 增加就订阅日志
  113. $insert_data = [
  114. 'company_id' => $company_id,
  115. 'employee_id' => $user_id,
  116. 'internal_notice_content' => $internal_notice_content,
  117. 'email' => $email_to,
  118. 'mobile' => $mobile,
  119. 'email_content' => trim($email_to) != '' ? json_encode(['title' => $email_title, 'content' => $email_content]) : '',
  120. 'mobile_content' => trim($mobile) != '' ? json_encode($sms_config_data) : '',
  121. 'push_time' => strtotime(date('Y-m-d', time()) . ' ' . $push_time . ':00'),
  122. 'ext_data' => json_encode($ext_data),
  123. 'insert_time' => time(),
  124. ];
  125. $SubNoticeLogModel->insert($insert_data);
  126. }
  127. }
  128. } catch (\Exception $e) {
  129. Log::info('job_error', '订阅短信通知推送队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  130. }
  131. }
  132. /**
  133. * 获取统计时间规则
  134. * @author 唐远望
  135. * @version 1.0
  136. * @date 2026-04-11
  137. */
  138. private function get_statistics_time_rule($company_id)
  139. {
  140. // 获取当时时间是星期几
  141. $now_week = date('w');
  142. if ($now_week == 0) {
  143. $now_week = 7;
  144. }
  145. // $now_week = 1;//调试星期
  146. $NoticeConfigModel = new NoticeConfigModel();
  147. $data = $NoticeConfigModel->where(['company_id' => $company_id])->first();
  148. if (!$data) return ['now_week' => $now_week, 'statistics_day' => 0, 'statistics_start_time' => '', 'statistics_end_time' => '', 'push_time' => '', 'notice_week_config' => ''];
  149. //校验这个星期是否需要进行推送
  150. $notice_week_config = json_decode($data->notice_week_config, true);
  151. $is_open = 0;
  152. $now_week_day = 'week_' . $now_week;
  153. if ($notice_week_config[$now_week_day] == 0) {
  154. $is_open = 1;
  155. }
  156. if ($is_open == 0) {
  157. return ['now_week' => $now_week, 'statistics_day' => 0, 'statistics_start_time' => '', 'statistics_end_time' => '', 'push_time' => '', 'notice_week_config' => $notice_week_config];
  158. }
  159. //是否每天都推送
  160. $is_day_day_push = 0;
  161. for ($i = 7; $i >= 1; $i--) {
  162. $week_string = 'week_' . $i;
  163. if ($notice_week_config[$week_string] == 0) {
  164. $is_day_day_push = $is_day_day_push + 1;
  165. }
  166. }
  167. if ($is_day_day_push == 7) {
  168. $statistics_day = 1;
  169. } else {
  170. $statistics_day = 0;
  171. //判断当时时间是否是星期一
  172. if ($now_week == 1) {
  173. for ($i = 7; $i >= 1; $i--) {
  174. //校验前一天是否推送
  175. $week_string = 'week_7';
  176. if ($notice_week_config[$week_string] == 0) { // 如果前一天没有开启推送则统计,状态 0=开启 1=关闭
  177. $statistics_day = $statistics_day + 1;
  178. break;
  179. } else {
  180. $week_oth_string = 'week_' . $i;
  181. if ($notice_week_config[$week_oth_string] == '1') { // 如果前一天没有开启推送则统计,状态 0=开启 1=关闭
  182. $statistics_day = $statistics_day + 1;
  183. } else {
  184. $statistics_day = $statistics_day + 1;
  185. break;
  186. }
  187. }
  188. }
  189. } else {
  190. $statistics_day = 0;
  191. //如果当前$now_week星期,往前推,当遇到开启的日期时,停止往前推,最大为7天
  192. for ($i = $now_week; $i >= 1; $i--) {
  193. //校验前一天是否推送
  194. $yesterday_week = $now_week - 1;
  195. if ($yesterday_week < 1) {
  196. continue;
  197. }
  198. $week_string = 'week_' . $yesterday_week;
  199. //校验前一天是否开启了推送
  200. if ($i == $now_week && $notice_week_config[$week_string] == '0') {
  201. $statistics_day = $statistics_day + 1;
  202. break;
  203. } else {
  204. $i_index = $i - 1; //往前推一天
  205. if ($i_index < 1) {
  206. continue;
  207. }
  208. $week_oth_string = 'week_' . $i_index;
  209. if ($notice_week_config[$week_oth_string] == '1') {
  210. $statistics_day = $statistics_day + 1;
  211. } else {
  212. $statistics_day = $statistics_day + 1;
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. if ($statistics_day < 1) {
  220. return ['now_week' => $now_week, 'statistics_day' => $statistics_day, 'statistics_start_time' => '', 'statistics_end_time' => '', 'push_time' => '', 'notice_week_config' => $notice_week_config];
  221. }
  222. //获取统计开始时间
  223. $statistics_start_time = Carbon::today()->subDays($statistics_day)->startOfDay()->toDateTimeString();
  224. //获取昨日结束时间
  225. $statistics_end_time = Carbon::today()->subDays(1)->endOfDay()->toDateTimeString();
  226. return ['now_week' => $now_week, 'statistics_day' => $statistics_day, 'statistics_start_time' => $statistics_start_time, 'statistics_end_time' => $statistics_end_time, 'push_time' => $data->push_time, 'notice_week_config' => $notice_week_config];
  227. }
  228. /**
  229. * 短信消息消息订阅推送(低价挂网)
  230. * @author 唐远望
  231. * @version 1.0
  232. * @date 2026-03-04
  233. */
  234. private function send_low_price_goods_notice($company_id, $statistics_start_time, $statistics_end_time)
  235. {
  236. $LowPriceGoodsMemberModel = new LowPriceGoodsMemberModel();
  237. $LowPriceGoodsModel = new LowPriceGoodsModel();
  238. //获取今日待处理的商品记录
  239. $todayStart = strtotime($statistics_start_time); // 统计开始时间
  240. $todayEnd = strtotime($statistics_end_time); // 统计结束时间
  241. $lowprice_ids = $LowPriceGoodsModel->where([['insert_time', '>=', $todayStart], ['insert_time', '<=', $todayEnd], ['company_id', '=', $company_id]])->pluck('id')->toArray();
  242. if (empty($lowprice_ids)) return true;
  243. //获取开启了通知服务的用户
  244. $usert_list = $LowPriceGoodsMemberModel
  245. ->whereIn('lowprice_product_logid', $lowprice_ids)
  246. ->join('personnel_employee', 'process_lowprice_product_member.employee_id', '=', 'personnel_employee.id')
  247. ->where('personnel_employee.open_notice', 0)
  248. ->where('personnel_employee.company_id', $company_id)
  249. ->select(['process_lowprice_product_member.*', 'personnel_employee.mobile', 'personnel_employee.email'])
  250. ->get()->toArray();
  251. if (empty($usert_list)) return true;
  252. //按用户统计待处理的商品数量
  253. foreach ($usert_list as $key => $value) {
  254. $user_id = $value['employee_id'];
  255. if (!isset($this->user_data[$user_id])) {
  256. $this->user_data[$user_id]['employee_id'] = $user_id;
  257. $this->user_data[$user_id]['mobile'] = $value['mobile'];
  258. $this->user_data[$user_id]['email'] = $value['email'];
  259. $this->user_data[$user_id]['lowprice_product_logids'][] = $value['lowprice_product_logid'];
  260. //去重
  261. $lowprice_product_logids_list = $this->user_data[$user_id]['lowprice_product_logids'];
  262. $this->user_data[$user_id]['lowprice_product_logids'] = array_unique($lowprice_product_logids_list);
  263. } else {
  264. $this->user_data[$user_id]['lowprice_product_logids'][] = $value['lowprice_product_logid'];
  265. $lowprice_product_logids_list = $this->user_data[$user_id]['lowprice_product_logids'];
  266. $this->user_data[$user_id]['lowprice_product_logids'] = array_unique($lowprice_product_logids_list);
  267. }
  268. }
  269. return true;
  270. }
  271. /**
  272. * 短信消息消息订阅推送(禁止挂网)
  273. * @author 唐远望
  274. * @version 1.0
  275. * @date 2026-03-04
  276. */
  277. private function send_violation_product($company_id, $statistics_start_time, $statistics_end_time)
  278. {
  279. $ViolationProductMemberModel = new ViolationProductMemberModel();
  280. $ViolationProductModel = new ViolationProductModel();
  281. //获取今日待处理的商品记录
  282. $todayStart = strtotime($statistics_start_time); // 统计开始时间
  283. $todayEnd = strtotime($statistics_end_time); // 统计结束时间
  284. $violation_price_ids = $ViolationProductModel->where([['insert_time', '>=', $todayStart], ['insert_time', '<=', $todayEnd], ['company_id', '=', $company_id]])->pluck('id')->toArray();
  285. if (empty($violation_price_ids)) return true;
  286. //获取开启了通知服务的用户
  287. $usert_list = $ViolationProductMemberModel
  288. ->whereIn('violation_product_logid', $violation_price_ids)
  289. ->join('personnel_employee', 'process_violation_product_member.employee_id', '=', 'personnel_employee.id')
  290. ->where('personnel_employee.open_notice', 0)
  291. ->where('personnel_employee.company_id', $company_id)
  292. ->select(['process_violation_product_member.*', 'personnel_employee.mobile', 'personnel_employee.email'])
  293. ->get()->toArray();
  294. if (empty($usert_list)) return true;
  295. //按用户统计待处理的商品数量
  296. foreach ($usert_list as $key => $value) {
  297. $user_id = $value['employee_id'];
  298. if (!isset($this->user_data[$user_id])) {
  299. $this->user_data[$user_id]['employee_id'] = $user_id;
  300. $this->user_data[$user_id]['mobile'] = $value['mobile'];
  301. $this->user_data[$user_id]['email'] = $value['email'];
  302. $this->user_data[$user_id]['violation_product_logids'][] = $value['violation_product_logid'];
  303. $iolation_product_logid_list = $this->user_data[$user_id]['violation_product_logids'];
  304. $this->user_data[$user_id]['violation_product_logids'] = array_unique($iolation_product_logid_list);
  305. } else {
  306. $this->user_data[$user_id]['violation_product_logids'][] = $value['violation_product_logid'];
  307. $iolation_product_logid_list = $this->user_data[$user_id]['violation_product_logids'];
  308. $this->user_data[$user_id]['violation_product_logids'] = array_unique($iolation_product_logid_list);
  309. }
  310. }
  311. return true;
  312. }
  313. /**
  314. * 短信消息消息订阅推送(禁止店铺)
  315. * @author 唐远望
  316. * @version 1.0
  317. * @date 2026-03-04
  318. */
  319. private function send_violation_store($company_id, $statistics_start_time, $statistics_end_time)
  320. {
  321. $ViolationStoreMemberModel = new ViolationStoreMemberModel();
  322. $ViolationStoreModel = new ViolationStoreModel();
  323. //获取今日待处理的店铺记录
  324. $todayStart = strtotime($statistics_start_time); // 统计开始时间
  325. $todayEnd = strtotime($statistics_end_time); // 统计结束时间
  326. $violation_price_ids = $ViolationStoreModel->where([['insert_time', '>=', $todayStart], ['insert_time', '<=', $todayEnd], ['company_id', '=', $company_id]])->pluck('id')->toArray();
  327. if (empty($violation_price_ids)) return true;
  328. //获取开启了通知服务的用户
  329. $usert_list = $ViolationStoreMemberModel
  330. ->whereIn('violation_store_logid', $violation_price_ids)
  331. ->join('personnel_employee', 'process_violation_store_member.employee_id', '=', 'personnel_employee.id')
  332. ->where('personnel_employee.open_notice', 0)
  333. ->where('personnel_employee.company_id', $company_id)
  334. ->select(['process_lowprice_product_member.*', 'personnel_employee.mobile', 'personnel_employee.email'])
  335. ->get()->toArray();
  336. if (empty($usert_list)) return true;
  337. //按用户统计待处理的商品数量
  338. foreach ($usert_list as $key => $value) {
  339. $user_id = $value['employee_id'];
  340. if (!isset($this->user_data[$user_id])) {
  341. $this->user_data[$user_id]['employee_id'] = $user_id;
  342. $this->user_data[$user_id]['mobile'] = $value['mobile'];
  343. $this->user_data[$user_id]['violation_store_logids'][] = $value['violation_store_logid'];
  344. //去重
  345. $violation_store_logids_list = $this->user_data[$user_id]['violation_store_logids'];
  346. $this->user_data[$user_id]['violation_store_logids'] = array_unique($violation_store_logids_list);
  347. } else {
  348. //去重
  349. $this->user_data[$user_id]['violation_store_logids'][] = $value['violation_store_logid'];
  350. $violation_store_logids_list = $this->user_data[$user_id]['violation_store_logids'];
  351. $this->user_data[$user_id]['violation_store_logids'] = array_unique($violation_store_logids_list);
  352. }
  353. }
  354. return true;
  355. }
  356. public function failed(\Throwable $exception)
  357. {
  358. Log::info('job_error', '订阅短信通知推送队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  359. }
  360. }