Notification.php 22 KB

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