Notification.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\Log;
  5. class Notification extends BaseModel
  6. {
  7. public static function send($order_id, $overdue = false)
  8. {
  9. $order = Order::find($order_id);
  10. if(!$order) return false;
  11. if($order['is_draft'] == 1) return false;
  12. $checked_id = Option::get('orders', 'status', 'checked');
  13. $reject_id = Option::get('orders', 'status', 'reject');
  14. $pass_id = Option::get('orders', 'status', 'pass');
  15. $back_id = Option::get('orders', 'status', 'back');
  16. $last_role = ProjectRole::find($order['last_project_role_id']);
  17. $role = ProjectRole::find($order['project_role_id']);
  18. $status = 1;
  19. $user_ids = [];
  20. $project_role_ids = ProjectRole::where('level', $order['level'])->pluck('id');
  21. if($overdue) {
  22. // 调用时间到期
  23. $role = ProjectRole::getFirstRole($order);
  24. $project_role_ids = [$role['id']];
  25. } else if($order['status'] == $pass_id) {
  26. // 租赁完成,调用完成
  27. if($order['type'] == 1) {
  28. // 租赁完成 经理通过后给工区负责人发消息
  29. if($order->level== 4 && $order->status == 2 ){
  30. $project_role_ids = ProjectRole::whereIn('key', ['work'])->pluck('id');
  31. }
  32. //老逻辑 审核状态都通知项目经理
  33. //$project_role_ids = ProjectRole::whereIn('key', ['manager'])->pluck('id');
  34. } else if($order['is_change' == 2]) {
  35. // 调用完成且未修改
  36. $project_role_ids = ProjectRole::whereIn('key', ['admin'])->pluck('id');
  37. } else {
  38. // 调用完成且修改
  39. $project_role_ids = ProjectRole::whereIn('key', ['admin'])->pluck('id');
  40. }
  41. } else if($order['status'] == $back_id) {
  42. $project_role_ids = ProjectRole::whereIn('key', ['admin'])->pluck('id');
  43. }
  44. if($overdue) {
  45. $project_role_ids = ProjectRole::whereIn('key', ['machine', 'admin'])->pluck('id');
  46. }
  47. \Log::info($user_ids);
  48. if(count($project_role_ids) > 0) {
  49. //如果拒绝只给创建人提醒
  50. if($order->status == 4){
  51. $user_ids = [$order['user_id']];
  52. } else if($order->type == 2&&$order->level == 7&&$order->status ==2&&$order->last_project_role_id ==4){//项目经理通过审核的调用订单,消息提醒机电负责人
  53. $user_ids = [$order['user_id']];
  54. } else {
  55. $user_ids = ProjectUser::where([
  56. ['project_id', $order['project_id']]
  57. ])->whereIn('project_role_id', $project_role_ids)->pluck('user_id');
  58. }
  59. }
  60. \Log::info($user_ids);
  61. // 外部租赁
  62. if($order['type'] == 1) {
  63. // 审批成功
  64. if($order['status'] == $checked_id && $last_role && $last_role['key'] == 'manager') {
  65. $status = 1;
  66. } else if($role && in_array($role['key'], ['machine', 'assist', 'manager'])) {
  67. // 审批待处理
  68. $status = 2;
  69. } else if($order['status'] == $reject_id) {
  70. // 审批被驳回
  71. $status = 3;
  72. } else if($order['status'] == $pass_id) {
  73. // 租赁已完成
  74. $status = 5;
  75. }
  76. } else {
  77. // 调用成功
  78. if($order['status'] == $checked_id && $role && $last_role && in_array($last_role['key'], ['admin', 'sub'])) {
  79. $status = $order['is_change'] == 1 ? 7 : 1;
  80. } else if($order['status'] == $reject_id) {
  81. // 调用被驳回
  82. $status = 3;
  83. } else if($order['status'] == $pass_id) {
  84. // 调用已完成,主动退回
  85. $status = 6;
  86. } else if($order['status'] == $back_id) {
  87. // 调用已归还
  88. $status = 6;
  89. } else if($order['is_change'] == 1) {
  90. // 调用修改通知
  91. $status = 7;
  92. } else if($role && in_array($role['key'], ['assist', 'manager', 'sub', 'admin'])) {
  93. // 调用待处理
  94. $status = 2;
  95. }
  96. }
  97. // 调用时间到期,发送调用完成通知 status = 5
  98. if($overdue) {
  99. $status = 5;
  100. }
  101. foreach($user_ids as $user_id) {
  102. $data = [
  103. 'user_id' => $user_id,
  104. 'order_id' => $order['id'],
  105. 'status' => $status,
  106. 'type' => $order['type'],
  107. 'is_read' => 2
  108. ];
  109. self::createAndSend($data);
  110. }
  111. return true;
  112. }
  113. /**
  114. * 租赁时间到期 status = 4
  115. * @param $id
  116. * @return bool
  117. */
  118. public static function sendOverdue($id)
  119. {
  120. $order = self::find($id);
  121. if(!$order) return false;
  122. $role = $order->type == 1 ? $role = ProjectRole::getByKey('work') : ProjectRole::getByKey('machine');
  123. $user_ids = ProjectUser::where([
  124. ['project_id', $order['project_id']],
  125. ['project_role_id', $role['id']]
  126. ])->pluck('user_id');
  127. foreach($user_ids as $user_id) {
  128. $data = [
  129. 'user_id' => $user_id,
  130. 'order_id' => $order['id'],
  131. 'status' => 4,
  132. 'type' => $order['type'],
  133. 'is_read' => 2
  134. ];
  135. self::createAndSend($data);
  136. }
  137. return false;
  138. }
  139. public static function createAndSend($data)
  140. {
  141. $item = Notification::create($data);
  142. \Log::info($data['user_id']);
  143. \Log::info('user_id');
  144. $item->sendOfficialInfo();
  145. }
  146. public function sendOfficialInfo()
  147. {
  148. $content = $this->getNameContent();
  149. $user = User::find($this['user_id']);
  150. \Log::info($user);
  151. \Log::info('------');
  152. $official_app = app('wechat.mini_program.default');
  153. if($content && isset($content['official']) && $user && $user['open_id']) {
  154. $info = $content['official'];
  155. $res = $official_app->uniform_message->send([
  156. 'touser' => $user['open_id'],
  157. 'mp_template_msg' => [
  158. 'appid'=>env('WECHAT_OFFICIAL_ACCOUNT_APPID'),
  159. 'template_id' => $info['template_id'],
  160. 'miniprogram' => [
  161. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  162. 'pagepath' => $info['page'],
  163. ],
  164. 'data' => $info['data'],
  165. ]
  166. ]);
  167. }
  168. }
  169. /**
  170. * 申请进度通知
  171. * @param $project_id
  172. */
  173. public static function sendProjectInfo($project_id)
  174. {
  175. $project = Project::find($project_id);
  176. if($project && isset($project['user_id'])) {
  177. $user = User::find($project['user_id']);
  178. if($user&&$user['open_id']) {
  179. $official_app = app('wechat.mini_program.default');
  180. $title = $project->active == 1 ? '您申请的项目已经通过审批,请及时查看!' : '您申请的项目未通过申请,请重新提交!';
  181. $official_app->uniform_message->send([
  182. 'touser' => $user['open_id'],
  183. 'mp_template_msg' => [
  184. 'appid'=>env('WECHAT_OFFICIAL_ACCOUNT_APPID'),
  185. 'template_id' => 'NusdON_pl0l32P6rFeYOlvE-53QYvu_eRnn5LthaVdM',
  186. 'miniprogram' => [
  187. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  188. 'pagepath' => 'pages/index/index',
  189. ],
  190. 'data' => [
  191. 'first' => $title,
  192. 'keyword1' => $project->name,
  193. 'keyword2' => $user->name,
  194. 'keyword3' => '中铁二局',
  195. 'keyword4' => '项目经理',
  196. 'keyword5' => $project->created_at,
  197. 'remark' => '点击进入小程序查看详情',
  198. ],
  199. ]
  200. ]);
  201. }
  202. }
  203. }
  204. /**
  205. * @param $project_user
  206. * @return boolean
  207. */
  208. public static function sendAddUserInfo($project_user)
  209. {
  210. if(!$project_user) return false;
  211. $project = Project::find($project_user->project_id);
  212. if($project && isset($project['user_id'])) {
  213. $user = User::find($project_user->user_id);
  214. $role = ProjectRole::find($project_user['project_role_id']);
  215. $work_point = WorkPoint::find($project['work_point_id']);
  216. if($user&&$user['open_id']) {
  217. $official_app = app('wechat.mini_program.default');
  218. $title = '你的项目授权审批已通过!';
  219. $remark = '授权角色:' . $project->name . ' - ' . ($work_point ? $work_point->name : '') . ' - ' . ($role ? $role->name : '');
  220. $official_app->uniform_message->send([
  221. 'touser' => $user['open_id'],
  222. 'mp_template_msg' => [
  223. 'appid'=>env('WECHAT_OFFICIAL_ACCOUNT_APPID'),
  224. 'template_id' => 'zzNETW2GEZ4_GfVp020yH8n9VL97G9EttONAtcrxG9c',
  225. 'miniprogram' => [
  226. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  227. 'pagepath' => 'pages/index/index',
  228. ],
  229. 'data' => [
  230. 'first' => $title,
  231. 'keyword1' => $user->name,
  232. 'keyword2' => $project_user->created_at,
  233. 'remark' => $remark,
  234. ],
  235. ]
  236. ]);
  237. }
  238. }
  239. return true;
  240. }
  241. public function getNameContent()
  242. {
  243. $order = Order::find($this['order_id']);
  244. //如果没有订单就不展示
  245. if(empty($order)){
  246. return ['name' => '', 'content' => ''];
  247. }
  248. $status_name = isset($order) ? $order->getStatusName() : '';
  249. $source = ((!empty($order) && !empty($order->project)) ? $order->project->name : '') . '-' . ($order->workPoint ? $order->workPoint->name : '') . '-' . ($order->user ? $order->user->name : '');
  250. $tomorrow = Carbon::tomorrow();
  251. $days = OrderDevice::where([
  252. 'order_id' => $order->id,
  253. 'end_date' => $tomorrow
  254. ])->first() ? 1 : 3;
  255. if($this['type'] == 1) {
  256. $detail = OrderDevice::where('order_id', $order->id)->count() . '个设备,¥' . ($order->money / 100);
  257. $type = '设备租赁订单';
  258. if($this['status'] == 1) {
  259. return [
  260. 'name' => '租赁审批成功通知',
  261. 'content' => '你的设备租赁申请已通过,请确认相关信息:',
  262. 'official' => [
  263. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  264. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  265. 'data' => [
  266. 'first' => '您有一个订单已通过审核,请注意查看!',
  267. 'keyword1' => $this['created_at'],
  268. 'keyword2' => $type,
  269. 'keyword3' => $status_name,
  270. 'keyword4' => $source,
  271. 'keyword5' => $detail,
  272. 'remark' => '点击进入小程序查看详情。',
  273. ]
  274. ]
  275. ];
  276. } else if($this['status'] == 2) {
  277. return [
  278. 'name' => '租赁审批待处理通知',
  279. 'content' => '你有一条设备租赁申请待审批,请确认相关信息:',
  280. 'official' => [
  281. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  282. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  283. 'data' => [
  284. 'first' => '您有一条待审核的订单,请注意查看!',
  285. 'keyword1' => $this['created_at'],
  286. 'keyword2' => $type,
  287. 'keyword3' => $status_name,
  288. 'keyword4' => $source,
  289. 'keyword5' => $detail,
  290. 'remark' => '点击进入小程序查看详情,请及时处理。',
  291. ]
  292. ]
  293. ];
  294. } else if($this['status'] == 3) {
  295. return [
  296. 'name' => '租赁审批驳回通知',
  297. 'content' => '你有一条设备租赁申请被驳回,请确认相关信息:',
  298. 'official' => [
  299. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  300. 'page' => '/pages/create-order/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  301. 'data' => [
  302. 'first' => '您有一个订单已被驳回,请注意查看!',
  303. 'keyword1' => $this['created_at'],
  304. 'keyword2' => $type,
  305. 'keyword3' => $status_name,
  306. 'keyword4' => $source,
  307. 'keyword5' => $detail,
  308. 'remark' => '点击进入小程序查看详情,请及时处理。',
  309. ]
  310. ]
  311. ];
  312. } else if($this['status'] == 4) {
  313. return [
  314. 'name' => '租赁时间到期通知',
  315. 'content' => '你有一条设备租赁已到期消息,请确认相关信息:',
  316. 'official' => [
  317. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  318. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  319. 'data' => [
  320. 'first' => '您有一个设备租赁订单即将到期,请注意查看',
  321. 'keyword1' => $this['created_at'],
  322. 'keyword2' => $type,
  323. 'keyword3' => $status_name,
  324. 'keyword4' => $source,
  325. 'keyword5' => $detail,
  326. 'remark' => '你租赁的设备将于' . $days . '天后到期,请进入小程序查看
  327. ',
  328. ]
  329. ]
  330. ];
  331. } else if($this['status'] == 5) {
  332. return [
  333. 'name' => '租赁完成通知',
  334. 'content' => '你有一条设备租赁已完成消息,请确认相关信息:',
  335. 'official' => [
  336. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  337. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  338. 'data' => [
  339. 'first' => '有一个订单已完成,请注意查看!',
  340. 'keyword1' => $this['created_at'],
  341. 'keyword2' => $type,
  342. 'keyword3' => $status_name,
  343. 'keyword4' => $source,
  344. 'keyword5' => $detail,
  345. 'remark' => '点击进入小程序查看详情。',
  346. ]
  347. ]
  348. ];
  349. }
  350. } else {
  351. $detail = OrderDevice::where('order_id', $order->id)->count() . '个设备';
  352. $type = '设备调用订单';
  353. if($this['status'] == 1) {
  354. return [
  355. 'name' => '调用审批成功通知',
  356. 'content' => '你的设备调用申请已通过,请确认相关信息:',
  357. 'official' => [
  358. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  359. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  360. 'data' => [
  361. 'first' => '您有一个订单已通过审核,请注意查看!',
  362. 'keyword1' => $this['created_at'],
  363. 'keyword2' => $type,
  364. 'keyword3' => $status_name,
  365. 'keyword4' => $source,
  366. 'keyword5' => $detail,
  367. 'remark' => '点击进入小程序查看详情。',
  368. ]
  369. ]
  370. ];
  371. } else if($this['status'] == 2) {
  372. return [
  373. 'name' => '调用审批待处理通知',
  374. 'content' => '你有一条设备调用申请待审批,请确认相关信息:',
  375. 'official' => [
  376. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  377. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  378. 'data' => [
  379. 'first' => '您有一条待审核的订单,请注意查看!',
  380. 'keyword1' => $this['created_at'],
  381. 'keyword2' => $type,
  382. 'keyword3' => $status_name,
  383. 'keyword4' => $source,
  384. 'keyword5' => $detail,
  385. 'remark' => '点击进入小程序查看详情,请及时处理。',
  386. ]
  387. ]
  388. ];
  389. } else if($this['status'] == 3) {
  390. return [
  391. 'name' => '调用审批驳回通知',
  392. 'content' => '你有一条设备调用申请被驳回,请确认相关信息:',
  393. 'official' => [
  394. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  395. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  396. 'data' => [
  397. 'first' => '您有一个订单已被驳回,请注意查看!',
  398. 'keyword1' => $this['created_at'],
  399. 'keyword2' => $type,
  400. 'keyword3' => $status_name,
  401. 'keyword4' => $source,
  402. 'keyword5' => $detail,
  403. 'remark' => '点击进入小程序查看详情,请及时处理。',
  404. ]
  405. ]
  406. ];
  407. } else if($this['status'] == 4) {
  408. return [
  409. 'name' => '调用时间到期通知',
  410. 'content' => '你有一条设备调用已到期消息,请确认相关信息:',
  411. 'official' => [
  412. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  413. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  414. 'data' => [
  415. 'first' => '您有一个设备租赁订单即将到期,请注意查看',
  416. 'keyword1' => $this['created_at'],
  417. 'keyword2' => $type,
  418. 'keyword3' => $status_name,
  419. 'keyword4' => $source,
  420. 'keyword5' => $detail,
  421. 'remark' => '你租赁的设备将于' . $days . '天后到期,请进入小程序查看
  422. ',
  423. ]
  424. ]
  425. ];
  426. } else if($this['status'] == 5) {
  427. return [
  428. 'name' => '调用完成通知',
  429. 'content' => '你有一条设备调用已完成消息,请确认相关信息:',
  430. 'official' => [
  431. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  432. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  433. 'data' => [
  434. 'first' => '有一个订单已完成,请注意查看!',
  435. 'keyword1' => $this['created_at'],
  436. 'keyword2' => $type,
  437. 'keyword3' => $status_name,
  438. 'keyword4' => $source,
  439. 'keyword5' => $detail,
  440. 'remark' => '点击进入小程序查看详情。',
  441. ]
  442. ]
  443. ];
  444. } else if($this['status'] == 6) {
  445. return [
  446. 'name' => '调用退回通知',
  447. 'content' => '你有一条设备调用已归还消息,请确认相关信息:',
  448. 'official' => [
  449. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  450. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  451. 'data' => [
  452. 'first' => '有一个设备调用订单已完成,请注意查看',
  453. 'keyword1' => $this['created_at'],
  454. 'keyword2' => $type,
  455. 'keyword3' => $status_name,
  456. 'keyword4' => $source,
  457. 'keyword5' => $detail,
  458. 'remark' => '请进入小程序查看详情',
  459. ]
  460. ]
  461. ];
  462. } else if($this['status'] == 7) {
  463. return [
  464. 'name' => '调用修改通知',
  465. 'content' => '你有一条设备调用被修改消息,请确认相关信息:'
  466. ];
  467. }
  468. }
  469. return ['name' => '', 'content' => ''];
  470. }
  471. }