glj_list_model.js 34 KB

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