Notification.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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.official_account.default');
  136. if($content && isset($content['official']) && $user && $user['official_open_id']) {
  137. $info = $content['official'];
  138. $official_app->template_message->send([
  139. 'touser' => $user['official_open_id'],
  140. 'template_id' => $info['template_id'],
  141. 'miniprogram' => [
  142. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  143. 'pagepath' => $info['page'],
  144. ],
  145. 'data' => $info['data'],
  146. ]);
  147. }
  148. }
  149. /**
  150. * 申请进度通知
  151. * @param $project_id
  152. */
  153. public static function sendProjectInfo($project_id)
  154. {
  155. $project = Project::find($project_id);
  156. if($project && isset($project['user_id'])) {
  157. $user = User::find($project['user_id']);
  158. if($user) {
  159. $official_app = app('wechat.official_account.default');
  160. $title = $project->active == 1 ? '您申请的项目已经通过审批,请及时查看!' : '您申请的项目未通过申请,请重新提交!';
  161. $official_app->template_message->send([
  162. 'touser' => $user['official_open_id'],
  163. 'template_id' => 'NusdON_pl0l32P6rFeYOlvE-53QYvu_eRnn5LthaVdM',
  164. 'miniprogram' => [
  165. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  166. 'pagepath' => 'pages/index/index',
  167. ],
  168. 'data' => [
  169. 'first' => $title,
  170. 'keyword1' => $project->name,
  171. 'keyword2' => $user->name,
  172. 'keyword3' => '中铁二局',
  173. 'keyword4' => '',
  174. 'keyword5' => $project->created_at,
  175. 'remark' => '点击进入小程序查看详情',
  176. ],
  177. ]);
  178. }
  179. }
  180. }
  181. /**
  182. * @param $project_user
  183. * @return boolean
  184. */
  185. public static function sendAddUserInfo($project_user)
  186. {
  187. if(!$project_user) return false;
  188. $project = Project::find($project_user->project_id);
  189. if($project && isset($project['user_id'])) {
  190. $user = User::find($project['user_id']);
  191. $role = ProjectRole::find($project_user['project_role_id']);
  192. $work_point = WorkPoint::find($project['work_point_id']);
  193. if($user) {
  194. $official_app = app('wechat.official_account.default');
  195. $title = '你的项目授权审批已通过!';
  196. $remark = '授权角色:' . $project->name . ' - ' . ($work_point ? $work_point->name : '') . ' - ' . ($role ? $role->name : '');
  197. $official_app->template_message->send([
  198. 'touser' => $user['official_open_id'],
  199. 'template_id' => 'zzNETW2GEZ4_GfVp020yH8n9VL97G9EttONAtcrxG9c',
  200. 'miniprogram' => [
  201. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  202. 'pagepath' => 'pages/index/index',
  203. ],
  204. 'data' => [
  205. 'first' => $title,
  206. 'keyword1' => $user->name,
  207. 'keyword2' => $project_user->created_at,
  208. 'remark' => $remark,
  209. ],
  210. ]);
  211. }
  212. }
  213. return true;
  214. }
  215. public function getNameContent()
  216. {
  217. $order = Order::find($this['order_id']);
  218. $status_name = $order ? $order->getStatusName() : '';
  219. $source = ($order->project ? $order->project->name : '') . '-' . ($order->workPoint ? $order->workPoint->name : '') . '-' . ($order->user ? $order->user->name : '');
  220. $tomorrow = Carbon::tomorrow();
  221. $days = OrderDevice::where([
  222. 'order_id' => $order->id,
  223. 'end_date' => $tomorrow
  224. ])->first() ? 1 : 3;
  225. if($this['type'] == 1) {
  226. $detail = OrderDevice::where('order_id', $order->id)->count() . '个设备,¥' . ($order->money / 100);
  227. $type = '设备租赁订单';
  228. if($this['status'] == 1) {
  229. return [
  230. 'name' => '租赁审批成功通知',
  231. 'content' => '你的设备租赁申请已通过,请确认相关信息:',
  232. 'official' => [
  233. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  234. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  235. 'data' => [
  236. 'first' => '您有一个订单已通过审核,请注意查看!',
  237. 'keyword1' => $this['created_at'],
  238. 'keyword2' => $type,
  239. 'keyword3' => $status_name,
  240. 'keyword4' => $source,
  241. 'keyword5' => $detail,
  242. 'remark' => '点击进入小程序查看详情。',
  243. ]
  244. ]
  245. ];
  246. } else if($this['status'] == 2) {
  247. return [
  248. 'name' => '租赁审批待处理通知',
  249. 'content' => '你有一条设备租赁申请待审批,请确认相关信息:',
  250. 'official' => [
  251. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  252. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  253. 'data' => [
  254. 'first' => '您有一条待审核的订单,请注意查看!',
  255. 'keyword1' => $this['created_at'],
  256. 'keyword2' => $type,
  257. 'keyword3' => $status_name,
  258. 'keyword4' => $source,
  259. 'keyword5' => $detail,
  260. 'remark' => '点击进入小程序查看详情,请及时处理。',
  261. ]
  262. ]
  263. ];
  264. } else if($this['status'] == 3) {
  265. return [
  266. 'name' => '租赁审批驳回通知',
  267. 'content' => '你有一条设备租赁申请被驳回,请确认相关信息:',
  268. 'official' => [
  269. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  270. 'page' => '/pages/create-order/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  271. 'data' => [
  272. 'first' => '您有一个订单已被驳回,请注意查看!',
  273. 'keyword1' => $this['created_at'],
  274. 'keyword2' => $type,
  275. 'keyword3' => $status_name,
  276. 'keyword4' => $source,
  277. 'keyword5' => $detail,
  278. 'remark' => '点击进入小程序查看详情,请及时处理。',
  279. ]
  280. ]
  281. ];
  282. } else if($this['status'] == 4) {
  283. return [
  284. 'name' => '租赁时间到期通知',
  285. 'content' => '你有一条设备租赁已到期消息,请确认相关信息:',
  286. 'official' => [
  287. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  288. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  289. 'data' => [
  290. 'first' => '您有一个设备租赁订单即将到期,请注意查看',
  291. 'keyword1' => $this['created_at'],
  292. 'keyword2' => $type,
  293. 'keyword3' => $status_name,
  294. 'keyword4' => $source,
  295. 'keyword5' => $detail,
  296. 'remark' => '你租赁的设备将于' . $days . '天后到期,请进入小程序查看
  297. ',
  298. ]
  299. ]
  300. ];
  301. } else if($this['status'] == 5) {
  302. return [
  303. 'name' => '租赁完成通知',
  304. 'content' => '你有一条设备租赁已完成消息,请确认相关信息:',
  305. 'official' => [
  306. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  307. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  308. 'data' => [
  309. 'first' => '有一个订单已完成,请注意查看!',
  310. 'keyword1' => $this['created_at'],
  311. 'keyword2' => $type,
  312. 'keyword3' => $status_name,
  313. 'keyword4' => $source,
  314. 'keyword5' => $detail,
  315. 'remark' => '点击进入小程序查看详情。',
  316. ]
  317. ]
  318. ];
  319. }
  320. } else {
  321. $detail = OrderDevice::where('order_id', $order->id)->count() . '个设备';
  322. $type = '设备调用订单';
  323. if($this['status'] == 1) {
  324. return [
  325. 'name' => '调用审批成功通知',
  326. 'content' => '你的设备调用申请已通过,请确认相关信息:',
  327. 'official' => [
  328. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  329. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  330. 'data' => [
  331. 'first' => '您有一个订单已通过审核,请注意查看!',
  332. 'keyword1' => $this['created_at'],
  333. 'keyword2' => $type,
  334. 'keyword3' => $status_name,
  335. 'keyword4' => $source,
  336. 'keyword5' => $detail,
  337. 'remark' => '点击进入小程序查看详情。',
  338. ]
  339. ]
  340. ];
  341. } else if($this['status'] == 2) {
  342. return [
  343. 'name' => '调用审批待处理通知',
  344. 'content' => '你有一条设备调用申请待审批,请确认相关信息:',
  345. 'official' => [
  346. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  347. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  348. 'data' => [
  349. 'first' => '您有一条待审核的订单,请注意查看!',
  350. 'keyword1' => $this['created_at'],
  351. 'keyword2' => $type,
  352. 'keyword3' => $status_name,
  353. 'keyword4' => $source,
  354. 'keyword5' => $detail,
  355. 'remark' => '点击进入小程序查看详情,请及时处理。',
  356. ]
  357. ]
  358. ];
  359. } else if($this['status'] == 3) {
  360. return [
  361. 'name' => '调用审批驳回通知',
  362. 'content' => '你有一条设备调用申请被驳回,请确认相关信息:',
  363. 'official' => [
  364. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  365. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  366. 'data' => [
  367. 'first' => '您有一个订单已被驳回,请注意查看!',
  368. 'keyword1' => $this['created_at'],
  369. 'keyword2' => $type,
  370. 'keyword3' => $status_name,
  371. 'keyword4' => $source,
  372. 'keyword5' => $detail,
  373. 'remark' => '点击进入小程序查看详情,请及时处理。',
  374. ]
  375. ]
  376. ];
  377. } else if($this['status'] == 4) {
  378. return [
  379. 'name' => '调用时间到期通知',
  380. 'content' => '你有一条设备调用已到期消息,请确认相关信息:',
  381. 'official' => [
  382. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  383. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  384. 'data' => [
  385. 'first' => '您有一个设备租赁订单即将到期,请注意查看',
  386. 'keyword1' => $this['created_at'],
  387. 'keyword2' => $type,
  388. 'keyword3' => $status_name,
  389. 'keyword4' => $source,
  390. 'keyword5' => $detail,
  391. 'remark' => '你租赁的设备将于' . $days . '天后到期,请进入小程序查看
  392. ',
  393. ]
  394. ]
  395. ];
  396. } else if($this['status'] == 5) {
  397. return [
  398. 'name' => '调用完成通知',
  399. 'content' => '你有一条设备调用已完成消息,请确认相关信息:',
  400. 'official' => [
  401. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  402. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  403. 'data' => [
  404. 'first' => '有一个订单已完成,请注意查看!',
  405. 'keyword1' => $this['created_at'],
  406. 'keyword2' => $type,
  407. 'keyword3' => $status_name,
  408. 'keyword4' => $source,
  409. 'keyword5' => $detail,
  410. 'remark' => '点击进入小程序查看详情。',
  411. ]
  412. ]
  413. ];
  414. } else if($this['status'] == 6) {
  415. return [
  416. 'name' => '调用退回通知',
  417. 'content' => '你有一条设备调用已归还消息,请确认相关信息:',
  418. 'official' => [
  419. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  420. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  421. 'data' => [
  422. 'first' => '有一个设备调用订单已完成,请注意查看',
  423. 'keyword1' => $this['created_at'],
  424. 'keyword2' => $type,
  425. 'keyword3' => $status_name,
  426. 'keyword4' => $source,
  427. 'keyword5' => $detail,
  428. 'remark' => '请进入小程序查看详情',
  429. ]
  430. ]
  431. ];
  432. } else if($this['status'] == 7) {
  433. return [
  434. 'name' => '调用修改通知',
  435. 'content' => '你有一条设备调用被修改消息,请确认相关信息:'
  436. ];
  437. }
  438. }
  439. return ['name' => '', 'content' => ''];
  440. }
  441. }