BorrowApp.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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['sign'] = Db::name('borrow_img')->where('borrow_id', $id)->select();
  299. $row['usersign'] = Db::name('sign')->where('uid', $this->auth->id)->value('sign');
  300. $this->success('', [
  301. 'row' => $row
  302. ]);
  303. }
  304. public function add(): void
  305. {
  306. if ($this->request->isPost()) {
  307. $data = $this->request->post()['data'];
  308. $this->BorrowTools->checkRules($data);
  309. $data = $this->BorrowTools->checkText($data);
  310. $asset_userArr = [];
  311. $admin_idArr = [];
  312. $open_idArr = [];
  313. //获取消息推送目标人
  314. foreach ($data['asset'] as $k => $v){
  315. $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user');
  316. $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id');
  317. $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid');
  318. if(!in_array($asset_user,$asset_userArr)){
  319. $asset_userArr[] = $asset_user;
  320. }
  321. if(!in_array($admin_id,$admin_idArr)){
  322. $admin_idArr[] = $admin_id;
  323. }
  324. if(!in_array($open_id,$open_idArr)){
  325. $open_idArr[] = $open_id;
  326. }
  327. }
  328. $data['approval_person_id'] = implode(',',$admin_idArr);
  329. $data['approval_person'] = implode(',',$asset_userArr);
  330. //校验数据
  331. $uniID = new SnowflakeId;
  332. $data['user_id'] = $this->auth->id;
  333. $this->model->startTrans();
  334. //尝试存储
  335. try {
  336. $data['status'] = 0;
  337. $data['encoding'] = $uniID->generateParticle();
  338. if ($open_idArr !== [] && $open_idArr !== null) {
  339. $text = [
  340. "pagepath" => "pages/login/index",
  341. "data" => [
  342. 'character_string7' => $this->model->id,
  343. 'thing4' => $data['username']
  344. ]
  345. ];
  346. }
  347. if ($data['purpose'] == 0) {
  348. $text['data']['const6'] = ['科研借单'];
  349. $data['college_leader'] = Db::name('admin')->where('switch',1)->value('nickname');
  350. } else {
  351. $text['data']['const6'] = ['教学借单'];
  352. }
  353. $result = $this->model->save($data);
  354. $data['accessories']['borrow_id'] = $this->model->id;
  355. Db::name('accessories')->insert($data['accessories']);
  356. $groupAccess = [];
  357. foreach ($data['asset'] as $datum) {
  358. if ($datum['model'] !== '') {
  359. $groupAccess[] = [
  360. 'borrow_id' => $this->model->id,
  361. 'asset_name' => $datum['model'],
  362. 'origin' => $datum['origin'],
  363. 'num' => $datum['num']
  364. ];
  365. }
  366. }
  367. // halt($result);
  368. $this->BorrowNumber->saveAll($groupAccess);
  369. $this->model->commit();
  370. } catch (Throwable $e) {
  371. // 回滚事务
  372. $this->model->rollback();
  373. $this->error($e->getMessage());
  374. }
  375. if ($result !== false) {
  376. if ($open_idArr !== [] && $open_idArr !== null) {
  377. foreach ($open_idArr as $k => $v){
  378. $text['open_id'] = $open_idArr[$k];
  379. if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) {
  380. // halt($text);
  381. $res = self::wxAuditMessage($text);
  382. }
  383. }
  384. if (isset($res)) {
  385. $this->success(__('Update successful'));
  386. } else {
  387. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  388. }
  389. }else{
  390. // halt(2);
  391. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  392. }
  393. } else {
  394. // halt(3);
  395. $this->error(__('No rows were added'));
  396. }
  397. }
  398. else {
  399. $res = $this->model->where('user_id',$this->auth->id)->find();
  400. $this->success('',$res);
  401. }
  402. }
  403. public function editPassword(): void
  404. {
  405. if ($this->request->isPost()) {
  406. $data = $this->request->param()['data'];
  407. if (!$data) {
  408. $this->error(__('Parameter %s can not be empty', ['']));
  409. }
  410. $result = $this->adminModel->resetPassword($this->auth->id, $data['_value']);
  411. if ($result !== false) {
  412. $this->success(__('Update successful'));
  413. } else {
  414. $this->error(__('No rows updated'));
  415. }
  416. }
  417. }
  418. /**
  419. * 仪器预约待审核通知
  420. * @param
  421. * @return void
  422. * @throws \Exception
  423. */
  424. public function wxAuditMessage($res = null): bool
  425. {
  426. $openid = $res["open_id"];
  427. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  428. $data = [
  429. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  430. 'thing4' => ['value' => $res["data"]["thing4"]],
  431. 'const6' => ['value' => $res["data"]["const6"]],
  432. 'time9' => ['value' => date('Y-m-d H:i')],
  433. ];
  434. $url = 'http://weixin.qq.com/download';
  435. $miniprogram = [
  436. 'appid' => 'wx58633590ab59b7a1',
  437. 'pagepath' => ''
  438. ];
  439. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  440. return $result;
  441. }
  442. public function getProtocol(){
  443. $data = Db::name('protocol')->where("switch",1)->value('editor');
  444. if($data){
  445. $this->success('', [
  446. 'data' => $data,
  447. 'data1' => htmlspecialchars_decode($data)
  448. ]);
  449. }else{
  450. $this->error("失败!");
  451. }
  452. }
  453. /**
  454. * 仪器预约审核结果通知
  455. * @param
  456. * @return void
  457. * @throws \Exception
  458. */
  459. public function wxResultMessage($res): bool
  460. {
  461. $openid = $res["openid"];
  462. // halt($res);
  463. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  464. $data = [
  465. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  466. 'const3' => ['value' => [$res["data"]["const3"]]],
  467. 'const4' => ['value' => $res["data"]["const4"]],
  468. 'thing5' => ['value' => $res["data"]["thing5"]],
  469. // 'time9' => ['value' => date('Y-m-d H:i')],
  470. ];
  471. $url = 'http://weixin.qq.com/download';
  472. $miniprogram = [
  473. 'appid' => 'wx58633590ab59b7a1',
  474. 'pagepath' => "pages/login/index"
  475. ];
  476. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  477. return $result;
  478. }
  479. // public function (): void
  480. // {
  481. // if ($this->request->param('select')) {
  482. // $this->select();
  483. // }
  484. // $user_id = $this->auth->id;
  485. // $user_group_id = Db::name('admin_group_access')->where('uid',$user_id)->value("group_id");
  486. // $res = [
  487. // 'damage_num' => Db::name('asset')->where("status",2)->count(),
  488. // 'damage_people' => Db::name('asset')->where("status",2)->where("damage_type",1)->count(),
  489. // 'damage_nature'=> Db::name('asset')->where("status",2)->where("damage_type",0)->count(),
  490. // 'apply'=> $this->model->where("status",0)->count(),
  491. // 'useing'=> $this->model->where("status",2)->count(),
  492. // 'overdue'=> $this->model->where("status",5)->count(),
  493. // ];
  494. // $this->success('', $res);
  495. // }
  496. }