info_price_facade.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. let nodejieba = require('../../dict/jieba');
  27. let nameWeightMap ={
  28. '普通':-99
  29. }
  30. async function getOptions(data,compilation){//data 是预留对象,暂时不用
  31. let compilationID = compilation._id;
  32. let areaMap={};
  33. let periodMap={};
  34. let areas =await infoAreasModel.find({"compilationID":compilationID}).lean();
  35. let libList = await infoLibModel.find({"compilationID":compilationID}).lean();
  36. for(let l of libList){
  37. // for(let area of l.areas){
  38. // if(!areaMap[area]) areas.push(area);
  39. // }
  40. //2020-05
  41. let periodArray = l.period.split("-");
  42. periodMap[periodArray[0]]?periodMap[periodArray[0]].push(periodArray[1]):periodMap[periodArray[0]]=[periodArray[1]]
  43. }
  44. for(let key in periodMap){
  45. periodMap[key] = _.sortBy(periodMap[key]);
  46. }
  47. return {areas:areas,periodMap:periodMap}
  48. }
  49. async function getClassByAreaID(data,compilation){
  50. //要先知道根据期数和编办查找库ID
  51. let newList = [];
  52. let lib = await infoLibModel.findOne({compilationID:compilation._id,period:data.period})
  53. if(lib){
  54. let tList =await getClassList(data.areaID,lib.ID);
  55. newList.push(...tList);
  56. if(data.commonInfoPriceID){
  57. cList = await getClassList(data.commonInfoPriceID,lib.ID);
  58. newList.push(...cList);
  59. }
  60. }
  61. async function getClassList(areaID,libID){
  62. let temList = [];
  63. let infoClass = await infoClassModel.find({areaID:areaID,libID:libID}).lean();
  64. let parentMap=_.groupBy(infoClass, 'ParentID');
  65. for(let key in parentMap){
  66. parentMap[key] = projectfacade.sortChildren(parentMap[key]);
  67. }
  68. if(parentMap && parentMap['-1']){
  69. getChildern(parentMap['-1'],temList,parentMap)
  70. }
  71. return temList;
  72. }
  73. function getChildern(children,list,pm){
  74. for(let c of children){
  75. list.push(c);
  76. if(pm[c.ID]){
  77. getChildern(pm[c.ID],list,pm)
  78. }
  79. }
  80. }
  81. return newList;
  82. }
  83. async function getDataByCondition(data,compilation){
  84. let result = {};
  85. data.condition["compilationID"] = compilation._id;
  86. //特殊处理重庆的,地区选择非“通用”时,搜索范围应是当前选择的地区,加上“通用”中的信息价。
  87. if (data.condition.commonInfoPriceID) {
  88. let idArray = [data.condition.areaID,data.condition.commonInfoPriceID];
  89. data.condition.areaID = {$in: idArray}
  90. delete data.condition.commonInfoPriceID;
  91. }
  92. //根据地区+期数+材料编号的前4位与信息价材料的分类编号匹配,如果有数据,则显示数据出来。
  93. //先按编号匹配
  94. if (data.code) {
  95. result = await getDataByCode(data.code, data);
  96. if (result.totalSize > 0) return result;
  97. }
  98. //编号匹配不上的情况:
  99. //有关键字的情况
  100. if (data.keyWord) {
  101. return await getDataByKeyWord(data.keyWord,data);
  102. }
  103. //查询所有的情况
  104. if(data.lastID){ //有最后一行说明是查询下一页
  105. data.condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)};
  106. }else{
  107. result.totalSize = await infoItemsModel.find(data.condition).count();
  108. }
  109. result.items = await infoItemsModel.find(data.condition).lean().sort({"_id":1}).limit(50);
  110. return result;
  111. }
  112. async function getDataByCode(code, data) {
  113. let condition = { ...data.condition };
  114. condition.code = code;
  115. /*
  116. 2021-03-31 先按编号去取,所有匹配结果再过滤,不能再分类了
  117. let totalSize = await infoItemsModel.find(condition).count();
  118. if (data.lastID) { //有最后一行说明是查询下一页
  119. condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)};
  120. }
  121. let items = [];
  122. if (totalSize > 0) {
  123. items = await infoItemsModel.find(condition).lean().sort({"_id":1}).limit(50);
  124. } */
  125. //新需求 ---------
  126. let allItems = await infoItemsModel.find(condition).lean().sort({"_id":1});
  127. let items = [];
  128. if(data.keyWord && allItems.length > 1){
  129. for(let item of allItems){
  130. if(item.name.indexOf(data.keyWord) != -1) items.push(item) //有完全匹配的,就不用编码下的返回所有数据了
  131. }
  132. }
  133. //没有完全匹配的,返回所有
  134. if (items.length === 0) items = allItems;
  135. let totalSize = items.length;
  136. //新需求结束 ---------
  137. return {totalSize,items}
  138. }
  139. async function getDataByKeyWord(keyword, data) {
  140. //先按全字匹配
  141. let fullResult =await getDataByFullKeyWord(keyword, data);
  142. if(fullResult.totalSize > 0) return fullResult;
  143. //如果没有才按模糊匹配
  144. return await getDataByFuzzyMatch(keyword, data);
  145. }
  146. //按全关键字匹配
  147. async function getDataByFullKeyWord(keyword, data){
  148. data.condition.name = new RegExp(keyword);
  149. let items = await infoItemsModel.find(data.condition).lean().sort({"_id":1});
  150. delete data.condition.name;
  151. return{totalSize:items.length,items}
  152. }
  153. function handelThreeWord(word){
  154. let arr = [];
  155. getArr(word[0]+word[1],arr);
  156. getArr(word[1]+word[2],arr);
  157. if(arr.length > 0) return arr;
  158. return[word];
  159. function getArr(tem,list){
  160. let nameArray = segment.doSegment(tem, {
  161. simple: true, //不返回词性
  162. stripPunctuation: true //去除标点符号
  163. });
  164. //说明是一个词
  165. if(nameArray.length == 1) list.push(tem)
  166. }
  167. }
  168. function getShortNameArray(nameArray){
  169. let newArray = [];
  170. for(let n of nameArray){
  171. if(n.length >= 5){
  172. newArray.push(...nodejieba.cutSmall(n,3))
  173. }else{
  174. newArray.push(n);
  175. }
  176. }
  177. return newArray;
  178. }
  179. function getMatchPrice(allInfoPrice,nameArray,needHandleLongWord = true){
  180. let items = [];
  181. let maxNum = 0;//最大匹配数
  182. let matchMap = {};//匹配储存
  183. let handleLongWord = false;
  184. if(needHandleLongWord){
  185. for(let na of nameArray){
  186. if(na.length >= 5) handleLongWord = true;
  187. }
  188. }
  189. for (let info of allInfoPrice) {
  190. //specs
  191. let mstring = info.name + info.specs;
  192. mstring = mstring.replace(/混凝土/g, "砼");
  193. info.mstring = mstring;
  194. let matchCount = 0;
  195. for (let na of nameArray) {
  196. if (mstring.indexOf(na) != -1) {
  197. matchCount++;
  198. if(needHandleLongWord && na.length >= 5) handleLongWord = false//有5个字的,并且匹配上了,这里就为false不用再处理一次了
  199. }
  200. }
  201. if (matchCount > 0) {
  202. matchMap[matchCount] ? matchMap[matchCount].push(info) : matchMap[matchCount] = [info];
  203. if (matchCount > maxNum) maxNum = matchCount;
  204. }
  205. }
  206. if (maxNum > 0) items = matchMap[maxNum];
  207. totalSize = items.length
  208. return {totalSize,items,handleLongWord};
  209. }
  210. //自定义特殊处理
  211. function cusSegment(nameArray,keyword){
  212. let temArr = [];
  213. for (let a of nameArray) {
  214. //混凝土 和 砼 统一认成砼
  215. if (a == "混凝土") a = '砼';
  216. if(a == '砼'||a == '砖' ){
  217. temArr.push(a);
  218. }else if(a.length > 1){
  219. //如果是三个字的词,优先识别成两个字
  220. if(a.length == 3){
  221. let sArr = handelThreeWord(a);
  222. temArr.push(...sArr);
  223. }else{
  224. temArr.push(a);
  225. }
  226. }
  227. }
  228. if (keyword.length == 1 && temArr.length == 0) temArr.push(keyword);
  229. return temArr;
  230. }
  231. //模糊匹配
  232. async function getDataByFuzzyMatch(keyword, data){
  233. let nameArray = [];
  234. if (keyword.length < 3) {
  235. nameArray.push(keyword)
  236. } else {
  237. nameArray = nodejieba.cut(keyword)
  238. }
  239. //自定义处理
  240. nameArray = cusSegment(nameArray,keyword);
  241. console.log(nameArray);
  242. let allInfoPrice = await infoItemsModel.find(data.condition).lean().sort({"_id":1});
  243. let {totalSize,items,handleLongWord} = getMatchPrice(allInfoPrice,nameArray)
  244. if(handleLongWord === true){
  245. nameArray = getShortNameArray(nameArray);
  246. console.log(`二次匹配:[${nameArray}]`);
  247. let newResult = getMatchPrice(allInfoPrice,nameArray,false)
  248. totalSize = newResult.totalSize;
  249. items = newResult.items;
  250. }
  251. //关键词按权重排序,为了给结果排序
  252. nameArray = _.sortBy(nameArray,(name)=>{
  253. if(nameWeightMap[name]) return nameWeightMap[name]*-1;//sortBy是升序排序,我们要的是权重越小排到越后即倒序 所以这里乘以-1
  254. return 1
  255. })
  256. //按匹配位置排序 如[ '橡胶', '胶圈', '给水' ] 先显示橡胶
  257. items = _.sortBy(items,(item)=>{
  258. let ms = item.mstring;
  259. for(let i = 0;i < nameArray.length;i ++){
  260. if (ms.indexOf(nameArray[i]) != -1) return i;
  261. }
  262. })
  263. return {totalSize,items}
  264. }
  265. async function mutiApplyInfoPrice(data,compilation){
  266. data.condition["compilationID"] = compilation._id;
  267. let infoPrices = await infoItemsModel.find(data.condition).lean();
  268. let tasks = [];
  269. let projectGLJMap = {};
  270. for(let info of infoPrices){
  271. let index = gljUtil.getIndex(info,["name","specs","unit"]);
  272. if(data.pgljMap[index]){
  273. for(let obj of data.pgljMap[index]){
  274. let infoPrice = gljUtil.getInfoMarketPrice(info,data.taxType);
  275. infoPrice = scMathUtil.roundToString(infoPrice,data.decimal);
  276. let doc = {'market_price':infoPrice,'priceFrom':data.priceFrom};
  277. let task = {
  278. updateOne:{
  279. filter:{'id':obj.unitPriceID},
  280. update:doc
  281. }
  282. };
  283. tasks.push(task);
  284. projectGLJMap[obj.pgljID] = {index:obj.fullIndex,doc:doc};
  285. }
  286. }
  287. }
  288. if(tasks.length > 0) await unitPriceModel.bulkWrite(tasks);
  289. return projectGLJMap;
  290. }