'use strict'; const request = require("superagent"); const cheerio = require('cheerio'); function addAffordableHousing(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; let errMsg = { message: '新增住房信息失败' } try { const { houseValue, personValue } = ctx.request.body; const arr = personValue.map(p => ({ ...houseValue, ...p })); await models.AffordableHousing.bulkCreate(arr); ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } function getPersonAge(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; let errMsg = { message: '获取租户年龄分布失败' } try { let age17 = 0, age18 = 0, age29 = 0, age41 = 0, age66 = 0, age85 = 0 // 17岁以下 18-28岁 29-40岁 41-65岁 66-85岁 85岁以上 age17 = await models.AffordableHousing.count({ where: { houseStatus: false, personAge: { $lte: 17 } } }); age18 = await models.AffordableHousing.count({ where: { houseStatus: false, personAge: { $between: [18, 28] } } }); age29 = await models.AffordableHousing.count({ where: { houseStatus: false, personAge: { $between: [29, 40] } } }); age41 = await models.AffordableHousing.count({ where: { houseStatus: false, personAge: { $between: [41, 65] } } }); age66 = await models.AffordableHousing.count({ where: { houseStatus: false, personAge: { $between: [66, 85] } } }); age85 = await models.AffordableHousing.count({ where: { houseStatus: false, personAge: { $gte: 86 } } }); ctx.status = 200; ctx.body = { age17, age18, age29, age41, age66, age85 }; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } function getHomePerson(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; let errMsg = { message: '获取租户家庭人口统计失败' } try { const home = await models.AffordableHousing.count({ attributes: [ 'house_id', // 分组的字段 ], group: ['house_id'], // 分组的字段 where: { houseStatus: false } }); ctx.status = 200; ctx.body = { home }; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } function getHomeInfo(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; let errMsg = { message: '获取社区房屋统计信息' } try { let communitys = await models.AffordableHousing.findAll({ attributes: [ 'houseCommunity', // 分组的字段 ], group: ['houseCommunity'], // 分组的字段 where: { houseStatus: false, } }); let communityInUse = [] for (let i = 0; i < communitys.length; i++) { let arr = await models.AffordableHousing.findAll({ attributes: [ 'house_id', // 分组的字段 ], group: ['house_id'], // 分组的字段 where: { houseStatus: false, houseCommunity: communitys[i].houseCommunity } }) communityInUse.push({ "houseCommunity": communitys[i].houseCommunity, "count": arr.length }) } //安社区分类-社区空置套数 let communityUnUse = await models.AffordableHousing.count({ attributes: [ 'houseCommunity', // 分组的字段 ], group: ['houseCommunity'], // 分组的字段 where: { houseStatus: true } }); //租赁套数 let inuse = 0; let useHomes = await models.AffordableHousing.count({ attributes: [ 'house_id', // 分组的字段 ], group: ['house_id'], // 分组的字段 where: { houseStatus: false } }); inuse = useHomes.length; //空置套数 const unuse = await models.AffordableHousing.count({ where: { houseStatus: true } }); let communityPersons = await models.AffordableHousing.count({ attributes: [ 'person_community', // 分组的字段 ], group: ['person_community'], // 分组的字段 where: { houseStatus: false } }); ctx.status = 200; ctx.body = { inuse: inuse, //租赁中套数 unuse: unuse, //空置套数 communityInUse, communityUnUse, communityPersons }; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } function getFgjvNotice(opts) { return async function (ctx, next) { let errMsg = { message: '获取房管局通知失败' } try { let rslt = []; const res = await request.get('https://www.nc.gov.cn/ncszf/fgjv/2021_nav_list.shtml'); const $ = cheerio.load(res.text); $('div.pageList-line ul').children('li').each(function (i, elem) { if (i >= 5) return false; let obj = {}; const a = $(this).find('a'); obj.title = a.text(); obj.time = $(this).find('span').text(); obj.link = 'https://www.nc.gov.cn' + a.attr('href'); rslt.push(obj); }); const promistArr = rslt.map(obj => request.get(obj.link)); const detailRes = await Promise.all(promistArr); for (let i = 0; i < rslt.length; i++) { const detail$ = cheerio.load(detailRes[i].text); const temp = detail$('ucapcontent').text().trim().substring(0, 50).replace(/\n/g, ""); rslt[i].detail = temp; } ctx.status = 200; ctx.body = rslt; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } module.exports = { addAffordableHousing, getPersonAge, getHomePerson, getHomeInfo, getFgjvNotice }