SubNoticeJobs.php 19 KB

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