| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package home
- import (
- "context"
- "database/sql"
- "fmt"
- "dashoo.cn/micro/app/model/home"
- "dashoo.cn/micro/app/service"
- contractService "dashoo.cn/micro/app/service/contract"
- "dashoo.cn/opms_libary/micro_srv"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gconv"
- )
- // getPersonalContractReportData 获取个人数据
- // dataType:collection为回款;其他为合同
- func getPersonalContractReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
- var reportData home.ReportData
- targetMap := make(map[int]gdb.Record, 0)
- realMap := make(map[int][]*gdb.Record, 0)
- targetField := "sales_target"
- realField := "contract_amount"
- year := gtime.Now().Format("Y")
- if params != nil && (*params)["year"] != nil {
- year = gconv.String((*params)["year"])
- }
- // 统计字段
- if dataType == "COLLECTION" {
- targetField = "collection_target"
- realField = "collection_amount"
- }
- srv, err := contractService.NewCtrContractService(ctx)
- if err != nil {
- return nil, err
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- targets, err := srv.Dao.DB.Model("rpt_sales_target").Where("sale_user_id", userInfo.Id).Order("monthly ASC").FindAll()
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- for index, target := range targets {
- targetMap[target["monthly"].Int()] = targets[index]
- }
- // 统计12个月份的数据
- // 统计目标值
- for index := 1; index <= 12; index++ {
- reportData.XData = append(reportData.XData, fmt.Sprintf("%v月", index))
- if target, ok := targetMap[index]; ok {
- reportData.YDataTarget = append(reportData.YDataTarget, target[targetField].Float64())
- } else {
- reportData.YDataTarget = append(reportData.YDataTarget, 0)
- }
- }
- // 统计合同值
- contractModel := srv.Dao.DB.Model("ctr_contract").Where("ctr_contract.incharge_id", userInfo.Id)
- if dataType != "COLLECTION" {
- contracts, err := contractModel.Where("ctr_contract.contract_start_time LIKE ?", year+"-%").Order("ctr_contract.contract_start_time ASC").FindAll()
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- for index, contract := range contracts {
- realMap[contract["contract_start_time"].GTime().Month()] = append(realMap[contract["contract_start_time"].GTime().Month()], &contracts[index])
- }
- } else {
- // 回款数据统计
- collections, err := contractModel.InnerJoin("ctr_contract_collection", "ctr_contract.id=ctr_contract_collection.contract_id").Where("ctr_contract_collection.appro_status", "20").Where("ctr_contract_collection.collection_datetime LIKE ?", year+"-%").Order("ctr_contract_collection.collection_datetime ASC").Fields("ctr_contract_collection.*").FindAll()
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- for index, collection := range collections {
- realMap[collection["collection_datetime"].GTime().Month()] = append(realMap[collection["collection_datetime"].GTime().Month()], &collections[index])
- }
- }
- // 统计12个月份的数据
- // 统计实际值
- for index := 1; index <= 12; index++ {
- if realData, ok := realMap[index]; ok {
- realAmount := float64(0)
- for _, data := range realData {
- realAmount += (*data)[realField].Float64()
- }
- reportData.YDataReal = append(reportData.YDataReal, realAmount)
- } else {
- reportData.YDataReal = append(reportData.YDataReal, 0)
- }
- if reportData.YDataTarget[index-1] == 0 {
- reportData.PercentData = append(reportData.PercentData, 0)
- } else {
- reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index-1]*100/reportData.YDataTarget[index-1])
- }
- }
- return &reportData, nil
- }
- // getCompanyContractReportData 获取总部数据
- // dataType:collection为回款;其他为合同
- func getCompanyContractReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
- var reportData home.ReportData
- realMap := make(map[int]gdb.Record, 0)
- targetField := "sales_target"
- realField := "contract_amount"
- dateWhere1 := "" // 查询条件
- dateWhere2 := "" // 查询条件
- dateWhere3 := ""
- date := gtime.Now()
- if params != nil && (*params)["date"] != nil {
- date = gconv.GTime((*params)["date"])
- }
- dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y")+"-%")
- dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y")+"-%")
- if params != nil && (*params)["searchType"] != nil {
- searchType := gconv.String((*params)["searchType"])
- if searchType == "month" {
- dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y-m")+"-%")
- dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y-m")+"-%")
- dateWhere3 = fmt.Sprintf("monthly=%v", date.Month())
- } else if searchType == "quarter" {
- dateWhere1 = getQuarterWhere(date, "ctr_contract.contract_start_time")
- dateWhere2 = getQuarterWhere(date, "ctr_contract_collection.collection_datetime")
- dateWhere3 = getQuarterMonthWhere(date, "monthly")
- }
- }
- // 统计字段
- if dataType == "COLLECTION" {
- targetField = "collection_target"
- realField = "collection_amount"
- }
- srv, err := contractService.NewCtrContractService(ctx)
- if err != nil {
- return nil, err
- }
- targets, err := srv.Dao.DB.Model("rpt_sales_target").Where(dateWhere3).Order("sale_user_id ASC").Group("sale_user_id").Fields(fmt.Sprintf("sale_user_id, sale_user_name, SUM(%v) %v", targetField, targetField)).FindAll()
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- // 统计合同值
- 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()
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- for index, contract := range contracts {
- realMap[contract["incharge_id"].Int()] = contracts[index]
- }
- } else {
- // 回款数据统计
- collections, err := contractModel.InnerJoin("ctr_contract_collection", "ctr_contract.id=ctr_contract_collection.contract_id").Where("ctr_contract_collection.appro_status", "20").Where(dateWhere2).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract_collection.collection_amount) collection_amount").FindAll()
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- for index, collection := range collections {
- realMap[collection["incharge_id"].Int()] = collections[index]
- }
- }
- // 统计目标值和实际值
- for index, target := range targets {
- reportData.XData = append(reportData.XData, target["sale_user_name"].String())
- reportData.YDataTarget = append(reportData.YDataTarget, target[targetField].Float64())
- if realData, ok := realMap[target["sale_user_id"].Int()]; ok {
- reportData.YDataReal = append(reportData.YDataReal, realData[realField].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 getQuarterGoalReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
- if params == nil {
- return nil, fmt.Errorf("请输入年度")
- }
- p := *params
- year, _ := p["year"].(int)
- quarter, _ := p["quarter"].(int)
- if year == 0 {
- return nil, fmt.Errorf("请输入年度")
- }
- srv, err := contractService.NewCtrContractService(ctx)
- if err != nil {
- return nil, err
- }
- goal := map[string]float64{}
- goaldao := srv.GoalDao.Where("year = ?", year).Where("goal_type = ?", dataType)
- if quarter != 0 {
- goaldao = goaldao.Where("quarter = ?", quarter)
- } else {
- goaldao = goaldao.Where("quarter = 1 || quarter = 2 || quarter = 3 || quarter = 4")
- }
- goalent, err := goaldao.All()
- if err != nil {
- return nil, err
- }
- for _, ent := range goalent {
- goal[ent.ProductLine] += ent.Amount
- }
- got := map[string]float64{}
- ctrdao := srv.Dao.Where("year(created_time) = ?", year)
- if quarter != 0 {
- if quarter == 1 {
- ctrdao = ctrdao.Where("month(created_time) in (1,2,3)")
- }
- if quarter == 2 {
- ctrdao = ctrdao.Where("month(created_time) in (4,5,6)")
- }
- if quarter == 3 {
- ctrdao = ctrdao.Where("month(created_time) in (7,8,9)")
- }
- if quarter == 4 {
- ctrdao = ctrdao.Where("month(created_time) in (10,11,12)")
- }
- }
- ctrent, err := ctrdao.All()
- if err != nil {
- return nil, err
- }
- for _, ent := range ctrent {
- if dataType == "10" {
- got[ent.ProductLine] += ent.ContractAmount
- }
- if dataType == "20" {
- got[ent.ProductLine] += ent.CollectedAmount
- }
- }
- productLine, err := service.GetDictDataByType(ctx, "sys_product_line")
- if err != nil {
- return nil, err
- }
- productLineCode := []string{}
- productLineName := []string{}
- for k, v := range productLine {
- productLineCode = append(productLineCode, k)
- productLineName = append(productLineName, v)
- }
- var reportData home.ReportData
- for _, c := range productLineCode {
- reportData.XData = append(reportData.XData, productLine[c])
- reportData.YDataTarget = append(reportData.YDataTarget, goal[c]/10000)
- reportData.YDataReal = append(reportData.YDataReal, got[c]/10000)
- }
- return &reportData, 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")
- } else if date.Month() >= 4 && date.Month() <= 6 {
- return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"04-01 00:00:00", field, date.Format("Y-")+"07-01 00:00:00")
- } else if date.Month() >= 7 && date.Month() <= 9 {
- return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"07-01 00:00:00", field, date.Format("Y-")+"10-01 00:00:00")
- } else if date.Month() >= 10 && date.Month() <= 12 {
- return fmt.Sprintf("%v >= '%v' AND %v <= '%v'", field, date.Format("Y-")+"10-01 00:00:00", field, date.Format("Y-")+"12-31 23:59:59")
- }
- 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)
- } else if date.Month() >= 4 && date.Month() <= 6 {
- return fmt.Sprintf("%v >= %v AND %v < %v", field, 4, field, 7)
- } else if date.Month() >= 7 && date.Month() <= 9 {
- return fmt.Sprintf("%v >= %v AND %v < %v", field, 7, field, 10)
- } else if date.Month() >= 10 && date.Month() <= 12 {
- return fmt.Sprintf("%v >= %v AND %v <= %v", field, 10, field, 12)
- }
- return ""
- }
|