SubNoticeJobs.php 18 KB

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