glj_list_model.js 21 KB

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