ration.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Created by jimiz on 2017/4/1.
  3. */
  4. var mongoose = require("mongoose");
  5. var db = require("../db/project_db");
  6. var subSchema = require("./billsSubSchemas");
  7. var Schema = mongoose.Schema;
  8. var rationSchema = new Schema({
  9. ID: Number,
  10. projectID: Number,
  11. billsItemID: Number,
  12. serialNo: Number,
  13. libID: Number,
  14. code: String,
  15. name: String,
  16. maskName: String,
  17. unit: String,
  18. quantity: String, // Decimal
  19. programId: Number,
  20. content: String,
  21. rationProjName: String,
  22. comments: String,
  23. // 费用字段
  24. fees: [subSchema.feesSchema],
  25. // 标记字段
  26. flags: [subSchema.flagsSchema]
  27. });
  28. var ration = db.model("rations", rationSchema);
  29. var rationDAO = function(){};
  30. rationDAO.prototype.getData = function(projectId, callback){
  31. rations.find({projectID: projectId}, function(err, datas){
  32. if (!err) {
  33. callback(0, '', datas);
  34. } else {
  35. callback(1, '', null);
  36. }
  37. });
  38. };
  39. rationDAO.prototype.save = function(projectId, datas, callback){
  40. var data, errList = [], updateLength = 0;
  41. var updateFunc = function (err, errData) {
  42. if (err){
  43. errList.push(errData);
  44. };
  45. };
  46. if (datas){
  47. for (var i = 0; i < datas.length; i++){
  48. data = datas[i];
  49. if (data.updateType === 'update') {
  50. delete data.updateType;
  51. data.save(updateFunc);
  52. } else if (data.updateType === 'create') {
  53. delete data.updateType;
  54. data.save(updateFunc);
  55. } else if (data.updateType === 'delete') {
  56. delete data.updateType;
  57. data.remove(updateFunc);
  58. };
  59. };
  60. if (errList.length > 0){
  61. callback(1, 'update error.', errList);
  62. } else {
  63. callback(0, '', null);
  64. };
  65. };
  66. };
  67. rationDAO.prototype.getItemTemplate = function(callback){
  68. var data = new ration;
  69. /* to do: 需要根据标准配置库填充fees和flags字段,是否需要更多的参数? */
  70. callback(0, '', data);
  71. };
  72. const
  73. IDStep = 50, IDModule = 'rations';
  74. rationDAO.prototype.allocIDs = function(IDStep, callback){
  75. counter.counterDAO.getIDAfterCount(counter.moduleName.ration, IDStep, function(err, highID){
  76. var lowID = highID - IDStep + 1;
  77. callback(0, '', {lowID: lowID, highID: highID});
  78. });
  79. };
  80. module.exports = new rationDAO();