Notification.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. Log::info('4');
  195. if(!$project_user) return false;
  196. Log::info('5');
  197. $project = Project::find($project_user->project_id);
  198. Log::info('6');
  199. if($project && isset($project['user_id'])) {
  200. Log::info('7');
  201. $user = User::find($project_user->user_id);
  202. Log::info('8');
  203. $role = ProjectRole::find($project_user['project_role_id']);
  204. Log::info('9');
  205. $work_point = WorkPoint::find($project['work_point_id']);
  206. Log::info('10');
  207. if($user&&$user['open_id']) {
  208. Log::info('11');
  209. $official_app = app('wechat.mini_program.default');
  210. Log::info('12');
  211. $title = '你的项目授权审批已通过!';
  212. Log::info('13');
  213. $remark = '授权角色:' . $project->name . ' - ' . ($work_point ? $work_point->name : '') . ' - ' . ($role ? $role->name : '');
  214. Log::info('进来了');
  215. $official_app->uniform_message->send([
  216. 'touser' => $user['open_id'],
  217. 'mp_template_msg' => [
  218. 'appid'=>env('WECHAT_OFFICIAL_ACCOUNT_APPID'),
  219. 'template_id' => 'zzNETW2GEZ4_GfVp020yH8n9VL97G9EttONAtcrxG9c',
  220. 'miniprogram' => [
  221. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  222. 'pagepath' => 'pages/index/index',
  223. ],
  224. 'data' => [
  225. 'first' => $title,
  226. 'keyword1' => $user->name,
  227. 'keyword2' => $project_user->created_at,
  228. 'remark' => $remark,
  229. ],
  230. ]
  231. ]);
  232. Log::info('15');
  233. }
  234. }
  235. return true;
  236. }
  237. public function getNameContent()
  238. {
  239. $order = Order::find($this['order_id']);
  240. //如果没有订单就不展示
  241. if(empty($order)){
  242. return ['name' => '', 'content' => ''];
  243. }
  244. $status_name = isset($order) ? $order->getStatusName() : '';
  245. $source = ((!empty($order) && !empty($order->project)) ? $order->project->name : '') . '-' . ($order->workPoint ? $order->workPoint->name : '') . '-' . ($order->user ? $order->user->name : '');
  246. $tomorrow = Carbon::tomorrow();
  247. $days = OrderDevice::where([
  248. 'order_id' => $order->id,
  249. 'end_date' => $tomorrow
  250. ])->first() ? 1 : 3;
  251. if($this['type'] == 1) {
  252. $detail = OrderDevice::where('order_id', $order->id)->count() . '个设备,¥' . ($order->money / 100);
  253. $type = '设备租赁订单';
  254. if($this['status'] == 1) {
  255. return [
  256. 'name' => '租赁审批成功通知',
  257. 'content' => '你的设备租赁申请已通过,请确认相关信息:',
  258. 'official' => [
  259. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  260. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  261. 'data' => [
  262. 'first' => '您有一个订单已通过审核,请注意查看!',
  263. 'keyword1' => $this['created_at'],
  264. 'keyword2' => $type,
  265. 'keyword3' => $status_name,
  266. 'keyword4' => $source,
  267. 'keyword5' => $detail,
  268. 'remark' => '点击进入小程序查看详情。',
  269. ]
  270. ]
  271. ];
  272. } else if($this['status'] == 2) {
  273. return [
  274. 'name' => '租赁审批待处理通知',
  275. 'content' => '你有一条设备租赁申请待审批,请确认相关信息:',
  276. 'official' => [
  277. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  278. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  279. 'data' => [
  280. 'first' => '您有一条待审核的订单,请注意查看!',
  281. 'keyword1' => $this['created_at'],
  282. 'keyword2' => $type,
  283. 'keyword3' => $status_name,
  284. 'keyword4' => $source,
  285. 'keyword5' => $detail,
  286. 'remark' => '点击进入小程序查看详情,请及时处理。',
  287. ]
  288. ]
  289. ];
  290. } else if($this['status'] == 3) {
  291. return [
  292. 'name' => '租赁审批驳回通知',
  293. 'content' => '你有一条设备租赁申请被驳回,请确认相关信息:',
  294. 'official' => [
  295. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  296. 'page' => '/pages/create-order/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  297. 'data' => [
  298. 'first' => '您有一个订单已被驳回,请注意查看!',
  299. 'keyword1' => $this['created_at'],
  300. 'keyword2' => $type,
  301. 'keyword3' => $status_name,
  302. 'keyword4' => $source,
  303. 'keyword5' => $detail,
  304. 'remark' => '点击进入小程序查看详情,请及时处理。',
  305. ]
  306. ]
  307. ];
  308. } else if($this['status'] == 4) {
  309. return [
  310. 'name' => '租赁时间到期通知',
  311. 'content' => '你有一条设备租赁已到期消息,请确认相关信息:',
  312. 'official' => [
  313. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  314. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  315. 'data' => [
  316. 'first' => '您有一个设备租赁订单即将到期,请注意查看',
  317. 'keyword1' => $this['created_at'],
  318. 'keyword2' => $type,
  319. 'keyword3' => $status_name,
  320. 'keyword4' => $source,
  321. 'keyword5' => $detail,
  322. 'remark' => '你租赁的设备将于' . $days . '天后到期,请进入小程序查看
  323. ',
  324. ]
  325. ]
  326. ];
  327. } else if($this['status'] == 5) {
  328. return [
  329. 'name' => '租赁完成通知',
  330. 'content' => '你有一条设备租赁已完成消息,请确认相关信息:',
  331. 'official' => [
  332. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  333. 'page' => 'pages/order-detail/index?id=' . $this['order_id'],
  334. 'data' => [
  335. 'first' => '有一个订单已完成,请注意查看!',
  336. 'keyword1' => $this['created_at'],
  337. 'keyword2' => $type,
  338. 'keyword3' => $status_name,
  339. 'keyword4' => $source,
  340. 'keyword5' => $detail,
  341. 'remark' => '点击进入小程序查看详情。',
  342. ]
  343. ]
  344. ];
  345. }
  346. } else {
  347. $detail = OrderDevice::where('order_id', $order->id)->count() . '个设备';
  348. $type = '设备调用订单';
  349. if($this['status'] == 1) {
  350. return [
  351. 'name' => '调用审批成功通知',
  352. 'content' => '你的设备调用申请已通过,请确认相关信息:',
  353. 'official' => [
  354. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  355. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  356. 'data' => [
  357. 'first' => '您有一个订单已通过审核,请注意查看!',
  358. 'keyword1' => $this['created_at'],
  359. 'keyword2' => $type,
  360. 'keyword3' => $status_name,
  361. 'keyword4' => $source,
  362. 'keyword5' => $detail,
  363. 'remark' => '点击进入小程序查看详情。',
  364. ]
  365. ]
  366. ];
  367. } else if($this['status'] == 2) {
  368. return [
  369. 'name' => '调用审批待处理通知',
  370. 'content' => '你有一条设备调用申请待审批,请确认相关信息:',
  371. 'official' => [
  372. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  373. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  374. 'data' => [
  375. 'first' => '您有一条待审核的订单,请注意查看!',
  376. 'keyword1' => $this['created_at'],
  377. 'keyword2' => $type,
  378. 'keyword3' => $status_name,
  379. 'keyword4' => $source,
  380. 'keyword5' => $detail,
  381. 'remark' => '点击进入小程序查看详情,请及时处理。',
  382. ]
  383. ]
  384. ];
  385. } else if($this['status'] == 3) {
  386. return [
  387. 'name' => '调用审批驳回通知',
  388. 'content' => '你有一条设备调用申请被驳回,请确认相关信息:',
  389. 'official' => [
  390. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  391. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  392. 'data' => [
  393. 'first' => '您有一个订单已被驳回,请注意查看!',
  394. 'keyword1' => $this['created_at'],
  395. 'keyword2' => $type,
  396. 'keyword3' => $status_name,
  397. 'keyword4' => $source,
  398. 'keyword5' => $detail,
  399. 'remark' => '点击进入小程序查看详情,请及时处理。',
  400. ]
  401. ]
  402. ];
  403. } else if($this['status'] == 4) {
  404. return [
  405. 'name' => '调用时间到期通知',
  406. 'content' => '你有一条设备调用已到期消息,请确认相关信息:',
  407. 'official' => [
  408. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  409. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  410. 'data' => [
  411. 'first' => '您有一个设备租赁订单即将到期,请注意查看',
  412. 'keyword1' => $this['created_at'],
  413. 'keyword2' => $type,
  414. 'keyword3' => $status_name,
  415. 'keyword4' => $source,
  416. 'keyword5' => $detail,
  417. 'remark' => '你租赁的设备将于' . $days . '天后到期,请进入小程序查看
  418. ',
  419. ]
  420. ]
  421. ];
  422. } else if($this['status'] == 5) {
  423. return [
  424. 'name' => '调用完成通知',
  425. 'content' => '你有一条设备调用已完成消息,请确认相关信息:',
  426. 'official' => [
  427. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  428. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  429. 'data' => [
  430. 'first' => '有一个订单已完成,请注意查看!',
  431. 'keyword1' => $this['created_at'],
  432. 'keyword2' => $type,
  433. 'keyword3' => $status_name,
  434. 'keyword4' => $source,
  435. 'keyword5' => $detail,
  436. 'remark' => '点击进入小程序查看详情。',
  437. ]
  438. ]
  439. ];
  440. } else if($this['status'] == 6) {
  441. return [
  442. 'name' => '调用退回通知',
  443. 'content' => '你有一条设备调用已归还消息,请确认相关信息:',
  444. 'official' => [
  445. 'template_id' => 'dlRsvUWqeziBvGbdT89b69slvZll6gZVRCotIXCwwjM',
  446. 'page' => '/pages/create-order-inner/index?id=' . $order->project_id . '&order_id=' . $order->id . '&type=edit',
  447. 'data' => [
  448. 'first' => '有一个设备调用订单已完成,请注意查看',
  449. 'keyword1' => $this['created_at'],
  450. 'keyword2' => $type,
  451. 'keyword3' => $status_name,
  452. 'keyword4' => $source,
  453. 'keyword5' => $detail,
  454. 'remark' => '请进入小程序查看详情',
  455. ]
  456. ]
  457. ];
  458. } else if($this['status'] == 7) {
  459. return [
  460. 'name' => '调用修改通知',
  461. 'content' => '你有一条设备调用被修改消息,请确认相关信息:'
  462. ];
  463. }
  464. }
  465. return ['name' => '', 'content' => ''];
  466. }
  467. }