ration_repository_controller.js 11 KB

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