glj_list_model.js 32 KB

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