glj_list_model.js 22 KB

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