You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
342 lines
13 KiB
342 lines
13 KiB
3 years ago
|
const moment = require('moment');
|
||
|
const { QueryTypes } = require('sequelize');
|
||
|
|
||
|
async function reportDailyStatistic (ctx, next) {
|
||
|
const rslt = {
|
||
|
added: 0, //今日新增
|
||
|
checked: 0, //今日已审填报
|
||
|
unChecked: 0, //未审填报
|
||
|
danger_place: 0, //隐患场所总数
|
||
|
history: 0, //历史填报
|
||
|
date: {}
|
||
|
};
|
||
|
try {
|
||
|
const models = ctx.fs.dc.models;
|
||
|
const curDay_ = moment();
|
||
|
const sequelize = ctx.fs.dc.orm;
|
||
|
|
||
|
|
||
|
rslt.added = await models.UserPlaceSecurityRecord.count({
|
||
|
where: {
|
||
|
time: {
|
||
|
$between: [
|
||
|
curDay_.startOf('day').format('YYYY-MM-DD HH:mm:ss'),
|
||
|
curDay_.endOf('day').format('YYYY-MM-DD HH:mm:ss')
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
rslt.unChecked = await models.UserPlaceSecurityRecord.count({
|
||
|
where: {
|
||
|
$or: [
|
||
|
{
|
||
|
audit2ManId: { $eq: null },
|
||
|
rejectManId: { $eq: null },
|
||
|
},
|
||
|
// {
|
||
|
// audit2ManId: { $ne: null },
|
||
|
// rejectManId: { $eq: null },
|
||
|
// },
|
||
|
{
|
||
|
audit1ManId: { $eq: null },
|
||
|
rejectManId: { $eq: null },
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
});
|
||
|
|
||
|
rslt.checked = await models.UserPlaceSecurityRecord.count({
|
||
|
where: {
|
||
|
$or: [
|
||
|
{
|
||
|
audit2ManId: { $ne: null },
|
||
|
audit2ManIdTime: {
|
||
|
$between: [
|
||
|
curDay_.startOf('day').format('YYYY-MM-DD HH:mm:ss'),
|
||
|
curDay_.endOf('day').format('YYYY-MM-DD HH:mm:ss')
|
||
|
]
|
||
|
}
|
||
|
},
|
||
|
{
|
||
|
rejectManId: { $ne: null },
|
||
|
rejectTime: {
|
||
|
$between: [
|
||
|
curDay_.startOf('day').format('YYYY-MM-DD HH:mm:ss'),
|
||
|
curDay_.endOf('day').format('YYYY-MM-DD HH:mm:ss')
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const list = await sequelize.query(`SELECT count(*) AS "count" FROM "user_placeSecurityRecord" AS "userPlaceSecurityRecord"
|
||
|
WHERE ("userPlaceSecurityRecord"."correctiveAction" IS NOT NULL AND "userPlaceSecurityRecord"."punishment" IS NOT NULL) AND "audit2ManId" IS NOT NULL GROUP BY "placeId";`, { type: QueryTypes.SELECT })
|
||
|
rslt.danger_place = list.length;
|
||
|
|
||
|
rslt.history = await models.UserPlaceSecurityRecord.count();
|
||
|
|
||
|
// seven days data
|
||
|
|
||
|
const startDay = moment().startOf('day');
|
||
|
for (let d = 1; d <= 7; d++) {
|
||
|
const START_DAY = moment(startDay).add(-d, 'day');
|
||
|
const date = START_DAY.format('YYYY-MM-DD');
|
||
|
const num = await models.UserPlaceSecurityRecord.count({
|
||
|
where: {
|
||
|
time: {
|
||
|
$between: [
|
||
|
START_DAY.startOf('day').format('YYYY-MM-DD HH:mm:ss'),
|
||
|
START_DAY.endOf('day').format('YYYY-MM-DD HH:mm:ss')
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
rslt.date[date] = num;
|
||
|
}
|
||
|
|
||
|
ctx.status = 200;
|
||
|
ctx.body = rslt;
|
||
|
|
||
|
} catch (error) {
|
||
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
||
|
ctx.status = 400;
|
||
|
ctx.body = {
|
||
|
"message": "获取数据中台数据失败"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
async function reportAreaStatistic (ctx, next) {
|
||
|
let rslt = [], relationRegion = {};
|
||
|
try {
|
||
|
const { startDate, endDate } = ctx.query;
|
||
|
const models = ctx.fs.dc.models;
|
||
|
const sequelize = ctx.fs.dc.orm;
|
||
|
|
||
|
const list = await sequelize.query(`select "regionId", count("regionId") from "user_placeSecurityRecord" WHERE "time" BETWEEN '${moment(startDate).startOf('day').format('YYYY-MM-DD HH:mm:ss')}' AND '${moment(endDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')}' AND "hiddenDangerItem12" IS NOT NULL GROUP BY "regionId" `, { type: QueryTypes.SELECT })
|
||
|
// let regionIds = []
|
||
|
// list.map(item => {
|
||
|
// if (item.regionId && item.regionId != '') {
|
||
|
// regionIds.push(item.regionId);
|
||
|
// }
|
||
|
// });
|
||
|
|
||
|
// const depts = await sequelize.query(`SELECT "id", "name", "type", "dependence" FROM "department" AS "department" WHERE "department"."id" IN (${regionIds.toString()});`, { type: QueryTypes.SELECT });
|
||
|
const deptRelation = await sequelize.query(`SELECT "id", "name", "type", "dependence" FROM "department" AS "department";`, { type: QueryTypes.SELECT });
|
||
|
const quArea = deptRelation.filter(f => f.type == 2);
|
||
|
quArea.map(item => {
|
||
|
relationRegion[item.id] = {};
|
||
|
const xiang = deptRelation.filter(f => f.type == 3 && f.dependence == item.id).map(item => item.id);
|
||
|
|
||
|
const cun = deptRelation.filter(f => f.type == 4 && xiang.some(ss => ss == f.dependence)).map(item => item.id);
|
||
|
relationRegion[item.id]['regionIds'] = [item.id, ...xiang, ...cun];
|
||
|
relationRegion[item.id]['name'] = item.name;
|
||
|
})
|
||
|
Object.keys(relationRegion).map(key => {
|
||
|
const { regionIds, name } = relationRegion[key];
|
||
|
let data = list.filter(item => regionIds.some(id => id == item.regionId))
|
||
|
let obj = {};
|
||
|
obj['name'] = name;
|
||
|
obj['count'] = 0;
|
||
|
obj['regionId'] = key;
|
||
|
data.map(item => {
|
||
|
obj['count'] += Number(item.count)
|
||
|
})
|
||
|
rslt.push(obj)
|
||
|
})
|
||
|
|
||
|
ctx.status = 200;
|
||
|
ctx.body = rslt;
|
||
|
|
||
|
} catch (error) {
|
||
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
||
|
ctx.status = 400;
|
||
|
ctx.body = {
|
||
|
"message": "获取数据中台数据失败"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
async function dangerAreaQuery (ctx, next) {
|
||
|
const { userId } = ctx.fs.api
|
||
|
let rslt = { rows: [], count: 0, ids: [] }, relationRegion = {};
|
||
|
try {
|
||
|
const { startDate, endDate, placeType, regionId, placeName, offset = 0, limit = 20 } = ctx.query;
|
||
|
const models = ctx.fs.dc.models;
|
||
|
const sequelize = ctx.fs.dc.orm;
|
||
|
|
||
|
let options = {
|
||
|
audit2ManId: { $ne: null },
|
||
|
}, places = [], dep4Ids = [];
|
||
|
if (startDate && endDate) {
|
||
|
options.time = {
|
||
|
$between: [
|
||
|
moment(startDate).startOf('day').format('YYYY-MM-DD HH:mm:ss'),
|
||
|
moment(endDate).endOf('day').format('YYYY-MM-DD HH:mm:ss')
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
if (placeName) {
|
||
|
places = await models.Places.findAll({
|
||
|
where: {
|
||
|
name: { $like: `%${placeName}%` }
|
||
|
}
|
||
|
})
|
||
|
options.placeId = {
|
||
|
$in: places.map(item => item.id)
|
||
|
}
|
||
|
} else {
|
||
|
places = await models.Places.findAll()
|
||
|
}
|
||
|
|
||
|
if (regionId && regionId != -1) {
|
||
|
let idList = [];
|
||
|
const curDeptRelation = await sequelize.query(`SELECT "id", "name", "type", "dependence" FROM "department" WHERE "id" in (${regionId});`, { type: QueryTypes.SELECT });
|
||
|
|
||
|
const type = curDeptRelation[0].type
|
||
|
if (type != 1) {
|
||
|
const deptRelation = await sequelize.query(`SELECT "id", "name", "type", "dependence" FROM "department" AS "department";`, { type: QueryTypes.SELECT });
|
||
|
const quArea = deptRelation.filter(f => f.type == 2);
|
||
|
quArea.map(item => {
|
||
|
relationRegion[item.id] = {};
|
||
|
deptRelation.filter(f => f.type == 3 && f.dependence == item.id).map(x => {
|
||
|
relationRegion[item.id][x.id] = deptRelation.filter(f => f.type == 4 && x.id == f.dependence).map(cun => cun.id);
|
||
|
});
|
||
|
})
|
||
|
if (type == 2) {
|
||
|
const quList = [regionId];
|
||
|
const xiangList = Object.keys(relationRegion[regionId])
|
||
|
let cunList = xiangList.map(key => {
|
||
|
return relationRegion[regionId][key]
|
||
|
})
|
||
|
idList = quList.concat(xiangList).concat(cunList.flat(Infinity))
|
||
|
|
||
|
options.regionId = { $in: idList };
|
||
|
}
|
||
|
if (type == 3) {
|
||
|
Object.keys(relationRegion).map(quKey => {
|
||
|
Object.keys(relationRegion[quKey]).map(xiangKey => {
|
||
|
if (xiangKey == regionId) {
|
||
|
const xiangList = [xiangKey];
|
||
|
const cunList = relationRegion[quKey][xiangKey]
|
||
|
idList = xiangList.concat(cunList);
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
dep4Ids = idList
|
||
|
}
|
||
|
if (type == 4) {
|
||
|
const curUser = await models.User.findOne({ where: { id: userId } })
|
||
|
const corUserDepId = curUser.departmentId
|
||
|
const corUseUserDepRes = await models.Department.findOne({ where: { id: corUserDepId } })
|
||
|
if(corUseUserDepRes.type < 4){
|
||
|
dep4Ids = [regionId]
|
||
|
} else {
|
||
|
options.userId = userId
|
||
|
}
|
||
|
// idList = [regionId]
|
||
|
// options.userId = userId
|
||
|
}
|
||
|
// options.departmentId = { $in: idList };
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (placeType != null && placeType != -1) {
|
||
|
|
||
|
if (placeType == 0) {
|
||
|
options = Object.assign({}, options, {
|
||
|
$or: [
|
||
|
{
|
||
|
correctiveAction: { $ne: null },
|
||
|
},
|
||
|
{
|
||
|
punishment: { $ne: null },
|
||
|
}
|
||
|
]
|
||
|
})
|
||
|
}
|
||
|
|
||
|
if (placeType == 1)
|
||
|
options = Object.assign({}, options, {
|
||
|
$or: [
|
||
|
{
|
||
|
correctiveAction: { $eq: null },
|
||
|
},
|
||
|
{
|
||
|
punishment: { $eq: null },
|
||
|
}
|
||
|
],
|
||
|
hiddenDangerItem12: {
|
||
|
$ne: null
|
||
|
}
|
||
|
})
|
||
|
|
||
|
if (placeType == 2)
|
||
|
options.hiddenDangerItem12 = {
|
||
|
$eq: null
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let findOption = {
|
||
|
where: options,
|
||
|
offset: offset,
|
||
|
limit: limit,
|
||
|
order: [['time', 'DESC']],
|
||
|
}
|
||
|
|
||
|
if (dep4Ids.length) {
|
||
|
findOption.include = [{
|
||
|
required: true,
|
||
|
model: models.User,
|
||
|
as: 'user',
|
||
|
where: {
|
||
|
departmentId: { $in: dep4Ids }
|
||
|
}
|
||
|
}]
|
||
|
}
|
||
|
|
||
|
const list = await models.UserPlaceSecurityRecord.findAll(findOption)
|
||
|
|
||
|
for (let item of list) {
|
||
|
const { name } = places.filter(p => p.id == item.placeId)[0] || {};
|
||
|
const checkAreaName = await sequelize.query(`SELECT "dpt"."name" FROM "department" as "dpt" WHERE "dpt"."id" in (SELECT "department_id" FROM "user" WHERE "id" = ${item.userId} );`, { type: QueryTypes.SELECT })
|
||
|
const checkUser = await sequelize.query(`SELECT "name", "phone" FROM "user" WHERE "id" = ${item.userId}`, { type: QueryTypes.SELECT })
|
||
|
rslt.rows.push(Object.assign({}, item.dataValues, { placeName: name, checkAreaName: (checkAreaName[0] || {}).name || '', checkUserName: (checkUser[0] || {}).name || '', checkUserPhone: (checkUser[0] || {}).phone }))
|
||
|
}
|
||
|
|
||
|
delete findOption.offset
|
||
|
delete findOption.limit
|
||
|
delete findOption.order
|
||
|
findOption.attributes = ['id']
|
||
|
const dataAll = await models.UserPlaceSecurityRecord.findAll(
|
||
|
findOption
|
||
|
// {
|
||
|
// attributes: ['id'],
|
||
|
// where: options,
|
||
|
// }
|
||
|
);
|
||
|
rslt.count = dataAll.length;
|
||
|
rslt.ids = dataAll.map(item => item.dataValues.id);
|
||
|
|
||
|
ctx.status = 200;
|
||
|
ctx.body = rslt;
|
||
|
|
||
|
} catch (error) {
|
||
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
||
|
ctx.status = 400;
|
||
|
ctx.body = {
|
||
|
"message": "获取数据中台数据失败"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
reportDailyStatistic,
|
||
|
reportAreaStatistic,
|
||
|
dangerAreaQuery,
|
||
|
}
|