util.service.ts 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. import { Inject, Provide, Config } from '@midwayjs/core';
  2. import { get, uniq } from 'lodash';
  3. import { Context } from '@midwayjs/koa';
  4. import { InjectEntityModel } from '@midwayjs/typeorm';
  5. import { ServiceError, ErrorCode } from '../error/service.error';
  6. import dayjs = require('dayjs');
  7. import { DictDataService } from './system/dictData.service';
  8. import { Equal, Repository, IsNull } from 'typeorm';
  9. import { Achievement } from '../entity/platform/achievement.entity';
  10. import { Company } from '../entity/users/company.entity';
  11. import { Supply } from '../entity/platform/supply.entity';
  12. import { Project } from '../entity/platform/project.entity';
  13. import { Demand } from '../entity/platform/demand.entity';
  14. import { UserService } from './system/user.service';
  15. import * as mappings from '../public/importMapping';
  16. import * as Excel from 'exceljs';
  17. import * as Path from 'path';
  18. import * as fs from 'fs';
  19. import { User } from '../entity/system/user.entity';
  20. import { CompanyYear } from '../entity/users/companyYear.entity';
  21. import { Expert } from '../entity/users/expert.entity';
  22. import { RoleService } from './system/role.service';
  23. import { Incubator } from '../entity/users/incubator.entity';
  24. import { SectorService } from './platform/sector.service';
  25. /**
  26. * 工具类服务,为其他地方提供一些通用的函数
  27. */
  28. @Provide()
  29. export class UtilService {
  30. @Inject()
  31. ctx: Context;
  32. @Inject()
  33. userService: UserService;
  34. @InjectEntityModel(User)
  35. userModel: Repository<User>;
  36. @InjectEntityModel(Achievement)
  37. achievementModel: Repository<Achievement>;
  38. @InjectEntityModel(Project)
  39. projectModel: Repository<Project>;
  40. @InjectEntityModel(Demand)
  41. demandModel: Repository<Demand>;
  42. @InjectEntityModel(Supply)
  43. supplyModel: Repository<Supply>;
  44. @InjectEntityModel(Company)
  45. companyModel: Repository<Company>;
  46. @InjectEntityModel(Expert)
  47. expertModel: Repository<Expert>;
  48. @InjectEntityModel(Incubator)
  49. incubatorModel: Repository<Incubator>;
  50. @InjectEntityModel(CompanyYear)
  51. yModel: Repository<CompanyYear>;
  52. @Inject()
  53. dictDataService: DictDataService;
  54. @Inject()
  55. sectorService: SectorService;
  56. @Inject()
  57. roleService: RoleService;
  58. @Config('PathConfig.path')
  59. path;
  60. // 管理端数据看板
  61. async oneStatistics(type) {
  62. let list = [];
  63. // 企业统计数量
  64. if (type === 'company') {
  65. list = [
  66. { name: '企业总数', data: [], percentage: 1 },
  67. { name: '高新技术企业', data: [], percentage: 1 },
  68. { name: '专精特新企业', data: [], percentage: 1 },
  69. { name: '审核成功', data: [], percentage: 1 },
  70. ];
  71. for (const [index, val] of list.entries()) {
  72. let count = 0;
  73. if (index === 0) {
  74. count = await this.companyModel.count();
  75. val.data.push(count);
  76. } else if (index === 1) {
  77. count = await this.companyModel.count({ where: { is_tech: '0' } });
  78. val.data.push(count);
  79. val.percentage = await this.calculatePercentage(count, await this.companyModel.count());
  80. } else if (index === 2) {
  81. count = await this.companyModel.count({ where: { is_new: '0' } });
  82. val.data.push(count);
  83. val.percentage = await this.calculatePercentage(count, await this.companyModel.count());
  84. } else {
  85. count = await this.companyModel.count({ where: { status: '1' } });
  86. val.data.push(count);
  87. val.percentage = await this.calculatePercentage(count, await this.companyModel.count());
  88. }
  89. }
  90. }
  91. // 专家统计数量
  92. if (type === 'expert') {
  93. list = [
  94. { name: '专家总数', data: [], percentage: 1 },
  95. { name: '高校、科研院所类', data: [], percentage: 1 },
  96. { name: '企业类', data: [], percentage: 1 },
  97. { name: '审核成功', data: [], percentage: 1 },
  98. ];
  99. for (const [index, val] of list.entries()) {
  100. let count = 0;
  101. if (index === 0) {
  102. count = await this.expertModel.count();
  103. val.data.push(count);
  104. } else if (index === 1) {
  105. count = await this.expertModel.count({ where: { work_type: '高校、科研院所' } });
  106. val.data.push(count);
  107. val.percentage = await this.calculatePercentage(count, await this.expertModel.count());
  108. } else if (index === 2) {
  109. count = await this.expertModel.count({ where: { work_type: '企业' } });
  110. val.data.push(count);
  111. val.percentage = await this.calculatePercentage(count, await this.expertModel.count());
  112. } else {
  113. count = await this.expertModel.count({ where: { status: '1' } });
  114. val.data.push(count);
  115. val.percentage = await this.calculatePercentage(count, await this.expertModel.count());
  116. }
  117. }
  118. }
  119. // 需求统计数量
  120. if (type === 'demand') {
  121. list = [
  122. { name: '需求总数', data: [], percentage: 1 },
  123. { name: '没有联系方式', data: [], percentage: 1 },
  124. { name: '是否公开', data: [], percentage: 1 },
  125. { name: '审核成功', data: [], percentage: 1 },
  126. ];
  127. for (const [index, val] of list.entries()) {
  128. let count = 0;
  129. if (index === 0) {
  130. count = await this.demandModel.count();
  131. val.data.push(count);
  132. } else if (index === 1) {
  133. count = await this.demandModel.count({ where: { tel: IsNull() } });
  134. val.data.push(count);
  135. val.percentage = await this.calculatePercentage(count, await this.demandModel.count());
  136. } else if (index === 2) {
  137. count = await this.demandModel.count({ where: { is_use: '0' } });
  138. val.data.push(count);
  139. val.percentage = await this.calculatePercentage(count, await this.demandModel.count());
  140. } else {
  141. count = await this.demandModel.count({ where: { status: '1' } });
  142. val.data.push(count);
  143. val.percentage = await this.calculatePercentage(count, await this.demandModel.count());
  144. }
  145. }
  146. }
  147. // 供给统计数量
  148. if (type === 'supply') {
  149. list = [
  150. { name: '供给总数', data: [], percentage: 1 },
  151. { name: '没有价格', data: [], percentage: 1 },
  152. { name: '是否公开', data: [], percentage: 1 },
  153. { name: '审核成功', data: [], percentage: 1 },
  154. ];
  155. for (const [index, val] of list.entries()) {
  156. let count = 0;
  157. if (index === 0) {
  158. count = await this.supplyModel.count();
  159. val.data.push(count);
  160. } else if (index === 1) {
  161. count = await this.supplyModel.count({ where: { money: IsNull() } });
  162. val.data.push(count);
  163. val.percentage = await this.calculatePercentage(count, await this.supplyModel.count());
  164. } else if (index === 2) {
  165. count = await this.supplyModel.count({ where: { is_use: '0' } });
  166. val.data.push(count);
  167. val.percentage = await this.calculatePercentage(count, await this.supplyModel.count());
  168. } else {
  169. count = await this.supplyModel.count({ where: { status: '1' } });
  170. val.data.push(count);
  171. val.percentage = await this.calculatePercentage(count, await this.supplyModel.count());
  172. }
  173. }
  174. }
  175. // 成果统计数量
  176. if (type === 'achievement') {
  177. list = [
  178. { name: '成果总数', data: [], percentage: 1 },
  179. { name: '没有联系方式', data: [], percentage: 1 },
  180. { name: '是否公开', data: [], percentage: 1 },
  181. { name: '审核成功', data: [], percentage: 1 },
  182. ];
  183. for (const [index, val] of list.entries()) {
  184. let count = 0;
  185. if (index === 0) {
  186. count = await this.achievementModel.count();
  187. val.data.push(count);
  188. } else if (index === 1) {
  189. count = await this.achievementModel.count({ where: { tel: IsNull() } });
  190. val.data.push(count);
  191. val.percentage = await this.calculatePercentage(count, await this.achievementModel.count());
  192. } else if (index === 2) {
  193. count = await this.achievementModel.count({ where: { is_use: '0' } });
  194. val.data.push(count);
  195. val.percentage = await this.calculatePercentage(count, await this.achievementModel.count());
  196. } else {
  197. count = await this.achievementModel.count({ where: { status: '1' } });
  198. val.data.push(count);
  199. val.percentage = await this.calculatePercentage(count, await this.achievementModel.count());
  200. }
  201. }
  202. }
  203. // 项目统计数量
  204. if (type === 'project') {
  205. list = [
  206. { name: '项目总数', data: [], percentage: 1 },
  207. { name: '工研院跟踪单位', data: [], percentage: 1 },
  208. { name: '是否公开', data: [], percentage: 1 },
  209. { name: '审核成功', data: [], percentage: 1 },
  210. ];
  211. for (const [index, val] of list.entries()) {
  212. let count = 0;
  213. if (index === 0) {
  214. count = await this.projectModel.count();
  215. val.data.push(count);
  216. } else if (index === 1) {
  217. count = await this.projectModel.count({ where: { track_unit: '工研院' } });
  218. val.data.push(count);
  219. val.percentage = await this.calculatePercentage(count, await this.projectModel.count());
  220. } else if (index === 2) {
  221. count = await this.projectModel.count({ where: { is_use: '0' } });
  222. val.data.push(count);
  223. val.percentage = await this.calculatePercentage(count, await this.projectModel.count());
  224. } else {
  225. count = await this.projectModel.count({ where: { status: '1' } });
  226. val.data.push(count);
  227. val.percentage = await this.calculatePercentage(count, await this.projectModel.count());
  228. }
  229. }
  230. }
  231. //孵化器统计数量
  232. if (type === 'incubate') {
  233. list = [
  234. { name: '孵化基地总数', data: [], percentage: 1 },
  235. { name: '是否具备中试场地', data: [], percentage: 1 },
  236. { name: '是否和平台合作标识', data: [], percentage: 1 },
  237. { name: '审核成功', data: [], percentage: 1 },
  238. ];
  239. for (const [index, val] of list.entries()) {
  240. let count = 0;
  241. if (index === 0) {
  242. count = await this.incubatorModel.count();
  243. val.data.push(count);
  244. } else if (index === 1) {
  245. count = await this.incubatorModel.count({ where: { is_have: '0' } });
  246. val.data.push(count);
  247. val.percentage = await this.calculatePercentage(count, await this.incubatorModel.count());
  248. } else if (index === 2) {
  249. count = await this.incubatorModel.count({ where: { cooperate: '0' } });
  250. val.data.push(count);
  251. val.percentage = await this.calculatePercentage(count, await this.incubatorModel.count());
  252. } else {
  253. count = await this.incubatorModel.count({ where: { status: '1' } });
  254. val.data.push(count);
  255. val.percentage = await this.calculatePercentage(count, await this.incubatorModel.count());
  256. }
  257. }
  258. }
  259. // 用户统计数量
  260. if (type === 'user') {
  261. list = [
  262. { name: '用户总数', data: [], percentage: 1 },
  263. { name: '未微信绑定', data: [], percentage: 1 },
  264. { name: '未选所属产业', data: [], percentage: 1 },
  265. { name: '审核成功', data: [], percentage: 1 },
  266. ];
  267. for (const [index, val] of list.entries()) {
  268. let count = 0;
  269. if (index === 0) {
  270. count = await this.userModel.count();
  271. val.data.push(count);
  272. } else if (index === 1) {
  273. count = await this.userModel.count({ where: { openid: IsNull() } });
  274. val.data.push(count);
  275. val.percentage = await this.calculatePercentage(count, await this.userModel.count());
  276. } else if (index === 2) {
  277. count = await this.userModel.count({ where: { industry: IsNull() } });
  278. val.data.push(count);
  279. val.percentage = await this.calculatePercentage(count, await this.userModel.count());
  280. } else {
  281. count = await this.userModel.count({ where: { status: '1' } });
  282. val.data.push(count);
  283. val.percentage = await this.calculatePercentage(count, await this.userModel.count());
  284. }
  285. }
  286. }
  287. return list;
  288. }
  289. async twoStatistics(type) {
  290. let res;
  291. let req;
  292. const nameList = [];
  293. const list = [];
  294. // 企业按规模统计
  295. if (type === 'company') {
  296. res = await this.companyModel.createQueryBuilder('company').select('company.pattern', 'pattern').addSelect('COUNT(company.id)', 'total').groupBy('company.pattern').getRawMany();
  297. req = await this.dictDataService.query({ code: 'companyType', is_use: '0' }, {});
  298. if (req.data) {
  299. for (const val of req.data) {
  300. nameList.push(val.label);
  301. const select = res.find(f => f.pattern === val.value);
  302. if (select) list.push(select.total);
  303. else list.push(0);
  304. }
  305. }
  306. }
  307. // 专家按职称统计
  308. if (type === 'expert') {
  309. res = await this.expertModel.createQueryBuilder('expert').select('expert.title', 'title').addSelect('COUNT(expert.id)', 'total').groupBy('expert.title').getRawMany();
  310. for (const val of res) {
  311. nameList.push(val.title);
  312. list.push(val.total);
  313. }
  314. }
  315. // 需求按行业领域统计
  316. if (type === 'demand') {
  317. res = await this.demandModel.createQueryBuilder('demand').select('demand.field', 'field').addSelect('COUNT(demand.id)', 'total').groupBy('demand.field').getRawMany();
  318. for (const val of res) {
  319. if (val.field) {
  320. nameList.push(val.field);
  321. list.push(val.total);
  322. }
  323. }
  324. }
  325. // 供给按行业领域统计
  326. if (type === 'supply') {
  327. res = await this.supplyModel.createQueryBuilder('supply').select('supply.field', 'field').addSelect('COUNT(supply.id)', 'total').groupBy('supply.field').getRawMany();
  328. for (const val of res) {
  329. if (val.field) {
  330. nameList.push(val.field);
  331. list.push(val.total);
  332. }
  333. }
  334. }
  335. // 项目按来源统计
  336. if (type === 'project') {
  337. res = await this.projectModel.createQueryBuilder('project').select('project.source', 'source').addSelect('COUNT(project.id)', 'total').groupBy('project.source').getRawMany();
  338. for (const val of res) {
  339. if (val.source) {
  340. nameList.push(val.source);
  341. list.push(val.total);
  342. }
  343. }
  344. }
  345. // 成果按来源统计
  346. if (type === 'achievement') {
  347. res = await this.achievementModel.createQueryBuilder('achievement').select('achievement.source', 'source').addSelect('COUNT(achievement.id)', 'total').groupBy('achievement.source').getRawMany();
  348. for (const val of res) {
  349. if (val.source) {
  350. nameList.push(val.source);
  351. list.push(val.total);
  352. }
  353. }
  354. }
  355. //孵化器入驻数量
  356. if (type === 'incubate') {
  357. res = await this.incubatorModel.createQueryBuilder('incubator').getMany();
  358. for (const val of res) {
  359. nameList.push(val.name);
  360. list.push(parseInt(val.company_num || 0));
  361. }
  362. }
  363. //用户所属产业包含总数
  364. if (type === 'user') {
  365. req = await this.sectorService.query({ is_use: '0' }, {});
  366. if (req.data) {
  367. for (const val of req.data) {
  368. const total = await this.userModel
  369. .createQueryBuilder()
  370. .where('industry IS NOT NULL') // 确保username字段不为空
  371. .andWhere('JSONB_EXISTS(industry, :column)', { column: val.title }) // 使用数组作为查询条件
  372. .getCount(); // 获取总数
  373. nameList.push(val.title);
  374. list.push({ name: val.title, value: total || 0 });
  375. }
  376. }
  377. }
  378. return { nameList, list };
  379. }
  380. async thrStatistics(type) {
  381. let nameList = [];
  382. let list = [];
  383. let res;
  384. let req;
  385. // 企业行业领域统计
  386. if (type === 'company') {
  387. res = await this.companyModel.createQueryBuilder('company').select('company.type', 'type').addSelect('COUNT(company.id)', 'total').groupBy('company.type').getRawMany();
  388. req = await this.dictDataService.query({ code: 'companyIndustry', is_use: '0' }, {});
  389. for (const val of res) {
  390. if (req.data) {
  391. const select = req.data.find(f => f.value === val.type);
  392. if (select) {
  393. nameList.push(select.label);
  394. list.push({ name: select.label, value: val.total });
  395. }
  396. }
  397. }
  398. }
  399. // 专家行业领域统计
  400. if (type === 'expert') {
  401. res = await this.expertModel.createQueryBuilder('expert').select('expert.industry_type', 'industry_type').addSelect('COUNT(expert.id)', 'total').groupBy('expert.industry_type').getRawMany();
  402. for (const val of res) {
  403. if (val.industry_type) {
  404. nameList.push(val.industry_type);
  405. list.push({ name: val.industry_type, value: val.total });
  406. }
  407. }
  408. }
  409. // 需求所属产业统计
  410. if (type === 'demand') {
  411. res = await this.demandModel.createQueryBuilder('demand').select('demand.industry', 'industry').addSelect('COUNT(demand.id)', 'total').groupBy('demand.industry').getRawMany();
  412. for (const val of res) {
  413. if (val.industry) {
  414. nameList.push(val.industry);
  415. list.push({ name: val.industry, value: val.total });
  416. }
  417. }
  418. }
  419. // 供给所属产业统计
  420. if (type === 'supply') {
  421. res = await this.supplyModel.createQueryBuilder('supply').select('supply.industry', 'industry').addSelect('COUNT(supply.id)', 'total').groupBy('supply.industry').getRawMany();
  422. for (const val of res) {
  423. if (val.industry) {
  424. nameList.push(val.industry);
  425. list.push({ name: val.industry, value: val.total });
  426. }
  427. }
  428. }
  429. // 项目项目进展统计
  430. if (type === 'project') {
  431. res = await this.projectModel.createQueryBuilder('project').select('project.progress', 'progress').addSelect('COUNT(project.id)', 'total').groupBy('project.progress').getRawMany();
  432. for (const val of res) {
  433. if (val.progress) {
  434. nameList.push(val.progress);
  435. list.push({ name: val.progress, value: val.total });
  436. }
  437. }
  438. }
  439. // 成果行业领域统计
  440. if (type === 'achievement') {
  441. res = await this.achievementModel.createQueryBuilder('achievement').select('achievement.field', 'field').addSelect('COUNT(achievement.id)', 'total').groupBy('achievement.field').getRawMany();
  442. for (const val of res) {
  443. if (val.field) {
  444. nameList.push(val.field);
  445. list.push({ name: val.field, value: val.total });
  446. }
  447. }
  448. }
  449. // 孵化器市级以上活动数统计数量
  450. if (type === 'incubate') {
  451. res = await this.incubatorModel.createQueryBuilder('incubator').getMany();
  452. for (const val of res) {
  453. nameList.push(val.name);
  454. list.push({ name: val.name, value: parseInt(val.actCity_num) || 0 });
  455. }
  456. }
  457. // 用户角色统计
  458. if (type === 'user') {
  459. res = await this.userModel.createQueryBuilder('expert').select('expert.role', 'role').addSelect('COUNT(expert.id)', 'total').groupBy('expert.role').getRawMany();
  460. req = await this.roleService.query({ is_use: '0' }, {});
  461. for (const val of res) {
  462. if (req.data) {
  463. for (const ass of val.role) {
  464. if (ass !== 'User') {
  465. const select = req.data.find(f => f.code === ass);
  466. if (select) {
  467. nameList.push(select.name);
  468. list.push({ name: select.name, value: parseInt(val.total) });
  469. }
  470. }
  471. }
  472. }
  473. }
  474. nameList = uniq(nameList);
  475. list = list.reduce((acc, obj) => {
  476. const existingObj = acc.find(item => item.name === obj.name);
  477. if (existingObj) {
  478. existingObj.value += obj.value;
  479. } else {
  480. acc.push({ ...obj });
  481. }
  482. return acc;
  483. }, []);
  484. }
  485. return { nameList, list };
  486. }
  487. async fourStatistics(type) {
  488. let list = [];
  489. let res;
  490. // 企业地区分布
  491. if (type === 'company') {
  492. res = await this.companyModel.createQueryBuilder('company').select('company.area', 'area').addSelect('COUNT(company.id)', 'total').groupBy('company.area').getRawMany();
  493. for (const val of res) {
  494. if (val.area && val.area.length > 1) {
  495. const area = val.area[1];
  496. list.push({ name: area, value: parseInt(val.total) });
  497. }
  498. }
  499. list = list.reduce((acc, obj) => {
  500. const existingObj = acc.find(item => item.name === obj.name);
  501. if (existingObj) {
  502. existingObj.value += obj.value;
  503. } else {
  504. acc.push({ ...obj });
  505. }
  506. return acc;
  507. }, []);
  508. }
  509. // 专家地区分布
  510. if (type === 'expert') {
  511. res = await this.expertModel.createQueryBuilder('expert').select('expert.area', 'area').addSelect('COUNT(expert.id)', 'total').groupBy('expert.area').getRawMany();
  512. for (const val of res) {
  513. if (val.area && val.area.length > 1) {
  514. const area = val.area[1];
  515. list.push({ name: area, value: parseInt(val.total) });
  516. }
  517. }
  518. list = list.reduce((acc, obj) => {
  519. const existingObj = acc.find(item => item.name === obj.name);
  520. if (existingObj) {
  521. existingObj.value += obj.value;
  522. } else {
  523. acc.push({ ...obj });
  524. }
  525. return acc;
  526. }, []);
  527. }
  528. // 需求地区分布
  529. if (type === 'demand') {
  530. res = await this.demandModel.createQueryBuilder('demand').select('demand.area', 'area').addSelect('COUNT(demand.id)', 'total').groupBy('demand.area').getRawMany();
  531. for (const val of res) {
  532. if (val.area && val.area.length > 1) {
  533. const area = val.area[1];
  534. list.push({ name: area, value: parseInt(val.total) });
  535. }
  536. }
  537. list = list.reduce((acc, obj) => {
  538. const existingObj = acc.find(item => item.name === obj.name);
  539. if (existingObj) {
  540. existingObj.value += obj.value;
  541. } else {
  542. acc.push({ ...obj });
  543. }
  544. return acc;
  545. }, []);
  546. }
  547. // 供给地区分布
  548. if (type === 'supply') {
  549. res = await this.supplyModel.createQueryBuilder('supply').select('supply.area', 'area').addSelect('COUNT(supply.id)', 'total').groupBy('supply.area').getRawMany();
  550. for (const val of res) {
  551. if (val.area && val.area.length > 1) {
  552. const area = val.area[1];
  553. list.push({ name: area, value: parseInt(val.total) });
  554. }
  555. }
  556. list = list.reduce((acc, obj) => {
  557. const existingObj = acc.find(item => item.name === obj.name);
  558. if (existingObj) {
  559. existingObj.value += obj.value;
  560. } else {
  561. acc.push({ ...obj });
  562. }
  563. return acc;
  564. }, []);
  565. }
  566. // 成果地区分布
  567. if (type === 'achievement') {
  568. res = await this.achievementModel.createQueryBuilder('achievement').select('achievement.area', 'area').addSelect('COUNT(achievement.id)', 'total').groupBy('achievement.area').getRawMany();
  569. for (const val of res) {
  570. if (val.area && val.area.length > 1) {
  571. const area = val.area[1];
  572. list.push({ name: area, value: parseInt(val.total) });
  573. }
  574. }
  575. list = list.reduce((acc, obj) => {
  576. const existingObj = acc.find(item => item.name === obj.name);
  577. if (existingObj) {
  578. existingObj.value += obj.value;
  579. } else {
  580. acc.push({ ...obj });
  581. }
  582. return acc;
  583. }, []);
  584. }
  585. // 项目地区分布
  586. if (type === 'project') {
  587. res = await this.projectModel.createQueryBuilder('project').select('project.area', 'area').addSelect('COUNT(project.id)', 'total').groupBy('project.area').getRawMany();
  588. for (const val of res) {
  589. if (val.area && val.area.length > 1) {
  590. const area = val.area[1];
  591. list.push({ name: area, value: parseInt(val.total) });
  592. }
  593. }
  594. list = list.reduce((acc, obj) => {
  595. const existingObj = acc.find(item => item.name === obj.name);
  596. if (existingObj) {
  597. existingObj.value += obj.value;
  598. } else {
  599. acc.push({ ...obj });
  600. }
  601. return acc;
  602. }, []);
  603. }
  604. // 孵化基地地区分布
  605. if (type === 'incubate') {
  606. res = await this.incubatorModel.createQueryBuilder('incubator').select('incubator.area', 'area').addSelect('COUNT(incubator.id)', 'total').groupBy('incubator.area').getRawMany();
  607. for (const val of res) {
  608. if (val.area && val.area.length > 1) {
  609. const area = val.area[1];
  610. list.push({ name: area, value: parseInt(val.total) });
  611. }
  612. }
  613. list = list.reduce((acc, obj) => {
  614. const existingObj = acc.find(item => item.name === obj.name);
  615. if (existingObj) {
  616. existingObj.value += obj.value;
  617. } else {
  618. acc.push({ ...obj });
  619. }
  620. return acc;
  621. }, []);
  622. }
  623. return list;
  624. }
  625. async fiveStatistics(type) {
  626. let res;
  627. let req;
  628. const nameList = [];
  629. const list = [];
  630. //用户所属产业包含总数
  631. if (type === 'user') {
  632. req = await this.sectorService.query({ is_use: '0' }, {});
  633. if (req.data) {
  634. for (const val of req.data) {
  635. const total = await this.userModel
  636. .createQueryBuilder()
  637. .where('industry IS NOT NULL') // 确保username字段不为空
  638. .andWhere('JSONB_EXISTS(industry, :column)', { column: val.title }) // 使用数组作为查询条件
  639. .getCount(); // 获取总数
  640. nameList.push(val.title);
  641. list.push({ name: val.title, value: total || 0 });
  642. }
  643. }
  644. }
  645. return { nameList, list };
  646. }
  647. async sixStatistics(type) {
  648. let res;
  649. let req;
  650. const nameList = [];
  651. const list = [];
  652. let max = [];
  653. // 企业按员工人数排序取前8个企业
  654. if (type === 'company') {
  655. res = await this.companyModel.createQueryBuilder('company').orderBy('company.person', 'DESC').limit(8).getMany();
  656. for (const val of res) {
  657. nameList.push(val.name);
  658. list.push(parseInt(val.person || 0));
  659. }
  660. max = [1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000];
  661. }
  662. // 专家按职称统计
  663. if (type === 'expert') {
  664. res = await this.expertModel.createQueryBuilder('expert').select('expert.title', 'title').addSelect('COUNT(expert.id)', 'total').groupBy('expert.title').getRawMany();
  665. res.sort((a, b) => b.total - a.total);
  666. for (const val of res.slice(0, 9)) {
  667. nameList.push(val.title);
  668. list.push(parseInt(val.total || 0));
  669. }
  670. max = [50, 50, 50, 50, 50, 50, 50, 50];
  671. }
  672. // 需求按行业领域统计
  673. if (type === 'demand') {
  674. res = await this.demandModel.createQueryBuilder('demand').select('demand.field', 'field').addSelect('COUNT(demand.id)', 'total').groupBy('demand.field').getRawMany();
  675. res.sort((a, b) => b.total - a.total);
  676. for (const val of res.slice(0, 9)) {
  677. if (val.field) {
  678. nameList.push(val.field);
  679. list.push({ name: val.field, value: val.total });
  680. }
  681. }
  682. max = [100, 100, 100, 100, 100, 100, 100, 100];
  683. }
  684. // 供给按行业领域统计
  685. if (type === 'supply') {
  686. res = await this.supplyModel.createQueryBuilder('supply').select('supply.field', 'field').addSelect('COUNT(supply.id)', 'total').groupBy('supply.field').getRawMany();
  687. res.sort((a, b) => b.total - a.total);
  688. for (const val of res.slice(0, 9)) {
  689. if (val.field) {
  690. nameList.push(val.field);
  691. list.push({ name: val.field, value: val.total });
  692. }
  693. }
  694. max = [100, 100, 100, 100, 100, 100, 100, 100];
  695. }
  696. // 项目按来源统计
  697. if (type === 'project') {
  698. res = await this.projectModel.createQueryBuilder('project').select('project.source', 'source').addSelect('COUNT(project.id)', 'total').groupBy('project.source').getRawMany();
  699. res.sort((a, b) => b.total - a.total);
  700. for (const val of res.slice(0, 9)) {
  701. if (val.source) {
  702. nameList.push(val.source);
  703. list.push({ name: val.source, value: val.total });
  704. }
  705. }
  706. max = [100, 100, 100, 100, 100, 100, 100, 100];
  707. }
  708. // 成果按来源统计
  709. if (type === 'achievement') {
  710. res = await this.achievementModel.createQueryBuilder('achievement').select('achievement.source', 'source').addSelect('COUNT(achievement.id)', 'total').groupBy('achievement.source').getRawMany();
  711. res.sort((a, b) => b.total - a.total);
  712. for (const val of res.slice(0, 9)) {
  713. if (val.source) {
  714. nameList.push(val.source);
  715. list.push({ name: val.source, value: val.total });
  716. }
  717. }
  718. max = [100, 100, 100, 100, 100, 100, 100, 100];
  719. }
  720. //孵化器省级以上导师数排名
  721. if (type === 'incubate') {
  722. res = await this.incubatorModel.createQueryBuilder('incubator').getMany();
  723. for (const val of res) {
  724. nameList.push(val.name);
  725. list.push({ name: val.name, value: parseInt(val.teacher_num) });
  726. }
  727. list.sort((a, b) => b.value - a.value);
  728. max = [100, 100, 100, 100, 100, 100, 100, 100];
  729. }
  730. //用户男女比例排名
  731. if (type === 'user') {
  732. res = await this.userModel.createQueryBuilder('user').select('user.gender', 'gender').addSelect('COUNT(user.id)', 'total').groupBy('user.gender').getRawMany();
  733. req = await this.dictDataService.query({ code: 'gender', is_use: '0' }, {});
  734. res.sort((a, b) => b.total - a.total);
  735. for (const val of res) {
  736. if (req.data) {
  737. const select = req.data.find(f => f.value === val.gender);
  738. if (select) {
  739. nameList.push(select.label);
  740. list.push({ name: select.label, value: val.total });
  741. }
  742. }
  743. }
  744. max = [100, 100, 100, 100, 100, 100, 100, 100];
  745. }
  746. return { nameList, list, max };
  747. }
  748. // 计算百分比
  749. calculatePercentage(numerator, denominator) {
  750. if (denominator === 0 || isNaN(numerator) || isNaN(denominator)) {
  751. return 0;
  752. }
  753. return (numerator / denominator).toFixed(1);
  754. }
  755. // 统计总数
  756. async toTotal() {
  757. const companyTotal = await this.companyModel.count();
  758. const supplyTotal = await this.supplyModel.count();
  759. const demandTotal = await this.demandModel.count();
  760. const projectTotal = await this.projectModel.count();
  761. const achievementTotal = await this.achievementModel.count();
  762. const data = [
  763. { id: '1', name: '企业', num: companyTotal, unit: '家' },
  764. { id: '2', name: '供给', num: supplyTotal, unit: '个' },
  765. { id: '3', name: '需求', num: demandTotal, unit: '个' },
  766. { id: '4', name: '项目', num: projectTotal, unit: '个' },
  767. { id: '5', name: '成果', num: achievementTotal, unit: '个' },
  768. ];
  769. return data;
  770. }
  771. // 企业行业领域统计
  772. async Companystatistics() {
  773. let res;
  774. const nameList1 = [];
  775. const list1 = [];
  776. const nameList2 = [];
  777. const list2 = [];
  778. let req;
  779. res = await this.companyModel.createQueryBuilder('company').select('company.type', 'type').addSelect('COUNT(company.id)', 'total').groupBy('company.type').getRawMany();
  780. req = await this.dictDataService.query({ code: 'companyIndustry', is_use: '0' }, {});
  781. for (const val of res) {
  782. if (req.data) {
  783. const select = req.data.find(f => f.value === val.type);
  784. if (select) {
  785. nameList1.push(select.label);
  786. list1.push({ name: select.label, value: val.total });
  787. }
  788. }
  789. }
  790. res = await this.companyModel.createQueryBuilder('company').select('company.pattern', 'pattern').addSelect('COUNT(company.id)', 'total').groupBy('company.pattern').getRawMany();
  791. req = await this.dictDataService.query({ code: 'companyType', is_use: '0' }, {});
  792. if (req.data) {
  793. for (const val of req.data) {
  794. nameList2.push(val.label);
  795. const select = res.find(f => f.pattern === val.value);
  796. if (select) list2.push(select.total);
  797. else list2.push(0);
  798. }
  799. }
  800. return { one: { nameList: nameList1, list: list1 }, two: { nameList: nameList2, list: list2 } };
  801. }
  802. // 供给行业统计
  803. async Supplystatistics() {
  804. let res;
  805. const nameList1 = [];
  806. const list1 = [];
  807. const nameList2 = [];
  808. const list2 = [];
  809. res = await this.supplyModel.createQueryBuilder('supply').select('supply.industry', 'industry').addSelect('COUNT(supply.id)', 'total').groupBy('supply.industry').getRawMany();
  810. for (const val of res) {
  811. if (val.industry) {
  812. nameList1.push(val.industry);
  813. list1.push({ name: val.industry, value: val.total });
  814. }
  815. }
  816. res = await this.supplyModel.createQueryBuilder('supply').select('supply.source', 'source').addSelect('COUNT(supply.id)', 'total').groupBy('supply.source').getRawMany();
  817. for (const val of res) {
  818. if (val.source) {
  819. nameList2.push(val.source);
  820. list2.push(val.total);
  821. }
  822. }
  823. return { one: { nameList: nameList1, list: list1 }, two: { nameList: nameList2, list: list2 } };
  824. }
  825. // 需求行业统计
  826. async Demandstatistics() {
  827. let res;
  828. const nameList1 = [];
  829. const list1 = [];
  830. const nameList2 = [];
  831. const list2 = [];
  832. res = await this.demandModel.createQueryBuilder('demand').select('demand.industry', 'industry').addSelect('COUNT(demand.id)', 'total').groupBy('demand.industry').getRawMany();
  833. for (const val of res) {
  834. if (val.industry) {
  835. nameList1.push(val.industry);
  836. list1.push({ name: val.industry, value: val.total });
  837. }
  838. }
  839. res = await this.demandModel.createQueryBuilder('demand').select('demand.year', 'year').addSelect('COUNT(demand.id)', 'total').groupBy('demand.year').getRawMany();
  840. for (const val of res) {
  841. if (val.year) {
  842. nameList2.push(val.year);
  843. list2.push(val.total);
  844. }
  845. }
  846. return { one: { nameList: nameList1, list: list1 }, two: { nameList: nameList2, list: list2 } };
  847. }
  848. // 项目行业统计
  849. async Projectstatistics() {
  850. let res;
  851. const nameList1 = [];
  852. const list1 = [];
  853. const nameList2 = [];
  854. const list2 = [];
  855. res = await this.projectModel.createQueryBuilder('project').select('project.industry', 'industry').addSelect('COUNT(project.id)', 'total').groupBy('project.industry').getRawMany();
  856. for (const val of res) {
  857. if (val.industry) {
  858. nameList1.push(val.industry);
  859. list1.push({ name: val.industry, value: val.total });
  860. }
  861. }
  862. res = await this.projectModel.createQueryBuilder('project').select('project.progress', 'progress').addSelect('COUNT(project.id)', 'total').groupBy('project.progress').getRawMany();
  863. for (const val of res) {
  864. if (val.progress) {
  865. nameList2.push(val.progress);
  866. list2.push(val.total);
  867. }
  868. }
  869. return { one: { nameList: nameList1, list: list1 }, two: { nameList: nameList2, list: list2 } };
  870. }
  871. // 成果行业统计
  872. async Achievementstatistics() {
  873. let res;
  874. const nameList1 = [];
  875. const list1 = [];
  876. const nameList2 = [];
  877. const list2 = [];
  878. res = await this.achievementModel.createQueryBuilder('achievement').select('achievement.field', 'field').addSelect('COUNT(achievement.id)', 'total').groupBy('achievement.field').getRawMany();
  879. for (const val of res) {
  880. if (val.field) {
  881. nameList1.push(val.field);
  882. list1.push({ name: val.field, value: val.total });
  883. }
  884. }
  885. res = await this.achievementModel.createQueryBuilder('achievement').select('achievement.source', 'source').addSelect('COUNT(achievement.id)', 'total').groupBy('achievement.source').getRawMany();
  886. for (const val of res) {
  887. if (val.source) {
  888. nameList2.push(val.source);
  889. list2.push(val.total);
  890. }
  891. }
  892. return { one: { nameList: nameList1, list: list1 }, two: { nameList: nameList2, list: list2 } };
  893. }
  894. // 导出
  895. async toExport(query) {
  896. const { table, config, user } = query;
  897. const nowDate = new Date().getTime();
  898. const filename = `产学研用导出-${table}-${nowDate}.xlsx`;
  899. const path = this.path;
  900. if (!path) {
  901. throw new ServiceError('服务端没有设置存储路径');
  902. }
  903. if (!fs.existsSync(path)) {
  904. // 如果不存在文件夹,就创建
  905. this.mkdir(path);
  906. }
  907. try {
  908. let info = {};
  909. if (user) info = { where: { user: Equal(user) } };
  910. const targetModel = this[`${table}Model`];
  911. const data = await targetModel.find(info);
  912. const workbook = new Excel.Workbook();
  913. const sheet = workbook.addWorksheet('sheet1');
  914. // 根据前端传回来的设置和顺序,将表头先塞进去
  915. const meta = config.map(i => i.label);
  916. sheet.addRow(meta);
  917. let arr = [];
  918. for (const d of data) {
  919. arr = [];
  920. for (const c of config) {
  921. const { mark, model, code } = c;
  922. if (mark === 'tags') {
  923. if (d[model]) d[model] = arr.push(d[model].join(','));
  924. else arr.push(d[model]);
  925. } else if (mark === 'area') {
  926. if (d[model]) d[model] = arr.push(d[model].join('-'));
  927. else arr.push(d[model]);
  928. } else if (mark === 'dict') {
  929. const req = await this.dictDataService.query({ code, is_use: '0' }, {});
  930. if (req.data) {
  931. const selected = req.data.find(f => f.value === d[model]);
  932. if (selected) {
  933. d[model] = get(selected, 'label');
  934. arr.push(d[model]);
  935. } else arr.push(d[model]);
  936. } else arr.push(d[model]);
  937. } else if (mark === 'time') {
  938. if (d.start_time && d.end_time) {
  939. arr.push((d[model] = `${d.start_time}-${d.end_time}`));
  940. } else arr.push(d[model]);
  941. } else arr.push(d[model]);
  942. }
  943. sheet.addRow(arr);
  944. }
  945. // 生成excel
  946. const filepath = `${path}${filename}`;
  947. if (data.length <= 0) return;
  948. await workbook.xlsx.writeFile(filepath);
  949. return `/files/cxyy/export/${filename}`;
  950. } catch (error) {
  951. console.log(error);
  952. }
  953. }
  954. // 创建文件夹
  955. mkdir(dirname) {
  956. if (fs.existsSync(dirname)) {
  957. return true;
  958. }
  959. if (this.mkdir(Path.dirname(dirname))) {
  960. fs.mkdirSync(dirname);
  961. return true;
  962. }
  963. }
  964. randomStr(len = 6) {
  965. return Math.random().toString(36).slice(-len);
  966. }
  967. /**
  968. * 成果
  969. * @param {Object} rows excel数据
  970. */
  971. async achievement(rows) {
  972. const mappingList = mappings.achievement;
  973. const result = await this.dealRows(rows, mappingList);
  974. const errorList = [];
  975. // 需要查看是否有人,有人更新数据,没人添加数据
  976. const user_id = this.ctx.user.id;
  977. for (const [index, i] of result.entries()) {
  978. // 根据 成果名称 查重
  979. const { name } = i;
  980. const data = await this.achievementModel.findOne({ where: { user: Equal(user_id), name: Equal(name) } });
  981. if (data && data.id) {
  982. try {
  983. i.id = data.id;
  984. await this.achievementModel.update({ id: data.id }, i);
  985. } catch (error) {
  986. errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
  987. }
  988. } else {
  989. try {
  990. await this.achievementModel.insert({ ...i, user: user_id, status: '0' });
  991. } catch (error) {
  992. const namek = Object.keys(error.errors)[0];
  993. const mapping = mappingList.find(i => i.field === namek);
  994. errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
  995. }
  996. }
  997. }
  998. return { result: result.length, errorList };
  999. }
  1000. /**
  1001. * 项目
  1002. * @param {Object} rows excel数据
  1003. */
  1004. async project(rows) {
  1005. const mappingList = mappings.project;
  1006. const result = await this.dealRows(rows, mappingList);
  1007. const errorList = [];
  1008. // 需要查看是否有人,有人更新数据,没人添加数据
  1009. const user_id = this.ctx.user.id;
  1010. for (const [index, i] of result.entries()) {
  1011. // 根据 项目名称 查重
  1012. const { name } = i;
  1013. const data = await this.projectModel.findOne({ where: { user: Equal(user_id), name: Equal(name) } });
  1014. if (data && data.id) {
  1015. try {
  1016. i.id = data.id;
  1017. await this.projectModel.update({ id: data.id }, i);
  1018. } catch (error) {
  1019. errorList.push(`修改第${index + 1}条${name}字段出现错误!!!`);
  1020. }
  1021. } else {
  1022. try {
  1023. await this.projectModel.insert({ ...i, user: user_id, status: '0' });
  1024. } catch (error) {
  1025. const namek = Object.keys(error.errors)[0];
  1026. const mapping = mappingList.find(i => i.field === namek);
  1027. errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
  1028. }
  1029. }
  1030. }
  1031. return { result: result.length, errorList };
  1032. }
  1033. /**
  1034. * 需求
  1035. * @param {Object} rows excel数据
  1036. */
  1037. async demand(rows) {
  1038. const mappingList = mappings.demand;
  1039. const result = await this.dealRows(rows, mappingList);
  1040. const errorList = [];
  1041. // 需要查看是否有人,有人更新数据,没人添加数据
  1042. const user_id = this.ctx.user.id;
  1043. for (const [index, i] of result.entries()) {
  1044. // 根据 需求名称 查重
  1045. const { name } = i;
  1046. const data = await this.demandModel.findOne({ where: { user: Equal(user_id), name: Equal(name) } });
  1047. if (data && data.id) {
  1048. try {
  1049. i.id = data.id;
  1050. await this.demandModel.update({ id: data.id }, i);
  1051. } catch (error) {
  1052. errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
  1053. }
  1054. } else {
  1055. try {
  1056. await this.demandModel.insert({ ...i, user: user_id, status: '0' });
  1057. } catch (error) {
  1058. const namek = Object.keys(error.errors)[0];
  1059. const mapping = mappingList.find(i => i.field === namek);
  1060. errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
  1061. }
  1062. }
  1063. }
  1064. return { result: result.length, errorList };
  1065. }
  1066. /**
  1067. * 供给
  1068. * @param {Object} rows excel数据
  1069. */
  1070. async supply(rows) {
  1071. const mappingList = mappings.supply;
  1072. const result = await this.dealRows(rows, mappingList);
  1073. const errorList = [];
  1074. // 需要查看是否有人,有人更新数据,没人添加数据
  1075. const user_id = this.ctx.user.id;
  1076. for (const [index, i] of result.entries()) {
  1077. // 根据 供给名称 查重
  1078. const { name } = i;
  1079. const data = await this.supplyModel.findOne({ where: { user: Equal(user_id), name: Equal(name) } });
  1080. if (data && data.id) {
  1081. try {
  1082. i.id = data.id;
  1083. await this.supplyModel.update({ id: data.id }, i);
  1084. } catch (error) {
  1085. errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
  1086. }
  1087. } else {
  1088. try {
  1089. await this.supplyModel.insert({ ...i, user: user_id, status: '0' });
  1090. } catch (error) {
  1091. const namek = Object.keys(error.errors)[0];
  1092. const mapping = mappingList.find(i => i.field === namek);
  1093. errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
  1094. }
  1095. }
  1096. }
  1097. return { result: result.length, errorList };
  1098. }
  1099. /**
  1100. * 企业
  1101. * @param {Object} rows excel数据
  1102. */
  1103. async company(rows) {
  1104. const mappingList = mappings.company;
  1105. const result = await this.dealRows(rows, mappingList);
  1106. const errorList = [];
  1107. // 需要查看是否有人,有人更新数据,没人添加数据
  1108. for (const [index, i] of result.entries()) {
  1109. // 根据 企业名称 查重
  1110. const { name } = i;
  1111. const data = await this.companyModel.findOne({ where: { name: Equal(name) } });
  1112. if (data && data.id) {
  1113. try {
  1114. i.id = data.id;
  1115. await this.companyModel.update({ id: data.id }, { ...i, status: '1' });
  1116. } catch (error) {
  1117. errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
  1118. }
  1119. } else {
  1120. try {
  1121. await this.companyModel.insert({ ...i, status: '1', is_show: '1' });
  1122. } catch (error) {
  1123. const namek = Object.keys(error.errors)[0];
  1124. const mapping = mappingList.find(i => i.field === namek);
  1125. errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
  1126. }
  1127. }
  1128. }
  1129. return { result: result.length, errorList };
  1130. }
  1131. /**
  1132. * 企业年度
  1133. * @param {Object} rows excel数据
  1134. */
  1135. async companyYear(rows) {
  1136. const mappingList = mappings.companyYear;
  1137. const result = await this.dealRows(rows, mappingList);
  1138. const errorList = [];
  1139. // 需要查看是否有人,有人更新数据,没人添加数据
  1140. for (const [index, i] of result.entries()) {
  1141. const { company, year } = i;
  1142. const data = await this.yModel.findOne({ where: { company: Equal(company), year: Equal(year) } });
  1143. if (data && data.id) {
  1144. try {
  1145. i.id = data.id;
  1146. await this.yModel.update({ id: data.id }, i);
  1147. } catch (error) {
  1148. errorList.push(`修改第${index + 1}条${company}出现错误!!!`);
  1149. }
  1150. } else {
  1151. try {
  1152. const companyInfo = await this.companyModel.findOne({ where: { id: Equal(company) } });
  1153. const userInfo = await this.userService.fetch({ id: companyInfo.user });
  1154. const id = get(userInfo, 'id');
  1155. await this.yModel.insert({ ...i, user: id, status: '1', is_use: '0' });
  1156. } catch (error) {
  1157. const namek = Object.keys(error.errors)[0];
  1158. const mapping = mappingList.find(i => i.field === namek);
  1159. errorList.push(`第${index + 1}条${mapping.zh}列${company}字段出现错误!!!`);
  1160. }
  1161. }
  1162. }
  1163. return { result: result.length, errorList };
  1164. }
  1165. /**
  1166. * 处理excel传来的数据
  1167. * @param {Array} rows excel数据,二维数组
  1168. * @param {Object} mappingList 设置的某个映射
  1169. * @return {Array} result 数据
  1170. */
  1171. async dealRows(rows, mappingList) {
  1172. const result = [];
  1173. const user = this.ctx.user;
  1174. if (!user) throw new ServiceError(ErrorCode.USER_NOT_FOUND);
  1175. for (let i = 0; i < rows.length; i++) {
  1176. const rowData = rows[i];
  1177. const obj = { user: user.id };
  1178. for (let k = 0; k < rowData.length; k++) {
  1179. let val = rowData[k];
  1180. if (val === null) continue;
  1181. if (typeof val === 'object' && val !== null) {
  1182. const richText = get(val, 'richText');
  1183. if (richText) {
  1184. let texts = '';
  1185. // 使用map提取name字段,然后检查是否为undefined
  1186. // eslint-disable-next-line array-callback-return
  1187. const mappedNames = richText.map(item => {
  1188. // 判断字段是否存在
  1189. if ('text' in item) {
  1190. return item.text;
  1191. }
  1192. });
  1193. // 过滤掉null或undefined的值
  1194. const filteredNames = mappedNames.filter(text => text !== null && text !== undefined);
  1195. // 拼接字符串
  1196. texts = filteredNames.join('');
  1197. val = texts;
  1198. }
  1199. }
  1200. const mapping = mappingList.find(f => f.index === k);
  1201. if (mapping) {
  1202. const { field, type } = mapping;
  1203. if (field !== 'tags') {
  1204. if (type) val = await this.dealTypeValue(mapping, val);
  1205. obj[field] = val;
  1206. } else {
  1207. if(val) {
  1208. const arr = val.split(';')
  1209. obj[field] = arr;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. result.push(obj);
  1215. }
  1216. return result;
  1217. }
  1218. /**
  1219. * 转换数据
  1220. * @param {Object} mapping 当前要处理的映射
  1221. * @param {any} val excel值
  1222. * @return {any} result 转换后的数据
  1223. */
  1224. async dealTypeValue(mapping, val) {
  1225. const { type, code } = mapping;
  1226. let result;
  1227. if (type === 'date') {
  1228. // 判断日期对不对,经过moment处理下
  1229. const mobj = dayjs(val);
  1230. if (mobj.isValid()) {
  1231. result = mobj.format('YYYY-MM-DD');
  1232. }
  1233. } else if (type === 'dict') {
  1234. const req = await this.dictDataService.query({ code }, {});
  1235. if (req.data) {
  1236. const selected = req.data.find(f => f.label === val);
  1237. if (selected) result = get(selected, 'value');
  1238. }
  1239. } else if (type === 'area') {
  1240. if (val) result = val.split('-');
  1241. } else if (type === 'number') {
  1242. if (val === 0) {
  1243. result = val;
  1244. } else {
  1245. if (val) result = parseInt(val) || 0;
  1246. }
  1247. } else if (type === 'id') {
  1248. if (val) {
  1249. const company = await this.companyModel.findOne({ where: { name: Equal(val) } });
  1250. if (company) result = get(company, 'id');
  1251. else result = val;
  1252. } else {
  1253. result = val;
  1254. }
  1255. }
  1256. return result;
  1257. }
  1258. nameToFunction(name) {
  1259. const obj = {
  1260. 成果: 'achievement',
  1261. 项目: 'project',
  1262. 需求: 'demand',
  1263. 供给: 'supply',
  1264. 企业: 'company',
  1265. 企业年度: 'companyYear',
  1266. };
  1267. return get(obj, name);
  1268. }
  1269. }