glj_list_model.js 34 KB

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