Commit d6fdb750 authored by 方斌's avatar 方斌
parents 40899e05 89786eda
'use strict';
module.exports = app => {
const { STRING, INTEGER } = app.Sequelize;
const HouseBizcircle = app.blockModel.define('house_bizcircle', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: STRING,
spell: STRING,
district_id: INTEGER,
}, {
timestamps: false,
tableName: 'house_bizcircle',
freezeTableName: true,
});
HouseBizcircle.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await HouseBizcircle.findOne({
attributes: attributes,
where : where,
});
}
HouseBizcircle.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await HouseBizcircle.findAll({
attributes: attributes,
where : where,
});
}
HouseBizcircle.list = async (data = {}) => {
const limit = data.limit ? data.limit : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await HouseBizcircle.findAndCountAll(condition);
return { page, count, rows };
}
HouseBizcircle.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await HouseBizcircle.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch(error) {
ctx.status = 500;
throw (error);
}
}
HouseBizcircle.edit = async (data) => {
const where = data.where;
const params = data.params;
HouseBizcircle.update(params, {
where: where
}).catch( e => res.json({status: 500, error: e}));
}
return HouseBizcircle;
};
'use strict';
module.exports = app => {
const { STRING, INTEGER } = app.Sequelize;
const HouseDistrict = app.blockModel.define('house_district', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: STRING,
spell: STRING,
city_id: INTEGER,
}, {
timestamps: false,
tableName: 'house_district',
freezeTableName: true,
});
HouseDistrict.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await HouseDistrict.findOne({
attributes: attributes,
where : where,
});
}
HouseDistrict.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await HouseDistrict.findAll({
attributes: attributes,
where : where,
});
}
HouseDistrict.list = async (data = {}) => {
const limit = data.limit ? data.limit : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await HouseDistrict.findAndCountAll(condition);
return { page, count, rows };
}
HouseDistrict.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await HouseDistrict.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch(error) {
ctx.status = 500;
throw (error);
}
}
HouseDistrict.edit = async (data) => {
const where = data.where;
const params = data.params;
HouseDistrict.update(params, {
where: where
}).catch( e => res.json({status: 500, error: e}));
}
return HouseDistrict;
};
......@@ -4,7 +4,7 @@ module.exports = app => {
const router = app.router.namespace(app.config.projectRootPath + '/house');
// const loginAuth = app.middleware.loginAuth({type: 'new'});//登录中间件
router.get('/options', 'house.options.getOptions');//首页信息
router.get('/rental_house', 'house.rentalHouse.getRentHouses');//首页信息
router.get('/options/:city_code', 'house.options.getOptions');//筛选项信息
router.get('/rental_house', 'house.rentalHouse.getRentHouses');//租房列表
};
......@@ -29,15 +29,6 @@ class OptionService extends Service {
async getOptions(city_code) {
const { ctx } = this;
const city = ctx.blockModel.City.one({ where: { code: city_code } });
if (!city || !city.name) {
ctx.failed('city code error');
}
const district = ctx.blockModel.District.all({ where: { city_id: city.code } });
if(!district || district){
ctx.failed('district error');
}
const bizcircle = ctx.blockModel.Bizcircle.all({where: {}});
const developers_results = await ctx.service.houseCommon.developer.all({});
const developers = developers_results.results;
const brands = [];
......@@ -51,8 +42,48 @@ class OptionService extends Service {
}
const house_types = HOUSE_TYPE;
const prices = PRICE_RANGE;
const areas = await this.getAreaOptions(city_code);
return { brands, prices, house_types, areas };
}
async getAreaOptions(city_code) {
const { ctx } = this;
const city = await ctx.blockModel.City.one({ where: { code: city_code } });
if (!city || !city.name) {
ctx.failed('city code error');
}
const districts = await ctx.blockModel.HouseDistrict.all({ where: { city_id: city.code } });
if (!districts || districts.length === 0) {
ctx.failed('district error');
}
const ret = []
for (let j in districts) {
const district = districts[j];
const _children = [];
const bizcircles = await ctx.blockModel.HouseBizcircle.all({ where: { district_id: district.id } });
if (!bizcircles || bizcircles.length === 0) {
continue;
}
for (let i in bizcircles) {
const bizcircle = bizcircles[i];
_children.push({
id: bizcircle.id,
name: bizcircle.name,
district_id: bizcircle.district_id,
});
}
ret.push({
id: district.id,
name: district.name,
city_id: district.city_id,
_children: _children,
})
}
return { brands, prices, house_types };
return ret;
}
}
......
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