123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- package push
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/encrypt"
- "app.yhyue.com/moapp/jybase/go-xweb/xweb"
- "app.yhyue.com/moapp/jybase/log"
- "app.yhyue.com/moapp/jybase/mongodb"
- "app.yhyue.com/moapp/jybase/redis"
- "bytes"
- . "cmplatform/util"
- "encoding/json"
- "fmt"
- "github.com/tealeg/xlsx"
- "go.uber.org/zap"
- "net/url"
- "strings"
- "time"
- )
- type Push struct {
- *xweb.Action
- index xweb.Mapper `xweb:"/push/index"` //首页
- dataPush xweb.Mapper `xweb:"/push/dataPush"` //数据推送
- }
- var (
- // fields1 = "city,district,projectname,bidamount,s_winner,winner_type,buyer,publishtime,buyer_type,href,winner_style,bidtype,budget,agency,agency_fee,projectcode,jybxhref,id,projectId"
- fields1 = "city,projectname,budget,bidopentime,buyer,publishtime,buyer_type,href,district,bidtype,bidway,bidendtime,docamount,agency,buyerperson,buyertel,projectcode,id,projectId,jybxhref"
- fields2 = "city,projectname,bidamount,s_winner,winner_type,buyer,publishtime,buyer_type,href,district,winner_style,bidtype,budget,agency,agency_fee,projectcode,jybxhref,id,projectId"
- SE = encrypt.SimpleEncrypt{Key: "topJYBX2019"}
- subMap = map[string]map[string]string{
- "招标": map[string]string{"0": "3"},
- "竞谈": map[string]string{"0": "3"},
- "单一": map[string]string{"0": "3"},
- "询价": map[string]string{"0": "3"},
- "竞价": map[string]string{"0": "3"},
- "邀标": map[string]string{"0": "3"},
- "论证意见": map[string]string{"1": "2"},
- "预告": map[string]string{"1": "2"},
- "预审": map[string]string{"1": "2"},
- "预审结果": map[string]string{"1": "2"},
- "需求公示": map[string]string{"1": "2"},
- "采购意向": map[string]string{"1": "2"},
- "变更": map[string]string{"2": "5"},
- "废标": map[string]string{"3": "7"},
- "流标": map[string]string{"3": "8"},
- "中标": map[string]string{"3": "10"},
- "成交": map[string]string{"3": "11"},
- "结果变更": map[string]string{"3": "11"},
- "合同": map[string]string{"3": "12"},
- "验收": map[string]string{"5": "13"},
- "违规": map[string]string{"5": "14"},
- }
- bidtypeMap = map[string]string{
- "招标": "0",
- "邀标": "1",
- "竞谈": "2",
- "单一": "3",
- "询价": "4",
- "竞价": "6",
- }
- subtypeMap = map[string]string{
- "招标": "0",
- "邀标": "1",
- "竞谈": "2",
- "单一": "3",
- "询价": "4",
- "其它": "5",
- "竞价": "6",
- }
- )
- func (this *Push) Index() {
- this.Render("/push/index.html")
- }
- func (this *Push) DataPush() {
- appid := this.GetString("appid")
- getDataType := this.GetString("getDataType")
- dataType := this.GetString("dataType")
- historyId := this.GetString("historyId")
- dataSource := this.GetString("dataSource")
- dataMap := map[string][]map[string]interface{}{}
- fields := ""
- if dataType == "0" {
- fields = fields1
- } else {
- fields = fields2
- }
- if appid == "jyIhxXQQoJAgVZQURLLwRE" {
- errstr := ttCRM(dataSource, historyId)
- this.ServeJson(map[string]interface{}{"errMsg": errstr})
- } else {
- if getDataType == "0" {
- query := map[string]interface{}{"appid": appid, "historyId": historyId}
- // fieldMap := map[string]int{}
- // for _, v := range strings.Split(fields, ",") {
- // fieldMap[v] = 1
- // }
- data, ok := MgoSave.Find(dataSource, query, nil, nil, false, -1, -1)
- if ok && data != nil && len(*data) > 0 {
- datasArr := []map[string]interface{}{}
- for _, v := range *data {
- datamap := map[string]interface{}{}
- for _, vv := range strings.Split(fields, ",") {
- if v[vv] != nil {
- datamap[vv] = fmt.Sprint(v[vv])
- } else {
- datamap[vv] = ""
- }
- }
- id := common.ObjToString(v["id"])
- datamap["id"] = SE.EncodeString(id)
- datasArr = append(datasArr, datamap)
- }
- dataMap["dataList"] = datasArr
- }
- } else if getDataType == "1" {
- dataMap["dataList"] = ExcelParse(dataType)
- }
- log.Debug("", zap.Any("dataMap", dataMap))
- databyte, _ := json.Marshal(dataMap)
- dataStr := string(databyte)
- //
- token := getAccessToken()
- // token := "f9cb92a6-356b-433f-a8bd-09f1deef6d52"
- log.Debug("token111:" + token)
- //
- tokens := getAccessTokens(token)
- log.Debug("token222:" + tokens)
- //
- dataStr = strings.ReplaceAll(dataStr, `"`, `\"`)
- data := `{"access_token":"` + tokens + `","postjson":"` + dataStr + `"}`
- log.Debug("data:", zap.Any("data", data))
- dataTypes := ""
- if dataType == "0" {
- dataTypes = "JY_TO_DICT_fishBid2"
- } else {
- dataTypes = "JY_TO_DICT_WinBid2"
- }
- apiurl := "http://111.7.112.55:20110/oppf?method=" + dataTypes + "&appId=1068246&appKey=15c1da231a946f9615ee9d56138952a2&format=json&busiSerial=1&OPCODE=A888888&version=1.0&accessToken=" + tokens + "×tamp=" + fmt.Sprint(time.Now().Format("20060102150405")) + "&sign=abc"
- log.Debug("apiurl:" + apiurl)
- errstr := ""
- response, err := HttpPostJson(apiurl, data)
- result := map[string]interface{}{}
- if err == nil {
- errstr = string(response)
- resMap := common.ObjToMap(string(response))
- result = *resMap
- log.Debug("---", zap.Any("响应信息: ", *resMap))
- if resMap != nil && *resMap != nil {
- respCode := common.ObjToString((*resMap)["respCode"])
- if respCode == "00000" {
- log.Debug("成功")
- } else {
- log.Debug("异常")
- }
- }
- } else {
- errstr = "响应失败 " + err.Error()
- log.Error("响应失败 ", zap.Error(err))
- }
- MgoSave.Save("dataPushLog", map[string]interface{}{
- "dataLenth": len(dataMap["dataList"]),
- "createtime": time.Now().Unix(),
- "appid": appid,
- "getDataType": getDataType,
- "dataType": dataType,
- "historyId": historyId,
- "dataSource": dataSource,
- "result": result,
- "errMsg": errstr,
- })
- this.ServeJson(map[string]interface{}{"errMsg": errstr})
- }
- }
- func ExcelParse(dataType string) []map[string]interface{} {
- filePath := "./web/res/push/push.xlsx"
- xlFile, _ := xlsx.OpenFile(filePath)
- fields := ""
- if dataType == "0" {
- fields = fields1
- } else {
- fields = fields2
- }
- //获取行数
- length := len(xlFile.Sheets[0].Rows)
- //开辟除表头外的行数的数组内存
- resourceArr := make([]map[string]interface{}, length-1)
- //遍历sheet
- for _, sheet := range xlFile.Sheets {
- //遍历每一行
- for rowIndex, row := range sheet.Rows {
- //跳过第一行表头信息
- if rowIndex == 0 {
- continue
- }
- dataMap := map[string]interface{}{}
- fieldsArr := strings.Split(fields, ",")
- for i := 0; i <= len(fieldsArr); i++ {
- dataMap[fieldsArr[i]] = row.Cells[i].String()
- }
- //遍历每一个单元
- // for cellIndex, cell := range row.Cells {
- // text := cell.String()
- // if text != "" {
- // //如果是每一行的第一个单元格
- // if cellIndex == 0 {
- // }
- // }
- // }
- resourceArr[rowIndex-1] = dataMap
- }
- }
- return resourceArr
- }
- func getAccessToken() string {
- res := redis.GetStr("datag", "yidongtoken1")
- if res != "" {
- return res
- } else {
- apiurls := "http://111.7.112.55:20200/aopoauth/oauth/token?app_id=1077846&app_key=c1165e4efc7b83831c4b76282f7f61c2&grant_type=client_credentials"
- data := make(url.Values)
- now := time.Now().Unix()
- bs, err := HttpPostForm(apiurls, map[string]string{
- "timestamp": fmt.Sprint(now),
- }, data)
- log.Debug("返回内容", zap.Error(err), zap.String("string(bs)", string(bs)))
- resMap := common.ObjToMap(string(bs))
- if *resMap != nil {
- log.Debug("调用token成功 ", zap.Any("resMap", *resMap))
- token := common.ObjToString((*resMap)["access_token"])
- if token != "" {
- log.Debug("成功")
- redis.Put("datag", "yidongtoken1", token, 60*60*24)
- return token
- } else {
- log.Debug("异常")
- return ""
- }
- } else {
- log.Debug("调用token失败")
- return ""
- }
- }
- }
- func getAccessTokens(token string) string {
- res := redis.GetStr("datag", "yidongtoken2")
- if res != "" {
- return res
- } else {
- apiurls := "http://111.7.112.55:20110/oppf?method=JY_TO_DICT_TOKEN&appId=1068246&appKey=15c1da231a946f9615ee9d56138952a2&format=json&busiSerial=1&OPCODE=A888888&version=1.0&accessToken=" + token + "×tamp=" + fmt.Sprint(time.Now().Format("20060102150405")) + "&sign=abc"
- log.Debug("EPMtokenapiurl:" + apiurls)
- grant_type := "client_credentials"
- scope := "all"
- client_id := "jianyu_client"
- client_secret := "ab7b85f23f181936bb22078a2c83cbc3"
- data := make(map[string]string)
- data["grant_type"] = grant_type
- data["scope"] = scope
- data["client_id"] = client_id
- data["client_secret"] = client_secret
- bytestr, _ := json.Marshal(data)
- bs, err := HttpPostJson(apiurls, string(bytestr))
- if err != nil {
- log.Error("调用 token 失败", zap.Error(err))
- }
- resMap := common.ObjToMap(string(bs))
- if *resMap != nil {
- log.Debug("调用token成功 ", zap.Any("resMap", *resMap))
- resMaps := common.ObjToMap((*resMap)["result"])
- jsonMap := common.ObjToMap((*resMaps)["response"])
- redis.Put("datag", "yidongtoken2", common.ObjToString((*jsonMap)["access_token"]), 7200)
- return common.ObjToString((*jsonMap)["access_token"])
- } else {
- log.Debug("调用token失败")
- return ""
- }
- }
- }
- func ttCRM(dataSource, historyId string) string {
- query, errStr := map[string]interface{}{"historyId": historyId}, ""
- data, ok := MgoSave.Find(dataSource, query, nil, nil, false, -1, -1)
- if ok && data != nil && len(*data) > 0 {
- for _, v := range *data {
- id := common.ObjToString(v["infoId"])
- v["infoId"] = SE.EncodeString(id)
- bidtype := common.ObjToString(v["biddingType"])
- toptype := common.ObjToString(v["infoType"])
- subtype := common.ObjToString(v["infoTypeSegment"])
- bidway := common.ObjToString(v["isElectronic"])
- if bidtype != "" {
- v["biddingType"] = bidtypeMap[bidtype]
- } else {
- v["biddingType"] = subtypeMap[subtype]
- }
- if bidway == "电子投标" {
- v["isElectronic"] = "1"
- } else {
- v["isElectronic"] = "0"
- }
- if subtype == "其它" {
- if toptype == "预告" {
- v["infoType"] = "1"
- v["infoTypeSegment"] = "2"
- } else if toptype == "招标" {
- v["infoType"] = "2"
- v["infoTypeSegment"] = "6"
- } else if toptype == "结果" {
- v["infoType"] = "3"
- v["infoTypeSegment"] = "9"
- } else {
- v["infoType"] = "5"
- v["infoTypeSegment"] = "15"
- }
- } else {
- for t, s := range subMap[subtype] {
- v["infoType"] = t
- v["infoTypeSegment"] = s
- }
- }
- infos, ok := MgoBidding.FindOne(BiddingColl, map[string]interface{}{"_id": mongodb.StringTOBsonId(id)})
- if ok && infos != nil && len(*infos) > 0 {
- info := *infos
- if info["projectinfo"] != nil {
- projectInfo := common.ObjToMap(info["projectinfo"])
- if projectInfo != nil && len(*projectInfo) > 0 {
- if (*projectInfo)["attachments"] != nil {
- filesArr := []string{}
- fileUrl := "http://jy-datafile.oss-cn-beijing.aliyuncs.com/"
- if attachments, oks := (*projectInfo)["attachments"].(map[string]interface{}); oks {
- for _, v := range attachments {
- if atta, ok := v.(map[string]interface{}); ok {
- if atta["fid"] != nil && common.ObjToString(atta["fid"]) != "" {
- filesArr = append(filesArr, fileUrl+common.ObjToString(atta["fid"]))
- } else {
- if atta["org_url"] != nil {
- filesArr = append(filesArr, common.ObjToString(atta["org_url"]))
- }
- }
- }
- }
- }
- log.Debug("查询附件结果:" + id + ", 附件数量:" + fmt.Sprint(len(filesArr)))
- if len(filesArr) > 0 {
- v["infoFile"] = strings.Join(filesArr, ",")
- }
- }
- }
- }
- }
- delete(v, "_id")
- delete(v, "appid")
- delete(v, "createtime")
- delete(v, "historyId")
- errStr = postTT(dataSource, v)
- if strings.Contains(errStr, "失败") || strings.Contains(errStr, "错误") {
- return errStr
- }
- }
- }
- return errStr
- }
- func postTT(dataSource string, data map[string]interface{}) string {
- apiurl := "https://zlbss-crm.chinatowercom.cn/erp/syncdata/open/objdata/push"
- log.Debug("apiurl:" + apiurl)
- header := map[string]string{
- "token": "43f8e3c05a8edf8ee85bbc8755043b01",
- "tenantId": "40070007",
- "objectApiName": "object_HDngT__c",
- "dataCenterId": "649d4eb19ced6c0001587f26",
- "id": common.ObjToString(data["infoId"]),
- "version": "v1",
- "Content-Type": "application/json",
- "directSync": "true",
- "destObjectApiName": "object_HDngT__c",
- }
- dataMap := map[string]interface{}{"objAPIName": "object_HDngT__c", "masterFieldVal": data, "detailFieldVals": map[string]interface{}{}}
- dataJson, _ := json.Marshal(&dataMap)
- log.Debug("dataJson: " + string(dataJson))
- response, err := HttpPost(apiurl, header, bytes.NewReader(dataJson))
- errstr := ""
- if err == nil {
- errstr = string(response)
- resMap := common.ObjToMap(string(response))
- log.Debug("响应信息 ", zap.Any("resMap", *resMap))
- if resMap != nil && *resMap != nil {
- errMsg := common.ObjToString((*resMap)["errMsg"])
- if errMsg == "成功" {
- log.Debug("成功")
- }
- }
- } else {
- errstr = "响应失败 " + err.Error()
- log.Debug("响应失败 ", zap.Error(err))
- }
- return errstr
- }
|