package statistic_report import ( "fmt" "hongze/hz_crm_api/models/company" "hongze/hz_crm_api/models/contract" "hongze/hz_crm_api/utils" "strings" "time" ) type UnusualCompanyRenewDataMap struct { UnusualRenewMap map[int]int `description:"续约异常客户数"` UnusualRenewIdMap map[int]string `description:"续约异常客户ids"` UnusualRenewTotal int `description:"续约异常客户总数(去重)"` FormalCompanyTotal int `description:"当前正常客户总数(去重)"` StartDate string `description:"开始日期"` EndDate string `description:"开始日期"` } // GetUnusualRenewWeekDataNum 周度数据 func GetUnusualRenewWeekDataNum(nowWeekMonday time.Time, productId, source, dataNum int) (adminDataList []UnusualCompanyRenewDataMap, firstDate time.Time, err error) { nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日 chanList := make([]chan UnusualCompanyRenewDataMap, 0) for i := 0; i < dataNum; i++ { tmpCh := make(chan UnusualCompanyRenewDataMap, 0) chanList = append(chanList, tmpCh) lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7*i) lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7*i) // 第一个开始日期 firstDate = lastWeekMonday go getUnusualRenewSectionData(productId, source, lastWeekMonday, lastWeekSunday, tmpCh) } adminDataList = make([]UnusualCompanyRenewDataMap, 0) for _, vChan := range chanList { var weekUnusualCompanyRenewDataMap UnusualCompanyRenewDataMap weekUnusualCompanyRenewDataMap = <-vChan close(vChan) adminDataList = append(adminDataList, weekUnusualCompanyRenewDataMap) } return } // GetUnusualRenewMonthDataNum 月度数据 func GetUnusualRenewMonthDataNum(nowMonthFirstDay time.Time, productId, source, dataNum int) (adminDataList []UnusualCompanyRenewDataMap, firstDate time.Time, err error) { //三个协程返回 //nowMonthLastDay := nowMonthFirstDay.AddDate(0, 1, 0).Add(-1 * time.Second) //本月最后一天 chanList := make([]chan UnusualCompanyRenewDataMap, 0) for i := 0; i < dataNum; i++ { tmpCh := make(chan UnusualCompanyRenewDataMap, 0) chanList = append(chanList, tmpCh) lastMonthFirstDay := nowMonthFirstDay.AddDate(0, -1*i, 0) //上个月第一天 lastMonthLastDay := nowMonthFirstDay.AddDate(0, -1*(i-1), 0).Add(-1 * time.Second) //上个月最后一天 // 第一个开始日期 firstDate = lastMonthFirstDay go getUnusualRenewSectionData(productId, source, lastMonthFirstDay, lastMonthLastDay, tmpCh) } adminDataList = make([]UnusualCompanyRenewDataMap, 0) for _, vChan := range chanList { var weekUnusualCompanyRenewDataMap UnusualCompanyRenewDataMap weekUnusualCompanyRenewDataMap = <-vChan close(vChan) adminDataList = append(adminDataList, weekUnusualCompanyRenewDataMap) } return } // GetUnusualRenewTimeIntervalData 区间数据 func GetUnusualRenewTimeIntervalData(productId, source int, startDate, endDate string) (adminDataMapList []UnusualCompanyRenewDataMap, firstDate time.Time, err error) { //三个协程返回 startDateTimer, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local) endDateTimer, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local) endDateTimer = endDateTimer.AddDate(0, 0, 1).Add(-1 * time.Second) //本月最后一天 //最早的一天 firstDate = startDateTimer ch1 := make(chan UnusualCompanyRenewDataMap, 0) go getUnusualRenewSectionData(productId, source, startDateTimer, endDateTimer, ch1) var adminDataMap UnusualCompanyRenewDataMap adminDataMap = <-ch1 close(ch1) adminDataMapList = make([]UnusualCompanyRenewDataMap, 0) adminDataMapList = append(adminDataMapList, adminDataMap) return } // getUnusualRenewSectionData 获取周期数据 func getUnusualRenewSectionData(productId, source int, startDate, endDate time.Time, ch chan UnusualCompanyRenewDataMap) (adminDataMap UnusualCompanyRenewDataMap, err error) { defer func() { ch <- adminDataMap }() unusualRenewMap := make(map[int]int) // 续约异常客户 unusualRenewCompanyIdMap := make(map[int]string) // 续约异常客户id var unusualRenewCompanyTotal, formalCompanyTotal int //续约异常客户总数 , 当前正式的客户数 // 当前正式的客户数 formalCompanyTotal, err = contract.GetCountFormalCompany(endDate, productId) if err != nil { utils.FileLog.Error(err.Error()) return } // 续约异常 { data, tmpErr := company.GetUnusualRenewCompanyByShareSellerList(startDate, endDate, productId, source) if tmpErr != nil { err = tmpErr utils.FileLog.Error(err.Error()) return } unusualRenewCompanyIdListMap := make(map[int][]string) // 续约异常客户 for _, v := range data { // 共享人id为空 if v.ShareSellerId <= 0 { continue } // 共享人id不为空,且不是所属销售 tmp, ok := unusualRenewMap[v.ShareSellerId] if !ok { tmp = 0 } companyIdSlice := strings.Split(v.CompanyIds, ",") unusualRenewMap[v.ShareSellerId] = tmp + len(companyIdSlice) // 客户id tmpCompanyIdList2, ok := unusualRenewCompanyIdListMap[v.ShareSellerId] if !ok { tmpCompanyIdList2 = make([]string, 0) } unusualRenewCompanyIdListMap[v.ShareSellerId] = append(tmpCompanyIdList2, fmt.Sprint(v.CompanyIds)) unusualRenewCompanyTotal += len(companyIdSlice) } for sellerId, tmpCompanyIdList := range unusualRenewCompanyIdListMap { unusualRenewCompanyIdMap[sellerId] = strings.Join(tmpCompanyIdList, ",") } } adminDataMap = UnusualCompanyRenewDataMap{ UnusualRenewMap: unusualRenewMap, UnusualRenewIdMap: unusualRenewCompanyIdMap, UnusualRenewTotal: unusualRenewCompanyTotal, FormalCompanyTotal: formalCompanyTotal, StartDate: startDate.Format(utils.FormatDate), EndDate: endDate.Format(utils.FormatDate), } return }