unit_price_model.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /**
  2. * 单价业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/30
  6. * @version
  7. */
  8. import mongoose from "mongoose";
  9. import BaseModel from "../../common/base/base_model"
  10. import GLJTypeConst from "../../common/const/glj_type_const"
  11. import CounterModel from "./counter_model"
  12. import MixRatioModel from "./mix_ratio_model";
  13. import _ from "lodash";
  14. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  15. let collectionName = 'unit_price';
  16. let decimal_facade = require('../../main/facade/decimal_facade');
  17. let gljListModel = mongoose.model("glj_list");
  18. let original_calc_model = mongoose.model('original_calc');
  19. let freight_calc_model = mongoose.model('freight_calc');
  20. let gljUtil = require('../../../public/gljUtil');
  21. const uuidV1 = require('uuid/v1');
  22. class UnitPriceModel extends BaseModel {
  23. /**
  24. * 构造函数
  25. *
  26. * @return {void}
  27. */
  28. constructor() {
  29. let parent = super();
  30. parent.model = mongoose.model(collectionName);
  31. parent.init();
  32. }
  33. /**
  34. * 根据单价文件id获取单价数据
  35. *
  36. * @param {Number} fileId
  37. * @return {Promise}
  38. */
  39. async getDataByFileId(fileId) {
  40. fileId = parseInt(fileId);
  41. if (isNaN(fileId) || fileId <= 0) {
  42. return null;
  43. }
  44. let unitPriceList = await this.db.model.find({unit_price_file_id: fileId});
  45. if (unitPriceList.length <= 0) {
  46. return null;
  47. }
  48. // 整理数据
  49. let result = {};
  50. for(let tmp of unitPriceList) {
  51. let index = this.getIndex(tmp,['code','name','specs','unit','type'])
  52. result[index] = tmp;
  53. }
  54. return result;
  55. }
  56. /**
  57. * 设置场景
  58. *
  59. * @param {string} scene
  60. * @return {void}
  61. */
  62. setScene(scene = '') {
  63. switch (scene) {
  64. // 新增数据的验证规则
  65. case 'add':
  66. this.model.schema.path('base_price').required(true);
  67. this.model.schema.path('market_price').required(true);
  68. this.model.schema.path('name').required(true);
  69. this.model.schema.path('code').required(true);
  70. // this.model.schema.path('unit').required(true);
  71. this.model.schema.path('type').required(true);
  72. this.model.schema.path('unit_price_file_id').required(true);
  73. }
  74. }
  75. /**
  76. * 新增单价数据
  77. *
  78. * @param {Object} data
  79. * @param {Number} unitPriceFileId
  80. * @param {Number} gljCount
  81. * @return {Promise} 返回数据以及是否新增
  82. */
  83. async addUnitPrice(data, unitPriceFileId,operation='add', gljCount = 0) {
  84. if (data.original_code===undefined||data.code === undefined || data.project_id === undefined || data.name === undefined
  85. || data.market_price === undefined) {
  86. return [null, false];
  87. }
  88. // 先查找是否有原始code相同的记录
  89. let unitPriceData = await this.db.model.find({original_code: data.original_code, unit_price_file_id: unitPriceFileId}).sort('code').exec();
  90. // 如果有记录,判断是否存在一样的名称,单位...等,有则直接返回数据
  91. let unitPrice=null;
  92. if(operation=='add'){//新增操作时,要把code也一起判断,是否完全一样。(新增的时候有可能存在编码一样,但是名称规格等不一样的情况,这种情况的话编码不用改变)
  93. unitPrice = this.isPropertyInclude(unitPriceData,['code','name','specs','unit','type'],data);
  94. }else {//修改操作时,code不用加入判断,因为code是需要改变的
  95. unitPrice = this.isPropertyInclude(unitPriceData,['name','specs','unit','type'],data);
  96. }
  97. if(unitPrice){
  98. return [unitPrice, false];
  99. }
  100. // 如果不存在基价单价,则在数据源中获取
  101. if (data.base_price === undefined) {
  102. let firstUnitPrice = unitPriceData[0] !== undefined ? unitPriceData[0] : [];
  103. data.base_price = firstUnitPrice.base_price !== undefined ? firstUnitPrice.base_price : 0;
  104. data.type = firstUnitPrice.type !== undefined ? firstUnitPrice.type : 0;
  105. }
  106. let insertData = {
  107. code: data.code,
  108. base_price: data.base_price,
  109. market_price: data.market_price,
  110. unit_price_file_id: unitPriceFileId,
  111. name: data.name,
  112. specs:data.specs?data.specs:'',
  113. original_code:data.original_code,
  114. unit:data.unit?data.unit:'',
  115. type: data.type,
  116. short_name: data.shortName !== undefined ? data.shortName : '',
  117. glj_id: data.glj_id,
  118. is_add:0,
  119. grossWeightCoe:data.grossWeightCoe,
  120. purchaseStorageRate:data.purchaseStorageRate,
  121. offSiteTransportLossRate:data.offSiteTransportLossRate,
  122. handlingLossRate:data.handlingLossRate
  123. };
  124. if(data.from=='cpt'){//如果是来自补充工料机,则都添加新增标记
  125. insertData.is_add=1;
  126. }
  127. if (unitPriceData&&unitPriceData.length>0&&operation!='add') {// 如果原始编码能找到,但不存在一样的编号,名称,单位.型号等,更改code和添加新增标记,新增的时候除外。新增的情况下能到这一步说明有存在编码一致但其它属性不一致的情况,所以不用更改编码
  128. //insertData.code = data.original_code+"-"+unitPriceData.length;
  129. insertData.code = data.original_code+"-"+this.getLastNumber(data.original_code,unitPriceData);
  130. insertData.is_add=1;
  131. }
  132. let addPriceResult = await this.add(insertData);
  133. return [addPriceResult, true];
  134. }
  135. getLastNumber(original_code,unitPriceData){
  136. let codeArray = _.map(unitPriceData,'code');
  137. let last = 1;
  138. while (true){
  139. if(_.includes(codeArray,original_code+"-"+last)){
  140. last +=1
  141. }else {
  142. break;
  143. }
  144. }
  145. return last;
  146. }
  147. /**
  148. * 新增记录
  149. *
  150. * @param {object} data
  151. * @return {Promise}
  152. */
  153. async add(data) {
  154. let counterModel = new CounterModel();
  155. if (data instanceof Array) {
  156. // 如果是批量新增
  157. for(let tmp in data) {
  158. data[tmp].id = await counterModel.getId(collectionName);
  159. }
  160. } else {
  161. data.id = await counterModel.getId(collectionName);
  162. }
  163. this.setScene('add');
  164. return this.db.model.create(data);
  165. }
  166. /**
  167. * 判断数据中是否包含某个市场价格的记录
  168. *
  169. * @param {Array} data
  170. * @param {Number} price
  171. * @return {Number}
  172. */
  173. isPriceIncluded(data, price) {
  174. let index = -1;
  175. if (data.length <= 0) {
  176. return index;
  177. }
  178. for(let tmp in data) {
  179. if (data[tmp].market_price === price) {
  180. index = tmp;
  181. break;
  182. }
  183. }
  184. return index;
  185. }
  186. isPropertyInclude(data,pops,obj){
  187. let condition={},me = this;
  188. if (data.length <= 0) {
  189. return null;
  190. }
  191. if(pops instanceof Array){
  192. return _.find(data,function (d) {
  193. return me.getIndex(d,pops) == me.getIndex(obj,pops)
  194. });
  195. }else {
  196. condition[pops]=obj[pops];
  197. return _.find(data,condition);
  198. }
  199. }
  200. /**
  201. * 更新市场单价
  202. *
  203. * @param {Object} condition
  204. * @param {Object} updateData
  205. * @param {String} extend
  206. * @return {Promise}
  207. */
  208. async updatePrice(condition, updateData, extend = '') {
  209. if (Object.keys(condition).length <= 0 || Object.keys(updateData).length <= 0) {
  210. return false;
  211. }
  212. // 首先查找相应的数据判断工料机类型
  213. let unitPriceData = await this.findDataByCondition(condition);
  214. if (!unitPriceData) {
  215. throw '找不到对应的单价数据';
  216. }
  217. /* // 基价单价的计算-----先不考虑同步
  218. switch (unitPriceData.type) {
  219. // 主材、设备自动赋值基价单价=市场单价
  220. case GLJTypeConst.MAIN_MATERIAL:
  221. case GLJTypeConst.EQUIPMENT:
  222. updateData.base_price = updateData.market_price;
  223. break;
  224. }*/
  225. // 额外更新数据
  226. if (extend !== '') {
  227. extend = JSON.parse(extend);
  228. let indexList = ['code','name','specs','unit','type'];
  229. for (let conKey in extend) {
  230. let extendUpdateData = {
  231. market_price: extend[conKey].market_price,
  232. };
  233. let tmpCondition = {
  234. unit_price_file_id: unitPriceData.unit_price_file_id,
  235. };
  236. let keyList = conKey.split("|-|");
  237. for(let i = 1;i<keyList.length;i++){
  238. if(keyList[i]!='null'){
  239. tmpCondition[indexList[i]]=keyList[i];
  240. }
  241. }
  242. let extendResult = await this.db.update(tmpCondition, extendUpdateData);
  243. if (!extendResult) {
  244. throw '更新额外数据,编码为' + code + '的数据失败!';
  245. }
  246. }
  247. }
  248. let result = await this.db.update(condition, updateData);
  249. return result.ok !== undefined && result.ok === 1;
  250. }
  251. async updateUnitPrice(data){
  252. //查找并更新单价
  253. let doc={},newValueMap={};
  254. doc[data.field]=data.newval;
  255. newValueMap[data.id]=doc;
  256. let unitPrice = await this.db.findAndModify({id:data.id,unit_price_file_id:data.unit_price_file_id},doc);
  257. if(!unitPrice){
  258. throw "没有找到对应的单价";
  259. }
  260. let rList=await this.checkAndUpdateParent(unitPrice,data.field,data.project_id,newValueMap);
  261. if(data.ext){
  262. let elecPrice = await this.db.findAndModify({id:data.ext.id,unit_price_file_id:data.ext.unit_price_file_id},data.ext.doc);
  263. let nm = {};
  264. nm[data.ext.id] = data.ext.doc;
  265. let erList=await this.checkAndUpdateParent(elecPrice,"market_price",data.project_id,nm);
  266. rList = rList.concat(erList);
  267. }
  268. return rList;
  269. }
  270. async updateCalcMaterial(data){
  271. let doc = data.ext?data.ext:{};
  272. doc[data.updateField] = data.value;
  273. let unitPrice = await this.db.findAndModify({id:data.id,unit_price_file_id:data.unit_price_file_id},doc);
  274. if(data.updateField == 'calcMaterial' && doc['calcMaterial'] == 0){//标记为0即删除材料计算标记,要删除其下挂的原价计算,运费计算,定额计算
  275. let connect_key = gljUtil.getIndex(unitPrice);
  276. await original_calc_model.deleteMany({unit_price_file_id:data.unit_price_file_id,connect_key:connect_key});
  277. await freight_calc_model.deleteMany({unit_price_file_id:data.unit_price_file_id,connect_key:connect_key});
  278. //to do 删除定额计算
  279. }
  280. if(!unitPrice){
  281. throw "没有找到对应的单价";
  282. }
  283. return unitPrice;
  284. }
  285. needUpdateParent(connect_key){
  286. let noNeedUpdateType = ["202","203","204"];//父类型为混凝土、砂浆,配合比,类型的,不用更新价格
  287. let keyList = connect_key.split("|-|");
  288. return noNeedUpdateType.indexOf(keyList[4]) == -1
  289. }
  290. async checkAndUpdateParent(unitPrice,field,project_id,newValueMap,batchUpdate=false){//检查是否属于某个工料机的组成物,如果是,并且不是批量更新的情况下,直接更新,如果是批量更新,返回更新任务
  291. //查找是否是属于某个项目工料机的组成物
  292. let mixRatioModel = new MixRatioModel();
  293. let condition = {unit_price_file_id:unitPrice.unit_price_file_id, code:unitPrice.code,name: unitPrice.name, specs: unitPrice.specs,unit:unitPrice.unit,type:unitPrice.type};
  294. let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
  295. let connectKeyMap={};
  296. //找到则计算项目工料机组成物的价格并更新
  297. let rList= [];
  298. if(mixRatioList&&mixRatioList.length>0){
  299. for(let m of mixRatioList){
  300. // 父类型不为混凝土、砂浆,配合比,类型的,才要更新价格
  301. if(this.needUpdateParent(m.connect_key) && !connectKeyMap.hasOwnProperty(m.connect_key)){//为了去重复,组成物会与其它项目同步,所以有可能重复。
  302. rList.push(await this.updateParentUnitPrice(m,field,project_id,newValueMap,batchUpdate));
  303. connectKeyMap[m.connect_key]=true;
  304. }
  305. }
  306. }
  307. return rList;
  308. }
  309. async batchUpdatePrices(data){//批量更新
  310. let tasks = [];
  311. let parentTask = [];
  312. let newValueMap = {};
  313. for(let d of data){//第一次循环生成更新提交的记录,并生成一个新值的映射表,为更新父节点使用
  314. let condition = {id:d.unit_price.id,unit_price_file_id:d.unit_price.unit_price_file_id};
  315. let doc = {};
  316. doc[d.field]=d.newval;
  317. newValueMap[d.unit_price.id] = doc;
  318. tasks.push(this.generateUpdateTask(condition,doc));
  319. }
  320. for(let d of data){//第二次更新父节点
  321. let rList = await this.checkAndUpdateParent(d.unit_price,d.field,d.project_id,newValueMap,true);
  322. parentTask = parentTask.concat(rList);
  323. }
  324. tasks = tasks.concat(parentTask);
  325. tasks.length>0?this.model.bulkWrite(tasks):'';
  326. return parentTask;
  327. }
  328. async updateParentUnitPrice(mixRatio,fieid,project_id,newValueMap,batchUpdate){//batchUpdate 批量更新标记,如果true,只生成task
  329. let decimalObject =await decimal_facade.getProjectDecimal(project_id);
  330. let quantity_decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  331. let price_decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.unitPrice)?decimalObject.glj.unitPrice:2;
  332. //查找该工料机所有组成物
  333. let indexList = ['code','name','specs','unit','type'];
  334. let mixRatioModel = new MixRatioModel();
  335. let mixRatioMap = await mixRatioModel.findDataByCondition({unit_price_file_id:mixRatio.unit_price_file_id,connect_key:mixRatio.connect_key}, null, false,indexList);
  336. //查找对应的价格
  337. let codeList = [];
  338. let nameList = [];
  339. let specsList= [];
  340. let typeList = [];
  341. let unitList = [];
  342. for(let mk in mixRatioMap){
  343. codeList.push(mixRatioMap[mk].code);
  344. nameList.push(mixRatioMap[mk].name);
  345. specsList.push(mixRatioMap[mk].specs);
  346. typeList.push(mixRatioMap[mk].type);
  347. unitList.push(mixRatioMap[mk].unit);
  348. }
  349. let condition = {unit_price_file_id: mixRatio.unit_price_file_id,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList}};
  350. let priceMap = await this.findDataByCondition(condition, {_id: 0}, false, indexList);
  351. let sumPrice=0;
  352. for(let pk in priceMap){
  353. let price = scMathUtil.roundForObj(priceMap[pk][fieid],price_decimal);
  354. let consumption = scMathUtil.roundForObj(mixRatioMap[pk].consumption,quantity_decimal);
  355. if(newValueMap[priceMap[pk].id]){//是需要更新的记录,取当前新的值
  356. price = scMathUtil.roundForObj(newValueMap[priceMap[pk].id][fieid],price_decimal);
  357. }
  358. sumPrice +=scMathUtil.roundForObj(price*consumption,price_decimal);
  359. }
  360. sumPrice= scMathUtil.roundForObj(sumPrice,price_decimal);
  361. if(sumPrice<=0){
  362. return null;
  363. }
  364. //更新父价格
  365. let keyList = mixRatio.connect_key.split("|-|");
  366. let pcondition = {
  367. unit_price_file_id:mixRatio.unit_price_file_id,
  368. code:keyList[0]
  369. };
  370. for(let i = 1;i<keyList.length;i++){
  371. if(keyList[i]!='null'){
  372. pcondition[indexList[i]]=keyList[i];
  373. }
  374. }
  375. let doc={};
  376. doc[fieid]=sumPrice;
  377. if(batchUpdate == true){
  378. return this.generateUpdateTask(pcondition,doc);
  379. }else {
  380. let uprice = await this.db.findAndModify(pcondition,doc,{new: true});
  381. //uprice[fieid]=sumPrice;
  382. return uprice;
  383. }
  384. }
  385. generateUpdateTask(condition,doc) {
  386. let task = {
  387. updateOne:{
  388. filter:condition,
  389. update:doc
  390. }
  391. };
  392. return task
  393. }
  394. /**
  395. * 复制单价文件数据
  396. *
  397. * @param {Number} currentUnitPriceId
  398. * @param {Number} changeUnitPriceId
  399. * @return {Promise}
  400. */
  401. async copyNotExist(currentUnitPriceId, changeUnitPriceId,projectId) {
  402. let result = false;
  403. // 首先查找原单价文件id下的数据
  404. let currentUnitList = await this.model.find({unit_price_file_id: currentUnitPriceId}).lean();
  405. if (currentUnitList === null) {
  406. return result;
  407. }
  408. let gljList = await gljListModel.find({'project_id':projectId});
  409. let gljMap = {};//用来记录glj的映射表,本项目有使用的工料机才需要copy过去
  410. for(let g of gljList){
  411. let g_index = this.getIndex(g,['code','name','specs','unit','type']);
  412. gljMap[g_index] = g;
  413. }
  414. let codeList = [];
  415. let nameList =[];
  416. for (let tmp of currentUnitList) {
  417. if (codeList.indexOf(tmp.code) >= 0) {
  418. continue;
  419. }
  420. codeList.push(tmp.code);
  421. if(nameList.indexOf(tmp.name)>=0){
  422. continue
  423. }
  424. nameList.push(tmp.name);
  425. }
  426. // 查找即将更替的单价文件是否存在对应的工料机数据 -- (这里只根据code和名称初步过滤,因为其它的几项更改的概率不大,在下一步的比较中再精确匹配)
  427. let condition = {unit_price_file_id: changeUnitPriceId, code: {"$in": codeList},name:{"$in": nameList}};
  428. let targetUnitList = await this.findDataByCondition(condition, null, false, ['code','name','specs','unit','type']);
  429. // 如果没有重叠的数据则原有的数据都复制一份
  430. let insertData = [];
  431. for (let tmp of currentUnitList) {
  432. let t_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  433. if (targetUnitList !== null && targetUnitList[t_index] !== undefined) {
  434. continue;
  435. }
  436. if(gljMap[t_index]){//如果本项目有用到才复制
  437. delete tmp._id; // 删除原有id信息
  438. delete tmp.id;
  439. tmp.unit_price_file_id = changeUnitPriceId;
  440. insertData.push(tmp);
  441. gljMap[t_index].copy = true;//复制标记,用于组成物,材料计算等的复制
  442. }
  443. }
  444. let uResult = insertData.length > 0 ? this.add(insertData) : true;
  445. let mixRatioModel = new MixRatioModel();
  446. let mResult = await mixRatioModel.copyNotExist(currentUnitPriceId, changeUnitPriceId,gljMap);//复制组成物
  447. let cResult = await this.copyMaterialNotExist(currentUnitPriceId, changeUnitPriceId,gljMap);
  448. return uResult&&mResult&&cResult;
  449. }
  450. async copyMaterialNotExist(currentUnitPriceId, changeUnitPriceId,gljMap){
  451. await this.copyCalcNotExist(currentUnitPriceId, changeUnitPriceId,gljMap,original_calc_model);//复制原价计算
  452. await this.copyCalcNotExist(currentUnitPriceId, changeUnitPriceId,gljMap,freight_calc_model);//复制运费计算
  453. return true;
  454. }
  455. async copyCalcNotExist(currentUnitPriceId, changeUnitPriceId,gljMap,model){
  456. let currentMap = {},targetMap = {}, insertData = [];
  457. //取原单价文件所有的原价、运费计算计录
  458. let currentList = await model.find({'unit_price_file_id':currentUnitPriceId});
  459. // 过滤mongoose格式
  460. currentList = JSON.stringify(currentList);
  461. currentList = JSON.parse(currentList);
  462. currentMap = _.groupBy(currentList,"connect_key");
  463. //切换后的单价文件所有的的组成物
  464. let targetList = await model.find({'unit_price_file_id':changeUnitPriceId});
  465. targetMap = _.groupBy(targetList,"connect_key");
  466. for(let ckey in currentMap){
  467. if(targetMap[ckey]){//如果切换后已经存在,则不用复制
  468. continue;
  469. }
  470. if(gljMap[ckey] && gljMap[ckey].copy == true){//在本项目中有用到并且复制标记为true
  471. for(let c of currentMap[ckey]){
  472. delete c._id; // 删除原有id信息
  473. c.ID = uuidV1();
  474. c.unit_price_file_id = changeUnitPriceId;
  475. insertData.push(c);
  476. }
  477. }
  478. }
  479. if(insertData.length > 0) await model.create(insertData);
  480. }
  481. }
  482. export default UnitPriceModel;