Notification.php 21 KB

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