ration_repository_controller.js 14 KB

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