glj_list_model.js 33 KB

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