glj_list_model.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. 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];
  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. try {
  81. // 首先获取对应标段下所有的项目工料机数据
  82. let condition = {project_id: projectId};
  83. let fields = {_id: 0};
  84. gljData = await this.db.find(condition, fields);
  85. // 没有数据则直接返回空
  86. if (gljData.length <= 0) {
  87. throw '无数据';
  88. }
  89. // 获取标段设置的单价文件数据
  90. let unitPriceModel = new UnitPriceModel();
  91. let unitPriceList = await unitPriceModel.getDataByFileId(unitPriceFileId);
  92. // 整理获取工料机ID list
  93. let gljIdList = [];
  94. for(let tmp of gljData) {
  95. gljIdList.push(tmp.id);
  96. let c_key = this.getIndex(tmp,['code','name','specs','unit','type']);
  97. keyMap[tmp.id] = c_key; //工料机ID和连接key的对照表;
  98. }
  99. // 从定额工料机库中获取消耗量
  100. condition = {
  101. projectID: projectId
  102. // projectGLJIDList: gljIdList 这里是取所有项目工料机,所以也是取所有定额工料机,不需要根据项目工料机ID再过滤
  103. };
  104. //为了提高性成计算消耗量功能改成了在前端计算
  105. // 整理获取有组成物的项目工料机的数据
  106. let connect_keys = [];
  107. for(let tmp of gljData) {
  108. // 有组成物的类型才查找
  109. let key = keyMap[tmp.id];
  110. if(this.ownCompositionTypes.indexOf(tmp.type)!=-1){
  111. connect_keys.push(key);
  112. }
  113. /*if (tmp.type === GLJTypeConst.CONCRETE || tmp.type === GLJTypeConst.MORTAR ||
  114. tmp.type === GLJTypeConst.MIX_RATIO || tmp.type === GLJTypeConst.GENERAL_MACHINE|| tmp.type === GLJTypeConst.MAIN_MATERIAL){
  115. connect_keys.push(key);
  116. }*/
  117. }
  118. // 查找组成物的消耗量
  119. let totalComposition = {};
  120. let mixRatioData = {};
  121. if (connect_keys.length > 0) {
  122. let mixRatioModel = new MixRatioModel();
  123. condition = {connect_key: {"$in": connect_keys}, unit_price_file_id: unitPriceFileId};
  124. let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
  125. for (let tmp of mixRatioList) {
  126. let t_index = tmp.connect_key;
  127. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  128. /*
  129. let consumption=parseFloat(tmp.consumption);
  130. let r_quantity = quantityList[t_index]?quantityList[t_index]:0;
  131. if(quantityList[m_index]!==undefined){
  132. quantityList[m_index]=quantityList[m_index]+r_quantity*consumption;
  133. }else {
  134. quantityList[m_index] = r_quantity*consumption;
  135. }
  136. quantityList[m_index] = scMathUtil.roundTo(quantityList[m_index],-quantity_decimal);*/
  137. if (mixRatioData[t_index] !== undefined) {
  138. mixRatioData[t_index].push(tmp);
  139. } else {
  140. mixRatioData[t_index] = [tmp];
  141. }
  142. if(mixRationMap[t_index]!=undefined){
  143. mixRationMap[t_index].push(tmp);
  144. }else {
  145. mixRationMap[t_index]=[tmp];
  146. }
  147. if (mixRatioConnectData[m_index] !== undefined) {
  148. mixRatioConnectData[m_index].push(tmp.connect_key);
  149. } else {
  150. mixRatioConnectData[m_index] = [tmp.connect_key];
  151. }
  152. }
  153. }
  154. // 组合单价数据
  155. gljData = this.combineData(gljData, unitPriceList, {}, mixRatioData, totalComposition);
  156. // 排序
  157. gljData.sort(function (a, b) {
  158. a.unit_price = a.unit_price === null ? 0 : a.unit_price;
  159. b.unit_price = b.unit_price === null ? 0 : b.unit_price;
  160. return a.unit_price.type - b.unit_price.type;
  161. });
  162. } catch (error) {
  163. console.log("glj_list_model:" + error);
  164. gljData = [];
  165. }
  166. return [gljData, mixRatioConnectData,mixRationMap];
  167. }
  168. /**
  169. * 组合工料机数据和单价文件数据
  170. *
  171. * @param {object} gljList
  172. * @param {object} unitPriceList
  173. * @param {object} quantityList
  174. * @param {object} mixRatioData 组合物明细数据
  175. * @param {object} totalComposition 组合物父工料机统计数据
  176. * @return {Array}
  177. */
  178. combineData(gljList, unitPriceList, quantityList = {}, mixRatioData = {}, totalComposition = {}) {
  179. // 整理组成物消耗量(只有在总列表显示的时候才需用到,获取单项项目工料机内容则忽略)
  180. let compositionConsumption = {};
  181. if (Object.keys(mixRatioData).length > 0 && Object.keys(totalComposition).length > 0) {
  182. for(let index in mixRatioData) {
  183. for(let tmp of mixRatioData[index]) {
  184. let t_index = this.getIndex(tmp,['code','name','specs','unit','type']);//取做为组成物的工料机的总消耗量
  185. compositionConsumption[t_index] = compositionConsumption[t_index] === undefined ? tmp.consumption :
  186. compositionConsumption[t_index] + tmp.consumption;
  187. }
  188. }
  189. }
  190. let result = [];
  191. // 循环组合数据
  192. for(let index in gljList) {
  193. let glj = gljList[index];
  194. if (glj.code === undefined) {
  195. continue;
  196. }
  197. let g_index = this.getIndex(glj,['code','name','specs','unit','type']);
  198. glj.unit_price = unitPriceList !== null && unitPriceList[g_index] !== undefined ? unitPriceList[g_index] : null;
  199. if (glj.unit_price === null) {
  200. continue;
  201. }
  202. let gljId = glj.glj_id + '';
  203. let projectGljId = glj.id + '';
  204. glj.quantity = quantityList[g_index]?quantityList[g_index]:0;
  205. // 组成物数据
  206. gljList[index].ratio_data = mixRatioData[g_index] !== undefined ? mixRatioData[g_index] : [];
  207. //因为schema中设置base_price 为string 类型,所以要通过中间变量转换为数字再做计算,不然会自动变成字符串类型
  208. this.getGLJPrice(glj);
  209. result.push(glj);
  210. }
  211. return result;
  212. }
  213. getGLJPrice(glj){
  214. let glj_basePrice = parseFloat(glj.unit_price.base_price);
  215. glj.unit_price.base_price = glj_basePrice;
  216. glj.unit_price.market_price = parseFloat(glj.unit_price.market_price);
  217. }
  218. /**
  219. * 新增项目工料机数据(包括新增单价文件) 定额工料机新增时调用
  220. *
  221. * @param {object} data
  222. * @return {Promise} 返回插入成功的数据id
  223. */
  224. async addList(data,unitFileId) {
  225. let result = null;
  226. try {
  227. if (Object.keys(data).length <= 0) {
  228. throw '新增数据为空';
  229. }
  230. let condition={
  231. code: data.code,
  232. project_id: data.project_id,
  233. name:data.name,
  234. specs:data.specs,
  235. type:data.type,
  236. unit:data.unit
  237. };
  238. let projectGljData = await this.findDataByCondition(condition);
  239. let isAddProjectGLJ = false;
  240. // 获取标段对应的单价文件id
  241. let unitPriceFileId = unitFileId?unitFileId:await ProjectModel.getUnitPriceFileId(data.project_id);
  242. if (unitPriceFileId <= 0) {
  243. throw '没有对应的单价文件';
  244. }
  245. let unitPriceModel = new UnitPriceModel();
  246. // 新增单条记录 (两个操作本来应该是事务操作,然而mongodb事务支持比较弱,就当作是都可以顺利执行)
  247. // 如果找不到数据则新增
  248. if (!projectGljData) {
  249. let gljInsertData = await this.add(data);
  250. if (!gljInsertData) {
  251. throw '新增项目工料机失败!';
  252. }
  253. isAddProjectGLJ = true;
  254. projectGljData = gljInsertData;
  255. }
  256. let CompositionGLJ=[];
  257. // 判断类型,如果是混凝土、砂浆、配合比或者主材则查找对应的组成物(前提是没有对应的项目工料机数据)
  258. if(this.ownCompositionTypes.indexOf(data.type)!=-1) {
  259. //如果是新增
  260. if(isAddProjectGLJ ){
  261. await this.compositionInit(data, unitPriceFileId);
  262. }
  263. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  264. if(isAddProjectGLJ==false&&CompositionGLJ.length==0){//如果不是新增,并且是有组成物的类型但又没有发现组成物的情况下,有可能是错误数据,重新在库中查找一下组成物,有则插入
  265. await this.compositionInit(data, unitPriceFileId);
  266. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  267. if(CompositionGLJ.length>0){//如果这次发现又有组成物了,则把旧的单价数据删除,在后面的操作中会重新增加
  268. let de_condition ={unit_price_file_id: unitPriceFileId,code:data.code,name:data.name,unit:data.unit,type:data.type};
  269. data.specs!=null&&data.specs!=undefined&&data.specs!=""?de_condition.specs = data.specs:de_condition;
  270. await unitPriceModel.db.delete(de_condition);
  271. }
  272. }
  273. }
  274. projectGljData.subList=CompositionGLJ;
  275. // 新增单价文件
  276. let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
  277. if (!unitPriceInsertData) {
  278. throw '新增单价失败!';
  279. }
  280. projectGljData.unit_price = unitPriceInsertData;
  281. result = projectGljData;
  282. } catch (error) {
  283. console.log(error);
  284. result = null;
  285. }
  286. return result;
  287. }
  288. /**
  289. * 新增单条工料机数据
  290. *
  291. * @param {object} data
  292. * @return {Promise}
  293. */
  294. async add(data) {
  295. if (Object.keys(data).length <= 0) {
  296. throw '新增数据为空';
  297. }
  298. let counterModel = new CounterModel();
  299. if (data instanceof Array) {
  300. // 如果是批量新增
  301. for(let tmp in data) {
  302. data[tmp].id = await counterModel.getId(gljCollectionName);
  303. }
  304. } else {
  305. data.id = await counterModel.getId(gljCollectionName);
  306. }
  307. this.setScene('add');
  308. let result = await this.db.create(data);
  309. return result;
  310. }
  311. /**
  312. * 修改名称、规格型号、单位、市场单价等
  313. * @param data
  314. * @returns {Promise.<void>}
  315. */
  316. async modifyGLJ(data,ration_glj){
  317. let unitPriceFileModel = new UnitPriceFileModel();
  318. let unitPriceFile = await unitPriceFileModel.getDataByProject(data.project_id);
  319. if (!unitPriceFile) {
  320. throw '没有对应的单价文件';
  321. }
  322. //查找单价信息,有则返回,没有则新增并返回
  323. let unitPriceFileId = unitPriceFile.id;
  324. let unitPriceModel = new UnitPriceModel();
  325. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId,"modify");
  326. let gljData=null;
  327. if(isAdd){ //如果是新增,则新增一条新的项目工料机
  328. data.code = unitPriceData.code;
  329. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  330. }else { //如果不是新增,则查找是否有对应的项目工料机,有则返回,没有则新增
  331. let condition = {
  332. project_id:data.project_id,
  333. original_code: data.original_code,
  334. name:data.name,
  335. specs:data.specs,
  336. type:data.type,
  337. unit:data.unit
  338. }
  339. let gljList = await this.findDataByCondition(condition,{_id: 0},false);
  340. if(gljList&&gljList.length>0){
  341. for(let tem of gljList){
  342. if(tem.code == unitPriceData.code){
  343. gljData = tem;
  344. }
  345. }
  346. }
  347. if(gljData==null){
  348. data.code = unitPriceData.code;
  349. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  350. }
  351. }
  352. gljData.unit_price = unitPriceData;
  353. return gljData
  354. }
  355. //修改属性后插入项目工料机
  356. async insertGLJWhenIsAdd(glj,ration_glj,unitPriceFileId){
  357. //新增项目工料机
  358. let gljData = await this.add(glj);
  359. //查看是否是有配合比的工料机类型
  360. if(this.ownCompositionTypes.indexOf(glj.type)!=-1){
  361. // 配合比数据插入
  362. let key_array = [];
  363. if(ration_glj.subType){//定额类型的工料机和定额工料机的key有点不同subType
  364. key_array= ['code','name','specs','unit','subType'];
  365. }else {
  366. key_array= ['code','name','specs','unit','type'];
  367. }
  368. let connect_key =this.getIndex(ration_glj,key_array);
  369. let connect_key_n =this.getIndex(glj,['code','name','specs','unit','type']);
  370. //先查找配合比数据是否已经存在
  371. let mixRatioModel = new MixRatioModel();
  372. let mixRatios_o = await mixRatioModel.findDataByCondition({connect_key: connect_key_n, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  373. if(mixRatios_o&&mixRatios_o.length>0){//已经存在,不用再插入配合比数据,直接返回
  374. return gljData;
  375. }
  376. //不存在,则查找原始的配合比数据,并为新工料机增加配合比数据
  377. let mixRatios = await mixRatioModel.findDataByCondition({connect_key: connect_key, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  378. let mixInsertResult ={};
  379. if(mixRatios&&mixRatios.length>0){
  380. let newMixRatioData = [];
  381. for(let m of mixRatios){
  382. let tem ={
  383. consumption: m.consumption,
  384. glj_id: m.glj_id,
  385. unit_price_file_id: m.unit_price_file_id,
  386. connect_key: connect_key_n,
  387. type: m.type,
  388. code: m.code,
  389. specs:m.specs,
  390. name:m.name,
  391. unit:m.unit
  392. };
  393. newMixRatioData.push(tem);
  394. }
  395. mixInsertResult= await mixRatioModel.add(newMixRatioData);
  396. }
  397. }
  398. return gljData;
  399. }
  400. /**
  401. * 根据工料机id修改市场单价
  402. *
  403. * @param {Object} updateData
  404. * @return {Promise}
  405. */
  406. async modifyMarketPrice(updateData) {
  407. let result = {};
  408. try {
  409. if (updateData.code === undefined || updateData.market_price === undefined ||
  410. updateData.name === undefined || updateData.project_id === undefined) {
  411. throw '参数有误!';
  412. }
  413. // 先查是否有对应code的数据
  414. let gljListData = await this.findDataByCondition({code: updateData.code,
  415. project_id: updateData.project_id}, {_id: 0}, false);
  416. if (!gljListData) {
  417. throw '不存在对应code数据';
  418. }
  419. // 获取标段对应的单价文件id
  420. let unitPriceFileModel = new UnitPriceFileModel();
  421. let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
  422. if (!unitPriceFile) {
  423. throw '没有对应的单价文件';
  424. }
  425. let unitPriceFileId = unitPriceFile.id;
  426. let unitPriceModel = new UnitPriceModel();
  427. let gljCount = gljListData.length;
  428. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId,"modify", gljCount);
  429. // 判断是否已存在对应数据
  430. let includeField = [
  431. {field: 'name', value: unitPriceData.name}
  432. ];
  433. let gljIndex = this.isIncluded(gljListData, includeField);
  434. let gljData = isAdd ? {} : gljListData[gljIndex];
  435. // 如果单价数据新增则工料机也需要新增
  436. if (isAdd) {
  437. // 如果没有对应的记录则新增一条工料机数据,并更改name
  438. let regular = /\(\d+\)/;
  439. let changeString = '(' + gljCount + ')';
  440. updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
  441. updateData.name + changeString;
  442. // 获取第一条数据作为数据源
  443. let originalData = gljListData[0];
  444. // 更改名称
  445. originalData.name = updateData.name;
  446. originalData = JSON.stringify(originalData);
  447. gljData = await this.add(JSON.parse(originalData));
  448. if (!gljData) {
  449. throw '新增工料机数据失败!';
  450. }
  451. }
  452. gljData.unit_price = unitPriceData;
  453. result = gljData;
  454. } catch (error) {
  455. console.log(error);
  456. result = {};
  457. }
  458. return result;
  459. }
  460. /**
  461. * 判断数据中是否包含某个数据
  462. *
  463. * @param {Array} data
  464. * @param {Array} includeField
  465. * @return {Number}
  466. */
  467. isIncluded(data, includeField) {
  468. let index = -1;
  469. if (data.length <= 0) {
  470. return index;
  471. }
  472. for(let tmp in data) {
  473. let counter = 0;
  474. for (let includeTmp of includeField) {
  475. if (data[tmp][includeTmp.field] === includeTmp.value) {
  476. counter++;
  477. }
  478. }
  479. if (counter === includeField.length) {
  480. index = tmp;
  481. break;
  482. }
  483. }
  484. return index;
  485. }
  486. /**
  487. * 工料机中组成物操作
  488. * 该方法只在确保没有对应项目工料机的时候才会调用
  489. *
  490. * @param {Object} data
  491. * @param {Number} projectId
  492. * @return {void}
  493. */
  494. async compositionInit(data, unitPriceFileId) {
  495. let gljId = data.glj_id === undefined ? 0 : data.glj_id;
  496. let projectId = data.project_id === undefined ? 0 : data.project_id;
  497. let mixRatioModel = new MixRatioModel();
  498. if (gljId === 0 || projectId === 0) {
  499. throw '参数错误';
  500. }
  501. let fromTable = data.from === undefined ? 'std' : data.from;
  502. // 查找对应组成物的项目工料机数据
  503. let indexs=['code','name','specs','unit','type'];
  504. let [projectGljList, compositionGljList] = await this.getCompositionGLJList(gljId, projectId, indexs, fromTable);
  505. // 整理配合比待插入数据
  506. let mixRatioInsertData = [];
  507. var connect_key =this.getIndex(data,['code','name','specs','unit','type']);
  508. //先查找是否已存在配合比数据,在共用单价文件的情况下,有可能配合比数据存在,项目工料机数据不存在
  509. let e_mList = await mixRatioModel.findDataByCondition({unit_price_file_id: unitPriceFileId,connect_key: connect_key},null,false);
  510. if(e_mList.length <= 0){
  511. for (let tmp of compositionGljList) {
  512. // 配合比数据插入
  513. let mixRatioData = {
  514. consumption: tmp.consumption,
  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. }
  527. // 插入配合比表
  528. // 因为有可能项目工料机与单价数据已存在,但配合比数据不存在,所以先插入配合比,后续判断如果存在项目工料机则可以省下数据库操作
  529. if(mixRatioInsertData.length>0){
  530. let addMixRatioResult = await mixRatioModel.add(mixRatioInsertData);
  531. if (!addMixRatioResult) {
  532. throw '组成物插入单价数据失败!';
  533. }
  534. }
  535. let unitPriceModel = new UnitPriceModel();
  536. let unitPriceList = await unitPriceModel.model.find({unit_price_file_id:unitPriceFileId});//查找按文件id查找单价文件数据,用来判断单价文件信息是否已存在
  537. // 整理插入的数据
  538. let gljInsertData = [];
  539. let unitPriceInsertData = [];
  540. for(let tmp of compositionGljList) {
  541. let key = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  542. if (projectGljList[key] === undefined) {
  543. // 项目工料机插入的数据
  544. let gljData = {
  545. glj_id: tmp.ID,
  546. project_id: projectId,
  547. code: tmp.code,
  548. name: tmp.name,
  549. specs: tmp.specs,
  550. unit: tmp.unit === undefined ? '' : tmp.unit,
  551. type: tmp.gljType,
  552. adjCoe:tmp.adjCoe,
  553. original_code:tmp.code,
  554. materialType: tmp.materialType, //三材类别
  555. materialCoe: tmp.materialCoe
  556. };
  557. gljInsertData.push(gljData);
  558. }
  559. let basePrice = scMathUtil.roundTo(tmp.basePrice,-6);
  560. // 单价文件插入的数据
  561. let unitPriceData = {
  562. base_price: basePrice,
  563. // 初始市场价=基价
  564. market_price: basePrice,
  565. code: tmp.code,
  566. name: tmp.name,
  567. unit_price_file_id: unitPriceFileId,
  568. type: tmp.gljType,
  569. short_name: tmp.shortName === undefined ? '' : tmp.shortName,
  570. glj_id: tmp.ID,
  571. specs: tmp.specs,
  572. unit: tmp.unit === undefined ? '' : tmp.unit,
  573. original_code:tmp.code,
  574. };
  575. if(tmp.from=='cpt'){
  576. unitPriceData.is_add = 1;
  577. }
  578. if(!unitPriceModel.isPropertyInclude(unitPriceList,['code','name','specs','unit','type'],unitPriceData)){
  579. unitPriceInsertData.push(unitPriceData);
  580. }
  581. }
  582. // 整理完后开始插入数据
  583. // 插入单价数据表
  584. if(unitPriceInsertData.length >0) await unitPriceModel.add(unitPriceInsertData);
  585. //插入项目工料机
  586. if(gljInsertData.length > 0) await this.add(gljInsertData);
  587. return
  588. }
  589. /**
  590. * 获取组成物具体数据
  591. *
  592. * @param {Number} projectGLJId
  593. * @param {Number} unitPriceFileId
  594. * @return {Promise}
  595. */
  596. async getCompositionList(projectGLJId, unitPriceFileId) {
  597. let result = [];
  598. try {
  599. // 查找对应的项目工料机数据
  600. let projectGLJData = await this.getDataById(projectGLJId,unitPriceFileId);
  601. let allowType = this.ownCompositionTypes;
  602. if (projectGLJData.unit_price === null || allowType.indexOf(projectGLJData.unit_price.type) < 0) {
  603. throw '找不到相关项目工料机';
  604. }
  605. // 查找对应的项目工料机数据配合比,单价数据
  606. let [gljData, mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(projectGLJData, unitPriceFileId);
  607. if (gljData.length <= 0) {
  608. throw '没有对应的组成物项目工料机';
  609. }
  610. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  611. // 排序
  612. gljData.sort(function (a, b) {
  613. return parseInt(a.code) - parseInt(b.code);
  614. });
  615. result = gljData;
  616. } catch (error) {
  617. console.log(error);
  618. result = [];
  619. }
  620. return result;
  621. }
  622. /**
  623. * 获取混凝土等有组成物相关工料机对应的组成物项目工料机数据
  624. *
  625. * @param {Number} gljId
  626. * @param {Number} projectId
  627. * @param {String} indexBy
  628. * @param {String} fromTable
  629. * @return {Promise} 返回组成物工料机数据和组成物列表数据
  630. */
  631. async getCompositionGLJList(gljId, projectId, indexBy = null, fromTable = 'std') {
  632. // 获取对应的组成物数据
  633. let gljListModel = fromTable === 'std' ? new STDGLJLibGLJListModel() : new GljModel();
  634. let componentGljList = await gljListModel.getComponent(gljId);
  635. if (componentGljList.length <= 0) {
  636. return [{},[]];
  637. }
  638. let codeList = [];
  639. let nameList = [];
  640. let specsList= [];
  641. let typeList = [];
  642. let unitList = [];
  643. for(let tmp of componentGljList) {
  644. codeList.push(tmp.code);
  645. nameList.push(tmp.name);
  646. specsList.push(tmp.specs);
  647. typeList.push(tmp.gljType);
  648. unitList.push(tmp.unit);
  649. }
  650. // 查找对应的项目工料机数据
  651. let condition = {project_id: projectId,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList} };
  652. let gljData = await this.findDataByCondition(condition, {_id: 0}, false, indexBy);
  653. return [gljData, componentGljList];
  654. }
  655. async getCompositionGLJByData(glj,unitPriceFileId){
  656. let [gljData,mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(glj,unitPriceFileId);
  657. let priceNum = unitPriceData?Object.getOwnPropertyNames(unitPriceData).length:0;
  658. let mixNum = mixRatioData?Object.getOwnPropertyNames(mixRatioData).length:0;
  659. if(priceNum !==mixNum){//检查组成物数据是否一致
  660. console.log("组成物数据有误,组成物单价文件个数:"+priceNum+", 组成物个数:"+mixNum);
  661. return []
  662. }
  663. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  664. return gljData;
  665. }
  666. /**
  667. * 反回组成物工料机和配合比数据
  668. * @param glj
  669. * @param unitPriceFileId
  670. * @returns {Promise.<void>}
  671. */
  672. async getCompositionListByGLJ(glj,unitPriceFileId){
  673. let t_index = this.getIndex(glj,['code','name','specs','unit','type']);
  674. // 查找对应的配合比数据
  675. let mixRatioModel = new MixRatioModel();
  676. let condition = {connect_key: t_index, unit_price_file_id: unitPriceFileId};
  677. let mixRatios = await mixRatioModel.findDataByCondition(condition, {_id: 0}, false);
  678. let codeList = [];
  679. let nameList = [];
  680. let specsList= [];
  681. let typeList = [];
  682. let unitList = [];
  683. if(mixRatios.length<=0){
  684. return [[],[],[]];
  685. }
  686. let mixRatioData={};
  687. for(let tmp of mixRatios) {
  688. codeList.push(tmp.code);
  689. nameList.push(tmp.name);
  690. specsList.push(tmp.specs);
  691. typeList.push(tmp.type);
  692. unitList.push(tmp.unit);
  693. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  694. mixRatioData[m_index]=tmp;
  695. }
  696. // 查找对应的项目工料机数据
  697. let gcondition = {project_id: glj.project_id,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList} };
  698. let gljData = await this.findDataByCondition(gcondition, {_id: 0}, false);
  699. // 查找对应的单价数据
  700. let unitPriceModel = new UnitPriceModel();
  701. let ucondition = { unit_price_file_id: unitPriceFileId,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList}};
  702. let unitPriceList = await unitPriceModel.findDataByCondition(ucondition, {_id: 0}, false);
  703. // 整理数据
  704. let unitPriceData = {};
  705. for(let tmp of unitPriceList) {
  706. let u_index = this.getIndex(tmp,['code','name','specs','unit','type'])
  707. unitPriceData[u_index] = tmp;
  708. }
  709. return [gljData,mixRatioData,unitPriceData];
  710. }
  711. /**
  712. * 根据条件获取对应项目工料机数据
  713. *
  714. * @param {Number} id
  715. * @return {Promise}
  716. */
  717. async getDataById(id,unitPriceFileId) {
  718. // 查找对应的项目工料机数据
  719. let projectGLJData = await this.findDataByCondition({id: id});
  720. if (projectGLJData === null) {
  721. throw '没有找到对应数据';
  722. }
  723. // 查找对应的单价数据
  724. let unitPriceModel = new UnitPriceModel();
  725. let condition={
  726. unit_price_file_id:unitPriceFileId,
  727. code: projectGLJData.code,
  728. name:projectGLJData.name,
  729. specs:projectGLJData.specs,
  730. type:projectGLJData.type,
  731. unit:projectGLJData.unit
  732. }
  733. let unitPrice = await unitPriceModel.findDataByCondition(condition);
  734. projectGLJData.unit_price = unitPrice;
  735. return projectGLJData;
  736. }
  737. }
  738. export default GLJListModel;