HouseTurnLessee.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. use think\Exception;
  7. class HouseTurnLessee extends Base
  8. {
  9. // 新增
  10. public function save(){
  11. model('HouseTurnLessee')->add();
  12. HelpHander::success([],'操作成功');
  13. }
  14. public function updates(){
  15. model('HouseTurnLessee')->edit();
  16. HelpHander::success([],'操作成功');
  17. }
  18. public function del(){
  19. model('HouseTurnLessee')->del();
  20. HelpHander::success([],'操作成功');
  21. }
  22. // 详情
  23. public function detail(){
  24. $id = input('id/d',0);
  25. $ret = model('HouseTurnLessee')->info($id);
  26. HelpHander::success($ret);
  27. }
  28. //列表
  29. public function list(){
  30. $page = input('page/d',1);
  31. $size = input('size/d',10);
  32. $id = input('id/d',0);
  33. $title = input('title','','trim');
  34. $ret = model('HouseTurnLessee')->lists($page,$size,$title,$id,$this->orgId);
  35. HelpHander::success($ret);
  36. }
  37. // 导入入住数据
  38. public function import(){
  39. $path = input('path','','trim');
  40. if(!$path){
  41. HelpHander::error('未上传文件');
  42. }
  43. $path = json_decode($path,true);
  44. $file = str_ireplace(request()->domain(),'.',$path[0]['url']) ;
  45. $filearr = explode('.',$file);
  46. $ext = strtolower($filearr[count($filearr) - 1]);
  47. $type = $ext == 'xlsx'?'Xlsx':'Xls';
  48. $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($type); //Xls,Xlsx
  49. $reader->setReadDataOnly(TRUE);
  50. $spreadsheet = $reader->load($file); //载入excel表格
  51. $worksheet = $spreadsheet->getActiveSheet();
  52. $highestRow = $worksheet->getHighestRow(); // 总行数
  53. $highestColumn = $worksheet->getHighestColumn(); // 总列数
  54. $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5
  55. $lines = $highestRow - 1;
  56. if ($lines <= 0) {
  57. HelpHander::error('Excel表格中没有数据');
  58. }
  59. $marrs = [];
  60. for ($row = 2; $row <= $highestRow; ++$row) {
  61. $data = [
  62. 'title' => $worksheet->getCellByColumnAndRow(1, $row)->getValue(),
  63. 'name' => $worksheet->getCellByColumnAndRow(2, $row)->getValue(),
  64. 'code' => $worksheet->getCellByColumnAndRow(3, $row)->getValue(),
  65. 'start' => $worksheet->getCellByColumnAndRow(4, $row)->getValue(),
  66. ];
  67. $ret = $this->createLessee($row,$data);
  68. if($ret !== true){
  69. $marrs[] = $ret;
  70. }
  71. }
  72. HelpHander::success($marrs,'导入成功');
  73. }
  74. private function createLessee($row,$data){
  75. if(!$data['title'] || !$data['name'] || !$data['code'] || !$data['start']){
  76. return '第'.$row.'行,数据存在为空的值';
  77. }
  78. // 获取房间并检查是否已入住
  79. $turn = Db::name('house_turn_list')
  80. ->where('org_id',$this->orgId)
  81. ->where('title','like','%'.$data['title'].'%')
  82. ->find();
  83. if(!$turn){
  84. return '第'.$row.'行,数据未找到相对应的房间';
  85. }
  86. if($turn['cur_lessee_id'] > 0){
  87. return '第'.$row.'行,找到相对应房间已有入住人';
  88. }
  89. // 获取入住人信息并保存入住
  90. $lessee = Db::name('house_lessee')
  91. ->where('org_id',$this->orgId)
  92. ->where('type',2)
  93. ->where('del',0)
  94. ->where('code',$data['code'])
  95. ->find();
  96. if(!$lessee){
  97. return '第'.$row.'行,未找到相对应的入住人';
  98. }
  99. Db::startTrans();
  100. try{
  101. $ret = Db::name('house_turn')->where('id',$turn['id'])->update([
  102. 'cur_lessee_id' => $lessee['id'],
  103. 'update_time' => date('Y-m-d H:i:s')
  104. ]);
  105. if(!$ret){
  106. \exception('保存失败');
  107. }
  108. $sdata = [
  109. 'house_turn_id' => $turn['id'],
  110. 'lessee_id' => $lessee['id'],
  111. 'start_time' => date('Y-m-d',strtotime($data['start'])),
  112. 'create_time' => date('Y-m-d H:i:s'),
  113. 'org_id' => $this->orgId,
  114. 'status' => 0
  115. ];
  116. $res = Db::name('house_turn_lessee')->insert($sdata);
  117. if(!$res){
  118. \exception('保存失败');
  119. }
  120. Db::commit();
  121. }catch (Exception $e){
  122. Db::rollback();
  123. return '第'.$row.'行,数据保存失败';
  124. }
  125. return true;
  126. }
  127. }