|
|
@@ -2,16 +2,19 @@ package home
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "database/sql"
|
|
|
- "fmt"
|
|
|
-
|
|
|
"dashoo.cn/micro/app/model/home"
|
|
|
+ "dashoo.cn/micro/app/model/plat"
|
|
|
"dashoo.cn/micro/app/service"
|
|
|
contractService "dashoo.cn/micro/app/service/contract"
|
|
|
+ platService "dashoo.cn/micro/app/service/plat"
|
|
|
"dashoo.cn/opms_libary/micro_srv"
|
|
|
+ "database/sql"
|
|
|
+ "fmt"
|
|
|
"github.com/gogf/gf/database/gdb"
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
+ "math"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
// getPersonalContractReportData 获取个人数据
|
|
|
@@ -147,7 +150,7 @@ func getCompanyContractReportData(ctx context.Context, dataType string, params *
|
|
|
// 统计合同值
|
|
|
contractModel := srv.Dao.DB.Model("ctr_contract").Group("ctr_contract.incharge_id").Order("ctr_contract.incharge_id ASC")
|
|
|
if dataType != "COLLECTION" {
|
|
|
- contracts, err := contractModel.Where(dateWhere1).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract.contract_amount) contract_amount").FindAll()
|
|
|
+ contracts, err := contractModel.Where(dateWhere1).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, count(ctr_contract.contract_amount) contract_amount").FindAll()
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -264,6 +267,115 @@ func getQuarterGoalReportData(ctx context.Context, dataType string, params *map[
|
|
|
return &reportData, nil
|
|
|
}
|
|
|
|
|
|
+//客户现场打卡频次
|
|
|
+func getClockfrequency(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
|
|
|
+ var reportData home.ReportData
|
|
|
+ realMap := make(map[int]gdb.Record, 0)
|
|
|
+ dateWhere1 := "" // 查询条件
|
|
|
+ date := gtime.Now()
|
|
|
+ nowTime := gtime.Datetime()
|
|
|
+ if params != nil && (*params)["date"] != nil {
|
|
|
+ date = gconv.GTime((*params)["date"])
|
|
|
+ }
|
|
|
+
|
|
|
+ if params != nil && (*params)["searchType"] != nil {
|
|
|
+ searchType := gconv.String((*params)["searchType"])
|
|
|
+ if searchType == "month" {
|
|
|
+ dateWhere1 = fmt.Sprintf("plat_punch_records.punch_time LIKE '%v'", date.Format("Y-m")+"-%")
|
|
|
+ } else if searchType == "week" {
|
|
|
+ weekday := transferWeekday(date.Weekday())
|
|
|
+ fmt.Println(weekday)
|
|
|
+ beforeTime := gtime.NewFromStr(nowTime).AddDate(0, 0, -gconv.Int(weekday)).String()
|
|
|
+ endTime := gtime.NewFromStr(nowTime).AddDate(0, 0, 7-gconv.Int(weekday)).String()
|
|
|
+ dateWhere1 = fmt.Sprintf("plat_punch_records.punch_time between '%v' and '%v'", beforeTime, endTime)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ srv, err := platService.NewPunchRecordsService(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ platpunchrecords, err := srv.Dao.DB.Model("plat_punch_records").Where(dateWhere1).Fields("plat_punch_records.user_nick_name,Count(plat_punch_records.user_nick_name) as punch_sum").Group("plat_punch_records.user_nick_name").FindAll()
|
|
|
+ if err != nil && err != sql.ErrNoRows {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ // 赋值实际值
|
|
|
+ for index, target := range platpunchrecords {
|
|
|
+ reportData.XData = append(reportData.XData, target["user_nick_name"].String())
|
|
|
+ reportData.YDataTarget = append(reportData.YDataTarget, target["punch_sum"].Float64())
|
|
|
+ if realData, ok := realMap[target["punch_sum"].Int()]; ok {
|
|
|
+ reportData.YDataReal = append(reportData.YDataReal, realData["punch_sum"].Float64())
|
|
|
+ } else {
|
|
|
+ reportData.YDataReal = append(reportData.YDataReal, 0)
|
|
|
+ }
|
|
|
+ if reportData.YDataTarget[index] == 0 {
|
|
|
+ reportData.PercentData = append(reportData.PercentData, 0)
|
|
|
+ } else {
|
|
|
+ reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index]*100/reportData.YDataTarget[index])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return &reportData, nil
|
|
|
+}
|
|
|
+
|
|
|
+//销售工程师跟进记录的打卡频次
|
|
|
+func getFollowUpRecord(ctx context.Context, dataType string, params *map[string]interface{}) ([]plat.Statistics, error) {
|
|
|
+ now := time.Now()
|
|
|
+ date := gconv.Int(gtime.Now().Month())
|
|
|
+ if params != nil && (*params)["date"] != nil {
|
|
|
+ date = gconv.Int((*params)["date"])
|
|
|
+ }
|
|
|
+ // 获取本月的第一天
|
|
|
+ firstOfMonth := time.Date(now.Year(), time.Month(date), 1, 0, 0, 0, 0, now.Location())
|
|
|
+ // 获取本月的最后一天
|
|
|
+ lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
|
|
|
+
|
|
|
+ l, _ := time.LoadLocation("Asia/Shanghai")
|
|
|
+
|
|
|
+ startTime, _ := time.ParseInLocation("2006-01-02", firstOfMonth.Format("2006-01-02"), l)
|
|
|
+
|
|
|
+ endTime, _ := time.ParseInLocation("2006-01-02", lastOfMonth.Format("2006-01-02"), l)
|
|
|
+
|
|
|
+ datas := GroupByWeekDate(startTime, endTime)
|
|
|
+
|
|
|
+ var Follows []plat.Followlist
|
|
|
+
|
|
|
+ srv, err := platService.NewFollowupService(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ var stats []plat.Statistics
|
|
|
+ for _, d := range datas {
|
|
|
+
|
|
|
+ err = srv.Dao.DB.Model("plat_followup").
|
|
|
+ Where("follow_date>=? and follow_date<=?", d.StartTime.Format("2006-01-02"), d.EndTime.Format("2006-01-02")).Fields("count(follow_type)as num ,follow_type").
|
|
|
+ Group("date_format(follow_date,'%v'),follow_type").Scan(&Follows)
|
|
|
+ if err != nil && err != sql.ErrNoRows {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if len(Follows) > 0 {
|
|
|
+ var stat plat.Statistics
|
|
|
+ for _, val := range Follows {
|
|
|
+
|
|
|
+ if val.FollowType == "10" {
|
|
|
+ stat.Phone = gconv.Int(val.Num)
|
|
|
+ } else if val.FollowType == "20" {
|
|
|
+ stat.Mail = gconv.Int(val.Num)
|
|
|
+ } else if val.FollowType == "30" {
|
|
|
+ stat.PayVisit = gconv.Int(val.Num)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ stat.Total = stat.Phone + stat.Mail + stat.PayVisit
|
|
|
+ stat.Week = d.WeekTh + "周"
|
|
|
+ stats = append(stats, stat)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return stats, nil
|
|
|
+
|
|
|
+}
|
|
|
func getQuarterWhere(date *gtime.Time, field string) string {
|
|
|
if date.Month() >= 1 && date.Month() <= 3 {
|
|
|
return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"01-01 00:00:00", field, date.Format("Y-")+"04-01 00:00:00")
|
|
|
@@ -276,7 +388,6 @@ func getQuarterWhere(date *gtime.Time, field string) string {
|
|
|
}
|
|
|
return ""
|
|
|
}
|
|
|
-
|
|
|
func getQuarterMonthWhere(date *gtime.Time, field string) string {
|
|
|
if date.Month() >= 1 && date.Month() <= 3 {
|
|
|
return fmt.Sprintf("%v >= %v AND %v < %v", field, 1, field, 4)
|
|
|
@@ -289,3 +400,156 @@ func getQuarterMonthWhere(date *gtime.Time, field string) string {
|
|
|
}
|
|
|
return ""
|
|
|
}
|
|
|
+func transferWeekday(day time.Weekday) string {
|
|
|
+ switch day {
|
|
|
+ case time.Monday:
|
|
|
+ return "1"
|
|
|
+ case time.Tuesday:
|
|
|
+ return "2"
|
|
|
+ case time.Wednesday:
|
|
|
+ return "3"
|
|
|
+ case time.Thursday:
|
|
|
+ return "4"
|
|
|
+ case time.Friday:
|
|
|
+ return "5"
|
|
|
+ case time.Saturday:
|
|
|
+ return "6"
|
|
|
+ case time.Sunday:
|
|
|
+ return "7"
|
|
|
+ default:
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//判断时间是当年的第几周
|
|
|
+func WeekByDate(t time.Time) string {
|
|
|
+
|
|
|
+ yearDay := t.YearDay()
|
|
|
+
|
|
|
+ yearFirstDay := t.AddDate(0, 0, -yearDay+1)
|
|
|
+
|
|
|
+ firstDayInWeek := int(yearFirstDay.Weekday())
|
|
|
+
|
|
|
+ //今年第一周有几天
|
|
|
+
|
|
|
+ firstWeekDays := 1
|
|
|
+
|
|
|
+ if firstDayInWeek != 0 {
|
|
|
+
|
|
|
+ firstWeekDays = 7 - firstDayInWeek + 1
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ var week int
|
|
|
+ Weeks := WeekByDates(t)
|
|
|
+ if yearDay <= firstWeekDays {
|
|
|
+
|
|
|
+ week = 1
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ week = (yearDay-firstWeekDays)/7 + 2
|
|
|
+
|
|
|
+ }
|
|
|
+ return fmt.Sprintf("%d", week-gconv.Int(Weeks)+1)
|
|
|
+}
|
|
|
+func WeekByDates(now time.Time) int {
|
|
|
+ l, _ := time.LoadLocation("Asia/Shanghai")
|
|
|
+ // 获取本月的第一天
|
|
|
+ firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
|
+ endTime, _ := time.ParseInLocation("2006-01-02", firstOfMonth.Format("2006-01-02"), l)
|
|
|
+ t := endTime
|
|
|
+ yearDay := t.YearDay()
|
|
|
+
|
|
|
+ yearFirstDay := t.AddDate(0, 0, -yearDay+1)
|
|
|
+
|
|
|
+ firstDayInWeek := int(yearFirstDay.Weekday())
|
|
|
+
|
|
|
+ //今年第一周有几天
|
|
|
+
|
|
|
+ firstWeekDays := 1
|
|
|
+
|
|
|
+ if firstDayInWeek != 0 {
|
|
|
+
|
|
|
+ firstWeekDays = 7 - firstDayInWeek + 1
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ var week int
|
|
|
+
|
|
|
+ if yearDay <= firstWeekDays {
|
|
|
+
|
|
|
+ week = 1
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ week = (yearDay-firstWeekDays)/7 + 2
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return week
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// 将开始时间和结束时间分割为周为单位
|
|
|
+func GroupByWeekDate(startTime, endTime time.Time) []plat.WeekDate {
|
|
|
+
|
|
|
+ weekDate := make([]plat.WeekDate, 0)
|
|
|
+
|
|
|
+ diffDuration := endTime.Sub(startTime)
|
|
|
+
|
|
|
+ days := int(math.Ceil(float64(diffDuration/(time.Hour*24)))) + 1
|
|
|
+
|
|
|
+ currentWeekDate := plat.WeekDate{}
|
|
|
+
|
|
|
+ currentWeekDate.WeekTh = WeekByDate(endTime)
|
|
|
+
|
|
|
+ currentWeekDate.EndTime = endTime
|
|
|
+
|
|
|
+ currentWeekDay := int(endTime.Weekday())
|
|
|
+
|
|
|
+ if currentWeekDay == 0 {
|
|
|
+
|
|
|
+ currentWeekDay = 7
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ currentWeekDate.StartTime = endTime.AddDate(0, 0, -currentWeekDay+1)
|
|
|
+
|
|
|
+ nextWeekEndTime := currentWeekDate.StartTime
|
|
|
+
|
|
|
+ weekDate = append(weekDate, currentWeekDate)
|
|
|
+
|
|
|
+ for i := 0; i < (days-currentWeekDay)/7; i++ {
|
|
|
+
|
|
|
+ weekData := plat.WeekDate{}
|
|
|
+
|
|
|
+ weekData.EndTime = nextWeekEndTime
|
|
|
+
|
|
|
+ weekData.StartTime = nextWeekEndTime.AddDate(0, 0, -7)
|
|
|
+
|
|
|
+ weekData.WeekTh = WeekByDate(weekData.StartTime)
|
|
|
+
|
|
|
+ nextWeekEndTime = weekData.StartTime
|
|
|
+
|
|
|
+ weekDate = append(weekDate, weekData)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if lastDays := (days - currentWeekDay) % 7; lastDays > 0 {
|
|
|
+
|
|
|
+ lastData := plat.WeekDate{}
|
|
|
+
|
|
|
+ lastData.EndTime = nextWeekEndTime
|
|
|
+
|
|
|
+ lastData.StartTime = nextWeekEndTime.AddDate(0, 0, -lastDays)
|
|
|
+
|
|
|
+ lastData.WeekTh = WeekByDate(lastData.StartTime)
|
|
|
+
|
|
|
+ weekDate = append(weekDate, lastData)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return weekDate
|
|
|
+
|
|
|
+}
|