infoController.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const ARCHIVER = require('archiver');
  4. const FS = require('fs');
  5. const path = require('path');
  6. const excelUtils = require('../util/excelUtils');
  7. const md5 = require('md5');
  8. class InfoController extends Controller {
  9. // 单查询
  10. async one() {
  11. const { ctx, service } = this;
  12. const query = ctx.query;
  13. const { id } = query;
  14. const result = await service.infoService.one(id);
  15. ctx.logic(result, '查询失败');
  16. }
  17. // 添加采集记录
  18. async add() {
  19. const { ctx, service } = this;
  20. const query = ctx.request.body;
  21. const user = ctx.user;
  22. query.userid = user._id;
  23. // query.userName = user.loginName;
  24. query.dept1 = user.dept1;
  25. query.dept2 = user.dept2;
  26. query.dept3 = user.dept3;
  27. query.dept4 = user.dept4;
  28. query.dept5 = user.dept5;
  29. if (!query.fid) {
  30. const familyResult = await service.familyService.add(query);
  31. query.fid = familyResult._id;
  32. }
  33. const infoResult = await service.infoService.add(query);
  34. ctx.success(infoResult);
  35. }
  36. // 修改采集记录
  37. async update() {
  38. const { ctx, service } = this;
  39. const query = ctx.request.body;
  40. const { id } = query;
  41. delete query.id;
  42. delete query.fid;
  43. const oneInfo = await this.ctx.service.infoService.one(id);
  44. // status == '2' 审核中 不允许进行修改
  45. // status == '3' 审核成功 状态不变,不允许修改为空
  46. if (oneInfo) {
  47. if (oneInfo.status !== '2') {
  48. if (oneInfo.status == '3') {
  49. const flag = await this.ctx.service.infoService.checkInfoUpdate(query);
  50. if (flag) {
  51. const result = await service.infoService.update(id, query);
  52. ctx.success(result);
  53. } else {
  54. ctx.error('审核成功,不允许修改为空', 333);
  55. }
  56. } else {
  57. const result = await service.infoService.update(id, query);
  58. ctx.success(result);
  59. }
  60. } else {
  61. ctx.error('审核中,不允许进行修改', 222);
  62. }
  63. } else {
  64. ctx.error('请求参数有误', 3333);
  65. }
  66. }
  67. // 删除采集记录,同时删除所在的户
  68. async delete() {
  69. const { ctx, service } = this;
  70. const query = ctx.query;
  71. const { id } = query;
  72. const info = await service.infoService.one(id);
  73. const fid = info.fid;
  74. const result = await service.infoService.dele(id);
  75. const family = await service.infoService.list({ fid });
  76. if (family.length === 0) {
  77. await service.familyService.delete(fid);
  78. }
  79. ctx.success(result);
  80. }
  81. // 不分页查询
  82. async list() {
  83. const { ctx, service } = this;
  84. const query = ctx.query;
  85. const result = await service.infoService.list(query);
  86. ctx.success(result);
  87. }
  88. // 分页查询
  89. async listForPage() {
  90. const { ctx, service } = this;
  91. const user = ctx.user;
  92. const level = user.dept.level;
  93. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  94. if (!ctx.query.dept1) {
  95. delete ctx.query.dept1;
  96. }
  97. if (!ctx.query.dept2) {
  98. delete ctx.query.dept2;
  99. }
  100. if (!ctx.query.dept3) {
  101. delete ctx.query.dept3;
  102. }
  103. if (!ctx.query.dept4) {
  104. delete ctx.query.dept4;
  105. }
  106. if (!ctx.query.dept5) {
  107. delete ctx.query.dept5;
  108. }
  109. delete ctx.query.deptId;
  110. // admin的dept 存在冲突,所以它不需要结合
  111. if (user.role._id != this.app.config.defaultAdminRoleId) {
  112. ctx.query['dept' + level] = user.dept._id;
  113. }
  114. // 判断如果当前是采集员看数据的话 只能看他自己
  115. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  116. ctx.query.cjname = user.loginName;
  117. }
  118. const result = await service.infoService.listForPage(ctx.query);
  119. ctx.success(result);
  120. }
  121. // 根据openId查询采集记录
  122. async listByOpenid() {
  123. const { ctx, service } = this;
  124. const query = ctx.query;
  125. const user = ctx.user;
  126. query.userid = user._id;
  127. const result = await this.ctx.service.infoService.list(query);
  128. ctx.success(result);
  129. }
  130. // 根据openId查询未按要求巡访记录
  131. async noVisitList() {
  132. const { ctx, service } = this;
  133. const query = ctx.query;
  134. const user = ctx.user;
  135. // console.log(ctx.user, 'ctx.user');
  136. query.userid = user._id;
  137. const result = await this.ctx.service.infoService.noVisitList(query);
  138. ctx.success(result);
  139. }
  140. // 根据户Id查询采集记录
  141. async listByFid() {
  142. const { ctx, service } = this;
  143. const query = ctx.query;
  144. const result = await service.infoService.listByFid(query);
  145. ctx.success(result);
  146. }
  147. // 审批流程
  148. async approval() {
  149. const { ctx, service } = this;
  150. const { id } = ctx.query;
  151. delete ctx.query.id;
  152. const result = await service.infoService.approval(id, ctx.query);
  153. // ctx.logic(result, '没有审批权限');
  154. ctx.success(result);
  155. }
  156. // 老人状态
  157. async isDeath() {
  158. const { ctx, service } = this;
  159. const { id } = ctx.query;
  160. delete ctx.query.id;
  161. const result = await service.infoService.isDeath(id, ctx.query);
  162. ctx.success(result);
  163. }
  164. // 二维码压缩包下载
  165. async allImageCount() {
  166. const { ctx, service } = this;
  167. const query = ctx.query;
  168. const user = ctx.user;
  169. delete query.sessionId;
  170. if (user.dept5) {
  171. query.dept5 = user.dept5;
  172. } else if (user.dept4) {
  173. query.dept4 = user.dept4;
  174. }
  175. // 采集已完成并且审核成功才可以下载二维码
  176. query.status = '3';
  177. query.$or = [{ online: { $exists: false } }, { online: 'true' }];
  178. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  179. const result = await ctx.model.SysUserModel.find({ loginName: { $regex: user.loginName } });
  180. if (result.length > 0) {
  181. query.userid = result[0]._id;
  182. }
  183. }
  184. const result = await service.infoService.list(query);
  185. ctx.success(result.length);
  186. }
  187. // 二维码压缩包下载
  188. async allImage() {
  189. // console.log('allImage');
  190. const { ctx, service } = this;
  191. const query = ctx.query;
  192. const user = ctx.user;
  193. delete query.sessionId;
  194. if (user.dept5) {
  195. query.dept5 = user.dept5;
  196. } else if (user.dept4) {
  197. query.dept4 = user.dept4;
  198. }
  199. // 采集已完成并且审核成功才可以下载二维码
  200. query.status = '3';
  201. query.$or = [{ online: { $exists: false } }, { online: 'true' }];
  202. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  203. const result = await ctx.model.SysUserModel.find({ loginName: { $regex: user.loginName } });
  204. if (result.length > 0) {
  205. query.userid = result[0]._id;
  206. }
  207. }
  208. let result;
  209. let pName = '';
  210. if (query.page) {
  211. pName = '_zip'+ query.page;
  212. const page = 500 * (query.page - 1);
  213. delete query.page;
  214. result = await ctx.model.InfoModel.find(query).limit(500).skip(page);
  215. } else {
  216. result = await service.infoService.list(query);
  217. }
  218. ctx.logger.info(query);
  219. // archiver可压缩为zip或tar格式,这里选择zip格式,注意这里新定义了一个变量archive,而不是原有的archiver包引用
  220. const archive = ARCHIVER('zip', {
  221. store: true,
  222. });
  223. archive.on('warning', function(err) {
  224. if (err.code === 'ENOENT') {
  225. ctx.logger.info(err.message);
  226. ctx.logger.info('stat故障和其他非阻塞错误');
  227. } else {
  228. ctx.logger.info(err.message);
  229. throw err;
  230. }
  231. });
  232. archive.on('error', function(err) {
  233. ctx.logger.info(err.message);
  234. throw err;
  235. });
  236. // 生成压缩包文件
  237. const zipName = Date.now() + pName + '.zip';
  238. const zipPath = this.app.config.defaultUploadPath + zipName;
  239. const output = FS.createWriteStream(zipPath);
  240. // 文件输出流结束
  241. output.on('close', function() {
  242. ctx.logger.info(`总共 ${archive.pointer()} 字节`);
  243. ctx.logger.info('archiver完成文件的归档,文件输出流描述符已关闭');
  244. });
  245. // 数据源是否耗尽
  246. output.on('end', function() {
  247. ctx.logger.info('数据源已耗尽');
  248. });
  249. // 将压缩路径、包名与压缩格式连接
  250. archive.pipe(output);
  251. // if (result.length <= 1000) {
  252. // for (let index = 0; index < result.length; index++) {
  253. // const element = result[index];
  254. // const picName = path.basename('' + element.pic);
  255. // // FS读取文件流,将读取的文件流append到压缩包中
  256. // archive.append(FS.createReadStream(this.app.config.defaultUploadPath + picName), { name: 'scan' + picName });
  257. // }
  258. // }
  259. // TODO 修改成OSS -CH (已完成)
  260. if (result.length <= 1000) {
  261. for (const item of result) {
  262. const picName = path.basename('' + item.pic);
  263. try {
  264. const OSSResult = await ctx.oss.get('bucket1').getStream(item.pic);
  265. archive.append(OSSResult.stream, { name: 'scan' + picName });
  266. } catch (e) {
  267. ctx.logger.info('有图片存在问题', e);
  268. }
  269. }
  270. }
  271. // 压缩结束
  272. await archive.finalize();
  273. // 下载文件
  274. ctx.set({
  275. 'Content-type': 'application/octet-stream',
  276. 'Content-Disposition': 'attachment;filename=' + encodeURI(zipName), // 定义文件名
  277. });
  278. ctx.body = FS.createReadStream(zipPath);
  279. }
  280. // 根据身份证号码查询采集记录
  281. async listByNumber() {
  282. const { ctx, service } = this;
  283. const query = ctx.query;
  284. const { number } = query;
  285. const result = await service.infoService.listByNumber({ idNumber: number });
  286. if (result.length > 0) {
  287. ctx.error('身份证号码已存在');
  288. } else {
  289. ctx.success();
  290. }
  291. }
  292. // 修改离线状态
  293. // async updateOffLine() {
  294. // const { ctx, service } = this;
  295. // const query = ctx.query;
  296. // const { id, offLine } = query;
  297. // delete query.id;
  298. // if (id) {
  299. // const result = await service.infoService.updateOffLine(id, offLine);
  300. // ctx.success(result);
  301. // }
  302. // }
  303. // * 筛选----身份证重复录入
  304. async groupByNumber() {
  305. const { ctx, service } = this;
  306. const user = ctx.user;
  307. const level = user.dept.level;
  308. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  309. if (!ctx.query.dept1) {
  310. delete ctx.query.dept1;
  311. }
  312. if (!ctx.query.dept2) {
  313. delete ctx.query.dept2;
  314. }
  315. if (!ctx.query.dept3) {
  316. delete ctx.query.dept3;
  317. }
  318. if (!ctx.query.dept4) {
  319. delete ctx.query.dept4;
  320. }
  321. if (!ctx.query.dept5) {
  322. delete ctx.query.dept5;
  323. }
  324. delete ctx.query.deptId;
  325. // admin的dept 存在冲突,所以它不需要结合
  326. if (user.role._id != this.app.config.defaultAdminRoleId) {
  327. ctx.query['dept' + level] = user.dept._id;
  328. }
  329. const result = await service.infoService.groupByNumber(ctx.query);
  330. ctx.success(result);
  331. }
  332. // 导出Excel
  333. async exportExcelByInfo() {
  334. const { ctx, service } = this;
  335. const query = ctx.query;
  336. const user = ctx.user;
  337. const level = user.dept.level;
  338. delete query.sessionId;
  339. // // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  340. // if (!ctx.query.dept1) {
  341. // delete ctx.query.dept1;
  342. // }
  343. // if (!ctx.query.dept2) {
  344. // delete ctx.query.dept2;
  345. // }
  346. // if (!ctx.query.dept3) {
  347. // delete ctx.query.dept3;
  348. // }
  349. // if (!ctx.query.dept4) {
  350. // delete ctx.query.dept4;
  351. // }
  352. // if (!ctx.query.dept5) {
  353. // delete ctx.query.dept5;
  354. // }
  355. // delete ctx.query.deptId;
  356. // 乡镇村级以上管理员
  357. if (level == '1' || level == '2') {
  358. ctx.logger.info('区及以上');
  359. if (!ctx.query.dept1 || !ctx.query.dept2 || !ctx.query.dept3) {
  360. delete ctx.query.dept1;
  361. delete ctx.query.dept2;
  362. delete ctx.query.dept3;
  363. this.ctx.error('请选择要导出的市、区乡镇村级地区');
  364. return;
  365. }
  366. } else {
  367. ctx.logger.info('区以下管理员');
  368. // 登录人的地区
  369. if (user.dept5) {
  370. query.dept5 = user.dept5;
  371. } else if (user.dept4) {
  372. query.dept4 = user.dept4;
  373. } else if (user.dept3) {
  374. query.dept3 = user.dept3;
  375. }
  376. }
  377. const begintime = new Date().getTime();
  378. ctx.logger.info('开始时间' + begintime);
  379. const result = await service.infoService.exportData(ctx.query);
  380. const endtime = new Date().getTime();
  381. ctx.logger.info('结束时间' + endtime);
  382. // if (result.length == 0) {
  383. // this.ctx.error('暂无数据', 200);
  384. // return;
  385. // }
  386. // if (result.length > 6 * 1000) {
  387. // this.ctx.error('数据量过大,请联系管理员导出', 500);
  388. // return;
  389. // }
  390. const config = [{
  391. sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
  392. sheetHeader: [
  393. {
  394. headerName:
  395. '吉林省民政厅居家老年人巡视关爱探访系统采集信息',
  396. headerConfig: { height: 40 },
  397. },
  398. ],
  399. sheetKey: [
  400. { label: '序号', key: 'num', letter: 'A', width: 6 },
  401. { label: '姓名', key: 'name', letter: 'B', width: 10 },
  402. { label: '性别', key: 'sex', letter: 'C', width: 10 },
  403. { label: '民族', key: 'nation', letter: 'D', width: 10 },
  404. { label: '身份证号', key: 'idNumber', letter: 'E', width: 20 },
  405. { label: '户籍地址', key: 'nativePlace', letter: 'F', width: 40 },
  406. { label: '现居地址', key: 'address', letter: 'G', width: 40 },
  407. // { label: '年龄', key: 'birthday', letter: 'H', width: 20 },
  408. ],
  409. sheetData: result,
  410. }];
  411. const workbook = excelUtils.getExcel(config);
  412. if (!workbook) {
  413. this.ctx.error();
  414. return;
  415. }
  416. this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
  417. this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(new Date().getTime()) + '.xlsx');
  418. this.ctx.body = await workbook.xlsx.writeBuffer();
  419. // const finalfilename = encodeURIComponent(new Date().getTime());
  420. // const path = this.app.config.defaultUploadPath + finalfilename + '.xlsx';
  421. // await workbook.xlsx.writeFile(path);
  422. // this.ctx.body = this.app.config.defaultUrl+this.app.config.defaultWritePathPre + finalfilename + '.xlsx'+new Date().getTime()+"生成文件时间"+
  423. // ";开始时间="+begintime+";结束时间="+endtime;
  424. }
  425. async exportExcelForPage() {
  426. const { ctx, service } = this;
  427. const query = ctx.query;
  428. const user = ctx.user;
  429. const level = user.dept.level;
  430. delete query.sessionId;
  431. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  432. if (!ctx.query.dept1) {
  433. delete ctx.query.dept1;
  434. }
  435. if (!ctx.query.dept2) {
  436. delete ctx.query.dept2;
  437. }
  438. if (!ctx.query.dept3) {
  439. delete ctx.query.dept3;
  440. }
  441. if (!ctx.query.dept4) {
  442. delete ctx.query.dept4;
  443. }
  444. if (!ctx.query.dept5) {
  445. delete ctx.query.dept5;
  446. }
  447. delete ctx.query.deptId;
  448. // admin的dept 存在冲突,所以它不需要结合
  449. if (user.role._id != this.app.config.defaultAdminRoleId) {
  450. ctx.query['dept' + level] = user.dept._id;
  451. }
  452. // 判断如果当前是采集员看数据的话 只能看他自己
  453. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  454. ctx.query.cjname = user.loginName;
  455. }
  456. // 登录人的地区
  457. // if (user.dept5) {
  458. // query.dept5 = user.dept5;
  459. // } else if (user.dept4) {
  460. // query.dept4 = user.dept4;
  461. // } else if (user.dept3) {
  462. // query.dept3 = user.dept3;
  463. // }
  464. const result = await service.infoService.exportData(ctx.query);
  465. if (result.length > 4 * 10000) {
  466. this.ctx.error('数据量过大,请联系管理员导出', 500);
  467. return;
  468. }
  469. // const config = [{
  470. // sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
  471. // sheetHeader: [
  472. // {
  473. // headerName:
  474. // '吉林省民政厅居家老年人巡视关爱探访系统',
  475. // headerConfig: { height: 40 },
  476. // },
  477. // ],
  478. // sheetKey: [
  479. // { label: '序号', key: 'num', letter: 'A', width: 6 },
  480. // { label: '姓名', key: 'name', letter: 'B', width: 10 },
  481. // { label: '性别', key: 'sex', letter: 'C', width: 20 },
  482. // { label: '民族', key: 'nation', letter: 'D', width: 20 },
  483. // { label: '身份证号', key: 'idNumber', letter: 'E', width: 20 },
  484. // { label: '户籍地址', key: 'nativePlace', letter: 'F', width: 30 },
  485. // { label: '现居地址', key: 'address', letter: 'G', width: 30 },
  486. // // { label: '年龄', key: 'birthday', letter: 'H', width: 20 },
  487. // ],
  488. // sheetData: result,
  489. // }];
  490. // const workbook = excelUtils.getExcel(config);
  491. // if (!workbook) {
  492. // this.ctx.error();
  493. // return;
  494. // }
  495. // this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
  496. // this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(new Date().getTime()) + '.xlsx');
  497. // this.ctx.body = await workbook.xlsx.writeBuffer();
  498. const archive = ARCHIVER('zip', {
  499. store: true,
  500. });
  501. archive.on('warning', function(err) {
  502. if (err.code === 'ENOENT') {
  503. ctx.logger.info(err.message);
  504. ctx.logger.info('stat故障和其他非阻塞错误');
  505. } else {
  506. ctx.logger.info(err.message);
  507. throw err;
  508. }
  509. });
  510. archive.on('error', function(err) {
  511. ctx.logger.info(err.message);
  512. throw err;
  513. });
  514. // 生成压缩包文件
  515. const zipName = Date.now() + '.zip';
  516. // const zipPath = this.app.config.defaultUploadPath + zipName;
  517. const zipPath = zipName;
  518. const output = FS.createWriteStream(zipPath);
  519. // 文件输出流结束
  520. output.on('close', function() {
  521. ctx.logger.info(`总共 ${archive.pointer()} 字节`);
  522. ctx.logger.info('archiver完成文件的归档,文件输出流描述符已关闭');
  523. });
  524. // 数据源是否耗尽
  525. output.on('end', function() {
  526. ctx.logger.info('数据源已耗尽');
  527. });
  528. // 将压缩路径、包名与压缩格式连接
  529. archive.pipe(output);
  530. if (result.length > 1000) {
  531. const n = result.length / 1000 + 1;
  532. for (let i = 1; i < n; i++) {
  533. // const result = await model.InfoModel.find(where).populate(pop).skip((page - 1) * rows)
  534. // .limit(rows)
  535. // .sort({ time: -1 });
  536. ctx.query.page = i;
  537. ctx.query.row = 1000;
  538. const resultForPage = await service.infoService.listForPage(ctx.query);
  539. // const config = [{
  540. // sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
  541. // sheetHeader: [
  542. // {
  543. // headerName:
  544. // '吉林省民政厅居家老年人巡视关爱探访系统',
  545. // headerConfig: { height: 40 },
  546. // },
  547. // ],
  548. // sheetKey: [
  549. // { label: '序号', key: 'num', letter: 'A', width: 6 },
  550. // { label: '姓名', key: 'name', letter: 'B', width: 10 },
  551. // { label: '性别', key: 'sex', letter: 'C', width: 20 },
  552. // { label: '民族', key: 'nation', letter: 'D', width: 20 },
  553. // { label: '身份证号', key: 'idNumber', letter: 'E', width: 20 },
  554. // { label: '户籍地址', key: 'nativePlace', letter: 'F', width: 30 },
  555. // { label: '现居地址', key: 'address', letter: 'G', width: 30 },
  556. // // { label: '年龄', key: 'birthday', letter: 'H', width: 20 },
  557. // ],
  558. // sheetData: result,
  559. // }];
  560. // const workbook = excelUtils.getExcel(config);
  561. // if (!workbook) {
  562. // this.ctx.error();
  563. // return;
  564. // }
  565. // archive.append('', { name: i });
  566. }
  567. // for (let index = 0; index < result.length; index++) {
  568. // const element = result[index];
  569. // const picName = path.basename('' + element.pic);
  570. // // FS读取文件流,将读取的文件流append到压缩包中
  571. // archive.append(FS.createReadStream(this.app.config.defaultUploadPath + picName), { name: 'scan' + picName });
  572. // }
  573. }
  574. // 压缩结束
  575. await archive.finalize();
  576. // 下载文件
  577. ctx.set({
  578. 'Content-type': 'application/octet-stream',
  579. 'Content-Disposition': 'attachment;filename=' + encodeURI(zipName), // 定义文件名
  580. });
  581. ctx.body = FS.createReadStream(zipPath);
  582. }
  583. // 根据户ID查询老人
  584. async visitByFid() {
  585. const { ctx, service } = this;
  586. const query = ctx.query;
  587. const { fid } = query;
  588. // delete query.fid;
  589. if (fid) {
  590. const result = await service.infoService.listByFid(query);
  591. // ctx.logger.error('visitIsExist', result);
  592. ctx.logic(result, ' ');
  593. } else {
  594. ctx.error('fid不存在');
  595. }
  596. }
  597. // 首页数据昨天相关采集数据
  598. async yestdayNum() {
  599. const { ctx, service } = this;
  600. const query = ctx.query;
  601. const user = ctx.user;
  602. // const level = user.dept.level;
  603. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  604. if (!ctx.query.dept1) {
  605. delete ctx.query.dept1;
  606. }
  607. if (!ctx.query.dept2) {
  608. delete ctx.query.dept2;
  609. }
  610. if (!ctx.query.dept3) {
  611. delete ctx.query.dept3;
  612. }
  613. if (!ctx.query.dept4) {
  614. delete ctx.query.dept4;
  615. }
  616. if (!ctx.query.dept5) {
  617. delete ctx.query.dept5;
  618. }
  619. delete ctx.query.deptId;
  620. // // admin的dept 存在冲突,所以它不需要结合
  621. // if (user.role._id != this.app.config.defaultAdminRoleId) {
  622. // ctx.query['dept' + level] = user.dept._id;
  623. // }
  624. const yestday = new Date();
  625. yestday.setTime(yestday.getTime() - 24 * 60 * 60 * 1000);
  626. let month = yestday.getMonth() + 1;
  627. if (month < 10) month = '0' + month;
  628. let day = yestday.getDate();
  629. if (day < 10) day = '0' + day;
  630. const ysday = yestday.getFullYear() + '-' + month + '-' + day;
  631. ctx.query.yestday = ysday;
  632. const result = await service.infoService.yestdayNum(query, user);
  633. ctx.success(result);
  634. }
  635. async getALiOssToken() {
  636. const { ctx, app } = this;
  637. const query = ctx.query;
  638. let tokenExpireTimeDefault = app.config.tokenExpireTime;
  639. if (query.tokenExpireTime) {
  640. tokenExpireTimeDefault = query.tokenExpireTime;
  641. }
  642. const roleArn = app.config.roleArn;
  643. const sessionName = app.config.sessionName;
  644. // const policy = {
  645. // Statement: [{
  646. // Action: [
  647. // 'oss:*',
  648. // ],
  649. // Effect: 'Allow',
  650. // Resource: [ 'acs:oss:*:*:*' ],
  651. // }],
  652. // Version: '1',
  653. // };
  654. try {
  655. const token = await app.oss.get('bucket2').assumeRole(roleArn, null, Number(tokenExpireTimeDefault), sessionName);
  656. ctx.success(token.credentials);
  657. } catch (error) {
  658. ctx.logic(error, 'STS 获取token失败');
  659. }
  660. }
  661. async createOldInfoOwnerUser() {
  662. const { ctx } = this;
  663. // 递归查询五级地区
  664. const result = await ctx.model.SysDeptModel.aggregate([
  665. {
  666. $match: {
  667. level: '5',
  668. },
  669. },
  670. {
  671. $project: {
  672. dept4: { $convert: { input: '$fid', to: 'objectId' } },
  673. name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: '$_id',
  674. },
  675. },
  676. {
  677. $lookup: {
  678. from: 'sys_dept',
  679. localField: 'dept4',
  680. foreignField: '_id',
  681. as: 'deptInfos',
  682. },
  683. },
  684. { $unwind: { path: '$deptInfos', preserveNullAndEmptyArrays: true } },
  685. {
  686. $project: {
  687. name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: 1, dept4: 1,
  688. dept3: { $convert: { input: '$deptInfos.fid', to: 'objectId' } },
  689. },
  690. },
  691. {
  692. $lookup: {
  693. from: 'sys_dept',
  694. localField: 'dept3',
  695. foreignField: '_id',
  696. as: 'deptInfos',
  697. },
  698. },
  699. { $unwind: { path: '$deptInfos', preserveNullAndEmptyArrays: true } },
  700. {
  701. $project: {
  702. name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: 1, dept4: 1, dept3: 1,
  703. dept2: { $convert: { input: '$deptInfos.fid', to: 'objectId' } },
  704. },
  705. },
  706. {
  707. $lookup: {
  708. from: 'sys_dept',
  709. localField: 'dept2',
  710. foreignField: '_id',
  711. as: 'deptInfos',
  712. },
  713. },
  714. { $unwind: { path: '$deptInfos', preserveNullAndEmptyArrays: true } },
  715. {
  716. $project: {
  717. name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: 1, dept4: 1, dept3: 1, dept2: 1,
  718. dept1: { $convert: { input: '$deptInfos.fid', to: 'objectId' } },
  719. },
  720. },
  721. ]);
  722. await Promise.all(result.map(async item => {
  723. const data = {};
  724. data.dept1 = item.dept1;
  725. data.dept2 = item.dept2;
  726. data.dept3 = item.dept3;
  727. data.dept4 = item.dept4;
  728. data.dept5 = item.dept5;
  729. data.dept = item.dept5;
  730. data.role = this.app.config.defaultOwnerManagerRoleId;
  731. data.loginName = item.code + '000';
  732. data.loginPwd = md5(this.app.config.defaultPassword);
  733. await ctx.model.SysUserModel.create(data);
  734. }));
  735. console.log('length', result.length);
  736. }
  737. // 自主上报添加记录
  738. async ownerAdd() {
  739. const { ctx, service } = this;
  740. const query = ctx.request.body;
  741. if (query.dept5 && query.name) {
  742. const result = await ctx.model.SysDeptModel.find({ _id: this.app.mongoose.Types.ObjectId(query.dept5) });
  743. if (result.length > 0) {
  744. const result = await ctx.model.SysUserModel.find({ dept: this.app.mongoose.Types.ObjectId(query.dept5), role: this.app.mongoose.Types.ObjectId(this.app.config.defaultOwnerManagerRoleId) });
  745. if (result.length > 0) {
  746. query.userid = result[0]._id;
  747. if (!query.fid) {
  748. const familyResult = await service.familyService.add(query);
  749. query.fid = familyResult._id;
  750. }
  751. query.ownerId = ctx.get('openId');
  752. const infoResult = await service.infoService.add(query);
  753. return ctx.success(infoResult);
  754. }
  755. return ctx.error('该地区没有自主上报员!');
  756. }
  757. return ctx.error('地区ObjectId错误,上报失败!');
  758. }
  759. return ctx.error('请将信息全部填写完毕方可上报!');
  760. }
  761. // 自主申报修改采集记录
  762. async ownerUpdate() {
  763. const { ctx, service } = this;
  764. const query = ctx.request.body;
  765. const { id } = query;
  766. delete query.id;
  767. delete query.fid;
  768. const oneInfo = await this.ctx.service.infoService.one(id);
  769. // status == '2' 审核中 不允许进行修改
  770. // status == '3' 审核成功 状态不变,不允许修改为空
  771. if (oneInfo) {
  772. if (oneInfo.status !== '2') {
  773. if (oneInfo.status == '3') {
  774. const flag = await this.ctx.service.infoService.checkInfoUpdate(query);
  775. if (flag) {
  776. const result = await service.infoService.update(id, query);
  777. ctx.success(result);
  778. } else {
  779. ctx.error('审核成功,不允许修改为空', 333);
  780. }
  781. } else {
  782. const result = await service.infoService.update(id, query);
  783. ctx.success(result);
  784. }
  785. } else {
  786. ctx.error('审核中,不允许进行修改', 222);
  787. }
  788. } else {
  789. ctx.error('请求参数有误', 3333);
  790. }
  791. }
  792. // 以ownerId查询 自主填报信息 ( 以手机的openId为准 )
  793. async findOwnerInfo() {
  794. const { ctx } = this;
  795. const user = ctx.user;
  796. const query = {};
  797. query.ownerId = user.openId;
  798. if (ctx.query.status) {
  799. query.status = ctx.query.status;
  800. }
  801. const result = await ctx.model.InfoModel.find(query).sort({ time: -1 });
  802. ctx.logic(result);
  803. }
  804. // 自主填报查询吉林省统计
  805. async ownerStatistics() {
  806. const { ctx } = this;
  807. const { model } = ctx;
  808. const result = {};
  809. const where = {};
  810. result.dept = '吉林省';
  811. where.dept1 = '5d4289205ffc6694f7e42082';
  812. const visitCount = await model.VisitModel.find(where).countDocuments();
  813. const infoTotal = await model.InfoModel.find(where).countDocuments();
  814. where.role = this.app.config.defaultUserRoleId;
  815. const userTotal = await model.SysUserModel.find(where).countDocuments();
  816. result.visitCount = visitCount;
  817. result.infoTotal = infoTotal;
  818. result.userTotal = userTotal;
  819. ctx.success(result);
  820. }
  821. async createYlxtSelectUser() {
  822. const { ctx } = this;
  823. // 查询前三级地区-CH-2022-11-15 15:51:05
  824. const result = await ctx.model.SysDeptModel.aggregate([
  825. { $match: { level: { $lte: '3' }, code: /^22/ } },
  826. ]);
  827. const jlResult = await ctx.model.SysDeptModel.find({name: '吉林省', level: '1' });
  828. await Promise.all(result.map(async item => {
  829. const data = {};
  830. if(item.level == '1'){
  831. data.dept1 = item._id;
  832. data.dept = item._id;
  833. }else if (item.level == '2'){
  834. data.dept1 = this.app.mongoose.Types.ObjectId(item.fid);
  835. data.dept2 = item._id;
  836. data.dept = item._id;
  837. }else{
  838. data.dept1 = jlResult[0]._id;
  839. data.dept2 = this.app.mongoose.Types.ObjectId(item.fid);
  840. data.dept3 = item._id;
  841. data.dept = item._id;
  842. }
  843. data.role = this.app.config.defaultYLXTSelectRoleId;
  844. data.loginName = 'ylxt_' + item.code;
  845. data.loginPwd = md5(this.app.config.defaultYlxtSelectPassword);
  846. // console.log('level ', item.level);
  847. // console.log('dept1 ', data.dept1);
  848. // console.log('dept2 ', data.dept2);
  849. // console.log('dept3 ', data.dept3);
  850. // console.log('dept ', data.dept);
  851. // console.log('loginName ', data.loginName);
  852. await ctx.model.SysUserModel.create(data);
  853. }));
  854. console.log('length', result.length);
  855. }
  856. }
  857. module.exports = InfoController;