package utility import ( "analyze/internal/model/entity" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "fmt" "math" "strings" "time" ) var ( y_m_day = map[int]int{1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31} ) func GetPreviousMarket(sTime, eTime time.Time) int64 { var os_time int64 s_time := sTime if SEMonth(sTime, eTime) { var min int //统计月份 for sTime.Before(eTime) { sTime = sTime.AddDate(0, 1, 0) min++ } os_time = s_time.AddDate(0, -min, 0).Unix() } else { os_time = s_time.AddDate(0, 0, -int(math.Ceil(eTime.Sub(sTime).Hours()/24))).Unix() } return os_time } // 判断是否月初到月末 func SEMonth(sTime, eTime time.Time) bool { var day int month := int(eTime.Month()) if month == 2 { if eTime.Year()%4 == 0 { day = 29 } else { day = 28 } } else { day = y_m_day[month] } if sTime.Day() == 1 && eTime.Day() == day { return true } return false } func GetMonthData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) { //整月多取一个月进行环比 if SEMonth(sTime, eTime) { _b = true sTime = sTime.AddDate(0, -1, 0) } dateSpans = getBidAmountStatistics(sTime, eTime) return } func getBidAmountStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) { tmpTime, rTime, tEndTime := sTime, sTime, getMonthRange(eTime, false) for rTime.Before(tEndTime) { ts, te := getMonthRange(tmpTime, true), getMonthRange(tmpTime, false) if sTime == tmpTime { ts = sTime } if te == tEndTime { te = eTime } if ts.Before(te) { dateSpans = append(dateSpans, entity.DateSpan{ Key: fmt.Sprintf("%d-%d", ts.Year(), ts.Month()), From: ts.Unix(), To: te.Unix(), }) } rTime = rTime.AddDate(0, 1, 0) if int(rTime.Month())-int(tmpTime.Month()) > 1 { rTime = rTime.AddDate(0, -1, 0) } tmpTime = rTime } return } // getMonthRange获取月份范围 // isStart true本月月初 false 本月月末(下月月初) func getMonthRange(t time.Time, isStart bool) time.Time { if isStart { return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()) } return time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location()) } func GetYearData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) { //整年多取一年进行环比 if sTime.Month() == 1 && sTime.Day() == 1 && eTime.Month() == 12 && eTime.Day() == 31 { _b = true sTime = sTime.AddDate(-1, 0, 0) } dateSpans = getCommonYearStatistics(sTime, eTime) return } // 年份统计 func getCommonYearStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) { tmpTime, rTime, tEndTime := sTime, sTime, getYearRange(eTime, false) for rTime.Before(tEndTime) { ts, te := getYearRange(tmpTime, true), getYearRange(tmpTime, false) if sTime == tmpTime { ts = sTime } if te == tEndTime { te = eTime } if ts.Before(te) { dateSpans = append(dateSpans, entity.DateSpan{ Key: fmt.Sprintf("%d", ts.Year()), From: ts.Unix(), To: te.Unix(), }) } rTime = rTime.AddDate(1, 0, 0) tmpTime = rTime } return } // getYearRange获取月份范围 // isStart true本月月初 false 本月月末(下月月初) func getYearRange(t time.Time, isStart bool) time.Time { if isStart { return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location()) } return time.Date(t.Year()+1, 1, 1, 0, 0, 0, 0, t.Location()) } // GetAllKeywordArr 获取所有匹配词 func GetAllKeywordArr(res []entity.KeyWordGroup) (rData []entity.ViewKeyWord) { for _, kwg := range res { rData = append(rData, GetGroupKeywordArr(kwg.A_Key)...) } return } // GetGroupKeywordArr 模糊拆分为多个精准匹配 func GetGroupKeywordArr(res []entity.ViewKeyWord) (rData []entity.ViewKeyWord) { for _, kw := range res { if kw.MatchWay == 1 { for _, kk := range kw.Keyword { rData = append(rData, entity.ViewKeyWord{ Keyword: []string{kk}, Exclude: kw.Exclude, }) } for _, kk := range kw.Appended { rData = append(rData, entity.ViewKeyWord{ Keyword: []string{kk}, Exclude: kw.Exclude, }) } } else { rData = append(rData, kw) } } return } func DateIsIn(from, to, n, m int64) bool { if m == 0 { return to >= n && from <= n } return to > n && from < n } func EncodeId(sid string) string { if sid == "" || sid == "-" { //不存在的id为- return "" } return encrypt.EncodeArticleId2ByCheck(sid) } // 项目金额---TOP3地区的重点中标单位 func GetWinnerInfoOfAmount(pt *entity.ProjectInfo) []*entity.WinnerAmountS { var winnerAmount []*entity.WinnerAmountS if pt.Winners != "" { for k, w := range strings.Split(pt.Winners, ",") { winnerAmount = append(winnerAmount, &entity.WinnerAmountS{ Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string), Winner: w, WinnerAmount: pt.Sortprice, }) } } return winnerAmount } // 项目数量---TOP3地区的重点中标单位 func GetWinnerInfoOfCount(pt *entity.ProjectInfo) []*entity.WinnerTotalS { var winnerCount []*entity.WinnerTotalS if pt.Winners != "" { for k, w := range strings.Split(pt.Winners, ",") { winnerCount = append(winnerCount, &entity.WinnerTotalS{ Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string), Winner: w, WinnerTotal: 1, }) } } return winnerCount } // 细化市场重点中标单位-项目数量|金额 func GetWinnerInfoTopList(pt *entity.ProjectInfo, value, total float64) []*entity.TopListS { var winnerTotal []*entity.TopListS if pt.Winners != "" { for k, w := range strings.Split(pt.Winners, ",") { winnerTotal = append(winnerTotal, &entity.TopListS{ Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string), Name: w, Value: value, Prop: Formula(value, total), }) } } return winnerTotal } // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位 func GetWinnerInfoOfBuyerCount(pt *entity.ProjectInfo) []*entity.Winnertop3Number { var winnertop3Number []*entity.Winnertop3Number if pt.Winners != "" { for k, w := range strings.Split(pt.Winners, ",") { winnertop3Number = append(winnertop3Number, &entity.Winnertop3Number{ Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string), Name: w, Number: 1, }) } } return winnertop3Number } // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位 func GetWinnerInfoOfBuyerAmount(pt *entity.ProjectInfo) []*entity.Winnertop3Amount { var winnertop3Amount []*entity.Winnertop3Amount if pt.Winners != "" { for k, w := range strings.Split(pt.Winners, ",") { winnertop3Amount = append(winnertop3Amount, &entity.Winnertop3Amount{ Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string), Name: w, Amount: pt.Sortprice, }) } } return winnertop3Amount } // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位 func GetWinnerInfoOfWinnerCount(pt *entity.ProjectInfo) []*entity.Buyertop3Number { var buyertop3Number []*entity.Buyertop3Number if pt.Buyer != "" { buyertop3Number = append(buyertop3Number, &entity.Buyertop3Number{ Name: pt.Buyer, Number: 1, }) } return buyertop3Number } // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位 func GetWinnerInfoOfWinnerAmount(pt *entity.ProjectInfo) []*entity.Buyertop3Amount { var buyertop3Amount []*entity.Buyertop3Amount if pt.Buyer != "" { buyertop3Amount = append(buyertop3Amount, &entity.Buyertop3Amount{ Name: pt.Buyer, Amount: pt.Sortprice, }) } return buyertop3Amount } func GetJudgmentPrevKey(first int, next string) (key string) { return fmt.Sprintf("%d-%s", first, next) }