123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace app\admin\controller\borrow;
- use app\admin\model\BorrowAccess;
- use app\admin\model\BorrowNumber;
- use app\common\controller\Backend;
- use think\facade\Db;
- use Throwable;
- class BorrowTools extends Backend
- {
- public function initialize(): void
- {
- parent::initialize();
- $this->BorrowAccess = new BorrowAccess();
- $this->BorrowNumber = new BorrowNumber();
- }
- //防止符号转义
- public function checkText($data)
- {
- if (isset($data['borrow_reason'])) {
- $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
- }
- if (isset($data['remarks'])) {
- $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
- }
- return $data;
- }
- //检查提交条件
- public function checkRules($data)
- {
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- if (!isset($data['asset']) || $data['asset'] == []) {
- $this->error("请先填写借用仪器!!");
- }
- $isHasAsset = [];
- foreach ($data['asset'] as $key => $datum) {
- if (isset($datum['model']) && $datum['model'] !== '') {
- $res = Db::name('asset')->where('asset_name',$datum['model'])->find();
- if($res) {
- $isHasAsset[] = 1;
- }else{
- $isHasAsset[] = 0;
- }
- } else {
- $isHasAsset[] = 0;
- }
- }
- if (in_array('0', $isHasAsset)) {
- $this->error("请正确填写借用仪器类型!!");
- }
- $isHasAssetNum = [];
- foreach ($data['asset'] as $key => $datum) {
- if (isset($datum['num']) && $datum['num'] !== '' && $datum['num'] != 0) {
- $isHasAssetNum[] = 1;
- } else {
- $isHasAssetNum[] = 0;
- }
- }
- if (in_array('0', $isHasAssetNum)) {
- $this->error("请完整填写借用仪器数量!!");
- }
- $timestamp1 = strtotime($data['expected_end_time']);
- $timestamp2 = strtotime($data['borrow_time']);
- // halt($timestamp1,$timestamp2);
- if ($timestamp1 < $timestamp2) {
- $this->error("请仔细填写借取时间!!");
- }
- }
- public function applyInformation($data)
- {
- if (!isset($data['asset']) || $data['asset'] == []) {
- $this->error("请先填写借用仪器!!");
- } else {
- $isHasAsset = false;
- foreach ($data['asset'] as $datum) {
- if ($datum['model'] !== '' && $datum['num'] !== 0) {
- $isHasAsset = true;
- }
- }
- if (!$isHasAsset) {
- $this->error("请先填写借用仪器!!");
- }
- }
- if ($data['expected_end_time'] >= $data['borrow_time']) {
- $this->error("请仔细填写借取时间!!");
- }
- }
- public function checkAsset($data)
- {
- // 首先检索当前所有相关记录
- $existingRecords = Db::name('borrow_number')
- ->where('borrow_id', $data['id'])
- ->column('*', 'id');
- $toUpdate = [];
- $toInsert = [];
- foreach ($data['asset'] as $datum) {
- if (isset($datum['id'], $existingRecords[$datum['id']])) {
- // 对比现有数据与新数据
- $current = $existingRecords[$datum['id']];
- if ($current['asset_name'] !== $datum['model'] || $current['num'] !== $datum['num'] || $current['origin'] !== $datum['origin']) {
- // 数据有变化,则准备更新
- $toUpdate[] = [
- 'id' => $datum['id'],
- 'borrow_id' => $data['id'],
- 'asset_name' => $datum['model'],
- 'origin' => $datum['origin'],
- 'num' => $datum['num'],
- ];
- }
- // 从现有记录中移除已经处理过的项
- unset($existingRecords[$datum['id']]);
- } else {
- // 数据不存在,准备插入项
- $toInsert[] = [
- 'borrow_id' => $data['id'],
- 'asset_name' => $datum['model'],
- 'origin' => $datum['origin'],
- 'num' => $datum['num'],
- ];
- }
- }
- // $existingRecords 中剩下的是需要软删除的
- $toDelete = array_keys($existingRecords);
- if (!empty($toDelete)) {
- Db::name('borrow_number')
- ->where('id', 'in', $toDelete)
- ->useSoftDelete('delete_time', date('Y-m-d H:i:s'))
- ->delete();
- }
- // 更新已变化的数据
- if (!empty($toUpdate)) {
- $this->BorrowNumber->saveAll($toUpdate);
- }
- // 插入新增的数据
- if (!empty($toInsert)) {
- $this->BorrowNumber->insertAll($toInsert);
- }
- }
- public function getDateForAssetName($asset_id,$asset_name,$borrow_id)
- {
- $is_old = $this->BorrowAccess->where(["asset_id" => $asset_id, 'status' => 0])->find();
- if ($is_old !== null) {
- $this->error(__('The instrument is bound'));
- }
- $isOldAssset = Db::name('asset')->where('asset_id', $asset_id)->value('status');
- if ($isOldAssset == 1) {
- $this->error("该仪器已借出!!");
- }
- if( strpos($asset_name,'无人机') !== false){
- $map = [
- ['asset_name','like','%' . '无人机' .'%'],
- ];
- }else if( strpos($asset_name,'RTK') !== false){
- $map = [
- ['asset_name','like','%' . 'RTK' .'%'],
- ];
- }else if( strpos($asset_name,'水准仪') !== false){
- $map = [
- ['asset_name','like','%' . '水准仪' .'%'],
- ];
- }else if( strpos($asset_name,'全站仪') !== false){
- $map = [
- ['asset_name','like','%' . '全站仪' .'%'],
- ];
- }else{
- $map = [
- ['asset_name', '=' , $asset_name]
- ];
- }
- $data = $this->BorrowAccess
- ->where(['borrow_id' => $borrow_id])
- ->where($map)
- ->where('asset_id', null)
- ->find();
-
- return $data;
- }
- }
|