123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\Base;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- class HouseTurnLessee extends Base
- {
- // 新增
- public function save(){
- model('HouseTurnLessee')->add();
- HelpHander::success([],'操作成功');
- }
- public function updates(){
- model('HouseTurnLessee')->edit();
- HelpHander::success([],'操作成功');
- }
- public function del(){
- model('HouseTurnLessee')->del();
- HelpHander::success([],'操作成功');
- }
- // 详情
- public function detail(){
- $id = input('id/d',0);
- $ret = model('HouseTurnLessee')->info($id);
- HelpHander::success($ret);
- }
- //列表
- public function list(){
- $page = input('page/d',1);
- $size = input('size/d',10);
- $id = input('id/d',0);
- $title = input('title','','trim');
- $ret = model('HouseTurnLessee')->lists($page,$size,$title,$id,$this->orgId);
- HelpHander::success($ret);
- }
- // 导入入住数据
- public function import(){
- $path = input('path','','trim');
- if(!$path){
- HelpHander::error('未上传文件');
- }
- $path = json_decode($path,true);
- $file = str_ireplace(request()->domain(),'.',$path[0]['url']) ;
- $filearr = explode('.',$file);
- $ext = strtolower($filearr[count($filearr) - 1]);
- $type = $ext == 'xlsx'?'Xlsx':'Xls';
- $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($type); //Xls,Xlsx
- $reader->setReadDataOnly(TRUE);
- $spreadsheet = $reader->load($file); //载入excel表格
- $worksheet = $spreadsheet->getActiveSheet();
- $highestRow = $worksheet->getHighestRow(); // 总行数
- $highestColumn = $worksheet->getHighestColumn(); // 总列数
- $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5
- $lines = $highestRow - 1;
- if ($lines <= 0) {
- HelpHander::error('Excel表格中没有数据');
- }
- $marrs = [];
- for ($row = 2; $row <= $highestRow; ++$row) {
- $data = [
- 'title' => $worksheet->getCellByColumnAndRow(1, $row)->getValue(),
- 'name' => $worksheet->getCellByColumnAndRow(2, $row)->getValue(),
- 'code' => $worksheet->getCellByColumnAndRow(3, $row)->getValue(),
- 'start' => $worksheet->getCellByColumnAndRow(4, $row)->getValue(),
- ];
- $ret = $this->createLessee($row,$data);
- if($ret !== true){
- $marrs[] = $ret;
- }
- }
- HelpHander::success($marrs,'导入成功');
- }
- private function createLessee($row,$data){
- if(!$data['title'] || !$data['name'] || !$data['code'] || !$data['start']){
- return '第'.$row.'行,数据存在为空的值';
- }
- // 获取房间并检查是否已入住
- $turn = Db::name('house_turn_list')
- ->where('org_id',$this->orgId)
- ->where('title','like','%'.$data['title'].'%')
- ->find();
- if(!$turn){
- return '第'.$row.'行,数据未找到相对应的房间';
- }
- if($turn['cur_lessee_id'] > 0){
- return '第'.$row.'行,找到相对应房间已有入住人';
- }
- // 获取入住人信息并保存入住
- $lessee = Db::name('house_lessee')
- ->where('org_id',$this->orgId)
- ->where('type',2)
- ->where('del',0)
- ->where('code',$data['code'])
- ->find();
- if(!$lessee){
- return '第'.$row.'行,未找到相对应的入住人';
- }
- Db::startTrans();
- try{
- $ret = Db::name('house_turn')->where('id',$turn['id'])->update([
- 'cur_lessee_id' => $lessee['id'],
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- if(!$ret){
- \exception('保存失败');
- }
- $sdata = [
- 'house_turn_id' => $turn['id'],
- 'lessee_id' => $lessee['id'],
- 'start_time' => date('Y-m-d',strtotime($data['start'])),
- 'create_time' => date('Y-m-d H:i:s'),
- 'org_id' => $this->orgId,
- 'status' => 0
- ];
- $res = Db::name('house_turn_lessee')->insert($sdata);
- if(!$res){
- \exception('保存失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- return '第'.$row.'行,数据保存失败';
- }
- return true;
- }
- }
|