|
|
@ -1,6 +1,6 @@ |
|
|
|
'use strict'; |
|
|
|
const { QueryTypes } = require('sequelize'); |
|
|
|
|
|
|
|
const moment = require('moment') |
|
|
|
async function reportList (ctx) { |
|
|
|
try { |
|
|
|
const models = ctx.fs.dc.models; |
|
|
@ -289,8 +289,160 @@ async function deleteReport (ctx) { |
|
|
|
|
|
|
|
// TODO 小程序填写道路名称的时候的道路筛选 是一起都返回 还是不断传关键字搜索返回
|
|
|
|
|
|
|
|
async function spotPrepare (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const sequelize = ctx.fs.dc.orm; |
|
|
|
const { percentage } = ctx.request.body; |
|
|
|
|
|
|
|
const departmentIdRes = await models.Department.findAll({ |
|
|
|
attributes: ['id', 'name'], |
|
|
|
where: { |
|
|
|
dependence: null, |
|
|
|
delete: false, |
|
|
|
} |
|
|
|
}) |
|
|
|
let lukyDepartment = '' |
|
|
|
if (departmentIdRes.length) { |
|
|
|
lukyDepartment = departmentIdRes[(Math.round(Math.random() * departmentIdRes.length) || 1) - 1] |
|
|
|
} else { |
|
|
|
throw `暂无乡镇信息` |
|
|
|
} |
|
|
|
|
|
|
|
const sqlStr = ` |
|
|
|
WITH RECURSIVE sub_departments AS ( |
|
|
|
SELECT id, dependence |
|
|
|
FROM department |
|
|
|
WHERE id = ${lukyDepartment.id} |
|
|
|
UNION ALL |
|
|
|
SELECT d.id, d.dependence |
|
|
|
FROM sub_departments sd |
|
|
|
JOIN department d ON sd.id = d.dependence |
|
|
|
) |
|
|
|
SELECT u.id |
|
|
|
FROM "user" AS u |
|
|
|
JOIN sub_departments sd ON u.department_id = sd.id |
|
|
|
WHERE u.delete = false; |
|
|
|
` |
|
|
|
|
|
|
|
const userRes = await sequelize.query(sqlStr, { type: QueryTypes.SELECT }) |
|
|
|
let findUsers = [] |
|
|
|
findUsers = userRes.map(item => { |
|
|
|
return item.id |
|
|
|
}) |
|
|
|
|
|
|
|
const reportCount = findUsers.length ? await models.Report.count({ |
|
|
|
where: { |
|
|
|
reportType: 'conserve', |
|
|
|
userId: { $in: findUsers } |
|
|
|
} |
|
|
|
}) : 0 |
|
|
|
|
|
|
|
const previewRes = await models.ReportSpotCheckPreview.create({ |
|
|
|
percentage: percentage, |
|
|
|
departmentId: lukyDepartment.id, |
|
|
|
date: moment().format(), |
|
|
|
reportCount: reportCount, |
|
|
|
checked: false, |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = { |
|
|
|
lukyDepartment, |
|
|
|
reportCount, |
|
|
|
previewId: previewRes.id |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function spotCheck (ctx) { |
|
|
|
const transaction = await ctx.fs.dc.orm.transaction(); |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const sequelize = ctx.fs.dc.orm; |
|
|
|
const { previewId } = ctx.query |
|
|
|
if (!previewId) { |
|
|
|
throw '参数错误' |
|
|
|
} |
|
|
|
|
|
|
|
const prepareRes = await models.ReportSpotCheckPreview.find({ |
|
|
|
id: previewId |
|
|
|
}) |
|
|
|
|
|
|
|
const sqlStr = ` |
|
|
|
WITH RECURSIVE sub_departments AS ( |
|
|
|
SELECT id, dependence |
|
|
|
FROM department |
|
|
|
WHERE id = ${prepareRes.departmentId} |
|
|
|
UNION ALL |
|
|
|
SELECT d.id, d.dependence |
|
|
|
FROM sub_departments sd |
|
|
|
JOIN department d ON sd.id = d.dependence |
|
|
|
) |
|
|
|
SELECT u.id |
|
|
|
FROM "user" AS u |
|
|
|
JOIN sub_departments sd ON u.department_id = sd.id |
|
|
|
WHERE u.delete = false; |
|
|
|
` |
|
|
|
|
|
|
|
const userRes = await sequelize.query(sqlStr, { type: QueryTypes.SELECT }) |
|
|
|
let findUsers = [] |
|
|
|
findUsers = userRes.map(item => { |
|
|
|
return item.id |
|
|
|
}) |
|
|
|
|
|
|
|
let checkCount = Math.ceil(prepareRes.reportCount * (prepareRes.percentage / 100)) |
|
|
|
const reportRes = findUsers.length && checkCount ? await models.Report.findAll({ |
|
|
|
where: { |
|
|
|
reportType: 'conserve', |
|
|
|
userId: { $in: findUsers } |
|
|
|
}, |
|
|
|
order: sequelize.random(), // 随机排序
|
|
|
|
limit: checkCount, // 限制返回的记录数
|
|
|
|
}) : [] |
|
|
|
|
|
|
|
await models.ReportSpotCheckPreview.update({ |
|
|
|
checked: true |
|
|
|
}, { |
|
|
|
where: { |
|
|
|
id: previewId |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
if (reportRes.length) { |
|
|
|
let spotDate = moment().format('YYYY-MM-DD') |
|
|
|
await models.ReportSpotCheck.bulkCreate(reportRes.map(r => { |
|
|
|
return { |
|
|
|
reportId: r.id, |
|
|
|
spotDate: spotDate, |
|
|
|
prepareId: previewId |
|
|
|
} |
|
|
|
})) |
|
|
|
} |
|
|
|
|
|
|
|
await transaction.commit(); |
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = reportRes |
|
|
|
} catch (error) { |
|
|
|
await transaction.rollback(); |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : undefined |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
reportList, |
|
|
|
reportPosition, |
|
|
|
reportDetail, createReport, deleteReport, reportHandle |
|
|
|
reportDetail, createReport, deleteReport, reportHandle, |
|
|
|
spotPrepare, spotCheck, |
|
|
|
}; |