ration_repository_controller.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. class RationRepositoryController extends baseController {
  18. async getCompilationList(req, res) {
  19. try {
  20. let compilationModel = new CompilationModel(), rst = [];
  21. let compilationList = await compilationModel.getCompilationList();
  22. if (compilationList.length <= 0) {
  23. throw '没有数据';
  24. }
  25. else {
  26. compilationList.forEach(function (compilation) {
  27. rst.push({_id: compilation._id, name: compilation.name});
  28. });
  29. //获得相关编办下的工料机库
  30. let parallelFucs = [];
  31. for (let i = 0; i < rst.length; i++) {
  32. parallelFucs.push((function (obj) {
  33. return function (cb) {
  34. gljMapModel.find({compilationId: obj._id, deleted: false}, function (err, result) {
  35. if (err) {
  36. cb(err);
  37. }
  38. else {
  39. cb(null, result);
  40. }
  41. })
  42. }
  43. })(rst[i]));
  44. }
  45. async.parallel(parallelFucs, function (err, result) {
  46. if (err) {
  47. callback(req, res, err, '没有数据', null);
  48. }
  49. else {
  50. let gljLibsRst = [];
  51. for (let i = 0; i < result.length; i++) {
  52. for (let j = 0; j < result[i].length; j++) {
  53. gljLibsRst.push(result[i][j]);
  54. }
  55. }
  56. callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  57. }
  58. })
  59. }
  60. }
  61. catch (err) {
  62. callback(req, res, err, '没有数据', null);
  63. }
  64. }
  65. addRationRepository(req, res) {
  66. var rationObj = JSON.parse(req.body.rationRepObj);
  67. rationRepository.addRationRepository(rationObj, function (err, data) {
  68. if (data) {
  69. callback(req, res, err, "has data", data);
  70. } else {
  71. callback(req, res, err, "no data", null);
  72. }
  73. })
  74. }
  75. getRationLib(req, res) {
  76. let libId = req.body.libId;
  77. rationRepository.getRationLib(libId, function (err, data) {
  78. if (data) {
  79. callback(req, res, err, "has data", data);
  80. } else {
  81. callback(req, res, err, "no data", null);
  82. }
  83. });
  84. }
  85. getDisPlayRationLibs(req, res) {
  86. rationRepository.getDisplayRationLibs(function (err, data) {
  87. if (data) {
  88. callback(req, res, err, "has data", data);
  89. } else {
  90. callback(req, res, err, "no data", null);
  91. }
  92. });
  93. }
  94. getRealLibName(req, res) {
  95. var libName = req.body.rationName;
  96. rationRepository.getRealLibName(libName, function (err, data) {
  97. if (data) {
  98. callback(req, res, err, "has data", data);
  99. } else {
  100. callback(req, res, err, "no data", null);
  101. }
  102. })
  103. }
  104. getLibIDByName(req, res) {
  105. rationRepository.getLibIDByName(req.body.libName, function (err, data) {
  106. if (data) {
  107. callback(req, res, err, "has ID", data);
  108. } else {
  109. callback(req, res, err, "no ID", null);
  110. }
  111. })
  112. }
  113. deleteRationLib(req, res) {
  114. let oprtor = req.body.oprtor,
  115. libId = req.body.libId;
  116. rationRepository.deleteRationLib(oprtor, libId, function (err, message) {
  117. callback(req, res, err, message, null);
  118. });
  119. }
  120. updateRationRepositoryName(req, res) {
  121. let oprtor = req.body.oprtor,
  122. renameObj = JSON.parse(req.body.renameObj);
  123. rationRepository.updateName(oprtor, renameObj, function (err, message) {
  124. callback(req, res, err, message, null);
  125. })
  126. }
  127. /**
  128. * 上传定额库原始数据
  129. *
  130. * @param {Object} request
  131. * @param {Object} response
  132. * @return {void}
  133. */
  134. async uploadSourceData(request, response) {
  135. const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
  136. try {
  137. const uploadOption = {
  138. uploadDir: './public/tmp'
  139. };
  140. const form = new multiparty.Form(uploadOption);
  141. form.parse(request, function(err, fields, files) {
  142. const file = files.source_file !== undefined ? files.source_file[0] : null;
  143. if (err || file === null) {
  144. throw '上传失败';
  145. }
  146. // 判断类型
  147. if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
  148. throw '不支持该类型';
  149. }
  150. // 重命名文件名
  151. const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
  152. fs.renameSync(file.path, uploadFullName);
  153. const test = excel.parse(uploadFullName);
  154. console.log(test[0].data);
  155. });
  156. } catch (error) {
  157. console.log(error);
  158. }
  159. return;
  160. }
  161. }
  162. export default RationRepositoryController;
  163. /*
  164. module.exports = {
  165. addRationRepository:function(req,res){
  166. var rationObj = JSON.parse(req.body.rationRepObj);
  167. rationRepository.addRationRepository(rationObj,function(err,data){
  168. if (data) {
  169. callback(req, res, err, "has data", data);
  170. } else {
  171. callback(req, res, err, "no data", null);
  172. }
  173. })
  174. },
  175. getDisPlayRationLibs: function(req, res){
  176. rationRepository.getDisplayRationLibs(function(err, data){
  177. if (data) {
  178. callback(req, res, err, "has data",data);
  179. } else {
  180. callback(req, res, err, "no data", null);
  181. }
  182. });
  183. },
  184. getRealLibName:function(req,res){
  185. var libName = req.body.rationName;
  186. rationRepository.getRealLibName(libName,function(err,data){
  187. if (data) {
  188. callback(req, res, err, "has data", data);
  189. } else {
  190. callback(req, res, err, "no data", null);
  191. }
  192. })
  193. },
  194. getLibIDByName:function(req,res){
  195. rationRepository.getLibIDByName(req.body.libName, function(err,data){
  196. if (data) {
  197. callback(req, res, err, "has ID", data);
  198. } else {
  199. callback(req, res, err, "no ID", null);
  200. }
  201. })
  202. },
  203. deleteRationLib:function(req,res){
  204. var rationName = req.body.rationName;
  205. rationRepository.deleteRationLib(rationName,function(err,data){
  206. if (data) {
  207. callback(req, res, err, "has data", data);
  208. } else {
  209. callback(req, res, err, "no data", null);
  210. }
  211. })
  212. },
  213. updateRationRepositoryName: function(req, res) {
  214. var orgName = req.body.rationName;
  215. var newName = req.body.newName;
  216. rationRepository.updateName(orgName, newName, function(err, data){
  217. if (data) {
  218. callback(req, res, err, "has data", data);
  219. } else {
  220. callback(req, res, err, "no data", null);
  221. }
  222. });
  223. }
  224. }*/