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.

203 lines
5.1 KiB

2 years ago
'use strict';
async function getFirmList (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { limit, page, siteId, siteNameKeyword, descKeyword } = ctx.query;
let userInfo = ctx.fs.api.userInfo;
let res = await models.Company.findAll({
where: {
del: false,
},
attributes: ['id', 'name'],
distinct: true,
});
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取公司列表失败"
}
}
}
async function projectList (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const { limit, page, companyId, name, type, status } = ctx.query;
let options = {
where: {
del: false,
},
include: [{
as: 'company',
model: models.Company,
attributes: ['id', 'name'],
},],
order: [['createTime', 'DESC']],
}
if (limit) {
options.limit = Number(limit)
}
if (page && limit) {
options.offset = Number(page) * Number(limit)
}
if (companyId) {
options.where.companyId = companyId
}
if (type) {
options.where.type = type
}
if (name) {
options.where.name = { $like: `%${name}%` }
}
if (status) {
options.where.status = status
}
let res = await models.Site.findAndCountAll(options)
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取工程交底列表失败"
}
}
}
async function getUserSiteList (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userSites = ctx.fs.api.userInfo.relateSites;//用户关注工地
let where = { del: false }
if (userSites && userSites.length) {
where.id = { $in: userSites }
}
let res = await models.Site.findAll({
where: where,
attributes: ['id', 'name'],
include: {
model: models.Company,
attributes: ['id', 'name'],
},
});
ctx.status = 200;
ctx.body = res
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "获取用户关注工地列表失败"
}
}
}
async function postAddProject (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const data = ctx.request.body;
const { companyId, address, amount, builder, contractor, createTime, designer, endTime, img, latitude,
longitude, name, peopleInCharge, scale, startTime, supervisor, tel, type, } = data
let errMsg = data.id ? '工程编辑失败' : '工程新增失败'
let project = {
companyId, address, amount, builder, contractor, createTime, designer, endTime, img, latitude,
longitude, name, peopleInCharge, scale, startTime, supervisor, tel, type, del: false, userId: userInfo.id
}
const alikeProject = await models.Site.findOne({
where: {
name: name,
del: false,
}
})
if ((!data.id && alikeProject) || (alikeProject && alikeProject.id !== data.id)) {
errMsg = '已有相同项目名称'
throw errMsg
}
if (data && data.id) {
await models.Site.update(project, {
where: {
id: data.id
}
})
} else {
await models.Site.create(project)
}
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400;
ctx.body = {
"message": errMsg
}
}
}
async function delProject (ctx, next) {
try {
const models = ctx.fs.dc.models;
let userInfo = ctx.fs.api.userInfo;
const { id } = ctx.params
await models.Site.destroy({
where: {
id,
}
})
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400;
ctx.body = {
"message": '删除工程概况失败'
}
}
}
async function putProjectStatus (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id, status } = ctx.request.body;
if (!id || !status) {
errMsg = '必须传入id和status'
throw errMsg
}
await models.Site.update({ status }, {
where: {
id
}
})
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400;
ctx.body = {
"message": '修改项目状态失败'
}
}
}
module.exports = {
getFirmList,
projectList,
getUserSiteList,
postAddProject,
delProject,
putProjectStatus
}