glj_list_model.js 37 KB

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