service.go 10.0 KB

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