labour_coe_facade.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Created by CSL on 2017-10-10.
  3. */
  4. const uuidV1 = require('uuid/v1');
  5. let logger = require("../../../logs/log_helper").logger;
  6. let mongoose = require('mongoose');
  7. let stdLabourCoesModel = mongoose.model('std_labour_coes');
  8. let projectLabourCoesModel = mongoose.model('labour_coes');
  9. let EngineeringLibModel = require("../../users/models/engineering_lib_model");
  10. let _=require("lodash");
  11. let consts = require('../models/project_consts');
  12. let projectConsts = consts.projectConst;
  13. module.exports = {
  14. newProjectLabourCoe: newProjectLabourCoe,
  15. getProjectLabourCoe: getProjectLabourCoe,
  16. getStdLabourCoe: getStdLabourCoe,
  17. getData: getData,
  18. save: save
  19. };
  20. async function newProjectLabourCoe(data) {
  21. logger.info(`Create new LabourCoe file for project : ${data.ID}`);
  22. let valid_LC_libs = [];
  23. let egnrID = data.property.engineering_id;
  24. let egnrModel = new EngineeringLibModel();
  25. let egnr = await egnrModel.getEngineering(egnrID);
  26. if(egnr){ valid_LC_libs = egnr._doc.artificial_lib };
  27. // 绑定多个人工系数标准文件时,默认取第一个作为标准模板。
  28. let stdLC = await getStdLabourCoe(valid_LC_libs[0].id);
  29. let doc={
  30. ID: uuidV1(),
  31. projectID: data.ID,
  32. name: data.name,
  33. libID: stdLC.ID,
  34. libName: stdLC.libName,
  35. coes: stdLC.coes
  36. };
  37. await projectLabourCoesModel.create(doc);
  38. let newLC = {ID: doc.ID, name: doc.name};
  39. return newLC;
  40. };
  41. async function getProjectLabourCoe(ID) {
  42. let projLC = await projectLabourCoesModel.findOne({ID:ID});
  43. return projLC;
  44. };
  45. async function getStdLabourCoe(libID) {
  46. let stdLC = await stdLabourCoesModel.findOne({ID:libID});
  47. return stdLC;
  48. };
  49. // 统一的 getData() 方法供project调用
  50. function getData (projectID, callback) {
  51. projectLabourCoesModel.findOne({projectID: projectID}, '-_id', function(err, datas) {
  52. if (!err) {
  53. callback(0, projectConsts.LABOUR_COE, datas);
  54. } else {
  55. callback(1, projectConsts.LABOUR_COE, null);
  56. };
  57. });
  58. };
  59. // 统一的 save() 方法供project调用
  60. function save (user_id, datas, callback) {
  61. projectLabourCoesModel.update({"projectID": 553}, {"libName":"goo"}, callback(null, {data: 'ok'}));
  62. }