glj_list_model.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. const ProjectModel = require('../../pm/models/project_model').project;
  18. class GLJListModel extends BaseModel {
  19. /**
  20. * 材料、主材、设备类型id
  21. *
  22. * @var {Array}
  23. */
  24. materialIdList = [GLJTypeConst.GENERAL_MATERIAL, GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
  25. GLJTypeConst.COMMERCIAL_CONCRETE, GLJTypeConst.COMMERCIAL_MORTAR, GLJTypeConst.MAIN_MATERIAL,
  26. GLJTypeConst.EQUIPMENT];
  27. /**
  28. * 拥有组成物的工料机类型id
  29. *
  30. * @var {Array}
  31. */
  32. ownCompositionTypes = [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
  33. GLJTypeConst.COMMERCIAL_CONCRETE, GLJTypeConst.COMMERCIAL_MORTAR, GLJTypeConst.GENERAL_MACHINE];
  34. /**
  35. * 构造函数
  36. *
  37. * @return {void}
  38. */
  39. constructor() {
  40. let parent = super();
  41. parent.model = GLJSchemas;
  42. parent.init();
  43. }
  44. /**
  45. * 设置场景
  46. *
  47. * @param {string} scene
  48. * @return {void}
  49. */
  50. setScene(scene = '') {
  51. switch (scene) {
  52. // 新增数据的验证规则
  53. case 'add':
  54. this.model.schema.path('glj_id').required(true);
  55. this.model.schema.path('project_id').required(true);
  56. this.model.schema.path('code').required(true);
  57. // this.model.schema.path('name').required(true);
  58. break;
  59. }
  60. }
  61. /**
  62. * 根据标段对应项目工料机列表
  63. *
  64. * @param {Number} projectId
  65. * @param {Number} unitPriceFileId
  66. * @return {Promise}
  67. */
  68. async getListByProjectId(projectId, unitPriceFileId) {
  69. let gljData = null;
  70. let mixRatioConnectData = {};
  71. try {
  72. // 首先获取对应标段下所有的项目工料机数据
  73. let condition = {project_id: projectId};
  74. let fields = {_id: 0};
  75. gljData = await this.db.find(condition, fields);
  76. // 没有数据则直接返回空
  77. if (gljData.length <= 0) {
  78. throw '无数据';
  79. }
  80. // 获取标段设置的单价文件数据
  81. let unitPriceModel = new UnitPriceModel();
  82. let unitPriceList = await unitPriceModel.getDataByFileId(unitPriceFileId);
  83. // 整理获取工料机ID list
  84. let gljIdList = [];
  85. for(let tmp of gljData) {
  86. gljIdList.push(tmp.id);
  87. }
  88. // 从定额工料机库中获取消耗量
  89. condition = {
  90. projectID: projectId,
  91. projectGLJIDList: gljIdList
  92. };
  93. let quantityData = await RationGLJFacade.getQuantityByProjectGLJ(condition);
  94. let quantityList = {};
  95. // 整理数据
  96. for (let tmp of quantityData) {
  97. let tmpNum = parseInt(tmp.rationQuantity);
  98. tmpNum = isNaN(tmpNum) ? 1 : tmpNum;
  99. if (quantityList[tmp.projectGLJID] === undefined) {
  100. quantityList[tmp.projectGLJID] = tmp.quantity * tmpNum;
  101. } else {
  102. quantityList[tmp.projectGLJID] += tmp.quantity * tmpNum;
  103. }
  104. }
  105. // 整理获取有组成物的项目工料机编码
  106. let projectGLJCode = [];
  107. for(let tmp of gljData) {
  108. // 有组成物的类型且消耗量大于0才查找
  109. if (quantityList[tmp.id] !== undefined) {
  110. projectGLJCode.push(tmp.code);
  111. }
  112. }
  113. // 查找组成物的消耗量
  114. let totalComposition = {};
  115. let mixRatioData = {};
  116. if (projectGLJCode.length > 0) {
  117. let mixRatioModel = new MixRatioModel();
  118. condition = {connect_code: {"$in": projectGLJCode}, unit_price_file_id: unitPriceFileId};
  119. let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
  120. for (let tmp of mixRatioList) {
  121. totalComposition[tmp.connect_code] = totalComposition[tmp.connect_code] === undefined ? tmp.consumption :
  122. totalComposition[tmp.connect_code] + tmp.consumption;
  123. totalComposition[tmp.connect_code] = Number(totalComposition[tmp.connect_code].toFixed(4));
  124. if (mixRatioData[tmp.glj_id] !== undefined) {
  125. mixRatioData[tmp.glj_id].push(tmp);
  126. } else {
  127. mixRatioData[tmp.glj_id] = [tmp];
  128. }
  129. if (mixRatioConnectData[tmp.glj_id] !== undefined) {
  130. mixRatioConnectData[tmp.glj_id].push(tmp.connect_code);
  131. } else {
  132. mixRatioConnectData[tmp.glj_id] = [tmp.connect_code];
  133. }
  134. }
  135. }
  136. // 组合单价数据
  137. gljData = this.combineData(gljData, unitPriceList, quantityList, mixRatioData, totalComposition);
  138. // 排序
  139. gljData.sort(function (a, b) {
  140. a.unit_price = a.unit_price === null ? 0 : a.unit_price;
  141. b.unit_price = b.unit_price === null ? 0 : b.unit_price;
  142. return a.unit_price.type - b.unit_price.type;
  143. });
  144. } catch (error) {
  145. console.log("glj_list_model:" + error);
  146. gljData = [];
  147. }
  148. return [gljData, mixRatioConnectData];
  149. }
  150. /**
  151. * 组合工料机数据和单价文件数据
  152. *
  153. * @param {object} gljList
  154. * @param {object} unitPriceList
  155. * @param {object} quantityList
  156. * @param {object} mixRatioData 组合物明细数据
  157. * @param {object} totalComposition 组合物父工料机统计数据
  158. * @return {Array}
  159. */
  160. combineData(gljList, unitPriceList, quantityList = {}, mixRatioData = {}, totalComposition = {}) {
  161. // 整理组成物消耗量(只有在总列表显示的时候才需用到,获取单项项目工料机内容则忽略)
  162. let compositionConsumption = {};
  163. if (Object.keys(mixRatioData).length > 0 && Object.keys(totalComposition).length > 0) {
  164. for(let index in mixRatioData) {
  165. for(let tmp of mixRatioData[index]) {
  166. compositionConsumption[tmp.glj_id] = compositionConsumption[tmp.glj_id] === undefined ? tmp.consumption :
  167. compositionConsumption[tmp.glj_id] + tmp.consumption;
  168. }
  169. }
  170. }
  171. let result = [];
  172. // 循环组合数据
  173. for(let index in gljList) {
  174. let glj = gljList[index];
  175. if (glj.code === undefined) {
  176. continue;
  177. }
  178. glj.unit_price = unitPriceList !== null && unitPriceList[glj.code + glj.name] !== undefined ? unitPriceList[glj.code + glj.name] : null;
  179. if (glj.unit_price === null) {
  180. continue;
  181. }
  182. let gljId = glj.glj_id + '';
  183. let projectGljId = glj.id + '';
  184. // 消耗量赋值
  185. glj.quantity = quantityList[projectGljId] !== undefined ? quantityList[projectGljId] : 0;
  186. glj.quantity = totalComposition[glj.code] !== undefined ? totalComposition[glj.code] : glj.quantity;
  187. glj.quantity = compositionConsumption[gljId] !== undefined ? glj.quantity + compositionConsumption[gljId] : glj.quantity;
  188. // 消耗值为0的数据不显示
  189. if (glj.quantity === 0) {
  190. delete gljList[index];
  191. continue;
  192. }
  193. // 组成物数据
  194. gljList[index].ratio_data = mixRatioData[gljId] !== undefined ? mixRatioData[gljId] : [];
  195. // 计算调整基价
  196. switch (glj.unit_price.type + '') {
  197. // 人工: 调整基价=基价单价*调整系数
  198. case GLJTypeConst.LABOUR:
  199. glj.adjust_price = glj.adjustment * glj.unit_price.base_price;
  200. break;
  201. // 机械类型的算法
  202. case GLJTypeConst.MACHINE:
  203. console.log('机械');
  204. break;
  205. // 材料、主材、设备
  206. default:
  207. glj.adjust_price = glj.unit_price.base_price;
  208. }
  209. result.push(glj);
  210. }
  211. return result;
  212. }
  213. /**
  214. * 新增项目工料机数据(包括新增单价文件) 定额工料机新增时调用
  215. *
  216. * @param {object} data
  217. * @return {Promise} 返回插入成功的数据id
  218. */
  219. async addList(data) {
  220. let result = null;
  221. try {
  222. if (Object.keys(data).length <= 0) {
  223. throw '新增数据为空';
  224. }
  225. // 首先查找是否有同编码同名称的工料机数据
  226. let projectGljData = await this.findDataByCondition({code: data.code, project_id: data.project_id});
  227. let isAddProjectGLJ = false;
  228. // 如果找不到数据则新增
  229. if (!projectGljData) {
  230. // 新增单条记录 (两个操作本来应该是事务操作,然而mongodb事务支持比较弱,就当作是都可以顺利执行)
  231. let gljInsertData = await this.add(data);
  232. if (!gljInsertData) {
  233. throw '新增项目工料机失败!';
  234. }
  235. isAddProjectGLJ = true;
  236. projectGljData = gljInsertData;
  237. }
  238. // 获取标段对应的单价文件id
  239. let unitPriceFileId = await ProjectModel.getUnitPriceFileId(data.project_id);
  240. if (unitPriceFileId <= 0) {
  241. throw '没有对应的单价文件';
  242. }
  243. // 判断类型,如果是混凝土、砂浆或者配合比则查找对应的组成物(前提是没有对应的项目工料机数据)
  244. if (isAddProjectGLJ && (data.type === GLJTypeConst.CONCRETE || data.type === GLJTypeConst.MORTAR ||
  245. data.type === GLJTypeConst.MIX_RATIO || data.type === GLJTypeConst.GENERAL_MACHINE)) {
  246. this.compositionInit(data.code, data.project_id, unitPriceFileId);
  247. }
  248. // 新增单价文件
  249. let unitPriceModel = new UnitPriceModel();
  250. let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
  251. if (!unitPriceInsertData) {
  252. throw '新增单价失败!';
  253. }
  254. projectGljData.unit_price = unitPriceInsertData;
  255. result = projectGljData;
  256. } catch (error) {
  257. console.log(error);
  258. result = null;
  259. }
  260. return result;
  261. }
  262. /**
  263. * 新增单条工料机数据
  264. *
  265. * @param {object} data
  266. * @return {Promise}
  267. */
  268. async add(data) {
  269. if (Object.keys(data).length <= 0) {
  270. throw '新增数据为空';
  271. }
  272. let counterModel = new CounterModel();
  273. if (data instanceof Array) {
  274. // 如果是批量新增
  275. for(let tmp in data) {
  276. data[tmp].id = await counterModel.getId(gljCollectionName);
  277. }
  278. } else {
  279. data.id = await counterModel.getId(gljCollectionName);
  280. }
  281. this.setScene('add');
  282. let result = await this.db.create(data);
  283. return result;
  284. }
  285. /**
  286. * 根据工料机id修改市场单价
  287. *
  288. * @param {Object} updateData
  289. * @return {Promise}
  290. */
  291. async modifyMarketPrice(updateData) {
  292. let result = {};
  293. try {
  294. if (updateData.code === undefined || updateData.market_price === undefined ||
  295. updateData.name === undefined || updateData.project_id === undefined) {
  296. throw '参数有误!';
  297. }
  298. // 先查是否有对应code的数据
  299. let gljListData = await this.findDataByCondition({code: updateData.code,
  300. project_id: updateData.project_id}, {_id: 0}, false);
  301. if (!gljListData) {
  302. throw '不存在对应code数据';
  303. }
  304. // 获取标段对应的单价文件id
  305. let unitPriceFileModel = new UnitPriceFileModel();
  306. let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
  307. if (!unitPriceFile) {
  308. throw '没有对应的单价文件';
  309. }
  310. let unitPriceFileId = unitPriceFile.id;
  311. let unitPriceModel = new UnitPriceModel();
  312. let gljCount = gljListData.length;
  313. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId, gljCount);
  314. // 判断是否已存在对应数据
  315. let includeField = [
  316. {field: 'name', value: unitPriceData.name}
  317. ];
  318. let gljIndex = this.isIncluded(gljListData, includeField);
  319. let gljData = isAdd ? {} : gljListData[gljIndex];
  320. // 如果单价数据新增则工料机也需要新增
  321. if (isAdd) {
  322. // 如果没有对应的记录则新增一条工料机数据,并更改name
  323. let regular = /\(\d+\)/;
  324. let changeString = '(' + gljCount + ')';
  325. updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
  326. updateData.name + changeString;
  327. // 获取第一条数据作为数据源
  328. let originalData = gljListData[0];
  329. // 更改名称
  330. originalData.name = updateData.name;
  331. originalData = JSON.stringify(originalData);
  332. gljData = await this.add(JSON.parse(originalData));
  333. if (!gljData) {
  334. throw '新增工料机数据失败!';
  335. }
  336. }
  337. gljData.unit_price = unitPriceData;
  338. result = gljData;
  339. } catch (error) {
  340. console.log(error);
  341. result = {};
  342. }
  343. return result;
  344. }
  345. /**
  346. * 判断数据中是否包含某个数据
  347. *
  348. * @param {Array} data
  349. * @param {Array} includeField
  350. * @return {Number}
  351. */
  352. isIncluded(data, includeField) {
  353. let index = -1;
  354. if (data.length <= 0) {
  355. return index;
  356. }
  357. for(let tmp in data) {
  358. let counter = 0;
  359. for (let includeTmp of includeField) {
  360. if (data[tmp][includeTmp.field] === includeTmp.value) {
  361. counter++;
  362. }
  363. }
  364. if (counter === includeField.length) {
  365. index = tmp;
  366. break;
  367. }
  368. }
  369. return index;
  370. }
  371. /**
  372. * 工料机中组成物操作
  373. * 该方法只在确保没有对应项目工料机的时候才会调用
  374. *
  375. * @param {String} code
  376. * @param {Number} projectId
  377. * @param {Number} unitPriceFileId
  378. * @return {void}
  379. */
  380. async compositionInit(code, projectId, unitPriceFileId) {
  381. // 查找对应组成物的项目工料机数据
  382. let [projectGljList, compositionGljList] = await this.getCompositionGLJList(code, projectId, 'name');
  383. // 整理配合比待插入数据
  384. let mixRatioInsertData = [];
  385. for (let tmp of compositionGljList) {
  386. // 配合比数据插入
  387. let mixRatioData = {
  388. consumption: tmp.consumption,
  389. glj_id: tmp.ID,
  390. unit_price_file_id: unitPriceFileId,
  391. connect_code: tmp.connectCode,
  392. glj_type: tmp.gljType
  393. };
  394. mixRatioInsertData.push(mixRatioData);
  395. }
  396. // 插入配合比表
  397. // 因为有可能项目工料机与单价数据已存在,但配合比数据不存在,所以先插入配合比,后续判断如果存在项目工料机则可以省下数据库操作
  398. let mixRatioModel = new MixRatioModel();
  399. let addMixRatioResult = await mixRatioModel.add(mixRatioInsertData);
  400. if (!addMixRatioResult) {
  401. throw '组成物插入单价数据失败!';
  402. }
  403. // 如果已经存在则后续操作停止
  404. if(projectGljList.length === compositionGljList.length) {
  405. return;
  406. }
  407. // 整理插入的数据
  408. let gljInsertData = [];
  409. let unitPriceInsertData = [];
  410. for(let tmp of compositionGljList) {
  411. if (projectGljList[tmp.name] !== undefined) {
  412. continue;
  413. }
  414. // 项目工料机插入的数据
  415. let gljData = {
  416. glj_id: tmp.ID,
  417. project_id: projectId,
  418. code: tmp.code,
  419. name: tmp.name,
  420. specs: tmp.specs,
  421. unit: tmp.unit === undefined ? '' : tmp.unit,
  422. };
  423. gljInsertData.push(gljData);
  424. // 单价文件插入的数据
  425. let unitPriceData = {
  426. base_price: tmp.basePrice,
  427. // 初始市场价=基价
  428. market_price: tmp.basePrice,
  429. code: tmp.code,
  430. name: tmp.name,
  431. unit_price_file_id: unitPriceFileId,
  432. type: tmp.gljType
  433. };
  434. unitPriceInsertData.push(unitPriceData);
  435. }
  436. // 整理完后开始插入数据
  437. let addResult = await this.add(gljInsertData);
  438. if (!addResult) {
  439. throw '组成物插入项目工料机失败!';
  440. }
  441. // 插入单价数据表
  442. let unitPriceModel = new UnitPriceModel();
  443. let addUnitPriceResult = await unitPriceModel.add(unitPriceInsertData);
  444. if (!addUnitPriceResult) {
  445. throw '组成物插入单价数据失败!';
  446. }
  447. }
  448. /**
  449. * 获取组成物具体数据
  450. *
  451. * @param {Number} projectGLJId
  452. * @param {Number} unitPriceFileId
  453. * @return {Promise}
  454. */
  455. async getCompositionList(projectGLJId, unitPriceFileId) {
  456. let result = [];
  457. try {
  458. // 查找对应的项目工料机数据
  459. let projectGLJData = await this.getDataById(projectGLJId);
  460. let allowType = [GLJTypeConst.MIX_RATIO, GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR,
  461. GLJTypeConst.GENERAL_MACHINE];
  462. if (projectGLJData.unit_price === null || allowType.indexOf(projectGLJData.unit_price.type) < 0) {
  463. throw '找不到相关项目工料机';
  464. }
  465. // 查找对应的项目工料机数据
  466. let [gljData, compositionList] = await this.getCompositionGLJList(projectGLJData.code, projectGLJData.project_id);
  467. if (gljData.length <= 0) {
  468. throw '没有对应的组成物项目工料机';
  469. }
  470. // 整理出code和name查找相关单价数据
  471. let codeList = [];
  472. let nameList = [];
  473. let gljIdList = [];
  474. for(let tmp of gljData) {
  475. codeList.push(tmp.code);
  476. nameList.push(tmp.name);
  477. gljIdList.push(tmp.glj_id);
  478. }
  479. // 查找对应的单价数据
  480. let unitPriceModel = new UnitPriceModel();
  481. let condition = {code: {"$in": codeList}, name: {"$in": nameList}, unit_price_file_id: unitPriceFileId};
  482. let unitPriceList = await unitPriceModel.findDataByCondition(condition, {_id: 0}, false);
  483. // 查找对应的配合比数据
  484. let mixRatioModel = new MixRatioModel();
  485. condition = {glj_id: {"$in": gljIdList}, connect_code: projectGLJData.code, unit_price_file_id: unitPriceFileId};
  486. let mixRatioData = await mixRatioModel.findDataByCondition(condition, {_id: 0}, false, 'glj_id');
  487. // 整理数据
  488. let unitPriceData = {};
  489. for(let tmp of unitPriceList) {
  490. unitPriceData[tmp.code + tmp.name] = tmp;
  491. }
  492. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  493. // 排序
  494. gljData.sort(function (a, b) {
  495. return parseInt(a.code) - parseInt(b.code);
  496. });
  497. result = gljData;
  498. } catch (error) {
  499. console.log(error);
  500. result = [];
  501. }
  502. return result;
  503. }
  504. /**
  505. * 获取混凝土等有组成物相关工料机对应的组成物项目工料机数据
  506. *
  507. * @param {String} code
  508. * @param {Number} projectId
  509. * @param {String} indexBy
  510. * @return {Promise} 返回组成物工料机数据和组成物列表数据
  511. */
  512. async getCompositionGLJList(code, projectId, indexBy = null) {
  513. // 获取对应的组成物数据
  514. let stdGljLibGljListModel = new STDGLJLibGLJListModel();
  515. let componentGljList = await stdGljLibGljListModel.getComponent(code);
  516. if (componentGljList.length <= 0) {
  517. throw '不存在对应的组成物';
  518. }
  519. let codeList = [];
  520. let nameList = [];
  521. for(let tmp of componentGljList) {
  522. codeList.push(tmp.code);
  523. nameList.push(tmp.name);
  524. }
  525. // 查找对应的项目工料机数据
  526. let condition = {code: {"$in": codeList}, name: {"$in": nameList}, project_id: projectId};
  527. let gljData = await this.findDataByCondition(condition, {_id: 0}, false, indexBy);
  528. return [gljData, componentGljList];
  529. }
  530. /**
  531. * 根据条件获取对应项目工料机数据
  532. *
  533. * @param {Number} id
  534. * @return {Promise}
  535. */
  536. async getDataById(id) {
  537. // 查找对应的项目工料机数据
  538. let projectGLJData = await this.findDataByCondition({id: id});
  539. if (projectGLJData === null) {
  540. throw '没有找到对应数据';
  541. }
  542. // 查找对应的单价数据
  543. let unitPriceModel = new UnitPriceModel();
  544. let unitPrice = await unitPriceModel.findDataByCondition({code: projectGLJData.code, name: projectGLJData.name});
  545. projectGLJData.unit_price = unitPrice;
  546. return projectGLJData;
  547. }
  548. }
  549. export default GLJListModel;