ration_repository_controller.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 libId = req.body.libId;
  78. rationRepository.getRationLib(libId, function (err, data) {
  79. if (data) {
  80. callback(req, res, err, "has data", data);
  81. } else {
  82. callback(req, res, err, "no data", null);
  83. }
  84. });
  85. }
  86. getDisPlayRationLibs(req, res) {
  87. rationRepository.getDisplayRationLibs(function (err, data) {
  88. if (data) {
  89. callback(req, res, err, "has data", data);
  90. } else {
  91. callback(req, res, err, "no data", null);
  92. }
  93. });
  94. }
  95. getRealLibName(req, res) {
  96. var libName = req.body.rationName;
  97. rationRepository.getRealLibName(libName, function (err, data) {
  98. if (data) {
  99. callback(req, res, err, "has data", data);
  100. } else {
  101. callback(req, res, err, "no data", null);
  102. }
  103. })
  104. }
  105. getLibIDByName(req, res) {
  106. rationRepository.getLibIDByName(req.body.libName, function (err, data) {
  107. if (data) {
  108. callback(req, res, err, "has ID", data);
  109. } else {
  110. callback(req, res, err, "no ID", null);
  111. }
  112. })
  113. }
  114. deleteRationLib(req, res) {
  115. let oprtor = req.body.oprtor,
  116. libId = req.body.libId;
  117. rationRepository.deleteRationLib(oprtor, libId, function (err, message) {
  118. callback(req, res, err, message, null);
  119. });
  120. }
  121. updateRationRepositoryName(req, res) {
  122. let oprtor = req.body.oprtor,
  123. renameObj = JSON.parse(req.body.renameObj);
  124. rationRepository.updateName(oprtor, renameObj, function (err, message) {
  125. callback(req, res, err, message, null);
  126. })
  127. }
  128. /**
  129. * 上传定额库原始数据
  130. *
  131. * @param {Object} request
  132. * @param {Object} response
  133. * @return {void}
  134. */
  135. async uploadSourceData(request, response) {
  136. let responseData = {
  137. err: 0,
  138. msg: ''
  139. };
  140. const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
  141. try {
  142. const uploadOption = {
  143. uploadDir: './public/tmp'
  144. };
  145. const form = new multiparty.Form(uploadOption);
  146. form.parse(request, function(err, fields, files) {
  147. const rationRepId = fields.rationRepId !== undefined && fields.rationRepId.length > 0 ?
  148. fields.rationRepId[0] : 0;
  149. if (rationRepId <= 0) {
  150. throw '参数错误';
  151. }
  152. const file = files.file !== undefined ? files.file[0] : null;
  153. if (err || file === null) {
  154. throw '上传失败';
  155. }
  156. // 判断类型
  157. if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
  158. throw '不支持该类型';
  159. }
  160. // 重命名文件名
  161. const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
  162. fs.renameSync(file.path, uploadFullName);
  163. const sheet = excel.parse(uploadFullName);
  164. if (sheet[0] === undefined || sheet[0].data === undefined) {
  165. throw 'excel没有对应数据';
  166. }
  167. rationItem.batchAddFromExcel(rationRepId, sheet[0].data);
  168. response.json(responseData);
  169. });
  170. } catch (error) {
  171. responseData.err = 1;
  172. responseData.msg = error;
  173. response.json(responseData);
  174. }
  175. return;
  176. }
  177. }
  178. export default RationRepositoryController;
  179. /*
  180. module.exports = {
  181. addRationRepository:function(req,res){
  182. var rationObj = JSON.parse(req.body.rationRepObj);
  183. rationRepository.addRationRepository(rationObj,function(err,data){
  184. if (data) {
  185. callback(req, res, err, "has data", data);
  186. } else {
  187. callback(req, res, err, "no data", null);
  188. }
  189. })
  190. },
  191. getDisPlayRationLibs: function(req, res){
  192. rationRepository.getDisplayRationLibs(function(err, data){
  193. if (data) {
  194. callback(req, res, err, "has data",data);
  195. } else {
  196. callback(req, res, err, "no data", null);
  197. }
  198. });
  199. },
  200. getRealLibName:function(req,res){
  201. var libName = req.body.rationName;
  202. rationRepository.getRealLibName(libName,function(err,data){
  203. if (data) {
  204. callback(req, res, err, "has data", data);
  205. } else {
  206. callback(req, res, err, "no data", null);
  207. }
  208. })
  209. },
  210. getLibIDByName:function(req,res){
  211. rationRepository.getLibIDByName(req.body.libName, function(err,data){
  212. if (data) {
  213. callback(req, res, err, "has ID", data);
  214. } else {
  215. callback(req, res, err, "no ID", null);
  216. }
  217. })
  218. },
  219. deleteRationLib:function(req,res){
  220. var rationName = req.body.rationName;
  221. rationRepository.deleteRationLib(rationName,function(err,data){
  222. if (data) {
  223. callback(req, res, err, "has data", data);
  224. } else {
  225. callback(req, res, err, "no data", null);
  226. }
  227. })
  228. },
  229. updateRationRepositoryName: function(req, res) {
  230. var orgName = req.body.rationName;
  231. var newName = req.body.newName;
  232. rationRepository.updateName(orgName, newName, function(err, data){
  233. if (data) {
  234. callback(req, res, err, "has data", data);
  235. } else {
  236. callback(req, res, err, "no data", null);
  237. }
  238. });
  239. }
  240. }*/