BorrowApp.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 ) {
  155. if(!isset($data['annotation'])){
  156. $this->error("驳回时请填写批注!");
  157. }
  158. }
  159. if ( $data['status'] == 7 ) {
  160. if(!isset($data['college_annotation'])){
  161. $this->error("驳回时请填写批注!");
  162. }
  163. }
  164. if (!(array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '')) {
  165. $this->error("请检查仪器状态");
  166. }
  167. $data = $this->excludeFields($data);
  168. //获取用户类型
  169. $user_id = $this->auth->id;
  170. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  171. $this->model->startTrans();
  172. $result = false;
  173. //判断是否是科研类借取并判断是否需要领导审核
  174. try {
  175. // 调整仪器的变化
  176. $this->BorrowTools->checkAsset($data);
  177. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  178. if ($open_id !== null) {
  179. $text = [
  180. "openid" => $open_id,
  181. "pagepath" => "pages/login/index",
  182. "data" => [
  183. 'character_string1' => $data['id'],
  184. 'thing5' => $this->auth->nickname,
  185. ]
  186. ];
  187. }
  188. //判断是否是科研类借取并判断是否需要领导审核
  189. if ($data['purpose'] == 0) {
  190. $text['data']['const3'] = '科研借单';
  191. if ($user_group_id !== 3) {
  192. $data['approval_person'] = $this->auth->nickname;
  193. $data['approval_person_id'] = $this->auth->id;
  194. if ($data['status'] == 6) {
  195. $text['data']['const4'] = ['审批通过'];
  196. $data['status'] = 6;
  197. } else if ($data['status'] == 1) {
  198. $text['data']['const4'] = ['已驳回'];
  199. $data['status'] = 1;
  200. }
  201. try {
  202. $result = $this->model->update($data);
  203. $this->model->commit();
  204. } catch (Throwable $e) {
  205. $this->model->rollback();
  206. $this->error($e->getMessage());
  207. }
  208. } else {
  209. $data['college_leader_id'] = $user_id;
  210. $data['college_leader'] = $this->auth->nickname;
  211. if ($data['status'] == '1') {
  212. $text['data']['const4'] = ['已驳回'];
  213. $data['status'] = 7;
  214. } else {
  215. $text['data']['const4'] = ['审批通过'];
  216. }
  217. $result = false;
  218. $this->model->startTrans();
  219. try {
  220. $result = $this->model->update($data);
  221. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  222. // $num = count($data['asset']);
  223. foreach ($data['asset'] as $v) {
  224. for ($i = 0; $i < $v['num']; $i++) {
  225. $this->BorrowAccess->insert(
  226. [
  227. 'asset_name' => $v['model'],
  228. 'origin' => $v['origin'],
  229. 'borrow_id' => $data['id'],
  230. 'status' => -1,
  231. ]
  232. );
  233. }
  234. }
  235. }
  236. $this->model->commit();
  237. } catch (Throwable $e) {
  238. $this->model->rollback();
  239. $this->error($e->getMessage());
  240. }
  241. }
  242. } else {
  243. $data['approval_person'] = $this->auth->nickname;
  244. $data['approval_person_id'] = $this->auth->id;
  245. $text['data']['const3'] = ['教学借单'];
  246. if ($data['status'] == 2) {
  247. $text['data']['const4'] = ['审批通过'];
  248. } else if ($data['status'] == 1) {
  249. $text['data']['const4'] = ['已驳回'];
  250. }
  251. $result = $this->model->update($data);
  252. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  253. foreach ($data['asset'] as $v) {
  254. for ($i = 0; $i < $v['num']; $i++) {
  255. $this->BorrowAccess->insert(
  256. [
  257. 'asset_name' => $v['model'],
  258. 'origin' => $v['origin'],
  259. 'borrow_id' => $data['id'],
  260. 'status' => -1,
  261. ]
  262. );
  263. }
  264. }
  265. }
  266. }
  267. // 提交事务
  268. $this->model->commit();
  269. } catch (Throwable $e) {
  270. // 回滚事务
  271. $this->model->rollback();
  272. $this->error($e->getMessage());
  273. }
  274. if ($result !== false) {
  275. // $this->success(__('Update successful'));
  276. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  277. if ($open_id !== null) {
  278. // halt($text);
  279. $res = self::wxResultMessage($text);
  280. if ($res === true) {
  281. $this->success(__('Update successful'));
  282. } else {
  283. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  284. }
  285. }else{
  286. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  287. }
  288. } else {
  289. $this->error(__('No rows updated'));
  290. }
  291. }
  292. $row = $this->model->find($id);
  293. if (!$row) {
  294. $this->error(__('Record not found'));
  295. }
  296. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) {
  297. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  298. } else {
  299. $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();
  300. }
  301. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  302. $row['asset'] = $rows;
  303. $row['borrowImg'] = Db::name('borrow_img')->where('borrow_id', $id)->select();
  304. $this->success('', [
  305. 'row' => $row
  306. ]);
  307. }
  308. public function add(): void
  309. {
  310. if ($this->request->isPost()) {
  311. $data = $this->request->post()['data'];
  312. $this->BorrowTools->checkRules($data);
  313. $data = $this->BorrowTools->checkText($data);
  314. $asset_userArr = [];
  315. $admin_idArr = [];
  316. $open_idArr = [];
  317. //获取消息推送目标人
  318. foreach ($data['asset'] as $k => $v){
  319. $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user');
  320. $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id');
  321. $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid');
  322. if(!in_array($asset_user,$asset_userArr)){
  323. $asset_userArr[] = $asset_user;
  324. }
  325. if(!in_array($admin_id,$admin_idArr)){
  326. $admin_idArr[] = $admin_id;
  327. }
  328. if(!in_array($open_id,$open_idArr)){
  329. $open_idArr[] = $open_id;
  330. }
  331. }
  332. $data['approval_person_id'] = implode(',',$admin_idArr);
  333. $data['approval_person'] = implode(',',$asset_userArr);
  334. //校验数据
  335. $uniID = new SnowflakeId;
  336. $data['user_id'] = $this->auth->id;
  337. $this->model->startTrans();
  338. //尝试存储
  339. try {
  340. $data['status'] = 0;
  341. $data['encoding'] = $uniID->generateParticle();
  342. if ($open_idArr !== [] && $open_idArr !== null) {
  343. $text = [
  344. "pagepath" => "pages/login/index",
  345. "data" => [
  346. 'character_string7' => $this->model->id,
  347. 'thing4' => $data['username']
  348. ]
  349. ];
  350. }
  351. if ($data['purpose'] == 0) {
  352. $text['data']['const6'] = ['科研借单'];
  353. $data['college_leader'] = Db::name('admin')->where('switch',1)->value('nickname');
  354. } else {
  355. $text['data']['const6'] = ['教学借单'];
  356. }
  357. $result = $this->model->save($data);
  358. $data['accessories']['borrow_id'] = $this->model->id;
  359. Db::name('accessories')->insert($data['accessories']);
  360. $groupAccess = [];
  361. foreach ($data['asset'] as $datum) {
  362. if ($datum['model'] !== '') {
  363. $groupAccess[] = [
  364. 'borrow_id' => $this->model->id,
  365. 'asset_name' => $datum['model'],
  366. 'origin' => $datum['origin'],
  367. 'num' => $datum['num']
  368. ];
  369. }
  370. }
  371. // halt($result);
  372. $this->BorrowNumber->saveAll($groupAccess);
  373. $this->model->commit();
  374. } catch (Throwable $e) {
  375. // 回滚事务
  376. $this->model->rollback();
  377. $this->error($e->getMessage());
  378. }
  379. if ($result !== false) {
  380. if ($open_idArr !== [] && $open_idArr !== null) {
  381. foreach ($open_idArr as $k => $v){
  382. $text['open_id'] = $open_idArr[$k];
  383. if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) {
  384. // halt($text);
  385. $res = self::wxAuditMessage($text);
  386. }
  387. }
  388. if (isset($res)) {
  389. $this->success(__('Update successful'));
  390. } else {
  391. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  392. }
  393. }else{
  394. // halt(2);
  395. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  396. }
  397. } else {
  398. // halt(3);
  399. $this->error(__('No rows were added'));
  400. }
  401. }
  402. else {
  403. $res = $this->model->where('user_id',$this->auth->id)->find();
  404. $this->success('',$res);
  405. }
  406. }
  407. public function editPassword(): void
  408. {
  409. if ($this->request->isPost()) {
  410. $data = $this->request->param()['data'];
  411. if (!$data) {
  412. $this->error(__('Parameter %s can not be empty', ['']));
  413. }
  414. $result = $this->adminModel->resetPassword($this->auth->id, $data['_value']);
  415. if ($result !== false) {
  416. $this->success(__('Update successful'));
  417. } else {
  418. $this->error(__('No rows updated'));
  419. }
  420. }
  421. }
  422. /**
  423. * 仪器预约待审核通知
  424. * @param
  425. * @return void
  426. * @throws \Exception
  427. */
  428. public function wxAuditMessage($res = null): bool
  429. {
  430. $openid = $res["open_id"];
  431. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  432. $data = [
  433. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  434. 'thing4' => ['value' => $res["data"]["thing4"]],
  435. 'const6' => ['value' => $res["data"]["const6"]],
  436. 'time9' => ['value' => date('Y-m-d H:i')],
  437. ];
  438. $url = 'http://weixin.qq.com/download';
  439. $miniprogram = [
  440. 'appid' => 'wx58633590ab59b7a1',
  441. 'pagepath' => ''
  442. ];
  443. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  444. return $result;
  445. }
  446. public function getProtocol(){
  447. $data = Db::name('protocol')->where("switch",1)->value('editor');
  448. if($data){
  449. $this->success('', [
  450. 'data' => $data,
  451. 'data1' => htmlspecialchars_decode($data)
  452. ]);
  453. }else{
  454. $this->error("失败!");
  455. }
  456. }
  457. /**
  458. * 仪器预约审核结果通知
  459. * @param
  460. * @return void
  461. * @throws \Exception
  462. */
  463. public function wxResultMessage($res): bool
  464. {
  465. $openid = $res["openid"];
  466. // halt($res);
  467. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  468. $data = [
  469. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  470. 'const3' => ['value' => [$res["data"]["const3"]]],
  471. 'const4' => ['value' => $res["data"]["const4"]],
  472. 'thing5' => ['value' => $res["data"]["thing5"]],
  473. // 'time9' => ['value' => date('Y-m-d H:i')],
  474. ];
  475. $url = 'http://weixin.qq.com/download';
  476. $miniprogram = [
  477. 'appid' => 'wx58633590ab59b7a1',
  478. 'pagepath' => "pages/login/index"
  479. ];
  480. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  481. return $result;
  482. }
  483. // public function (): void
  484. // {
  485. // if ($this->request->param('select')) {
  486. // $this->select();
  487. // }
  488. // $user_id = $this->auth->id;
  489. // $user_group_id = Db::name('admin_group_access')->where('uid',$user_id)->value("group_id");
  490. // $res = [
  491. // 'damage_num' => Db::name('asset')->where("status",2)->count(),
  492. // 'damage_people' => Db::name('asset')->where("status",2)->where("damage_type",1)->count(),
  493. // 'damage_nature'=> Db::name('asset')->where("status",2)->where("damage_type",0)->count(),
  494. // 'apply'=> $this->model->where("status",0)->count(),
  495. // 'useing'=> $this->model->where("status",2)->count(),
  496. // 'overdue'=> $this->model->where("status",5)->count(),
  497. // ];
  498. // $this->success('', $res);
  499. // }
  500. }