glj_list_model.js 33 KB

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