info_price_facade.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Created by zhang on 2020/7/20
  3. */
  4. module.exports={
  5. getOptions,
  6. getDataByCondition,
  7. getClassByAreaID,
  8. mutiApplyInfoPrice
  9. };
  10. const mongoose = require('mongoose');
  11. let infoLibModel = mongoose.model("std_price_info_lib");
  12. let infoClassModel = mongoose.model("std_price_info_class");
  13. let infoItemsModel = mongoose.model("std_price_info_items");
  14. let infoAreasModel = mongoose.model("std_price_info_areas");
  15. let unitPriceModel = mongoose.model("unit_price");
  16. let _ = require("lodash");
  17. let gljUtil = require('../../../public/web/gljUtil');
  18. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  19. let projectfacade = require('./project_facade');
  20. // 载入模块
  21. var Segment = require('segment');
  22. // 创建实例
  23. var segment = new Segment();
  24. // 使用默认的识别模块及字典,载入字典文件需要1秒,仅初始化时执行一次即可
  25. segment.useDefault();
  26. async function getOptions(data,compilation){//data 是预留对象,暂时不用
  27. let compilationID = compilation._id;
  28. let areaMap={};
  29. let periodMap={};
  30. let areas =await infoAreasModel.find({"compilationID":compilationID}).lean();
  31. let libList = await infoLibModel.find({"compilationID":compilationID}).lean();
  32. for(let l of libList){
  33. // for(let area of l.areas){
  34. // if(!areaMap[area]) areas.push(area);
  35. // }
  36. //2020-05
  37. let periodArray = l.period.split("-");
  38. periodMap[periodArray[0]]?periodMap[periodArray[0]].push(periodArray[1]):periodMap[periodArray[0]]=[periodArray[1]]
  39. }
  40. for(let key in periodMap){
  41. periodMap[key] = _.sortBy(periodMap[key]);
  42. }
  43. return {areas:areas,periodMap:periodMap}
  44. }
  45. async function getClassByAreaID(data,compilation){
  46. console.log(data);
  47. //要先知道根据期数和编办查找库ID
  48. let newList = [];
  49. let lib = await infoLibModel.findOne({compilationID:compilation._id,period:data.period})
  50. if(lib){
  51. let infoClass = await infoClassModel.find({areaID:data.areaID,libID:lib.ID}).lean();
  52. let parentMap=_.groupBy(infoClass, 'ParentID');
  53. for(let key in parentMap){
  54. parentMap[key] = projectfacade.sortChildren(parentMap[key]);
  55. }
  56. if(parentMap && parentMap['-1']){
  57. getChildern(parentMap['-1'],newList,parentMap)
  58. }
  59. }
  60. function getChildern(children,list,pm){
  61. for(let c of children){
  62. list.push(c);
  63. if(pm[c.ID]){
  64. getChildern(pm[c.ID],list,pm)
  65. }
  66. }
  67. }
  68. return newList;
  69. }
  70. async function getDataByCondition(data,compilation){
  71. let result = {};
  72. data.condition["compilationID"] = compilation._id;
  73. //特殊处理重庆的,地区选择非“通用”时,搜索范围应是当前选择的地区,加上“通用”中的信息价。
  74. if (data.condition.commonInfoPriceID) {
  75. let idArray = [data.condition.areaID,data.condition.commonInfoPriceID];
  76. data.condition.areaID = {$in: idArray}
  77. delete data.condition.commonInfoPriceID;
  78. }
  79. //根据地区+期数+材料编号的前4位与信息价材料的分类编号匹配,如果有数据,则显示数据出来。
  80. //先按编号匹配
  81. if (data.code) {
  82. result = await getDataByCode(data.code, data);
  83. if (result.totalSize > 0) return result;
  84. }
  85. //编号匹配不上的情况:
  86. //有关键字的情况
  87. if (data.keyWord) {
  88. return await getDataByKeyWord(data.keyWord,data);
  89. }
  90. //查询所有的情况
  91. if(data.lastID){ //有最后一行说明是查询下一页
  92. data.condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)};
  93. }else{
  94. result.totalSize = await infoItemsModel.find(data.condition).count();
  95. }
  96. result.items = await infoItemsModel.find(data.condition).lean().sort({"_id":1}).limit(50);
  97. return result;
  98. }
  99. async function getDataByCode(code, data) {
  100. let condition = { ...data.condition };
  101. condition.code = code;
  102. let totalSize = await infoItemsModel.find(condition).count();
  103. if (data.lastID) { //有最后一行说明是查询下一页
  104. condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)};
  105. }
  106. let items = [];
  107. if (totalSize > 0) {
  108. items = await infoItemsModel.find(condition).lean().sort({"_id":1}).limit(50);
  109. }
  110. return {totalSize,items}
  111. }
  112. async function getDataByKeyWord(keyword, data) {
  113. let items = [];
  114. let nameArray = [];
  115. //混凝土 和 砼 认成一个
  116. // keyword = keyword.replace(/混凝土/g, "砼");
  117. if (keyword.length < 3) {
  118. nameArray.push(keyword)
  119. } else {
  120. nameArray = segment.doSegment(keyword, {
  121. simple: true, //不返回词性
  122. stripPunctuation: true //去除标点符号
  123. });
  124. }
  125. let temArr = [];
  126. for (let a of nameArray) {
  127. if (a == "混凝土") a = '砼';
  128. if (a == '砼' || a.length > 1) temArr.push(a);
  129. }
  130. if (keyword.length == 1 && temArr.length == 0) temArr.push(keyword);
  131. nameArray = temArr;
  132. console.log(nameArray);
  133. let allInfoPrice = await infoItemsModel.find(data.condition).lean().sort({"_id":1});
  134. let maxNum = 0;//最大匹配数
  135. let matchMap = {};//匹配储存
  136. for (let info of allInfoPrice) {
  137. //specs
  138. let mstring = info.name + info.spec;
  139. mstring = mstring.replace(/混凝土/g, "砼");
  140. let matchCount = 0;
  141. for (let na of nameArray) {
  142. if (mstring.indexOf(na) != -1) {
  143. matchCount++;
  144. }
  145. }
  146. if (matchCount > 0) {
  147. matchMap[matchCount] ? matchMap[matchCount].push(info) : matchMap[matchCount] = [info];
  148. if (matchCount > maxNum) maxNum = matchCount;
  149. }
  150. }
  151. if (maxNum > 0) items = matchMap[maxNum];
  152. totalSize = items.length
  153. return {totalSize,items}
  154. }
  155. async function mutiApplyInfoPrice(data,compilation){
  156. data.condition["compilationID"] = compilation._id;
  157. let infoPrices = await infoItemsModel.find(data.condition).lean();
  158. let tasks = [];
  159. let projectGLJMap = {};
  160. for(let info of infoPrices){
  161. let index = gljUtil.getIndex(info,["name","specs","unit"]);
  162. if(data.pgljMap[index]){
  163. for(let obj of data.pgljMap[index]){
  164. let infoPrice = gljUtil.getInfoMarketPrice(info,data.taxType);
  165. infoPrice = scMathUtil.roundToString(infoPrice,data.decimal);
  166. let doc = {'market_price':infoPrice,'priceFrom':data.priceFrom};
  167. let task = {
  168. updateOne:{
  169. filter:{'id':obj.unitPriceID},
  170. update:doc
  171. }
  172. };
  173. tasks.push(task);
  174. projectGLJMap[obj.pgljID] = {index:obj.fullIndex,doc:doc};
  175. }
  176. }
  177. }
  178. if(tasks.length > 0) await unitPriceModel.bulkWrite(tasks);
  179. return projectGLJMap;
  180. }