'use strict'; const moment = require('moment') function getApproveList(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { page, limit, applyAt, approveState, resourceName, applyBy, applyById, state ,} = ctx.query; let errMsg = { message: '获取消费审批列表失败' } try { let option = { where: {}, order: [["id", "desc"]], distinct: true, include: [{ model: models.User, as: 'applyUser', attributes: ['id', 'name'] }, { model: models.User, as: 'approveUser', attributes: ['id', 'name'] },] } if (resourceName) { option.where.resourceName = { $iLike: `%${resourceName}%` } } if (applyBy) { option.include[0].where = { '$applyUser.name$': { $iLike: `%${applyBy}%` } } } if (applyById) { option.where.applyBy = applyById } if (limit) { option.limit = Number(limit) } if (page && limit) { option.offset = Number(page) * Number(limit) } if (approveState) { option.where.approveState = approveState } if (state == 1) { option.where.approveState = '已审批' option.where.token = { $ne: null } } if (state == 2) { option.where.approveState = '已审批' option.where.token = null option.where.approveRemarks = { $ne: null } } if (state == 3) { option.where.approveState = '审批中' } if (applyAt) { option.where.applyAt = { $between: [ moment(applyAt).startOf('day').format('YYYY-MM-DD HH:mm:ss'), moment(applyAt).endOf('day').format('YYYY-MM-DD HH:mm:ss') ] } } const res = await models.ResourceConsumption.findAndCount(option); ctx.status = 200; ctx.body = res; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } // 新增模型 function addModelManagement(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; try { const { attributeName, attributeCode, modelType } = ctx.request.body const checkName = await models.MetaModel.findOne({ where: { attributeName, modelType } }); const checkCode = await models.MetaModel.findOne({ where: { attributeCode, modelType } }); if (checkName || checkCode) { ctx.status = 400; ctx.body = { message: checkName ? '该属性名称已存在' : "该属性代码已存在" } } else { let rslt = ctx.request.body; await models.MetaModel.create(Object.assign({}, rslt)) ctx.status = 204; ctx.body = { message: '新建模型成功' } } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '新建模型失败' } } } } // 修改模型 function postApprove(opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const data = ctx.request.body; const { id } = data await models.ResourceConsumption.update( data, { where: { id: id, } } ) ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '消费审批失败' } } } } // 删除模型 function deleteModelManagement(opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.params; await models.MetaModel.destroy({ where: { id: id } }) ctx.status = 204; ctx.body = { message: '删除模型成功' } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '删除模型失败' } } } } module.exports = { getApproveList, postApprove, }