ration_glj_controller.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Created by chen on 2017/6/29.
  3. */
  4. let mongoose = require("mongoose")
  5. let ration_glj_facade = require('../facade/ration_glj_facade')
  6. const EngineeringLibModel = require("../../users/models/engineering_lib_model");
  7. let logger = require("../../../logs/log_helper").logger;
  8. const { COMPILATION, COMPLEMENTARY_LIB } = require('../../../public/common_constants');
  9. module.exports={
  10. createRationGLJ:createRationGLJ,
  11. testGetQuantify:testGetQuantify,
  12. getGLJData:getGLJData,
  13. getGLJDataPaging: getGLJDataPaging,
  14. getGLJDataByCodes:getGLJDataByCodes,
  15. addGLJ:addGLJ,
  16. replaceGLJ:replaceGLJ,
  17. mReplaceGLJ:mReplaceGLJ,
  18. updateRationGLJByEdit:updateRationGLJByEdit,
  19. getGLJClass:getGLJClass
  20. }
  21. function createRationGLJ() {
  22. let gls = mongoose.model('ration_glj');
  23. gls.create({'GLJID':1,'basePrice':23.23,'name':"testgls"},function(err, gls){
  24. console.log(gls)
  25. })
  26. }
  27. //测试获取消耗量:
  28. async function testGetQuantify() {
  29. var condition = {
  30. projectID:99,
  31. projectGLJIDList:[60,61,62]
  32. }
  33. try{
  34. let result = await ration_glj_facade.getQuantityByProjectGLJ(condition);
  35. console.log(result)
  36. }catch (err){
  37. console.log(err);
  38. }
  39. }
  40. async function getGLJDataByCodes(req,res){
  41. let result={
  42. error:0
  43. }
  44. try {
  45. let data = req.body.data;
  46. data = JSON.parse(data);
  47. let datas= await ration_glj_facade.getGLJDataByCodes(data,req.session.sessionCompilation);
  48. result.data=datas;
  49. }catch (err){
  50. logger.err(err);
  51. result.error=1;
  52. result.message = err.message;
  53. }
  54. res.json(result);
  55. }
  56. async function getGLJDataPaging(req, res) {
  57. let result={
  58. error:0
  59. };
  60. let { condition } = JSON.parse(req.body.data);
  61. try {
  62. const compilationId = req.session.sessionCompilation._id;
  63. const userID = req.session.sessionUser.id;
  64. let libData = null;
  65. if (condition.gljLibID !== COMPLEMENTARY_LIB) {
  66. condition.gljLibID = +condition.gljLibID;
  67. }
  68. if (condition.initLibs) {
  69. libData = await ration_glj_facade.getLibOptionsForCompilation(compilationId);
  70. libData.push({ name: '补充工料机', gljLibId: COMPLEMENTARY_LIB });
  71. if (condition.gljLibID) { // 替换人材机初始化会触发此条件(初始化库且定位了库)
  72. const orgDefalutLib = libData.find(lib => lib.isDefault);
  73. const newDefaultLib = libData.find(lib => lib.gljLibId === condition.gljLibID);
  74. if (orgDefalutLib && newDefaultLib) {
  75. orgDefalutLib.isDefault = false;
  76. newDefaultLib.isDefault = true;
  77. }
  78. }
  79. }
  80. if (!condition.gljLibID && libData) {
  81. condition.gljLibID = (libData.find(lib => lib.isDefault) || {}).gljLibId;
  82. }
  83. const info = condition.gljLibID === COMPLEMENTARY_LIB
  84. ? { gljLibId: null, userID, compilationId }
  85. : { gljLibId: condition.gljLibID, userID: null, compilationId: null };
  86. result.data = await ration_glj_facade.getGLJDataPaging(info, condition);
  87. if (req.session.sessionCompilation.priceProperties) {
  88. result.data.priceProperties = req.session.sessionCompilation.priceProperties
  89. }
  90. result.data.libData = libData;
  91. res.json(result);
  92. } catch (err) {
  93. logger.err(err);
  94. result.error=1;
  95. result.message = err.message;
  96. res.json(result);
  97. }
  98. }
  99. /* async function getGLJData(req, res) {
  100. let result={
  101. error:0
  102. }
  103. try {
  104. let info = await ration_glj_facade.getLibInfo(req);
  105. ration_glj_facade.getGLJData(info,function (err,datas) {
  106. if(err){
  107. result.error=1;
  108. result.message = err.message;
  109. }else {
  110. //多单价字段
  111. if(req.session.sessionCompilation.priceProperties) datas.priceProperties = req.session.sessionCompilation.priceProperties;
  112. result.datas = datas;
  113. }
  114. res.json(result);
  115. });
  116. }catch (err){
  117. logger.err(err);
  118. result.error=1;
  119. result.message = err.message;
  120. res.json(result);
  121. }
  122. } */
  123. async function getGLJData(req, res) {
  124. let result={
  125. error:0
  126. }
  127. try {
  128. let libData = null;
  129. let { engineerID, gljLibId, isInitial } = req.params;
  130. if (gljLibId !== COMPLEMENTARY_LIB) {
  131. gljLibId = +gljLibId;
  132. }
  133. isInitial = JSON.parse(isInitial);
  134. if (!gljLibId || isInitial) { // 替换人材机的话,可能存在gljLibID,但是是初始化的操作
  135. libData = engineerID === COMPILATION
  136. ? await ration_glj_facade.getLibOptionsForCompilation(req.session.sessionCompilation._id)
  137. : await ration_glj_facade.getLibOptions(engineerID);
  138. libData.push({ name: '补充工料机', gljLibId: COMPLEMENTARY_LIB });
  139. if (gljLibId) { // 替换人材机初始化会触发此条件
  140. const orgDefalutLib = libData.find(lib => lib.isDefault);
  141. const newDefaultLib = libData.find(lib => lib.gljLibId === gljLibId);
  142. if (orgDefalutLib && newDefaultLib) {
  143. orgDefalutLib.isDefault = false;
  144. newDefaultLib.isDefault = true;
  145. }
  146. }
  147. }
  148. let skipGLJ = gljLibId === 0;
  149. if (!gljLibId && libData) {
  150. gljLibId = (libData.find(lib => lib.isDefault) || {}).gljLibId;
  151. }
  152. const info = gljLibId === COMPLEMENTARY_LIB
  153. ? { gljLibId: null, userID: req.session.sessionUser.id, compilationId: req.session.sessionCompilation._id }
  154. : { gljLibId, userID: null, compilationId: null };
  155. info.skipGLJ = skipGLJ;
  156. ration_glj_facade.getGLJData(info,function (err,datas) {
  157. if(err){
  158. result.error=1;
  159. result.message = err.message;
  160. }else {
  161. //多单价字段
  162. if(req.session.sessionCompilation.priceProperties) datas.priceProperties = req.session.sessionCompilation.priceProperties;
  163. result.datas = datas;
  164. result.datas.libData = libData;
  165. }
  166. res.json(result);
  167. });
  168. }catch (err){
  169. logger.err(err);
  170. result.error=1;
  171. result.message = err.message;
  172. res.json(result);
  173. }
  174. }
  175. async function addGLJ(req, res){
  176. let result={
  177. error:0
  178. }
  179. try {
  180. let data = req.body.data;
  181. data = JSON.parse(data);
  182. let datas= await ration_glj_facade.addGLJ(data,req.session.sessionCompilation);
  183. result.data=datas;
  184. }catch (err){
  185. logger.err(err);
  186. result.error=1;
  187. result.message = err.message;
  188. }
  189. res.json(result);
  190. }
  191. async function replaceGLJ(req, res){
  192. let result={
  193. error:0
  194. }
  195. try {
  196. let data = req.body.data;
  197. data = JSON.parse(data);
  198. let rdata= await ration_glj_facade.replaceGLJ(data,req.session.sessionCompilation);
  199. result.data=rdata;
  200. }catch (err){
  201. logger.err(err);
  202. result.error=1;
  203. result.message = err.message;
  204. }
  205. res.json(result);
  206. }
  207. async function mReplaceGLJ(req, res){
  208. let result={
  209. error:0
  210. }
  211. try {
  212. let data = req.body.data;
  213. data = JSON.parse(data);
  214. let mresult= await ration_glj_facade.mReplaceGLJ(data,req.session.sessionCompilation);
  215. result.data=mresult;
  216. }catch (err){
  217. logger.err(err);
  218. result.error=1;
  219. result.message = err.message;
  220. }
  221. res.json(result);
  222. }
  223. async function updateRationGLJByEdit(req, res) {
  224. let result={
  225. error:0
  226. }
  227. try {
  228. let data = req.body.data;
  229. data = JSON.parse(data);
  230. let uresult= await ration_glj_facade.updateRationGLJByEdit(data);
  231. result.data=uresult;
  232. }catch (err){
  233. logger.err(err);
  234. result.error=1;
  235. result.message = err.message;
  236. }
  237. res.json(result);
  238. }
  239. async function getGLJClass(req, res) {
  240. let result={
  241. error:0
  242. }
  243. try {
  244. let data = req.body.data;
  245. data = JSON.parse(data);
  246. let info = await ration_glj_facade.getLibInfo(req);
  247. let tresult= await ration_glj_facade.getGLJClass(info,data);
  248. result.data=tresult;
  249. }catch (err){
  250. logger.err(err);
  251. result.error=1;
  252. result.message = err.message;
  253. }
  254. res.json(result);
  255. }