BorrowRecord.php 14 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 ba\Random;
  8. use think\facade\Db;
  9. use app\common\controller\SnowflakeId;
  10. use Throwable;
  11. class BorrowRecord extends Backend
  12. {
  13. protected array|string $quickSearchField = ['username', 'mobile'];
  14. public function initialize(): void
  15. {
  16. parent::initialize();
  17. $this->model = new BorrowModel;
  18. $this->BorrowAccess = new BorrowAccess();
  19. $this->BorrowNumber = new BorrowNumber();
  20. }
  21. public function index(): void
  22. {
  23. if ($this->request->param('select')) {
  24. $this->select();
  25. }
  26. $user_id = $this->auth->id;
  27. $user_group_id = Db::name('admin_group_access')->where('uid',$user_id)->value("group_id");
  28. list($where, $alias, $limit, $order) = $this->queryBuilder();
  29. // halt($where, $alias, $limit, $order);
  30. if($user_group_id == 1 || $user_group_id == 2){
  31. $res = $this->model
  32. ->alias($alias)
  33. ->where($where)
  34. ->where("status",'in',[1,4])
  35. ->where("approval_person_id",$this->auth->id)
  36. ->order($order)
  37. ->paginate($limit);
  38. }else if($user_group_id == 3){
  39. $res = $this->model
  40. ->alias($alias)
  41. ->where($where)
  42. ->where("status",'in',[4,7])
  43. ->where('college_leader_id',$user_id)
  44. ->order($order)
  45. ->paginate($limit);
  46. }else if($user_group_id == 4){
  47. $res = $this->model
  48. ->alias($alias)
  49. ->where($where)
  50. ->where("status",'in',[4])
  51. ->where('user_id',$user_id)
  52. ->order($order)
  53. ->paginate($limit);
  54. }
  55. $this->success('', [
  56. 'list' => $res->items(),
  57. 'total' => $res->total(),
  58. 'remark' => get_route_remark(),
  59. ]);
  60. }
  61. public function add(): void
  62. {
  63. if ($this->request->isPost()) {
  64. $data = $this->request->post();
  65. if (!$data) {
  66. $this->error(__('Parameter %s can not be empty', ['']));
  67. }
  68. if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null ) {
  69. $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
  70. }
  71. if (array_key_exists('remarks', $data) && $data['remarks'] !== null) {
  72. $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
  73. }
  74. if(!isset($data['asset']) || $data['asset'] == []){
  75. $this->error("仪器选择不能为空!!");
  76. }
  77. $data = $this->excludeFields($data);
  78. //获取用户类型
  79. $this->model->startTrans();
  80. $result = false;
  81. try {
  82. $uniID = new SnowflakeId;
  83. $data['user_id'] = $this->auth->id;
  84. $data['encoding'] = $uniID->generateParticle();
  85. $result = $this->model->save($data);
  86. // 调整仪器
  87. if (array_key_exists('asset', $data) && !empty($data['asset'])) {
  88. $toInsert = [];
  89. foreach ($data['asset'] as $datum) {
  90. // 数据不存在,准备插入项
  91. $toInsert[] = [
  92. 'borrow_id' => $this->model->id,
  93. 'asset_name' => $datum['model'],
  94. 'asset_id' => $datum['asset_id'],
  95. 'origin' => $datum['origin'],
  96. 'status' => $data['status'],
  97. 'collection_time' => $datum['collection_time'],
  98. 'lend_processor' => $datum['lend_processor'],
  99. 'restitution_processor' => $datum['restitution_processor'],
  100. 'restitution_time' => $datum['restitution_time'],
  101. ];
  102. }
  103. // 插入新增的数据
  104. if (!empty($toInsert)) {
  105. $this->BorrowAccess->insertAll($toInsert);
  106. }
  107. }
  108. $groupImg = [];
  109. if(isset($data['fileList'])){
  110. foreach ($data['fileList'] as $datum) {
  111. $groupImg[] = [
  112. 'borrow_id' => $this->model->id,
  113. 'url' => $datum['sign'],
  114. ];
  115. }
  116. Db::name('borrow_img')->insertAll($groupImg);
  117. }
  118. // 提交事务
  119. $this->model->commit();
  120. } catch (Throwable $e) {
  121. // 回滚事务
  122. $this->model->rollback();
  123. $this->error($e->getMessage());
  124. }
  125. if ($result !== false) {
  126. $this->success(__('Update successful'));
  127. } else {
  128. $this->error(__('No rows updated'));
  129. }
  130. }
  131. $this->error(__('Parameter error'));
  132. }
  133. public function del($ids = null): void
  134. {
  135. if (!$this->request->isDelete() || !$ids) {
  136. $this->error(__('Parameter error'));
  137. }
  138. $pk = $this->model->getPk();
  139. $data = $this->model->where($pk, 'in', $ids)->select();
  140. $count = 0;
  141. $this->model->startTrans();
  142. try {
  143. foreach ($data as $v) {
  144. $count += $v->delete();
  145. $this->BorrowAccess->where('borrow_id', 'in', $ids)->select();
  146. Db::name('borrow_number')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  147. Db::name('borrow_access')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  148. }
  149. $this->model->commit();
  150. } catch (Throwable $e) {
  151. $this->model->rollback();
  152. $this->error($e->getMessage());
  153. }
  154. if ($count) {
  155. $this->success(__('Deleted successfully'));
  156. } else {
  157. $this->error(__('No rows were deleted'));
  158. }
  159. }
  160. public function edit($id = null): void
  161. {
  162. $row = $this->model->find($id);
  163. if (!$row) {
  164. $this->error(__('Record not found'));
  165. }
  166. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6 ) {
  167. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id',$id)->select();
  168. } else {
  169. $rows = $this->BorrowAccess->field('*,asset_name as model')->where('borrow_id',$id)->select();
  170. }
  171. $row['asset'] = $rows;
  172. $row['fileList'] = Db::name('borrow_img')->where('borrow_id',$id)->field('*,url as sign')->select();
  173. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  174. if ($this->request->isPost()) {
  175. $data = $this->request->post();
  176. if (!$data) {
  177. $this->error(__('Parameter %s can not be empty', ['']));
  178. }
  179. if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null ) {
  180. $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
  181. }
  182. if (array_key_exists('remarks', $data) && $data['remarks'] !== null) {
  183. $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
  184. }
  185. if(!isset($data['asset']) || $data['asset'] == []){
  186. $this->error("仪器选择不能为空!!");
  187. }
  188. $data = $this->excludeFields($data);
  189. //获取用户类型
  190. $user_id = $this->auth->id;
  191. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  192. $this->model->startTrans();
  193. $result = false;
  194. try {
  195. // 调整仪器的变化
  196. if (array_key_exists('asset', $data) && !empty($data['asset'])) {
  197. // 首先检索当前所有相关记录
  198. $existingRecords = Db::name('borrow_access')
  199. ->where('borrow_id', $data['id'])
  200. ->column('*', 'id');
  201. $toUpdate = [];
  202. $toInsert = [];
  203. foreach ($data['asset'] as $datum) {
  204. if (isset($datum['id'], $existingRecords[$datum['id']])) {
  205. // 对比现有数据与新数据
  206. $current = $existingRecords[$datum['id']];
  207. if ($current['asset_name'] !== $datum['model']|| $current['asset_id'] !== $datum['asset_id']
  208. || $current['student_id'] !== $datum['student_id'] || $current['origin'] !== $datum['origin']
  209. || $current['status'] !== $datum['status'] || $current['collection_time'] !== $datum['collection_time']
  210. || $current['lend_processor'] !== $datum['lend_processor']
  211. || $current['restitution_processor'] !== $datum['restitution_processor']|| $current['restitution_time'] !== $datum['restitution_time']
  212. ) {
  213. // 数据有变化,则准备更新
  214. $toUpdate[] = [
  215. 'id' => $datum['id'],
  216. 'borrow_id' => $data['id'],
  217. 'asset_name' => $datum['model'],
  218. 'asset_id' => $datum['asset_id'],
  219. 'student_id' => $datum['student_id'],
  220. 'origin' => $datum['origin'],
  221. 'status' => $data['status'],
  222. 'collection_time' => $datum['collection_time'],
  223. 'lend_processor' => $datum['lend_processor'],
  224. 'restitution_processor' => $datum['restitution_processor'],
  225. 'restitution_time' => $datum['restitution_time'],
  226. ];
  227. }
  228. // 从现有记录中移除已经处理过的项
  229. unset($existingRecords[$datum['id']]);
  230. } else {
  231. // 数据不存在,准备插入项
  232. $toInsert[] = [
  233. 'borrow_id' => $data['id'],
  234. 'asset_name' => $datum['model'],
  235. 'asset_id' => $datum['asset_id'],
  236. 'student_id' => $datum['student_id'],
  237. 'origin' => $datum['origin'],
  238. 'status' => $data['status'],
  239. 'collection_time' => $datum['collection_time'],
  240. 'lend_processor' => $datum['lend_processor'],
  241. 'restitution_processor' => $datum['restitution_processor'],
  242. 'restitution_time' => $datum['restitution_time'],
  243. ];
  244. }
  245. }
  246. // $existingRecords 中剩下的是需要软删除的
  247. $toDelete = array_keys($existingRecords);
  248. if (!empty($toDelete)) {
  249. Db::name('borrow_number')
  250. ->where('id', 'in', $toDelete)
  251. ->useSoftDelete('delete_time', date('Y-m-d H:i:s'))
  252. ->delete();
  253. }
  254. // 更新已变化的数据
  255. if (!empty($toUpdate)) {
  256. $this->BorrowNumber->saveAll($toUpdate);
  257. }
  258. // 插入新增的数据
  259. if (!empty($toInsert)) {
  260. $this->BorrowNumber->insertAll($toInsert);
  261. }
  262. }
  263. if (isset($data['fileList'])) {
  264. $result = Db::name('borrow_img')->where('borrow_id', $id)->delete();
  265. if($result){
  266. $groupImg = [];
  267. foreach ($data['fileList'] as $datum) {
  268. $groupImg[] = [
  269. 'borrow_id' => $data['id'],
  270. 'url' => $datum['sign'],
  271. ];
  272. }
  273. Db::name('borrow_img')->insertAll($groupImg);
  274. }
  275. }
  276. if(array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '') {
  277. if ($data['purpose'] == 0) {
  278. $this->model->startTrans();
  279. try {
  280. $result = $this->model->update($data);
  281. $this->model->commit();
  282. } catch (Throwable $e) {
  283. $this->model->rollback();
  284. $this->error($e->getMessage());
  285. }
  286. }
  287. }
  288. // 提交事务
  289. $this->model->commit();
  290. } catch (Throwable $e) {
  291. // 回滚事务
  292. $this->model->rollback();
  293. $this->error($e->getMessage());
  294. }
  295. if ($result !== false) {
  296. $this->success(__('Update successful'));
  297. } else {
  298. $this->error(__('No rows updated'));
  299. }
  300. }
  301. $this->success('', [
  302. 'row' => $row
  303. ]);
  304. }
  305. }