glj_list_model.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /**
  2. * 项目工料机列表数据模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/22
  6. * @version
  7. */
  8. const mongoose = require("mongoose");
  9. const BaseModel = require("../../common/base/base_model");
  10. const CounterModel = require("./counter_model");
  11. const UnitPriceModel = require("./unit_price_model");
  12. const UnitPriceFileModel = require("./unit_price_file_model");
  13. const GLJTypeConst = require("../../common/const/glj_type_const");
  14. const STDGLJLibGLJListModel = require("../../common/std/std_glj_lib_glj_list_model");
  15. const MixRatioModel = require("./mix_ratio_model");
  16. const GljModel = require("../../complementary_glj_lib/models/gljModel");
  17. const ProjectModel = require('../../pm/models/project_model').project;
  18. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  19. const unitPriceFacade = require("../../unit_price_file/facade/unit_price_facade");
  20. let gljCollectionName = 'glj_list';
  21. let GLJSchemas = mongoose.model(gljCollectionName);
  22. let _ = require("lodash");
  23. class GLJListModel extends BaseModel {
  24. /**
  25. * 材料、主材、设备类型id
  26. *
  27. * @var {Array}
  28. */
  29. materialIdList = [GLJTypeConst.GENERAL_MATERIAL, GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
  30. GLJTypeConst.COMMERCIAL_CONCRETE, GLJTypeConst.COMMERCIAL_MORTAR, GLJTypeConst.MAIN_MATERIAL,
  31. GLJTypeConst.EQUIPMENT];
  32. /**
  33. * 拥有组成物的工料机类型id
  34. *
  35. * @var {Array}
  36. */
  37. ownCompositionTypes = [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
  38. GLJTypeConst.GENERAL_MACHINE,GLJTypeConst.MAIN_MATERIAL,GLJTypeConst.INSTRUMENT];
  39. /**
  40. * 构造函数
  41. *
  42. * @return {void}
  43. */
  44. constructor() {
  45. let parent = super();
  46. parent.model = GLJSchemas;
  47. parent.init();
  48. }
  49. /**
  50. * 设置场景
  51. *
  52. * @param {string} scene
  53. * @return {void}
  54. */
  55. setScene(scene = '') {
  56. switch (scene) {
  57. // 新增数据的验证规则
  58. case 'add':
  59. //this.model.schema.path('glj_id').required(true);
  60. this.model.schema.path('project_id').required(true);
  61. this.model.schema.path('code').required(true);
  62. // this.model.schema.path('name').required(true);
  63. break;
  64. }
  65. }
  66. /**
  67. * 根据标段对应项目工料机列表
  68. *
  69. * @param {Number} projectId
  70. * @param {Number} unitPriceFileId
  71. * @return {Promise}
  72. */
  73. async getListByProjectId(projectId, unitPriceFileId) {
  74. let gljData = null;
  75. /*let decimal =await decimal_facade.getProjectDecimal(projectId);
  76. let quantity_decimal = decimal&&decimal.glj.quantity?decimal.glj.quantity:6;//取消耗量保留小数据位,默认6位*/
  77. let mixRatioConnectData = {};
  78. let mixRationMap={};
  79. let keyMap={};
  80. let unitPriceList={};
  81. unitPriceFileId = unitPriceFileId?unitPriceFileId:await ProjectModel.getUnitPriceFileId(projectId);
  82. try {
  83. // 首先获取对应标段下所有的项目工料机数据
  84. let condition = {project_id: projectId};
  85. let fields = {_id: 0};
  86. gljData = await this.model.find(condition, fields).lean();
  87. // 没有数据则直接返回空
  88. if (gljData.length <= 0) {
  89. throw '无数据';
  90. }
  91. // 获取标段设置的单价文件数据
  92. let unitPriceModel = new UnitPriceModel();
  93. unitPriceList = await unitPriceModel.getDataByFileId(unitPriceFileId);
  94. let connect_keys = [];
  95. for(let tmp of gljData) {
  96. let c_key = this.getIndex(tmp,['code','name','specs','unit','type']);
  97. if(this.ownCompositionTypes.indexOf(tmp.type)!=-1){
  98. connect_keys.push(c_key);
  99. }
  100. keyMap[c_key] = tmp; //项目工料机连接key和工料机的对照表;
  101. }
  102. // 查找组成物的消耗量
  103. let totalComposition = {};
  104. let mixRatioData = {};
  105. let missMaxRatio = [];//由于共享单价文件,单价文件编辑器造成的,存在组成物和对应的单价,但是项目工料机不存的情况
  106. if (connect_keys.length > 0) {
  107. let mixRatioModel = new MixRatioModel();
  108. condition = {connect_key: {"$in": connect_keys}, unit_price_file_id: unitPriceFileId};
  109. let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
  110. for (let tmp of mixRatioList) {
  111. let t_index = tmp.connect_key;
  112. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  113. if (mixRatioData[t_index] !== undefined) {
  114. mixRatioData[t_index].push(tmp);
  115. } else {
  116. mixRatioData[t_index] = [tmp];
  117. }
  118. if(mixRationMap[t_index]!=undefined){
  119. mixRationMap[t_index].push(tmp);
  120. }else {
  121. mixRationMap[t_index]=[tmp];
  122. }
  123. if (mixRatioConnectData[m_index] !== undefined) {
  124. mixRatioConnectData[m_index].push(tmp.connect_key);
  125. } else {
  126. mixRatioConnectData[m_index] = [tmp.connect_key];
  127. }
  128. //检查该组成物对应的项目工料机是否存在
  129. if(!keyMap[m_index]) missMaxRatio.push(tmp);//这里可能会有重复,但是后面的添加操作中会去重
  130. }
  131. }
  132. if(missMaxRatio.length > 0){
  133. let newList = await unitPriceFacade.getNewProjectGLJFromMissMixratio(projectId,missMaxRatio,keyMap,[]);
  134. await unitPriceFacade.setIDfromCounter("glj_list",newList);
  135. await this.model.insertMany(newList);
  136. gljData=gljData.concat(newList);
  137. }
  138. // 组合单价数据
  139. gljData = this.combineData(gljData, unitPriceList, {}, mixRatioData, totalComposition);
  140. // 排序
  141. gljData.sort(function (a, b) {
  142. a.unit_price = a.unit_price === null ? 0 : a.unit_price;
  143. b.unit_price = b.unit_price === null ? 0 : b.unit_price;
  144. return a.unit_price.type - b.unit_price.type;
  145. });
  146. } catch (error) {
  147. console.log("glj_list_model:" + error);
  148. gljData = [];
  149. }
  150. return [gljData, mixRatioConnectData,mixRationMap,unitPriceList];
  151. }
  152. /**
  153. * 组合工料机数据和单价文件数据
  154. *
  155. * @param {object} gljList
  156. * @param {object} unitPriceList
  157. * @param {object} quantityList
  158. * @param {object} mixRatioData 组合物明细数据
  159. * @param {object} totalComposition 组合物父工料机统计数据
  160. * @return {Array}
  161. */
  162. combineData(gljList, unitPriceList, quantityList = {}, mixRatioData = {}, totalComposition = {}) {
  163. // 整理组成物消耗量(只有在总列表显示的时候才需用到,获取单项项目工料机内容则忽略)
  164. let compositionConsumption = {};
  165. if (Object.keys(mixRatioData).length > 0 && Object.keys(totalComposition).length > 0) {
  166. for(let index in mixRatioData) {
  167. for(let tmp of mixRatioData[index]) {
  168. let t_index = this.getIndex(tmp,['code','name','specs','unit','type']);//取做为组成物的工料机的总消耗量
  169. compositionConsumption[t_index] = compositionConsumption[t_index] === undefined ? tmp.consumption :
  170. compositionConsumption[t_index] + tmp.consumption;
  171. }
  172. }
  173. }
  174. let result = [];
  175. // 循环组合数据
  176. for(let index in gljList) {
  177. let glj = gljList[index];
  178. if (glj.code === undefined) {
  179. continue;
  180. }
  181. let g_index = this.getIndex(glj,['code','name','specs','unit','type']);
  182. glj.unit_price = unitPriceList !== null && unitPriceList[g_index] !== undefined ? unitPriceList[g_index] : null;
  183. if (glj.unit_price === null) {
  184. continue;
  185. }
  186. let gljId = glj.glj_id + '';
  187. let projectGljId = glj.id + '';
  188. glj.quantity = quantityList[g_index]?quantityList[g_index]:0;
  189. // 组成物数据
  190. gljList[index].ratio_data = mixRatioData[g_index] !== undefined ? mixRatioData[g_index] : [];
  191. //因为schema中设置base_price 为string 类型,所以要通过中间变量转换为数字再做计算,不然会自动变成字符串类型
  192. this.getGLJPrice(glj);
  193. result.push(glj);
  194. }
  195. return result;
  196. }
  197. getGLJPrice(glj){
  198. let glj_basePrice = parseFloat(glj.unit_price.base_price);
  199. glj.unit_price.base_price = glj_basePrice;
  200. glj.unit_price.market_price = parseFloat(glj.unit_price.market_price);
  201. }
  202. /**
  203. * 新增项目工料机数据(包括新增单价文件) 定额工料机新增时调用
  204. *
  205. * @param {object} data
  206. * @return {Promise} 返回插入成功的数据id
  207. */
  208. async addList(data,unitFileId,ext) {
  209. let result = null;
  210. try {
  211. if (Object.keys(data).length <= 0) {
  212. throw '新增数据为空';
  213. }
  214. //if(data.specs==undefined||data.specs==null||data.specs=="") data.specs = null;
  215. let condition={
  216. code: data.code,
  217. project_id: data.project_id,
  218. name:data.name,
  219. type:data.type,
  220. unit:data.unit
  221. };
  222. let projectGljData = null;
  223. let projectGLJList = await this.findDataByCondition(condition,null,false);
  224. if(projectGLJList.length > 0){//specs有可能为空或"",所以这里做这样处理
  225. let p = data.specs;
  226. if(p==undefined||p==null||p=="") p = null;
  227. for(let g of projectGLJList){
  228. let t = g.specs;
  229. if(t==undefined||t==null||t=="") t = null;
  230. if(p == t) projectGljData = g;
  231. }
  232. }
  233. // let projectGljData = await this.findDataByCondition(condition);
  234. let isAddProjectGLJ = false;
  235. // 获取标段对应的单价文件id
  236. let unitPriceFileId = unitFileId?unitFileId:await ProjectModel.getUnitPriceFileId(data.project_id);
  237. if (unitPriceFileId <= 0) {
  238. throw '没有对应的单价文件';
  239. }
  240. let unitPriceModel = new UnitPriceModel();
  241. // 新增单条记录 (两个操作本来应该是事务操作,然而mongodb事务支持比较弱,就当作是都可以顺利执行)
  242. // 如果找不到数据则新增
  243. if (!projectGljData) {
  244. let gljInsertData = await this.add(data);
  245. if (!gljInsertData) {
  246. throw '新增项目工料机失败!';
  247. }
  248. isAddProjectGLJ = true;
  249. projectGljData = gljInsertData;
  250. }
  251. let CompositionGLJ=[];
  252. // 判断类型,如果是混凝土、砂浆、配合比或者主材则查找对应的组成物(前提是没有对应的项目工料机数据)
  253. if(this.ownCompositionTypes.indexOf(data.type)!=-1) {
  254. //如果是新增
  255. if(isAddProjectGLJ ){
  256. await this.compositionInit(data, unitPriceFileId,ext);
  257. }
  258. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  259. if(isAddProjectGLJ==false&&CompositionGLJ.length==0){//如果不是新增,并且是有组成物的类型但又没有发现组成物的情况下,有可能是错误数据,重新在库中查找一下组成物,有则插入
  260. await this.compositionInit(data, unitPriceFileId,ext);
  261. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  262. if(CompositionGLJ.length>0){//如果这次发现又有组成物了,则把旧的单价数据删除,在后面的操作中会重新增加
  263. let de_condition ={unit_price_file_id: unitPriceFileId,code:data.code,name:data.name,unit:data.unit,type:data.type};
  264. data.specs!=null&&data.specs!=undefined&&data.specs!=""?de_condition.specs = data.specs:de_condition;
  265. await unitPriceModel.db.delete(de_condition);
  266. }
  267. }
  268. }
  269. projectGljData.subList=CompositionGLJ;
  270. // 新增单价文件
  271. let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
  272. if (!unitPriceInsertData) {
  273. throw '新增单价失败!';
  274. }
  275. projectGljData.unit_price = unitPriceInsertData;
  276. result = projectGljData;
  277. } catch (error) {
  278. console.log(error);
  279. result = null;
  280. }
  281. return result;
  282. }
  283. /**
  284. * 新增单条工料机数据
  285. *
  286. * @param {object} data
  287. * @return {Promise}
  288. */
  289. async add(data) {
  290. if (Object.keys(data).length <= 0) {
  291. throw '新增数据为空';
  292. }
  293. let counterModel = new CounterModel();
  294. if (data instanceof Array) {
  295. // 如果是批量新增
  296. for(let tmp in data) {
  297. data[tmp].id = await counterModel.getId(gljCollectionName);
  298. }
  299. } else {
  300. data.id = await counterModel.getId(gljCollectionName);
  301. }
  302. this.setScene('add');
  303. let result = await this.db.create(data);
  304. return result;
  305. }
  306. /**
  307. * 修改名称、规格型号、单位、市场单价等
  308. * @param data
  309. * @returns {Promise.<void>}
  310. */
  311. async modifyGLJ(data,ration_glj){
  312. let unitPriceFileModel = new UnitPriceFileModel();
  313. let unitPriceFile = await unitPriceFileModel.getDataByProject(data.project_id);
  314. if (!unitPriceFile) {
  315. throw '没有对应的单价文件';
  316. }
  317. //查找单价信息,有则返回,没有则新增并返回
  318. let unitPriceFileId = unitPriceFile.id;
  319. let unitPriceModel = new UnitPriceModel();
  320. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId,"modify");
  321. let gljData=null;
  322. if(isAdd){ //如果是新增,则新增一条新的项目工料机
  323. data.code = unitPriceData.code;
  324. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  325. }else { //如果不是新增,则查找是否有对应的项目工料机,有则返回,没有则新增
  326. let condition = {
  327. project_id:data.project_id,
  328. original_code: data.original_code,
  329. name:data.name,
  330. specs:data.specs,
  331. type:data.type,
  332. unit:data.unit
  333. }
  334. let gljList = await this.findDataByCondition(condition,{_id: 0},false);
  335. if(gljList&&gljList.length>0){
  336. for(let tem of gljList){
  337. if(tem.code == unitPriceData.code){
  338. gljData = tem;
  339. }
  340. }
  341. }
  342. if(gljData==null){
  343. data.code = unitPriceData.code;
  344. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  345. }
  346. }
  347. gljData.unit_price = unitPriceData;
  348. return gljData
  349. }
  350. //修改属性后插入项目工料机
  351. async insertGLJWhenIsAdd(glj,ration_glj,unitPriceFileId){
  352. //新增项目工料机
  353. let gljData = await this.add(glj);
  354. //查看是否是有配合比的工料机类型
  355. if(this.ownCompositionTypes.indexOf(glj.type)!=-1){
  356. // 配合比数据插入
  357. let key_array = [];
  358. if(ration_glj.subType){//定额类型的工料机和定额工料机的key有点不同subType
  359. key_array= ['code','name','specs','unit','subType'];
  360. }else {
  361. key_array= ['code','name','specs','unit','type'];
  362. }
  363. let connect_key =this.getIndex(ration_glj,key_array);
  364. let connect_key_n =this.getIndex(glj,['code','name','specs','unit','type']);
  365. //先查找配合比数据是否已经存在
  366. let mixRatioModel = new MixRatioModel();
  367. let mixRatios_o = await mixRatioModel.findDataByCondition({connect_key: connect_key_n, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  368. if(mixRatios_o&&mixRatios_o.length>0){//已经存在,不用再插入配合比数据,直接返回
  369. return gljData;
  370. }
  371. //不存在,则查找原始的配合比数据,并为新工料机增加配合比数据
  372. let mixRatios = await mixRatioModel.findDataByCondition({connect_key: connect_key, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  373. let mixInsertResult ={};
  374. if(mixRatios&&mixRatios.length>0){
  375. let newMixRatioData = [];
  376. for(let m of mixRatios){
  377. let tem ={
  378. consumption: m.consumption,
  379. glj_id: m.glj_id,
  380. unit_price_file_id: m.unit_price_file_id,
  381. connect_key: connect_key_n,
  382. type: m.type,
  383. code: m.code,
  384. specs:m.specs,
  385. name:m.name,
  386. unit:m.unit
  387. };
  388. newMixRatioData.push(tem);
  389. }
  390. mixInsertResult= await mixRatioModel.add(newMixRatioData);
  391. }
  392. }
  393. return gljData;
  394. }
  395. /**
  396. * 根据工料机id修改市场单价
  397. *
  398. * @param {Object} updateData
  399. * @return {Promise}
  400. */
  401. async modifyMarketPrice(updateData) {
  402. let result = {};
  403. try {
  404. if (updateData.code === undefined || updateData.market_price === undefined ||
  405. updateData.name === undefined || updateData.project_id === undefined) {
  406. throw '参数有误!';
  407. }
  408. // 先查是否有对应code的数据
  409. let gljListData = await this.findDataByCondition({code: updateData.code,
  410. project_id: updateData.project_id}, {_id: 0}, false);
  411. if (!gljListData) {
  412. throw '不存在对应code数据';
  413. }
  414. // 获取标段对应的单价文件id
  415. let unitPriceFileModel = new UnitPriceFileModel();
  416. let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
  417. if (!unitPriceFile) {
  418. throw '没有对应的单价文件';
  419. }
  420. let unitPriceFileId = unitPriceFile.id;
  421. let unitPriceModel = new UnitPriceModel();
  422. let gljCount = gljListData.length;
  423. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId,"modify", gljCount);
  424. // 判断是否已存在对应数据
  425. let includeField = [
  426. {field: 'name', value: unitPriceData.name}
  427. ];
  428. let gljIndex = this.isIncluded(gljListData, includeField);
  429. let gljData = isAdd ? {} : gljListData[gljIndex];
  430. // 如果单价数据新增则工料机也需要新增
  431. if (isAdd) {
  432. // 如果没有对应的记录则新增一条工料机数据,并更改name
  433. let regular = /\(\d+\)/;
  434. let changeString = '(' + gljCount + ')';
  435. updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
  436. updateData.name + changeString;
  437. // 获取第一条数据作为数据源
  438. let originalData = gljListData[0];
  439. // 更改名称
  440. originalData.name = updateData.name;
  441. originalData = JSON.stringify(originalData);
  442. gljData = await this.add(JSON.parse(originalData));
  443. if (!gljData) {
  444. throw '新增工料机数据失败!';
  445. }
  446. }
  447. gljData.unit_price = unitPriceData;
  448. result = gljData;
  449. } catch (error) {
  450. console.log(error);
  451. result = {};
  452. }
  453. return result;
  454. }
  455. /**
  456. * 判断数据中是否包含某个数据
  457. *
  458. * @param {Array} data
  459. * @param {Array} includeField
  460. * @return {Number}
  461. */
  462. isIncluded(data, includeField) {
  463. let index = -1;
  464. if (data.length <= 0) {
  465. return index;
  466. }
  467. for(let tmp in data) {
  468. let counter = 0;
  469. for (let includeTmp of includeField) {
  470. if (data[tmp][includeTmp.field] === includeTmp.value) {
  471. counter++;
  472. }
  473. }
  474. if (counter === includeField.length) {
  475. index = tmp;
  476. break;
  477. }
  478. }
  479. return index;
  480. }
  481. /**
  482. * 工料机中组成物操作
  483. * 该方法只在确保没有对应项目工料机的时候才会调用
  484. *
  485. * @param {Object} data
  486. * @param {Number} projectId
  487. * @return {void}
  488. */
  489. async compositionInit(data, unitPriceFileId,ext) {
  490. let gljId = data.glj_id === undefined ? 0 : data.glj_id;
  491. let projectId = data.project_id === undefined ? 0 : data.project_id;
  492. let mixRatioModel = new MixRatioModel();
  493. if (gljId === 0 || projectId === 0) {
  494. throw '参数错误';
  495. }
  496. let fromTable = data.from === undefined ? 'std' : data.from;
  497. var connect_key =this.getIndex(data,['code','name','specs','unit','type']);
  498. let e_mList = await this.getCompositionGLJListFromMixRatio(projectId,unitPriceFileId,connect_key,fromTable);
  499. //如果组成物数据已经存在,则以单价文件中组成物的数据为准,不用再往下做判断了(项目工料机中需要插入的已经在上一步中生成插入)
  500. if(e_mList.length > 0) return;
  501. // 查找对应组成物的项目工料机数据
  502. let indexs=['code','name','specs','unit','type'];
  503. let [projectGljList, compositionGljList] = await this.getCompositionGLJList(gljId, projectId, indexs, fromTable);
  504. // 整理配合比待插入数据,单价文件中配合比没有数据,则以库中的组成物信息为准
  505. let mixRatioInsertData = [];
  506. for (let tmp of compositionGljList) {
  507. let consumpiton = tmp.consumption;
  508. //多单价、多组成物消耗量的情况 fromTable
  509. if(ext && ext.quantityField &&( tmp.consumption[ext.quantityField]!= undefined && tmp.consumption[ext.quantityField]!=null)){
  510. consumpiton = tmp.consumption[ext.quantityField];
  511. }
  512. // 配合比数据插入
  513. let mixRatioData = {
  514. consumption: consumpiton,
  515. glj_id: tmp.ID,
  516. unit_price_file_id: unitPriceFileId,
  517. connect_key: connect_key,
  518. type: tmp.gljType,
  519. code: tmp.code,
  520. specs:tmp.specs,
  521. name:tmp.name,
  522. unit:tmp.unit
  523. };
  524. mixRatioInsertData.push(mixRatioData);
  525. }
  526. let unitPriceModel = new UnitPriceModel();
  527. let unitPriceList = await unitPriceModel.model.find({unit_price_file_id:unitPriceFileId});//查找按文件id查找单价文件数据,用来判断单价文件信息是否已存在
  528. // 整理插入的数据
  529. let gljInsertData = [];
  530. let unitPriceInsertData = [];
  531. for(let tmp of compositionGljList) {
  532. let key = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  533. if (projectGljList[key] === undefined) {
  534. // 项目工料机插入的数据
  535. let gljData = this.getProjectGLJNewData(tmp,projectId);
  536. gljInsertData.push(gljData);
  537. }
  538. let basePrice = tmp.basePrice;
  539. //多单价、多组成物消耗量的情况 fromTable
  540. if(ext && ext.priceField &&(tmp.priceProperty && tmp.priceProperty[ext.priceField]!= undefined && tmp.priceProperty[ext.priceField]!=null)){
  541. basePrice = tmp.priceProperty[ext.priceField];
  542. }
  543. basePrice = scMathUtil.roundTo(basePrice,-6);
  544. // 单价文件插入的数据
  545. let unitPriceData = {
  546. base_price: basePrice,
  547. // 初始市场价=基价
  548. market_price: basePrice,
  549. code: tmp.code,
  550. name: tmp.name,
  551. unit_price_file_id: unitPriceFileId,
  552. type: tmp.gljType,
  553. short_name: tmp.shortName === undefined ? '' : tmp.shortName,
  554. glj_id: tmp.ID,
  555. specs: tmp.specs,
  556. unit: tmp.unit === undefined ? '' : tmp.unit,
  557. original_code:tmp.code,
  558. };
  559. if(tmp.from=='cpt'){
  560. unitPriceData.is_add = 1;
  561. }
  562. if(!unitPriceModel.isPropertyInclude(unitPriceList,['code','name','specs','unit','type'],unitPriceData)){
  563. unitPriceInsertData.push(unitPriceData);
  564. }
  565. }
  566. // 整理完后开始插入数据
  567. // 插入单价数据表
  568. if(unitPriceInsertData.length >0) await unitPriceModel.add(unitPriceInsertData);
  569. // 插入配合比表
  570. // 因为有可能项目工料机与单价数据已存在,但配合比数据不存在,所以先插入配合比,后续判断如果存在项目工料机则可以省下数据库操作
  571. if(mixRatioInsertData.length>0){
  572. let addMixRatioResult = await mixRatioModel.add(mixRatioInsertData);
  573. if (!addMixRatioResult) {
  574. throw '组成物插入单价数据失败!';
  575. }
  576. }
  577. //插入项目工料机
  578. if(gljInsertData.length > 0) await this.add(gljInsertData);
  579. return
  580. }
  581. getProjectGLJNewData(tmp,projectId){
  582. let gljData = {
  583. glj_id: tmp.ID,
  584. project_id: projectId,
  585. code: tmp.code,
  586. name: tmp.name,
  587. specs: tmp.specs,
  588. unit: tmp.unit === undefined ? '' : tmp.unit,
  589. type: tmp.gljType,
  590. adjCoe:tmp.adjCoe,
  591. original_code:tmp.code,
  592. materialType: tmp.materialType, //三材类别
  593. materialCoe: tmp.materialCoe,
  594. materialIndexType: tmp.materialIndexType,
  595. materialIndexUnit: tmp.materialIndexUnit,
  596. materialIndexCoe: tmp.materialIndexCoe
  597. };
  598. return gljData;
  599. }
  600. //从配合比表中取组成物的信息以及对应的项目工料机信息,再从配合比数据中反查工料机库中的信息
  601. async getCompositionGLJListFromMixRatio (projectId,unitPriceFileId,connect_key,fromTable = 'std'){
  602. let mixRatioModel = new MixRatioModel();
  603. let e_mList = await mixRatioModel.model.find({unit_price_file_id: unitPriceFileId,connect_key: connect_key}).lean();
  604. if (e_mList.length <= 0) return [];
  605. let codeList = [];
  606. let ratioMap = {};
  607. for(let tmp of e_mList) {
  608. codeList.push(tmp.code);
  609. let ckey = this.getIndex(tmp);
  610. ratioMap[ckey] = tmp
  611. }
  612. // 查找对应的项目工料机数据
  613. let condition = {project_id: projectId,code: {"$in": codeList} };
  614. let gljData = await this.model.find(condition, {_id: 0}).lean();
  615. let matchGljs = {};
  616. for(let g of gljData){
  617. let gkey = this.getIndex(g);
  618. if(ratioMap[gkey]) matchGljs[gkey] = g;
  619. }
  620. //如果有组成物信息存在,但项目工料机信息不全的情况,应从数据库中查存询原始信息
  621. // (只有在父工料机是来自标准的情况下做这个操作,因为组成物表中没有存具体的工料机是来自补充的还是标准库,而父工料机是补充库的,组成物有可能来自两个库,而父来自标准,子只会来自标准)
  622. let lessIDList = [],lessMix=[];//lessMix只有在父工料机是来自补充工料机的时候才要用到
  623. let componentGljData = [];
  624. let gljInsertData = [];
  625. for(let mk in ratioMap){//找出缺少的工料机
  626. if(!matchGljs[mk]){
  627. lessMix.push(ratioMap[mk]);
  628. lessIDList.push(ratioMap[mk].glj_id);
  629. }
  630. }
  631. if(lessIDList.length > 0){
  632. if(fromTable == "std"){
  633. let gljListModel = new STDGLJLibGLJListModel();
  634. let condition = {ID: {$in: lessIDList}};
  635. componentGljData = await gljListModel.model.find(condition).lean();
  636. for(let tmp of componentGljData){
  637. let newProjctData = this.getProjectGLJNewData(tmp,projectId);
  638. gljInsertData.push(newProjctData)
  639. }
  640. }else {
  641. for(let m of lessMix){
  642. m.from = 'cpt';
  643. let newProjctData = this.getProjectGLJNewData(m,projectId);
  644. gljInsertData.push(newProjctData);
  645. }
  646. }
  647. }
  648. //这边只插入项目工料机数据,对于组成物文件中有的数据,理论上单价文件也是应该要有的,如果没有,那只能是mongodb数据库不支持事务的情况的问题
  649. if(gljInsertData.length > 0) await this.add(gljInsertData);
  650. return e_mList;
  651. }
  652. /**
  653. * 获取组成物具体数据
  654. *
  655. * @param {Number} projectGLJId
  656. * @param {Number} unitPriceFileId
  657. * @return {Promise}
  658. */
  659. async getCompositionList(projectGLJId, unitPriceFileId) {
  660. let result = [];
  661. try {
  662. // 查找对应的项目工料机数据
  663. let projectGLJData = await this.getDataById(projectGLJId,unitPriceFileId);
  664. let allowType = this.ownCompositionTypes;
  665. if (projectGLJData.unit_price === null || allowType.indexOf(projectGLJData.unit_price.type) < 0) {
  666. throw '找不到相关项目工料机';
  667. }
  668. // 查找对应的项目工料机数据配合比,单价数据
  669. let [gljData, mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(projectGLJData, unitPriceFileId);
  670. if (gljData.length <= 0) {
  671. throw '没有对应的组成物项目工料机';
  672. }
  673. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  674. // 排序
  675. gljData.sort(function (a, b) {
  676. return parseInt(a.code) - parseInt(b.code);
  677. });
  678. result = gljData;
  679. } catch (error) {
  680. console.log(error);
  681. result = [];
  682. }
  683. return result;
  684. }
  685. /**
  686. * 获取混凝土等有组成物相关工料机对应的组成物项目工料机数据
  687. *
  688. * @param {Number} gljId
  689. * @param {Number} projectId
  690. * @param {String} indexBy
  691. * @param {String} fromTable
  692. * @return {Promise} 返回组成物工料机数据和组成物列表数据
  693. */
  694. async getCompositionGLJList(gljId, projectId, indexBy = null, fromTable = 'std') {
  695. // 获取对应的组成物数据
  696. let gljListModel = fromTable === 'std' ? new STDGLJLibGLJListModel() : new GljModel();
  697. let componentGljList = await gljListModel.getComponent(gljId);
  698. if (componentGljList.length <= 0) {
  699. return [{},[]];
  700. }
  701. //2019-11-15 优化,用5个属性条件去查意义不大,用编号就能过滤得差不多了,最后再用5个属性去匹配。 同时可以提高查询效率
  702. let codeList = [];
  703. let componentMap = {};
  704. for(let tmp of componentGljList) {
  705. codeList.push(tmp.code);
  706. let ckey = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  707. componentMap[ckey] = tmp
  708. }
  709. // 查找对应的项目工料机数据
  710. let condition = {project_id: projectId,code: {"$in": codeList} };
  711. let gljData = await this.model.find(condition, {_id: 0}).lean();
  712. let matchGljs = {};
  713. for(let g of gljData){
  714. let gkey = this.getIndex(g);
  715. if(componentMap[gkey]) matchGljs[gkey] = g;
  716. }
  717. return [matchGljs, componentGljList];
  718. }
  719. async getCompositionGLJByData(glj,unitPriceFileId){
  720. let [gljData,mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(glj,unitPriceFileId);
  721. let priceNum = unitPriceData?Object.getOwnPropertyNames(unitPriceData).length:0;
  722. let mixNum = mixRatioData?Object.getOwnPropertyNames(mixRatioData).length:0;
  723. if(priceNum !==mixNum){//检查组成物数据是否一致
  724. console.log("组成物数据有误,组成物单价文件个数:"+priceNum+", 组成物个数:"+mixNum);
  725. return []
  726. }
  727. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  728. return gljData;
  729. }
  730. /**
  731. * 反回组成物工料机和配合比数据
  732. * @param glj
  733. * @param unitPriceFileId
  734. * @returns {Promise.<void>}
  735. */
  736. async getCompositionListByGLJ(glj,unitPriceFileId){
  737. let t_index = this.getIndex(glj,['code','name','specs','unit','type']);
  738. // 查找对应的配合比数据
  739. let mixRatioModel = new MixRatioModel();
  740. let condition = {connect_key: t_index, unit_price_file_id: unitPriceFileId};
  741. let mixRatios = await mixRatioModel.model.find(condition, {_id: 0}).lean();
  742. //2019-11-15 优化,用5个属性条件去查意义不大,用编号就能过滤得差不多了,最后再用5个属性去匹配。 同时可以提高查询效率
  743. let codeList = [];
  744. if(mixRatios.length<=0){
  745. return [[],[],[]];
  746. }
  747. let mixRatioData={};
  748. for(let tmp of mixRatios) {
  749. codeList.push(tmp.code);
  750. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  751. mixRatioData[m_index]=tmp;
  752. }
  753. //数组去重
  754. codeList = _.uniq(codeList);
  755. // 查找对应的项目工料机数据
  756. let gcondition = {project_id: glj.project_id?glj.project_id:glj.projectID,code: {"$in": codeList}};
  757. let gljData = await this.model.find(gcondition, {_id: 0}).lean();
  758. // 查找对应的单价数据
  759. let unitPriceModel = new UnitPriceModel();
  760. let ucondition = { unit_price_file_id: unitPriceFileId,code: {"$in": codeList}};
  761. let unitPriceList = await unitPriceModel.model.find(ucondition, {_id: 0}).lean();
  762. // 整理数据
  763. let unitPriceData = {};
  764. for(let tmp of unitPriceList) {
  765. let u_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  766. if(mixRatioData[u_index]) unitPriceData[u_index] = tmp;//过滤掉不完全匹配的工料机单价
  767. }
  768. let c_gljData = [];
  769. for(let g of gljData){//过滤掉不是组成物的工料机
  770. let g_index = this.getIndex(g,['code','name','specs','unit','type']);
  771. if(mixRatioData[g_index]) c_gljData.push(g);
  772. }
  773. return [c_gljData,mixRatioData,unitPriceData];
  774. }
  775. /**
  776. * 根据条件获取对应项目工料机数据
  777. *
  778. * @param {Number} id
  779. * @return {Promise}
  780. */
  781. async getDataById(id,unitPriceFileId) {
  782. // 查找对应的项目工料机数据
  783. let projectGLJData = await this.findDataByCondition({id: id});
  784. if (projectGLJData === null) {
  785. throw '没有找到对应数据';
  786. }
  787. // 查找对应的单价数据
  788. let unitPriceModel = new UnitPriceModel();
  789. let condition={
  790. unit_price_file_id:unitPriceFileId,
  791. code: projectGLJData.code,
  792. name:projectGLJData.name,
  793. specs:projectGLJData.specs,
  794. type:projectGLJData.type,
  795. unit:projectGLJData.unit
  796. };
  797. if (!condition.specs) {
  798. delete condition.specs;
  799. condition.$or = [{specs: null}, {specs: ''}];
  800. }
  801. let unitPrice = await unitPriceModel.findDataByCondition(condition);
  802. projectGLJData.unit_price = unitPrice;
  803. return projectGLJData;
  804. }
  805. }
  806. module.exports = GLJListModel;