BorrowApplication.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. $user_id = Db::name('borrow')->where('id', $id)->value('user_id');
  158. $row['sign'] = Db::name('sign')->where('uid', $user_id)->find()['sign'];
  159. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  160. if ($this->request->isPost()) {
  161. $data = $this->request->post();
  162. $data = $this->BorrowTools->checkText($data);
  163. $data = $this->excludeFields($data);
  164. //获取用户类型
  165. $user_id = $this->auth->id;
  166. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  167. $open_id = Db::name('oauth_log')->where('user_id', $data['user_id'])->value('opid');
  168. if ($open_id !== null) {
  169. $text = [
  170. "openid" => $open_id,
  171. "pagepath" => "pages/login/index",
  172. "data" => [
  173. 'character_string1' => $id,
  174. 'thing5' => $this->auth->nickname,
  175. ]
  176. ];
  177. }
  178. $this->model->startTrans();
  179. $result = false;
  180. try {
  181. //判断是否是科研类借取并判断是否需要领导审核
  182. if ($data['purpose'] == 0) {
  183. $text['data']['const3'] = '科研借单';
  184. if ($user_group_id !== 3) {
  185. $data['approval_person'] = $this->auth->nickname;
  186. $data['approval_person_id'] = $this->auth->id;
  187. if ($data['status'] == 2) {
  188. $text['data']['const4'] = ['审批通过'];
  189. $data['status'] = 6;
  190. } else if ($data['status'] == 1) {
  191. $text['data']['const4'] = ['审批未通过'];
  192. $data['status'] = 1;
  193. }
  194. $result = $this->model->update($data);
  195. } else {
  196. $data['college_leader_id'] = $user_id;
  197. $data['college_leader'] = $this->auth->nickname;
  198. if ($data['status'] == '1') {
  199. $text['data']['const4'] = ['审批未通过'];
  200. $data['status'] = 7;
  201. } else {
  202. $text['data']['const4'] = ['审批通过'];
  203. }
  204. $result = $this->model->update($data);
  205. if ($data['status'] == 2) {
  206. foreach ($data['asset'] as $v) {
  207. for ($i = 0; $i < $v['num']; $i++) {
  208. $this->BorrowAccess->insert(
  209. [
  210. 'asset_name' => $v['model'],
  211. 'origin' => $v['origin'],
  212. 'borrow_id' => $data['id'],
  213. 'status' => -1,
  214. ]
  215. );
  216. }
  217. }
  218. }
  219. }
  220. } else {
  221. $data['approval_person'] = $this->auth->nickname;
  222. $data['approval_person_id'] = $this->auth->id;
  223. $text['data']['const3'] = ['教学借单'];
  224. if ($data['status'] == 2) {
  225. $text['data']['const4'] = ['审批通过'];
  226. } else if ($data['status'] == 1) {
  227. $text['data']['const4'] = ['审批未通过'];
  228. }
  229. $result = $this->model->update($data);
  230. if ($data['status'] == 2) {
  231. foreach ($data['asset'] as $v) {
  232. for ($i = 0; $i < $v['num']; $i++) {
  233. $this->BorrowAccess->insert(
  234. [
  235. 'asset_name' => $v['model'],
  236. 'origin' => $v['origin'],
  237. 'borrow_id' => $data['id'],
  238. 'status' => -1,
  239. ]
  240. );
  241. }
  242. }
  243. }
  244. }
  245. // 提交事务
  246. $this->model->commit();
  247. } catch (Throwable $e) {
  248. // 回滚事务
  249. $this->model->rollback();
  250. $this->error($e->getMessage());
  251. }
  252. if ($result !== false) {
  253. $open_id = Db::name('oauth_log')->where('user_id', $data['user_id'])->value('opid');
  254. if ($open_id !== null) {
  255. // halt($text);
  256. $res = self::wxResultMessage($text);
  257. if ($res === true) {
  258. $this->success(__('Update successful'));
  259. } else {
  260. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  261. }
  262. }else{
  263. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  264. }
  265. } else {
  266. $this->error(__('No rows updated'));
  267. }
  268. }
  269. $this->success('', [
  270. 'row' => $row
  271. ]);
  272. }
  273. /**
  274. * 仪器预约审核结果通知
  275. * @param
  276. * @return void
  277. * @throws \Exception
  278. */
  279. public function wxResultMessage($res): bool
  280. {
  281. $openid = $res["openid"];
  282. // halt($res);
  283. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  284. $data = [
  285. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  286. 'const3' => ['value' => [$res["data"]["const3"]]],
  287. 'const4' => ['value' => $res["data"]["const4"]],
  288. 'thing5' => ['value' => $res["data"]["thing5"]],
  289. // 'time9' => ['value' => date('Y-m-d H:i')],
  290. ];
  291. $url = 'http://weixin.qq.com/download';
  292. $miniprogram = [
  293. 'appid' => 'wx58633590ab59b7a1',
  294. 'pagepath' => "pages/login/index"
  295. ];
  296. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  297. return $result;
  298. }
  299. }