glj_list_model.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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&&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. let unitPriceModel = new UnitPriceModel();
  285. // 判断类型,如果是混凝土、砂浆或者配合比则查找对应的组成物(前提是没有对应的项目工料机数据)
  286. if (data.type === GLJTypeConst.CONCRETE || data.type === GLJTypeConst.MORTAR ||
  287. data.type === GLJTypeConst.MIX_RATIO || data.type === GLJTypeConst.GENERAL_MACHINE) {
  288. //如果是新增
  289. if(isAddProjectGLJ ){
  290. await this.compositionInit(data, unitPriceFileId);
  291. }
  292. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  293. if(isAddProjectGLJ==false&&CompositionGLJ.length==0){//如果不是新增,并且是有组成物的类型但又没有发现组成物的情况下,有可能是错误数据,重新在库中查找一下组成物,有则插入
  294. await this.compositionInit(data, unitPriceFileId);
  295. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  296. if(CompositionGLJ.length>0){//如果这次发现又有组成物了,则把旧的单价数据删除,在后面的操作中会重新增加
  297. let de_condition ={unit_price_file_id: unitPriceFileId,code:data.code,name:data.name,unit:data.unit,type:data.type};
  298. data.specs!=null&&data.specs!=undefined&&data.specs!=""?de_condition.specs = data.specs:de_condition;
  299. await unitPriceModel.db.delete(de_condition);
  300. }
  301. }
  302. }
  303. projectGljData.subList=CompositionGLJ;
  304. // 新增单价文件
  305. let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
  306. if (!unitPriceInsertData) {
  307. throw '新增单价失败!';
  308. }
  309. projectGljData.unit_price = unitPriceInsertData;
  310. result = projectGljData;
  311. } catch (error) {
  312. console.log(error);
  313. result = null;
  314. }
  315. return result;
  316. }
  317. /**
  318. * 新增单条工料机数据
  319. *
  320. * @param {object} data
  321. * @return {Promise}
  322. */
  323. async add(data) {
  324. if (Object.keys(data).length <= 0) {
  325. throw '新增数据为空';
  326. }
  327. let counterModel = new CounterModel();
  328. if (data instanceof Array) {
  329. // 如果是批量新增
  330. for(let tmp in data) {
  331. data[tmp].id = await counterModel.getId(gljCollectionName);
  332. }
  333. } else {
  334. data.id = await counterModel.getId(gljCollectionName);
  335. }
  336. this.setScene('add');
  337. let result = await this.db.create(data);
  338. return result;
  339. }
  340. /**
  341. * 修改名称、规格型号、单位、市场单价等
  342. * @param data
  343. * @returns {Promise.<void>}
  344. */
  345. async modifyGLJ(data,ration_glj){
  346. let unitPriceFileModel = new UnitPriceFileModel();
  347. let unitPriceFile = await unitPriceFileModel.getDataByProject(data.project_id);
  348. if (!unitPriceFile) {
  349. throw '没有对应的单价文件';
  350. }
  351. //查找单价信息,有则返回,没有则新增并返回
  352. let unitPriceFileId = unitPriceFile.id;
  353. let unitPriceModel = new UnitPriceModel();
  354. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId,"modify");
  355. let gljData=null;
  356. if(isAdd){ //如果是新增,则新增一条新的项目工料机
  357. data.code = unitPriceData.code;
  358. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  359. }else { //如果不是新增,则查找是否有对应的项目工料机,有则返回,没有则新增
  360. let condition = {
  361. project_id:data.project_id,
  362. original_code: data.original_code,
  363. name:data.name,
  364. specs:data.specs,
  365. type:data.type,
  366. unit:data.unit
  367. }
  368. let gljList = await this.findDataByCondition(condition,{_id: 0},false);
  369. if(gljList&&gljList.length>0){
  370. for(let tem of gljList){
  371. if(tem.code == unitPriceData.code){
  372. gljData = tem;
  373. }
  374. }
  375. }
  376. if(gljData==null){
  377. data.code = unitPriceData.code;
  378. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  379. }
  380. }
  381. gljData.unit_price = unitPriceData;
  382. return gljData
  383. }
  384. //修改属性后插入项目工料机
  385. async insertGLJWhenIsAdd(glj,ration_glj,unitPriceFileId){
  386. //新增项目工料机
  387. let gljData = await this.add(glj);
  388. //查看是否是有配合比的工料机类型
  389. if (glj.type === GLJTypeConst.CONCRETE || glj.type === GLJTypeConst.MORTAR ||
  390. glj.type === GLJTypeConst.MIX_RATIO || glj.type === GLJTypeConst.GENERAL_MACHINE){
  391. // 配合比数据插入
  392. let key_array = [];
  393. if(ration_glj.subType){//定额类型的工料机和定额工料机的key有点不同subType
  394. key_array= ['code','name','specs','unit','subType'];
  395. }else {
  396. key_array= ['code','name','specs','unit','type'];
  397. }
  398. let connect_key =this.getIndex(ration_glj,key_array);
  399. let connect_key_n =this.getIndex(glj,['code','name','specs','unit','type']);
  400. //先查找配合比数据是否已经存在
  401. let mixRatioModel = new MixRatioModel();
  402. let mixRatios_o = await mixRatioModel.findDataByCondition({connect_key: connect_key_n, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  403. if(mixRatios_o&&mixRatios_o.length>0){//已经存在,不用再插入配合比数据,直接返回
  404. return gljData;
  405. }
  406. //不存在,则查找原始的配合比数据,并为新工料机增加配合比数据
  407. let mixRatios = await mixRatioModel.findDataByCondition({connect_key: connect_key, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  408. let mixInsertResult ={};
  409. if(mixRatios&&mixRatios.length>0){
  410. let newMixRatioData = [];
  411. for(let m of mixRatios){
  412. let tem ={
  413. consumption: m.consumption,
  414. glj_id: m.glj_id,
  415. unit_price_file_id: m.unit_price_file_id,
  416. connect_key: connect_key_n,
  417. type: m.type,
  418. code: m.code,
  419. specs:m.specs,
  420. name:m.name,
  421. unit:m.unit
  422. };
  423. newMixRatioData.push(tem);
  424. }
  425. mixInsertResult= mixRatioModel.add(newMixRatioData);
  426. }
  427. }
  428. return gljData;
  429. }
  430. /**
  431. * 根据工料机id修改市场单价
  432. *
  433. * @param {Object} updateData
  434. * @return {Promise}
  435. */
  436. async modifyMarketPrice(updateData) {
  437. let result = {};
  438. try {
  439. if (updateData.code === undefined || updateData.market_price === undefined ||
  440. updateData.name === undefined || updateData.project_id === undefined) {
  441. throw '参数有误!';
  442. }
  443. // 先查是否有对应code的数据
  444. let gljListData = await this.findDataByCondition({code: updateData.code,
  445. project_id: updateData.project_id}, {_id: 0}, false);
  446. if (!gljListData) {
  447. throw '不存在对应code数据';
  448. }
  449. // 获取标段对应的单价文件id
  450. let unitPriceFileModel = new UnitPriceFileModel();
  451. let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
  452. if (!unitPriceFile) {
  453. throw '没有对应的单价文件';
  454. }
  455. let unitPriceFileId = unitPriceFile.id;
  456. let unitPriceModel = new UnitPriceModel();
  457. let gljCount = gljListData.length;
  458. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId,"modify", gljCount);
  459. // 判断是否已存在对应数据
  460. let includeField = [
  461. {field: 'name', value: unitPriceData.name}
  462. ];
  463. let gljIndex = this.isIncluded(gljListData, includeField);
  464. let gljData = isAdd ? {} : gljListData[gljIndex];
  465. // 如果单价数据新增则工料机也需要新增
  466. if (isAdd) {
  467. // 如果没有对应的记录则新增一条工料机数据,并更改name
  468. let regular = /\(\d+\)/;
  469. let changeString = '(' + gljCount + ')';
  470. updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
  471. updateData.name + changeString;
  472. // 获取第一条数据作为数据源
  473. let originalData = gljListData[0];
  474. // 更改名称
  475. originalData.name = updateData.name;
  476. originalData = JSON.stringify(originalData);
  477. gljData = await this.add(JSON.parse(originalData));
  478. if (!gljData) {
  479. throw '新增工料机数据失败!';
  480. }
  481. }
  482. gljData.unit_price = unitPriceData;
  483. result = gljData;
  484. } catch (error) {
  485. console.log(error);
  486. result = {};
  487. }
  488. return result;
  489. }
  490. /**
  491. * 判断数据中是否包含某个数据
  492. *
  493. * @param {Array} data
  494. * @param {Array} includeField
  495. * @return {Number}
  496. */
  497. isIncluded(data, includeField) {
  498. let index = -1;
  499. if (data.length <= 0) {
  500. return index;
  501. }
  502. for(let tmp in data) {
  503. let counter = 0;
  504. for (let includeTmp of includeField) {
  505. if (data[tmp][includeTmp.field] === includeTmp.value) {
  506. counter++;
  507. }
  508. }
  509. if (counter === includeField.length) {
  510. index = tmp;
  511. break;
  512. }
  513. }
  514. return index;
  515. }
  516. /**
  517. * 工料机中组成物操作
  518. * 该方法只在确保没有对应项目工料机的时候才会调用
  519. *
  520. * @param {Object} data
  521. * @param {Number} projectId
  522. * @return {void}
  523. */
  524. async compositionInit(data, unitPriceFileId) {
  525. let gljId = data.glj_id === undefined ? 0 : data.glj_id;
  526. let projectId = data.project_id === undefined ? 0 : data.project_id;
  527. if (gljId === 0 || projectId === 0) {
  528. throw '参数错误';
  529. }
  530. let fromTable = data.from === undefined ? 'std' : data.from;
  531. // 查找对应组成物的项目工料机数据
  532. let indexs=['code','name','specs','unit','type'];
  533. let [projectGljList, compositionGljList] = await this.getCompositionGLJList(gljId, projectId, indexs, fromTable);
  534. // 整理配合比待插入数据
  535. let mixRatioInsertData = [];
  536. for (let tmp of compositionGljList) {
  537. // 配合比数据插入
  538. var connect_key =this.getIndex(data,['code','name','specs','unit','type']);
  539. let mixRatioData = {
  540. consumption: tmp.consumption,
  541. glj_id: tmp.ID,
  542. unit_price_file_id: unitPriceFileId,
  543. connect_key: connect_key,
  544. type: tmp.gljType,
  545. code: tmp.code,
  546. specs:tmp.specs,
  547. name:tmp.name,
  548. unit:tmp.unit
  549. };
  550. mixRatioInsertData.push(mixRatioData);
  551. }
  552. // 插入配合比表
  553. // 因为有可能项目工料机与单价数据已存在,但配合比数据不存在,所以先插入配合比,后续判断如果存在项目工料机则可以省下数据库操作
  554. if(mixRatioInsertData.length>0){
  555. let mixRatioModel = new MixRatioModel();
  556. let addMixRatioResult = await mixRatioModel.add(mixRatioInsertData);
  557. if (!addMixRatioResult) {
  558. throw '组成物插入单价数据失败!';
  559. }
  560. }
  561. let pglj_length = projectGljList instanceof Array ? projectGljList.length:Object.getOwnPropertyNames(projectGljList).length;
  562. // 如果已经存在则后续操作停止
  563. if(pglj_length === compositionGljList.length) {
  564. return
  565. }
  566. // 整理插入的数据
  567. let gljInsertData = [];
  568. let unitPriceInsertData = [];
  569. for(let tmp of compositionGljList) {
  570. let key = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  571. if (projectGljList[key] !== undefined) {
  572. continue;
  573. }
  574. // 项目工料机插入的数据
  575. let gljData = {
  576. glj_id: tmp.ID,
  577. project_id: projectId,
  578. code: tmp.code,
  579. name: tmp.name,
  580. specs: tmp.specs,
  581. unit: tmp.unit === undefined ? '' : tmp.unit,
  582. type: tmp.gljType,
  583. adjCoe:tmp.adjCoe,
  584. original_code:tmp.code
  585. };
  586. gljInsertData.push(gljData);
  587. let basePrice = scMathUtil.roundTo(tmp.basePrice,-6);
  588. // 单价文件插入的数据
  589. let unitPriceData = {
  590. base_price: basePrice,
  591. // 初始市场价=基价
  592. market_price: basePrice,
  593. code: tmp.code,
  594. name: tmp.name,
  595. unit_price_file_id: unitPriceFileId,
  596. type: tmp.gljType,
  597. short_name: tmp.shortName === undefined ? '' : tmp.shortName,
  598. glj_id: tmp.ID,
  599. specs: tmp.specs,
  600. unit: tmp.unit === undefined ? '' : tmp.unit,
  601. original_code:tmp.code
  602. };
  603. unitPriceInsertData.push(unitPriceData);
  604. }
  605. // 整理完后开始插入数据
  606. let addResult = await this.add(gljInsertData);
  607. if (!addResult) {
  608. throw '组成物插入项目工料机失败!';
  609. }
  610. // 插入单价数据表
  611. let unitPriceModel = new UnitPriceModel();
  612. let addUnitPriceResult = await unitPriceModel.add(unitPriceInsertData);
  613. if (!addUnitPriceResult) {
  614. throw '组成物插入单价数据失败!';
  615. }
  616. return
  617. }
  618. /**
  619. * 获取组成物具体数据
  620. *
  621. * @param {Number} projectGLJId
  622. * @param {Number} unitPriceFileId
  623. * @return {Promise}
  624. */
  625. async getCompositionList(projectGLJId, unitPriceFileId) {
  626. let result = [];
  627. try {
  628. // 查找对应的项目工料机数据
  629. let projectGLJData = await this.getDataById(projectGLJId,unitPriceFileId);
  630. let allowType = [GLJTypeConst.MIX_RATIO, GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR,
  631. GLJTypeConst.GENERAL_MACHINE];
  632. if (projectGLJData.unit_price === null || allowType.indexOf(projectGLJData.unit_price.type) < 0) {
  633. throw '找不到相关项目工料机';
  634. }
  635. // 查找对应的项目工料机数据配合比,单价数据
  636. let [gljData, mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(projectGLJData, unitPriceFileId);
  637. if (gljData.length <= 0) {
  638. throw '没有对应的组成物项目工料机';
  639. }
  640. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  641. // 排序
  642. gljData.sort(function (a, b) {
  643. return parseInt(a.code) - parseInt(b.code);
  644. });
  645. result = gljData;
  646. } catch (error) {
  647. console.log(error);
  648. result = [];
  649. }
  650. return result;
  651. }
  652. /**
  653. * 获取混凝土等有组成物相关工料机对应的组成物项目工料机数据
  654. *
  655. * @param {Number} gljId
  656. * @param {Number} projectId
  657. * @param {String} indexBy
  658. * @param {String} fromTable
  659. * @return {Promise} 返回组成物工料机数据和组成物列表数据
  660. */
  661. async getCompositionGLJList(gljId, projectId, indexBy = null, fromTable = 'std') {
  662. // 获取对应的组成物数据
  663. let gljListModel = fromTable === 'std' ? new STDGLJLibGLJListModel() : new GljModel();
  664. let componentGljList = await gljListModel.getComponent(gljId);
  665. if (componentGljList.length <= 0) {
  666. return [{},[]];
  667. }
  668. let codeList = [];
  669. let nameList = [];
  670. let specsList= [];
  671. let typeList = [];
  672. let unitList = [];
  673. for(let tmp of componentGljList) {
  674. codeList.push(tmp.code);
  675. nameList.push(tmp.name);
  676. specsList.push(tmp.specs);
  677. typeList.push(tmp.gljType);
  678. unitList.push(tmp.unit);
  679. }
  680. // 查找对应的项目工料机数据
  681. let condition = {project_id: projectId,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList} };
  682. let gljData = await this.findDataByCondition(condition, {_id: 0}, false, indexBy);
  683. return [gljData, componentGljList];
  684. }
  685. async getCompositionGLJByData(glj,unitPriceFileId){
  686. let [gljData,mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(glj,unitPriceFileId);
  687. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  688. return gljData;
  689. }
  690. /**
  691. * 反回组成物工料机和配合比数据
  692. * @param glj
  693. * @param unitPriceFileId
  694. * @returns {Promise.<void>}
  695. */
  696. async getCompositionListByGLJ(glj,unitPriceFileId){
  697. let t_index = this.getIndex(glj,['code','name','specs','unit','type']);
  698. // 查找对应的配合比数据
  699. let mixRatioModel = new MixRatioModel();
  700. let condition = {connect_key: t_index, unit_price_file_id: unitPriceFileId};
  701. let mixRatios = await mixRatioModel.findDataByCondition(condition, {_id: 0}, false);
  702. let codeList = [];
  703. let nameList = [];
  704. let specsList= [];
  705. let typeList = [];
  706. let unitList = [];
  707. if(mixRatios.length<=0){
  708. return [[],[],[]];
  709. }
  710. let mixRatioData={};
  711. for(let tmp of mixRatios) {
  712. codeList.push(tmp.code);
  713. nameList.push(tmp.name);
  714. specsList.push(tmp.specs);
  715. typeList.push(tmp.type);
  716. unitList.push(tmp.unit);
  717. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  718. mixRatioData[m_index]=tmp;
  719. }
  720. // 查找对应的项目工料机数据
  721. let gcondition = {project_id: glj.project_id,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList} };
  722. let gljData = await this.findDataByCondition(gcondition, {_id: 0}, false);
  723. // 查找对应的单价数据
  724. let unitPriceModel = new UnitPriceModel();
  725. let ucondition = { unit_price_file_id: unitPriceFileId,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList}};
  726. let unitPriceList = await unitPriceModel.findDataByCondition(ucondition, {_id: 0}, false);
  727. // 整理数据
  728. let unitPriceData = {};
  729. for(let tmp of unitPriceList) {
  730. let u_index = this.getIndex(tmp,['code','name','specs','unit','type'])
  731. unitPriceData[u_index] = tmp;
  732. }
  733. return [gljData,mixRatioData,unitPriceData];
  734. }
  735. /**
  736. * 根据条件获取对应项目工料机数据
  737. *
  738. * @param {Number} id
  739. * @return {Promise}
  740. */
  741. async getDataById(id,unitPriceFileId) {
  742. // 查找对应的项目工料机数据
  743. let projectGLJData = await this.findDataByCondition({id: id});
  744. if (projectGLJData === null) {
  745. throw '没有找到对应数据';
  746. }
  747. // 查找对应的单价数据
  748. let unitPriceModel = new UnitPriceModel();
  749. let condition={
  750. unit_price_file_id:unitPriceFileId,
  751. code: projectGLJData.code,
  752. name:projectGLJData.name,
  753. specs:projectGLJData.specs,
  754. type:projectGLJData.type,
  755. unit:projectGLJData.unit
  756. }
  757. let unitPrice = await unitPriceModel.findDataByCondition(condition);
  758. projectGLJData.unit_price = unitPrice;
  759. return projectGLJData;
  760. }
  761. }
  762. export default GLJListModel;