glj_list_model.js 22 KB

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