glj_list_model.js 22 KB

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