glj_list_model.js 23 KB

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