BorrowTools.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\admin\controller\borrow;
  3. use app\admin\model\BorrowAccess;
  4. use app\admin\model\BorrowNumber;
  5. use app\common\controller\Backend;
  6. use think\facade\Db;
  7. use Throwable;
  8. class BorrowTools extends Backend
  9. {
  10. public function initialize(): void
  11. {
  12. parent::initialize();
  13. $this->BorrowAccess = new BorrowAccess();
  14. $this->BorrowNumber = new BorrowNumber();
  15. }
  16. //防止符号转义
  17. public function checkText($data)
  18. {
  19. if (isset($data['borrow_reason'])) {
  20. $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
  21. }
  22. if (isset($data['remarks'])) {
  23. $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
  24. }
  25. return $data;
  26. }
  27. //检查提交条件
  28. public function checkRules($data)
  29. {
  30. if (!$data) {
  31. $this->error(__('Parameter %s can not be empty', ['']));
  32. }
  33. if (!isset($data['asset']) || $data['asset'] == []) {
  34. $this->error("请先填写借用仪器!!");
  35. }
  36. $isHasAsset = [];
  37. foreach ($data['asset'] as $key => $datum) {
  38. if (isset($datum['model']) && $datum['model'] !== '') {
  39. $res = Db::name('asset')->where('asset_name',$datum['model'])->find();
  40. if($res) {
  41. $isHasAsset[] = 1;
  42. }else{
  43. $isHasAsset[] = 0;
  44. }
  45. } else {
  46. $isHasAsset[] = 0;
  47. }
  48. }
  49. if (in_array('0', $isHasAsset)) {
  50. $this->error("请正确填写借用仪器类型!!");
  51. }
  52. $isHasAssetNum = [];
  53. foreach ($data['asset'] as $key => $datum) {
  54. if (isset($datum['num']) && $datum['num'] !== '' && $datum['num'] != 0) {
  55. $isHasAssetNum[] = 1;
  56. } else {
  57. $isHasAssetNum[] = 0;
  58. }
  59. }
  60. if (in_array('0', $isHasAssetNum)) {
  61. $this->error("请完整填写借用仪器数量!!");
  62. }
  63. $timestamp1 = strtotime($data['expected_end_time']);
  64. $timestamp2 = strtotime($data['borrow_time']);
  65. // halt($timestamp1,$timestamp2);
  66. if ($timestamp1 < $timestamp2) {
  67. $this->error("请仔细填写借取时间!!");
  68. }
  69. }
  70. public function applyInformation($data)
  71. {
  72. if (!isset($data['asset']) || $data['asset'] == []) {
  73. $this->error("请先填写借用仪器!!");
  74. } else {
  75. $isHasAsset = false;
  76. foreach ($data['asset'] as $datum) {
  77. if ($datum['model'] !== '' && $datum['num'] !== 0) {
  78. $isHasAsset = true;
  79. }
  80. }
  81. if (!$isHasAsset) {
  82. $this->error("请先填写借用仪器!!");
  83. }
  84. }
  85. if ($data['expected_end_time'] >= $data['borrow_time']) {
  86. $this->error("请仔细填写借取时间!!");
  87. }
  88. }
  89. public function checkAsset($data)
  90. {
  91. // 首先检索当前所有相关记录
  92. $existingRecords = Db::name('borrow_number')
  93. ->where('borrow_id', $data['id'])
  94. ->column('*', 'id');
  95. $toUpdate = [];
  96. $toInsert = [];
  97. foreach ($data['asset'] as $datum) {
  98. if (isset($datum['id'], $existingRecords[$datum['id']])) {
  99. // 对比现有数据与新数据
  100. $current = $existingRecords[$datum['id']];
  101. if ($current['asset_name'] !== $datum['model'] || $current['num'] !== $datum['num'] || $current['origin'] !== $datum['origin']) {
  102. // 数据有变化,则准备更新
  103. $toUpdate[] = [
  104. 'id' => $datum['id'],
  105. 'borrow_id' => $data['id'],
  106. 'asset_name' => $datum['model'],
  107. 'origin' => $datum['origin'],
  108. 'num' => $datum['num'],
  109. ];
  110. }
  111. // 从现有记录中移除已经处理过的项
  112. unset($existingRecords[$datum['id']]);
  113. } else {
  114. // 数据不存在,准备插入项
  115. $toInsert[] = [
  116. 'borrow_id' => $data['id'],
  117. 'asset_name' => $datum['model'],
  118. 'origin' => $datum['origin'],
  119. 'num' => $datum['num'],
  120. ];
  121. }
  122. }
  123. // $existingRecords 中剩下的是需要软删除的
  124. $toDelete = array_keys($existingRecords);
  125. if (!empty($toDelete)) {
  126. Db::name('borrow_number')
  127. ->where('id', 'in', $toDelete)
  128. ->useSoftDelete('delete_time', date('Y-m-d H:i:s'))
  129. ->delete();
  130. }
  131. // 更新已变化的数据
  132. if (!empty($toUpdate)) {
  133. $this->BorrowNumber->saveAll($toUpdate);
  134. }
  135. // 插入新增的数据
  136. if (!empty($toInsert)) {
  137. $this->BorrowNumber->insertAll($toInsert);
  138. }
  139. }
  140. public function getDateForAssetName($asset_id,$asset_name,$borrow_id)
  141. {
  142. $is_old = $this->BorrowAccess->where(["asset_id" => $asset_id, 'status' => 0])->find();
  143. if ($is_old !== null) {
  144. $this->error(__('The instrument is bound'));
  145. }
  146. $isOldAssset = Db::name('asset')->where('asset_id', $asset_id)->value('status');
  147. if ($isOldAssset == 1) {
  148. $this->error("该仪器已借出!!");
  149. }
  150. if( strpos($asset_name,'无人机') !== false){
  151. $map = [
  152. ['asset_name','like','%' . '无人机' .'%'],
  153. ];
  154. }else if( strpos($asset_name,'RTK') !== false){
  155. $map = [
  156. ['asset_name','like','%' . 'RTK' .'%'],
  157. ];
  158. }else if( strpos($asset_name,'水准仪') !== false){
  159. $map = [
  160. ['asset_name','like','%' . '水准仪' .'%'],
  161. ];
  162. }else if( strpos($asset_name,'全站仪') !== false){
  163. $map = [
  164. ['asset_name','like','%' . '全站仪' .'%'],
  165. ];
  166. }else{
  167. $map = [
  168. ['asset_name', '=' , $asset_name]
  169. ];
  170. }
  171. $data = $this->BorrowAccess
  172. ->where(['borrow_id' => $borrow_id])
  173. ->where($map)
  174. ->where('asset_id', null)
  175. ->find();
  176. return $data;
  177. }
  178. }