glj_list_model.js 22 KB

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