123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package main
- import (
- "fmt"
- utils "jygit.jydev.jianyu360.cn/data_processing/common_utils"
- "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
- "sort"
- "time"
- )
- var (
- MgoB *mongodb.MongodbSim
- MgoC *mongodb.MongodbSim
- channels = []string{"招标公告", "重新招标", "意见征集", "招标预告", "信息变更", "答疑公告", "废标公告", "流标公告",
- "开标公示", "候选人公示", "中标通知", "合同公告", "验收合同", "违规公告", "其他公告", "预告", "公告", "变更", "结果", "其他"}
- )
- func main() {
- MgoB = &mongodb.MongodbSim{
- MongodbAddr: "172.17.189.140:27080",
- //MongodbAddr: "127.0.0.1:27083",
- DbName: "qfw",
- Size: 10,
- UserName: "SJZY_RWbid_ES",
- Password: "SJZY@B4i4D5e6S",
- //Direct: true,
- }
- MgoB.InitPool()
- MgoC = &mongodb.MongodbSim{
- MongodbAddr: "172.17.4.87:27080",
- //MongodbAddr: "127.0.0.1:27081",
- DbName: "qlm",
- Size: 10,
- UserName: "",
- Password: "",
- //Direct: true,
- }
- MgoC.InitPool()
- dealData()
- fmt.Println("over")
- }
- func dealData() {
- //5.竞品覆盖率,每周4统计上周的数据
- sessC := MgoC.GetMgoConn()
- defer MgoC.DestoryMongoConn(sessC)
- //获取上周3,千里马的招标数据;然后获取标讯前后个3天,一共7天的所有数据,对比看标题或者项目名称是否存在
- lastWednesday := time.Date(2023, 12, 2, 0, 0, 0, 0, time.Local)
- whereQlm := map[string]interface{}{
- "publishtime": lastWednesday.Format("2006-01-02"),
- "site": "千里马",
- }
- query := sessC.DB("qlm").C("data_merge").Find(whereQlm).Select(map[string]interface{}{"title": 1, "projectname": 1, "channel": 1, "href": 1}).Iter()
- count := 0
- qlmData := make([]map[string]interface{}, 0) //标讯所有数据
- for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
- data := map[string]interface{}{
- "title": tmp["title"],
- "projectname": tmp["projectname"],
- "href": tmp["href"],
- }
- channel := utils.ObjToString(tmp["channel"])
- //标讯所有数据
- if IsInStringArray(channel, channels) {
- qlmData = append(qlmData, data)
- }
- }
- biddingWhere := map[string]interface{}{
- "publishtime": map[string]interface{}{
- "$gte": lastWednesday.AddDate(0, 0, -3).Unix(),
- "$lte": lastWednesday.AddDate(0, 0, 3).Unix(),
- },
- }
- biddingDatas, _ := MgoB.Find("bidding", biddingWhere, nil, map[string]interface{}{"title": 1, "projectname": 1}, false, -1, -1)
- //log.Info("coverageA", zap.Int("标讯一周总数", len(*biddingDatas)))
- fmt.Println("bbbbb", len(*biddingDatas))
- // 将切片B中的标题和项目名称分别存储在哈希表中
- titlesInB, projectsInB := getUniqueFields(*biddingDatas)
- //5.1.1 统计 标讯-整体 数据
- matches := countMatches(qlmData, titlesInB, projectsInB)
- fmt.Println("matches", matches)
- for _, itemA := range qlmData {
- title, titleExists := itemA["title"].(string)
- project, projectExists := itemA["projectname"].(string)
- insert := map[string]interface{}{
- "title": itemA["title"],
- "projectname": itemA["projectname"],
- "href": itemA["href"],
- }
- if titleExists && titlesInB[title] {
- insert["matched"] = true
- insert["match_field"] = "title"
- } else if projectExists && projectsInB[project] {
- insert["matched"] = true
- insert["match_field"] = "projectname"
- } else {
- insert["matched"] = false
- }
- MgoB.Save("wcc_cover2", insert)
- }
- }
- // IsInStringArray 判断数组中是否存在字符串
- func IsInStringArray(str string, arr []string) bool {
- // 先对字符串数组进行排序
- sort.Strings(arr)
- // 使用二分查找算法查找字符串
- pos := sort.SearchStrings(arr, str)
- // 如果找到了则返回 true,否则返回 false
- return pos < len(arr) && arr[pos] == str
- }
- // getUniqueFields 获取切片中标题和项目名称的唯一值
- func getUniqueFields(slice []map[string]interface{}) (map[string]bool, map[string]bool) {
- titles := make(map[string]bool)
- projects := make(map[string]bool)
- for _, item := range slice {
- title, titleExists := item["title"].(string)
- project, projectExists := item["projectname"].(string)
- if titleExists {
- titles[title] = true
- }
- if projectExists {
- projects[project] = true
- }
- }
- return titles, projects
- }
- // countMatches 统计切片A中的元素在切片B中存在的数量和总数量
- func countMatches(sliceA []map[string]interface{}, titlesInB, projectsInB map[string]bool) int {
- count := 0
- for _, itemA := range sliceA {
- title, titleExists := itemA["title"].(string)
- project, projectExists := itemA["projectname"].(string)
- if titleExists && titlesInB[title] {
- count++
- } else if projectExists && projectsInB[project] {
- count++
- }
- }
- return count
- }
|