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 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, 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. rationItem.batchAddFromExcel(rationRepId, sheet[0].data);
  169. response.json(responseData);
  170. });
  171. } catch (error) {
  172. responseData.err = 1;
  173. responseData.msg = error;
  174. response.json(responseData);
  175. }
  176. return;
  177. }
  178. }
  179. export default RationRepositoryController;
  180. /*
  181. module.exports = {
  182. addRationRepository:function(req,res){
  183. var rationObj = JSON.parse(req.body.rationRepObj);
  184. rationRepository.addRationRepository(rationObj,function(err,data){
  185. if (data) {
  186. callback(req, res, err, "has data", data);
  187. } else {
  188. callback(req, res, err, "no data", null);
  189. }
  190. })
  191. },
  192. getDisPlayRationLibs: function(req, res){
  193. rationRepository.getDisplayRationLibs(function(err, data){
  194. if (data) {
  195. callback(req, res, err, "has data",data);
  196. } else {
  197. callback(req, res, err, "no data", null);
  198. }
  199. });
  200. },
  201. getRealLibName:function(req,res){
  202. var libName = req.body.rationName;
  203. rationRepository.getRealLibName(libName,function(err,data){
  204. if (data) {
  205. callback(req, res, err, "has data", data);
  206. } else {
  207. callback(req, res, err, "no data", null);
  208. }
  209. })
  210. },
  211. getLibIDByName:function(req,res){
  212. rationRepository.getLibIDByName(req.body.libName, function(err,data){
  213. if (data) {
  214. callback(req, res, err, "has ID", data);
  215. } else {
  216. callback(req, res, err, "no ID", null);
  217. }
  218. })
  219. },
  220. deleteRationLib:function(req,res){
  221. var rationName = req.body.rationName;
  222. rationRepository.deleteRationLib(rationName,function(err,data){
  223. if (data) {
  224. callback(req, res, err, "has data", data);
  225. } else {
  226. callback(req, res, err, "no data", null);
  227. }
  228. })
  229. },
  230. updateRationRepositoryName: function(req, res) {
  231. var orgName = req.body.rationName;
  232. var newName = req.body.newName;
  233. rationRepository.updateName(orgName, newName, function(err, data){
  234. if (data) {
  235. callback(req, res, err, "has data", data);
  236. } else {
  237. callback(req, res, err, "no data", null);
  238. }
  239. });
  240. }
  241. }*/