index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import BaseController from "../../common/base/base_controller";
  2. import CompilationModel from '../../users/models/compilation_model';
  3. const multiparty = require('multiparty');
  4. const excel = require('node-xlsx');
  5. const fs = require('fs');
  6. const facade = require('../facade/index');
  7. const config = require("../../../config/config.js");
  8. class PriceInfoController extends BaseController {
  9. async main(req, res) {
  10. const compilationModel = new CompilationModel();
  11. const compilationList = await compilationModel.getCompilationList({ _id: 1, name: 1 });
  12. compilationList.unshift({ _id: 'all', name: '所有' });
  13. const activeCompilation = compilationList.find(compilation => compilation._id.toString() === req.query.filter);
  14. if (activeCompilation) {
  15. activeCompilation.active = 'active';
  16. } else {
  17. compilationList[0].active = 'active'
  18. }
  19. const filter = req.query.filter ? { compilationID: req.query.filter } : {};
  20. const libs = await facade.getLibs(filter);
  21. libs.forEach(lib => {
  22. compilationList.forEach(compilation => {
  23. if (compilation._id.toString() === lib.compilationID) {
  24. lib.compilationName = compilation.name;
  25. }
  26. });
  27. });
  28. const listItem = `
  29. <li class="nav-item">
  30. <a class="nav-link" href="javascript:void(0);" aria-haspopup="true" aria-expanded="false" data-toggle="modal" data-target="#crawl">导入材料价格信息</a>
  31. </li>`
  32. const renderData = {
  33. title: '材料信息价库',
  34. userAccount: req.session.managerData.username,
  35. userID: req.session.managerData.userID,
  36. libs: libs,
  37. compilationList: compilationList,
  38. listItem,
  39. layout: 'maintain/common/html/layout'
  40. };
  41. res.render("maintain/price_info_lib/html/main.html", renderData);
  42. }
  43. async editView(req, res) {
  44. const { libID } = req.query;
  45. const libs = await facade.getLibs({ ID: libID });
  46. if (!libs.length) {
  47. return res.send(404);
  48. }
  49. const areaList = await facade.getAreas(libs[0].compilationID);
  50. const renderData = {
  51. compilationID: libs[0].compilationID,
  52. libName: libs[0].name,
  53. period: libs[0].period,
  54. areaList: JSON.stringify(areaList),
  55. userAccount: req.session.managerData.username,
  56. userID: req.session.managerData.userID,
  57. LicenseKey: config.getLicenseKey(process.env.NODE_ENV),
  58. };
  59. res.render("maintain/price_info_lib/html/edit.html", renderData);
  60. }
  61. async addLib(req, res) {
  62. try {
  63. const { name, period, compilationID } = req.body;
  64. await facade.createLib(name, period, compilationID)
  65. } catch (err) {
  66. console.log(err);
  67. }
  68. res.redirect(req.headers.referer);
  69. }
  70. async renameLib(req, res) {
  71. try {
  72. const { libID, name } = JSON.parse(req.body.data);
  73. await facade.updateLib({ ID: libID }, { name });
  74. res.json({ error: 0, message: 'rename success' });
  75. } catch (err) {
  76. console.log(err);
  77. res.json({ error: 1, message: err.toString() });
  78. }
  79. }
  80. async deleteLib(req, res) {
  81. try {
  82. const { libID } = JSON.parse(req.body.data);
  83. await facade.deleteLib(libID);
  84. res.json({ error: 0, message: 'delete success' });
  85. } catch (err) {
  86. console.log(err);
  87. res.json({ error: 1, message: err.toString() });
  88. }
  89. }
  90. async processChecking(req, res) {
  91. try {
  92. const { key } = JSON.parse(req.body.data);
  93. const data = await facade.processChecking(key);
  94. res.json({ error: 0, message: 'processChecking', data });
  95. } catch (err) {
  96. res.json({ error: 1, message: err.toString() });
  97. }
  98. }
  99. // 爬取数据
  100. async crawlData(req, res) {
  101. try {
  102. const { from, to, compilationID } = JSON.parse(req.body.data);
  103. //res.setTimeout(1000 * 60 * 60 * 2); // 不设置的话,处理时间过长,会触发默认的响应超时,报错(前端报错,后台还继续在处理)
  104. await facade.crawlDataByCompilation(compilationID, from, to);
  105. res.json({ error: 0, message: 'crawl processing' });
  106. } catch (err) {
  107. console.log(err);
  108. res.json({ error: 1, message: err.toString() });
  109. }
  110. }
  111. async importExcel(req, res) {
  112. let responseData = {
  113. err: 0,
  114. msg: ''
  115. };
  116. res.setTimeout(1000 * 60 * 10);
  117. const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
  118. const uploadOption = {
  119. uploadDir: './public'
  120. };
  121. const form = new multiparty.Form(uploadOption);
  122. let uploadFullName;
  123. form.parse(req, async function (err, fields, files) {
  124. try {
  125. const libID = fields.libID !== undefined && fields.libID.length > 0 ?
  126. fields.libID[0] : null;
  127. const importType = fields.importType !== undefined && fields.importType.length > 0 ?
  128. fields.importType[0] : null;
  129. if (!libID || !importType) {
  130. throw '参数错误。';
  131. }
  132. const file = files.file !== undefined ? files.file[0] : null;
  133. if (err || file === null) {
  134. throw '上传失败。';
  135. }
  136. // 判断类型
  137. if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
  138. throw '不支持该类型';
  139. }
  140. // 重命名文件名
  141. uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
  142. fs.renameSync(file.path, uploadFullName);
  143. const sheet = excel.parse(uploadFullName);
  144. if (sheet[0] === undefined || sheet[0].data === undefined || sheet[0].data.length <= 0) {
  145. throw 'excel没有对应数据。';
  146. }
  147. // 提取excel数据并入库
  148. importType === 'originalData' ? await facade.importExcelData(libID, sheet[0].data) : await facade.importKeyData(libID, sheet[0].data, sheet[1].data);
  149. // 删除文件
  150. if (uploadFullName && fs.existsSync(uploadFullName)) {
  151. fs.unlink(uploadFullName);
  152. }
  153. res.json(responseData);
  154. }
  155. catch (error) {
  156. console.log(error);
  157. if (uploadFullName && fs.existsSync(uploadFullName)) {
  158. fs.unlink(uploadFullName);
  159. }
  160. responseData.err = 1;
  161. responseData.msg = error.toString();
  162. res.json(responseData);
  163. }
  164. });
  165. }
  166. async editArea(req, res) {
  167. try {
  168. const { updateData } = JSON.parse(req.body.data);
  169. await facade.updateAres(updateData);
  170. res.json({ error: 0, message: 'update areas success' });
  171. } catch (err) {
  172. console.log(err);
  173. res.json({ error: 1, message: err.toString() });
  174. }
  175. }
  176. async insertArea(req, res) {
  177. try {
  178. const { insertData } = JSON.parse(req.body.data);
  179. await facade.insertAreas(insertData);
  180. res.json({ error: 0, message: 'update areas success' });
  181. } catch (err) {
  182. console.log(err);
  183. res.json({ error: 1, message: err.toString() });
  184. }
  185. }
  186. async deleteArea(req, res) {
  187. try {
  188. const { deleteData } = JSON.parse(req.body.data);
  189. await facade.deleteAreas(deleteData);
  190. res.json({ error: 0, message: 'update areas success' });
  191. } catch (err) {
  192. console.log(err);
  193. res.json({ error: 1, message: err.toString() });
  194. }
  195. }
  196. async getClassData(req, res) {
  197. try {
  198. const { libID, areaID } = JSON.parse(req.body.data);
  199. const data = await facade.getClassData(libID, areaID);
  200. res.json({ error: 0, message: 'getCLass success', data });
  201. } catch (err) {
  202. console.log(err);
  203. res.json({ error: 1, message: err.toString() });
  204. }
  205. }
  206. async calcPriceIndex(req, res) {
  207. try {
  208. const { period, libID, compilationID } = JSON.parse(req.body.data);
  209. const areaID = '971fb9a0-0f93-11eb-b53c-45271c1df90f';//写死珠海地区
  210. const data = await facade.calcPriceIndex(libID, period, areaID, compilationID);
  211. res.json({ error: 0, message: 'getCLass success', data });
  212. } catch (err) {
  213. console.log(err);
  214. res.json({ error: 1, message: err.toString() });
  215. }
  216. }
  217. async getPriceData(req, res) {
  218. try {
  219. const { classIDList } = JSON.parse(req.body.data);
  220. const data = await facade.getPriceData(classIDList);
  221. res.json({ error: 0, message: 'getPriceData success', data });
  222. } catch (err) {
  223. console.log(err);
  224. res.json({ error: 1, message: err.toString() });
  225. }
  226. }
  227. async getPriceEmptyData(req, res) {
  228. try {
  229. const { libID, compilationID } = JSON.parse(req.body.data);
  230. const data = await facade.getPriceEmptyData(compilationID, libID);
  231. res.json({ error: 0, message: 'getPriceEmptyData success', data });
  232. } catch (err) {
  233. console.log(err);
  234. res.json({ error: 1, message: err.toString() });
  235. }
  236. }
  237. async getRecommendPriceSummaryData(req, res) {
  238. try {
  239. const { keyword } = JSON.parse(req.body.data);
  240. const data = await facade.getRecommendPriceSummaryData(keyword);
  241. res.json({ error: 0, message: 'getRecommendPriceSummaryData success', data });
  242. } catch (err) {
  243. console.log(err);
  244. res.json({ error: 1, message: err.toString() });
  245. }
  246. }
  247. async editPriceData(req, res) {
  248. try {
  249. const { postData } = JSON.parse(req.body.data);
  250. await facade.editPriceData(postData);
  251. res.json({ error: 0, message: 'editPrice success' });
  252. } catch (err) {
  253. console.log(err);
  254. res.json({ error: 1, message: err.toString() });
  255. }
  256. }
  257. async editClassData(req, res) {
  258. try {
  259. const { updateData } = JSON.parse(req.body.data);
  260. await facade.editClassData(updateData);
  261. res.json({ error: 0, message: 'editClass success' });
  262. } catch (err) {
  263. console.log(err);
  264. res.json({ error: 1, message: err.toString() });
  265. }
  266. }
  267. // 匹配总表
  268. async matchSummary(req, res) {
  269. try {
  270. const { compilationID, libID } = JSON.parse(req.body.data);
  271. await facade.matchSummary(compilationID, libID);
  272. res.json({ error: 0, message: 'matchSummary success' });
  273. } catch (err) {
  274. console.log(err);
  275. res.json({ error: 1, message: err.toString() });
  276. }
  277. }
  278. }
  279. module.exports = {
  280. priceInfoController: new PriceInfoController()
  281. };