123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- package main
- import (
- "app.yhyue.com/BP/servicerd/proto"
- util "app.yhyue.com/data_processing/common_utils"
- "app.yhyue.com/data_processing/common_utils/redis"
- "context"
- "encoding/json"
- "fmt"
- "google.golang.org/grpc"
- jyProto "jy_member/proto"
- "net/http"
- "strings"
- )
- type JyService struct {
- }
- //附件
- type AtRequest struct {
- InfoId string
- }
- type AtResponse struct {
- Rep []map[string]interface{}
- }
- //预测
- type FcRequest struct {
- RedisFKey string
- Id string
- Pname string
- Buyer string
- Area string
- City string
- Budget float64
- BuyerContent []string
- }
- type FcResponse struct {
- Rep bool
- }
- func (j *JyService) Download(req AtRequest, res *AtResponse) error {
- //util.Debug("Download----infoid:", req.InfoId)
- if req.InfoId == "" {
- return fmt.Errorf(" _id error")
- }
- var err error
- res.Rep, err = Method(req.InfoId)
- return err
- }
- func (j *JyService) Forecast(req FcRequest, res *FcResponse) error {
- util.Debug("Forecast----ProjectId:", req)
- if req.RedisFKey == "" {
- res.Rep = false
- return fmt.Errorf("RedisFKey is nil")
- }
- if req.Id == "" {
- res.Rep = false
- return fmt.Errorf("ProjectId is nil")
- }
- if req.Pname == "" {
- res.Rep = false
- return fmt.Errorf("ProjectName is nil")
- }
- if len(req.BuyerContent) <= 0 {
- res.Rep = false
- return fmt.Errorf("BuyerContent is nil")
- }
- go ForecastMethod(&req)
- res.Rep = true
- return nil
- }
- func Method(infoid string) ([]map[string]interface{}, error) {
- fields := map[string]interface{}{"projectinfo": 1, "site": 1, "isValidFile": 1}
- info := &map[string]interface{}{}
- var resp []map[string]interface{}
- b := false
- info, b = MongoTool.FindById(MgoColl, infoid, fields)
- if !b {
- info, b = MongoTool.FindById(MgoColl1, infoid, fields)
- if !b {
- util.Debug("Not found data.")
- return resp, fmt.Errorf("Not found data.")
- }
- }
- if (*info)["isValidFile"] != nil && (*info)["isValidFile"].(bool) {
- if att, ok := (*info)["projectinfo"].(map[string]interface{}); ok {
- file := util.ObjToMap(att["attachments"])
- for _, v := range *file {
- attachment := map[string]interface{}{}
- if v1, ok := v.(map[string]interface{}); ok {
- if valiType(v1) {
- if ossid, ok := v1["ossid"].(string); ok {
- attachment["downurl"] = down_url + ossid
- attachment["filename"] = util.ObjToString(v1["filename"])
- attachment["org_url"] = util.ObjToString(v1["org_url"])
- attachment["size"] = util.ObjToString(v1["size"])
- resp = append(resp, attachment)
- } else if v1["fid"] != nil {
- if util.ObjToString((*info)["site"]) == "中国招标投标公共服务平台" && util.ObjToString(v1["size"]) == "8.4 KB" {
- break
- }
- attachment["downurl"] = down_url + util.ObjToString(v1["fid"])
- attachment["org_url"] = util.ObjToString(v1["org_url"])
- attachment["filename"] = util.ObjToString(v1["filename"])
- attachment["size"] = util.ObjToString(v1["size"])
- resp = append(resp, attachment)
- } else {
- // 不提供原文下载链接 2022.06.02
- //if govalidator.IsURL(util.ObjToString(v1["org_url"])) && util.ObjToString((*info)["site"]) != "中国招标投标公共服务平台" &&
- // util.ObjToString(v1["filename"]) != "文件下载.jpg" && util.ObjToString(v1["filename"]) != "文件下载.png" {
- // attachment["downurl"] = util.ObjToString(v1["org_url"])
- // attachment["org_url"] = util.ObjToString(v1["org_url"])
- // attachment["filename"] = util.ObjToString(v1["filename"])
- // attachment["size"] = util.ObjToString(v1["size"])
- // resp = append(resp, attachment)
- //}
- }
- }
- }
- }
- }
- }
- return resp, nil
- }
- func FindProject(projectId string, query map[string]interface{}) map[string]interface{} {
- client := Es.GetEsConn()
- defer Es.DestoryEsConn(client)
- esquery := `{"query": {"bool": {"must": [{"match": {"_id": "` + projectId + `"}}]}}}`
- data := Es.Get(Index, esquery)
- util.Debug(*data)
- if len(*data) > 0 {
- tmp := (*data)[0]
- if CheckContain(util.ObjToString(query["projectname"]), util.ObjToString(tmp["projectname"])) != 3 &&
- CheckContain(util.ObjToString(query["purchasing"]), util.ObjToString(tmp["purchasing"])) != 3 &&
- CheckContain(util.ObjToString(query["buyer"]), util.ObjToString(tmp["buyer"])) != 3 &&
- CheckContain(util.ObjToString(query["area"]), util.ObjToString(tmp["area"])) != 3 &&
- CheckContain(util.ObjToString(query["city"]), util.ObjToString(tmp["city"])) != 3 {
- return tmp
- } else {
- //项目名称或者标的物不相同,查询项目
- findPro := EsFindPro(query)
- if util.ObjToString(findPro["_id"]) == projectId {
- return findPro
- } else {
- //用户填写的内容
- findPro["projectname"] = query["projectname"]
- findPro["purchasing"] = query["purchasing"]
- if query["buyer"] != "" {
- findPro["buyer"] = query["buyer"]
- }
- if query["area"] != "" {
- findPro["area"] = query["area"]
- }
- if query["city"] != "" {
- findPro["city"] = query["city"]
- }
- if query["budget"] != 0 {
- findPro["budget"] = query["budget"]
- }
- }
- return findPro
- }
- } else {
- return EsFindPro(query)
- }
- }
- func EsFindPro(q map[string]interface{}) map[string]interface{} {
- esObj := NesEsObject{}
- if util.ObjToString(q["projectname"]) != "" {
- pobj := PnameObj{Match: ProjectName{
- Pname: util.ObjToString(q["projectname"]),
- }}
- esObj.Query.Should = append(esObj.Query.Should, pobj)
- }
- if q["purchasing"] != nil {
- purObj := PurObj{Match: Purchasing{
- Pur: q["purchasing"].([]string),
- }}
- esObj.Query.Should = append(esObj.Query.Should, purObj)
- }
- if util.ObjToString(q["buyer"]) != "" {
- bObj := BuyerObj{Match: Buyer{
- Buyer: util.ObjToString(q["buyer"]),
- }}
- esObj.Query.Should = append(esObj.Query.Should, bObj)
- }
- if util.ObjToString(q["area"]) != "" {
- areaObj := AreaObj{Match: Area{
- Area: util.ObjToString(q["area"]),
- }}
- esObj.Query.Should = append(esObj.Query.Should, areaObj)
- }
- if util.ObjToString(q["city"]) != "" {
- cityObj := CityObj{Match: City{
- City: util.ObjToString(q["city"]),
- }}
- esObj.Query.Should = append(esObj.Query.Should, cityObj)
- }
- rdata := make(map[string]interface{})
- rdata["query"] = esObj
- util.Debug(esObj)
- util.Debug(rdata)
- esbytes, _ := json.Marshal(rdata)
- util.Debug(string(esbytes))
- data := Es.Get(Index, string(esbytes))
- if len(*data) > 0 {
- return (*data)[0]
- } else {
- return map[string]interface{}{}
- }
- }
- func getSize(url string) int64 {
- resp, err := http.Get(url)
- if err != nil {
- fmt.Printf("HEAD failed: %v", err)
- }
- fmt.Printf("resp: %s\n", resp.Status)
- fmt.Printf("size: %d\n", resp.ContentLength)
- return resp.ContentLength
- }
- func valiType(tmp map[string]interface{}) bool {
- for _, v := range FileType {
- if util.ObjToString(v) == "*" {
- return true
- }
- if strings.Contains(util.ObjToString(tmp["filename"]), util.ObjToString(v)) {
- return true
- }
- if strings.Contains(util.ObjToString(tmp["ftype"]), util.ObjToString(v)) {
- return true
- }
- }
- return false
- }
- func CheckContain(b1, b2 string) (res int) {
- if b1 == b2 {
- res = 1 //相等
- return
- }
- bs1 := []rune(b1)
- bs2 := []rune(b2)
- tmp := ""
- for i := 0; i < len(bs1); i++ {
- for j := 0; j < len(bs2); j++ {
- if bs1[i] == bs2[j] {
- tmp += string(bs1[i])
- } else if tmp != "" {
- b1 = strings.Replace(b1, tmp, "", -1)
- b2 = strings.Replace(b2, tmp, "", -1)
- tmp = ""
- }
- }
- }
- if tmp != "" {
- b1 = strings.Replace(b1, tmp, "", -1)
- b2 = strings.Replace(b2, tmp, "", -1)
- }
- if b1 == b2 {
- res = 1 //相等
- } else if b1 == "" || b2 == "" {
- res = 2 //包含
- } else {
- res = 3 //不相同
- }
- return
- }
- func ForecastMethod(req *FcRequest) {
- query := map[string]interface{}{}
- query["projectname"] = req.Pname
- query["purchasing"] = req.BuyerContent
- if req.Buyer != "" {
- query["buyer"] = req.Buyer
- }
- if req.Area != "" {
- query["area"] = req.Area
- }
- if req.City != "" {
- query["city"] = req.City
- }
- query["budget"] = req.Budget
- result := FindProject(req.Id, query)
- wins := []string{}
- budget := 0.0
- if len(result) > 0 {
- if result["winnerorder"] != nil {
- wins = util.ObjArrToStringArr(result["winnerorder"].([]interface{}))
- }
- if result["buyer"] != nil && query["buyer"] == nil {
- query["buyer"] = result["buyer"]
- }
- if result["area"] != nil && query["area"] == nil {
- query["area"] = result["area"]
- }
- if result["city"] != nil && query["city"] == nil {
- query["city"] = result["city"]
- }
- if result["budget"] != nil {
- budget = util.Float64All(result["budget"])
- }
- if result["winner"] != nil && _ent.MatchString(util.ObjToString(result["winner"])) {
- query["winner"] = result["winner"]
- }
- }
- reqs := &jyProto.PredictReq{
- ProjectName: req.Pname,
- Mount: float32(budget),
- BidUnit: util.ObjToString(query["buyer"]),
- Area: util.ObjToString(query["area"]),
- City: util.ObjToString(query["city"]),
- Goods: req.BuyerContent,
- Winner: util.ObjToString(query["winner"]),
- WinnerOrder: wins}
- // 调用gRPC接口
- util.Debug(reqs)
- grpcConn(reqs, req.RedisFKey)
- }
- func grpcConn(reqs *jyProto.PredictReq, risKey string) {
- conn, err := grpc.Dial(ClientAddr, grpc.WithInsecure())
- if err != nil {
- redis.Put("predict_pro", risKey, "", -1)
- util.Debug(err)
- }
- var client proto.ServiceClient
- client = proto.NewServiceClient(conn)
- repl, err := client.Apply(context.Background(), &proto.ApplyReqData{Name: "winner_predict", Balance: 0})
- if err != nil {
- util.Debug(err)
- redis.Put("predict_pro", risKey, "", -1)
- return
- }
- util.Debug("结果", repl.Ip, repl.Port)
- //2.业务调用
- addr := fmt.Sprintf("%s:%d", repl.Ip, repl.Port)
- conn_b, err := grpc.Dial(addr, grpc.WithInsecure())
- if err != nil {
- util.Debug(err)
- redis.Put("predict_pro", risKey, "", -1)
- return
- }
- defer conn_b.Close()
- pc := jyProto.NewWinnerPredictServiceClient(conn_b)
- rep, err := pc.Predict(context.Background(), reqs)
- if err != nil {
- redis.Put("predict_pro", risKey, "", -1)
- util.Debug(err)
- } else {
- util.Debug(rep.Item)
- jsonStr, _ := json.Marshal(rep.Item)
- bol := redis.Put("predict_pro", risKey, string(jsonStr), -1)
- util.Debug("预测结果:", bol)
- }
- }
|