table)->where($_where)->count(); $page = $this->request->param('page', 1); $pagesize = $this->request->param('pagesize', 10); $pages = ceil($count / $pagesize); //总页数 $_result = Db::name($this->table)->where($_where)->page($page, $pagesize)->order('id', "DESC")->select(); $_data['list'] = $_result; $_data['pages'] = $pages; self::formatData($_data['list']); return send_http_status(200, $_data); } /**房源到期列表 * @param int $day 是否只返回过期房源数量,false只返回 ,默认true * @param bool $is_day * @return \type * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function expire($day = 3, $is_day = true) { if ($day) { //计算指定天数后的时间戳 $day_time = strtotime('+' . $day . ' day'); $_where[] = ['c.create_time', '<', $day_time]; //结束时间小于当前时间+3天 $_where[] = ['c.create_time', '>', time()]; //且结束时间大于当前时间 $_where[] = ['c.create_time', '<>', '']; } $_where[] = ['c.user_id', '=', USER_ID]; $db = Db::name($this->table); $db->alias('h')->join(['cloud_house_crontab' => 'c'], 'h.id = c.house_id'); $db->where($_where)->where('h.is_del', 0); $db->field('h.*,c.create_time AS expire_time'); $_result = $db->order('h.id', "DESC")->select(); if (!$is_day) { return count($_result); } if (empty(count($_result))) { return send_http_status(200, []); } self::formatRoomData($_result); $_data['list'] = $_result; $_data['pages'] = 1; self::formatData($_data['list']); return send_http_status(200, $_data); } /** * 获取子房间列表 * @param int $day 即将到期参数 不传默认 * @param int $is_is_day 是否只返回过期房源数量,false只返回 ,默认true * @return \type * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function room($day = 0, $is_day = true) { if ($day && is_numeric($day)) { //计算指定天数后的时间戳 $day_time = strtotime('+' . $day . ' day'); $_where[] = ['r.end_time', '<', $day_time]; //结束时间小于当前时间+30天 // $_where[] = ['end_time', '>', time()]; //且结束时间大于当前时间 $_where[] = ['r.end_time', '<>', '']; } $_where[] = ['r.user_id', '=', USER_ID]; $_where[] = ['r.is_del', '=', 0]; $count = Db::name($this->table_room)->alias('r')->where($_where)->count(); if (!$is_day) { return $count; } $page = $this->request->param('page', 1); $pagesize = $this->request->param('pagesize', 10); $pages = ceil($count / $pagesize); //总页数 $_result = Db::name($this->table_room)->alias('r')->leftJoin('cloud_house h', 'r.house_id = h.id')->where($_where)->page($page, $pagesize)->order('r.id', "DESC")->field('r.*,h.name,h.photo,h.province,h.city,h.area,h.door_number')->select(); self::formatRoomData($_result); $_data['list'] = $_result; $_data['pages'] = $pages; self::formatData($_data['list']); return send_http_status(200, $_data); } /**房间到期列表 * @param int $day 是否只返回过期房源数量,false只返回 ,默认true * @param bool $is_day * @return \type * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function expire_room($day = 3, $is_day = true) { if ($day) { //计算指定天数后的时间戳 $day_time = strtotime('+' . $day . ' day'); $_where[] = ['c.end_time', '<', $day_time]; //结束时间小于当前时间+3天 // $_where[] = ['c.end_time', '>', time()]; //且结束时间大于当前时间 $_where[] = ['c.end_time', '<>', '']; $_where[] = ['c.status', '=', 0]; $_where[] = ['c.is_del', '=', 0]; } $_where[] = ['c.user_id', '=', USER_ID]; // $room_id = Db::name($this->table_room_crontab)->where($_where)->field('room_id')->select(); $db = Db::name($this->table_room); $db->alias('r')->leftJoin(['cloud_house' => 'h'], 'r.house_id = h.id')->join(['cloud_order_room' => 'c'], 'r.id = c.room_id'); $db->where($_where)->where('r.is_del', 0); $db->field('r.*,h.name,h.photo,h.province,h.city,h.area,h.door_number,c.end_time AS expire_time,c.start_time AS order_time'); $_result = $db->order('r.id', "DESC")->select(); if (!$is_day) { return count($_result); } self::formatRoomData($_result); $_data['list'] = $_result; $_data['pages'] = 1; self::formatData($_data['list']); return send_http_status(200, $_data); } /** * 房源详情 * @param string $id 房源id * @return \type|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function view($id = '') { if (!$id) { return; } //获取房源详情 $_data['house'] = Db::name($this->table)->find($id); if ($_data['house']) { //查询户主信息 $_data['holder'] = Db::name($this->table_holder)->find($_data['house']['holder_id']); //查询子房源列表 $_data['room_list'] = Db::name($this->table_room)->where('is_del', 0)->where('house_id', $id)->order('id', "DESC")->select(); //统计房间支付数据 $_order = new Order(); $_data['house']['orderAmount'] = $_order->orderAmount('', $id); self::formatRoomData($_data['room_list']); } self::formatViewData($_data['house']); return send_http_status(200, $_data); } /** * 子房间详情 */ public function viewRoom($id = '') { if (!$id) { return; } //获取房源详情 $_data['room'] = Db::name($this->table_room)->find($id); if (!empty($_data['room'])) { //查询租户列表 $_where['room_id'] = $_data['room']['id']; $_where['is_del'] = 0; $_data['tenant_list'] = Db::name($this->table_tenant)->where($_where)->order('create_time DESC')->select(); //统计房间支付数据 $_order = new Order(); $_data['room']['orderAmount'] = $_order->orderAmount($_data['room']['id']); self::formatData($_data['tenant_list']); } self::formatViewData($_data['room']); return send_http_status(200, $_data); } /** * 租户详情 */ public function viewTenant($id = '') { if (!$id) { return; } //获取租户详情 $_data['tenant'] = Db::name($this->table_tenant)->find($id); if (empty($_data['tenant'])) { return; } $_data['tenant']['sex_id'] = $_data['tenant']['sex']; $_data['tenant']['sex'] = config('sex')[$_data['tenant']['sex']]; if (!empty($_data['tenant'])) { //查询交租记录 $_where['room_id'] = $_data['tenant']['id']; $_data['tenant_list'] = Db::name($this->table_tenant)->where($_where)->select(); } return send_http_status(200, $_data); } /** * 添加一手房源数据及一手房东信息 */ public function add() { $_data = $this->request->post(); $_data['create_time'] = $_data['update_time'] = time(); $_data['start_time'] = !empty($_data['start_time']) ? strtotime($_data['start_time']) : ''; $_data['end_time'] = !empty($_data['end_time']) ? strtotime($_data['end_time']) : ''; if ($_data['start_time'] >= $_data['end_time']) { return send_http_status(51000); } //会员等级权限判断 $count = Db::name($this->table)->where('user_id', USER_ID)->count(); self::authAddHouse($count); $_data['user_id'] = USER_ID; $_data = array_filter($_data); //添加一房东信息 $holder_id = Db::name($this->table_holder)->strict(false)->insertGetId($_data); if ($holder_id) { //添加房源数据 $_data['holder_id'] = $holder_id; $id = Db::name($this->table)->strict(false)->insertGetId($_data); if ($id) { //定时任务短信提醒房租到期数据更新 $_data['id'] = $id; //添加成功后的房源id $this->crontab($_data); return send_http_status(200); } } return send_http_status(413); } /** * 修改一手房源数据及一手房东信息 */ public function edit() { $_data = $this->request->post(); $_data['update_time'] = time(); $_data['user_id'] = USER_ID; $_data['start_time'] = !empty($_data['start_time']) ? strtotime($_data['start_time']) : ''; $_data['end_time'] = !empty($_data['end_time']) ? strtotime($_data['end_time']) : ''; if ($_data['start_time'] >= $_data['end_time']) { return send_http_status(51000); } //修改房源数据 $affected = Db::name($this->table)->strict(false)->update($_data); $_data = array_filter($_data); if ($affected) { //定时任务短信提醒房租到期数据更新 $this->crontab($_data); //修改一房东信息 unset($_data['id']); Db::name($this->table_holder)->where('id', $_data['holder_id'])->strict(false)->update($_data); } if ($affected) { return send_http_status(200); } return send_http_status(413); } /** * 添加子房间 */ public function addRoom() { //会员等级权限判断 $count = Db::name($this->table_room)->where('user_id', USER_ID)->count(); self::authAddHouse($count, 'count_room'); $_data = $this->request->post(); $_data['create_time'] = $_data['update_time'] = time(); $_data['user_id'] = USER_ID; $_data = array_filter($_data); $id = Db::name($this->table_room)->strict(false)->insertGetId($_data); if (!$id) { return send_http_status(51002); } return send_http_status(200); } /** * 修改子房间 */ public function editRoom() { self::authAddHouse(0, 'count_room'); $_data = $this->request->post(); //是否上架到首页共享房源,即清空时间节点 if ($_data['is_up'] == 'true') { $_data['start_time'] = $_data['end_time'] = time() - 10000; } else { $_data['start_time'] = !empty($_data['start_time']) ? strtotime($_data['start_time']) : ''; $_data['end_time'] = !empty($_data['end_time']) ? strtotime($_data['end_time']) : ''; if ($_data['start_time'] > $_data['end_time']) { return send_http_status(51000); } } $_data['update_time'] = time(); $_data['user_id'] = USER_ID; unset($_data['house_id']); $refected = Db::name($this->table_room)->strict(false)->update($_data); if (!$refected) { return send_http_status(51002); } //根据用户指定收费方式 切割时间 $_data['id'] = $_data['id']; //添加成功后的房间id $this->crontab_room($_data, $_data['rent_type']); return send_http_status(200); } /** * 添加租户 */ public function addTenant() { $_data = $this->request->post(); $where_tenant['room_id'] = $_data['room_id']; $where_tenant['card_id'] = $_data['card_id']; $_tenant = Db::name($this->table_tenant)->where($where_tenant)->find(); if ($_tenant) { return send_http_status(51001); } $_data['start_time'] = $house_data['start_time'] = !empty($_data['start_time']) ? strtotime($_data['start_time']) : ''; $_data['end_time'] = $house_data['end_time'] = !empty($_data['end_time']) ? strtotime($_data['end_time']) : ''; if ($_data['start_time'] > $_data['end_time']) { return send_http_status(51000); } $_data['create_time'] = $_data['update_time'] = time(); $_data['user_id'] = USER_ID; $_data = array_filter($_data); $refected = Db::name($this->table_tenant)->strict(false)->insert($_data); if ($refected && $_data['start_time'] && $_data['end_time']) { //更新房间开始时间及结束时间 $where['id'] = $_data['room_id']; Db::name($this->table_room)->where($where)->strict(false)->update($house_data); //根据用户指定收费方式 切割时间 $_data['id'] = $_data['room_id']; //添加成功后的房间id $this->crontab_room($_data, $_data['rent_type']); } return send_http_status(200); } /** * 添加租户 */ public function editTenant() { $_data = $this->request->post(); $_data['update_time'] = time(); $_data['user_id'] = USER_ID; $_data = array_filter($_data); $refected = Db::name($this->table_tenant)->strict(false)->update($_data); if ($refected) { return send_http_status(200); } return send_http_status(413); } /** * 删除房源 * @param string $id */ public function delHouse($id = '') { if (!$id || !is_numeric($id)) { return send_http_status(51003); } $affected = Db::name($this->table)->where('id', $id)->where('user_id', USER_ID)->update(['is_del' => 1]); if ($affected) { //删除房间数据 $this->delRoom('', $id); return send_http_status(200); } return send_http_status(413); } /** * 删除房间 * @param string $id 房间id * @param string $house_id 一手房源id * @return \type * @throws \think\Exception * @throws \think\exception\PDOException */ public function delRoom($id = '', $house_id = '') { if ((!$id || !is_numeric($id)) && !$house_id) { return send_http_status(51003); } //如果一手房源id存在,删除所有属于一手房源的子房间 $_where = !empty($house_id) ? 'house_id=' . $house_id : 'id=' . $id; //(如果存在house_id说明执行了删除一手房源,即room_id为多个,所以这里需要根据house_id查询所有room_id) if ($house_id) { $id = Db::name($this->table_room)->where('house_id', $house_id)->field('id')->select(); $id = !empty($id) ? implode(',', array_column($id, 'id')) : ''; } $affected = Db::name($this->table_room)->where($_where)->where('user_id', USER_ID)->update(['is_del' => 1]); if ($affected) { //删除租客 $this->delTenant('', $id); return send_http_status(200); } return send_http_status(413); } /** * 删除租客信息 * @param string $id 租客id * @param string $room_id 所属房间id * @return \type * @throws \think\Exception * @throws \think\exception\PDOException */ public function delTenant($id = '', $room_id = '') { if ((!$id || !is_numeric($id)) && !$room_id) { return send_http_status(51003); } //如果存在房间id,删除所有属于房间的租房用户 if (!empty($room_id)) { $_where[] = ['room_id', 'in', $room_id]; //tp5.1 需要这样写 } else { $_where = 'id=' . $id; } $affected = Db::name($this->table_tenant)->where($_where)->where('user_id', USER_ID)->update(['is_del' => 1]); if ($affected) { //删除租客 Db::name($this->table_order_room)->where('tenant_id', $id)->update(['is_del' => 1]); //租客交费记录删除(没有总数据统计,可不删除) return send_http_status(200); } return send_http_status(413); } /** * 房间数据格式化 * @param $data */ static private function formatRoomData(&$data) { if (!$data) { return; } array_walk($data, function (&$v, &$k) { $v['days'] = time_days($v['end_time']); $v['expire_days'] = isset($v['expire_time']) ? time_days($v['expire_time']) : ''; !empty($v['expire_time']) ? $v['expire_time'] = date('Y-m-d', $v['expire_time']) : $v['expire_time'] = ''; !empty($v['order_time']) ? $v['order_time'] = date('Y-m-d', $v['order_time']) : $v['order_time'] = ''; //列表封面 if (isset($v['photo'])) { $_arr = explode(',', $v['photo']); $v['thumb'] = !empty($_arr[0]) ? $_arr[0] : config('default_list_thumb'); } }); } /** * 添加定时任务时间节点(定时任务短信提醒房租到期数据更新) * @param $_data 房源数据 * @param int $month 指定分割月数 * @throws \think\Exception * @throws \think\exception\PDOException */ private function crontab($_data, $month = 3) { $start_time = $_data['start_time']; $end_time = $_data['end_time']; //计算指定天数后的时间戳 $_arr = []; while ($start_time < $end_time) { $arr_temp['house_id'] = $_data['id']; $arr_temp['user_id'] = USER_ID; $arr_temp['house_name'] = $_data['name']; $arr_temp['phone'] = MEMBER['phone']; $arr_temp['rent'] = $_data['rent']; $arr_temp['address'] = $_data['province'] . $_data['city'] . $_data['area']; $arr_temp['door_number'] = $_data['door_number']; $arr_temp['create_time'] = $start_time = strtotime('+' . $month . ' month', $start_time); $_arr[] = $arr_temp; } //删除最后一个元素,置换为end_time(防止开始时间与结束时间不一致) $last_arr = array_pop($_arr); $last_arr['create_time'] = $end_time; $_arr[] = $last_arr; //新增前先清空 $house_crontab = Db::name($this->table_house_crontab); $house_crontab->where('house_id', $_data['id'])->where('user_id', USER_ID)->delete(); $house_crontab->insertAll($_arr); } static private function formatData(&$data) { if (!$data) { return; } array_walk($data, function (&$v, &$k) { !empty($v['create_time']) ? $v['create_time'] = date('Y-m-d', $v['create_time']) : $v['create_time'] = ''; !empty($v['update_time']) ? $v['update_time'] = date('Y-m-d', $v['update_time']) : $v['update_time'] = ''; !empty($v['end_time']) ? $v['end_time'] = date('Y-m-d', $v['end_time']) : $v['end_time'] = ''; !empty($v['start_time']) ? $v['start_time'] = date('Y-m-d', $v['start_time']) : $v['start_time'] = ''; isset($v['sex']) ? $v['sex'] = config('sex')[$v['sex']] : ''; //列表封面 if (isset($v['photo'])) { $_arr = explode(',', $v['photo']); $v['thumb'] = !empty($_arr[0]) ? $_arr[0] : config('default_list_thumb'); } }); } static private function formatViewData(&$data) { if (!empty($data['housing_allocation'])) { //获取房间配置 $housingAllocation = config('housingAllocation'); $data['housing_allocation_str'] = ''; //转换房间配置信息 $allocation_arr = explode(',', $data['housing_allocation']); foreach ($allocation_arr as $k => $v) { $data['housing_allocation_str'] .= $housingAllocation[$k]['name'] . ','; } $data['housing_allocation_str'] = trim($data['housing_allocation_str'], ','); $data['housing_allocation'] = explode(',', $data['housing_allocation']); } if (!empty($data['photo'])) { $data['imgList'] = explode(',', $data['photo']); } else { $data['imgList'] = []; } !empty($data['create_time']) ? $data['create_time'] = date('Y-m-d', $data['create_time']) : $data['create_time'] = ''; !empty($data['update_time']) ? $data['update_time'] = date('Y-m-d', $data['update_time']) : $data['update_time'] = ''; !empty($data['end_time']) ? $data['end_time'] = date('Y-m-d', $data['end_time']) : $data['end_time'] = ''; !empty($data['start_time']) ? $data['start_time'] = date('Y-m-d', $data['start_time']) : $data['start_time'] = ''; } }