info_price_facade.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. //要先知道根据期数和编办查找库ID
  47. let newList = [];
  48. let lib = await infoLibModel.findOne({compilationID:compilation._id,period:data.period})
  49. if(lib){
  50. let tList =await getClassList(data.areaID,lib.ID);
  51. newList.push(...tList);
  52. if(data.commonInfoPriceID){
  53. cList = await getClassList(data.commonInfoPriceID,lib.ID);
  54. newList.push(...cList);
  55. }
  56. }
  57. async function getClassList(areaID,libID){
  58. let temList = [];
  59. let infoClass = await infoClassModel.find({areaID:areaID,libID:libID}).lean();
  60. let parentMap=_.groupBy(infoClass, 'ParentID');
  61. for(let key in parentMap){
  62. parentMap[key] = projectfacade.sortChildren(parentMap[key]);
  63. }
  64. if(parentMap && parentMap['-1']){
  65. getChildern(parentMap['-1'],temList,parentMap)
  66. }
  67. return temList;
  68. }
  69. function getChildern(children,list,pm){
  70. for(let c of children){
  71. list.push(c);
  72. if(pm[c.ID]){
  73. getChildern(pm[c.ID],list,pm)
  74. }
  75. }
  76. }
  77. return newList;
  78. }
  79. async function getDataByCondition(data,compilation){
  80. let result = {};
  81. data.condition["compilationID"] = compilation._id;
  82. //特殊处理重庆的,地区选择非“通用”时,搜索范围应是当前选择的地区,加上“通用”中的信息价。
  83. if (data.condition.commonInfoPriceID) {
  84. let idArray = [data.condition.areaID,data.condition.commonInfoPriceID];
  85. data.condition.areaID = {$in: idArray}
  86. delete data.condition.commonInfoPriceID;
  87. }
  88. //根据地区+期数+材料编号的前4位与信息价材料的分类编号匹配,如果有数据,则显示数据出来。
  89. //先按编号匹配
  90. if (data.code) {
  91. result = await getDataByCode(data.code, data);
  92. if (result.totalSize > 0) return result;
  93. }
  94. //编号匹配不上的情况:
  95. //有关键字的情况
  96. if (data.keyWord) {
  97. return await getDataByKeyWord(data.keyWord,data);
  98. }
  99. //查询所有的情况
  100. if(data.lastID){ //有最后一行说明是查询下一页
  101. data.condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)};
  102. }else{
  103. result.totalSize = await infoItemsModel.find(data.condition).count();
  104. }
  105. result.items = await infoItemsModel.find(data.condition).lean().sort({"_id":1}).limit(50);
  106. return result;
  107. }
  108. async function getDataByCode(code, data) {
  109. let condition = { ...data.condition };
  110. condition.code = code;
  111. let totalSize = await infoItemsModel.find(condition).count();
  112. if (data.lastID) { //有最后一行说明是查询下一页
  113. condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)};
  114. }
  115. let items = [];
  116. if (totalSize > 0) {
  117. items = await infoItemsModel.find(condition).lean().sort({"_id":1}).limit(50);
  118. }
  119. return {totalSize,items}
  120. }
  121. async function getDataByKeyWord(keyword, data) {
  122. //先按全字匹配
  123. let fullResult =await getDataByFullKeyWord(keyword, data);
  124. if(fullResult.totalSize > 0) return fullResult;
  125. //如果没有才按模糊匹配
  126. return await getDataByFuzzyMatch(keyword, data);
  127. }
  128. //按全关键字匹配
  129. async function getDataByFullKeyWord(keyword, data){
  130. data.condition.name = new RegExp(keyword);
  131. let items = await infoItemsModel.find(data.condition).lean().sort({"_id":1});
  132. delete data.condition.name;
  133. return{totalSize:items.length,items}
  134. }
  135. function handelThreeWord(word){
  136. let arr = [];
  137. getArr(word[0]+word[1],arr);
  138. getArr(word[1]+word[2],arr);
  139. if(arr.length > 0) return arr;
  140. return[word];
  141. function getArr(tem,list){
  142. let nameArray = segment.doSegment(tem, {
  143. simple: true, //不返回词性
  144. stripPunctuation: true //去除标点符号
  145. });
  146. //说明是一个词
  147. if(nameArray.length == 1) list.push(tem)
  148. }
  149. }
  150. //自定义特殊处理
  151. function cusSegment(nameArray,keyword){
  152. let temArr = [];
  153. for (let a of nameArray) {
  154. //混凝土 和 砼 统一认成砼
  155. if (a == "混凝土") a = '砼';
  156. if(a == '砼'||a == '砖' ){
  157. temArr.push(a);
  158. }else if(a.length > 1){
  159. //如果是三个字的词,优先识别成两个字
  160. if(a.length == 3){
  161. let sArr = handelThreeWord(a);
  162. temArr.push(...sArr);
  163. }else{
  164. temArr.push(a);
  165. }
  166. }
  167. }
  168. if (keyword.length == 1 && temArr.length == 0) temArr.push(keyword);
  169. return temArr;
  170. }
  171. //模糊匹配
  172. async function getDataByFuzzyMatch(keyword, data){
  173. let items = [];
  174. let nameArray = [];
  175. if (keyword.length < 3) {
  176. nameArray.push(keyword)
  177. } else {
  178. nameArray = segment.doSegment(keyword, {
  179. simple: true, //不返回词性
  180. stripPunctuation: true //去除标点符号
  181. });
  182. }
  183. //自定义处理
  184. nameArray = cusSegment(nameArray,keyword);
  185. console.log(nameArray);
  186. let allInfoPrice = await infoItemsModel.find(data.condition).lean().sort({"_id":1});
  187. let maxNum = 0;//最大匹配数
  188. let matchMap = {};//匹配储存
  189. for (let info of allInfoPrice) {
  190. //specs
  191. let mstring = info.name + info.spec;
  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. }
  199. }
  200. if (matchCount > 0) {
  201. matchMap[matchCount] ? matchMap[matchCount].push(info) : matchMap[matchCount] = [info];
  202. if (matchCount > maxNum) maxNum = matchCount;
  203. }
  204. }
  205. if (maxNum > 0) items = matchMap[maxNum];
  206. totalSize = items.length
  207. //按匹配位置排序 如[ '橡胶', '胶圈', '给水' ] 先显示橡胶
  208. items = _.sortBy(items,(item)=>{
  209. let ms = item.mstring;
  210. for(let i = 0;i < nameArray.length;i ++){
  211. if (ms.indexOf(nameArray[i]) != -1) return i;
  212. }
  213. })
  214. return {totalSize,items}
  215. }
  216. async function mutiApplyInfoPrice(data,compilation){
  217. data.condition["compilationID"] = compilation._id;
  218. let infoPrices = await infoItemsModel.find(data.condition).lean();
  219. let tasks = [];
  220. let projectGLJMap = {};
  221. for(let info of infoPrices){
  222. let index = gljUtil.getIndex(info,["name","specs","unit"]);
  223. if(data.pgljMap[index]){
  224. for(let obj of data.pgljMap[index]){
  225. let infoPrice = gljUtil.getInfoMarketPrice(info,data.taxType);
  226. infoPrice = scMathUtil.roundToString(infoPrice,data.decimal);
  227. let doc = {'market_price':infoPrice,'priceFrom':data.priceFrom};
  228. let task = {
  229. updateOne:{
  230. filter:{'id':obj.unitPriceID},
  231. update:doc
  232. }
  233. };
  234. tasks.push(task);
  235. projectGLJMap[obj.pgljID] = {index:obj.fullIndex,doc:doc};
  236. }
  237. }
  238. }
  239. if(tasks.length > 0) await unitPriceModel.bulkWrite(tasks);
  240. return projectGLJMap;
  241. }