coe.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Created by CSL on 2017/5/3.
  3. * 系数表。
  4. */
  5. var mongoose = require("mongoose");
  6. var dbm = require("../../../config/db/db_manager");
  7. var db = dbm.getCfgConnection("rationRepository");
  8. var counter = require('../../../public/counter/counter');
  9. var coeSchema = mongoose.Schema({
  10. coeType: String, // 系数类型,指作用范围:
  11. // 单个(如:111量0.001)、人工类、材料类、机械类、全部(如:定额×0.925)。
  12. gljID: Number, // 要调整的工料机ID(当coeType=0时有效)
  13. operator: String, // 运算符(*、+、-、=)
  14. amount: String, // 调整的量
  15. _id: false
  16. });
  17. var coeListSchema = mongoose.Schema({
  18. libID: Number, // 所属定额定ID
  19. ID: Number, // 系数ID(流水号ID)
  20. name: String, // 名称
  21. content: String, // 说明
  22. coes: [coeSchema]
  23. });
  24. var coeListModel = db.model("coeLists",coeListSchema, "coeLists")
  25. var coeListDAO = function(){};
  26. coeListDAO.prototype.getCoe = function (data, callback) {
  27. coeListModel.findOne({
  28. "libID": data.libID,
  29. "ID": data.ID,
  30. "$or": [{"isDeleted": null}, {"isDeleted": false}]
  31. },
  32. function (err, doc) {
  33. if (err) callback(true, "获取系数明细错误!", "")
  34. else callback(false, "获取系数明细成功", doc);
  35. })
  36. };
  37. coeListDAO.prototype.getCoesByLibID = function (libID, callback) {
  38. coeListModel.find({ "libID": libID },
  39. function (err, doc) {
  40. if (err) callback("获取定额库系数表错误", null)
  41. else callback(null, doc);
  42. })
  43. };
  44. coeListDAO.prototype.saveToCoeList = function(data, callback) {
  45. var me = this;
  46. console.log(data);
  47. if (data.addArr.length > 0) {
  48. me.addItems(data.addArr, callback);
  49. };
  50. //if (data.deleteArr.length > 0) {
  51. // me.deleteItems(data.deleteArr, callback);
  52. //};
  53. if (data.updateArr.length > 0) {
  54. me.updateItems(data.updateArr, callback);
  55. };
  56. };
  57. coeListDAO.prototype.addItems = function(addArr, callback) {
  58. if (addArr && addArr.length > 0) {
  59. counter.counterDAO.getIDAfterCount(counter.moduleName.coeList, addArr.length, function(err, result){
  60. var maxId = result.value.sequence_value;
  61. console.log(maxId);
  62. for (var i = 0; i < addArr.length; i++) {
  63. var obj = new coeListModel(addArr[i]);
  64. obj.ID = (maxId - (addArr.length - 1) + i);
  65. coeListModel.create(obj, function(err) {
  66. if (err) {
  67. callback(true, "add fail", null);
  68. } else {
  69. callback(false, "add success", obj.ID);
  70. };
  71. });
  72. };
  73. });
  74. } else {
  75. callback(true, "no source", null);
  76. };
  77. };
  78. coeListDAO.prototype.updateItems = function(updateArr, callback) {
  79. if (updateArr && updateArr.length > 0) {
  80. for (var i = 0; i < updateArr.length; i++) {
  81. var obj = updateArr[i];
  82. coeListModel.update({"libID": obj.libID, "ID": obj.ID}, updateArr[i], function(err) {
  83. if (err) {
  84. callback(true, "update fail", null);
  85. } else {
  86. callback(false, "update success", obj.ID);
  87. };
  88. });
  89. };
  90. } else {
  91. callback(true, "no source", null);
  92. };
  93. };
  94. module.exports = new coeListDAO();