ration_repository_controller.js 14 KB

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