BorrowApp.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. namespace app\admin\controller\borrow;
  3. use app\admin\model\Borrow as BorrowModel;
  4. use app\admin\model\BorrowAccess;
  5. use app\admin\model\BorrowNumber;
  6. use app\common\controller\Backend;
  7. use ba\Random;
  8. use modules\notification\Notification;
  9. use think\facade\Db;
  10. use app\common\controller\SnowflakeId;
  11. use Throwable;
  12. use app\admin\controller\borrow\BorrowTools;
  13. use app\common\library\xmwechat\offiaccount\OaService;
  14. class BorrowApp extends Backend
  15. {
  16. protected array|string $quickSearchField = ['username', 'mobile'];
  17. public function initialize(): void
  18. {
  19. parent::initialize();
  20. $this->model = new BorrowModel;
  21. $this->BorrowAccess = new BorrowAccess();
  22. $this->BorrowNumber = new BorrowNumber();
  23. $this->adminModel = $this->auth->getAdmin();
  24. $this->BorrowTools = new BorrowTools($this->app);
  25. }
  26. public function apply(): void
  27. {
  28. if ($this->request->param('select')) {
  29. $this->select();
  30. }
  31. $user_id = $this->auth->id;
  32. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  33. list($where, $alias, $limit, $order) = $this->queryBuilder();
  34. if ($user_group_id == 1 || $user_group_id == 2) {
  35. $res = $this->model
  36. ->alias($alias)
  37. ->where($where)
  38. ->order($order)
  39. ->select();
  40. } else if ($user_group_id == 3) {
  41. $res = $this->model
  42. ->alias($alias)
  43. ->where($where)
  44. ->where("status", 6)
  45. ->where('purpose', 0)
  46. ->order($order)
  47. ->select();
  48. } else if ($user_group_id == 4) {
  49. $res = $this->model
  50. ->alias($alias)
  51. ->where($where)
  52. ->where('user_id', $user_id)
  53. ->order($order)
  54. ->select();
  55. }
  56. $this->success('', [
  57. 'list' => $res,
  58. 'remark' => get_route_remark(),
  59. ]);
  60. }
  61. public function Useinng(): void
  62. {
  63. if ($this->request->param('select')) {
  64. $this->select();
  65. }
  66. $user_id = $this->auth->id;
  67. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  68. list($where, $alias, $limit, $order) = $this->queryBuilder();
  69. if ($user_group_id == 1 || $user_group_id == 2) {
  70. $res = $this->model
  71. ->where($where)
  72. ->where("status", 'in', [3, 5])
  73. ->order($order)
  74. ->paginate($limit);
  75. } else if ($user_group_id == 3) {
  76. $res = $this->model
  77. ->where($where)
  78. ->where("status", 'in', [3, 5])
  79. ->where('college_leader_id', $user_id)
  80. ->order($order)
  81. ->paginate($limit);
  82. } else if ($user_group_id == 4) {
  83. $res = $this->model
  84. ->where($where)
  85. ->where("status", 'in', [3, 5])
  86. ->where('user_id', $user_id)
  87. ->order($order)
  88. ->paginate($limit);
  89. }
  90. $this->success('', [
  91. 'list' => $res->items(),
  92. 'total' => $res->total(),
  93. 'remark' => get_route_remark(),
  94. ]);
  95. }
  96. public function toEnd(): void
  97. {
  98. if ($this->request->param('select')) {
  99. $this->select();
  100. }
  101. $user_id = $this->auth->id;
  102. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  103. list($where, $alias, $limit, $order) = $this->queryBuilder();
  104. if ($user_group_id == 1 || $user_group_id == 2) {
  105. $res = $this->model
  106. ->where($where)
  107. ->where("status", 'in', [1, 4])
  108. ->order($order)
  109. ->paginate($limit);
  110. } else if ($user_group_id == 3) {
  111. $res = $this->model
  112. ->where($where)
  113. ->where("status", 'in', [1, 4])
  114. ->where('college_leader_id', $user_id)
  115. ->order($order)
  116. ->paginate($limit);
  117. } else if ($user_group_id == 4) {
  118. $res = $this->model
  119. ->where($where)
  120. ->where("status", 'in', [1, 4])
  121. ->where('user_id', $user_id)
  122. ->order($order)
  123. ->paginate($limit);
  124. }
  125. $this->success('', [
  126. 'list' => $res->items(),
  127. 'total' => $res->total(),
  128. 'remark' => get_route_remark(),
  129. ]);
  130. }
  131. public function consoleInformation(): void
  132. {
  133. $user_id = $this->auth->id;
  134. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  135. $res = [
  136. 'damage_num' => Db::name('asset')->where("status", 2)->count(),
  137. 'asset_count' => Db::name('asset')->count(),
  138. 'asset_lent' => Db::name('asset')->where('status', 1)->count(),
  139. 'asset_value' => Db::name('asset')->sum('unit_price'),
  140. 'damage_people' => Db::name('asset')->where("status", 2)->count(),
  141. 'damage_nature' => Db::name('asset')->where("status", 2)->count(),
  142. 'apply' => $this->model->where("status", 0)->count(),
  143. 'useing' => $this->model->where("status", 2)->count(),
  144. 'overdue' => $this->model->where("status", 5)->count(),
  145. ];
  146. $this->success('', $res);
  147. }
  148. public function edit($id = null): void
  149. {
  150. if ($this->request->isPost()) {
  151. $data = $this->request->post()['data'];
  152. $this->BorrowTools->checkRules($data);
  153. $data = $this->BorrowTools->checkText($data);
  154. if ($data['status'] == 1 || $data['status'] == 7 ) {
  155. if(!isset($data['annotation'])){
  156. $this->error("驳回时请填写批注!");
  157. }
  158. }
  159. if (!(array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '')) {
  160. $this->error("请检查仪器状态");
  161. }
  162. $data = $this->excludeFields($data);
  163. //获取用户类型
  164. $user_id = $this->auth->id;
  165. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  166. $this->model->startTrans();
  167. $result = false;
  168. //判断是否是科研类借取并判断是否需要领导审核
  169. try {
  170. // 调整仪器的变化
  171. $this->BorrowTools->checkAsset($data);
  172. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  173. if ($open_id !== null) {
  174. $text = [
  175. "openid" => $open_id,
  176. "pagepath" => "pages/login/index",
  177. "data" => [
  178. 'character_string1' => $data['id'],
  179. 'thing5' => $this->auth->nickname,
  180. ]
  181. ];
  182. }
  183. //判断是否是科研类借取并判断是否需要领导审核
  184. if ($data['purpose'] == 0) {
  185. $text['data']['const3'] = '科研借单';
  186. if ($user_group_id !== 3) {
  187. $data['approval_person'] = $this->auth->nickname;
  188. $data['approval_person_id'] = $this->auth->id;
  189. if ($data['status'] == 6) {
  190. $text['data']['const4'] = ['审批通过'];
  191. $data['status'] = 6;
  192. } else if ($data['status'] == 1) {
  193. $text['data']['const4'] = ['已驳回'];
  194. $data['status'] = 1;
  195. }
  196. try {
  197. $result = $this->model->update($data);
  198. $this->model->commit();
  199. } catch (Throwable $e) {
  200. $this->model->rollback();
  201. $this->error($e->getMessage());
  202. }
  203. } else {
  204. $data['college_leader_id'] = $user_id;
  205. $data['college_leader'] = $this->auth->nickname;
  206. if ($data['status'] == '1') {
  207. $text['data']['const4'] = ['已驳回'];
  208. $data['status'] = 7;
  209. } else {
  210. $text['data']['const4'] = ['审批通过'];
  211. }
  212. $result = false;
  213. $this->model->startTrans();
  214. try {
  215. $result = $this->model->update($data);
  216. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  217. // $num = count($data['asset']);
  218. foreach ($data['asset'] as $v) {
  219. for ($i = 0; $i < $v['num']; $i++) {
  220. $this->BorrowAccess->insert(
  221. [
  222. 'asset_name' => $v['model'],
  223. 'origin' => $v['origin'],
  224. 'borrow_id' => $data['id'],
  225. 'status' => -1,
  226. ]
  227. );
  228. }
  229. }
  230. }
  231. $this->model->commit();
  232. } catch (Throwable $e) {
  233. $this->model->rollback();
  234. $this->error($e->getMessage());
  235. }
  236. }
  237. } else {
  238. $data['approval_person'] = $this->auth->nickname;
  239. $data['approval_person_id'] = $this->auth->id;
  240. $text['data']['const3'] = ['教学借单'];
  241. if ($data['status'] == 2) {
  242. $text['data']['const4'] = ['审批通过'];
  243. } else if ($data['status'] == 1) {
  244. $text['data']['const4'] = ['已驳回'];
  245. }
  246. $result = $this->model->update($data);
  247. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  248. foreach ($data['asset'] as $v) {
  249. for ($i = 0; $i < $v['num']; $i++) {
  250. $this->BorrowAccess->insert(
  251. [
  252. 'asset_name' => $v['model'],
  253. 'origin' => $v['origin'],
  254. 'borrow_id' => $data['id'],
  255. 'status' => -1,
  256. ]
  257. );
  258. }
  259. }
  260. }
  261. }
  262. // 提交事务
  263. $this->model->commit();
  264. } catch (Throwable $e) {
  265. // 回滚事务
  266. $this->model->rollback();
  267. $this->error($e->getMessage());
  268. }
  269. if ($result !== false) {
  270. // $this->success(__('Update successful'));
  271. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  272. if ($open_id !== null) {
  273. // halt($text);
  274. $res = self::wxResultMessage($text);
  275. if ($res === true) {
  276. $this->success(__('Update successful'));
  277. } else {
  278. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  279. }
  280. }else{
  281. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  282. }
  283. } else {
  284. $this->error(__('No rows updated'));
  285. }
  286. }
  287. $row = $this->model->find($id);
  288. if (!$row) {
  289. $this->error(__('Record not found'));
  290. }
  291. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) {
  292. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  293. } else {
  294. $rows = $this->BorrowAccess->field('a.*,s.student_name,a.asset_name as model')->alias('a')->leftjoin('student s', 'a.student_id = s.id')->where('borrow_id', $id)->select();
  295. }
  296. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  297. $row['asset'] = $rows;
  298. $row['borrowImg'] = Db::name('borrow_img')->where('borrow_id', $id)->select();
  299. $this->success('', [
  300. 'row' => $row
  301. ]);
  302. }
  303. public function add(): void
  304. {
  305. if ($this->request->isPost()) {
  306. $data = $this->request->post()['data'];
  307. $this->BorrowTools->checkRules($data);
  308. $data = $this->BorrowTools->checkText($data);
  309. $asset_userArr = [];
  310. $admin_idArr = [];
  311. $open_idArr = [];
  312. //获取消息推送目标人
  313. foreach ($data['asset'] as $k => $v){
  314. $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user');
  315. $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id');
  316. $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid');
  317. if(!in_array($asset_user,$asset_userArr)){
  318. $asset_userArr[] = $asset_user;
  319. }
  320. if(!in_array($admin_id,$admin_idArr)){
  321. $admin_idArr[] = $admin_id;
  322. }
  323. if(!in_array($open_id,$open_idArr)){
  324. $open_idArr[] = $open_id;
  325. }
  326. }
  327. $data['approval_person_id'] = implode(',',$admin_idArr);
  328. $data['approval_person'] = implode(',',$asset_userArr);
  329. //校验数据
  330. $uniID = new SnowflakeId;
  331. $data['user_id'] = $this->auth->id;
  332. $this->model->startTrans();
  333. //尝试存储
  334. try {
  335. $data['status'] = 0;
  336. $data['encoding'] = $uniID->generateParticle();
  337. if ($open_idArr !== [] && $open_idArr !== null) {
  338. $text = [
  339. "pagepath" => "pages/login/index",
  340. "data" => [
  341. 'character_string7' => $this->model->id,
  342. 'thing4' => $data['username']
  343. ]
  344. ];
  345. }
  346. if ($data['purpose'] == 0) {
  347. $text['data']['const6'] = ['科研借单'];
  348. $data['college_leader'] = Db::name('admin')->where('switch',1)->value('nickname');
  349. } else {
  350. $text['data']['const6'] = ['教学借单'];
  351. }
  352. $result = $this->model->save($data);
  353. $data['accessories']['borrow_id'] = $this->model->id;
  354. Db::name('accessories')->insert($data['accessories']);
  355. $groupAccess = [];
  356. foreach ($data['asset'] as $datum) {
  357. if ($datum['model'] !== '') {
  358. $groupAccess[] = [
  359. 'borrow_id' => $this->model->id,
  360. 'asset_name' => $datum['model'],
  361. 'origin' => $datum['origin'],
  362. 'num' => $datum['num']
  363. ];
  364. }
  365. }
  366. // halt($result);
  367. $this->BorrowNumber->saveAll($groupAccess);
  368. $this->model->commit();
  369. } catch (Throwable $e) {
  370. // 回滚事务
  371. $this->model->rollback();
  372. $this->error($e->getMessage());
  373. }
  374. if ($result !== false) {
  375. if ($open_idArr !== [] && $open_idArr !== null) {
  376. foreach ($open_idArr as $k => $v){
  377. $text['open_id'] = $open_idArr[$k];
  378. if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) {
  379. // halt($text);
  380. $res = self::wxAuditMessage($text);
  381. }
  382. }
  383. if (isset($res)) {
  384. $this->success(__('Update successful'));
  385. } else {
  386. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  387. }
  388. }else{
  389. // halt(2);
  390. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  391. }
  392. } else {
  393. // halt(3);
  394. $this->error(__('No rows were added'));
  395. }
  396. }
  397. else {
  398. $res = $this->model->where('user_id',$this->auth->id)->find();
  399. $this->success('',$res);
  400. }
  401. }
  402. public function editPassword(): void
  403. {
  404. if ($this->request->isPost()) {
  405. $data = $this->request->param()['data'];
  406. if (!$data) {
  407. $this->error(__('Parameter %s can not be empty', ['']));
  408. }
  409. $result = $this->adminModel->resetPassword($this->auth->id, $data['_value']);
  410. if ($result !== false) {
  411. $this->success(__('Update successful'));
  412. } else {
  413. $this->error(__('No rows updated'));
  414. }
  415. }
  416. }
  417. /**
  418. * 仪器预约待审核通知
  419. * @param
  420. * @return void
  421. * @throws \Exception
  422. */
  423. public function wxAuditMessage($res = null): bool
  424. {
  425. $openid = $res["open_id"];
  426. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  427. $data = [
  428. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  429. 'thing4' => ['value' => $res["data"]["thing4"]],
  430. 'const6' => ['value' => $res["data"]["const6"]],
  431. 'time9' => ['value' => date('Y-m-d H:i')],
  432. ];
  433. $url = 'http://weixin.qq.com/download';
  434. $miniprogram = [
  435. 'appid' => 'wx58633590ab59b7a1',
  436. 'pagepath' => ''
  437. ];
  438. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  439. return $result;
  440. }
  441. public function getProtocol(){
  442. $data = Db::name('protocol')->where("switch",1)->value('editor');
  443. if($data){
  444. $this->success('', [
  445. 'data' => $data,
  446. 'data1' => htmlspecialchars_decode($data)
  447. ]);
  448. }else{
  449. $this->error("失败!");
  450. }
  451. }
  452. /**
  453. * 仪器预约审核结果通知
  454. * @param
  455. * @return void
  456. * @throws \Exception
  457. */
  458. public function wxResultMessage($res): bool
  459. {
  460. $openid = $res["openid"];
  461. // halt($res);
  462. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  463. $data = [
  464. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  465. 'const3' => ['value' => [$res["data"]["const3"]]],
  466. 'const4' => ['value' => $res["data"]["const4"]],
  467. 'thing5' => ['value' => $res["data"]["thing5"]],
  468. // 'time9' => ['value' => date('Y-m-d H:i')],
  469. ];
  470. $url = 'http://weixin.qq.com/download';
  471. $miniprogram = [
  472. 'appid' => 'wx58633590ab59b7a1',
  473. 'pagepath' => "pages/login/index"
  474. ];
  475. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  476. return $result;
  477. }
  478. // public function (): void
  479. // {
  480. // if ($this->request->param('select')) {
  481. // $this->select();
  482. // }
  483. // $user_id = $this->auth->id;
  484. // $user_group_id = Db::name('admin_group_access')->where('uid',$user_id)->value("group_id");
  485. // $res = [
  486. // 'damage_num' => Db::name('asset')->where("status",2)->count(),
  487. // 'damage_people' => Db::name('asset')->where("status",2)->where("damage_type",1)->count(),
  488. // 'damage_nature'=> Db::name('asset')->where("status",2)->where("damage_type",0)->count(),
  489. // 'apply'=> $this->model->where("status",0)->count(),
  490. // 'useing'=> $this->model->where("status",2)->count(),
  491. // 'overdue'=> $this->model->where("status",5)->count(),
  492. // ];
  493. // $this->success('', $res);
  494. // }
  495. }