ration_repository_controller.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/tmp'
  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. if (rationRepId <= 0) {
  151. throw '参数错误';
  152. }
  153. const file = files.file !== undefined ? files.file[0] : null;
  154. if (err || file === null) {
  155. throw '上传失败';
  156. }
  157. // 判断类型
  158. if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
  159. throw '不支持该类型';
  160. }
  161. // 重命名文件名
  162. const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
  163. fs.renameSync(file.path, uploadFullName);
  164. const sheet = excel.parse(uploadFullName);
  165. if (sheet[0] === undefined || sheet[0].data === undefined) {
  166. throw 'excel没有对应数据';
  167. }
  168. const result = await rationItem.batchAddFromExcel(rationRepId, sheet[0].data);
  169. // 删除文件
  170. if (result) {
  171. fs.unlink(uploadFullName, null);
  172. }
  173. response.json(responseData);
  174. });
  175. } catch (error) {
  176. responseData.err = 1;
  177. responseData.msg = error;
  178. response.json(responseData);
  179. }
  180. return;
  181. }
  182. /**
  183. * 导出内容数据
  184. *
  185. * @param {Object} request
  186. * @param {Object} response
  187. * @return {void}
  188. */
  189. async exportRationData(request, response) {
  190. let rationRepId = request.query.rationRepId;
  191. rationRepId = parseInt(rationRepId);
  192. try {
  193. if (isNaN(rationRepId) || rationRepId <= 0) {
  194. throw '参数错误';
  195. }
  196. const excelData = await rationItem.exportExcelData(rationRepId);
  197. const buffer = excel.build([{name: "sheet", data: excelData}]);
  198. const filePath = './public/tmp/export.xlsx';
  199. fs.writeFileSync(filePath, buffer, 'binary');
  200. const stats = fs.statSync(filePath);
  201. // 下载相关header
  202. response.set({
  203. 'Content-Type': 'application/octet-stream',
  204. 'Content-Disposition': 'attachment; filename=ration.xlsx',
  205. 'Content-Length': stats.size
  206. });
  207. fs.createReadStream(filePath).pipe(response);
  208. fs.unlink(filePath);
  209. } catch (error) {
  210. response.end(error);
  211. }
  212. }
  213. }
  214. export default RationRepositoryController;
  215. /*
  216. module.exports = {
  217. addRationRepository:function(req,res){
  218. var rationObj = JSON.parse(req.body.rationRepObj);
  219. rationRepository.addRationRepository(rationObj,function(err,data){
  220. if (data) {
  221. callback(req, res, err, "has data", data);
  222. } else {
  223. callback(req, res, err, "no data", null);
  224. }
  225. })
  226. },
  227. getDisPlayRationLibs: function(req, res){
  228. rationRepository.getDisplayRationLibs(function(err, data){
  229. if (data) {
  230. callback(req, res, err, "has data",data);
  231. } else {
  232. callback(req, res, err, "no data", null);
  233. }
  234. });
  235. },
  236. getRealLibName:function(req,res){
  237. var libName = req.body.rationName;
  238. rationRepository.getRealLibName(libName,function(err,data){
  239. if (data) {
  240. callback(req, res, err, "has data", data);
  241. } else {
  242. callback(req, res, err, "no data", null);
  243. }
  244. })
  245. },
  246. getLibIDByName:function(req,res){
  247. rationRepository.getLibIDByName(req.body.libName, function(err,data){
  248. if (data) {
  249. callback(req, res, err, "has ID", data);
  250. } else {
  251. callback(req, res, err, "no ID", null);
  252. }
  253. })
  254. },
  255. deleteRationLib:function(req,res){
  256. var rationName = req.body.rationName;
  257. rationRepository.deleteRationLib(rationName,function(err,data){
  258. if (data) {
  259. callback(req, res, err, "has data", data);
  260. } else {
  261. callback(req, res, err, "no data", null);
  262. }
  263. })
  264. },
  265. updateRationRepositoryName: function(req, res) {
  266. var orgName = req.body.rationName;
  267. var newName = req.body.newName;
  268. rationRepository.updateName(orgName, newName, function(err, data){
  269. if (data) {
  270. callback(req, res, err, "has data", data);
  271. } else {
  272. callback(req, res, err, "no data", null);
  273. }
  274. });
  275. }
  276. }*/