Commit 89bf5e6b authored by Hsinli's avatar Hsinli

add cd

parent fb7d1822
Pipeline #19636 passed with stage
in 1 minute 6 seconds
'use strict';
const Controller = require('egg').Controller;
class LikeController extends Controller {
/**
* 贷款计算器
*/
async caculator() {
const { ctx } = this;
let inputParams = ctx.request.body;
let rule = {
type: { type: 'string', required: true },
};
ctx.validate(rule, inputParams);
if (inputParams.type === 'commercial') {
let rule = {
commercial: { type: 'object', required: true },
};
ctx.validate(rule, inputParams);
let params = {
total: { type: 'int', required: true },//贷款额
years: { type: 'int', required: true },//年限
lpr: { type: 'int', required: true },//lpr
basic_point: { type: 'int', required: true },//基点
};
ctx.validate(params, inputParams.commercial);
} else if (inputParams.type === 'gjj') {
let rule = {
gjj: { type: 'object', required: true },
};
ctx.validate(rule, inputParams);
let params = {
total: { type: 'int', required: true },//贷款额
years: { type: 'int', required: true },//年限
rate: { type: 'int', required: true },//利率
};
ctx.validate(params, inputParams.gjj);
} else if (inputParams.type === 'combination') {
let rule = {
commercial: { type: 'object', required: true },
gjj: { type: 'object', required: true },
};
ctx.validate(rule, inputParams);
let paramsCommercial = {
total: { type: 'int', required: true },//贷款额
years: { type: 'int', required: true },//年限
lpr: { type: 'int', required: true },//lpr
basic_point: { type: 'int', required: true },//基点
};
ctx.validate(paramsCommercial, inputParams.commercial);
let paramsGjj = {
total: { type: 'int', required: true },//贷款额
years: { type: 'int', required: true },//年限
rate: { type: 'int', required: true },//利率
};
ctx.validate(paramsGjj, inputParams.gjj);
} else {
ctx.failed('error type');
}
let ret = await ctx.service.cd.loanCaculator.getLoanCaculatorResult(inputParams);
ctx.success(ret);
}
}
module.exports = LikeController;
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const CdLotteryPublicise = app.realestateModel.define('cd_lottery_publicise', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
house_name: {
type: STRING,
allowNull: true
},
times: {
type: INTEGER,
allowNull: true
},
select_order: {
type: INTEGER,
allowNull: true
},
notarization_no: {
type: STRING,
allowNull: true
},
register_no: {
type: STRING,
allowNull: true
},
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
}
}, {
timestamps: false,
tableName: 'cd_lottery_publicise',
});
return CdLotteryPublicise;
};
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const CdLotteryUserQuery = app.realestateModel.define('cd_lottery_user_query', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: STRING,
allowNull: true
},
app_user_id: {
type: STRING,
allowNull: true
},
app_id: {
type: STRING,
allowNull: true
},
app_type_id: {
type: STRING,
allowNull: true
},
house_name: {
type: STRING,
allowNull: true
},
notarization_no: {
type: STRING,
allowNull: true
},
phone: {
type: STRING,
allowNull: true
},
publicise_id: {
type: INTEGER,
allowNull: true
},
valid: {
type: INTEGER,
allowNull: true
},
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
deleted_at: {
type: DATE,
get() {
const date = this.getDataValue('deleted_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'cd_lottery_user_query',
});
return CdLotteryUserQuery;
};
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, ENUM, TEXT, DATE } = app.Sequelize;
const CdNews = app.realestateModel.define('cd_news', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING,
allowNull: true
},
images: {
type: STRING,
allowNull: true
},
source: {
type: STRING,
allowNull: true
},
source_time: {
type: DATE,
get() {
const date = this.getDataValue('source_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
content: {
type: TEXT,
allowNull: true
},
tags: {
type: STRING,
allowNull: true
},
author: {
type: STRING,
allowNull: true
},
top: {
type: STRING,
allowNull: true
},
top_start: {
type: DATE,
get() {
const date = this.getDataValue('top_start');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
top_end: {
type: DATE,
get() {
const date = this.getDataValue('top_end');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
initial_num: {
type: INTEGER,
allowNull: true
},
hourly_growth: {
type: INTEGER,
allowNull: true
},
status: {
type: ENUM('pass', 'refuse', 'wait'),
allowNull: true
},
valid: {
type: INTEGER,
allowNull: true
},
operator: {
type: STRING,
allowNull: true
},
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
deleted_at: {
type: DATE,
get() {
const date = this.getDataValue('deleted_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'cd_news',
});
return CdNews;
};
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, ENUM, TEXT, DATE } = app.Sequelize;
const CdNewsTag = app.realestateModel.define('cd_news_tag', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
tag: {
type: STRING,
allowNull: true
},
status: {
type: ENUM('online', 'offline'),
allowNull: true
},
valid: {
type: INTEGER,
allowNull: true
},
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
deleted_at: {
type: DATE,
get() {
const date = this.getDataValue('deleted_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'cd_news_tag',
});
return CdNewsTag;
};
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, ENUM, TEXT, DATE, DECIMAL } = app.Sequelize;
const CdPresellHouse = app.realestateModel.define('cd_presell_house', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: {
type: STRING,
allowNull: true
},
phone: {
type: STRING,
allowNull: true
},
presell_permit: {
type: STRING,
allowNull: true
},
purpose: {
type: STRING,
allowNull: true
},
presell_area: {
type: DECIMAL,
allowNull: true
},
market_time: {
type: DATE,
get() {
const date = this.getDataValue('market_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
status: {
type: ENUM('online', 'offline'),
allowNull: true
},
valid: {
type: INTEGER,
allowNull: true
},
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
deleted_at: {
type: DATE,
get() {
const date = this.getDataValue('deleted_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'cd_presell_house',
});
return CdPresellHouse;
};
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const CdPresellSearchHistory = app.realestateModel.define('cd_presell_search_history', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: STRING,
allowNull: true
},
app_user_id: {
type: STRING,
allowNull: true
},
app_id: {
type: STRING,
allowNull: true
},
app_type_id: {
type: STRING,
allowNull: true
},
key_word: {
type: STRING,
allowNull: true
},
state: {
type: INTEGER,
allowNull: true
},
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
deleted_at: {
type: DATE,
get() {
const date = this.getDataValue('deleted_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'cd_presell_search_history',
});
return CdPresellSearchHistory;
};
'use strict';
module.exports = app => {
const router = app.router.namespace(app.config.projectRootPath + '/cd');
const loginAuth = app.middleware.loginAuth({ type: 'new' });//登录中间件
router.post('/cacilator', 'cd.loanCaculator.caculator');//贷款计算器
};
\ No newline at end of file
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
const _ = require('lodash');
class LoanCaculatorService extends Service {
/**
* 贷款计算器
* @param {object} inputParams
*/
async getLoanCaculatorResult(inputParams) {
const { ctx } = this;
let type = inputParams.type;
if (!['comercial', 'gjj', 'combination'].includes(type)) {
ctx.failed('error type');
}
let ret = {
debx: {},
debj: {}
}
if (type === 'commercial') {
inputParams.commercial.rate = (inputParams.commercial.lpr * 1000 + inputParams.commercial.basic_point * 100) / 1000 / 100;
ret.debx = this.debx(inputParams.commercial);
ret.debj = this.debj(inputParams.commercial);
} else if (type === 'gjj') {
inputParams.gjj.rate = inputParams.gjj.rate / 100;
ret.debx = this.debx(inputParams.gjj);
ret.debj = this.debj(inputParams.gjj);
} else {
inputParams.commercial.rate = (inputParams.commercial.lpr * 1000 + inputParams.commercial.basic_point * 100) / 1000 / 100;
inputParams.gjj.rate = inputParams.gjj.rate / 100;
let commercialDebx = this.debx(inputParams.commercial);
let commercialDebj = this.debj(inputParams.commercial);
let gjjDebx = this.debx(inputParams.gjj);
let gjjDebj = this.debj(inputParams.gjj);
//组合贷款将两部分加起来
ret.debx = {
month_repayment: Number((commercialDebx.month_repayment + gjjDebx.month_repayment).toFixed(2)),
total_loan: Number((commercialDebx.total_loan + gjjDebx.total_loan).toFixed(2)),
total_repayment: Number((commercialDebx.total_repayment + gjjDebx.total_repayment).toFixed(2)),
total_interest: Number((commercialDebx.total_interest + gjjDebx.total_interest).toFixed(2)),
total_years: inputParams.commercial.years,
list: {}
}
ret.debj = {
month_repayment: Number((commercialDebj.month_repayment + gjjDebj.month_repayment).toFixed(2)),
total_loan: Number((commercialDebj.total_loan + gjjDebj.total_loan).toFixed(2)),
total_repayment: Number((commercialDebj.total_repayment + gjjDebj.total_repayment).toFixed(2)),
total_interest: Number((commercialDebj.total_interest + gjjDebj.total_interest).toFixed(2)),
total_years: inputParams.commercial.years,
list: {}
}
for (let i = 1; i <= Number(inputParams.commercial.years * 12); i++) {
ret.debx.list[i] = {
repayment: Number((commercialDebx[i].repayment + gjjDebx[i].repayment).toFixed(2)),
pricipal: Number((commercialDebx[i].pricipal + gjjDebx[i].pricipal).toFixed(2)),
interest: Number((commercialDebx[i].interest + gjjDebx[i].interest).toFixed(2)),
surplus: Number((commercialDebx[i].surplus + gjjDebx[i].surplus).toFixed(2)),
}
ret.debj.list[i] = {
repayment: Number((commercialDebj[i].repayment + gjjDebj[i].repayment).toFixed(2)),
pricipal: Number((commercialDebj[i].pricipal + gjjDebj[i].pricipal).toFixed(2)),
interest: Number((commercialDebj[i].interest + gjjDebj[i].interest).toFixed(2)),
surplus: Number((commercialDebj[i].surplus + gjjDebj[i].surplus).toFixed(2)),
}
}
}
return ret;
}
/**
* 1.等额本息:
* 月还款额=贷款本金*月利率*(1+月利率)^总还款月数/[(1+月利率)^总还款月数-1]。
* 还款总额=还款月数×贷款额×月利率×(1+月利率)^贷款月数/〔(1+月利率)^还款月数- 1〕
* 总利息=贷款本金×贷款月数×月利率×(1+月利率)^贷款月数/〔(1+月利率)^还款月数-1〕-贷款本金
*
* 2.等额本金:
* 月还款额=贷款本金/总还款月数+(贷款本金-累计已还款本金)*月利率
* 还款总额=(还款月数+1)×贷款额×月利率/2+贷款本金
* 总利息=(还款月数+1)×贷款额×月利率/2
*/
/**
* 等额本息
* @param {*} inputParams
*/
debx(inputParams) {
//贷款总额
let totalLoan = inputParams.total * 10000;
//贷款期数(月)
let months = inputParams.years * 12;
//月利率
let monthRate = inputParams.rate / 12;
//月供
let monthRepayment = Number(((totalLoan * monthRate * Math.pow((1 + monthRate), months)) / (Math.pow(1 + monthRate, months) - 1)).toFixed(2));
//还款总额
let totalRepayment = Number(((months * totalLoan * monthRate * Math.pow((1 + monthRate), months)) / (Math.pow(1 + monthRate, months) - 1)).toFixed(2));
//总利息
let totalInterest = Number((totalRepayment - totalLoan).toFixed(2));
/**
* 等额本息还款法的利息计算:
* 等额本息还贷,先算每月还贷本息:BX = a * i(1 + i) ^ N / [(1 + i) ^ N - 1]
* 等额本息还贷第n个月还贷本金:
* B = a * i(1 + i) ^ (n - 1) / [(1 + i) ^ N - 1]
* 等额本息还贷第n个月还贷利息:
* X = BX - B= a * i(1 + i) ^ N / [(1 + i) ^ N - 1] - a * i(1 + i) ^ (n - 1) / [(1 + i) ^ N - 1]
* (注:BX = 等额本息还贷每月所还本金和利息总额,
* B = 等额本息还贷每月所还本金,
* a = 贷款总金额
* i = 贷款月利率,
* N = 还贷总月数,
* n = 第n个月
* X = 等额本息还贷每月所还的利息)
*/
//计算每期的数据
let list = {};
let surplus = totalRepayment;
for (let i = 1; i <= months; i++) {
//最后一个月处理一下
let monthRepaymentTemp = i === months ? surplus : monthRepayment;
surplus = Number((surplus - monthRepaymentTemp).toFixed(2));
//本月还贷本金
let monthPricipal = Number((totalLoan * monthRate * Math.pow(1 + monthRate, i - 1) / (Math.pow(1 + monthRate, months) - 1)).toFixed(2));
//本月还贷利息
let monthInterest = Number((monthRepaymentTemp - monthPricipal).toFixed(2));
list[i] = {
repayment: monthRepaymentTemp,
pricipal: monthPricipal,
interest: monthInterest,
surplus
}
}
return {
month_repayment: monthRepayment,
total_loan: totalLoan,
total_repayment: totalRepayment,
total_interest: totalInterest,
total_years: inputParams.years,
list
};
}
/**
* 等额本金
* @param {*} inputParams
*/
debj(inputParams) {
//贷款总额
let totalLoan = inputParams.total * 10000;
//贷款期数(月)
let months = inputParams.years * 12;
//月利率
let monthRate = inputParams.rate / 12;
//首月月供
let monthRepayment = Number((totalLoan / months + (totalLoan - 0) * monthRate).toFixed(2));
//还款总额
let totalRepayment = Number(((months + 1) * totalLoan * monthRate / 2 + totalLoan).toFixed(2));
//总利息
let totalInterest = totalRepayment - totalLoan;
/**
* 每月还本付息金额=(本金/还款月数)+(本金-累计已还本金)×月利率
* 每月本金=总本金/还款月数
* 每月利息=(本金-累计已还本金)×月利率
* 还款总利息=(还款月数+1)×贷款额×月利率/2
* 还款总额=(还款月数+1)×贷款额×月利率/2+ 贷款额
* 注意:在等额本金法中,人们每月归还的本金额始终不变,利息随剩余本金的减少而减少,因而其每月还款额逐渐减少。
*/
//计算每期的数据
let list = {};
//本月还贷本金
let monthPricipal = Number((totalLoan / months).toFixed(2));
let surplus = totalRepayment;
for (let i = 1; i <= months; i++) {
let monthRepaymentTemp = i === months ? surplus : Number((totalLoan / months + (totalLoan - monthPricipal * (i - 1)) * monthRate).toFixed(2));
surplus = Number((surplus - monthRepaymentTemp).toFixed(2));
//本月还贷利息
let monthInterest = Number((monthRepaymentTemp - monthPricipal).toFixed(2));
list[i] = {
repayment: monthRepaymentTemp,
pricipal: monthPricipal,
interest: monthInterest,
surplus
}
}
return {
month_repayment: monthRepayment,
total_loan: totalLoan,
total_repayment: totalRepayment,
total_interest: totalInterest,
total_years: inputParams.years,
list
};
}
}
module.exports = LoanCaculatorService;
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
const _ = require('lodash');
class LotteryPubliciseService extends Service {
/**
* 根据用户的摇号楼盘和公证编号查询摇号结果
* @param {object} {name,no,phone} 摇号楼盘 编号 用户手机号
*/
async getLotteryPublicise(inputParams) {
const { ctx } = this;
//对用户的每次查询进行记录
let data = {
house_name: inputParams.name,
notarization_no: inputParams.no,
phone: inputParams.phone
}
let add = await ctx.realestateModel.CdLotteryUserQuery.create(data);
//查询楼盘
let filter = {
where: {
house_name: inputParams.name,
notarization_no: inputParams.no,
},
order: [['creates_at', 'desc']]
}
let lotteryPublicise = await ctx.realestateModel.CdLotteryPublicise.findOne(filter);
let ret = {
status: 0,
data: {
times: '',
sort: '',
no: '',
house_name: '',
}
}
if (lotteryPublicise) {
ret.status = 1;
ret.data = {
times: lotteryPublicise.times,
sort: lotteryPublicise.select_order,
no: lotteryPublicise.notarization_no,
house_name: lotteryPublicise.house_name,
}
//如果能查询到则增加查询到的id到用户查询记录表中,方便用户查看历史的时候查询
await ctx.realestateModel.CdLotteryUserQuery.update({ publicise_id: lotteryPublicise.id }, { where: { id: add.id } });
}
return ret;
}
/**
* 根据用户输入的摇号楼盘的名字模糊匹配所有的楼盘
* @param {*} houseName
*/
async getHouseName(houseName) {
const { ctx } = this;
let likeHouseName = '%' + houseName.split('').join('%') + '%';
let filter = {
attributes: ['DISTINCT' `house_name`],
where: {
house_name: { $like: likeHouseName }
}
}
let houseNames = await ctx.realestateModel.CdLotteryPublicise.findAll(filter);
let ret = _.map(houseNames, 'house_name');
return ret;
}
/**
* 获取用户的查询记录
*/
async getUserQueryList() {
const { ctx } = this;
if (!ctx.appUserId || !ctx.userId) {
//如果没有登录就不做处理
return false;
}
let query = 'SELECT aa.id query_id,aa.notarization_no no FROM (select id,notarization_no,house_name,created_at FROM cd_lottery_user_query WHERE user_id = ? AND valid = 1 ORDER BY created_at DESC) aa GROUP BY aa.house_name DESC ORDER BY aa.created_at DESC;';
let queryFilter = {
replacements: [ctx.userId],
type: ctx.actModel.QueryTypes.SELECT,
}
let ret = await ctx.realestateModel.CdLotteryUserQuery.query(query, queryFilter);
return ret;
}
/**
* 根据用户查询记录id查询用户以前的查询公示记录
* @param {integer} id 用户查询记录的id
*/
async getPubliciseByQueryId(id) {
const { ctx } = this;
let ret = {
status: 0,
data: {
times: '',
sort: '',
no: '',
house_name: '',
}
}
let queryFilter = {
attributes: ['house_name', 'notarization_no', 'phone', 'publicise_id'],
where: {
id: id
}
}
let queryInfo = await ctx.realestateModel.CdLotteryUserQuery.findOne(queryFilter);
if (!queryInfo) {
return ret;
}
if (queryInfo.publicise_id === 0) {
//如果用户以前没有查到,此处需不需要重新搜索还是说知己网显示以前的结果
return ret;
}
let publiciseFilter = {
where: {
id: queryInfo.publicise_id
},
}
let lotteryPublicise = await ctx.realestateModel.CdLotteryPublicise.findOne(publiciseFilter);
ret.status = 1;
ret.data = {
times: lotteryPublicise.times,
sort: lotteryPublicise.select_order,
no: lotteryPublicise.notarization_no,
house_name: lotteryPublicise.house_name,
}
return ret;
}
}
module.exports = LotteryPubliciseService;
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
const _ = require('lodash');
class NewsService extends Service {
/**
* 资讯列表
* @param {*} condition
*/
async getList(condition) {
const { ctx } = this;
let page = Number(condition.page) || 1;
let limit = Number(condition.limit) || 20;
let offset = (page - 1) * limit;
let filter = {
page,
limit,
offset,
attributes: ['id', 'title', 'images', 'source', 'source_time'],
where: {
valid: 1,
status: 'pass',
},
order: [['top', 'desc'], ['source_time', 'desc']]
}
let list = await ctx.realestateModel.CdNews.findAndCountAll(filter);
let ret = {
list: list.row,
page: list.count,
page_size: Math.ceil(list.count / limit)
}
return ret;
}
/**
* 根据编号获取资讯信息
* @param {integer} id 资讯id
*/
async getNews(id) {
const { ctx } = this;
let filter = {
attributes: ['id', 'title', 'images', 'source', 'source_time', 'content', 'author', 'initial_num'],
where: {
id: id,
valid: 1,
status: 'pass',
}
}
let ret = await ctx.realestateModel.CdNews.findOne(filter);
return ret;
}
}
module.exports = NewsService;
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
const _ = require('lodash');
class NewsService extends Service {
/**
* 资讯标签列表
* @param {*} condition
*/
async getList() {
const { ctx } = this;
let filter = {
attributes: ['tag'],
where: {
valid: 1,
status: 'online',
},
}
let list = await ctx.realestateModel.CdNewsTag.findAll(filter);
let ret = _.map(list, 'tag')
return ret;
}
}
module.exports = NewsService;
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
const _ = require('lodash');
class PresellHouseService extends Service {
/**
* 预售楼盘列表
* @param {*} condition
*/
async getList(condition) {
const { ctx } = this;
let page = Number(condition.page) || 1;
let limit = Number(condition.limit) || 20;
let offset = (page - 1) * limit;
let filter = {
page,
limit,
offset,
attributes: ['id', 'name', 'market_time', 'purpose'],
where: {
valid: 1,
status: 'online',
},
order: [['market_time', 'desc']]
}
let list = await ctx.realestateModel.CdNews.findAndCountAll(filter);
let ret = {
list: list.row,
page: list.count,
page_size: Math.ceil(list.count / limit)
}
return ret;
}
/**
* 根据编号获取预售楼盘信息
* @param {integer} id 资讯id
*/
async getPresellHouse(id) {
const { ctx } = this;
let filter = {
attributes: ['id', 'name', 'phone', 'presell_permit', 'purpose', 'presell_area', 'market_time'],
where: {
id: id,
valid: 1,
status: 'online',
}
}
let ret = await ctx.realestateModel.CdPresellHouse.findOne(filter);
return ret;
}
}
module.exports = PresellHouseService;
'use strict';
const Service = require('egg').Service;
const _ = require('lodash');
const moment = require('moment');
class PresellSearchHistoryService extends Service {
/**
* 添加搜索历史
* @param {object} inputParams
*/
async addSearchHistory(keyWord) {
const { ctx } = this;
if (!ctx.appUserId || !ctx.userId) {
//如果没有登录就不做处理
return false;
}
let data = {
user_id: ctx.userId,
app_user_id: ctx.appUserId,
app_id: ctx.appId,
app_type_id: ctx.appTypeId,
key_word: keyWord,
state: 1,
};
let ret = await ctx.realestateModel.CdPresellSearchHistory.create(data);
return { id: ret.id };
}
/**
* 获取搜索历史
* 通过输入的关键字搜索楼盘
* 历史记录取用户最近6个月的搜索记录,去重处理,条数不超过50条
* 排序根据搜索时间逆序排列,距离当前时间近的排在前面,重复搜索的关键词取最近一次的搜索时间进行排序
* 如果没有搜索记录的就不显示搜索词
*/
async getList() {
const { ctx } = this;
let ret = {
search_history: {
results: [],
count: 0
}
};
if (!ctx.appUserId || !ctx.userId) {
//如果没有登录就返回
return ret;
}
//用户的搜索记录
let endDate = moment().subtract(6, 'months').format('YYYY-MM-DD HH:mm:ss');
let filter = {
limit: 50,
attributes: ['key_word'],
where: {
state: 1,
user_id: ctx.userId,
created_at: { $gt: endDate }
},
order: [['created_at', 'desc']],
}
let searchHistoryList = await ctx.realestateModel.CdPresellSearchHistory.findAll(filter);
let list = _.uniq(_.map(searchHistoryList, 'key_word'));
ret.search_history.results = list;
ret.search_history.count = list.length;
return ret;
}
/**
* 用户点击清除搜索记录
*/
async cleanSearchHistory() {
const { ctx } = this;
let ret = { status: true };
if (!ctx.appUserId || !ctx.userId) {
//如果没有登录就不做处理
return ret;
}
let filter = {
attributes: ['id'],
where: {
state: 1,
user_id: ctx.userId,
}
}
let searchHistoryList = await ctx.realestateModel.CdPresellSearchHistory.findAll(filter);
if (searchHistoryList.length > 0) {
let params = { state: 0 };
let where = {
user_id: ctx.userId
}
await ctx.realestateModel.CdPresellSearchHistory.update(params, { where: where });
}
return ret;
}
}
module.exports = PresellSearchHistoryService;
'use strict';
const Service = require('egg').Service;
class ArithmeticService extends Service {
/**
* @param num1被加数
* @param num2加数
*/ add(num1, num2) {
let baseNum, baseNum1, baseNum2;
try {
baseNum1 = num1.toString().split(".")[1].length;
} catch (e) {
baseNum1 = 0;
}
try {
baseNum2 = num2.toString().split(".")[1].length;
} catch (e) {
baseNum2 = 0;
}
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
return (num1 * baseNum + num2 * baseNum) / baseNum;
};
/**
* @param num1被减数
* @param num2减数
*/
sub(num1, num2) {
let baseNum, baseNum1, baseNum2;
try {
baseNum1 = num1.toString().split(".")[1].length;
} catch (e) {
baseNum1 = 0;
}
try {
baseNum2 = num2.toString().split(".")[1].length;
} catch (e) {
baseNum2 = 0;
}
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
let n = baseNum1 >= baseNum2 ? baseNum1 : baseNum2;
return Number(((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(n));
};
/**
* @param num1被乘数
* @param num2乘数
*/
mul(num1, num2) {
let baseNum = 0;
try {
baseNum += num1.toString().split(".")[1].length;
} catch (e) { }
try {
baseNum += num2.toString().split(".")[1].length;
} catch (e) { }
return Number(num1.toString().replace(".", "")) * Number(num2.toString().replace(".", "")) / Math.pow(10, baseNum);
};
/**
* @param num1被除数
* @param num2除数
*/
div(num1, num2) {
let baseNum1 = 0, baseNum2 = 0;
let baseNum3, baseNum4;
try {
baseNum1 = num1.toString().split(".")[1].length;
} catch (e) {
baseNum1 = 0;
}
try {
baseNum2 = num2.toString().split(".")[1].length;
} catch (e) {
baseNum2 = 0;
}
baseNum3 = Number(num1.toString().replace(".", ""));
baseNum4 = Number(num2.toString().replace(".", ""));
return this.mul((baseNum3 / baseNum4), Math.pow(10, baseNum2 - baseNum1));
}
}
module.exports = ArithmeticService;
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