'use strict'; /** * Created by EllisRan on 2020/3/3. */ const BaseService = require('../base/base_service'); const constructionConst = require('../const/construction'); module.exports = app => { class ConstructionLog extends BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'construction_log'; } async getCount(params) { return await this.count(params); } async getLogList(params) { // 获取所有标段的成员管理人员 // const userList = await this.ctx.service.constructionAudit.getUserList(params.tid); const list = await this.getList({ where: params }); if (list.length > 0) { const userIdList = this._.uniq(this._.concat(this._.map(list, 'report_uid'), this._.map(list, 'shenpi_uid'))); const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: userIdList } }); for (const l of list) { l.report_username = this._.find(userList, { id: l.report_uid }) ? this._.find(userList, { id: l.report_uid }).name : ''; l.shenpi_username = l.shenpi_uid && this._.find(userList, { id: l.shenpi_uid }) ? this._.find(userList, { id: l.shenpi_uid }).name : ''; } } return list; } async getUncheckedLogList(tid, uid) { const uncheckList = await this.getAllDataByCondition({ where: { tid, report_uid: uid, status: constructionConst.status.uncheck }, orders: [['id', 'desc']] }); const list = this._.filter(uncheckList, item => item.shenpi_uid !== null); if (list.length > 0) { const userIdList = this._.uniq(this._.concat(this._.map(list, 'report_uid'), this._.map(list, 'shenpi_uid'))); const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: userIdList } }); for (const l of list) { l.report_username = this._.find(userList, { id: l.report_uid }) ? this._.find(userList, { id: l.report_uid }).name : ''; l.shenpi_username = l.shenpi_uid && this._.find(userList, { id: l.shenpi_uid }) ? this._.find(userList, { id: l.shenpi_uid }).name : ''; } } return list; } async getDateCodeList(tid) { const sql = 'SELECT `date_code`, count(*) AS num FROM ?? WHERE `tid` = ? GROUP BY `date_code` ORDER BY `date_code`'; const params = [this.tableName, tid]; return await this.db.query(sql, params); } async addLog(tid, uid, data) { if (!data) throw '参数有误'; if (!data.code || !data.period || data.type === undefined) throw '参数有误'; const newData = { tid, report_uid: uid, create_time: new Date(), type: data.type ? data.type : 0, period: data.period, code: data.code, date_code: data.code.substring(0, 8), }; const result = await this.db.insert(this.tableName, newData); return result.insertId; } async updateLogJson(id, json, data) { json[data.key] = data.value; const updateData = { id, log_json: JSON.stringify(json), }; return await this.db.update(this.tableName, updateData); } async setShenpiUser(tid, id, shenpi_uid) { const transaction = await this.db.beginTransaction(); try { const userInfo = await this.ctx.service.projectAccount.getDataById(shenpi_uid); if (!userInfo) throw '该用户不存在'; await transaction.update(this.tableName, { id, shenpi_uid }); // 判断有无,无则插入到constructionAudit成员管理表里 const audit = await this.ctx.service.constructionAudit.getDataByCondition({ tid, uid: shenpi_uid }); if (!audit) { const addAudit = { tid, uid: shenpi_uid, in_time: new Date(), }; await transaction.insert(this.ctx.service.constructionAudit.tableName, addAudit); } await transaction.commit(); return { shenpi_uid, shenpi_username: userInfo.name }; } catch (err) { await transaction.rollback(); throw err; } } async removeShenpiUser(id) { return await this.db.update(this.tableName, { id, shenpi_uid: null }); } async start(id) { return await this.db.update(this.tableName, { id, status: constructionConst.status.checking, start_time: new Date() }); } async startMulti(ids, uid) { const list = await this.getAllDataByCondition({ where: { id: ids } }); if (list.length > 0) { const updateList = []; for (const l of list) { if (l.report_uid !== uid) { throw '非填报人无法提交日志'; } if (l.status === constructionConst.status.uncheck) { updateList.push({ id: l.id, status: constructionConst.status.checking, start_time: new Date(), }); } } if (updateList.length > 0) await this.db.updateRows(this.tableName, updateList); } return true; } async checked(id) { return await this.db.update(this.tableName, { id, status: constructionConst.status.checked, checked_time: new Date() }); } async checkNo(id) { return await this.db.update(this.tableName, { id, status: constructionConst.status.uncheck }); } async deleteLog(id) { const transaction = await this.db.beginTransaction(); try { // 先删附件,再删数据记录 const attList = await this.ctx.service.constructionAtt.getAllDataByCondition({ where: { log_id: id } }); await this.ctx.helper.delFiles(attList); await transaction.delete(this.ctx.service.constructionAtt.tableName, { log_id: id }); await transaction.delete(this.tableName, { id }); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } } return ConstructionLog; };