glj_list_model.js 34 KB

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