ration_repository_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // 导入原始数据
  187. await rationItem.batchAddFromExcel(rationRepId, sheet[0].data) :
  188. // 导入内部数据
  189. await rationItem.batchUpdateSectionIdFromExcel(sheet[0].data);
  190. if (rationItem.failGLJList && rationItem.failGLJList.length > 0) {
  191. responseData.msg = rationItem.failGLJList.join("\r\n");
  192. rationItem.failGLJList = [];
  193. }
  194. // 删除文件
  195. if(uploadFullName && fs.existsSync(uploadFullName)){
  196. fs.unlink(uploadFullName);
  197. }
  198. response.json(responseData);
  199. }
  200. catch (error){
  201. console.log(error);
  202. if(uploadFullName && fs.existsSync(uploadFullName)){
  203. fs.unlink(uploadFullName);
  204. }
  205. responseData.err = 1;
  206. responseData.msg = error;
  207. response.json(responseData);
  208. }
  209. });
  210. return;
  211. }
  212. /**
  213. * 导出内部数据
  214. *
  215. * @param {Object} request
  216. * @param {Object} response
  217. * @return {void}
  218. */
  219. async exportRationData(request, response) {
  220. let rationRepId = request.query.rationRepId;
  221. rationRepId = parseInt(rationRepId);
  222. try {
  223. if (isNaN(rationRepId) || rationRepId <= 0) {
  224. throw '参数错误';
  225. }
  226. const excelData = await rationItem.exportExcelData(rationRepId);
  227. const buffer = excel.build([{name: "sheet", data: excelData}]);
  228. const filePath = './public/export.xlsx';
  229. fs.writeFileSync(filePath, buffer, 'binary');
  230. const stats = fs.statSync(filePath);
  231. // 下载相关header
  232. response.set({
  233. 'Content-Type': 'application/octet-stream',
  234. 'Content-Disposition': 'attachment; filename=ration.xlsx',
  235. 'Content-Length': stats.size
  236. });
  237. fs.createReadStream(filePath).pipe(response);
  238. fs.unlink(filePath);
  239. } catch (error) {
  240. response.end(error);
  241. }
  242. }
  243. //一键重新计算所有定额数据
  244. async reCalcAll(req, res){
  245. try{
  246. let data = JSON.parse(req.body.data);
  247. let rationRepId = data.rationRepId;
  248. let rationLib = await rationLibModel.findOne({ID: rationRepId, $or: [{deleted: null},{deleted: false}]});
  249. if(!rationLib){
  250. throw '不存在此定额库';
  251. }
  252. let task = [];
  253. // 获取标准工料机库数据
  254. const stdBillsLibListsModel = new STDGLJListModel();
  255. const stdGLJData = await stdBillsLibListsModel.getGljItemsByRepId(rationLib.gljLib);
  256. // 整理标准工料机库数据
  257. let stdGLJList = {};
  258. let stdGLJListByID = {};
  259. for (const tmp of stdGLJData) {
  260. if (tmp.priceProperty && tmp.priceProperty.price1) {
  261. tmp.basePrice = tmp.priceProperty.price1;
  262. }
  263. stdGLJList[tmp.code.toString()] = tmp.ID;
  264. stdGLJListByID[tmp.ID] = tmp;
  265. }
  266. //获取所有的定额
  267. let allRations = await rationItemModel.find({rationRepId: rationRepId, $or: [{isDeleted: false}, {isDeleted: null}]});
  268. for(let ration of allRations){
  269. rationItem.calcForRation(stdGLJListByID, ration);
  270. task.push({
  271. updateOne: {
  272. filter: {ID: ration.ID},
  273. update: {$set: {basePrice: ration.basePrice}}
  274. }
  275. });
  276. }
  277. if(task.length > 0){
  278. await rationItemModel.bulkWrite(task);
  279. }
  280. res.json({error: 0, message: 'success', data: null});
  281. }
  282. catch (err){
  283. res.json({error: 1, message: err, data: null});
  284. }
  285. }
  286. }
  287. export default RationRepositoryController;
  288. /*
  289. module.exports = {
  290. addRationRepository:function(req,res){
  291. var rationObj = JSON.parse(req.body.rationRepObj);
  292. rationRepository.addRationRepository(rationObj,function(err,data){
  293. if (data) {
  294. callback(req, res, err, "has data", data);
  295. } else {
  296. callback(req, res, err, "no data", null);
  297. }
  298. })
  299. },
  300. getDisPlayRationLibs: function(req, res){
  301. rationRepository.getDisplayRationLibs(function(err, data){
  302. if (data) {
  303. callback(req, res, err, "has data",data);
  304. } else {
  305. callback(req, res, err, "no data", null);
  306. }
  307. });
  308. },
  309. getRealLibName:function(req,res){
  310. var libName = req.body.rationName;
  311. rationRepository.getRealLibName(libName,function(err,data){
  312. if (data) {
  313. callback(req, res, err, "has data", data);
  314. } else {
  315. callback(req, res, err, "no data", null);
  316. }
  317. })
  318. },
  319. getLibIDByName:function(req,res){
  320. rationRepository.getLibIDByName(req.body.libName, function(err,data){
  321. if (data) {
  322. callback(req, res, err, "has ID", data);
  323. } else {
  324. callback(req, res, err, "no ID", null);
  325. }
  326. })
  327. },
  328. deleteRationLib:function(req,res){
  329. var rationName = req.body.rationName;
  330. rationRepository.deleteRationLib(rationName,function(err,data){
  331. if (data) {
  332. callback(req, res, err, "has data", data);
  333. } else {
  334. callback(req, res, err, "no data", null);
  335. }
  336. })
  337. },
  338. updateRationRepositoryName: function(req, res) {
  339. var orgName = req.body.rationName;
  340. var newName = req.body.newName;
  341. rationRepository.updateName(orgName, newName, function(err, data){
  342. if (data) {
  343. callback(req, res, err, "has data", data);
  344. } else {
  345. callback(req, res, err, "no data", null);
  346. }
  347. });
  348. }
  349. }*/