import { Instance } from "noomi"; import { EntityManager, getEntityManager } from "relaen"; import { buildStateDataReport, saveStateDataReportDocx } from "../../util/__statreport"; import tables from "../../data/stateconfig"; import * as fsMdl from 'fs'; import * as pathMdl from 'path'; /** * report service */ @Instance() export default class AMReportService{ async genReport(){ const data = await this.readData(); const report = buildStateDataReport(data as any); const path = pathMdl.resolve(process.cwd(),'download'); if(!fsMdl.existsSync(path)){ fsMdl.mkdirSync(path,{recursive:true}); } const path1 = pathMdl.resolve(path,'统计报表.docx'); saveStateDataReportDocx(report,path1); return path1; } async readData(){ const em:EntityManager = await getEntityManager() const result = [] for(let config of tables){ const rows = await config.entity.findMany(); for(const row of rows){ const stat = [] for(const tbl of config.children){ const sql = `select count(*) from ${tbl.entity.name} where ${config.entity.name}=?`; const query = em.createNativeQuery(sql); query.setParameter(0,row['stateId']); try{ const r = await query.getResult() as number; if(r > 0){ stat.push({label:tbl.label,count:r}); } }catch(e){ console.error(e); } } if(stat.length>0){ const arr = [] for(let f of config.fields){ arr.push({ name:f.name, label:f.label, value:row[f.name] }) } result.push({ tag:config.entity.name, state:arr, data:stat }) } } } await em.close(); return result; } }