xingzai 4 bulan lalu
induk
melakukan
6342c3d8aa

+ 314 - 0
controllers/statistic_company_merge.go

@@ -7,6 +7,7 @@ import (
 	"hongze/hz_crm_api/models"
 	"hongze/hz_crm_api/models/company"
 	"hongze/hz_crm_api/models/response"
+	"hongze/hz_crm_api/models/system"
 	"hongze/hz_crm_api/services"
 	cygxService "hongze/hz_crm_api/services/cygx"
 	"os"
@@ -1808,6 +1809,319 @@ func (this *StatisticCompanyMergerController) CompanyContractPercentageListV2()
 	br.Data = resp
 }
 
+// @Title 权益客户未续约率下载
+// @Description 权益客户未续约率下载接口
+// @Param   AdminId   query   string  true       "销售id,多个用英文逗号隔开,空字符串为全部"
+// @Param   StartDate   query   string  false       "开始日期"
+// @Param   EndDate   query   string  false       "结束日期"
+// @Param   ExportType   query   int	  true	       "下载类型 1:下载当前销售的合同明细数据;2:下载所有销售未续约数据列表"
+// @Success 200 {object} response.IncrementalCompanyListResp
+// @router /merge_company/company_contract_percentage/list_export [get]
+func (this *StatisticCompanyMergerController) CompanyContractPercentageListExport() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	sysUser := this.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+	exportType, _ := this.GetInt("ExportType")
+	adminId := this.GetString("AdminId")
+	contractDataType := this.GetString("ContractDataType")
+	startDate := this.GetString("StartDate")
+	endDate := this.GetString("EndDate")
+	//initendDate := this.GetString("EndDate")
+	if startDate == "" || endDate == "" {
+		br.Msg = "开始时间或结束时间不能为空"
+		return
+	}
+
+	//判断结束时间是否晚于当前时间,如果晚于当前时间,那么就把当前时间作为截止时间。
+	endDateTime, _ := time.Parse(utils.FormatDate, endDate)
+	if endDateTime.After(time.Now()) {
+		endDate = time.Now().Format(utils.FormatDate)
+	}
+	//fmt.Println("endDate", endDate)
+	if contractDataType == "" {
+		contractDataType = "续约合同"
+	}
+
+	var startSize int
+	if pageSize <= 0 {
+		pageSize = 10000
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+	startSize = utils.StartIndex(currentIndex, pageSize)
+
+	var condition string
+	var pars []interface{}
+
+	if exportType == 1 {
+		//条件
+		if adminId != "" {
+			condition += ` AND a.seller_id_init in  (` + adminId + `) `
+		}
+	}
+
+	//权益有效合同
+	condition += ` AND c.product_id = 2   AND  a.status = 1 `
+
+	//var list []*models.IncrementalList
+
+	condition1 := condition
+	pars1 := pars
+	condition1 += ` AND a.end_date >= ? AND a.end_date <= ? `
+	pars1 = append(pars1, startDate, endDate)
+	condition1 += ` AND  a.company_ascribe_id  > 0 AND  a.company_ascribe_id  !=9   ` // 已确认未续约
+
+	//if contractDataType == "续约合同" || contractDataType == "确认不续约合同" {
+	//不续约列表数据
+	listRenewal, err := models.GetIncrementalCompanyMergeListEnd(condition1, pars1, startSize, pageSize)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,Err:" + err.Error()
+		return
+	}
+
+	//}
+
+	conditionEnd1 := condition
+	parsEnd1 := pars
+
+	conditionEnd1 += ` AND a.end_date >= ? AND a.end_date <= ?  AND  a.company_ascribe_id  !=9   `
+	parsEnd1 = append(parsEnd1, startDate, endDate)
+
+	//if contractDataType == "到期合同" {
+	//到期列表数据
+	listEndDate, err := models.GetIncrementalCompanyMergeListEnd(conditionEnd1, parsEnd1, startSize, pageSize)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,Err:" + err.Error()
+		return
+	}
+
+	var companyContractIds []int
+	for _, v := range listRenewal {
+		companyContractIds = append(companyContractIds, v.CompanyContractId)
+		v.SellerId = v.SellerIdInit
+	}
+	for _, v := range listEndDate {
+		companyContractIds = append(companyContractIds, v.CompanyContractId)
+		v.SellerId = v.SellerIdInit
+	}
+
+	//合同归因标签
+	mapGetCompanyAscribeContent, mapContent := services.GetCompanyContractAscribeContentMap(companyContractIds)
+
+	//合并合同所对应的权限
+	mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,Err:" + err.Error()
+		return
+	}
+	for _, v := range listRenewal {
+		v.PermissionName = mappermissionName[v.CompanyContractId]
+		if mapGetCompanyAscribeContent[v.CompanyContractId] != "" {
+			v.Content = mapContent[v.CompanyContractId]
+			v.AscribeContent = mapGetCompanyAscribeContent[v.CompanyContractId]
+			v.IsShowNoRenewedNote = true
+		}
+	}
+	for _, v := range listEndDate {
+		v.PermissionName = mappermissionName[v.CompanyContractId]
+		if mapGetCompanyAscribeContent[v.CompanyContractId] != "" {
+			v.Content = mapContent[v.CompanyContractId]
+			v.AscribeContent = mapGetCompanyAscribeContent[v.CompanyContractId]
+			v.IsShowNoRenewedNote = true
+		}
+	}
+	//创建excel
+	dir, err := os.Executable()
+	exPath := filepath.Dir(dir)
+	downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	xlsxFile := xlsx.NewFile()
+	if err != nil {
+		br.Msg = "生成文件失败"
+		br.ErrMsg = "生成文件失败"
+		return
+	}
+	style := xlsx.NewStyle()
+	alignment := xlsx.Alignment{
+		Horizontal: "center",
+		Vertical:   "center",
+		WrapText:   true,
+	}
+	style.Alignment = alignment
+	style.ApplyAlignment = true
+	if exportType == 1 {
+		for pidIndex := 0; pidIndex <= 1; pidIndex++ {
+			var sheetName string
+			var listDate []*models.IncrementalList
+			if pidIndex == 0 {
+				sheetName = "确认不续约合同"
+				listDate = listRenewal
+			} else {
+				sheetName = "到期合同"
+				listDate = listEndDate
+			}
+			sheet, err := xlsxFile.AddSheet(sheetName)
+			if err != nil {
+				br.Msg = "新增Sheet失败"
+				br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
+				return
+			}
+			//标头
+			rowTitle := sheet.AddRow()
+			cellA := rowTitle.AddCell()
+			cellA.Value = "客户名称"
+			cellB := rowTitle.AddCell()
+			cellB.Value = "客户状态"
+			cellC := rowTitle.AddCell()
+			cellC.Value = "所属销售"
+			cellD := rowTitle.AddCell()
+			cellD.Value = "合同期限"
+			cellE := rowTitle.AddCell()
+			cellE.Value = "签约套餐"
+			cellF := rowTitle.AddCell()
+			cellF.Value = "合同金额"
+
+			if pidIndex == 0 {
+				cellG := rowTitle.AddCell()
+				cellG.Value = "不续约归因"
+			}
+
+			for _, item := range listDate {
+				row := sheet.AddRow()
+				cellAData := row.AddCell()
+				cellAData.Value = item.CompanyName
+				cellBData := row.AddCell()
+				cellBData.Value = item.ProductStatus
+				cellCData := row.AddCell()
+				cellCData.Value = item.SellerName
+				cellDData := row.AddCell()
+				cellDData.Value = item.StartDate + "~" + item.EndDate
+				cellEData := row.AddCell()
+				cellEData.Value = item.PermissionName
+				cellFData := row.AddCell()
+				cellFData.Value = fmt.Sprint(item.Money)
+				if pidIndex == 0 {
+					cellGData := row.AddCell()
+					cellGData.Value = item.Content
+				}
+			}
+		}
+		err = xlsxFile.Save(downLoadnFilePath)
+		if err != nil {
+			br.Msg = "保存文件失败"
+			br.ErrMsg = "保存文件失败"
+			return
+		}
+	} else {
+
+		var sheetName string
+		//var listDate []*models.IncrementalList
+		sheetName = "未续约率"
+		sheet, err := xlsxFile.AddSheet(sheetName)
+		if err != nil {
+			br.Msg = "新增Sheet失败"
+			br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
+			return
+		}
+		//标头
+		rowTitle := sheet.AddRow()
+		cellA := rowTitle.AddCell()
+		cellA.Value = "姓名"
+		cellB := rowTitle.AddCell()
+		cellB.Value = "确认不续约合同数"
+		cellC := rowTitle.AddCell()
+		cellC.Value = "确认不续约合同金额"
+		cellD := rowTitle.AddCell()
+		cellD.Value = "到期合同数"
+		cellE := rowTitle.AddCell()
+		cellE.Value = "到期合同金额"
+		cellF := rowTitle.AddCell()
+		cellF.Value = "确认不续约率"
+
+		listRaiSeller, err := system.GetSysuserRaiListNoServer()
+		if err != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取权益销售信息失败,Err:" + err.Error()
+			return
+		}
+
+		//NoRenewalContractTotal      int    `description:"确认不续约合同数"`
+		//NoRenewalContractMoney      int    `description:"确认不续约合同金额"`
+		//ExpireRenewalContractTotal  int    `description:"到期合同数量"`
+		//ExpireRenewalContractMoney  int    `description:"到期合同总金额"`
+		//NoRenewalContractPercentage string `description:"确认不续约率"`
+		mapNoRenewalContractTotal := make(map[int]int)
+		mapNoRenewalContractMoney := make(map[int]float64)
+		mapExpireRenewalContractTotal := make(map[int]int)
+		mapExpireRenewalContractMoney := make(map[int]float64)
+
+		//mapData := make(map[int]*response.IncrementalCompanyPercentageExportResp)
+		for _, v := range listRenewal {
+			mapNoRenewalContractTotal[v.SellerId]++
+			mapNoRenewalContractMoney[v.SellerId] += v.Money
+		}
+
+		for _, v := range listEndDate {
+			mapExpireRenewalContractTotal[v.SellerId]++
+			mapExpireRenewalContractMoney[v.SellerId] += v.Money
+		}
+		for _, item := range listRaiSeller {
+			row := sheet.AddRow()
+			cellAData := row.AddCell()
+			cellAData.Value = item.RealName
+			cellBData := row.AddCell()
+			cellBData.Value = fmt.Sprint(mapNoRenewalContractTotal[item.AdminId])
+			cellCData := row.AddCell()
+			cellCData.Value = fmt.Sprint(mapNoRenewalContractMoney[item.AdminId])
+
+			cellEData := row.AddCell()
+			cellEData.Value = fmt.Sprint(mapExpireRenewalContractTotal[item.AdminId])
+			cellFData := row.AddCell()
+			cellFData.Value = fmt.Sprint(mapExpireRenewalContractMoney[item.AdminId])
+
+			cellGData := row.AddCell()
+
+			//分子或者分母为零的时候,不做计算
+			if mapNoRenewalContractMoney[item.AdminId] == 0 || mapExpireRenewalContractMoney[item.AdminId] == 0 {
+				cellGData.Value = "0%"
+			} else {
+				cellGData.Value = fmt.Sprint(utils.SubFloatToString(float64(mapNoRenewalContractMoney[item.AdminId])/float64(mapExpireRenewalContractMoney[item.AdminId])*100, 2), "%")
+			}
+		}
+		err = xlsxFile.Save(downLoadnFilePath)
+		if err != nil {
+			br.Msg = "保存文件失败"
+			br.ErrMsg = "保存文件失败"
+			return
+		}
+	}
+	randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
+	downloadFileName := "未续约率明细" + randStr + ".xlsx"
+	this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
+	defer func() {
+		os.Remove(downLoadnFilePath)
+	}()
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "导出成功"
+}
+
 func init213() {
 	var condition string
 	var pars []interface{}

+ 14 - 14
controllers/statistic_report.go

@@ -6885,20 +6885,20 @@ func getTodayStackCompanyListV2Rai(sysUser *system.Admin, currentIndex, pageSize
 		item := list[i]
 
 		//剩余可用天数
-		//expireDay := "0"
-		//endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
-		//var sub time.Duration
-		//if dataType != "未续约客户" {
-		//	endDateTime = endDateTime.AddDate(0, 0, 1)
-		//	sub = endDateTime.Sub(time.Now())
-		//	//if sub < 0 {
-		//	//	sub = 0
-		//	//}
-		//} else {
-		//	sub = time.Now().Sub(endDateTime)
-		//}
-		//expireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
-		//list[i].ExpireDay = expireDay
+		expireDay := "0"
+		endDateTime, _ := time.Parse(utils.FormatDate, item.EndDate)
+		var sub time.Duration
+		if dataType != "未续约客户" {
+			endDateTime = endDateTime.AddDate(0, 0, 1)
+			sub = endDateTime.Sub(time.Now())
+			//if sub < 0 {
+			//	sub = 0
+			//}
+		} else {
+			sub = time.Now().Sub(endDateTime)
+		}
+		expireDay = fmt.Sprintf("%v", int(sub.Hours()/24))
+		list[i].ExpireDay = expireDay
 		companyContractIds = append(companyContractIds, item.CompanyContractId)
 	}
 

+ 9 - 0
models/response/statistic_report.go

@@ -143,3 +143,12 @@ type IncrementalCompanyPercentageListResp struct {
 	ExpireRenewalContractTotal int    `description:"到期合同数量"`
 	ExpireRenewalContractMoney int    `description:"到期合同总金额"`
 }
+
+// 权益客户统计,续约率,返回类
+type IncrementalCompanyPercentageExportResp struct {
+	NoRenewalContractTotal      int    `description:"确认不续约合同数"`
+	NoRenewalContractMoney      int    `description:"确认不续约合同金额"`
+	ExpireRenewalContractTotal  int    `description:"到期合同数量"`
+	ExpireRenewalContractMoney  int    `description:"到期合同总金额"`
+	NoRenewalContractPercentage string `description:"确认不续约率"`
+}

+ 3 - 1
models/statistic_report.go

@@ -377,6 +377,7 @@ type IncrementalList struct {
 	CompanyName          string                             `description:"企业客户名称"`
 	ProductId            int                                `description:"产品id"`
 	ProductName          string                             `description:"产品名称"`
+	ProductStatus        string                             `description:"产品名称"`
 	CompanyProductId     int                                `description:"客户购买产品授权id"`
 	ContractCode         string                             `description:"合同编码"`
 	StartDate            string                             `description:"合同开始日期"`
@@ -408,6 +409,7 @@ type IncrementalList struct {
 	//CompanyContractIdGroup string                             `description:"表company_contract合并的 company_contract_id"`
 	IsUserMaker     int    `description:"近四周之内是否包含决策人互动过 ,0否,1是"`
 	SellerNameInit  string `description:"权益初始化销售"`
+	SellerIdInit    int    `description:"权益初始化销售ID"`
 	ShareSellerInit string `description:"共享销售员"`
 }
 
@@ -882,7 +884,7 @@ func GetIncrementalCompanyMergeList(condition string, pars []interface{}, startS
 func GetIncrementalCompanyMergeListEnd(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
 	o := orm.NewOrm()
 
-	sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
+	sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason,c.status AS product_status FROM company_contract a
 		 JOIN company b ON a.company_id = b.company_id
 		 JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
 

+ 8 - 0
models/system/sys_admin.go

@@ -478,6 +478,14 @@ func GetSysuserRaiList() (items []*AdminItem, err error) {
 	return
 }
 
+// GetSysuserRaiList 获取除服务组之外的所有权益销售
+func GetSysuserRaiListNoServer() (items []*AdminItem, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT  * FROM admin WHERE role_type_code  IN ('rai_group','rai_seller')  AND enabled = 1  AND role_name NOT IN ('权益服务销售','权益服务组长')  `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
 func GetSellerIdsByDepId(depId int) (ids []string, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT

+ 9 - 0
routers/commentsRouter.go

@@ -10915,6 +10915,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:StatisticCompanyMergerController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:StatisticCompanyMergerController"],
+        beego.ControllerComments{
+            Method: "CompanyContractPercentageListExport",
+            Router: `/merge_company/company_contract_percentage/list_export`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:StatisticCompanyMergerController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:StatisticCompanyMergerController"],
         beego.ControllerComments{
             Method: "CompanyContractPermissionList",