ration_repository_controller.js 10 KB

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