|  | @@ -0,0 +1,513 @@
 | 
	
		
			
				|  |  | +'use strict';
 | 
	
		
			
				|  |  | +const moment = require('moment');
 | 
	
		
			
				|  |  | +const path = require('path');
 | 
	
		
			
				|  |  | +const sendToWormhole = require('stream-wormhole');
 | 
	
		
			
				|  |  | +const constructionConst = require('../const/construction');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +module.exports = app => {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    class ConstructionController extends app.BaseController {
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 构造函数
 | 
	
		
			
				|  |  | +         *
 | 
	
		
			
				|  |  | +         * @param {Object} ctx - egg全局变量
 | 
	
		
			
				|  |  | +         * @return {void}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        constructor(ctx) {
 | 
	
		
			
				|  |  | +            super(ctx);
 | 
	
		
			
				|  |  | +            ctx.showProject = true;
 | 
	
		
			
				|  |  | +            // ctx.showTitle = true;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 支付审批列表页
 | 
	
		
			
				|  |  | +         *
 | 
	
		
			
				|  |  | +         * @param {Object} ctx - egg全局页面
 | 
	
		
			
				|  |  | +         * @return {void}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async index(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                // 获取用户新建标段权利
 | 
	
		
			
				|  |  | +                const accountInfo = await this.ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
 | 
	
		
			
				|  |  | +                const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
 | 
	
		
			
				|  |  | +                    ? JSON.parse(accountInfo.permission) : null;
 | 
	
		
			
				|  |  | +                const tenderList = await ctx.service.tender.getConstructionList('', userPermission, ctx.session.sessionUser.is_admin);
 | 
	
		
			
				|  |  | +                const categoryData = await ctx.service.category.getAllCategory(ctx.session.sessionProject.id);
 | 
	
		
			
				|  |  | +                const renderData = {
 | 
	
		
			
				|  |  | +                    jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.construction.index),
 | 
	
		
			
				|  |  | +                    tenderList,
 | 
	
		
			
				|  |  | +                    categoryData,
 | 
	
		
			
				|  |  | +                    // selfCategoryLevel: accountInfo ? accountInfo.self_category_level : '',
 | 
	
		
			
				|  |  | +                    selfCategoryLevel: '',
 | 
	
		
			
				|  |  | +                    pid: ctx.session.sessionProject.id,
 | 
	
		
			
				|  |  | +                    uid: ctx.session.sessionUser.accountId,
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                if (ctx.session.sessionUser.is_admin) {
 | 
	
		
			
				|  |  | +                    const projectId = ctx.session.sessionProject.id;
 | 
	
		
			
				|  |  | +                    // 获取所有项目参与者
 | 
	
		
			
				|  |  | +                    const accountList = await ctx.service.projectAccount.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                        where: { project_id: ctx.session.sessionProject.id, enable: 1 },
 | 
	
		
			
				|  |  | +                        columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
 | 
	
		
			
				|  |  | +                    const accountGroupList = unitList.map(item => {
 | 
	
		
			
				|  |  | +                        const groupList = accountList.filter(item1 => item1.company === item.name);
 | 
	
		
			
				|  |  | +                        return { groupName: item.name, groupList };
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    renderData.accountList = accountList;
 | 
	
		
			
				|  |  | +                    renderData.accountGroup = accountGroupList;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                await this.layout('construction/index.ejs', renderData, 'construction/modal.ejs');
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                console.log(err);
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                ctx.session.postError = err.toString();
 | 
	
		
			
				|  |  | +                ctx.redirect(this.menu.menu.dashboard.url);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        async auditSave(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                if (ctx.session.sessionUser.is_admin === 0) throw '没有设置权限';
 | 
	
		
			
				|  |  | +                const tid = parseInt(ctx.params.tid);
 | 
	
		
			
				|  |  | +                const responseData = {
 | 
	
		
			
				|  |  | +                    err: 0, msg: '', data: null,
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                const tenderInfo = await ctx.service.tender.getDataById(tid);
 | 
	
		
			
				|  |  | +                if (!tenderInfo) throw '标段不存在';
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                const data = JSON.parse(ctx.request.body.data);
 | 
	
		
			
				|  |  | +                if (!data.type) {
 | 
	
		
			
				|  |  | +                    throw '提交数据错误';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                let uids;
 | 
	
		
			
				|  |  | +                let result = false;
 | 
	
		
			
				|  |  | +                let auditList = [];
 | 
	
		
			
				|  |  | +                switch (data.type) {
 | 
	
		
			
				|  |  | +                    case 'add-audit':
 | 
	
		
			
				|  |  | +                        // // 判断用户是单个还是数组
 | 
	
		
			
				|  |  | +                        uids = data.id instanceof Array ? data.id : [data.id];
 | 
	
		
			
				|  |  | +                        // // 判断该用户的组是否已加入到表中,已加入则提示无需添加
 | 
	
		
			
				|  |  | +                        auditList = await ctx.service.constructionAudit.getAllDataByCondition({ where: { tid } });
 | 
	
		
			
				|  |  | +                        const addAidList = ctx.helper._.difference(uids, ctx.helper._.map(auditList, 'uid'));
 | 
	
		
			
				|  |  | +                        if (addAidList.length === 0) {
 | 
	
		
			
				|  |  | +                            throw '用户已存在成员管理中,无需重复添加';
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { id: addAidList } });
 | 
	
		
			
				|  |  | +                        await ctx.service.constructionAudit.saveAudits(tid, accountList);
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionAudit.getList(tid);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'del-audit':
 | 
	
		
			
				|  |  | +                        uids = data.id instanceof Array ? data.id : [data.id];
 | 
	
		
			
				|  |  | +                        auditList = await ctx.service.constructionAudit.getAllDataByCondition({ where: { tid, id: uids } });
 | 
	
		
			
				|  |  | +                        if (auditList.length !== uids.length) {
 | 
	
		
			
				|  |  | +                            throw '该用户已不存在成员管理中,移除失败';
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        await ctx.service.constructionAudit.delAudit(uids);
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionAudit.getList(tid);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'save-report':
 | 
	
		
			
				|  |  | +                        result = await ctx.service.constructionAudit.updateReport(data.updateData);
 | 
	
		
			
				|  |  | +                        if (!result) {
 | 
	
		
			
				|  |  | +                            throw '修改权限失败';
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'list':
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionAudit.getList(tid);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    default: throw '参数有误';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                ctx.body = responseData;
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                ctx.body = { err: 1, msg: err.toString(), data: null };
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        async list(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                // 获取上报人列表
 | 
	
		
			
				|  |  | +                const reportUserList = await ctx.service.constructionAudit.getUserList(ctx.constructionTender.id, 1);
 | 
	
		
			
				|  |  | +                // 判断是否有状态及上报人选中
 | 
	
		
			
				|  |  | +                const params = { tid: ctx.constructionTender.id };
 | 
	
		
			
				|  |  | +                let curReportUser = null;
 | 
	
		
			
				|  |  | +                if (ctx.query.uid) {
 | 
	
		
			
				|  |  | +                    curReportUser = ctx.helper._.find(reportUserList, { uid: parseInt(ctx.query.uid) });
 | 
	
		
			
				|  |  | +                    if (curReportUser) {
 | 
	
		
			
				|  |  | +                        params.report_uid = curReportUser.uid;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                const statusList = [];
 | 
	
		
			
				|  |  | +                for (const s in constructionConst.status) {
 | 
	
		
			
				|  |  | +                    const findObject = { tid: ctx.constructionTender.id, status: constructionConst.status[s] };
 | 
	
		
			
				|  |  | +                    if (params.report_uid) findObject.report_uid = params.report_uid;
 | 
	
		
			
				|  |  | +                    const num = await ctx.service.constructionLog.count(findObject);
 | 
	
		
			
				|  |  | +                    statusList.push({
 | 
	
		
			
				|  |  | +                        statusName: constructionConst.statusString[constructionConst.status[s]],
 | 
	
		
			
				|  |  | +                        status: constructionConst.status[s],
 | 
	
		
			
				|  |  | +                        num,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                let curStatus = null;
 | 
	
		
			
				|  |  | +                if (ctx.query.status) {
 | 
	
		
			
				|  |  | +                    curStatus = ctx.helper._.find(statusList, { status: parseInt(ctx.query.status) });
 | 
	
		
			
				|  |  | +                    if (curStatus) {
 | 
	
		
			
				|  |  | +                        params.status = curStatus.status;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                const page = ctx.page;
 | 
	
		
			
				|  |  | +                const total = await ctx.service.constructionLog.getCount(params);
 | 
	
		
			
				|  |  | +                ctx.sort = ['id', 'desc'];
 | 
	
		
			
				|  |  | +                const logList = await ctx.service.constructionLog.getLogList(params);
 | 
	
		
			
				|  |  | +                // 分页相关
 | 
	
		
			
				|  |  | +                const pageInfo = {
 | 
	
		
			
				|  |  | +                    page,
 | 
	
		
			
				|  |  | +                    total: Math.ceil(total / app.config.pageSize),
 | 
	
		
			
				|  |  | +                    queryData: JSON.stringify(ctx.urlInfo.query),
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                const renderData = {
 | 
	
		
			
				|  |  | +                    reportUserList,
 | 
	
		
			
				|  |  | +                    statusList,
 | 
	
		
			
				|  |  | +                    curStatus,
 | 
	
		
			
				|  |  | +                    curReportUser,
 | 
	
		
			
				|  |  | +                    pageInfo,
 | 
	
		
			
				|  |  | +                    logList,
 | 
	
		
			
				|  |  | +                    constructionConst,
 | 
	
		
			
				|  |  | +                    moment,
 | 
	
		
			
				|  |  | +                    jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.construction.list),
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                // 获取自己所属的待提交状态,且shenpi_uid不为空的日志列表
 | 
	
		
			
				|  |  | +                let reportFlag = false;
 | 
	
		
			
				|  |  | +                if (ctx.helper._.findIndex(reportUserList, { uid: ctx.session.sessionUser.accountId }) !== -1) {
 | 
	
		
			
				|  |  | +                    reportFlag = true;
 | 
	
		
			
				|  |  | +                    renderData.uncheckLogList = await ctx.service.constructionLog.getUncheckedLogList(ctx.constructionTender.id, ctx.session.sessionUser.accountId);
 | 
	
		
			
				|  |  | +                    renderData.dateCodeList = await ctx.service.constructionLog.getDateCodeList(ctx.constructionTender.id);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                renderData.reportFlag = reportFlag;
 | 
	
		
			
				|  |  | +                await this.layout('construction/list.ejs', renderData, 'construction/list_modal.ejs');
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                console.log(err);
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                ctx.session.postError = err.toString();
 | 
	
		
			
				|  |  | +                ctx.redirect(this.request && this.request.headers && this.request.headers.referer ? this.request.headers.referer : '/construction');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        async addLog(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const responseData = {
 | 
	
		
			
				|  |  | +                    err: 0, msg: '', data: null,
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                const data = JSON.parse(ctx.request.body.data);
 | 
	
		
			
				|  |  | +                const reportUser = await ctx.service.constructionAudit.getDataByCondition({ tid: ctx.constructionTender.id, uid: ctx.session.sessionUser.accountId });
 | 
	
		
			
				|  |  | +                if (!reportUser) {
 | 
	
		
			
				|  |  | +                    throw '非填报人无法新建日志';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                responseData.data = await ctx.service.constructionLog.addLog(ctx.constructionTender.id, ctx.session.sessionUser.accountId, data);
 | 
	
		
			
				|  |  | +                ctx.body = responseData;
 | 
	
		
			
				|  |  | +            } catch (error) {
 | 
	
		
			
				|  |  | +                this.log(error);
 | 
	
		
			
				|  |  | +                ctx.body = { err: 1, msg: error.toString(), data: null };
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 批量提交
 | 
	
		
			
				|  |  | +        async startMulti(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const responseData = {
 | 
	
		
			
				|  |  | +                    err: 0, msg: '', data: null,
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                const data = JSON.parse(ctx.request.body.data);
 | 
	
		
			
				|  |  | +                const reportUser = await ctx.service.constructionAudit.getDataByCondition({ tid: ctx.constructionTender.id, uid: ctx.session.sessionUser.accountId });
 | 
	
		
			
				|  |  | +                if (!reportUser) {
 | 
	
		
			
				|  |  | +                    throw '非填报人无法提交日志';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                responseData.data = await ctx.service.constructionLog.startMulti(data.ids, ctx.session.sessionUser.accountId);
 | 
	
		
			
				|  |  | +                ctx.body = responseData;
 | 
	
		
			
				|  |  | +            } catch (error) {
 | 
	
		
			
				|  |  | +                this.log(error);
 | 
	
		
			
				|  |  | +                ctx.body = { err: 1, msg: error.toString(), data: null };
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        async deleteLog(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const responseData = {
 | 
	
		
			
				|  |  | +                    err: 0, msg: '', data: null,
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                const id = parseInt(ctx.params.id);
 | 
	
		
			
				|  |  | +                if (!id) throw '参数有误';
 | 
	
		
			
				|  |  | +                const logInfo = await ctx.service.constructionLog.getDataById(id);
 | 
	
		
			
				|  |  | +                if (!logInfo) throw '该日志不存在';
 | 
	
		
			
				|  |  | +                if (logInfo.report_uid !== ctx.session.sessionUser.accountId) {
 | 
	
		
			
				|  |  | +                    throw '非该日志填报人无法删除日志';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                responseData.data = await ctx.service.constructionLog.deleteLog(logInfo.id);
 | 
	
		
			
				|  |  | +                ctx.body = responseData;
 | 
	
		
			
				|  |  | +            } catch (error) {
 | 
	
		
			
				|  |  | +                this.log(error);
 | 
	
		
			
				|  |  | +                ctx.body = { err: 1, msg: error.toString(), data: null };
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        async logInfo(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const id = parseInt(ctx.params.id);
 | 
	
		
			
				|  |  | +                if (!id) throw '参数有误';
 | 
	
		
			
				|  |  | +                const logInfo = await ctx.service.constructionLog.getDataById(id);
 | 
	
		
			
				|  |  | +                if (!logInfo) throw '该日志不存在';
 | 
	
		
			
				|  |  | +                logInfo.log_json = logInfo.log_json ? JSON.parse(logInfo.log_json) : ctx.helper._.cloneDeep(constructionConst.logJson);
 | 
	
		
			
				|  |  | +                const report_user = await ctx.service.projectAccount.getDataById(logInfo.report_uid);
 | 
	
		
			
				|  |  | +                logInfo.report_username = report_user ? report_user.name : '';
 | 
	
		
			
				|  |  | +                const shenpi_user = logInfo.shenpi_uid ? await ctx.service.projectAccount.getDataById(logInfo.shenpi_uid) : null;
 | 
	
		
			
				|  |  | +                logInfo.shenpi_username = shenpi_user ? shenpi_user.name : '';
 | 
	
		
			
				|  |  | +                let filePermission = false;
 | 
	
		
			
				|  |  | +                if (ctx.session.sessionUser.accountId === logInfo.report_uid || ctx.session.sessionUser.accountId === logInfo.shenpi_uid) {
 | 
	
		
			
				|  |  | +                    filePermission = true;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                // 获取附件列表
 | 
	
		
			
				|  |  | +                const attList = await ctx.service.constructionAtt.getConstructionAttachment(logInfo.id, 'desc');
 | 
	
		
			
				|  |  | +                const renderData = {
 | 
	
		
			
				|  |  | +                    logInfo,
 | 
	
		
			
				|  |  | +                    constructionConst,
 | 
	
		
			
				|  |  | +                    attList,
 | 
	
		
			
				|  |  | +                    filePermission,
 | 
	
		
			
				|  |  | +                    whiteList: ctx.app.config.multipart.whitelist,
 | 
	
		
			
				|  |  | +                    OSS_PATH: ctx.app.config.fujianOssPath,
 | 
	
		
			
				|  |  | +                    jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.construction.info),
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                if (ctx.session.sessionUser.accountId === logInfo.report_uid && logInfo.status === constructionConst.status.uncheck) {
 | 
	
		
			
				|  |  | +                    const projectId = ctx.session.sessionProject.id;
 | 
	
		
			
				|  |  | +                    // 获取所有项目参与者
 | 
	
		
			
				|  |  | +                    const accountList = await ctx.service.projectAccount.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                        where: { project_id: ctx.session.sessionProject.id, enable: 1 },
 | 
	
		
			
				|  |  | +                        columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
 | 
	
		
			
				|  |  | +                    const accountGroupList = unitList.map(item => {
 | 
	
		
			
				|  |  | +                        const groupList = accountList.filter(item1 => item1.company === item.name);
 | 
	
		
			
				|  |  | +                        return { groupName: item.name, groupList };
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    renderData.accountList = accountList;
 | 
	
		
			
				|  |  | +                    renderData.accountGroup = accountGroupList;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                await this.layout('construction/info.ejs', renderData, 'construction/info_modal.ejs');
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                console.log(err);
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                ctx.session.postError = err.toString();
 | 
	
		
			
				|  |  | +                ctx.redirect(ctx.constructionTender ? '/constrution/' + ctx.constructionTender.id + '/list' : '/construction');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        async logSave(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const id = parseInt(ctx.params.id);
 | 
	
		
			
				|  |  | +                if (!id) throw '参数有误';
 | 
	
		
			
				|  |  | +                const logInfo = await ctx.service.constructionLog.getDataById(id);
 | 
	
		
			
				|  |  | +                if (!logInfo) throw '该日志不存在';
 | 
	
		
			
				|  |  | +                const data = JSON.parse(ctx.request.body.data);
 | 
	
		
			
				|  |  | +                // 检查权限等
 | 
	
		
			
				|  |  | +                if (logInfo.status === constructionConst.status.checked) {
 | 
	
		
			
				|  |  | +                    throw '该日志已审签,无法再操作';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                if (logInfo.report_uid !== ctx.session.sessionUser.accountId && !(logInfo.shenpi_uid === ctx.session.sessionUser.accountId && data.type === 'checked')) {
 | 
	
		
			
				|  |  | +                    throw '您无权操作';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                const responseData = {
 | 
	
		
			
				|  |  | +                    err: 0, msg: '', data: {},
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                switch (data.type) {
 | 
	
		
			
				|  |  | +                    case 'update_json':
 | 
	
		
			
				|  |  | +                        logInfo.log_json = logInfo.log_json ? JSON.parse(logInfo.log_json) : ctx.helper._.cloneDeep(constructionConst.logJson);
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionLog.updateLogJson(logInfo.id, logInfo.log_json, data.updateData);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'set_shenpi':
 | 
	
		
			
				|  |  | +                        if (data.uid === logInfo.report_uid) {
 | 
	
		
			
				|  |  | +                            throw '审签人不能同时为填报人';
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionLog.setShenpiUser(ctx.constructionTender.id, logInfo.id, data.uid);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'remove_shenpi':
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionLog.removeShenpiUser(logInfo.id);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'checked':
 | 
	
		
			
				|  |  | +                        if (logInfo.shenpi_uid !== ctx.session.sessionUser.accountId) {
 | 
	
		
			
				|  |  | +                            throw '非当前审签人无法审签日志';
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionLog.checked(logInfo.id);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'start':
 | 
	
		
			
				|  |  | +                        const reportUser = await ctx.service.constructionAudit.getDataByCondition({ tid: ctx.constructionTender.id, uid: ctx.session.sessionUser.accountId });
 | 
	
		
			
				|  |  | +                        if (!reportUser) {
 | 
	
		
			
				|  |  | +                            throw '非填报人无法提交日志';
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        if (!logInfo.shenpi_uid) throw '请选择审签人再提交';
 | 
	
		
			
				|  |  | +                        responseData.data = await ctx.service.constructionLog.start(logInfo.id);
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    default: throw '参数有误';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                ctx.body = responseData;
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                ctx.body = { err: 1, msg: err.toString(), data: null };
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 上传附件
 | 
	
		
			
				|  |  | +         * @param {Object} ctx - egg全局变量
 | 
	
		
			
				|  |  | +         * @return {void}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async uploadFile(ctx) {
 | 
	
		
			
				|  |  | +            const responseData = {
 | 
	
		
			
				|  |  | +                err: 0,
 | 
	
		
			
				|  |  | +                msg: '',
 | 
	
		
			
				|  |  | +                data: [],
 | 
	
		
			
				|  |  | +            };
 | 
	
		
			
				|  |  | +            let stream;
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const id = parseInt(ctx.params.id);
 | 
	
		
			
				|  |  | +                if (!id) throw '参数有误';
 | 
	
		
			
				|  |  | +                const logInfo = await ctx.service.constructionLog.getDataById(id);
 | 
	
		
			
				|  |  | +                if (!logInfo) throw '该日志不存在';
 | 
	
		
			
				|  |  | +                const parts = ctx.multipart({ autoFields: true });
 | 
	
		
			
				|  |  | +                const files = [];
 | 
	
		
			
				|  |  | +                let index = 0;
 | 
	
		
			
				|  |  | +                const extra_upload = logInfo.status === constructionConst.status.checked;
 | 
	
		
			
				|  |  | +                const original_data = {
 | 
	
		
			
				|  |  | +                    tid: ctx.constructionTender.id,
 | 
	
		
			
				|  |  | +                    log_id: logInfo.id,
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                while ((stream = await parts()) !== undefined) {
 | 
	
		
			
				|  |  | +                    // 判断用户是否选择上传文件
 | 
	
		
			
				|  |  | +                    if (!stream.filename) {
 | 
	
		
			
				|  |  | +                        throw '请选择上传的文件!';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    const fileInfo = path.parse(stream.filename);
 | 
	
		
			
				|  |  | +                    const create_time = Date.parse(new Date()) / 1000;
 | 
	
		
			
				|  |  | +                    const filepath = `app/public/upload/${original_data.tid}/construction/fujian_${create_time + index.toString() + fileInfo.ext}`;
 | 
	
		
			
				|  |  | +                    await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
 | 
	
		
			
				|  |  | +                    await sendToWormhole(stream);
 | 
	
		
			
				|  |  | +                    // 保存数据到att表
 | 
	
		
			
				|  |  | +                    const fileData = {
 | 
	
		
			
				|  |  | +                        upload_time: new Date(),
 | 
	
		
			
				|  |  | +                        filename: fileInfo.name,
 | 
	
		
			
				|  |  | +                        fileext: fileInfo.ext,
 | 
	
		
			
				|  |  | +                        filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
 | 
	
		
			
				|  |  | +                        filepath,
 | 
	
		
			
				|  |  | +                        extra_upload,
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                    const result = await ctx.service.constructionAtt.save(original_data, fileData, ctx.session.sessionUser.accountId);
 | 
	
		
			
				|  |  | +                    if (!result) {
 | 
	
		
			
				|  |  | +                        throw '导入数据库保存失败';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    fileData.uid = ctx.session.sessionUser.accountId;
 | 
	
		
			
				|  |  | +                    fileData.u_name = ctx.session.sessionUser.name;
 | 
	
		
			
				|  |  | +                    fileData.id = result.insertId;
 | 
	
		
			
				|  |  | +                    fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
 | 
	
		
			
				|  |  | +                    delete fileData.filepath;
 | 
	
		
			
				|  |  | +                    if (!ctx.helper.canPreview(fileData.fileext)) {
 | 
	
		
			
				|  |  | +                        fileData.filepath = `/construction/${original_data.tid}/log/${original_data.log_id}/file/${fileData.id}/download`;
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        fileData.filepath = ctx.app.config.fujianOssPath + filepath;
 | 
	
		
			
				|  |  | +                        fileData.viewpath = ctx.app.config.fujianOssPath + filepath;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    files.push(fileData);
 | 
	
		
			
				|  |  | +                    ++index;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                responseData.data = files;
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                // 失败需要消耗掉stream 以防卡死
 | 
	
		
			
				|  |  | +                if (stream) {
 | 
	
		
			
				|  |  | +                    await sendToWormhole(stream);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                this.setMessage(err.toString(), this.messageType.ERROR);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            ctx.body = responseData;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 下载附件
 | 
	
		
			
				|  |  | +         * @param {Object} ctx - egg全局变量
 | 
	
		
			
				|  |  | +         * @return {void}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async downloadFile(ctx) {
 | 
	
		
			
				|  |  | +            const id = ctx.params.fid;
 | 
	
		
			
				|  |  | +            if (id) {
 | 
	
		
			
				|  |  | +                try {
 | 
	
		
			
				|  |  | +                    const fileInfo = await ctx.service.constructionAtt.getDataById(id);
 | 
	
		
			
				|  |  | +                    if (fileInfo !== undefined && fileInfo !== '') {
 | 
	
		
			
				|  |  | +                        // 解决中文无法下载问题
 | 
	
		
			
				|  |  | +                        const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
 | 
	
		
			
				|  |  | +                        let disposition = '';
 | 
	
		
			
				|  |  | +                        if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
 | 
	
		
			
				|  |  | +                            disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
 | 
	
		
			
				|  |  | +                        } else if (userAgent.indexOf('firefox') >= 0) {
 | 
	
		
			
				|  |  | +                            disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
 | 
	
		
			
				|  |  | +                        } else {
 | 
	
		
			
				|  |  | +                            /* safari等其他非主流浏览器只能自求多福了 */
 | 
	
		
			
				|  |  | +                            disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        ctx.response.set({
 | 
	
		
			
				|  |  | +                            'Content-Type': 'application/octet-stream',
 | 
	
		
			
				|  |  | +                            'Content-Disposition': disposition,
 | 
	
		
			
				|  |  | +                            'Content-Length': fileInfo.filesize,
 | 
	
		
			
				|  |  | +                        });
 | 
	
		
			
				|  |  | +                        // ctx.body = await fs.createReadStream(fileName);
 | 
	
		
			
				|  |  | +                        ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        throw '不存在该文件';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                } catch (err) {
 | 
	
		
			
				|  |  | +                    this.log(err);
 | 
	
		
			
				|  |  | +                    this.setMessage(err.toString(), this.messageType.ERROR);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 删除附件
 | 
	
		
			
				|  |  | +         * @param {Object} ctx - egg全局变量
 | 
	
		
			
				|  |  | +         * @return {void}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async deleteFile(ctx) {
 | 
	
		
			
				|  |  | +            const responseData = {
 | 
	
		
			
				|  |  | +                err: 0,
 | 
	
		
			
				|  |  | +                msg: '',
 | 
	
		
			
				|  |  | +                data: '',
 | 
	
		
			
				|  |  | +            };
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const id = parseInt(ctx.params.id);
 | 
	
		
			
				|  |  | +                if (!id) throw '参数有误';
 | 
	
		
			
				|  |  | +                const logInfo = await ctx.service.constructionLog.getDataById(id);
 | 
	
		
			
				|  |  | +                if (!logInfo) throw '该日志不存在';
 | 
	
		
			
				|  |  | +                const data = JSON.parse(ctx.request.body.data);
 | 
	
		
			
				|  |  | +                const fileInfo = await ctx.service.constructionAtt.getDataById(data.id);
 | 
	
		
			
				|  |  | +                if (!fileInfo || !Object.keys(fileInfo).length) {
 | 
	
		
			
				|  |  | +                    throw '该文件不存在';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                if (!fileInfo.extra_upload && logInfo.status === constructionConst.status.checked) {
 | 
	
		
			
				|  |  | +                    throw '无权限删除';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                if (fileInfo !== undefined && fileInfo !== '') {
 | 
	
		
			
				|  |  | +                    // 先删除文件
 | 
	
		
			
				|  |  | +                    await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
 | 
	
		
			
				|  |  | +                    // 再删除数据库
 | 
	
		
			
				|  |  | +                    await ctx.service.constructionAtt.deleteById(data.id);
 | 
	
		
			
				|  |  | +                    responseData.data = '';
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    throw '不存在该文件';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                responseData.err = 1;
 | 
	
		
			
				|  |  | +                responseData.msg = err;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            ctx.body = responseData;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    return ConstructionController;
 | 
	
		
			
				|  |  | +};
 |