Commit 25b6b0e7 authored by 蔡闯's avatar 蔡闯

社区基本信息功能

parent ac5c4c71
......@@ -19,8 +19,14 @@ abstract class BaseController
"property" => '物业费',
"electric" => '电费',
"gas" => '燃气费',
"parking" => '停车费'
"park" => '停车费'
];
protected $myUnit = [
'water' =>"吨",
'electric' =>'度',
'gas' =>'立方'
];
/**
* Request实例
* @var \think\Request
......
<?php
namespace app\api\controller;
use app\common\controller\Common;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
class Index extends Base
{
public function index() {
return "这是我请求的内容";
}
//建筑列表
public function buildList() {
$where['lb.village_id'] = Request::param('village_id');
$parent_id = Request::param('parent_id',0);
$where['lb.parent_id'] = $parent_id;
//检查当前的parent_id是否是最下的一个建筑了,如果是,则查询该下面的房屋
if($parent_id != 0) {
$layout_id = Request::param('layout_id');
$listInfo = Db::name('layout_list')->where(['layout_id'=>$layout_id])->field('code,level')->find();
$level = $listInfo['level']-1;
$listArr = explode('-',$listInfo['code']);
$name = array_pop($listArr);
$is_last = Db::name('layout_build')->where(['layout_build_id'=>$parent_id,'level'=>$level])->find();
if($is_last){ //查找房间
$data = Db::name('house_vacancy')->where(['parent_id'=>$parent_id])->field('vacancy_id,vacancy_code')->select()->toArray();
foreach ($data as $k => $v) {
$vacancyArr = explode('-',$v['vacancy_code']);
$code = array_pop($vacancyArr);
$data[$k]['code_name'] = $code.$name;
}
} else{
$data = $this->getBuildInfo($where);
}
} else {
$data = $this->getBuildInfo($where);
}
return $this->returnJson($data);
}
//获取建筑信息
public function getBuildInfo($where) {
if(Request::param('layout_id')) {
$where['lb.layout_id'] = Request::param('layout_id');
}
$data = Db::name('layout_build')->alias('lb')->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')->where($where)->order(['lb.layout_id'=>'asc','lb.sort_id'=>'desc'])
->field("lb.layout_build_id,lb.layout_id,lb.code,lb.level,lb.village_id,ll.code as list_code")
->select()
->toArray();
foreach ($data as $k =>$v) {
$data[$k]['code_name'] = $v['code'].explode('-',$v['list_code'])[$v['level']-1];
}
return $data;
}
//获取单个房间基本信息
public function getVacancyInfo() {
$vacancy_id = Request::param('vacancy_id');
$data = Db::name('house_vacancy')->alias('hv')
->Join('house_village hvi','hvi.village_id = hv.village_id')
->Join('house_user_bind hub','hub.vacancy_id = hv.vacancy_id')
->where(['hv.vacancy_id'=>$vacancy_id])
->field('hv.vacancy_code,hv.village_id,hv.parent_id,layout_id,hvi.village_name,hvi.village_address,hvi.province_name,hvi.city_name,hvi.area_name')
->find();
$data['vacancy_address'] = Common::getVacancyAddress($data['vacancy_code'],$data['layout_id']);
$data['vacancyOwnerInfo'] = Common::getOwnerInfo($vacancy_id); //物业信息
$data['getUserBindType'] = Common::getUserBindType();
$data['getFamilyBindType'] = Common::getFamilyBindType();
return $this->returnJson($data);
}
//常用电话
public function commonPhone() {
$village_id = Request::param('village_id');
$where['village_id'] = $village_id;
$where['status'] = 1;
$data = Db::name('village_phone')->where($where)->order(['is_on_call_phone'=>'desc','sort'=>'desc'])->select()->toArray();
return $this->returnJson($data);
}
<?php
namespace app\api\controller;
use app\common\controller\Common;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
class Index extends Base
{
//app首页信息,如果用户只绑定了一个房间,则默认展示一个房间的
public function index() {
$page = Request::param('page',1);
$uid = $this->uid;
$village_id = Request::param('village_id');
if(empty($village_id)) {
$data = Db::name('house_user_bind')->alias('hub')
->leftJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')
->leftJoin('house_village hvi','hvi.village_id = hv.village_id')
->where(['hub.uid'=>$uid])
->field('hvi.village_name,hvi.village_id')->find();
} else {
$data = Db::name('house_village')->where(['village_id'=>$village_id])->field('village_id,village_name')->find();
}
$news= Db::name('news')->where(['village_id'=>$data['village_id'],'is_recommend'=>1])->order('recommend_sort_id','desc')
->field('title,pic,content')
->page($page,config('app.limit'))
->select()->toArray();
foreach ($news as $k=>$v) {
$news[$k]['content'] = mb_substr(strip_tags($v['content']),0,100);
}
$data['news'] = $news;
return $this->returnJson($data);
}
//建筑列表
public function buildList() {
$where['lb.village_id'] = Request::param('village_id');
$parent_id = Request::param('parent_id',0);
$where['lb.parent_id'] = $parent_id;
//检查当前的parent_id是否是最下的一个建筑了,如果是,则查询该下面的房屋
if($parent_id != 0) {
$layout_id = Request::param('layout_id');
$listInfo = Db::name('layout_list')->where(['layout_id'=>$layout_id])->field('code,level')->find();
$level = $listInfo['level']-1;
$listArr = explode('-',$listInfo['code']);
$name = array_pop($listArr);
$is_last = Db::name('layout_build')->where(['layout_build_id'=>$parent_id,'level'=>$level])->find();
if($is_last){ //查找房间
$data = Db::name('house_vacancy')->where(['parent_id'=>$parent_id])->field('vacancy_id,vacancy_code')->select()->toArray();
foreach ($data as $k => $v) {
$vacancyArr = explode('-',$v['vacancy_code']);
$code = array_pop($vacancyArr);
$data[$k]['code_name'] = $code.$name;
}
} else{
$data = $this->getBuildInfo($where);
}
} else {
$data = $this->getBuildInfo($where);
}
return $this->returnJson($data);
}
//获取建筑信息
public function getBuildInfo($where) {
if(Request::param('layout_id')) {
$where['lb.layout_id'] = Request::param('layout_id');
}
$data = Db::name('layout_build')->alias('lb')->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')->where($where)->order(['lb.layout_id'=>'asc','lb.sort_id'=>'desc'])
->field("lb.layout_build_id,lb.layout_id,lb.code,lb.level,lb.village_id,ll.code as list_code")
->select()
->toArray();
foreach ($data as $k =>$v) {
$data[$k]['code_name'] = $v['code'].explode('-',$v['list_code'])[$v['level']-1];
}
return $data;
}
//获取单个房间基本信息
public function getVacancyInfo() {
$vacancy_id = Request::param('vacancy_id');
$data = Db::name('house_vacancy')->alias('hv')
->Join('house_village hvi','hvi.village_id = hv.village_id')
->Join('house_user_bind hub','hub.vacancy_id = hv.vacancy_id')
->where(['hv.vacancy_id'=>$vacancy_id])
->field('hv.vacancy_code,hv.village_id,hv.parent_id,layout_id,hvi.village_name,hvi.village_address,hvi.province_name,hvi.city_name,hvi.area_name')
->find();
$data['vacancy_address'] = Common::getVacancyAddress($data['vacancy_code'],$data['layout_id']);
$data['vacancyOwnerInfo'] = Common::getOwnerInfo($vacancy_id); //物业信息
$data['getUserBindType'] = Common::getUserBindType();
$data['getFamilyBindType'] = Common::getFamilyBindType();
return $this->returnJson($data);
}
//常用电话
public function commonPhone() {
$village_id = Request::param('village_id');
$where['village_id'] = $village_id;
$where['status'] = 1;
$data = Db::name('village_phone')->where($where)->order(['is_on_call_phone'=>'desc','sort'=>'desc'])->select()->toArray();
return $this->returnJson($data);
}
//当前用户的所有的房间列表
public function allVacancy() {
$uid = $this->uid;
$where[] = ['hub.uid','=',$uid];
$page = Request::param('page',1);
$data = Db::name('house_user_bind')->alias('hub')->where($where)
->leftJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')
->leftJoin('house_village hvi','hvi.village_id = hub.village_id')
->field('hub.house_user_bind_id,hvi.village_name,hv.vacancy_code,hv.layout_id,hub.pass_time,hub.type,hub.status,hub.vacancy_id,hvi.village_logo,hvi.village_id')
->page($page,config('app.limit'))
->select()->toArray();
foreach ($data as $k=>$v) {
$rest = Common::getVacancyAddress($v['vacancy_code'],$v['layout_id']);
$data[$k]['vacancy_address'] = $rest['vacancy_address'];
//获取用户车辆信息
$cars = Db::name('car')->where(['uid'=>$this->uid,'village_id'=>$v['village_id']])->field('car_id,license_plate')->select()->toArray();
$data[$k]['cars'] = $cars;
$car_total = count($cars);
if($car_total ==1) {
$data[$k]['cars'] = $cars[0];
}
$data[$k]['car_total'] = $car_total;
}
$total = Db::name('house_user_bind')->alias('hub')->where($where)->count();
$res['total'] = $total;
$res['data'] = Common::changeField($data,'pass_time');
return $this->returnJson($res);
}
//查看车辆详情
public function detailCar() {
$car_id = Request::param('car_id');
$data = Db::name('car')->where(['car_id'=>$car_id,'uid'=>$this->uid])->find();
return $this->returnJson($data);
}
//修改车辆
public function changeCar() {
$car_id = Request::param('car_id');
$data['license_plate'] = Request::param('license_plate');
$is_exit = Db::name('car')->where('uid','<>',$this->uid)->where($data)->find();
if($is_exit){
return $this->returnJson([],'该车牌号已存在!',400);
}
$data['name'] = Request::param('name');
$data['car_color'] = Request::param('car_color');
$data['brand'] = Request::param('brand');
$data['car_type'] = Request::param('car_type');
$change = Db::name('car')->where(['car_id'=>$car_id])->save($data);
if($change) {
return $this->returnJson([],'操作成功!');
} else {
return $this->returnJson([],'操作失败!',400);
}
}
//用户添加车辆
public function addCar() {
$uid = $this->uid;
$car_id = Request::param('car_id');
$data['phone'] = Db::name('user')->where(['uid'=>$uid])->value('phone');
$data['name'] = Request::param('name');
$data['brand'] = Request::param('brand');
$data['car_color'] = Request::param('car_color');
$data['license_plate'] = Request::param('license_plate');
$data['sort_id'] = Request::param('sort_id',1);
$data['uid'] = $uid;
$data['village_id'] = Request::param('village_id');
if($car_id) {
//判断车牌是否存在
$is_exit = Db::name('car')->where(['license_plate'=>$data['license_plate']])->where('car_id','<>',$car_id)->find();
if($is_exit) {
return $this->returnJson([],'系统中已存在该车牌!',400);
}
$operation = Db::name('car')->where(['car_id'=>$car_id])->save($data);
} else {
$data['create_time'] = time();
//判断车牌是否存在
$is_exit = Db::name('car')->where(['license_plate'=>$data['license_plate'],'village_id'=>$data['village_id']])->find();
if($is_exit) {
return $this->returnJson([],'系统中已存在该车牌!',400);
}
$operation = Db::name('car')->save($data);
}
if($operation) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'操作失败!',400);
}
}
//删除车辆
public function deleteCar() {
$car_id = Request::param('car_id');
$delete = Db::name('car')->where(['car_id'=>$car_id,'uid'=>$this->uid])->delete();
if($delete) {
return $this->returnJson();
} else {
return $this->returnJson([],'删除失败!',400);
}
}
//我的房间详情
public function detailVacancy() {
$bind_id = Request::param('bind_id');
$uid = $this->uid;
$where[] = ['hub.uid','=',$uid];
$where[] = ['hub.house_user_bind_id','=',$bind_id];
$data = Db::name('house_user_bind')->alias('hub')->where($where)
->leftJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')
->leftJoin('house_village hvi','hvi.village_id = hub.village_id')
->field('hvi.village_id,hvi.village_name,hv.vacancy_code,hv.layout_id,hub.pass_time,hub.type,hub.status,hub.vacancy_id,hvi.village_logo,hv.name,hv.phone,hub.house_user_bind_id')
->find();
$res = Common::getVacancyAddress($data['vacancy_code'],$data['layout_id']);
$data['vacancy_address'] = $res['vacancy_address'];
if($data['type'] ==0 ){
//查询家属
$data['family'] = $this->getFamily($data['vacancy_id']);
//查询租客
$data['tenant'] = $this->getTenant($data['vacancy_id']);
} elseif($data['type'] ==2){
$data['tenant'] = $this->getTenant($data['vacancy_id']);
}
return $this->returnJson($data);
}
//根据房间id,查询租客
public function getTenant($vacancy_id) {
//查询租客
$tenant = Db::name('house_user_bind')->alias('hub')->rightJoin('user','user.uid = hub.uid')
->where(['hub.vacancy_id'=>$vacancy_id,'hub.type'=>2])
->field('hub.name,hub.phone,user.avatar,user.uid,hub.village_id')->select()->toArray();
foreach ($tenant as $k =>$v) {
$car = Db::name('car')->where(['uid'=>$v['uid'],'village_id'=>$v['village_id']])->select()->toArray();
$tenant[$k]['cars'] = $car;
$tenant[$k]['car_total'] = count($car);
}
return $tenant;
}
//根据房间id,查询家属
public function getFamily($vacancy_id) {
//查询租客
$family = Db::name('house_user_bind')->alias('hub')->rightJoin('user','user.uid = hub.uid')
->where(['hub.vacancy_id'=>$vacancy_id,'hub.type'=>1])
->field('hub.village_id,hub.name,hub.phone,user.avatar,user.uid')->select()->toArray();
foreach ($family as $k =>$v) {
$car = Db::name('car')->where(['uid'=>$v['uid'],'village_id'=>$v['village_id']])->select()->toArray();
$family[$k]['cars'] = $car;
$family[$k]['car_total'] = count($car);
}
return $family;
}
}
\ No newline at end of file
<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\facade\Db;
use think\facade\Request;
class News extends Base{
//新闻分类
public function newsType() {
$villate_id = Request::param('village_id');
$new_type = Db::name('news_type')->where(['village_id'=>$villate_id])->order('sort_id','desc')->select()->toArray();
return $this->returnJson($new_type);
}
//分类下的新闻列表
public function newsList() {
$type_id = Request::param('type_id');
$page = Request::param('page',1);
$where['news_type_id'] = $type_id;
$data = Db::name('news')->where($where)->page($page,config('app.limit'))->order(['sort_id'=>'desc','create_time'=>'desc'])->field('news_id,title,content,create_time')->page($page,config('app.limit'))->select()->toArray();
foreach ($data as $k => $v) {
$data[$k]['content'] = mb_substr(strip_tags($v['content']),0,100);
}
$res['total'] = Db::name('news')->where($where)->count();
$res['data'] = Common::changeField($data);
return $this->returnJson($res);
}
//新闻详情
public function detailNews() {
$news_id = Request::param('news_id');
$data = Db::name('news')->where(['news_id'=>$news_id])->find();
if($data){
return $this->returnJson(Common::changeField($data));
} else {
return $this->returnJson();
}
}
}
\ No newline at end of file
<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\facade\Db;
use think\facade\Request;
class Payorder extends Base
{
//查看当前房间下的未缴费的水电物业等,只有业主才能看到记录
public function payList() {
$convertArr = $this->convertArr;
$vacancy_id = Request::param('vacancy_id');
$uid = $this->uid;
$page = Request::param('page',1);
$isPay = Request::param('is_pay',0);
//不是房主不显示费用明细
$isOwner = Db::name('house_user_bind')->where(['uid'=>$uid,'vacancy_id'=>$vacancy_id])->find();
if($isOwner['type'] != 0) {
return $this->returnJson([]);
}
$where['vacancy_id'] = $vacancy_id;
$where['is_pay'] = $isPay;
$data = Db::name('cost')->where($where)->order('cost_month','asc')->page($page,config('app.limit'))->select()->toArray();
foreach ($data as $k => $v){
$data[$k]['type'] = isset($convertArr[$v['type']]) ? $convertArr[$v['type']]: $v['type'] ;
}
$total = Db::name('cost')->where($where)->count();
$res['total'] = $total;
$res['data'] = Common::changeField($data);
return $this->returnJson($res);
}
//生成订单
public function createOrder(){
$vacancy_id = Request::param('vacancy_id');
$bind_id = Request::param('bind_id');
$cost_ids = Request::param('cost_ids');
$total_money = Request::param('total_money',0);
//验证物业费订单是否是连续的
$where['type'] = 'property';
$where['is_pay'] = 0;
$where['vacancy_id'] = $vacancy_id;
$property_cost_ids = Db::name('cost')->where($where)->whereIn('cost_id',$cost_ids)->column('cost_id'); //上传的未缴物业费的连续cost_id
$limit = count($property_cost_ids);
$cost_property_ids = Db::name('cost')->where($where)->limit($limit)->column('cost_id');
if($cost_property_ids != $property_cost_ids) {
return $this->returnJson([],'物业费缴费必须连续!');
}
//判断金额是否一致
unset($where['type']);
$total = Db::name('cost')->where($where)->whereIn('cost_id',$cost_ids)->sum('pay_money');
if($total != $total_money) {
return $this->returnJson([],'支付金额不正确!',400);
}
$data['uid'] = $this->uid;
if(count($cost_ids)>1){
$data['order_type'] = "多订单收费";
} else {
$type =Db::name('cost')->whereIn('cost_id',$cost_ids)->value('type');
$data['order_type'] = $type;
}
$data['order_name'] = "社区收费";
$data['order_num'] = createOrderNum();
$data['vacancy_id'] = $vacancy_id;
$res = $this->getProperty($data['vacancy_id']);
if($res['code'] !=200){
return $this->returnJson([],$res['data'],400);
}
$data['property_id'] =$res['data']['property_id'];
$data['village_id'] =$res['data']['village_id'];
$data['bind_id'] = $bind_id;
$data['money'] = $total;
$data['create_time'] = time();
$data['cost_ids'] = json_encode($cost_ids);
$data['money'] = $total;
$order_id = Db::name('pay_order')->insertGetId($data);
if($order_id) {
unset($res);
$res['order_id'] = $order_id;
return $this->returnJson($res);
} else{
return $this->returnJson([],'添加失败!',400);
}
}
//根据房间id获取当前的物业id和小区id
public function getProperty($vacancy_id) {
$data = Db::name('house_vacancy')->alias('hv')->leftJoin('house_village hvi','hvi.village_id = hv.village_id')->where(['hv.vacancy_id'=>$vacancy_id])->field('hvi.village_id,hvi.property_id')->find();
if($data){
return ['code'=>200,'data'=>$data];
} else {
return ['code'=>400,'data'=>'数据错误!'];
}
}
//生成支付参数
public function createPaySign() {
$order_id = Request::param('order_id');
$total_money = Request::param('total_money');
$vacancy_id = Request::param('vacancy_id');
$pay_type = Request::param('pay_type','alipay'); //weixin|alipay
if(empty($order_id)) {
return $this->returnJson([],'订单未找到!',400);
}
if($this->checkOrder($order_id,$total_money,$vacancy_id)){
$orderInfo = Db::name('pay_order')->where(['order_id'=>$order_id])->find();
//支付宝支付
if($pay_type =='alipay') {
$param['out_trade_no'] = $orderInfo['order_num'];
$param['subject'] = $orderInfo['order_name'];
$param['total_amount'] = floatval($orderInfo['money']);
$res= $this->getAliPaySign($param);
//微信支付
} else {
$param['order_id'] = $orderInfo['order_id'];
$param['total_money'] = $orderInfo['money'];
$param['order_name'] = $orderInfo['order_name'];
$param['order_num'] = $orderInfo['order_num'];
$res= $this->getWeiXinPaySign($param);
}
return $this->returnJson($res);
} else {
return $this->returnJson([],'非法的订单信息!',400);
}
}
//获取支付宝支付参数
public function getAliPaySign($param) {
require_once "../extend/aliPay/AopClient.php";
require_once "../extend/aliPay/request/AlipayTradeAppPayRequest.php";
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = config('app.pay_alipay_appid');
$aop->rsaPrivateKey = config('app.pay_alipay_merchant_private_key');
$aop->alipayrsaPublicKey = config('app.pay_alipay_public_key');
$aop->apiVersion = '1.0';
$aop->signType = config('app.pay_alipay_sign_type');
$aop->postCharset = 'utf-8';
$aop->format = 'json';
$request = new \AlipayTradeAppPayRequest();
$request->setNotifyUrl(config('app.notifyurl'));
$request->setBizContent(json_encode($param));
$response = $aop->sdkExecute($request);
return $response;
}
//获取微信支付参数
public function getWeiXinPaySign($param) {
require_once "../extend/weixin/Weixin.php";
$configArr = config('app.weixinPay');
$configArr['notifyurl'] = config('app.notifyurl');
$weixin = new \Weixin($param,$configArr);
return $weixin->pay();
}
//检查订单是否合法
public function checkOrder($order_id,$total_money,$vacancy_id) {
$where['order_id'] = $order_id;
$where['uid'] = $this->uid;
$where['vacancy_id'] = $vacancy_id;
$money = Db::name('pay_order')->where($where)->value('money');
if($money != $total_money) {
return false;
}
return true;
}
<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\facade\Db;
use think\facade\Request;
class Payorder extends Base
{
//业主所有房间下的未缴费的水电物业等,只有业主才能看到记录
public function payList() {
//获取是业主身份的房间
$vacancys = Db::name('house_user_bind')->alias('hub')
->rightJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')
->where(['hub.type'=>0,'hub.status'=>1])
->field('hub.house_user_bind_id,hv.vacancy_id,hv.vacancy_code,hv.layout_id')
->select()->toArray();
if($vacancys) {
$convertArr = $this->convertArr;
$array_keys = array_keys($convertArr);
foreach ($vacancys as $k =>$v) {
$where['vacancy_id'] = $v['vacancy_id'];
$where['is_pay'] = 0;
$res = Common::getVacancyAddress($v['vacancy_code'],$v['layout_id']);
$vacancys[$k]['vacancy_address'] = $res['vacancy_address'];
foreach ($convertArr as $key=>$value) {
$where['type'] = $key;
//总金额
$data= Db::name('cost')->where($where)->sum('pay_money');
$all_list[$key] = $data;
}
$vacancys[$k]['pay_list'] = $all_list;
//获取自定义的
unset($where['type']);
$vacancys[$k]['pay_list']['other'] = Db::name('cost')->where($where)->whereNotIn('type',array_keys($convertArr))->sum('pay_money');
}
foreach ($vacancys as $k =>$v) {
foreach ($array_keys as $val ) { //删除水电物业费为0的记录
if($v['pay_list'][$val] ==0) {
unset($vacancys[$k]['pay_list'][$val]);
}
}
if($v['pay_list']['other'] ==0) {
unset($vacancys[$k]['pay_list']['other']);
}
}
} else {
$vacancys = [];
}
return $this->returnJson($vacancys);
}
//查看已缴费的列表
public function getPayList() {
//获取是业主身份的房间
$vacancys = Db::name('house_user_bind')->alias('hub')
->rightJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')
->where(['hub.type'=>0,'hub.status'=>1])
->field('hub.house_user_bind_id,hv.vacancy_id,hv.vacancy_code,hv.layout_id')
->select()->toArray();
if($vacancys) {
$convertArr = $this->convertArr;
$array_keys = array_keys($convertArr);
$unit = $this->myUnit;
foreach ($vacancys as $k =>$v) {
$where['vacancy_id'] = $v['vacancy_id'];
$where['is_pay'] = 1;
$res = Common::getVacancyAddress($v['vacancy_code'],$v['layout_id']);
$vacancys[$k]['vacancy_address'] = $res['vacancy_address'];
foreach ($convertArr as $key=>$value) {
$where['type'] = $key;
//总金额
$data= Db::name('cost')->where($where)->sum('pay_money');
$rest['total_money'] = $data;
if($key == "property" || $key =="park"){
$count = Db::name('cost')->where($where)->count();
$total_num = $count."个月";
} else if($key == "water" || $key=="electric" || $key == "gas") {
$num = Db::name('cost')->where($where)->sum('area');
$total_num = $num.$unit[$key];
}
$rest['total_num'] = $total_num;
$all[$key] = $rest;
//$vacancys[$k][$key] = $rest;
}
//获取自定义的
unset($where['type']);
$count = Db::name('cost')->where($where)->whereNotIn('type',$array_keys)->sum('pay_money');
// $vacancys[$k]['other'] = $count;
$all['other'] = $count;
$vacancys[$k]['pay_list'] = $all;
}
//如果已缴费金额为0,删除,不显示
foreach ($vacancys as $k =>$v) {
foreach ($array_keys as $val ) { //删除水电物业费为0的记录
if($v['pay_list'][$val]['total_money'] ==0) {
unset($vacancys[$k]['pay_list'][$val]);
} else {
$pay_time = Db::name('cost')->where(['is_pay'=>1,'type'=>$val,'vacancy_id'=>$v['vacancy_id']])->order('pay_time','desc')->value('pay_time');
$vacancys[$k]['pay_list'][$val]['pay_time'] = date('Y-m-d H:i:s',$pay_time);
}
}
if($v['pay_list']['other'] ==0) {
unset($vacancys[$k]['pay_list']['other']);
} else {
$pay_time = Db::name('cost')->where(['is_pay'=>1,'vacancy_id'=>$v['vacancy_id']])->whereNotIn('type',$array_keys)->order('pay_time','desc')->value('pay_time');
$vacancys[$k]['pay_list'][$val]['pay_time'] = date('Y-m-d H:i:s',$pay_time);
}
}
//要分开循环删除
foreach ($vacancys as $k => $v) {
if(!isset($v['pay_list']['water']) && !isset($v['pay_list']['property']) && !isset($v['pay_list']['electric']) && !isset($v['pay_list']['gas']) && !isset($v['pay_list']['park']) && !isset($v['pay_list']['other'])){
unset($vacancys[$k]);
}
}
} else {
$vacancys = [];
}
return $this->returnJson($vacancys);
}
//某个房间下的某个收费项的以缴费缴费列表
public function vacancyPayedList(){
$vacancy_id = Request::param('vacancy_id');
$type = Request::param('type'); //property|water|electric|gas|park|other,除了other,其他都能对应表中的type
$page = Request::param('page',1);
$convertArr = $this->convertArr;
if(in_array($type,array_keys($convertArr))){
$where[] = ['type','=',$type];
} else {
$where[] = ['type','not in',array_keys($convertArr)];
}
$where[] = ['vacancy_id','=',$vacancy_id];
$where[] = ['is_pay','=',1];
//先查询是否合法
$is_exit = Db::name('house_user_bind')->where(['uid'=>$this->uid,'vacancy_id'=>$vacancy_id,'type'=>0])->find();
if(!$is_exit) {
return $this->returnJson([],'查询错误!',400);
}
$data = Db::name('cost')->where($where)->page($page,config('app.limit'))->order('cost_month','desc')->field('cost_id,cost_month,pay_money,type')->select()->toArray();
$total = Db::name('cost')->where($where)->count();
$res['total'] = $total;
$res['data'] = $data;
return $this->returnJson($res);
}
//某个房间下的某个收费项的未缴费缴费列表
public function vacancyPayList() {
$vacancy_id = Request::param('vacancy_id');
$type = Request::param('type'); //property|water|electric|gas|park|other,除了other,其他都能对应表中的type
$page = Request::param('page',1);
$convertArr = $this->convertArr;
if(in_array($type,array_keys($convertArr))){
$where[] = ['type','=',$type];
} else {
$where[] = ['type','not in',array_keys($convertArr)];
}
$where[] = ['vacancy_id','=',$vacancy_id];
$where[] = ['is_pay','=',0];
//先查询是否合法
$is_exit = Db::name('house_user_bind')->where(['uid'=>$this->uid,'vacancy_id'=>$vacancy_id,'type'=>0])->find();
if(!$is_exit) {
return $this->returnJson([],'查询错误!',400);
}
$data = Db::name('cost')->where($where)->page($page,config('app.limit'))->order('cost_month','desc')->field('cost_id,cost_month,pay_money,type')->select()->toArray();
$total = Db::name('cost')->where($where)->count();
$res['total'] = $total;
$res['data'] = $data;
return $this->returnJson($res);
}
//查看具体的订单详情
public function getDetailPay(){
$cost_id = Request::param('cost_id');
$data = Db::name('cost')->where(['cost_id'=>$cost_id])->find();
return $this->returnJson(Common::changeField($data));
}
//生成订单
public function createOrder(){
$vacancy_id = Request::param('vacancy_id');
$bind_id = Request::param('bind_id');
$cost_ids = Request::param('cost_ids');
$total_money = Request::param('total_money',0);
//验证物业费订单是否是连续的
$where['type'] = 'property';
$where['is_pay'] = 0;
$where['vacancy_id'] = $vacancy_id;
$property_cost_ids = Db::name('cost')->where($where)->whereIn('cost_id',$cost_ids)->column('cost_id'); //上传的未缴物业费的连续cost_id
$limit = count($property_cost_ids);
$cost_property_ids = Db::name('cost')->where($where)->limit($limit)->column('cost_id');
if($cost_property_ids != $property_cost_ids) {
return $this->returnJson([],'物业费缴费必须连续!');
}
//判断金额是否一致
unset($where['type']);
$total = Db::name('cost')->where($where)->whereIn('cost_id',$cost_ids)->sum('pay_money');
if($total != $total_money) {
return $this->returnJson([],'支付金额不正确!',400);
}
$data['uid'] = $this->uid;
if(count($cost_ids)>1){
$data['order_type'] = "多订单收费";
} else {
$type =Db::name('cost')->whereIn('cost_id',$cost_ids)->value('type');
$data['order_type'] = $type;
}
$data['order_name'] = "社区收费";
$data['order_num'] = createOrderNum();
$data['vacancy_id'] = $vacancy_id;
$res = $this->getProperty($data['vacancy_id']);
if($res['code'] !=200){
return $this->returnJson([],$res['data'],400);
}
$data['property_id'] =$res['data']['property_id'];
$data['village_id'] =$res['data']['village_id'];
$data['bind_id'] = $bind_id;
$data['money'] = $total;
$data['create_time'] = time();
$data['cost_ids'] = json_encode($cost_ids);
$data['money'] = $total;
$order_id = Db::name('pay_order')->insertGetId($data);
if($order_id) {
unset($res);
$res['order_id'] = $order_id;
return $this->returnJson($res);
} else{
return $this->returnJson([],'添加失败!',400);
}
}
//根据房间id获取当前的物业id和小区id
public function getProperty($vacancy_id) {
$data = Db::name('house_vacancy')->alias('hv')->leftJoin('house_village hvi','hvi.village_id = hv.village_id')->where(['hv.vacancy_id'=>$vacancy_id])->field('hvi.village_id,hvi.property_id')->find();
if($data){
return ['code'=>200,'data'=>$data];
} else {
return ['code'=>400,'data'=>'数据错误!'];
}
}
//生成支付参数
public function createPaySign() {
$order_id = Request::param('order_id');
$total_money = Request::param('total_money');
$vacancy_id = Request::param('vacancy_id');
$pay_type = Request::param('pay_type','alipay'); //weixin|alipay
if(empty($order_id)) {
return $this->returnJson([],'订单未找到!',400);
}
if($this->checkOrder($order_id,$total_money,$vacancy_id)){
$orderInfo = Db::name('pay_order')->where(['order_id'=>$order_id])->find();
//支付宝支付
if($pay_type =='alipay') {
$param['out_trade_no'] = $orderInfo['order_num'];
$param['subject'] = $orderInfo['order_name'];
$param['total_amount'] = floatval($orderInfo['money']);
$res= $this->getAliPaySign($param);
//微信支付
} else {
$param['order_id'] = $orderInfo['order_id'];
$param['total_money'] = $orderInfo['money'];
$param['order_name'] = $orderInfo['order_name'];
$param['order_num'] = $orderInfo['order_num'];
$res= $this->getWeiXinPaySign($param);
}
return $this->returnJson($res);
} else {
return $this->returnJson([],'非法的订单信息!',400);
}
}
//获取支付宝支付参数
public function getAliPaySign($param) {
require_once "../extend/aliPay/AopClient.php";
require_once "../extend/aliPay/request/AlipayTradeAppPayRequest.php";
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = config('app.pay_alipay_appid');
$aop->rsaPrivateKey = config('app.pay_alipay_merchant_private_key');
$aop->alipayrsaPublicKey = config('app.pay_alipay_public_key');
$aop->apiVersion = '1.0';
$aop->signType = config('app.pay_alipay_sign_type');
$aop->postCharset = 'utf-8';
$aop->format = 'json';
$request = new \AlipayTradeAppPayRequest();
$request->setNotifyUrl(config('app.notifyurl'));
$request->setBizContent(json_encode($param));
$response = $aop->sdkExecute($request);
return $response;
}
//获取微信支付参数
public function getWeiXinPaySign($param) {
require_once "../extend/weixin/Weixin.php";
$configArr = config('app.weixinPay');
$configArr['notifyurl'] = config('app.notifyurl');
$weixin = new \Weixin($param,$configArr);
return $weixin->pay();
}
//检查订单是否合法
public function checkOrder($order_id,$total_money,$vacancy_id) {
$where['order_id'] = $order_id;
$where['uid'] = $this->uid;
$where['vacancy_id'] = $vacancy_id;
$money = Db::name('pay_order')->where($where)->value('money');
if($money != $total_money) {
return false;
}
return true;
}
}
\ No newline at end of file
<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\facade\Db;
use think\facade\Request;
class User extends Base
{
//获取用户所入住的小区|搜索小区
public function getUserVillage() {
$keyword = Request::param('keyword');
if($keyword) { //如果有关键字就搜索关键字的小区
$villages = Db::name('house_village')->where('village_name','like','%'.$keyword.'%')->field('village_name,village_id')->select()->toArray();
$res=[];
foreach ($villages as $k =>$v) {
$res[$k]['village_name'] = $v['village_name'];
$res[$k]['vacancy'] = Common::getVacancy($v['village_id'],$this->uid);
}
} else { //没有就展示已入住的小区
$where['hub.uid'] = $this->uid;
$where['hub.status'] = 1;
$village_ids = DB::name('house_user_bind')->alias('hub')->where($where)->column('village_id');
$village_name = Db::name('house_village')->whereIn('village_id',$village_ids)->column('village_name','village_id');
$res=[];
foreach ($village_ids as $k =>$v) {
$res[$k]['village_name'] = $village_name[$v];
$res[$k]['vacancy'] = Common::getVacancy($v,$this->uid);
}
}
return $this->returnJson($res);
}
//修改|忘记密码
public function changePassword() {
if(request()->isPost()) {
$original_password = md5(Request::param('original_password'));
$new_password = md5(Request::param('new_password'));
$code = Request::param('code');
$phone = Request::param('phone');
if($code){
if(Common::rightCode($phone,$code)){
$change = Db::name('user')->where(['uid'=>$this->uid,'phone'=>$phone])->save(['password'=>$new_password]);
if($change){
return $this->returnJson([]);
} else {
return $this->returnJson([],'修改失败',400);
}
} else {
return $this->returnJson([],"验证码不正确",400);
}
} else {
//验证原密码是否正确
$is_exit = Db::name('user')->where(['uid'=>$this->uid,'phone'=>$phone,'password'=>$original_password])->find();
if($is_exit) {
$change = Db::name('user')->where(['uid'=>$this->uid,'phone'=>$phone])->save(['password'=>$new_password]);
if($change){
return $this->returnJson();
} else {
return $this->returnJson([],'修改失败',400);
}
} else {
return $this->returnJson([],'验证码不正确',400);
}
}
} else {
return $this->returnJson([],'请求方式不正确',400);
}
}
//用户设置密码
public function setPassword() {
$data['password'] = md5(Request::param('password'));
$save = Db::name('user')->where(['uid'=>$this->uid])->save($data);
if($save) {
return $this->returnJson();
} else {
return $this->returnJson([],'','111111');
}
}
//用户绑定房间
public function userBindVacancy(){
$data['type'] = Request::param('type');
$village_id = Request::param('village_id');
$vacancy_id = Request::param('vacancy_id');
$data['vacancy_id'] = $vacancy_id;
$data['name'] = Request::param("name");
$data['phone'] = Request::param("phone");
$data['village_id'] = $village_id;
$is_exit = Common::getOwnerInfo($vacancy_id);
$data['id_card'] = Request::param("id_card");
$data['uid'] = $this->uid;
$data['status'] = 2;
$data['create_time'] = time();
$data['memo'] = Request::param('memo');
$data['emer_user'] = Request::param('emer_user');//紧急联系人
$data['emer_phone'] = Request::param('emer_phone'); //紧急联系人电话
//查看当前手机号是否重复绑定当前房间,一个房间对应的一个手机号只能有一种身份
$is_only = Db::name('house_user_bind')->where(['village_id'=>$village_id,'vacancy_id'=>$vacancy_id,'phone'=>$data['phone']])->where('status',"<>",3)->find();
if($is_only){
return $this->returnJson([],'用户已绑定房间!',400);
}
if($data['type'] == 0) { //申请业主不需要填写业主后四位手机号
//检查该房间是否有房主绑定
if(!empty($is_exit)){
return $this->returnJson([],'该房间下已有业主!',);
}
$save['id_card'] = $data['id_card'];
$save['name'] = $data['name'];
$save['phone'] = $data['phone'];
$save['uid'] = $data['uid'];
//修改房间的信息
Db::name('house_vacancy')->where(['vacancy_id'=>$vacancy_id])->save($save);
}else{
if(empty($is_exit)){
return $this->returnJson([],'该房间未绑定业主,不能绑定租客!',);
}
$ownerPhone = $is_exit['phone'];
$phone_num = Request::param('phone_num');
if($phone_num != substr($ownerPhone,7)){
return $this->returnJson([],'业主后四位手机号不正确!');
}
}
$add = Db::name('house_user_bind')->save($data);
if($add) {
return $this->returnJson([],'申请成功,等待审核!',200);
} else {
return $this->returnJson([],'申请失败,请重试!',400);
}
}
//用户申请解绑
public function userUnBind(){
$where['house_user_bind_id'] = Request::param('house_user_bind_id');
$where['uid'] = $this->uid;
$save['status'] = 4;
$change =Db::name('house_user_bind')->where($where)->save($save);
if($change) {
return $this->returnJson([],'申请成功!');
} else {
return $this->returnJson([],'申请失败!',400);
}
}
//查看所有的房间,如果是业主,展示房间下的所有租客或家属
public function getUserVacancy() {
$uid = $this->uid;
$where['hub.uid'] = $uid;
$data = Db::name('house_user_bind')->alias('hub')
->leftJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')
->leftJoin('house_village hvi','hvi.village_id = hub.village_id')
->where($where)->whereIn('hub.status',[1,2,4])
->field('hub.type,hub.house_user_bind_id,hub.vacancy_id,parent_id,hv.vacancy_code,hv.layout_id,hv.name,hv.phone,hvi.village_name')
->select()->toArray();
foreach ($data as $k =>$v) {
$data[$k]['vacancy_address'] = Common::getVacancyAddress($v['vacancy_code'],$v['layout_id']);
if($v['type'] ==0) {
//如果是业主身份,查找下面的租客或家属等
$data[$k]['myTenant'] = Db::name('house_user_bind')->where(['vacancy_id'=>$v['vacancy_id']])->whereIn('type',[1,2,3])->field('name,phone,status')->select()->toArray();
}else{
//如果是租客身份,查找所属业主
$data[$k]['myOwner'] = Db::name('house_user_bind')->where(['vacancy_id'=>$v['vacancy_id'],'type'=>0,'status'=>1])->field('name,phone,status')->find();
}
}
return $this->returnJson($data);
}
//用户添加车辆
public function addCar() {
$uid = $this->uid;
$car_id = Request::param('car_id');
$data['phone'] = Db::name('user')->where(['uid'=>$uid])->value('phone');
$data['name'] = Request::param('name');
$data['brand'] = Request::param('brand');
$data['car_color'] = Request::param('car_color');
$data['license_plate'] = Request::param('license_plate');
$data['sort_id'] = Request::param('sort_id',1);
$data['uid'] = $uid;
$data['village_id'] = Request::param('village_id');
if($car_id) {
//判断车牌是否存在
$is_exit = Db::name('car')->where(['license_plate'=>$data['license_plate']])->where('car_id','<>',$car_id)->find();
if($is_exit) {
return $this->returnJson([],'系统中已存在该车牌!',400);
}
$operation = Db::name('car')->where(['car_id'=>$car_id])->save($data);
} else {
$data['create_time'] = time();
//判断车牌是否存在
$is_exit = Db::name('car')->where(['license_plate'=>$data['license_plate'],'village_id'=>$data['village_id']])->find();
if($is_exit) {
return $this->returnJson([],'系统中已存在该车牌!',400);
}
$operation = Db::name('car')->save($data);
}
if($operation) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'操作失败!',400);
}
}
<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\facade\Db;
use think\facade\Request;
class User extends Base
{
//获取用户所入住的小区|搜索小区
public function getUserVillage() {
$keyword = Request::param('keyword');
$page = Request::param('page',1);
$province_id = Request::param('province_id');
$city_id = Request::param('city_id');
$area_id = Request::param('area_id');
$where =[];
if($province_id) {
$where['province_id'] = $province_id;
}
if($city_id) {
$where['city_id'] = $city_id;
}
if($area_id) {
$where['area_id'] = $area_id;
}
if($keyword) { //如果有关键字就搜索关键字的小区
$villages = Db::name('house_village')->where($where)->where('village_name','like','%'.$keyword.'%')->field('village_name,village_id')->page($page,config('app.limit'))->select()->toArray();
$res['total'] = Db::name('house_village')->where($where)->where('village_name','like','%'.$keyword.'%')->count();
$rest=[];
foreach ($villages as $k =>$v) {
$rest['village_name'] = $v['village_name'];
$rest['vacancy'] = Common::getVacancy($v['village_id'],$this->uid);
$res['data'][] = $rest;
}
} else { //没有就展示已入住的小区
$where['hub.uid'] = $this->uid;
$where['hub.status'] = 1;
$village_ids = DB::name('house_user_bind')->alias('hub')->where($where)->column('village_id');
$village_name = Db::name('house_village')->whereIn('village_id',$village_ids)->column('village_name','village_id');
$res['total'] = count($village_ids);
$rest=[];
foreach ($village_ids as $k =>$v) {
$rest['village_name'] = $village_name[$v];
$rest['vacancy'] = Common::getVacancy($v,$this->uid);
$res['data'][] = $rest;
}
}
if($res['total']==1){
$res['data'] = array_values($res['data']);
}
return $this->returnJson($res);
}
//修改|忘记密码
public function changePassword() {
if(request()->isPost()) {
$original_password = md5(Request::param('original_password'));
$new_password = md5(Request::param('new_password'));
$code = Request::param('code');
$phone = Request::param('phone');
if($code){
if(Common::rightCode($phone,$code)){
$change = Db::name('user')->where(['uid'=>$this->uid,'phone'=>$phone])->save(['password'=>$new_password]);
if($change){
return $this->returnJson([]);
} else {
return $this->returnJson([],'修改失败',400);
}
} else {
return $this->returnJson([],"验证码不正确",400);
}
} else {
//验证原密码是否正确
$is_exit = Db::name('user')->where(['uid'=>$this->uid,'phone'=>$phone,'password'=>$original_password])->find();
if($is_exit) {
$change = Db::name('user')->where(['uid'=>$this->uid,'phone'=>$phone])->save(['password'=>$new_password]);
if($change){
return $this->returnJson();
} else {
return $this->returnJson([],'修改失败',400);
}
} else {
return $this->returnJson([],'验证码不正确',400);
}
}
} else {
return $this->returnJson([],'请求方式不正确',400);
}
}
//用户设置密码
public function setPassword() {
$data['password'] = md5(Request::param('password'));
$save = Db::name('user')->where(['uid'=>$this->uid])->save($data);
if($save) {
return $this->returnJson();
} else {
return $this->returnJson([],'','111111');
}
}
//用户绑定房间
public function userBindVacancy(){
$data['type'] = Request::param('type');
$village_id = Request::param('village_id');
$vacancy_id = Request::param('vacancy_id');
$data['vacancy_id'] = $vacancy_id;
$data['name'] = $car['name'] = Request::param("name");
$data['phone'] = $car['phone'] = Request::param("phone");
$data['village_id'] = $car['village_id']= $village_id;
$is_exit = Common::getOwnerInfo($vacancy_id);
$data['id_card'] = Request::param("id_card");
$data['uid'] =$car['uid'] = $this->uid;
$data['status'] = 2;
$data['create_time'] =$car['create_time']= time();
$data['memo'] = Request::param('memo');
$data['emer_user'] = Request::param('emer_user');//紧急联系人
$data['emer_phone'] = Request::param('emer_phone'); //紧急联系人电话
$car['brand'] = Request::param('barnd');
$car['car_type'] = Request::param('car_type');
$car['license_plate'] = Request::param('license_plate');
//查看当前手机号是否重复绑定当前房间,一个房间对应的一个手机号只能有一种身份
$is_only = Db::name('house_user_bind')->where(['village_id'=>$village_id,'vacancy_id'=>$vacancy_id,'phone'=>$data['phone']])->where('status',"<>",3)->find();
if($is_only){
return $this->returnJson([],'用户已绑定房间!',400);
}
if($data['type'] == 0) { //申请业主不需要填写业主后四位手机号
//检查该房间是否有房主绑定
if(!empty($is_exit)){
return $this->returnJson([],'该房间下已有业主!',);
}
$save['id_card'] = $data['id_card'];
$save['name'] = $data['name'];
$save['phone'] = $data['phone'];
$save['uid'] = $data['uid'];
//修改房间的信息
Db::name('house_vacancy')->where(['vacancy_id'=>$vacancy_id])->save($save);
}else{
if(empty($is_exit)){
return $this->returnJson([],'该房间未绑定业主,不能绑定租客!',);
}
$ownerPhone = $is_exit['phone'];
$phone_num = Request::param('phone_num');
if($phone_num != substr($ownerPhone,7)){
return $this->returnJson([],'业主后四位手机号不正确!');
}
}
Db::startTrans();
try{
Db::name('house_user_bind')->save($data);
if($car['brand'] && $car['car_type'] && $car['license_plate']) {
Db::name('car')->save($car);
}
Db::commit();
return $this->returnJson();
}catch(\Exception $e){
Db::rollback();
return $this->returnJson([],'绑定失败,请重试!',400);
}
}
//用户申请解绑
public function userUnBind(){
$where['house_user_bind_id'] = Request::param('house_user_bind_id');
$where['uid'] = $this->uid;
$save['status'] = 4;
$change =Db::name('house_user_bind')->where($where)->save($save);
if($change) {
return $this->returnJson([],'申请成功!');
} else {
return $this->returnJson([],'申请失败!',400);
}
}
//查看所有的房间,如果是业主,展示房间下的所有租客或家属
public function getUserVacancy() {
$uid = $this->uid;
$where['hub.uid'] = $uid;
$data = Db::name('house_user_bind')->alias('hub')
->leftJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')
->leftJoin('house_village hvi','hvi.village_id = hub.village_id')
->where($where)->whereIn('hub.status',[1,2,4])
->field('hub.type,hub.house_user_bind_id,hub.vacancy_id,parent_id,hv.vacancy_code,hv.layout_id,hv.name,hv.phone,hvi.village_name')
->select()->toArray();
foreach ($data as $k =>$v) {
$data[$k]['vacancy_address'] = Common::getVacancyAddress($v['vacancy_code'],$v['layout_id']);
if($v['type'] ==0) {
//如果是业主身份,查找下面的租客或家属等
$data[$k]['myTenant'] = Db::name('house_user_bind')->where(['vacancy_id'=>$v['vacancy_id']])->whereIn('type',[1,2,3])->field('name,phone,status')->select()->toArray();
}else{
//如果是租客身份,查找所属业主
$data[$k]['myOwner'] = Db::name('house_user_bind')->where(['vacancy_id'=>$v['vacancy_id'],'type'=>0,'status'=>1])->field('name,phone,status')->find();
}
}
return $this->returnJson($data);
}
}
\ No newline at end of file
<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\facade\Db;
use think\facade\Request;
class Village extends Base
{
<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\facade\Db;
use think\facade\Request;
class Village extends Base
{
//搜索小区
}
\ No newline at end of file
<?php
namespace app\common\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
class Common extends BaseController
{
//获取省市区
public function getArea() {
$where['area_pid'] = Request::param('area_id',0);
$data = Db::name('area')->where($where)->field("area_id,area_name")->select()->toArray();
return $this->returnJson($data,'success');
}
//通过id获取地区名称
public function getName($id) {
$where['area_id'] = $id;
return Db::name('area')->where($where)->value('area_name');
}
//查看所有的银行列表
public function bankAccountList() {
$data = Db::name("bank_account")->select()->toArray();
return $this->returnJson($data,'success');
}
//对数组中的时间字段进行日期转换
public static function changeField($data,$field="create_time",$format="Y-m-d H:i:s") {
$prompt = "暂无";
if(empty($data)) {
return $data;
}
foreach($data as $k =>$v) {
if(is_array($v)) {
if(is_array($field)) {
foreach ($field as $value) {
if($v[$value] != 0 || !is_null($v[$value])){
$data[$k][$value] = date($format,$v[$value]);
} else {
$data[$k][$value] = $prompt;
}
}
} else {
if($v[$field] != 0 || !is_null($v[$field])){
$data[$k][$field] = date($format,$v[$field]);
} else {
$data[$k][$field] = $prompt;
}
}
} else {
if(is_array($field)) {
foreach ($field as $value) {
if(isset($data[$value]) || $data[$value] !=0){
$data[$value] = date($format,$data[$value]);
} else{
$data[$value] = $prompt;
}
}
break;
} else {
if(isset($data[$field]) || $data[$field] !=0) {
$data[$field] = date($format, $data[$field]);
} else {
$data[$field] = $prompt;
}
break;
}
}
}
return $data;
}
//对数组中的图片字段进行日期转换
public static function changeImg($data,$field="pic"){
if(empty($data)){
return $data;
}
if(isHTTPS()){
$http = "https://";
} else{
$http = 'http://';
}
$server_name = $http.$_SERVER['SERVER_NAME'].'/';
$prompt ='';
foreach($data as $k =>$v) {
if(is_array($v)) {
if(is_array($field)) {
foreach ($field as $value) {
if($v[$value] != '' || !is_null($v[$value])){
$data[$k][$value] = $server_name.$v[$value];
} else {
$data[$k][$value] = $prompt;
}
}
} else {
if($v[$field] != '' || !is_null($v[$field])){
$data[$k][$field] = $server_name.$v[$field];
} else {
$data[$k][$field] = $prompt;
}
}
} else {
if(is_array($field)) {
foreach ($field as $value) {
if(isset($data[$value]) || $data[$value] !=0){
$data[$value] =$server_name.$data[$value];
} else{
$data[$value] = $prompt;
}
}
break;
} else {
if(isset($data[$field]) || $data[$field] !=0) {
$data[$field] = $server_name.$data[$field];
} else {
$data[$field] = $prompt;
}
break;
}
}
}
return $data;
}
/**
* @param $url_ids string 1,2,3
* @param $navAl array
* @param $type int 1 所有都赋值,0 给特定的赋值
*/
public static function changeNav($url_ids,$navAl,$type=1) {
$urlsArr = explode(",",$url_ids);
if($type==1 ) {
return $navAl;
} else {
foreach($navAl as $k =>$v ) {
$navAl[$k]['is_power'] =0 ;
if(isset($v['property_nav_id']) && !in_array($v['property_nav_id'],$urlsArr)) {
unset($navAl[$k]) ;
}
if(isset($v['community_nav_id']) && !in_array($v['community_nav_id'],$urlsArr)) {
unset($navAl[$k]);
}
if(isset($v['admin_nav_id']) && !in_array($v['admin_nav_id'],$urlsArr)) {
unset($navAl[$k]);
}
}
}
return $navAl;
}
//获取流水详情,village_money_list表
public static function getWithDrawDetail($where) {
$data = Db::name('village_money_list')->alias('ml')
->leftJoin('house_village hv','ml.village_id = hv.village_id')
->leftJoin('house_property hp','hp.property_id = ml.property_id')
->where($where)
->field("ml.*,hv.village_name,hp.property_name")
->find();
if($data) {
$where_one['order_id'] = $data['order_id'];
switch ($data['table_name']) {
case "pay_order" :
$orderInfo = Db::name('pay_order')->alias('po')
->leftJoin('house_vacancy hv', 'hv.vacancy_id = po.vacancy_id')
->leftJoin('user u', 'po.uid = u.uid')
->field('po.*,hv.vacancy_address,u.phone,u.nickname')
->where($where_one)
->find();
break;
case "withdraw_order":
$orderInfo = Db::name('withdraw_order')
->where($where_one)->find();
break;
default:
return false;
}
return ['data' => $data, 'orderInfo' => $orderInfo];
} else {
return false;
}
}
//获取流水列表
public static function getWithDrawList($where,$page) {
$count = Db::name('village_money_list')->alias('ml')
->leftJoin('house_village hv','ml.village_id = hv.village_id')
->leftJoin('house_property hp','hp.property_id = ml.property_id')
->where($where)->count();
$data = Db::name('village_money_list')->alias('ml')
->leftJoin('house_village hv','ml.village_id = hv.village_id')
->leftJoin('house_property hp','hp.property_id = ml.property_id')
->where($where)
->field("ml.id,ml.type,ml.total_money,ml.income,ml.create_time,ml.now_village_money,ml.now_property_money,hv.village_name,hp.property_name")
->order('ml.create_time','desc')
->page($page,config('app.limit'))->select()->toArray();
return ['total'=>$count,'data'=>$data];
}
//房间导入
public static function importVacancy($filePath,$village_id) {
$where['village_id'] = $village_id;
//设置获取excel对象
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
//获取社区布局
$layout_list = Db::name('layout_list')->where($where)->column('layout_id','code');
$park_block_list = Db::name('park_block')->where($where)->column('park_block_id','name');
//获取社区不同布局的所有的建筑id
$errorData =[]; //存放导入失败的数据;
for($j=1;$j<$highestRow;$j++)
{
if(!empty($dataAll[$j][0]) && !empty($dataAll[$j][4]) &&!empty($dataAll[$j][7]) &&!empty($dataAll[$j][8])){
$msg = "";
$vacancyCode = $dataAll[$j][0]; //房间编号
$layout_list_code = $dataAll[$j][7];//布局
if($layout_list_code) {
$layout_id = $layout_list[$layout_list_code];
$data['layout_id'] = $layout_id;
$res_vacancy_code = self::getVacancyCode($vacancyCode,$layout_id);
if($res_vacancy_code['code'] !=200) {
$msg .= "房间编号不正确!";
}
$vacancy_code = $res_vacancy_code['msg'];
$data['vacancy_code'] = $vacancy_code;
$parent_id = $res_vacancy_code['parent_id'];
$data['parent_id'] = $parent_id;
} else {
$msg .="社区布局未选择!";
}
$name = $dataAll[$j][1];//业主名字
$id_card = $dataAll[$j][2];//布局;//身份证id
if($id_card && !isCreditNo($id_card)) {
$msg .= "身份证id不符合!";
}
$phone = $dataAll[$j][3];//布局;//手机号
if($phone && !isPhoneNo($phone)) {
$msg .="手机号不符合!";
}
$area = $dataAll[$j][4];//面积
$car_block_name = $dataAll[$j][5];//车库位置
$park_code = $dataAll[$j][6];//车位编号
if($car_block_name && $park_code) {
$park_block_id = $park_block_list[$car_block_name];
$park_car_id = Db::name('park_car')->where(['village_id'=>$village_id,'park_block_id'=>$park_block_id,'park_code'=>$park_code])->value('park_car_id');
$data['park_car_id'] = $park_car_id;
$data['park_block_id'] = $park_block_id;
}
$house_type = $dataAll[$j][8];//住宅类型
switch ($house_type) {
case "住宅" :
$type =1;
break;
case "商铺" :
$type = 2;
break;
case "办公" :
$type = 3;
break;
default :
$msg.= "住宅类型未选择!";
}
$data['name'] = $bind['name'] = $name;
$data['id_card'] = $bind['id_card']= $id_card;
$data['phone'] = $bind['phone'] = $phone;
$data['area'] = $area;
$data['house_type'] = $type;
$data['create_time'] = $bind['create_time'] = $bind['pass_time']= time();
$data['village_id'] = $bind['village_id'] = $village_id;
if(empty($msg)) { //表明没有错误
$bind['vacancy_id'] = Db::name('house_vacancy')->insertGetId($data);
//导入到userbind表中
$bind['type'] = 0;
$bind['status'] = 1;
if($bind['name'] && $bind['id_card'] && $bind['phone']){
Db::name('house_user_bind')->save($bind);
}
} else {
$dataAll[$j][] = $msg;
$errorData[] = $dataAll[$j];
}
}else {
$dataAll[$j][] = "数据不能为空!";
$errorData[] = $dataAll[$j];
}
}
if(empty($errorData)) {
return ['code'=>200,'data'=>[]];
}else {
//导出错误的数据
$file = "./formwork/importVacancy.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D','E','F','G','H','I','J'];
$i = 2;
$optionsArr = Db::name('layout_list')->where(['village_id'=>$village_id])->column('code');
$optionsString = implode(',',$optionsArr);//这个是选择布局
$car_blockArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('name');
$optionsCarBlockString = implode(',',$car_blockArr);//这个是车库布局
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('H'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('社区布局')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$phpExcel->getActiveSheet()->getCell('I'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('房屋类型')
-> setFormula1('"住宅,商铺,办公"');; //这一句为要设置数据有效性的单元格
if($optionsCarBlockString) {
$phpExcel->getActiveSheet()->getCell('F'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('车库位置')
-> setFormula1('"'.$optionsCarBlockString.'"');; //这一句为要设置数据有效性的单元格
}
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="房间导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
}
}
//通过房间编号获取房间的父id
public static function getParentId($layout_id,$vacancyCode) {
$arr = explode('-',$vacancyCode);
$level= 0;
$parent_id = 0;
foreach($arr as $k => $v) {
$level++;
$layout_build_id = Db::name('layout_build')->where(['layout_id'=>$layout_id,'code'=>$v,'level'=>$level,'parent_id'=>$parent_id])->value('layout_build_id');
if($layout_build_id) {
$parent_id = $layout_build_id;
}
}
return $parent_id;
}
//通过填写的用户的房间号来转化成真正的房间编号
public static function getVacancyCode($vacancyCode,$layout_id) {
$vacancyCodeArr = explode('-',$vacancyCode);
$roomCode = array_pop($vacancyCodeArr);//去掉最后一个是房间的
$vacancy_code = '';
$parent_id = 0;
$level = 1;
foreach ($vacancyCodeArr as $k => $v) {
$parent_id = Db::name('layout_build')->where(['layout_id'=>$layout_id,'parent_id'=>$parent_id,'code'=>$v,'level'=>$level])->value('layout_build_id');
if(!$parent_id){
return ['code'=>400,'msg'=>'房间编号不存在!'];
}
$vacancy_code = $vacancy_code.$parent_id."-";
$level++;
}
$arrCode = explode('-',rtrim($vacancy_code,'-'));
return ['code'=>200,'msg'=>$vacancy_code.$roomCode,'parent_id'=>array_pop($arrCode)];
}
//用户导入
public static function importTenant($filePath,$village_id) {
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$errorData =[]; //存放导入失败的数据;
$insertData = [];
$layout_list = Db::name('layout_list')->where($where)->column('layout_id','code');
for($i=1;$i<$highestRow;$i++) {
$msg="";
$name = $dataAll[$i][0];
if(is_null($name)){
continue;
}
$phone = $dataAll[$i][1];
if(!isPhoneNo($phone)){
$msg.="手机号不正确!";
}
$id_card = $dataAll[$i][2];
if(!isCreditNo($id_card)){
$msg.="身份证号码不正确!";
}
$layout_list_code = $dataAll[$i][5];
if($layout_list_code) {
$layout_id = $layout_list[$layout_list_code];
} else {
$msg.="社区布局未选择!";
}
$vacancyCode = $dataAll[$i][3];
//验证房间编号是否存在,是否合理
$res_vacancy_code = self::getVacancyCode($vacancyCode,$layout_id);
if($res_vacancy_code['code'] !=200) {
$msg.= "房间编号不正确!";
}
$where['vacancy_code'] = $res_vacancy_code['msg'];
$vacancy_id = Db::name('house_vacancy')->where($where)->value('vacancy_id');
$userType = $dataAll[$i][4];
switch($userType){
case "租客":
$type=2;
break;
case "业主":
$type=0;
break;
case "家属":
$type =1;
break;
default :
$msg.="未选择用户身份!";
}
//如果type=0,需要检测是否房间绑定了业主
if($type==0){
$is_exit = Db::name('house_user_bind')->where(['village_id'=>$village_id,'vacancy_id'=>$vacancy_id,'type'=>0,'status'=>1])->value('house_user_bind_id');
if($is_exit){
$msg.='该房间已绑定业主,请在解绑!';
}
}
$data['vacancy_id'] = $vacancy_id;
$data['village_id'] = $village_id;
$data['id_card'] = $id_card;
$data['name'] = $name;
$data['phone'] = $phone;
$data['type'] = $type;
$data['relatives_type'] =1; //默认
$data['create_time'] = $data['pass_time'] = time();
$data['status'] =1;
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][] = $msg;
$errorData[] = $dataAll[$i];
}
}
//导入对的数据
if(!empty($insertData)){
Db::name('house_user_bind')->insertAll($insertData);
}
//如果有错误的数据,导出
if($errorData){
//导出错误的数据
$file = "./formwork/importTenant.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D','E','F','G'];
$i = 2;
$optionsArr = Db::name('layout_list')->where(['village_id'=>$village_id])->column('code');
$optionsString = implode(',',$optionsArr);//这个是选择布局
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('F'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('社区布局')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$phpExcel->getActiveSheet()->getCell('E'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('房屋类型')
-> setFormula1('"租客,业主,家属"'); //这一句为要设置数据有效性的单元格
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else {
return ['code'=>200,'data'=>[]];
}
}
//车辆导入
public static function importCar($filePath,$village_id) {
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$optionsArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('park_block_id','name');
$errorData =[]; //存放导入失败的数据;
$insertData = [];
for($i=1;$i<$highestRow;$i++) {
$msg="";
$name = $dataAll[$i][0];
$phone = $dataAll[$i][1];
if(!isPhoneNo($phone)){
$msg.="用户手机号不正确!";
}
$license_plate = $dataAll[$i][2];
$car_color = $dataAll[$i][3];
$brand = $dataAll[$i][4];
$car_type = $dataAll[$i][5];
$data['name'] = $name;
$data['phone'] = $phone;
$data['license_plate'] = $license_plate;
$data['car_color'] = $car_color;
$data['create_time'] = time();
$data['village_id'] = $village_id;
$data['brand'] = $brand;
$data['car_type'] = $car_type;
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][] = $msg;
$errorData[] = $dataAll[$i];
}
}
if(!empty($insertData)){
Db::name('park_car')->insertAll($insertData);
if($errorData) {
//导出错误的数据
$file = "./formwork/importCar.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D','E','F','G'];
$i = 2;
$optionsArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('name');
$optionsString = implode(',',$optionsArr);//这个是的车库
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('F'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('车库')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else {
return ['code'=>200,'data'=>[]];
}
} else {
return ['code'=>200,'data'=>[]];
}
}
//车位导入
public static function importCarPark($filePath,$village_id) {
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$optionsArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('park_block_id','name');
$errorData =[]; //存放导入失败的数据;
$insertData = [];//存放最后需要插入到数据库的数据
for($i=1;$i<$highestRow;$i++) {
$msg="";
$park_code = $dataAll[$i][0];
$park_block_name = $dataAll[$i][1];
$type_name = $dataAll[$i][2];
if($type_name == "非机动车位"){
$data['type'] =0;
}
if($park_block_name) {
$park_block_id = $optionsArr[$park_block_name];
$data['park_block_id'] = $park_block_id;
} else{
$msg.="车库必选选!";
}
$data['park_code'] = $park_code;
//查询当前的车位是否已存在
$is_exit = Db::name('park_car')->where(['village_id'=>$village_id,'park_block_id'=>$park_block_id,'park_code'=>$park_code])->value('park_car_id');
if($is_exit) {
$msg.="该车位已存在!";
}
$data['create_time'] = time();
$data['village_id'] = $village_id;
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][] = $msg;
$errorData[] = $dataAll[$i];
}
}
if(!empty($insertData)) {
Db::name('park_car')->insertAll($insertData);
if($errorData){
//导出错误的数据
$file = "./formwork/importCarPark.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D'];
$i = 2;
$optionsArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('name');
$optionsString = implode(',',$optionsArr);//这个是的车库
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('B'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('车库')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else{
return ['code'=>200,'data'=>[]];
}
} else {
return ['code'=>400,'msg'=>'文件内容为空!'];
}
}
//每月水费导入
public static function importCost($filePath,$village_id,$type){
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$errorData =[]; //存放导入失败的数据;
$insertData = [];//存放最后需要插入到数据库的数据
for($i=1;$i<$highestRow;$i++) {
$msg="";
$vacancy_code = $dataAll[$i][0];
//判断房间号是否存在
$vacancyInfo = Db::name('house_vacancy')->alias('hv')
->leftJoin('layout_build lb','lb.layout_build_id = hv.parent_id')
->where(['hv.village_id'=>$village_id,'hv.vacancy_code'=>$vacancy_code])
->field('hv.vacancy_id,lb.water_price,lb.electric_price,lb.gas_price,hv.water_fee,hv.electric_fee,hv.gas_fee,hv.is_inherit')->find();
if($vacancyInfo) {
$data['vacancy_id'] = $vacancyInfo['vacancy_id'];
} else {
$msg.="物业编号不存在!";
}
switch ($type){
case "water":
if($vacancyInfo['is_inherit'] ==1) {
$price = $vacancyInfo['water_price'];
} else {
$price = $vacancyInfo['water_fee'];
}
$outFileName = "水费导入失败.xls";
break;
case "electric":
if($vacancyInfo['is_inherit'] ==1) {
$price = $vacancyInfo['electric_price'];
} else {
$price = $vacancyInfo['electric_fee'];
}
$outFileName = "电费导入失败.xls";
break;
case "gas":
if($vacancyInfo['is_inherit'] ==1) {
$price = $vacancyInfo['gas_price'];
} else {
$price = $vacancyInfo['gas_fee'];
}
$outFileName = "燃气费导入失败.xls";
break;
default :
$outFileName = "未知错误.xls";
return ['code'=>400,'msg'=>'小区费用未设置,请先设置费用!'];
}
//查看月份的费用是否存在
$data['cost_month'] = $dataAll[$i][1];
//判断月份是否超过了当前时间,不能导入超前月份
if(strtotime(date('Y-m',time())) <= strtotime($data['cost_month'])) {
$msg.="不允许导入超过当前月份的数据!";
}
$data['village_id'] = $village_id;
$data['type'] = $type;
$is_exit = Db::name('cost')->where($data)->find();
if($is_exit) {
$msg.="该条记录已存在!";
}
$data['price'] = $price;
$data['area'] = $dataAll[$i][2];
$data['total_money'] = $data['pay_money'] = number_format($price*$data['area'],2);
$data['is_pay'] = 0;
$data['create_time'] = time();
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][3] = $msg;
$errorData[] = $dataAll[$i];
}
}
if(!empty($insertData) || !empty($errorData)) {
Db::name('cost')->insertAll($insertData);
if($errorData){
//导出错误的数据
if($type == "water") {
$file = "./formwork/importWater.xls";
} else if($type == "electric") {
$file = "./formwork/importElectric.xls";
} else {
$file = "./formwork/importGas.xls";
}
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D'];
$i = 2;
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename='.$outFileName);
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else{
return ['code'=>200,'data'=>[]];
}
} else {
return ['code'=>400,'msg'=>'文件内容为空!'];
}
}
//验证短信验证码是否正确
public static function rightCode($phone,$code) {
$is_exit = Db::name('send_code')->where(['phone'=>$phone,'code'=>$code])->where('expires_time',">",time())->find();
if($is_exit){
return true;
}
return false;
}
//app用户登入之后同步数据
public static function synUserData($uid,$phone) {
//房间列表用户修改
Db::name('house_vacancy')->where(['phone'=>$phone])->save(['uid'=>$uid]);
//汽车关联
Db::name('car')->where(['phone'=>$phone])->save(['uid'=>$uid]);
}
//通过房间编号和布局id获取房间地址
public static function getVacancyAddress($vacancy_code,$layout_id) {
$layoutString = Db::name('layout_list')->where(['layout_id'=>$layout_id])->value('code');
$layoutArr = explode('-',$layoutString);
$vacancyCodeArr = explode('-',$vacancy_code);
$address="";
foreach ($vacancyCodeArr as $k =>$v){
$address.=$v.$layoutArr[$k];
}
return $address;
}
//通过房间id获取业主信息
public static function getOwnerInfo($vacancy_id) {
$data = Db::name('house_user_bind')->where(['vacancy_id'=>$vacancy_id,'type'=>0,'status'=>1])->field('type,status,phone,name')->find();
return $data??[];
}
//获取需要绑定人员设备
public static function getUserBindType() {
return ['0'=>'业主','1'=>'家人','2'=>'租客','3'=>'替换业主'];
}
//获取需要绑定人员设备
public static function getFamilyBindType() {
return ['1'=>'配偶','2'=>'父母','3'=>'子女','4'=>'亲朋好友'];
}
//根据查询到的小区id,获取当前用户所在小区绑定的房间
public static function getVacancy($village_id,$uid) {
$data = Db::name('house_user_bind')->alias('hub')->leftJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')->where(['hub.village_id'=>$village_id,'hub.uid'=>$uid])->whereIn('hub.status',[1,2,4])
->field('hub.house_user_bind_id,hub.vacancy_id,hub.type,hv.layout_id,hv.vacancy_code')->select()->toArray();
foreach ($data as $key =>$val) {
$data[$key]['vacancy_address'] = self::getVacancyAddress($val['vacancy_code'],$val['layout_id']);
}
return $data;
}
<?php
namespace app\common\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
class Common extends BaseController
{
//获取省市区
public function getArea() {
$where['area_pid'] = Request::param('area_id',0);
$data = Db::name('area')->where($where)->field("area_id,area_name")->select()->toArray();
return $this->returnJson($data,'success');
}
//通过id获取地区名称
public function getName($id) {
$where['area_id'] = $id;
return Db::name('area')->where($where)->value('area_name');
}
//查看所有的银行列表
public function bankAccountList() {
$data = Db::name("bank_account")->select()->toArray();
return $this->returnJson($data,'success');
}
//对数组中的时间字段进行日期转换
public static function changeField($data,$field="create_time",$format="Y-m-d H:i:s") {
$prompt = "暂无";
if(empty($data)) {
return $data;
}
foreach($data as $k =>$v) {
if(is_array($v)) {
if(is_array($field)) {
foreach ($field as $value) {
if($v[$value] != 0 || !is_null($v[$value])){
$data[$k][$value] = date($format,$v[$value]);
} else {
$data[$k][$value] = $prompt;
}
}
} else {
if($v[$field] != 0 || !is_null($v[$field])){
$data[$k][$field] = date($format,$v[$field]);
} else {
$data[$k][$field] = $prompt;
}
}
} else {
if(is_array($field)) {
foreach ($field as $value) {
if(isset($data[$value]) || $data[$value] !=0){
$data[$value] = date($format,$data[$value]);
} else{
$data[$value] = $prompt;
}
}
break;
} else {
if(isset($data[$field]) || $data[$field] !=0) {
$data[$field] = date($format, $data[$field]);
} else {
$data[$field] = $prompt;
}
break;
}
}
}
return $data;
}
//对数组中的图片字段进行日期转换
public static function changeImg($data,$field="pic"){
if(empty($data)){
return $data;
}
if(isHTTPS()){
$http = "https://";
} else{
$http = 'http://';
}
$server_name = $http.$_SERVER['SERVER_NAME'].'/';
$prompt ='';
foreach($data as $k =>$v) {
if(is_array($v)) {
if(is_array($field)) {
foreach ($field as $value) {
if($v[$value] != '' || !is_null($v[$value])){
$data[$k][$value] = $server_name.$v[$value];
} else {
$data[$k][$value] = $prompt;
}
}
} else {
if($v[$field] != '' || !is_null($v[$field])){
$data[$k][$field] = $server_name.$v[$field];
} else {
$data[$k][$field] = $prompt;
}
}
} else {
if(is_array($field)) {
foreach ($field as $value) {
if(isset($data[$value]) || $data[$value] !=0){
$data[$value] =$server_name.$data[$value];
} else{
$data[$value] = $prompt;
}
}
break;
} else {
if(isset($data[$field]) || $data[$field] !=0) {
$data[$field] = $server_name.$data[$field];
} else {
$data[$field] = $prompt;
}
break;
}
}
}
return $data;
}
/**
* @param $url_ids string 1,2,3
* @param $navAl array
* @param $type int 1 所有都赋值,0 给特定的赋值
*/
public static function changeNav($url_ids,$navAl,$type=1) {
$urlsArr = explode(",",$url_ids);
if($type==1 ) {
return $navAl;
} else {
foreach($navAl as $k =>$v ) {
$navAl[$k]['is_power'] =0 ;
if(isset($v['property_nav_id']) && !in_array($v['property_nav_id'],$urlsArr)) {
unset($navAl[$k]) ;
}
if(isset($v['community_nav_id']) && !in_array($v['community_nav_id'],$urlsArr)) {
unset($navAl[$k]);
}
if(isset($v['admin_nav_id']) && !in_array($v['admin_nav_id'],$urlsArr)) {
unset($navAl[$k]);
}
}
}
return $navAl;
}
//获取流水详情,village_money_list表
public static function getWithDrawDetail($where) {
$data = Db::name('village_money_list')->alias('ml')
->leftJoin('house_village hv','ml.village_id = hv.village_id')
->leftJoin('house_property hp','hp.property_id = ml.property_id')
->where($where)
->field("ml.*,hv.village_name,hp.property_name")
->find();
if($data) {
$where_one['order_id'] = $data['order_id'];
switch ($data['table_name']) {
case "pay_order" :
$orderInfo = Db::name('pay_order')->alias('po')
->leftJoin('house_vacancy hv', 'hv.vacancy_id = po.vacancy_id')
->leftJoin('user u', 'po.uid = u.uid')
->field('po.*,hv.vacancy_address,u.phone,u.nickname')
->where($where_one)
->find();
break;
case "withdraw_order":
$orderInfo = Db::name('withdraw_order')
->where($where_one)->find();
break;
default:
return false;
}
return ['data' => $data, 'orderInfo' => $orderInfo];
} else {
return false;
}
}
//获取流水列表
public static function getWithDrawList($where,$page) {
$count = Db::name('village_money_list')->alias('ml')
->leftJoin('house_village hv','ml.village_id = hv.village_id')
->leftJoin('house_property hp','hp.property_id = ml.property_id')
->where($where)->count();
$data = Db::name('village_money_list')->alias('ml')
->leftJoin('house_village hv','ml.village_id = hv.village_id')
->leftJoin('house_property hp','hp.property_id = ml.property_id')
->where($where)
->field("ml.id,ml.type,ml.total_money,ml.income,ml.create_time,ml.now_village_money,ml.now_property_money,hv.village_name,hp.property_name")
->order('ml.create_time','desc')
->page($page,config('app.limit'))->select()->toArray();
return ['total'=>$count,'data'=>$data];
}
//房间导入
public static function importVacancy($filePath,$village_id) {
$where['village_id'] = $village_id;
//设置获取excel对象
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
//获取社区布局
$layout_list = Db::name('layout_list')->where($where)->column('layout_id','code');
$park_block_list = Db::name('park_block')->where($where)->column('park_block_id','name');
//获取社区不同布局的所有的建筑id
$errorData =[]; //存放导入失败的数据;
for($j=1;$j<$highestRow;$j++)
{
if(!empty($dataAll[$j][0]) && !empty($dataAll[$j][4]) &&!empty($dataAll[$j][7]) &&!empty($dataAll[$j][8])){
$msg = "";
$vacancyCode = $dataAll[$j][0]; //房间编号
$layout_list_code = $dataAll[$j][7];//布局
if($layout_list_code) {
$layout_id = $layout_list[$layout_list_code];
$data['layout_id'] = $layout_id;
$res_vacancy_code = self::getVacancyCode($vacancyCode,$layout_id);
if($res_vacancy_code['code'] !=200) {
$msg .= "房间编号不正确!";
}
$vacancy_code = $res_vacancy_code['msg'];
$data['vacancy_code'] = $vacancy_code;
$parent_id = $res_vacancy_code['parent_id'];
$data['parent_id'] = $parent_id;
} else {
$msg .="社区布局未选择!";
}
$name = $dataAll[$j][1];//业主名字
$id_card = $dataAll[$j][2];//布局;//身份证id
if($id_card && !isCreditNo($id_card)) {
$msg .= "身份证id不符合!";
}
$phone = $dataAll[$j][3];//布局;//手机号
if($phone && !isPhoneNo($phone)) {
$msg .="手机号不符合!";
}
$area = $dataAll[$j][4];//面积
$car_block_name = $dataAll[$j][5];//车库位置
$park_code = $dataAll[$j][6];//车位编号
if($car_block_name && $park_code) {
$park_block_id = $park_block_list[$car_block_name];
$park_car_id = Db::name('park_car')->where(['village_id'=>$village_id,'park_block_id'=>$park_block_id,'park_code'=>$park_code])->value('park_car_id');
$data['park_car_id'] = $park_car_id;
$data['park_block_id'] = $park_block_id;
}
$house_type = $dataAll[$j][8];//住宅类型
switch ($house_type) {
case "住宅" :
$type =1;
break;
case "商铺" :
$type = 2;
break;
case "办公" :
$type = 3;
break;
default :
$msg.= "住宅类型未选择!";
}
$data['name'] = $bind['name'] = $name;
$data['id_card'] = $bind['id_card']= $id_card;
$data['phone'] = $bind['phone'] = $phone;
$data['area'] = $area;
$data['house_type'] = $type;
$data['create_time'] = $bind['create_time'] = $bind['pass_time']= time();
$data['village_id'] = $bind['village_id'] = $village_id;
if(empty($msg)) { //表明没有错误
$bind['vacancy_id'] = Db::name('house_vacancy')->insertGetId($data);
//导入到userbind表中
$bind['type'] = 0;
$bind['status'] = 1;
if($bind['name'] && $bind['id_card'] && $bind['phone']){
Db::name('house_user_bind')->save($bind);
}
} else {
$dataAll[$j][] = $msg;
$errorData[] = $dataAll[$j];
}
}else {
$dataAll[$j][] = "数据不能为空!";
$errorData[] = $dataAll[$j];
}
}
if(empty($errorData)) {
return ['code'=>200,'data'=>[]];
}else {
//导出错误的数据
$file = "./formwork/importVacancy.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D','E','F','G','H','I','J'];
$i = 2;
$optionsArr = Db::name('layout_list')->where(['village_id'=>$village_id])->column('code');
$optionsString = implode(',',$optionsArr);//这个是选择布局
$car_blockArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('name');
$optionsCarBlockString = implode(',',$car_blockArr);//这个是车库布局
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('H'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('社区布局')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$phpExcel->getActiveSheet()->getCell('I'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('房屋类型')
-> setFormula1('"住宅,商铺,办公"');; //这一句为要设置数据有效性的单元格
if($optionsCarBlockString) {
$phpExcel->getActiveSheet()->getCell('F'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('车库位置')
-> setFormula1('"'.$optionsCarBlockString.'"');; //这一句为要设置数据有效性的单元格
}
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="房间导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
}
}
//通过房间编号获取房间的父id
public static function getParentId($layout_id,$vacancyCode) {
$arr = explode('-',$vacancyCode);
$level= 0;
$parent_id = 0;
foreach($arr as $k => $v) {
$level++;
$layout_build_id = Db::name('layout_build')->where(['layout_id'=>$layout_id,'code'=>$v,'level'=>$level,'parent_id'=>$parent_id])->value('layout_build_id');
if($layout_build_id) {
$parent_id = $layout_build_id;
}
}
return $parent_id;
}
//通过填写的用户的房间号来转化成真正的房间编号
public static function getVacancyCode($vacancyCode,$layout_id) {
$vacancyCodeArr = explode('-',$vacancyCode);
$roomCode = array_pop($vacancyCodeArr);//去掉最后一个是房间的
$vacancy_code = '';
$parent_id = 0;
$level = 1;
foreach ($vacancyCodeArr as $k => $v) {
$parent_id = Db::name('layout_build')->where(['layout_id'=>$layout_id,'parent_id'=>$parent_id,'code'=>$v,'level'=>$level])->value('layout_build_id');
if(!$parent_id){
return ['code'=>400,'msg'=>'房间编号不存在!'];
}
$vacancy_code = $vacancy_code.$parent_id."-";
$level++;
}
$arrCode = explode('-',rtrim($vacancy_code,'-'));
return ['code'=>200,'msg'=>$vacancy_code.$roomCode,'parent_id'=>array_pop($arrCode)];
}
//用户导入
public static function importTenant($filePath,$village_id) {
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$errorData =[]; //存放导入失败的数据;
$insertData = [];
$layout_list = Db::name('layout_list')->where($where)->column('layout_id','code');
for($i=1;$i<$highestRow;$i++) {
$msg="";
$name = $dataAll[$i][0];
if(is_null($name)){
continue;
}
$phone = $dataAll[$i][1];
if(!isPhoneNo($phone)){
$msg.="手机号不正确!";
}
$id_card = $dataAll[$i][2];
if(!isCreditNo($id_card)){
$msg.="身份证号码不正确!";
}
$layout_list_code = $dataAll[$i][5];
if($layout_list_code) {
$layout_id = $layout_list[$layout_list_code];
} else {
$msg.="社区布局未选择!";
}
$vacancyCode = $dataAll[$i][3];
//验证房间编号是否存在,是否合理
$res_vacancy_code = self::getVacancyCode($vacancyCode,$layout_id);
if($res_vacancy_code['code'] !=200) {
$msg.= "房间编号不正确!";
}
$where['vacancy_code'] = $res_vacancy_code['msg'];
$vacancy_id = Db::name('house_vacancy')->where($where)->value('vacancy_id');
$userType = $dataAll[$i][4];
switch($userType){
case "租客":
$type=2;
break;
case "业主":
$type=0;
break;
case "家属":
$type =1;
break;
default :
$msg.="未选择用户身份!";
}
//如果type=0,需要检测是否房间绑定了业主
if($type==0){
$is_exit = Db::name('house_user_bind')->where(['village_id'=>$village_id,'vacancy_id'=>$vacancy_id,'type'=>0,'status'=>1])->value('house_user_bind_id');
if($is_exit){
$msg.='该房间已绑定业主,请在解绑!';
}
}
$data['vacancy_id'] = $vacancy_id;
$data['village_id'] = $village_id;
$data['id_card'] = $id_card;
$data['name'] = $name;
$data['phone'] = $phone;
$data['type'] = $type;
$data['relatives_type'] =1; //默认
$data['create_time'] = $data['pass_time'] = time();
$data['status'] =1;
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][] = $msg;
$errorData[] = $dataAll[$i];
}
}
//导入对的数据
if(!empty($insertData)){
Db::name('house_user_bind')->insertAll($insertData);
}
//如果有错误的数据,导出
if($errorData){
//导出错误的数据
$file = "./formwork/importTenant.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D','E','F','G'];
$i = 2;
$optionsArr = Db::name('layout_list')->where(['village_id'=>$village_id])->column('code');
$optionsString = implode(',',$optionsArr);//这个是选择布局
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('F'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('社区布局')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$phpExcel->getActiveSheet()->getCell('E'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('房屋类型')
-> setFormula1('"租客,业主,家属"'); //这一句为要设置数据有效性的单元格
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else {
return ['code'=>200,'data'=>[]];
}
}
//车辆导入
public static function importCar($filePath,$village_id) {
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$errorData =[]; //存放导入失败的数据;
$insertData = [];
for($i=1;$i<$highestRow;$i++) {
$msg="";
$name = $dataAll[$i][0];
$phone = $dataAll[$i][1];
if(!isPhoneNo($phone)){
$msg.="用户手机号不正确!";
}
$license_plate = $dataAll[$i][2];
$car_color = $dataAll[$i][3];
$brand = $dataAll[$i][4];
$car_type = $dataAll[$i][5];
$data['name'] = $name;
$data['phone'] = $phone;
$data['license_plate'] = $license_plate;
$data['car_color'] = $car_color;
$data['create_time'] = time();
$data['village_id'] = $village_id;
$data['brand'] = $brand;
$data['car_type'] = $car_type;
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][] = $msg;
$errorData[] = $dataAll[$i];
}
}
if(!empty($insertData)){
Db::name('park_car')->insertAll($insertData);
if($errorData) {
//导出错误的数据
$file = "./formwork/importCar.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D','E','F','G'];
$i = 2;
$optionsArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('name');
$optionsString = implode(',',$optionsArr);//这个是的车库
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('F'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('车库')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else {
return ['code'=>200,'data'=>[]];
}
} else {
return ['code'=>200,'data'=>[]];
}
}
//车位导入
public static function importCarPark($filePath,$village_id) {
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$optionsArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('park_block_id','name');
$errorData =[]; //存放导入失败的数据;
$insertData = [];//存放最后需要插入到数据库的数据
for($i=1;$i<$highestRow;$i++) {
$msg="";
$park_code = $dataAll[$i][0];
$park_block_name = $dataAll[$i][1];
$type_name = $dataAll[$i][2];
if($type_name == "非机动车位"){
$data['type'] =0;
}
if($park_block_name) {
$park_block_id = $optionsArr[$park_block_name];
$data['park_block_id'] = $park_block_id;
} else{
$msg.="车库必选选!";
}
$data['park_code'] = $park_code;
//查询当前的车位是否已存在
$is_exit = Db::name('park_car')->where(['village_id'=>$village_id,'park_block_id'=>$park_block_id,'park_code'=>$park_code])->value('park_car_id');
if($is_exit) {
$msg.="该车位已存在!";
}
$data['create_time'] = time();
$data['village_id'] = $village_id;
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][] = $msg;
$errorData[] = $dataAll[$i];
}
}
if(!empty($insertData)) {
Db::name('park_car')->insertAll($insertData);
if($errorData){
//导出错误的数据
$file = "./formwork/importCarPark.xls";
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D'];
$i = 2;
$optionsArr = Db::name('park_block')->where(['village_id'=>$village_id])->column('name');
$optionsString = implode(',',$optionsArr);//这个是的车库
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
//设置单元楼下拉
$phpExcel->getActiveSheet()->getCell('B'.$i)->getDataValidation()-> setType(\PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('车库')
-> setFormula1('"'.$optionsString.'"');; //这一句为要设置数据有效性的单元格
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户导入失败数据.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else{
return ['code'=>200,'data'=>[]];
}
} else {
return ['code'=>400,'msg'=>'文件内容为空!'];
}
}
//每月水费导入
public static function importCost($filePath,$village_id,$type){
$where['village_id'] = $village_id;
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本,因为office版本可以向下兼容
$objPHPExcel = $objReader->load($filePath,$encode='utf-8');//$file 为解读的excel文件
$sheet = $objPHPExcel->getSheet(0);
$dataAll = $objPHPExcel->getSheet(0)->toArray(); //获取导入的数据;
$highestRow = $sheet->getHighestRow(); // 取得总行数
$errorData =[]; //存放导入失败的数据;
$insertData = [];//存放最后需要插入到数据库的数据
for($i=1;$i<$highestRow;$i++) {
$msg="";
$vacancy_code = $dataAll[$i][0];
//判断房间号是否存在
$vacancyInfo = Db::name('house_vacancy')->alias('hv')
->leftJoin('layout_build lb','lb.layout_build_id = hv.parent_id')
->where(['hv.village_id'=>$village_id,'hv.vacancy_code'=>$vacancy_code])
->field('hv.vacancy_id,lb.water_price,lb.electric_price,lb.gas_price,hv.water_fee,hv.electric_fee,hv.gas_fee,hv.is_inherit')->find();
if($vacancyInfo) {
$data['vacancy_id'] = $vacancyInfo['vacancy_id'];
} else {
$msg.="物业编号不存在!";
}
switch ($type){
case "water":
if($vacancyInfo['is_inherit'] ==1) {
$price = $vacancyInfo['water_price'];
} else {
$price = $vacancyInfo['water_fee'];
}
$outFileName = "水费导入失败.xls";
break;
case "electric":
if($vacancyInfo['is_inherit'] ==1) {
$price = $vacancyInfo['electric_price'];
} else {
$price = $vacancyInfo['electric_fee'];
}
$outFileName = "电费导入失败.xls";
break;
case "gas":
if($vacancyInfo['is_inherit'] ==1) {
$price = $vacancyInfo['gas_price'];
} else {
$price = $vacancyInfo['gas_fee'];
}
$outFileName = "燃气费导入失败.xls";
break;
default :
$outFileName = "未知错误.xls";
return ['code'=>400,'msg'=>'小区费用未设置,请先设置费用!'];
}
//查看月份的费用是否存在
$data['cost_month'] = $dataAll[$i][1];
//判断月份是否超过了当前时间,不能导入超前月份
if(strtotime(date('Y-m',time())) <= strtotime($data['cost_month'])) {
$msg.="不允许导入超过当前月份的数据!";
}
$data['village_id'] = $village_id;
$data['type'] = $type;
$is_exit = Db::name('cost')->where($data)->find();
if($is_exit) {
$msg.="该条记录已存在!";
}
$data['price'] = $price;
$data['area'] = $dataAll[$i][2];
$data['total_money'] = $data['pay_money'] = number_format($price*$data['area'],2);
$data['is_pay'] = 0;
$data['create_time'] = time();
if(empty($msg)){
$insertData[] = $data;
} else {
$dataAll[$i][3] = $msg;
$errorData[] = $dataAll[$i];
}
}
if(!empty($insertData) || !empty($errorData)) {
Db::name('cost')->insertAll($insertData);
if($errorData){
//导出错误的数据
if($type == "water") {
$file = "./formwork/importWater.xls";
} else if($type == "electric") {
$file = "./formwork/importElectric.xls";
} else {
$file = "./formwork/importGas.xls";
}
$phpExcel = \PHPExcel_IOFactory::load($file);
$title = ['A','B','C','D'];
$i = 2;
foreach ($errorData as $k =>$v) {
foreach ($title as $key=>$value) {
$phpExcel->getActiveSheet()->setCellValue($value.$i,$v[$key]);
}
$i++;
}
$objWriter = new \PHPExcel_Writer_Excel5($phpExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename='.$outFileName);
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
} else{
return ['code'=>200,'data'=>[]];
}
} else {
return ['code'=>400,'msg'=>'文件内容为空!'];
}
}
//验证短信验证码是否正确
public static function rightCode($phone,$code) {
$is_exit = Db::name('send_code')->where(['phone'=>$phone,'code'=>$code])->where('expires_time',">",time())->find();
if($is_exit){
return true;
}
return false;
}
//app用户登入之后同步数据
public static function synUserData($uid,$phone) {
//房间列表用户修改
Db::name('house_vacancy')->where(['phone'=>$phone])->save(['uid'=>$uid]);
//汽车关联
Db::name('car')->where(['phone'=>$phone])->save(['uid'=>$uid]);
}
//通过房间编号和布局id获取房间地址
public static function getVacancyAddress($vacancyCode,$layout_id) {
$layoutCode = Db::name('layout_list')->where(['layout_id'=>$layout_id])->value('code');
$codeArr = explode('-',$layoutCode);
$rom = array_pop($codeArr); //获取最后一个房间的单位
$arr = explode('-',$vacancyCode);
$room_code = array_pop($arr);
$string = '';
$room_num = '';
foreach ($arr as $v) {
$code_name = Db::name('layout_build')->where(['layout_build_id'=>$v])->field('code,level')->find();
$key = $code_name['level'] -1;
$string.=$code_name['code'].$codeArr[$key];
$room_num.=$code_name['code']."-";
}
$room_num = $room_num.$room_code;
return ['vacancy_address'=>$string.$room_code.$rom,'room_num'=>$room_num];
}
//通过房间id获取业主信息
public static function getOwnerInfo($vacancy_id) {
$data = Db::name('house_user_bind')->where(['vacancy_id'=>$vacancy_id,'type'=>0,'status'=>1])->field('type,status,phone,name')->find();
return $data??[];
}
//获取需要绑定人员设备
public static function getUserBindType() {
return ['0'=>'业主','1'=>'家人','2'=>'租客','3'=>'替换业主'];
}
//获取需要绑定人员设备
public static function getFamilyBindType() {
return ['1'=>'配偶','2'=>'父母','3'=>'子女','4'=>'亲朋好友'];
}
//根据查询到的小区id,获取当前用户所在小区绑定的房间
public static function getVacancy($village_id,$uid) {
$data = Db::name('house_user_bind')->alias('hub')->leftJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id')->where(['hub.village_id'=>$village_id,'hub.uid'=>$uid])->whereIn('hub.status',[1,2,4])
->field('hub.house_user_bind_id,hub.vacancy_id,hub.type,hv.layout_id,hv.vacancy_code')->select()->toArray();
foreach ($data as $key =>$val) {
$res = self::getVacancyAddress($val['vacancy_code'],$val['layout_id']);
$data[$key]['vacancy_address'] = $res['vacancy_address'];
}
return $data;
}
}
\ No newline at end of file
<?php
namespace app\shequ\controller;
use app\admin\controller\Common;
use think\facade\Db;
use think\facade\Request;
//这是关于创建建筑之类的接口
class Build extends Base
{
protected $middleware = ['app\middleware\CommunityLoginCheck::class','app\middleware\CommunityAfter::class'];
//添加建筑时候选择布局
public function layoutInfo() {
$where['village_id'] = $this->village_id;
$data = Db::name('layout_list')->where($where)->select()->toArray();
$res = [];
foreach ($data as $k =>$v) {
$arr = explode('-',$v['code']);
array_pop($arr); //最后一个是房间的,去掉
$res[] = $this->zuhe($arr,$v['layout_id']);
}
return $this->returnJson($res,'success');
}
public function zuhe($arr,$layout_id) {
$res['code'] = $arr[0];
$res['layout_id'] = $layout_id;
$nam = ['code'];
$res['son']['code'] = isset($arr[1]) ? $arr[1] : [];
if(!$res['son']['code']){
unset($res['son']['code']);
return $res;
}
$res['son']['son']['code'] = isset($arr[2]) ? $arr[2] :[];
if(!$res['son']['son']['code']){
unset($res['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['code'] = isset($arr[3]) ? $arr[3] : [];
if(!$res['son']['son']['son']['code']){
unset($res['son']['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['son']['code'] = isset($arr[4]) ? $arr[4] : [];
if(!$res['son']['son']['son']['son']['code']){
unset($res['son']['son']['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['son']['son']['code'] = isset($arr[5]) ? $arr[5] : [];
if(!$res['son']['son']['son']['son']['son']['code']){
unset($res['son']['son']['son']['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['son']['son']['son'] = isset($arr[6]) ? $arr[6] : [];
if(!$res['son']['son']['son']['son']['son']['son']){
unset($res['son']['son']['son']['son']['son']['son']['code']);
return $res;
}
}
//创建建筑
public function createBuild() {
$village_id = $this->village_id;
$data['layout_id'] = Request::param('layout_id');
$data['code'] = Request::param('code');
$layoutInfo = DB::name('layout_list')->where(['layout_id'=>$data['layout_id']])->value('code');
if($layoutInfo) {
$codeArr = explode('-',$layoutInfo);
} else {
return $this->returnJson([],'未找到对应的模板',400);
}
$data['parent_id'] = Request::param('parent_id',0);
//查询是否有相同的编号
$is_exit = DB::name('layout_build')->where($data)->find();
if($is_exit) {
return $this->returnJson([],'相同编号建筑已存在!',400);
}
//位置 拼接
if($data['parent_id'] ==0) {
$village_name = DB::name('house_village')->where(['village_id'=>$village_id])->value('village_name');
$data['level'] = 1;
} else {
$village_name = DB::name('layout_build')->where(['layout_build_id'=>$data['parent_id']])->value('address');
$p_level = Db::name('layout_build')->where(['layout_build_id'=>$data['parent_id']])->value('level');
$data['level'] = ++$p_level;
}
$addre = $data['code'].$codeArr[$data['level']-1]; //小区编号转码
$data['property_price'] = $save['property_price']= Request::param('property_price');
$data['water_price'] = $save['water_price']= Request::param('water_price');
$data['electric_price'] = $save['electric_price']= Request::param('electric_price');
$data['gas_price'] = $save['gas_price']= Request::param('gas_price');
$data['parking_price'] = $save['parking_price']= Request::param('parking_price');
$data['explain'] = Request::param('explain');
$data['village_id'] = $village_id;
$data['name'] = $codeArr[$data['level']-1];
$data['create_time'] = time();
$data['sort_id'] = Request::param('sort_id');
$data['is_inherit'] = Request::param('is_inherit',1);//默认集成上一级的费用明细
$data['measure_area'] = Request::param('measure_area');//面积
$data['address'] = $village_name.$addre;
//判断是否设置了小区的参数,设置了就不用设置,
$is_exit = DB::name('house_village')->where(['village_id'=>$village_id])->field('village_id,property_price,water_price,electric_price,gas_price,park_price')->find();
if(!$is_exit['property_price'] && !$is_exit['water_price'] &&!$is_exit['electric_price'] &&!$is_exit['gas_price']&&!$is_exit['parking_price'] ) {
DB::name('house_village')->where(['village_id'=>$village_id])->save($save);
}
$insert = Db::name('layout_build')->insert($data);
if($insert) {
return $this->returnJson([],'success');
} else{
return $this->returnJson([],'error',400);
}
}
//修改建筑信息
public function changeBuild() {
$layout_build_id = Request::param('layout_build_id');
$where['layout_build_id'] = $layout_build_id;
//先查询原先的数据
$buildInfo = Db::name('layout_build')->where($where)->find();
$code = Request::param('code');
//查询修改的code是否存在
$is_exit = Db::name('layout_build')->where(['village_id'=>$buildInfo['village_id'],'layout_id'=>$buildInfo['layout_id'],'parent_id'=>$buildInfo['parent_id'],'code'=>$code])->where('layout_build_id','<>',$buildInfo['layout_build_id'])->find();
if($is_exit) {
return $this->returnJson([],'该建筑编号已存在!',400);
}
$data['code']= $code;
$data['explain']= Request::param('explain');
$data['measure_area']= Request::param('measure_area');
$data['property_price'] = $where['property_price'] = Request::param('property_price');
$data['water_price'] = $where['water_price'] = Request::param('water_price');
$data['electric_price'] = $where['electric_price'] = Request::param('electric_price');
$data['gas_price'] = $where['gas_price'] = Request::param('gas_price');
$data['parking_price'] = $where['parking_price'] = Request::param('parking_price');
$data['parent_id'] = $buildInfo['parent_id'];
$is_inherit = Request::param('is_inherit',1); //物业费等是否继承上一级,1继承,0不继承
if($is_inherit == 1 ) {
if($data['parent_id'] == 0) {
$is_find = Db::name('house_village')->where(['village_id'=>$this->village_id])->where($where)->find();
} else {
$is_find = Db::name('layout_build')->where(['village_id'=>$this->village_id,'layout_build_id'=>$data['parent_id']])->where($where)->find();
}
if(!$is_find){
return $this->returnJson([],'费用有误,请重新输入!',400);
}
}
//判断是否修改了费用字段
if($data['property_price'] != $buildInfo['property_price'] || $data['water_price'] != $buildInfo['water_price'] || $data['electric_price'] != $buildInfo['electric_price'] || $data['gas_price'] != $buildInfo['gas_price'] || $data['parking_price'] != $buildInfo['parking_price'] ) {
//先修改下面的建筑,然后在修改其他
$ids = $this->findBuildId($layout_build_id);
unset($where['layout_build_id']);
Db::name('layout_build')->where('layout_build_id','in',$ids)->save($where);
}
$data['is_inherit'] = $is_inherit;
$change = Db::name('layout_build')->where(['layout_build_id'=>$layout_build_id])->save($data);
if($change) {
return $this->returnJson([],'success');
} else{
return $this->returnJson([],'error',400);
}
}
//删除建筑
public function deleteBuild() {
//如果该建筑下有子建筑,则无法删除
$layout_build_id = Request::param('layout_build_id');
$is_exit = Db::name('layout_build')->where(['village_id'=>$this->village_id,'parent_id'=>$layout_build_id])->find();
if($is_exit) {
return $this->returnJson([],'该建筑有子建筑,无法删除!',400);
}
$del = Db::name('layout_build')->where(['layout_build_id'=>$layout_build_id])->delete();
if($del) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'删除失败',400);
}
}
//建筑列表
public function buildList() {
$village_id = $this->village_id;
$where['lb.village_id'] = $village_id;
$where['lb.parent_id'] = Request::param('parent_id',0);
if(Request::param('layout_id')) {
$where['lb.layout_id'] = Request::param('layout_id');
}
$page = Request::param('page',1);
$data = Db::name('layout_build')->alias('lb')->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')->where($where)->order(['lb.layout_id'=>'asc','lb.sort_id'=>'desc'])->page($page,config('app.limit'))
->field("lb.*,ll.name as layout_name,ll.level as layout_level")
->select()
->toArray();
foreach ($data as $k => $v) {
$is_exit = Db::name('layout_build')->where(['parent_id'=>$v['layout_build_id']])->value('layout_build_id');
if($is_exit){
$data[$k]['hasChildren'] = true;
} else{
$data[$k]['hasChildren'] = false;
}
//查看当前是否是最后一级建筑
if($v['level'] == ($v['layout_level']-1)){
$data[$k]['isEnd'] = true;
//获取建筑的物业编号
$code = [];
$data[$k]['buildNum'] = $this->createNum($v['layout_build_id'],$code);
} else {
$data[$k]['isEnd'] = false;
$data[$k]['buildNum'] = '-';
}
}
$count = Db::name('layout_build')->alias('lb')->where($where)->count();
$res['count'] = $count;
$res['data'] = Common::changeField($data);
return $this->returnJson($res,'success');
}
//根据建筑id拼接物业编号
public function createNum($build_id,$code) {
$data = Db::name('layout_build')->where(['layout_build_id'=>$build_id])->field('layout_build_id,parent_id,code')->find();
array_unshift($code,$data['code']);
if($data['parent_id'] == 0) {
return implode('-',$code);
} else {
return $this->createNum($data['parent_id'],$code);
}
}
//根据建筑id获取同一级的建筑
public function getCurrent() {
$build_id = Request::param('build_id');
$layoutInfo = Db::name('layout_build')->alias('lb')
->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')
->where(['layout_build_id'=>$build_id])
->field('lb.level,lb.code,lb.layout_build_id,lb.layout_id,lb.village_id,ll.code as ll_code')->find();
$where['layout_id'] = $layoutInfo['layout_id'];
$where['village_id'] = $layoutInfo['village_id'];
$where['level'] = $layoutInfo['level'];
$data = Db::name('layout_build')->where($where)->field('layout_build_id,layout_id,code,level')->select()->toArray();
if($layoutInfo){
$res['code_name'] = explode('-',$layoutInfo['ll_code'])[$layoutInfo['level']-1];
} else {
$res['code_name'] ='';
}
$res['data'] = $data;
return $this->returnJson($res);
}
//查看社区的水电物业费等
public function costPrice() {
$where['village_id'] = Request::param('village_id');
$data =DB::name('house_village')->where($where)->field('village_id,property_price,water_price,electric_price,gas_price,park_price')->find();
return $this->returnJson($data,'success');
}
//根据id查看建筑详情
public function queryCostPrice(){
$where['lb.layout_build_id'] = Request::param('layout_build_id');//建筑id
$where['lb.village_id'] = $this->village_id;
$res = Db::name('layout_build')->alias('lb')->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')->where($where)
->field('lb.is_inherit,lb.layout_build_id,lb.layout_build_id,lb.property_price,lb.water_price,lb.electric_price,lb.gas_price,lb.parking_price,lb.code,lb.explain,lb.parent_id,lb.create_time,lb.measure_area,lb.address,lb.sort_id,ll.code as code_name, ll.name')
->find();
$data = Common::changeField($res);
return $this->returnJson($data,'success');
}
//获取水电费等集成当前建筑的id
public function findBuildId($pid){
static $id = [];
$ids = DB::name('layout_build')->where(['parent_id'=>$pid,'is_inherit'=>1])->column('layout_build_id');
if(!empty($ids)){
foreach ($ids as $v) {
$this->findBuildId($v);
$id[] = $v;
}
}
sort($id);
return $id;
}
//获取子元素的所有建筑ID
public function allBuildId($pid) {
static $id = [];
$ids = DB::name('layout_build')->where(['parent_id'=>$pid])->column('layout_build_id');
if(!empty($ids)){
foreach ($ids as $v) {
$this->findBuildId($v);
$id[] = $v;
}
}
sort($id);
return $id;
}
<?php
namespace app\shequ\controller;
use app\admin\controller\Common;
use think\facade\Db;
use think\facade\Request;
//这是关于创建建筑之类的接口
class Build extends Base
{
protected $middleware = ['app\middleware\CommunityLoginCheck::class','app\middleware\CommunityAfter::class'];
//添加建筑时候选择布局
public function layoutInfo() {
$where['village_id'] = $this->village_id;
$data = Db::name('layout_list')->where($where)->select()->toArray();
$res = [];
foreach ($data as $k =>$v) {
$arr = explode('-',$v['code']);
array_pop($arr); //最后一个是房间的,去掉
$res[] = $this->zuhe($arr,$v['layout_id']);
}
return $this->returnJson($res,'success');
}
public function zuhe($arr,$layout_id) {
$res['code'] = $arr[0];
$res['layout_id'] = $layout_id;
$nam = ['code'];
$res['son']['code'] = isset($arr[1]) ? $arr[1] : [];
if(!$res['son']['code']){
unset($res['son']['code']);
return $res;
}
$res['son']['son']['code'] = isset($arr[2]) ? $arr[2] :[];
if(!$res['son']['son']['code']){
unset($res['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['code'] = isset($arr[3]) ? $arr[3] : [];
if(!$res['son']['son']['son']['code']){
unset($res['son']['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['son']['code'] = isset($arr[4]) ? $arr[4] : [];
if(!$res['son']['son']['son']['son']['code']){
unset($res['son']['son']['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['son']['son']['code'] = isset($arr[5]) ? $arr[5] : [];
if(!$res['son']['son']['son']['son']['son']['code']){
unset($res['son']['son']['son']['son']['son']['code']);
return $res;
}
$res['son']['son']['son']['son']['son']['son'] = isset($arr[6]) ? $arr[6] : [];
if(!$res['son']['son']['son']['son']['son']['son']){
unset($res['son']['son']['son']['son']['son']['son']['code']);
return $res;
}
}
//创建建筑
public function createBuild() {
$village_id = $this->village_id;
$data['layout_id'] = Request::param('layout_id');
$data['code'] = Request::param('code');
$layoutInfo = DB::name('layout_list')->where(['layout_id'=>$data['layout_id']])->value('code');
if($layoutInfo) {
$codeArr = explode('-',$layoutInfo);
} else {
return $this->returnJson([],'未找到对应的模板',400);
}
$data['parent_id'] = Request::param('parent_id',0);
//查询是否有相同的编号
$is_exit = DB::name('layout_build')->where($data)->find();
if($is_exit) {
return $this->returnJson([],'相同编号建筑已存在!',400);
}
//位置 拼接
if($data['parent_id'] ==0) {
$village_name = DB::name('house_village')->where(['village_id'=>$village_id])->value('village_name');
$data['level'] = 1;
} else {
$village_name = DB::name('layout_build')->where(['layout_build_id'=>$data['parent_id']])->value('address');
$p_level = Db::name('layout_build')->where(['layout_build_id'=>$data['parent_id']])->value('level');
$data['level'] = ++$p_level;
}
$addre = $data['code'].$codeArr[$data['level']-1]; //小区编号转码
$data['property_price'] = $save['property_price']= Request::param('property_price');
$data['water_price'] = $save['water_price']= Request::param('water_price');
$data['electric_price'] = $save['electric_price']= Request::param('electric_price');
$data['gas_price'] = $save['gas_price']= Request::param('gas_price');
$data['parking_price'] = $save['parking_price']= Request::param('parking_price');
$data['explain'] = Request::param('explain');
$data['village_id'] = $village_id;
$data['name'] = $codeArr[$data['level']-1];
$data['create_time'] = time();
$data['sort_id'] = Request::param('sort_id');
$data['is_inherit'] = Request::param('is_inherit',1);//默认集成上一级的费用明细
$data['measure_area'] = Request::param('measure_area');//面积
$data['address'] = $village_name.$addre;
//判断是否设置了小区的参数,设置了就不用设置,
$is_exit = DB::name('house_village')->where(['village_id'=>$village_id])->field('village_id,property_price,water_price,electric_price,gas_price,park_price')->find();
if(!$is_exit['property_price'] && !$is_exit['water_price'] &&!$is_exit['electric_price'] &&!$is_exit['gas_price']&&!$is_exit['parking_price'] ) {
DB::name('house_village')->where(['village_id'=>$village_id])->save($save);
}
$insert = Db::name('layout_build')->insert($data);
if($insert) {
return $this->returnJson([],'success');
} else{
return $this->returnJson([],'error',400);
}
}
//修改建筑信息
public function changeBuild() {
$layout_build_id = Request::param('layout_build_id');
$where['layout_build_id'] = $layout_build_id;
//先查询原先的数据
$buildInfo = Db::name('layout_build')->where($where)->find();
$code = Request::param('code');
//查询修改的code是否存在
$is_exit = Db::name('layout_build')->where(['village_id'=>$buildInfo['village_id'],'layout_id'=>$buildInfo['layout_id'],'parent_id'=>$buildInfo['parent_id'],'code'=>$code])->where('layout_build_id','<>',$buildInfo['layout_build_id'])->find();
if($is_exit) {
return $this->returnJson([],'该建筑编号已存在!',400);
}
$data['code']= $code;
$data['explain']= Request::param('explain');
$data['measure_area']= Request::param('measure_area');
$data['property_price'] = $where['property_price'] = Request::param('property_price');
$data['water_price'] = $where['water_price'] = Request::param('water_price');
$data['electric_price'] = $where['electric_price'] = Request::param('electric_price');
$data['gas_price'] = $where['gas_price'] = Request::param('gas_price');
$data['parking_price'] = $where['parking_price'] = Request::param('parking_price');
$data['parent_id'] = $buildInfo['parent_id'];
$is_inherit = Request::param('is_inherit',1); //物业费等是否继承上一级,1继承,0不继承
if($is_inherit == 1 ) {
if($data['parent_id'] == 0) {
$is_find = Db::name('house_village')->where(['village_id'=>$this->village_id])->where($where)->find();
} else {
$is_find = Db::name('layout_build')->where(['village_id'=>$this->village_id,'layout_build_id'=>$data['parent_id']])->where($where)->find();
}
if(!$is_find){
return $this->returnJson([],'费用有误,请重新输入!',400);
}
}
//判断是否修改了费用字段
if($data['property_price'] != $buildInfo['property_price'] || $data['water_price'] != $buildInfo['water_price'] || $data['electric_price'] != $buildInfo['electric_price'] || $data['gas_price'] != $buildInfo['gas_price'] || $data['parking_price'] != $buildInfo['parking_price'] ) {
//先修改下面的建筑,然后在修改其他
$ids = $this->findBuildId($layout_build_id);
unset($where['layout_build_id']);
Db::name('layout_build')->where('layout_build_id','in',$ids)->save($where);
}
$data['is_inherit'] = $is_inherit;
$change = Db::name('layout_build')->where(['layout_build_id'=>$layout_build_id])->save($data);
if($change) {
return $this->returnJson([],'success');
} else{
return $this->returnJson([],'error',400);
}
}
//删除建筑
public function deleteBuild() {
//如果该建筑下有子建筑,则无法删除
$layout_build_id = Request::param('layout_build_id');
$is_exit = Db::name('layout_build')->where(['village_id'=>$this->village_id,'parent_id'=>$layout_build_id])->find();
if($is_exit) {
return $this->returnJson([],'该建筑有子建筑,无法删除!',400);
}
$del = Db::name('layout_build')->where(['layout_build_id'=>$layout_build_id])->delete();
if($del) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'删除失败',400);
}
}
//建筑列表
public function buildList() {
$village_id = $this->village_id;
$where['lb.village_id'] = $village_id;
$where['lb.parent_id'] = Request::param('parent_id',0);
if(Request::param('layout_id')) {
$where['lb.layout_id'] = Request::param('layout_id');
}
$page = Request::param('page',1);
$data = Db::name('layout_build')->alias('lb')->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')->where($where)->order(['lb.layout_id'=>'asc','lb.sort_id'=>'desc'])->page($page,config('app.limit'))
->field("lb.*,ll.name as layout_name,ll.level as layout_level")
->select()
->toArray();
foreach ($data as $k => $v) {
$is_exit = Db::name('layout_build')->where(['parent_id'=>$v['layout_build_id']])->value('layout_build_id');
if($is_exit){
$data[$k]['hasChildren'] = true;
} else{
$data[$k]['hasChildren'] = false;
}
//查看当前是否是最后一级建筑
if($v['level'] == ($v['layout_level']-1)){
$data[$k]['isEnd'] = true;
//获取建筑的物业编号
$code = [];
$data[$k]['buildNum'] = $this->createNum($v['layout_build_id'],$code);
} else {
$data[$k]['isEnd'] = false;
$data[$k]['buildNum'] = '-';
}
}
$count = Db::name('layout_build')->alias('lb')->where($where)->count();
$res['count'] = $count;
$res['data'] = Common::changeField($data);
return $this->returnJson($res,'success');
}
//根据建筑id拼接物业编号
public function createNum($build_id,$code) {
$data = Db::name('layout_build')->where(['layout_build_id'=>$build_id])->field('layout_build_id,parent_id,code')->find();
array_unshift($code,$data['code']);
if($data['parent_id'] == 0) {
return implode('-',$code);
} else {
return $this->createNum($data['parent_id'],$code);
}
}
//根据建筑id获取同一级的建筑
public function getCurrent() {
$build_id = Request::param('build_id');
$layoutInfo = Db::name('layout_build')->alias('lb')
->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')
->where(['layout_build_id'=>$build_id])
->field('lb.level,lb.code,lb.layout_build_id,lb.layout_id,lb.village_id,ll.code as ll_code')->find();
$where['layout_id'] = $layoutInfo['layout_id'];
$where['village_id'] = $layoutInfo['village_id'];
$where['level'] = $layoutInfo['level'];
$data = Db::name('layout_build')->where($where)->field('layout_build_id,layout_id,code,level')->select()->toArray();
if($layoutInfo){
$res['code_name'] = explode('-',$layoutInfo['ll_code'])[$layoutInfo['level']-1];
} else {
$res['code_name'] ='';
}
$res['data'] = $data;
return $this->returnJson($res);
}
//查看社区的水电物业费等
public function costPrice() {
$where['village_id'] = Request::param('village_id');
$data =DB::name('house_village')->where($where)->field('village_id,property_price,water_price,electric_price,gas_price,park_price')->find();
return $this->returnJson($data,'success');
}
//根据id查看建筑详情
public function queryCostPrice(){
$where['lb.layout_build_id'] = Request::param('layout_build_id');//建筑id
$where['lb.village_id'] = $this->village_id;
$res = Db::name('layout_build')->alias('lb')->leftJoin('layout_list ll','ll.layout_id = lb.layout_id')->where($where)
->field('lb.is_inherit,lb.layout_build_id,lb.layout_build_id,lb.property_price,lb.water_price,lb.electric_price,lb.gas_price,lb.parking_price,lb.code,lb.explain,lb.parent_id,lb.create_time,lb.measure_area,lb.address,lb.sort_id,ll.code as code_name, ll.name')
->find();
$data = Common::changeField($res);
return $this->returnJson($data,'success');
}
//获取水电费等集成当前建筑的id
public function findBuildId($pid){
static $id = [];
$ids = DB::name('layout_build')->where(['parent_id'=>$pid,'is_inherit'=>1])->column('layout_build_id');
if(!empty($ids)){
foreach ($ids as $v) {
$this->findBuildId($v);
$id[] = $v;
}
}
sort($id);
return $id;
}
//获取子元素的所有建筑ID
public function allBuildId($pid) {
static $id = [];
$ids = DB::name('layout_build')->where(['parent_id'=>$pid])->column('layout_build_id');
if(!empty($ids)){
foreach ($ids as $v) {
$this->findBuildId($v);
$id[] = $v;
}
}
sort($id);
return $id;
}
}
\ No newline at end of file
<?php
namespace app\shequ\controller;
use think\facade\Db;
use think\facade\Request;
class Village extends Base
{
protected $middleware = ['app\middleware\CommunityLoginCheck::class','app\middleware\CommunityAfter::class'];
//社区房屋排布编号
public function arrangementList() {
$where['village_id'] = Request::param('village_id');
$data = Db::name('layout_list')->where($where)->select()->toArray();
return $this->returnJson($data,'success');
}
//添加|修改社区排布
public function createArrangement() {
$layout_id = Request::param('layout_id');
$data['village_id'] = $where['village_id'] = Request::param('village_id');
$data['code'] = Request::param('code');
$data['name'] = Request::param('name');
$data['pic'] = Request::param('pic');
$data['level'] = $where['level'] = count(explode('-',$data['code']));
if($layout_id) {
//当前布局下没有房屋,可以修改
$where['layout_id'] = $layout_id;
$is_exit = Db::name('layout_build')->where($where)->find();
if($is_exit) {
return $this->returnJson([],'已有该布局排布,添加失败',400);
}
//检查是否有类似的布局
unset($where['layout_id']);
$is_exit = Db::name('layout_list')->where($where)->find();
if($is_exit){
return $this->returnJson([],'已有该布局排布,添加失败',400);
}
unset($where['level']);
$change = Db::name('layout_list')->where($where)->save($data);
if($change) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'error',400);
}
} else {
//检查是否有类似的布局
$is_exit = Db::name('layout_list')->where($where)->find();
if($is_exit){
return $this->returnJson([],'已有该布局排布,添加失败',400);
}
$add = Db::name('layout_list')->insert($data);
if($add) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'error',400);
}
}
}
//删除社区排布编号
public function deleteArrangement() {
$layout_id =Request::param('layout_id');
$where['layout_id'] = $layout_id;
$is_exit = Db::name('layout_build')->where($where)->find();
if($is_exit){
return $this->returnJson([],'以用该布局创建了建筑,请先删除建筑,在删除',400);
}
$delete = Db::name('layout_list')->where($where)->delete();
if($delete) {
return $this->returnJson([],'success',200);
} else {
return $this->returnJson([],'error');
}
}
<?php
namespace app\shequ\controller;
use app\admin\controller\Common;
use think\facade\Db;
use think\facade\Request;
class Village extends Base
{
protected $middleware = ['app\middleware\CommunityLoginCheck::class','app\middleware\CommunityAfter::class'];
//社区房屋排布编号
public function arrangementList() {
$where['village_id'] = Request::param('village_id');
$data = Db::name('layout_list')->where($where)->select()->toArray();
return $this->returnJson($data,'success');
}
//添加|修改社区排布
public function createArrangement() {
$layout_id = Request::param('layout_id');
$data['village_id'] = $where['village_id'] = Request::param('village_id');
$data['code'] = Request::param('code');
$data['name'] = Request::param('name');
$data['pic'] = Request::param('pic');
$data['level'] = $where['level'] = count(explode('-',$data['code']));
if($layout_id) {
//当前布局下没有房屋,可以修改
$where['layout_id'] = $layout_id;
$is_exit = Db::name('layout_build')->where($where)->find();
if($is_exit) {
return $this->returnJson([],'已有该布局排布,添加失败',400);
}
//检查是否有类似的布局
unset($where['layout_id']);
$is_exit = Db::name('layout_list')->where($where)->find();
if($is_exit){
return $this->returnJson([],'已有该布局排布,添加失败',400);
}
unset($where['level']);
$change = Db::name('layout_list')->where($where)->save($data);
if($change) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'error',400);
}
} else {
//检查是否有类似的布局
$is_exit = Db::name('layout_list')->where($where)->find();
if($is_exit){
return $this->returnJson([],'已有该布局排布,添加失败',400);
}
$add = Db::name('layout_list')->insert($data);
if($add) {
return $this->returnJson([],'success');
} else {
return $this->returnJson([],'error',400);
}
}
}
//删除社区排布编号
public function deleteArrangement() {
$layout_id =Request::param('layout_id');
$where['layout_id'] = $layout_id;
$is_exit = Db::name('layout_build')->where($where)->find();
if($is_exit){
return $this->returnJson([],'以用该布局创建了建筑,请先删除建筑,在删除',400);
}
$delete = Db::name('layout_list')->where($where)->delete();
if($delete) {
return $this->returnJson([],'success',200);
} else {
return $this->returnJson([],'error');
}
}
//查看社区的基本信息
public function villageInfo() {
$where['village_id'] = $this->village_id;
$data =DB::name('house_village')->where($where)
->field('village_id,property_price,water_price,electric_price,gas_price,park_price,village_Logo,village_name,village_address,province_id,province_name,city_id,city_name,area_id,area_name')->find();
return $this->returnJson($data,'success');
}
//修改小区的基本信息
public function changeVillageInfo(){
$property_price = Request::param('property_price');
$water_price = Request::param('water_price');
$electric_price = Request::param('electric_price');
$gas_price = Request::param('gas_price');
$park_price = Request::param('park_price');
if(!isset($park_price) || !isset($gas_price) || !isset($electric_price) || !isset($water_price) || !isset($property_price)){
return $this->returnJson([],'费用必传递!',400);
}
$where['village_id'] = $this->village_id;
//查看小区的水电物业费等
$info = Db::name('house_village')->where($where)->field('property_price,water_price,electric_price,gas_price,park_price,village_logo')->find();
//修改了小区的费用
if($info['property_price'] != $property_price || $info['water_price'] != $water_price || $info['electric_price'] != $electric_price || $info['gas_price'] != $gas_price ||$info['park_price'] != $park_price ) {
$save['property_price'] = $property_price;
$save['water_price'] = $water_price;
$save['electric_price'] = $electric_price;
$save['gas_price'] = $gas_price;
$save['park_price'] = $park_price;
Db::startTrans();
try{
Db::name('layout_build')->where($where)->save($save);
DB::name('house_village')->where($where)->save($save);
Db::commit();
return $this->returnJson();
}catch(\Exception $e){
Db::rollback();
return $this->returnJson([],'修改失败',400);
}
}
$data['village_name'] = Request::param('village_name');
$data['village_address'] = Request::param('village_address');
$data['province_id'] = Request::param('province_id');
$data['province_name'] = Request::param('province_name');
$data['city_id'] = Request::param('city_id');
$data['city_name'] = Request::param('city_name');
$data['area_id'] = Request::param('area_id');
$data['area_name'] = Request::param('area_name');
$data['village_logo'] = Request::param('logo');
$change = DB::name('house_village')->where($where)->save($data);
if(!$change) {
return $this->returnJson([], '修改失败', 400);
}
return $this->returnJson();
}
//banner图添或修改
public function addBanner() {
$banner_id =Request::param('banner_id');
$data['pic'] =Request::param('pic');
$data['url'] =Request::param('url');
$data['sort_id'] =Request::param('sort_id',1);
if($banner_id){
$where['banner_id'] = $banner_id;
$where['village_id'] = $this->village_id;
$operation = Db::name('village_banner')->where($where)->save($data);
} else {
$data['village_id'] = $this->village_id;
$data['create_time'] = time();
$operation = Db::name('village_banner')->insert($data);
}
if($operation) {
return $this->returnJson();
} else {
return $this->returnJson([],'操作失败!',400);
}
}
//删除banner
public function deleteBanner() {
$banner_id =Request::param('banner_id');
$where['banner_id'] = $banner_id;
$where['village_id'] = $this->village_id;
$delete = Db::name('village_banner')->where($where)->delete();
if($delete) {
return $this->returnJson();
} else {
return $this->returnJson([],'删除失败!',400);
}
}
//banner列表
public function bannerList(){
$where['village_id'] = $this->village_id;
$data =Db::name('village_banner')->where($where)->order(['sort_id'=>'desc','create_time'=>'desc'])->select()->toArray();
return $this->returnJson(Common::changeField($data));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment