BorrowApplication.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 app\common\library\xmwechat\offiaccount\OaService;
  8. use ba\Random;
  9. use modules\notification\Notification;
  10. use think\facade\Db;
  11. use app\common\controller\SnowflakeId;
  12. use Throwable;
  13. class BorrowApplication extends Backend
  14. {
  15. protected array|string $quickSearchField = ['username', 'mobile'];
  16. public function initialize(): void
  17. {
  18. parent::initialize();
  19. $this->model = new BorrowModel;
  20. $this->BorrowAccess = new BorrowAccess();
  21. $this->BorrowNumber = new BorrowNumber();
  22. $this->BorrowTools = new BorrowTools($this->app);
  23. }
  24. public function index(): void
  25. {
  26. if ($this->request->param('select')) {
  27. $this->select();
  28. }
  29. $user_id = $this->auth->id;
  30. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  31. list($where, $alias, $limit, $order) = $this->queryBuilder();
  32. // halt($where, $alias, $limit, $order);
  33. if ($user_group_id == 1 || $user_group_id == 2) {
  34. $res = $this->model
  35. ->alias($alias)
  36. ->where($where)
  37. ->where("status", 0)
  38. ->order($order)
  39. ->paginate($limit);
  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. ->paginate($limit);
  48. }
  49. $this->success('', [
  50. 'list' => $res->items(),
  51. 'total' => $res->total(),
  52. 'remark' => get_route_remark(),
  53. ]);
  54. }
  55. // public function add(): void
  56. // {
  57. // if ($this->request->isPost()) {
  58. // $data = $this->request->post();
  59. // if (!$data) {
  60. // $this->error(__('Parameter %s can not be empty', ['']));
  61. // }
  62. // if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null) {
  63. // $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
  64. // }
  65. // if (array_key_exists('remarks', $data) && $data['remarks'] !== null) {
  66. // $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
  67. // }
  68. //
  69. // if(!isset($data['asset']) || $data['asset'] == []){
  70. // $this->error("请先填写借用仪器!!");
  71. // }else{
  72. // $isHasAsset = false;
  73. // foreach ($data['asset'] as $datum) {
  74. // if($datum['model'] !== '' && $datum['num'] !== 0){
  75. // $isHasAsset = true;
  76. // }
  77. // }
  78. // if (!$isHasAsset){
  79. // $this->error("请先填写借用仪器!!");
  80. // }
  81. // }
  82. // //校验数据
  83. // $uniID = new SnowflakeId;
  84. // $data['user_id'] = $this->auth->id;
  85. //
  86. // //尝试存储
  87. // try {
  88. // $data['status'] = 0;
  89. // $data['encoding'] = $uniID->generateParticle();
  90. // // halt($data);
  91. // $result = $this->model->save($data);
  92. // if (array_key_exists('asset', $data)) {
  93. // $groupAccess = [];
  94. // foreach ($data['asset'] as $datum) {
  95. // $groupAccess[] = [
  96. // 'borrow_id' => $this->model->id,
  97. // 'asset_name' => $datum['model'],
  98. // 'origin' => $datum['origin'],
  99. // 'num' => $datum['num']
  100. // ];
  101. // }
  102. // $this->BorrowNumber->saveAll($groupAccess);
  103. // }
  104. // $this->model->commit();
  105. // } catch (Throwable $e) {
  106. // $this->model->rollback();
  107. // $this->error($e->getMessage());
  108. // }
  109. //
  110. // if ($result !== false) {
  111. // $this->success(__('Added successfully'));
  112. // } else {
  113. // $this->error(__('No rows were added'));
  114. // }
  115. // }
  116. // $this->error(__('Parameter error'));
  117. // }
  118. public function del($ids = null): void
  119. {
  120. if (!$this->request->isDelete() || !$ids) {
  121. $this->error(__('Parameter error'));
  122. }
  123. $pk = $this->model->getPk();
  124. $data = $this->model->where($pk, 'in', $ids)->select();
  125. $count = 0;
  126. $this->model->startTrans();
  127. try {
  128. foreach ($data as $v) {
  129. $count += $v->delete();
  130. $this->BorrowAccess->where('borrow_id', 'in', $ids)->select();
  131. Db::name('borrow_number')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  132. Db::name('borrow_access')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  133. }
  134. $this->model->commit();
  135. } catch (Throwable $e) {
  136. $this->model->rollback();
  137. $this->error($e->getMessage());
  138. }
  139. if ($count) {
  140. $this->success(__('Deleted successfully'));
  141. } else {
  142. $this->error(__('No rows were deleted'));
  143. }
  144. }
  145. public function edit($id = null): void
  146. {
  147. $row = $this->model->find($id);
  148. if (!$row) {
  149. $this->error(__('Record not found'));
  150. }
  151. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) {
  152. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  153. } else {
  154. $rows = $this->BorrowAccess->field('*,asset_name as model')->where('borrow_id', $id)->select();
  155. }
  156. $row['asset'] = $rows;
  157. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  158. if ($this->request->isPost()) {
  159. $data = $this->request->post();
  160. $data = $this->BorrowTools->checkText($data);
  161. $data = $this->excludeFields($data);
  162. //获取用户类型
  163. $user_id = $this->auth->id;
  164. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  165. if($user_id == $data['user_id'] && $user_group_id == 4){
  166. $data['annotation'] = '';
  167. $data['college_annotation'] = '';
  168. }
  169. $open_id = Db::name('oauth_log')->where('user_id', $data['user_id'])->value('opid');
  170. if ($open_id !== null) {
  171. $text = [
  172. "openid" => $open_id,
  173. "pagepath" => "pages/login/index",
  174. "data" => [
  175. 'character_string1' => $id,
  176. 'thing5' => $this->auth->nickname,
  177. ]
  178. ];
  179. }
  180. $this->model->startTrans();
  181. $result = false;
  182. try {
  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'] == 2) {
  190. $text['data']['const4'] = ['审批通过'];
  191. $data['status'] = 6;
  192. } else if ($data['status'] == 1) {
  193. $text['data']['const4'] = ['审批未通过'];
  194. $data['status'] = 1;
  195. }
  196. $result = $this->model->update($data);
  197. } else {
  198. $data['college_leader_id'] = $user_id;
  199. $data['college_leader'] = $this->auth->nickname;
  200. if ($data['status'] == '1') {
  201. $text['data']['const4'] = ['审批未通过'];
  202. $data['status'] = 7;
  203. } else {
  204. $text['data']['const4'] = ['审批通过'];
  205. }
  206. $result = $this->model->update($data);
  207. if ($data['status'] == 2) {
  208. foreach ($data['asset'] as $v) {
  209. for ($i = 0; $i < $v['num']; $i++) {
  210. $this->BorrowAccess->insert(
  211. [
  212. 'asset_name' => $v['model'],
  213. 'origin' => $v['origin'],
  214. 'borrow_id' => $data['id'],
  215. 'status' => -1,
  216. ]
  217. );
  218. }
  219. }
  220. }
  221. }
  222. } else {
  223. $data['approval_person'] = $this->auth->nickname;
  224. $data['approval_person_id'] = $this->auth->id;
  225. $text['data']['const3'] = ['教学借单'];
  226. if ($data['status'] == 2) {
  227. $text['data']['const4'] = ['审批通过'];
  228. } else if ($data['status'] == 1) {
  229. $text['data']['const4'] = ['审批未通过'];
  230. }
  231. $result = $this->model->update($data);
  232. if ($data['status'] == 2) {
  233. foreach ($data['asset'] as $v) {
  234. for ($i = 0; $i < $v['num']; $i++) {
  235. $this->BorrowAccess->insert(
  236. [
  237. 'asset_name' => $v['model'],
  238. 'origin' => $v['origin'],
  239. 'borrow_id' => $data['id'],
  240. 'status' => -1,
  241. ]
  242. );
  243. }
  244. }
  245. }
  246. }
  247. // 提交事务
  248. $this->model->commit();
  249. } catch (Throwable $e) {
  250. // 回滚事务
  251. $this->model->rollback();
  252. $this->error($e->getMessage());
  253. }
  254. if ($result !== false) {
  255. $open_id = Db::name('oauth_log')->where('user_id', $data['user_id'])->value('opid');
  256. if ($open_id !== null) {
  257. // halt($text);
  258. $res = self::wxResultMessage($text);
  259. if ($res === true) {
  260. $this->success(__('Update successful'));
  261. } else {
  262. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  263. }
  264. }else{
  265. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  266. }
  267. } else {
  268. $this->error(__('No rows updated'));
  269. }
  270. }
  271. $this->success('', [
  272. 'row' => $row
  273. ]);
  274. }
  275. /**
  276. * 仪器预约审核结果通知
  277. * @param
  278. * @return void
  279. * @throws \Exception
  280. */
  281. public function wxResultMessage($res): bool
  282. {
  283. $openid = $res["openid"];
  284. // halt($res);
  285. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  286. $data = [
  287. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  288. 'const3' => ['value' => [$res["data"]["const3"]]],
  289. 'const4' => ['value' => $res["data"]["const4"]],
  290. 'thing5' => ['value' => $res["data"]["thing5"]],
  291. // 'time9' => ['value' => date('Y-m-d H:i')],
  292. ];
  293. $url = 'http://weixin.qq.com/download';
  294. $miniprogram = [
  295. 'appid' => 'wx58633590ab59b7a1',
  296. 'pagepath' => "pages/login/index"
  297. ];
  298. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  299. return $result;
  300. }
  301. }