ration_repository_controller.js 13 KB

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