glj_list_model.js 22 KB

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