ration_repository_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * Created by Tony on 2017/4/20.
  3. */
  4. const mongoose = require('mongoose');
  5. var rationRepository = require("../models/repository_map");
  6. import baseController from "../../common/base/base_controller";
  7. import CompilationModel from "../../users/models/compilation_model";
  8. const gljMapModel = mongoose.model('std_glj_lib_gljList');
  9. import async from "async";
  10. var callback = function(req, res, err, message, data){
  11. res.json({error: err, message: message, data: data});
  12. };
  13. // 上传控件
  14. const multiparty = require("multiparty");
  15. const fs = require("fs");
  16. // excel解析
  17. const excel = require("node-xlsx");
  18. const rationItem = require("../models/ration_item");
  19. class RationRepositoryController extends baseController {
  20. async getCompilationList(req, res) {
  21. try {
  22. let compilationModel = new CompilationModel(), rst = [];
  23. let compilationList = await compilationModel.getCompilationList();
  24. if (compilationList.length <= 0) {
  25. throw '没有数据';
  26. }
  27. else {
  28. compilationList.forEach(function (compilation) {
  29. rst.push({_id: compilation._id, name: compilation.name});
  30. });
  31. //获得相关编办下的工料机库
  32. let parallelFucs = [];
  33. for (let i = 0; i < rst.length; i++) {
  34. parallelFucs.push((function (obj) {
  35. return function (cb) {
  36. gljMapModel.find({compilationId: obj._id, deleted: false}, function (err, result) {
  37. if (err) {
  38. cb(err);
  39. }
  40. else {
  41. cb(null, result);
  42. }
  43. })
  44. }
  45. })(rst[i]));
  46. }
  47. async.parallel(parallelFucs, function (err, result) {
  48. if (err) {
  49. callback(req, res, err, '没有数据', null);
  50. }
  51. else {
  52. let gljLibsRst = [];
  53. for (let i = 0; i < result.length; i++) {
  54. for (let j = 0; j < result[i].length; j++) {
  55. gljLibsRst.push(result[i][j]);
  56. }
  57. }
  58. callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  59. }
  60. })
  61. }
  62. }
  63. catch (err) {
  64. callback(req, res, err, '没有数据', null);
  65. }
  66. }
  67. addRationRepository(req, res) {
  68. var rationObj = JSON.parse(req.body.rationRepObj);
  69. rationRepository.addRationRepository(rationObj, function (err, data) {
  70. if (data) {
  71. callback(req, res, err, "has data", data);
  72. } else {
  73. callback(req, res, err, "no data", null);
  74. }
  75. })
  76. }
  77. getRationLib(req, res){
  78. let data = JSON.parse(req.body.data);
  79. let libId = data.libId;
  80. rationRepository.getRationLib(libId, function(err, data){
  81. if (data) {
  82. callback(req, res, err, "has data", data);
  83. } else {
  84. callback(req, res, err, "no data", null);
  85. }
  86. });
  87. }
  88. getDisPlayRationLibs(req, res) {
  89. rationRepository.getDisplayRationLibs(function (err, data) {
  90. if (data) {
  91. callback(req, res, err, "has data", data);
  92. } else {
  93. callback(req, res, err, "no data", null);
  94. }
  95. });
  96. }
  97. getRealLibName(req, res) {
  98. var libName = req.body.rationName;
  99. rationRepository.getRealLibName(libName, function (err, data) {
  100. if (data) {
  101. callback(req, res, err, "has data", data);
  102. } else {
  103. callback(req, res, err, "no data", null);
  104. }
  105. })
  106. }
  107. getLibIDByName(req, res) {
  108. rationRepository.getLibIDByName(req.body.libName, function (err, data) {
  109. if (data) {
  110. callback(req, res, err, "has ID", data);
  111. } else {
  112. callback(req, res, err, "no ID", null);
  113. }
  114. })
  115. }
  116. deleteRationLib(req, res) {
  117. let oprtor = req.body.oprtor,
  118. libId = req.body.libId;
  119. rationRepository.deleteRationLib(oprtor, libId, function (err, message) {
  120. callback(req, res, err, message, null);
  121. });
  122. }
  123. updateRationRepositoryName(req, res) {
  124. let oprtor = req.body.oprtor,
  125. renameObj = JSON.parse(req.body.renameObj);
  126. rationRepository.updateName(oprtor, renameObj, function (err, message) {
  127. callback(req, res, err, message, null);
  128. })
  129. }
  130. /**
  131. * 上传定额库原始数据
  132. *
  133. * @param {Object} request
  134. * @param {Object} response
  135. * @return {void}
  136. */
  137. async uploadSourceData(request, response) {
  138. let responseData = {
  139. err: 0,
  140. msg: ''
  141. };
  142. const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
  143. try {
  144. const uploadOption = {
  145. uploadDir: './public'
  146. };
  147. const form = new multiparty.Form(uploadOption);
  148. form.parse(request, async function(err, fields, files) {
  149. const rationRepId = fields.rationRepId !== undefined && fields.rationRepId.length > 0 ?
  150. fields.rationRepId[0] : 0;
  151. const type = fields.type !== undefined && fields.type.length > 0 ?
  152. fields.type[0] : '';
  153. if (rationRepId <= 0 || type === '') {
  154. throw '参数错误';
  155. }
  156. const file = files.file !== undefined ? files.file[0] : null;
  157. if (err || file === null) {
  158. throw '上传失败';
  159. }
  160. // 判断类型
  161. if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
  162. throw '不支持该类型';
  163. }
  164. // 重命名文件名
  165. const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
  166. fs.renameSync(file.path, uploadFullName);
  167. const sheet = excel.parse(uploadFullName);
  168. if (sheet[0] === undefined || sheet[0].data === undefined) {
  169. throw 'excel没有对应数据';
  170. }
  171. const result = type === 'source_file' ?
  172. await rationItem.batchAddFromExcel(rationRepId, sheet[0].data) :
  173. await rationItem.batchUpdateSectionIdFromExcel(sheet[0].data);
  174. if (rationItem.failGLJList.length > 0) {
  175. responseData.msg = rationItem.failGLJList.join("\r\n");
  176. }
  177. // 删除文件
  178. if (result) {
  179. fs.unlink(uploadFullName);
  180. }
  181. response.json(responseData);
  182. });
  183. } catch (error) {
  184. responseData.err = 1;
  185. responseData.msg = error;
  186. response.json(responseData);
  187. }
  188. return;
  189. }
  190. /**
  191. * 导出内容数据
  192. *
  193. * @param {Object} request
  194. * @param {Object} response
  195. * @return {void}
  196. */
  197. async exportRationData(request, response) {
  198. let rationRepId = request.query.rationRepId;
  199. rationRepId = parseInt(rationRepId);
  200. try {
  201. if (isNaN(rationRepId) || rationRepId <= 0) {
  202. throw '参数错误';
  203. }
  204. const excelData = await rationItem.exportExcelData(rationRepId);
  205. const buffer = excel.build([{name: "sheet", data: excelData}]);
  206. const filePath = './public/export.xlsx';
  207. fs.writeFileSync(filePath, buffer, 'binary');
  208. const stats = fs.statSync(filePath);
  209. // 下载相关header
  210. response.set({
  211. 'Content-Type': 'application/octet-stream',
  212. 'Content-Disposition': 'attachment; filename=ration.xlsx',
  213. 'Content-Length': stats.size
  214. });
  215. fs.createReadStream(filePath).pipe(response);
  216. fs.unlink(filePath);
  217. } catch (error) {
  218. response.end(error);
  219. }
  220. }
  221. }
  222. export default RationRepositoryController;
  223. /*
  224. module.exports = {
  225. addRationRepository:function(req,res){
  226. var rationObj = JSON.parse(req.body.rationRepObj);
  227. rationRepository.addRationRepository(rationObj,function(err,data){
  228. if (data) {
  229. callback(req, res, err, "has data", data);
  230. } else {
  231. callback(req, res, err, "no data", null);
  232. }
  233. })
  234. },
  235. getDisPlayRationLibs: function(req, res){
  236. rationRepository.getDisplayRationLibs(function(err, data){
  237. if (data) {
  238. callback(req, res, err, "has data",data);
  239. } else {
  240. callback(req, res, err, "no data", null);
  241. }
  242. });
  243. },
  244. getRealLibName:function(req,res){
  245. var libName = req.body.rationName;
  246. rationRepository.getRealLibName(libName,function(err,data){
  247. if (data) {
  248. callback(req, res, err, "has data", data);
  249. } else {
  250. callback(req, res, err, "no data", null);
  251. }
  252. })
  253. },
  254. getLibIDByName:function(req,res){
  255. rationRepository.getLibIDByName(req.body.libName, function(err,data){
  256. if (data) {
  257. callback(req, res, err, "has ID", data);
  258. } else {
  259. callback(req, res, err, "no ID", null);
  260. }
  261. })
  262. },
  263. deleteRationLib:function(req,res){
  264. var rationName = req.body.rationName;
  265. rationRepository.deleteRationLib(rationName,function(err,data){
  266. if (data) {
  267. callback(req, res, err, "has data", data);
  268. } else {
  269. callback(req, res, err, "no data", null);
  270. }
  271. })
  272. },
  273. updateRationRepositoryName: function(req, res) {
  274. var orgName = req.body.rationName;
  275. var newName = req.body.newName;
  276. rationRepository.updateName(orgName, newName, function(err, data){
  277. if (data) {
  278. callback(req, res, err, "has data", data);
  279. } else {
  280. callback(req, res, err, "no data", null);
  281. }
  282. });
  283. }
  284. }*/