Notification.php 21 KB

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