1.时间的判断,还有就是在php这边如何去拿前端html元素上面的值
input('$row.borrowtime');
// 创建两个 DateTime 对象$row_expecttime = new \DateTime(input('$row.borrowtime'));$par_expecttime = new \DateTime( $params['expecttime']);
// // 计算两个日期之间的差异
// $diff = $par_expecttime->diff($row_expecttime);// 计算总的天数差异$totalDays = $par_expecttime->diff($row_expecttime);if ($totalDays->invert == 0){$this->error('续借时间不能小于原归还时间');}if ($totalDays->m >= 7){$this->error('借阅总时间最多不能超过7个月');}
2.批量存储数据的2中方法
2.1第一种数据量不大的话,就直接分别放到数组,然后一起sevaAll()一起存了算了
$books_ids = explode(',', $params['books_id']);if(count($books_ids)>3){$this->error('一次最多不超过3本图书');}// print_r($params);$result = false;Db::startTrans();try {//是否采用模型验证if ($this->modelValidate) {$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;$this->model->validateFailException()->validate($validate);}if(count($books_ids)>1){$seva_data = [];foreach ($books_ids as $key => $value) {$params['books_id'] = $value;$params['bookstatus']= Db::name('books')->where('id','=',$value)->value('status');
// print_r($params);$seva_data[] = $params;}
// print_r($seva_data);$result = $this->model->allowField(true)->saveAll($seva_data);if (!$result){throw new \Exception('图书'.$params['books_id'].'状态更新失败');}Db::commit();}else{$params['bookstatus']= Db::name('books')->where('id','=',$params['books_id'])->value('status');
// print_r($params);$result = $this->model->allowField(true)->save($params);if (!$result){
// $res = Books::where('id','=',$params['books_id'])->where('status',1)->update(['status'=>2]);
// if (!$res){throw new \Exception('图书'.$params['books_id'].'状态更新失败');
// }}Db::commit();}} catch (ValidateException|PDOException|Exception $e) {Db::rollback();$this->error($e->getMessage());}if ($result === false) {$this->error(__('No rows were inserted'));}$this->success();
2.2如果数据量大的话,那么就选择用foreach循环一条一条的存,但是会有个问题就是model要放到循环里面去
$books_ids = explode(',', $params['books_id']);if(count($books_ids)>3){$this->error('一次最多不超过3本图书');}$diff = strtotime($params['expecttime']) - strtotime($params['borrowtime']);if($diff > 180*24*3600){$this->error('借书时间不能超过180天');}else if($diff < 24*3600){$this->error('借书时间不能少于1天');}$result = false;Db::startTrans();try {//是否采用模型验证if ($this->modelValidate) {$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;$this->model->validateFailException()->validate($validate);}if(count($books_ids)>1){foreach ($books_ids as $key => $value) {$dt = $params;$dt['books_id'] = $value;$model = new \app\admin\model\read\Borrow;$result = $model->allowField(true)->save($dt);if ($result){$res = Books::where('id','=',$dt['books_id'])->where('status',1)->update(['status'=>2]);if (!$res){throw new \Exception('图书'.$dt['books_id'].'状态更新失败');}}}}else{$result = $this->model->allowField(true)->save($params);if ($result){$res = Books::where('id','=',$params['books_id'])->where('status',1)->update(['status'=>2]);if (!$res){throw new \Exception('图书'.$params['books_id'].'状态更新失败');}}}Db::commit();} catch (ValidateException|PDOException|Exception $e) {Db::rollback();$this->error($e->getMessage());}if ($result === false) {$this->error(__('No rows were inserted'));}$this->success();
3.数据库的关联查询
$oldData = $this->model->with('books')->where('books_id' ,'in',$books_ids)->where('subscription.status','=','1')->select();// $jsonData = json_encode($oldData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); // 格式化 JSON 输出
// print_r($jsonData);if ($oldData){$titles = array_map(function ($item){return $item['books']['name'];},$oldData);$this->error('已经在预约中不能重复预约:'.implode(",",$titles));}
4.用fastadmin自带的邮件系统发邮件
4.1进入到api里面可以自己,自定义一个api
4.2具体邮件类的封装是在下图的目录下,可以自己去找然后看里面的注释