glj_list_model.js 35 KB

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