public function actionApply()
 {
     //获取客户端POST过来的数据
     $stuId = $_POST['stuId'];
     $assetName = $_POST['assetName'];
     $specification = $_POST['specification'];
     $loanTime = $_POST['loanTime'];
     $returnTime = $_POST['returnTime'];
     //计算申请时间
     $applyTime = date('Y-m-d H:m:s', time());
     //通过stuId查找学生信息
     $stuData = StStudent::model()->findByPk($stuId);
     $stuData = json_decode(CJSON::encode($stuData), TRUE);
     //插入数据库
     $apply = new AsApply();
     // 资产申请数据库表
     $apply->stuId = $stuId;
     $apply->stuName = $stuData['stuName'];
     $apply->assetName = $assetName;
     $apply->specification = $specification;
     $apply->applyTime = $applyTime;
     $apply->RFID = "123";
     $apply->borrowTime = $loanTime;
     $apply->returnTime = $returnTime;
     $apply->stuTelNum = '123';
     $apply->type = 'static';
     if ($apply->save() > 0) {
         // 数据库表类的函数save(),存储是否成功,来自yii框架
         $retArr = array('flag' => 1);
         exit(json_encode($retArr));
     } else {
         $retArr = array('flag' => 0);
         exit(json_encode($retArr));
     }
 }
 /**
  *主要是删除该功能
  */
 public function actionDelete($applyId)
 {
     $data = AsApply::model()->deleteByPk($applyId);
     echo "<script language=\"JavaScript\">alert(\"删除成功\");</script>";
     $criteria = new CDbCriteria();
     $criteria->select = '*';
     // 处理提交数据
     $count = AsApply::model()->count($criteria);
     $pages = new CPagination($count);
     // results per page
     $pages->pageSize = PAGESIZE;
     $pages->applyLimit($criteria);
     //echo var_dump($pages);
     $data = AsApply::model()->findAll($criteria);
     $data = json_decode(CJSON::encode($data), TRUE);
     is_array($data) ? null : ($data = array());
     // 防止空数组
     $this->render('index', array('data' => $data, 'pages' => $pages));
 }
 /**
  * 资产借出操作
  *
  * @author 张钰、徐晨阳
  * @param
  *        	资产名称
  * @todo 显示资产借出页面,允许向AsApply表中增加新借出纪录,同时根据(资产名称-学生id)删除AsApply中已有的申请记录
  */
 public function actionBorrow($assetName, $specification, $totalNum, $type, $applyId = "", $assetId = "", $RFID = "")
 {
     if ($type == "static") {
         $sql = "SELECT COUNT(IF(state='in',TRUE,NULL)) from as_asset where specification=:specification and assetName=:assetName";
     } else {
         $sql = "SELECT COUNT(IF(state='in',TRUE,NULL)) from as_consume where specification=:specification and assetName=:assetName";
     }
     $totalNum = Yii::app()->db->createCommand($sql)->queryScalar(array(':specification' => $specification, ':assetName' => $assetName));
     if (XUtils::method() == 'POST') {
         // var_dump($_POST);
         $verify = "SELECT * FROM st_admin WHERE id = " . $_POST['userId'] . " AND name = '" . $_POST['userName'] . "'";
         $userData = Yii::app()->db->createCommand($verify)->queryAll();
         $userData = json_decode(CJSON::encode($userData), TRUE);
         if (!empty($userData)) {
             // var_dump($userData);
             //添加借出信息到借出表中
             $borrow = new AsBorrow();
             $borrow->assetName = $assetName;
             $borrow->assetSpecification = $specification;
             $borrow->userTeleNum = $_POST['userTeleNum'];
             $borrow->userId = $_POST['userId'];
             $borrow->userName = $_POST['userName'];
             $borrow->type = $type;
             $borrow->assetID = $_POST['assetId'];
             $borrow->borrowTime = $_POST['borrowTime'];
             $borrow->returnTime = $_POST['returnTime'];
             if ($totalNum > 0) {
                 if ($type == "static") {
                     $asset = AsAsset::model()->findByPk($borrow->assetID);
                     if (empty($asset)) {
                         echo "<script language=\"JavaScript\">alert(\"改标签未注册!\");</script>";
                     } else {
                         if ($asset['outPrm'] == 'y') {
                             if ($asset['assetName'] == $borrow->assetName && $asset['specification'] == $borrow->assetSpecification) {
                                 if ($borrow->save() > 0) {
                                     $count = AsAsset::model()->updateByPk($borrow->assetID, array('state' => 'out', 'brwPhone' => $borrow->userTeleNum));
                                     if ($count > 0) {
                                         $data = AsApply::model()->deleteByPk($applyId);
                                         echo "<script language=\"JavaScript\">alert(\"借出成功\");</script>";
                                     } else {
                                         echo "<script language=\"JavaScript\">alert(\"借出失败\");</script>";
                                     }
                                 }
                             } else {
                                 echo "<script language=\"JavaScript\">alert(\"信息不符\");</script>";
                             }
                         } else {
                             echo "<script language=\"JavaScript\">alert(\"该商品不允许被借出\");</script>";
                         }
                     }
                 } elseif ($type == "consume") {
                     $asset = AsConsume::model()->findByPk($borrow->assetID);
                     if (empty($asset)) {
                         echo "<script language=\"JavaScript\">alert(\"改标签未注册!\");</script>";
                     } else {
                         if ($asset['outPrm'] == 'y') {
                             if ($asset['assetName'] == $borrow->assetName && $asset['specification'] == $borrow->assetSpecification) {
                                 if ($borrow->save() > 0) {
                                     $count = AsConsume::model()->updateByPk($borrow->assetID, array('state' => 'out'));
                                     if ($count > 0) {
                                         $data = AsApply::model()->deleteByPk($applyId);
                                         echo "<script language=\"JavaScript\">alert(\"借出成功\");</script>";
                                     } else {
                                         echo "<script language=\"JavaScript\">alert(\"借出失败\");</script>";
                                     }
                                 }
                             } else {
                                 echo "<script language=\"JavaScript\">alert(\"信息不符\");</script>";
                             }
                         } else {
                             echo "<script language=\"JavaScript\">alert(\"该商品不允许被借出\");</script>";
                         }
                     }
                 }
             } else {
                 echo "<script language=\"JavaScript\">alert(\"该商品没有库存\");</script>";
             }
         } else {
             echo "<script language=\"JavaScript\">alert(\"借出人信息不符\");</script>";
         }
     }
     // echo var_dump($totalNum);
     // if($totalNum>0){
     $this->render('borrow', array('assetName' => $assetName, 'specification' => $specification, 'totalNum' => $totalNum, 'type' => $type, 'assetID' => $assetID, 'RFID' => $RFID, 'userId' => $borrow->userId, 'userName' => $borrow->userName, 'userTeleNum' => $borrow->userTeleNum, 'applyId' => $applyId));
 }