ration_repository_controller.js 13 KB

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