service.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. es "github.com/olivere/elastic/v7"
  7. "google.golang.org/grpc"
  8. jyProto "jy_member/proto"
  9. "jygit.jydev.jianyu360.cn/BP/servicerd/proto"
  10. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/redis"
  12. "net/http"
  13. "strings"
  14. )
  15. type JyService struct {
  16. }
  17. // 附件
  18. type AtRequest struct {
  19. InfoId string
  20. }
  21. type AtResponse struct {
  22. Rep []map[string]interface{}
  23. }
  24. // 预测
  25. type FcRequest struct {
  26. RedisFKey string
  27. Id string
  28. Pname string
  29. Buyer string
  30. Area string
  31. City string
  32. Budget float64
  33. BuyerContent []string
  34. }
  35. type FcResponse struct {
  36. Rep bool
  37. }
  38. func (j *JyService) Download(req AtRequest, res *AtResponse) error {
  39. util.Debug("Download----infoid:", req.InfoId)
  40. if req.InfoId == "" {
  41. return fmt.Errorf(" _id error")
  42. }
  43. var err error
  44. res.Rep, err = Method(req.InfoId)
  45. util.Debug(res.Rep)
  46. return err
  47. }
  48. func (j *JyService) Forecast(req FcRequest, res *FcResponse) error {
  49. util.Debug("Forecast----ProjectId:", req)
  50. if req.RedisFKey == "" {
  51. res.Rep = false
  52. return fmt.Errorf("RedisFKey is nil")
  53. }
  54. if req.Id == "" {
  55. res.Rep = false
  56. return fmt.Errorf("ProjectId is nil")
  57. }
  58. if req.Pname == "" {
  59. res.Rep = false
  60. return fmt.Errorf("ProjectName is nil")
  61. }
  62. if len(req.BuyerContent) <= 0 {
  63. res.Rep = false
  64. return fmt.Errorf("BuyerContent is nil")
  65. }
  66. go ForecastMethod(&req)
  67. res.Rep = true
  68. return nil
  69. }
  70. func Method(infoid string) ([]map[string]interface{}, error) {
  71. fields := map[string]interface{}{"projectinfo": 1, "site": 1, "isValidFile": 1}
  72. info := &map[string]interface{}{}
  73. var resp []map[string]interface{}
  74. b := false
  75. info, b = MongoTool.FindById(MgoColl, infoid, fields)
  76. if !b {
  77. info, b = MongoTool.FindById(MgoColl1, infoid, fields)
  78. if !b {
  79. util.Debug("Not found data.")
  80. return resp, fmt.Errorf("Not found data.")
  81. }
  82. }
  83. if (*info)["isValidFile"] != nil && (*info)["isValidFile"].(bool) {
  84. if att, ok := (*info)["projectinfo"].(map[string]interface{}); ok {
  85. file := util.ObjToMap(att["attachments"])
  86. for _, v := range *file {
  87. attachment := map[string]interface{}{}
  88. if v1, ok := v.(map[string]interface{}); ok {
  89. if valiType(v1) {
  90. if ossid, ok := v1["ossid"].(string); ok {
  91. attachment["downurl"] = down_url + ossid
  92. attachment["filename"] = util.ObjToString(v1["filename"])
  93. attachment["org_url"] = util.ObjToString(v1["org_url"])
  94. attachment["size"] = util.ObjToString(v1["size"])
  95. resp = append(resp, attachment)
  96. } else if v1["fid"] != nil {
  97. if util.ObjToString((*info)["site"]) == "中国招标投标公共服务平台" && util.ObjToString(v1["size"]) == "8.4 KB" {
  98. break
  99. }
  100. attachment["downurl"] = down_url + util.ObjToString(v1["fid"])
  101. attachment["org_url"] = util.ObjToString(v1["org_url"])
  102. attachment["filename"] = util.ObjToString(v1["filename"])
  103. attachment["size"] = util.ObjToString(v1["size"])
  104. resp = append(resp, attachment)
  105. } else {
  106. // 不提供原文下载链接 2022.06.02
  107. //if govalidator.IsURL(util.ObjToString(v1["org_url"])) && util.ObjToString((*info)["site"]) != "中国招标投标公共服务平台" &&
  108. // util.ObjToString(v1["filename"]) != "文件下载.jpg" && util.ObjToString(v1["filename"]) != "文件下载.png" {
  109. // attachment["downurl"] = util.ObjToString(v1["org_url"])
  110. // attachment["org_url"] = util.ObjToString(v1["org_url"])
  111. // attachment["filename"] = util.ObjToString(v1["filename"])
  112. // attachment["size"] = util.ObjToString(v1["size"])
  113. // resp = append(resp, attachment)
  114. //}
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return resp, nil
  122. }
  123. func FindProject(projectId string, query map[string]interface{}) map[string]interface{} {
  124. client := Es.GetEsConn()
  125. defer Es.DestoryEsConn(client)
  126. //esquery := `{"query": {"bool": {"must": [{"match": {"_id": "` + projectId + `"}}]}}}`
  127. q := es.NewBoolQuery().Must(es.NewTermQuery("_id", projectId))
  128. data := Es.Get(Index, q)
  129. util.Debug(*data)
  130. if len(*data) > 0 {
  131. tmp := (*data)[0]
  132. if CheckContain(util.ObjToString(query["projectname"]), util.ObjToString(tmp["projectname"])) != 3 &&
  133. CheckContain(util.ObjToString(query["purchasing"]), util.ObjToString(tmp["purchasing"])) != 3 &&
  134. CheckContain(util.ObjToString(query["buyer"]), util.ObjToString(tmp["buyer"])) != 3 &&
  135. CheckContain(util.ObjToString(query["area"]), util.ObjToString(tmp["area"])) != 3 &&
  136. CheckContain(util.ObjToString(query["city"]), util.ObjToString(tmp["city"])) != 3 {
  137. return tmp
  138. } else {
  139. //项目名称或者标的物不相同,查询项目
  140. findPro := EsFindPro(query)
  141. if util.ObjToString(findPro["_id"]) == projectId {
  142. return findPro
  143. } else {
  144. //用户填写的内容
  145. findPro["projectname"] = query["projectname"]
  146. findPro["purchasing"] = query["purchasing"]
  147. if query["buyer"] != "" {
  148. findPro["buyer"] = query["buyer"]
  149. }
  150. if query["area"] != "" {
  151. findPro["area"] = query["area"]
  152. }
  153. if query["city"] != "" {
  154. findPro["city"] = query["city"]
  155. }
  156. if query["budget"] != 0 {
  157. findPro["budget"] = query["budget"]
  158. }
  159. }
  160. return findPro
  161. }
  162. } else {
  163. return EsFindPro(query)
  164. }
  165. }
  166. func EsFindPro(q map[string]interface{}) map[string]interface{} {
  167. esObj := NesEsObject{}
  168. if util.ObjToString(q["projectname"]) != "" {
  169. pobj := PnameObj{Match: ProjectName{
  170. Pname: util.ObjToString(q["projectname"]),
  171. }}
  172. esObj.Query.Should = append(esObj.Query.Should, pobj)
  173. }
  174. if q["purchasing"] != nil {
  175. purObj := PurObj{Match: Purchasing{
  176. Pur: q["purchasing"].([]string),
  177. }}
  178. esObj.Query.Should = append(esObj.Query.Should, purObj)
  179. }
  180. if util.ObjToString(q["buyer"]) != "" {
  181. bObj := BuyerObj{Match: Buyer{
  182. Buyer: util.ObjToString(q["buyer"]),
  183. }}
  184. esObj.Query.Should = append(esObj.Query.Should, bObj)
  185. }
  186. if util.ObjToString(q["area"]) != "" {
  187. areaObj := AreaObj{Match: Area{
  188. Area: util.ObjToString(q["area"]),
  189. }}
  190. esObj.Query.Should = append(esObj.Query.Should, areaObj)
  191. }
  192. if util.ObjToString(q["city"]) != "" {
  193. cityObj := CityObj{Match: City{
  194. City: util.ObjToString(q["city"]),
  195. }}
  196. esObj.Query.Should = append(esObj.Query.Should, cityObj)
  197. }
  198. rdata := make(map[string]interface{})
  199. rdata["query"] = esObj
  200. util.Debug(esObj)
  201. util.Debug(rdata)
  202. //esbytes, _ := json.Marshal(rdata)
  203. //util.Debug(string(esbytes))
  204. //data := Es.Get(Index, string(esbytes))
  205. //if len(*data) > 0 {
  206. // return (*data)[0]
  207. //} else {
  208. return map[string]interface{}{}
  209. //}
  210. }
  211. func getSize(url string) int64 {
  212. resp, err := http.Get(url)
  213. if err != nil {
  214. fmt.Printf("HEAD failed: %v", err)
  215. }
  216. fmt.Printf("resp: %s\n", resp.Status)
  217. fmt.Printf("size: %d\n", resp.ContentLength)
  218. return resp.ContentLength
  219. }
  220. func valiType(tmp map[string]interface{}) bool {
  221. for _, v := range FileType {
  222. if util.ObjToString(v) == "*" {
  223. return true
  224. }
  225. if strings.Contains(util.ObjToString(tmp["filename"]), util.ObjToString(v)) {
  226. return true
  227. }
  228. if strings.Contains(util.ObjToString(tmp["ftype"]), util.ObjToString(v)) {
  229. return true
  230. }
  231. }
  232. return false
  233. }
  234. func CheckContain(b1, b2 string) (res int) {
  235. if b1 == b2 {
  236. res = 1 //相等
  237. return
  238. }
  239. bs1 := []rune(b1)
  240. bs2 := []rune(b2)
  241. tmp := ""
  242. for i := 0; i < len(bs1); i++ {
  243. for j := 0; j < len(bs2); j++ {
  244. if bs1[i] == bs2[j] {
  245. tmp += string(bs1[i])
  246. } else if tmp != "" {
  247. b1 = strings.Replace(b1, tmp, "", -1)
  248. b2 = strings.Replace(b2, tmp, "", -1)
  249. tmp = ""
  250. }
  251. }
  252. }
  253. if tmp != "" {
  254. b1 = strings.Replace(b1, tmp, "", -1)
  255. b2 = strings.Replace(b2, tmp, "", -1)
  256. }
  257. if b1 == b2 {
  258. res = 1 //相等
  259. } else if b1 == "" || b2 == "" {
  260. res = 2 //包含
  261. } else {
  262. res = 3 //不相同
  263. }
  264. return
  265. }
  266. func ForecastMethod(req *FcRequest) {
  267. query := map[string]interface{}{}
  268. query["projectname"] = req.Pname
  269. query["purchasing"] = req.BuyerContent
  270. if req.Buyer != "" {
  271. query["buyer"] = req.Buyer
  272. }
  273. if req.Area != "" {
  274. query["area"] = req.Area
  275. }
  276. if req.City != "" {
  277. query["city"] = req.City
  278. }
  279. query["budget"] = req.Budget
  280. result := FindProject(req.Id, query)
  281. wins := []string{}
  282. budget := 0.0
  283. if len(result) > 0 {
  284. if result["winnerorder"] != nil {
  285. wins = util.ObjArrToStringArr(result["winnerorder"].([]interface{}))
  286. }
  287. if result["buyer"] != nil && query["buyer"] == nil {
  288. query["buyer"] = result["buyer"]
  289. }
  290. if result["area"] != nil && query["area"] == nil {
  291. query["area"] = result["area"]
  292. }
  293. if result["city"] != nil && query["city"] == nil {
  294. query["city"] = result["city"]
  295. }
  296. if result["budget"] != nil {
  297. budget = util.Float64All(result["budget"])
  298. }
  299. if result["winner"] != nil && _ent.MatchString(util.ObjToString(result["winner"])) {
  300. query["winner"] = result["winner"]
  301. }
  302. }
  303. reqs := &jyProto.PredictReq{
  304. ProjectName: req.Pname,
  305. Mount: float32(budget),
  306. BidUnit: util.ObjToString(query["buyer"]),
  307. Area: util.ObjToString(query["area"]),
  308. City: util.ObjToString(query["city"]),
  309. Goods: req.BuyerContent,
  310. Winner: util.ObjToString(query["winner"]),
  311. WinnerOrder: wins}
  312. // 调用gRPC接口
  313. util.Debug(reqs)
  314. grpcConn(reqs, req.RedisFKey)
  315. }
  316. func grpcConn(reqs *jyProto.PredictReq, risKey string) {
  317. conn, err := grpc.Dial(ClientAddr, grpc.WithInsecure())
  318. if err != nil {
  319. redis.Put("predict_pro", risKey, "", -1)
  320. util.Debug(err)
  321. }
  322. var client proto.ServiceClient
  323. client = proto.NewServiceClient(conn)
  324. repl, err := client.Apply(context.Background(), &proto.ApplyReqData{Name: "winner_predict", Balance: 0})
  325. if err != nil {
  326. util.Debug(err)
  327. redis.Put("predict_pro", risKey, "", -1)
  328. return
  329. }
  330. util.Debug("结果", repl.Ip, repl.Port)
  331. //2.业务调用
  332. addr := fmt.Sprintf("%s:%d", repl.Ip, repl.Port)
  333. conn_b, err := grpc.Dial(addr, grpc.WithInsecure())
  334. if err != nil {
  335. util.Debug(err)
  336. redis.Put("predict_pro", risKey, "", -1)
  337. return
  338. }
  339. defer conn_b.Close()
  340. pc := jyProto.NewWinnerPredictServiceClient(conn_b)
  341. rep, err := pc.Predict(context.Background(), reqs)
  342. if err != nil {
  343. redis.Put("predict_pro", risKey, "", -1)
  344. util.Debug(err)
  345. } else {
  346. util.Debug(rep.Item)
  347. jsonStr, _ := json.Marshal(rep.Item)
  348. bol := redis.Put("predict_pro", risKey, string(jsonStr), -1)
  349. util.Debug("预测结果:", bol)
  350. }
  351. }