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.
 
 
 
 

208 lines
8.6 KiB

const fs = require('fs');
const moment = require('moment')
const path = require('path')
const OSS = require('ali-oss');
const uuid = require('uuid');
const TEST = false
// const TEST = true
module.exports = function (app, opts) {
const hideDangerStatistic = app.fs.scheduleInit(
// 按月、季度、年统计隐患整改
{
interval: '0 32 4 1 */1 *',
immediate: TEST,
proRun: !TEST,
},
async () => {
const { aliOss } = opts
const { utils: { simpleExcelDown } } = app.fs;
try {
const { models } = app.fs.dc
const today = moment()
const date = today.date()
const month = today.month() + 1
const quarter = today.quarter()
const year = today.year()
const client = new OSS({
// yourRegion填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。
region: aliOss.region,
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
accessKeyId: aliOss.accessKey,
accessKeySecret: aliOss.secretKey,
// 填写Bucket名称,例如examplebucket。
bucket: aliOss.bucket,
});
const statistic = async (startTime, endTime, type, timeShow, typeEnglish, time) => {
const siteRectifyRes = await models.HideDangerRectifySites.findAll({
where: {},
distinct: true,
include: [{
model: models.Site,
attributes: ['id', 'name'],
}, {
model: models.HideDangerDispose,
order: [['id', 'ASC']],
// include: [{
// model: models.User,
// attributes: ['id', 'displayName'],
// }]
}, {
model: models.HideDangerRectify,
where: {
createTime: {
$between: [
startTime.format('YYYY-MM-DD HH:mm:ss'),
endTime.format('YYYY-MM-DD HH:mm:ss')
]
}
},
},]
})
let reportHeader = [{
title: "工程项目名称",
key: "siteName",
}, {
title: "整改任务名称",
key: "name",
}, {
title: "提交时间",
key: "submitTime",
}, {
title: "审批状态",
key: "state",
},]
let reportData = []
let siteMap = new Set()
let completedRectifyCount = 0
let uncompletedRectifyCount = 0
for (let s of siteRectifyRes) {
siteMap.add(s.siteId);
let sts = s.status
let stsChinese = ''
if (sts == 0) {
stsChinese = '待整改'
} else if (sts == 1) {
stsChinese = '待审批'
} else if (sts == 2) {
stsChinese = '待复审'
} else if (sts == 3 || sts == 4) {
stsChinese = '审批驳回'
} else if (sts == 5) {
stsChinese = '审批通过'
}
if (
s.hideDangerDisposes.length
&& s.hideDangerDisposes.some(sd => sd.type == 3 && sd.admit)
) {
completedRectifyCount++
} else {
uncompletedRectifyCount++
}
reportData.push({
siteName: s.dataValues.site.dataValues.name,
name: s.dataValues.hideDangerRectify.dataValues.name,
submitTime: s.dataValues.lastDisposeTime ? moment(s.dataValues.lastDisposeTime).format('YYYY-MM-DD HH:mm:ss') : '',
state: stsChinese
})
}
const fileName = `中鼎国际隐患整改数据报表-${type}-${timeShow}` + '.xlsx'
const filePath = await simpleExcelDown({
data: reportData, header: reportHeader, fileName
})
// const fileData = fs.readFileSync(filePath);
// 保存文件到云
let uploadPath = path.posix.join('hideDangerReport', uuid.v4(), fileName);
let uploadResult = await client.put(
uploadPath,
filePath,
// { contentLength: size }
);
//保存信息到数据库
const existReportRes = await models.HideDangerReport.findOne({
where: {
type: typeEnglish,
time: String(time),
}
})
const storageData = {
siteCount: siteMap.size,
rectifyCount: completedRectifyCount + uncompletedRectifyCount,
completedRectifyCount,
uncompletedRectifyCount,
report: uploadResult.name,
type: typeEnglish,
time: String(time),
}
if (existReportRes) {
await models.HideDangerReport.update(storageData, {
where: {
id: existReportRes.id
}
})
} else {
await models.HideDangerReport.create(storageData)
}
}
if (month == 1) {
// 统计一下上一年
let startTime = today.clone().subtract(1, 'year').startOf('year')
let endTime = today.clone().subtract(1, 'year').endOf('year')
await statistic(
startTime,
endTime,
'年报',
`${startTime.year()}${startTime.month() + 1}-${endTime.month() + 1}`,
'year',
startTime.year()
)
}
if ([1, 4, 7, 10].includes(month)) {
// 统计一下上季度
let startTime = today.clone().subtract(3, 'month').startOf('month')
let endTime = today.clone().subtract(1, 'month').endOf('month')
await statistic(
startTime,
endTime,
'季报',
`${startTime.year()}${startTime.month() + 1}-${endTime.month() + 1}`,
'quarter',
`${startTime.year()}-${month == 1 ? 'Q4' : month == 4 ? 'Q1' : month == 7 ? 'Q2' : 'Q3'}`
)
}
// 统计一下上个月
let startTime = today.clone().subtract(1, 'month').startOf('month')
let endTime = today.clone().subtract(1, 'month').endOf('month')
await statistic(
startTime,
endTime,
'月报',
`${startTime.year()}${startTime.month() + 1}`,
'month',
startTime.format('YYYY-MM')
)
} catch (error) {
app.fs.logger.error(`sechedule: hideDangerStatistic, error: ${error}`);
}
}
);
return {
hideDangerStatistic
}
}