Эх сурвалжийг харах

feature(统计): 三大产品线,新增以及转化项目
三大产品线,AB类项目出货金额
三大产品线,当月和累计的签约、回款额

ZZH-wl 2 жил өмнө
parent
commit
6d396b9bf3

+ 11 - 1
opms_parent/app/service/home/home.go

@@ -112,6 +112,16 @@ func (s *HomeService) getReportData(id int64, params *map[string]interface{}) (i
 	case 20007:
 	case 20007:
 		// 报表数据   季度回款指标
 		// 报表数据   季度回款指标
 		return getQuarterGoalReportData(s.ContextService.Ctx, "20", params)
 		return getQuarterGoalReportData(s.ContextService.Ctx, "20", params)
+
+	case 20010:
+		// 报表数据   三大产品线,新增以及转化项目(C转B、B转A、A转签约、C转A、C转签约、B转签约、储备转A/B/C/签约)数量, 按周和月
+		return s.getNewAndConvertBusiness([]string{"10", "20", "30"}, params)
+	case 20011:
+		// 报表数据   三大产品线,AB类项目出货金额
+		return s.getShipmentAmount([]string{"10", "20", "30"}, []string{projSrv.StatusA, projSrv.StatusB})
+	case 20012:
+		// 报表数据   三大产品线,当月和累计的签约、回款额
+		return s.getProductLineSignedAndBackAmount([]string{"10", "20", "30"})
 	default:
 	default:
 		return nil, nil
 		return nil, nil
 	}
 	}
@@ -188,7 +198,7 @@ func (s *HomeService) getNumStatisticsData(id int64, params *map[string]interfac
 	case 10013: //当月项目转化数量(升级)
 	case 10013: //当月项目转化数量(升级)
 		count, err := busDynamicsDao.As("dynamics").LeftJoin(businessDao.Table, "bus", "bus.id=dynamics.bus_id").
 		count, err := busDynamicsDao.As("dynamics").LeftJoin(businessDao.Table, "bus", "bus.id=dynamics.bus_id").
 			WhereGTE("dynamics."+busDynamicsDao.C.CreatedTime, monthStart).WhereLTE("dynamics."+busDynamicsDao.C.CreatedTime, monthEnd).
 			WhereGTE("dynamics."+busDynamicsDao.C.CreatedTime, monthStart).WhereLTE("dynamics."+busDynamicsDao.C.CreatedTime, monthEnd).
-			Where(busDynamicsDao.C.OpnType, projSrv.OpnDowngradeApproval).
+			Where(busDynamicsDao.C.OpnType, projSrv.OpnUpgradeApproval).
 			Fields(busDynamicsDao.C.BusId).DataScope(s.Ctx, "sale_id", "bus").Distinct().Count()
 			Fields(busDynamicsDao.C.BusId).DataScope(s.Ctx, "sale_id", "bus").Distinct().Count()
 		return gconv.String(count), err
 		return gconv.String(count), err
 
 

+ 229 - 0
opms_parent/app/service/home/report.go

@@ -2,6 +2,10 @@ package home
 
 
 import (
 import (
 	"context"
 	"context"
+	contDao "dashoo.cn/micro/app/dao/contract"
+	projDao "dashoo.cn/micro/app/dao/proj"
+	projSrv "dashoo.cn/micro/app/service/proj"
+	"dashoo.cn/opms_libary/myerrors"
 	"database/sql"
 	"database/sql"
 	"fmt"
 	"fmt"
 
 
@@ -289,3 +293,228 @@ func getQuarterMonthWhere(date *gtime.Time, field string) string {
 	}
 	}
 	return ""
 	return ""
 }
 }
+
+type BusCount struct {
+	NboType     string `json:"nboType"`
+	ProductLine string `json:"productLine"`
+	Count       int    `json:"count"`
+	Remark      string `json:"remark"`
+}
+
+// 报表数据   三大产品线,新增以及转化项目(C转B、B转A、A转签约、C转A、C转签约、B转签约、储备转A/B/C/签约)数量, 按周和月
+// params 10:周 20:月
+func (s *HomeService) getNewAndConvertBusiness(productLine []string, params *map[string]interface{}) (interface{}, error) {
+	if params == nil {
+		return nil, myerrors.TipsError("请求参数传递不正确")
+	}
+	searchType, ok := (*params)["searchType"]
+	if !ok {
+		return nil, myerrors.TipsError("请求查询类型参数传递错误")
+	}
+	currentTime := gtime.Now()
+	weekStart, weekEnd := currentTime.StartOfWeek(), currentTime.EndOfWeek()
+	monthStart, monthEnd := currentTime.StartOfMonth(), currentTime.EndOfMonth()
+
+	businessDao := projDao.NewProjBusinessDao(s.Tenant)
+	busDynamicsDao := projDao.NewProjBusinessDynamicsDao(s.Tenant)
+	contractDao := contDao.NewCtrContractDao(s.Tenant)
+
+	// 获取三大产品线新增项目
+	getAddBusCount := func(searchType string) (addCount []BusCount, err error) {
+		commonDb := businessDao.DataScope(s.Ctx, "sale_id").Group(businessDao.C.ProductLine).OrderAsc(businessDao.C.ProductLine).
+			Fields("product_line, count(id) as count")
+		if searchType == "week" {
+			err = commonDb.WhereGTE(businessDao.C.FilingTime, weekStart).WhereLTE(businessDao.C.FilingTime, weekEnd).Scan(&addCount)
+		}
+		if searchType == "month" {
+			err = commonDb.WhereGTE(businessDao.C.FilingTime, monthStart).WhereLTE(businessDao.C.FilingTime, monthEnd).Scan(&addCount)
+		}
+		return addCount, err
+	}
+	addCount, err := getAddBusCount(searchType.(string))
+	if err != nil {
+		return nil, err
+	}
+
+	// 获取三大产品线签约项目
+	getSignBusCount := func(searchType string) (signCount []BusCount, err error) {
+		commonDb := contractDao.As("contract").DataScope(s.Ctx, "incharge_id").LeftJoin(businessDao.Table, "bus", "bus.id=contract.nbo_id").
+			Fields("bus.product_line, bus.nbo_type, count(contract.id) as count").
+			Group("bus.product_line, bus.nbo_type").Order("bus.product_line ASC, bus.nbo_type ASC")
+		if searchType == "week" {
+			err = commonDb.WhereGTE("contract."+contractDao.C.CreatedTime, weekStart).WhereLTE("contract."+contractDao.C.CreatedTime, weekEnd).Scan(&signCount)
+		}
+		if searchType == "month" {
+			err = commonDb.WhereGTE("contract."+contractDao.C.CreatedTime, monthStart).WhereLTE("contract."+contractDao.C.CreatedTime, monthEnd).Scan(&signCount)
+		}
+		return signCount, err
+	}
+	signCount, err := getSignBusCount(searchType.(string))
+	if err != nil {
+		return nil, err
+	}
+
+	// 获取三大产品线转化项目(C转B、B转A、A转签约、C转A、C转签约、B转签约、储备转A/B/C/签约)数量
+	getConvertBusData := func(searchType string) (convertData []BusCount, err error) {
+		commonDb := busDynamicsDao.As("dy").LeftJoin(businessDao.Table, "bus", "bus.id=dy.bus_id").DataScope(s.Ctx, "sale_id", "bus").
+			Where("dy."+busDynamicsDao.C.OpnType, projSrv.OpnUpgradeApproval).WhereNot("dy."+busDynamicsDao.C.Remark, "").
+			Fields("bus.product_line, dy.remark").OrderAsc("bus.product_line")
+		if searchType == "week" {
+			err = commonDb.WhereGTE("dy."+busDynamicsDao.C.CreatedTime, weekStart).WhereLTE("dy."+busDynamicsDao.C.CreatedTime, weekEnd).Scan(&convertData)
+		}
+		if searchType == "month" {
+			err = commonDb.WhereGTE("dy."+busDynamicsDao.C.CreatedTime, monthStart).WhereLTE("dy."+busDynamicsDao.C.CreatedTime, monthEnd).Scan(&convertData)
+		}
+		return convertData, err
+	}
+	convertBusData, err := getConvertBusData(searchType.(string))
+	if err != nil {
+		return nil, err
+	}
+
+	// 处理数据
+	result := make([][]int, len(productLine))
+	for i, pl := range productLine {
+		result[i] = make([]int, 8)
+		for _, item := range addCount {
+			if item.ProductLine == pl {
+				result[i][0] = item.Count
+			}
+		}
+		for _, item := range signCount {
+			if item.ProductLine != pl {
+				continue
+			}
+			switch item.NboType {
+			case projSrv.StatusA:
+				result[i][3] += item.Count
+			case projSrv.StatusB:
+				result[i][6] += item.Count
+			case projSrv.StatusC:
+				result[i][5] += item.Count
+			case projSrv.StatusReserve:
+				result[i][7] += item.Count
+			}
+		}
+		for _, item := range convertBusData {
+			if item.ProductLine != pl {
+				continue
+			}
+			info := gconv.Map(item.Remark)
+			if info["origNboType"] == projSrv.StatusC && info["nboType"] == projSrv.StatusB {
+				result[i][1] += 1
+			}
+			if info["origNboType"] == projSrv.StatusB && info["nboType"] == projSrv.StatusA {
+				result[i][2] += 1
+			}
+			if info["origNboType"] == projSrv.StatusC && info["nboType"] == projSrv.StatusA {
+				result[i][4] += 1
+			}
+			if info["origNboType"] == projSrv.StatusReserve {
+				result[i][7] += 1
+			}
+		}
+	}
+	return result, nil
+}
+
+// 报表数据   三大产品线,AB类项目出货金额
+func (s *HomeService) getShipmentAmount(productLine, nboType []string) (interface{}, error) {
+	businessDao := projDao.NewProjBusinessDao(s.Tenant)
+	data, err := businessDao.Fields("product_line, nbo_type, SUM(est_trans_price) as est_trans_price").
+		WhereIn("product_line", productLine).WhereIn("nbo_type", nboType).
+		Group("product_line, nbo_type").Order("product_line ASC, nbo_type ASC").DataScope(s.Ctx, "sale_id").All()
+	if err != nil {
+		return nil, err
+	}
+	result := make([][]float64, len(productLine))
+	for i, pl := range productLine {
+		result[i] = make([]float64, len(nboType))
+		for j, t := range nboType {
+			for _, item := range data {
+				if item.ProductLine == pl && item.NboType == t {
+					result[i][j] = item.EstTransPrice
+				}
+			}
+		}
+	}
+
+	return arrayReversal(result), nil
+}
+
+// 报表数据   三大产品线,当月和累计的签约、回款额
+func (s *HomeService) getProductLineSignedAndBackAmount(productLine []string) (interface{}, error) {
+	currentTime := gtime.Now()
+	monthStart := currentTime.StartOfMonth()
+	monthEnd := currentTime.EndOfMonth()
+
+	contractDao := contDao.NewCtrContractDao(s.Tenant)
+	commonDao := contractDao.DataScope(s.Ctx, "incharge_id").WhereIn(contractDao.C.ProductLine, productLine).
+		Group(contractDao.C.ProductLine).OrderAsc(contractDao.C.ProductLine)
+
+	//累计签约合同金额 contract_amount
+	allContractAmount, err := commonDao.Fields("product_line, SUM(contract_amount) as contract_amount").All()
+	if err != nil {
+		return nil, err
+	}
+	//累计回款金额 collected_amount
+	allCollectedAmount, err := commonDao.Fields("product_line, SUM(collected_amount) as collected_amount").All()
+	if err != nil {
+		return nil, err
+	}
+	//当月签约合同金额(维度:月)
+	monthContractAmount, err := commonDao.WhereGTE(contractDao.C.CreatedTime, monthStart).WhereLTE(contractDao.C.CreatedTime, monthEnd).
+		Fields("product_line, SUM(contract_amount) as contract_amount").All()
+	if err != nil {
+		return nil, err
+	}
+	//当月回款金额(维度:月)
+	monthCollectedAmount, err := commonDao.WhereGTE(contractDao.C.CreatedTime, monthStart).WhereLTE(contractDao.C.CreatedTime, monthEnd).
+		Fields("product_line, SUM(collected_amount) as collected_amount").All()
+	if err != nil {
+		return nil, err
+	}
+	result := make([][]float64, len(productLine))
+	for i, pl := range productLine {
+		result[i] = make([]float64, 4)
+		for _, item := range monthContractAmount {
+			if item.ProductLine == pl {
+				result[i][0] = item.ContractAmount
+			}
+		}
+		for _, item := range allContractAmount {
+			if item.ProductLine == pl {
+				result[i][1] = item.ContractAmount
+			}
+		}
+		for _, item := range monthCollectedAmount {
+			if item.ProductLine == pl {
+				result[i][2] = item.CollectedAmount
+			}
+		}
+		for _, item := range allCollectedAmount {
+			if item.ProductLine == pl {
+				result[i][3] = item.CollectedAmount
+			}
+		}
+	}
+
+	return arrayReversal(result), nil
+}
+
+// 二维数组反转
+func arrayReversal(result [][]float64) [][]float64 {
+	if len(result) == 0 {
+		return [][]float64{}
+	}
+	data := make([][]float64, len(result[0]))
+	for k, v := range result {
+		for m, n := range v {
+			if k == 0 {
+				data[m] = make([]float64, len(result))
+			}
+			data[m][k] = n
+		}
+	}
+	return data
+}