zwxi 1 year ago
parent
commit
c4df197766

+ 403 - 13
controller/census/seller.go

@@ -25,8 +25,8 @@ import (
 type SellerController struct{}
 
 // GroupInvoiceList
-// @Title 销售组开票统计列表
-// @Description 销售组开票统计列表
+// @Title 销售组开票统计列表-弃用
+// @Description 销售组开票统计列表-弃用
 // @Param   StartDate		query	string	false	"开始日期"
 // @Param   EndDate			query	string	false	"结束日期"
 // @Param   SortField		query	int		false	"排序字段: 1-开票金额; 2-组别占比"
@@ -429,6 +429,388 @@ func (ct *SellerController) InvoiceList(c *gin.Context) {
 	resp.OkData("获取成功", baseData, c)
 }
 
+func (this *SellerController) InvoiceListV2(c *gin.Context) {
+	var req fms.CensusSellerInvoiceListReq
+	if e := c.BindQuery(&req); e != nil {
+		err, ok := e.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+e.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", err.Translate(global.Trans), c)
+		return
+	}
+	page := new(base.Page)
+	page.SetPageSize(req.PageSize)
+	page.SetCurrent(req.Current)
+
+	if req.IsExport == 1 {
+		page.SetPageSize(10000)
+		page.SetCurrent(1)
+	}
+
+	//收入统计
+	//获取最新的开票到款日期
+	cond1 := ``
+	historyCond := ``
+	//pars1 := make([]interface{}, 0)
+	//historyPars1 := make([]interface{}, 0)
+
+	//if req.SellerIds != "" {
+	//	sellerIds := strings.Split(req.SellerIds, ",")
+	//	cond += ` AND (a.seller_id in ? ) `
+	//	historyCond += ` AND (seller_id in ? ) `
+	//	pars = append(pars, sellerIds)
+	//	historyPars = append(historyPars, sellerIds)
+	//}
+	if req.CompanyType == 1 {
+		cond1 += ` AND b.contract_type = 1 `
+		historyCond += ` AND new_company = 1 `
+	} else if req.CompanyType == 2 {
+		cond1 += ` AND b.contract_type IN (2,3,4) `
+		historyCond += ` AND new_company = 0 `
+	}
+	var departmentId int
+	if req.SellerType == 1 {
+		departmentId = crm.SellerDepartmentId
+	} else if req.SellerType == 2 {
+		departmentId = crm.RaiSellerDepartmentId
+	} else if req.SellerType == 0 {
+		resp.Fail("请选择销售类型", c)
+		return
+	} else {
+		resp.Fail("请选择正确的销售类型", c)
+		return
+	}
+
+	var totalMoneySlice []float64
+	var reqStartDate, reqEndDate time.Time
+	historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
+
+	if req.StartDate != "" && req.EndDate != "" {
+		st := fmt.Sprint(req.StartDate, "-01 00:00:00")
+		ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
+		reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
+		reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
+	}
+
+	// 累计值
+	var accumulate float64
+	//dataList := make([]*fms.IncomeSummaryItem, 0)
+	//historydataList := make([]*fms.IncomeSummaryItem, 0)
+
+	//timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
+
+	var prevStartDate string
+	var startDate, endDate string
+	//开始日期
+	if req.StartDate != "" && req.EndDate != "" {
+		startDate = reqStartDate.Format("2006-01")
+		prevStartDate = reqStartDate.Format("2006-01")
+	}
+	startDate = fmt.Sprint(startDate, "-01")
+	prevStartDate = fmt.Sprint(prevStartDate, "-01")
+	startDateTime, _ := time.Parse(utils.FormatDate, startDate)
+	endDateTime, _ := time.Parse(utils.FormatDate, endDate)
+
+	//结束日期
+	if req.StartDate != "" && req.EndDate != "" {
+		endDate = reqEndDate.Format("2006-01")
+	}
+
+	cond := `1 = 1`
+	histrtyCond := `1 = 1`
+	pars := make([]interface{}, 0)
+	historyPars := make([]interface{}, 0)
+
+	if req.CompanyType == 1 {
+		cond += ` AND b.contract_type = 1 `
+		histrtyCond += ` AND new_company = 1 `
+	} else if req.CompanyType == 2 {
+		cond += ` AND b.contract_type IN (2,3,4) `
+		histrtyCond += ` AND new_company = 0 `
+	}
+
+	//if req.SellerIds != "" {
+	//	sellerIds := strings.Split(req.SellerIds, ",")
+	//	cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
+	//	pars = append(pars, sellerIds, sellerIds)
+	//	prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
+	//	prevPars = append(prevPars, sellerIds, sellerIds)
+	//	histrtyCond += ` AND seller_id in ? `
+	//	prevHistoryCond += ` AND seller_id in ? `
+	//	historyPars = append(historyPars, sellerIds)
+	//	prevHistoryPars = append(prevHistoryPars, sellerIds)
+	//}
+
+	st := fmt.Sprint(startDate, " 00:00:00")
+	ed := fmt.Sprint(endDate, " 23:59:59")
+	groupInvoiceMap := make(map[int]float64)
+	sellerInvoiceMap := make(map[int]float64)
+	sellerGroupMap := make(map[int]int)
+
+	//校验日期,分段查询
+	if req.StartDate == "" && req.EndDate == "" {
+		summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
+		if e != nil {
+			return
+		}
+
+		// 开票到款金额合计(换算后)
+		var amountTotal float64
+
+		if len(summaryIds) > 0 {
+			amountCond := `a.id IN ? `
+			amountPars := make([]interface{}, 0)
+			amountPars = append(amountPars, summaryIds)
+			//if req.SellerIds != "" {
+			//	sellerIds := strings.Split(req.SellerIds, ",")
+			//	amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
+			//	amountCond += `OR ( d.seller_id IN ? AND  a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
+			//	amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
+			//} else {
+			//	amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
+			//	amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
+			//	amountPars = append(amountPars, st, ed, st, ed)
+			//}
+			results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
+			if e != nil {
+				resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+				return
+			}
+			//dataList = append(dataList, results...)
+			var amountSum float64
+			for _, result := range results {
+				amountSum += result.Amount
+				fmt.Println("result.Amount:", result.Amount)
+				if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
+					groupInvoiceMap[result.SellerGroupId] += result.Amount
+				} else {
+					groupInvoiceMap[result.SellerGroupId] = result.Amount
+				}
+				if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
+					sellerInvoiceMap[result.FinalSellerId] += result.Amount
+				} else {
+					sellerInvoiceMap[result.FinalSellerId] = result.Amount
+				}
+				if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
+					sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
+				}
+			}
+			amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
+			accumulate += amountTotal
+
+			//var amountTotal float64
+			results, e = fms.GetIncomeHistory(histrtyCond, historyPars)
+			if e != nil {
+				resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+				return
+			}
+			//dataList = append(dataList, results...)
+			for _, result := range results {
+				amountSum += result.Amount
+				if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
+					groupInvoiceMap[result.SellerGroupId] += result.Amount
+				} else {
+					groupInvoiceMap[result.SellerGroupId] = result.Amount
+				}
+				if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
+					sellerInvoiceMap[result.FinalSellerId] += result.Amount
+				} else {
+					sellerInvoiceMap[result.FinalSellerId] = result.Amount
+				}
+				if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
+					sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
+				}
+			}
+			amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
+			accumulate += amountTotal
+		}
+	} else if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
+		//全部走新查询
+		//fmt.Println("新查询")
+		if st != "" && ed != "" {
+			cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
+			pars = append(pars, st, ed, st, ed)
+		}
+		summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
+		if e != nil {
+			return
+		}
+
+		// 开票到款金额合计(换算后)
+		var amountTotal float64
+
+		if len(summaryIds) > 0 {
+			amountCond := `a.id IN ? `
+			amountPars := make([]interface{}, 0)
+			amountPars = append(amountPars, summaryIds)
+			//if req.SellerIds != "" {
+			//	sellerIds := strings.Split(req.SellerIds, ",")
+			//	amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
+			//	amountCond += `OR ( d.seller_id IN ? AND  a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
+			//	amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
+			//} else {
+			//	amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
+			//	amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
+			//	amountPars = append(amountPars, st, ed, st, ed)
+			//}
+			results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
+			if e != nil {
+				resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+				return
+			}
+			//dataList = append(dataList, results...)
+			var amountSum float64
+			for _, result := range results {
+				amountSum += result.Amount
+				fmt.Println("result.Amount:", result.Amount)
+				if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
+					groupInvoiceMap[result.SellerGroupId] += result.Amount
+				} else {
+					groupInvoiceMap[result.SellerGroupId] = result.Amount
+				}
+				if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
+					sellerInvoiceMap[result.FinalSellerId] += result.Amount
+				} else {
+					sellerInvoiceMap[result.FinalSellerId] = result.Amount
+				}
+				if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
+					sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
+				}
+			}
+			amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
+			accumulate += amountTotal
+		}
+	} else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
+		//全部走旧查询
+		//fmt.Println("旧查询")
+		//fmt.Println("st:",st)
+		//fmt.Println("ed:",ed)
+		if st != "" && ed != "" {
+			histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
+			historyPars = append(historyPars, st, ed)
+		}
+		//fmt.Println("st:",st)
+		//fmt.Println("ed:",ed)
+		// 开票到款金额合计(换算后)
+		var amountTotal float64
+		results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
+		if e != nil {
+			resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+			return
+		}
+		var amountSum float64
+		//dataList = append(dataList, results...)
+		for _, result := range results {
+			amountSum += result.Amount
+			if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
+				groupInvoiceMap[result.SellerGroupId] += result.Amount
+			} else {
+				groupInvoiceMap[result.SellerGroupId] = result.Amount
+			}
+			if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
+				sellerInvoiceMap[result.FinalSellerId] += result.Amount
+			} else {
+				sellerInvoiceMap[result.FinalSellerId] = result.Amount
+			}
+			if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
+				sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
+			}
+		}
+		amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
+		accumulate += amountTotal
+		//fmt.Println("partAccumulate:",partAccumulate)
+	}
+
+	fmt.Println("totalMoneySlice:", len(totalMoneySlice))
+
+	if req.SellerType == 1 {
+		departmentId = crm.SellerDepartmentId
+	} else if req.SellerType == 2 {
+		departmentId = crm.RaiSellerDepartmentId
+	} else if req.SellerType == 0 {
+		resp.FailMsg("查询错误", "请选择正确的销售类型", c)
+		return
+	} else {
+		resp.FailMsg("查询错误", "请选择正确的销售类型", c)
+		return
+	}
+
+	adminOB := new(crm.Admin)
+	adminPars := make([]interface{}, 0)
+	adminCond := ` department_id = %d `
+	adminCond = fmt.Sprintf(adminCond, departmentId)
+	adminList, e := adminOB.List(adminCond, adminPars)
+	if e != nil {
+		resp.FailMsg("获取失败", "获取销售列表失败, Err: "+e.Error(), c)
+		return
+	}
+
+	// 获取销售分组信息
+	sellerList, e := crmService.GetSellerDepartmentListWithEnable()
+	if e != nil {
+		resp.FailData("获取销售失败", "Err:"+e.Error(), c)
+		return
+	}
+	sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
+	for i := range sellerList {
+		sellerMap[sellerList[i].SellerId] = sellerList[i]
+	}
+
+	list := make([]*fms.CensusSellerInvoiceItem, 0)
+
+	for _, v := range adminList {
+		if vv, ok := sellerInvoiceMap[v.AdminId]; ok {
+			rate := vv / accumulate
+			mulNum := decimal.NewFromFloat(100)
+			newRate := decimal.NewFromFloat(rate)
+			newRate = newRate.Mul(mulNum).Round(2)
+			finalRate, _ := newRate.Float64()
+
+			groupId := sellerGroupMap[v.AdminId]
+			groupRate := vv / groupInvoiceMap[groupId]
+			newGroupRate := decimal.NewFromFloat(groupRate)
+			newGroupRate = newGroupRate.Mul(mulNum).Round(2)
+			finalGroupRate, _ := newGroupRate.Float64()
+
+			amuont, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", vv), 64)
+
+			var sellerName, groupName string
+			if seller, ok := sellerMap[v.AdminId]; ok {
+				sellerName = seller.SellerName
+				groupName = seller.GroupName
+			}
+
+			list = append(list, &fms.CensusSellerInvoiceItem{
+				SellerId:      v.AdminId,
+				SellerName:    sellerName,
+				GroupId:       groupId,
+				GroupName:     groupName,
+				InvoiceAmount: amuont,
+				GroupRate:     finalGroupRate,
+				SellerRate:    finalRate,
+			})
+		}
+	}
+
+	var respList fms.CensusSellerList
+	respList = list
+
+	sort.Sort(respList)
+
+	// 是否导出
+	//if req.IsExport == 1 {
+	//	ExportIncomeList(c, incomeList.DataList)
+	//	return
+	//}
+	page.SetTotal(int64(len(list)))
+	baseData := new(base.BaseData)
+	baseData.SetPage(page)
+	baseData.SetList(respList)
+	resp.OkData("获取成功", baseData, c)
+}
+
 // ExportInvoiceList 导出销售开票统计列表
 func ExportInvoiceList(c *gin.Context, list []*fms.CensusSellerInvoiceItem, req fms.CensusSellerInvoiceListReq) {
 	// 生成Excel文件
@@ -494,6 +876,16 @@ func ExportInvoiceList(c *gin.Context, list []*fms.CensusSellerInvoiceItem, req
 	http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
 }
 
+// GroupInvoiceList
+// @Title 销售组开票统计列表
+// @Description 销售组开票统计列表
+// @Param   StartDate		query	string	false	"开始日期"
+// @Param   EndDate			query	string	false	"结束日期"
+// @Param   SortField		query	int		false	"排序字段: 1-开票金额; 2-组别占比"
+// @Param   SortType		query	int		false	"排序方式: 1-正序; 2-倒序"
+// @Param   IsExport		query	int		false	"是否导出: 0-否; 1-是"
+// @Success 200 {object} fms.CensusSellerGroupInvoiceItem
+// @router /census/seller/group_invoice_list [get]
 func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 	var req fms.CensusSellerGroupInvoiceListReq
 	if e := c.BindQuery(&req); e != nil {
@@ -616,7 +1008,7 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 			//}
 			results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
 			if e != nil {
-				resp.FailMsg("查询错误",fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+				resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
 				return
 			}
 			//dataList = append(dataList, results...)
@@ -633,11 +1025,10 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 			amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
 			accumulate += amountTotal
 
-
 			//var amountTotal float64
 			results, e = fms.GetIncomeHistory(histrtyCond, historyPars)
 			if e != nil {
-				resp.FailMsg("查询错误",fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+				resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
 				return
 			}
 			//dataList = append(dataList, results...)
@@ -683,7 +1074,7 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 			//}
 			results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
 			if e != nil {
-				resp.FailMsg("查询错误",fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+				resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
 				return
 			}
 			//dataList = append(dataList, results...)
@@ -715,7 +1106,7 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 		var amountTotal float64
 		results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
 		if e != nil {
-			resp.FailMsg("查询错误",fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
+			resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
 			return
 		}
 		var amountSum float64
@@ -731,7 +1122,6 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 		amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
 		accumulate += amountTotal
 
-
 		//fmt.Println("partAccumulate:",partAccumulate)
 	}
 
@@ -743,10 +1133,10 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 	} else if req.SellerType == 2 {
 		departmentId = crm.RaiSellerDepartmentId
 	} else if req.SellerType == 0 {
-		resp.FailMsg("查询错误","请选择正确的销售类型", c)
+		resp.FailMsg("查询错误", "请选择正确的销售类型", c)
 		return
 	} else {
-		resp.FailMsg("查询错误","请选择正确的销售类型", c)
+		resp.FailMsg("查询错误", "请选择正确的销售类型", c)
 		return
 	}
 	groupCond := ` department_id = %d AND parent_id = 0 `
@@ -755,7 +1145,7 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 	groupOB := new(crm.SysGroup)
 	groupList, e := groupOB.List(groupCond, groupPars)
 	if e != nil {
-		resp.FailMsg("查询错误",fmt.Sprintf("获取组别列表失败, Err:  %s", e.Error()), c)
+		resp.FailMsg("查询错误", fmt.Sprintf("获取组别列表失败, Err:  %s", e.Error()), c)
 		return
 	}
 	//total := len(groupList)
@@ -775,10 +1165,10 @@ func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
 		newRate = newRate.Mul(mulNum).Round(2)
 		finalRate, _ := newRate.Float64()
 
-		amuont,_ := strconv.ParseFloat(fmt.Sprintf("%.2f", v), 64)
+		amuont, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v), 64)
 
 		groupName := ""
-		if groupItem ,ok := groupMap[k]; ok {
+		if groupItem, ok := groupMap[k]; ok {
 			groupName = groupItem.GroupName
 		}
 		list = append(list, &fms.CensusSellerGroupInvoiceItem{

+ 25 - 14
controller/crm/company_seller.go

@@ -106,29 +106,40 @@ func (rg *CompanySellerController) TeamList(c *gin.Context) {
 
 // 修复历史数据表中导入的销售seller_id
 func FIXIncomeHistorySellerId() (err error) {
-	adminCond := ``
-	adminPars := make([]interface{}, 0)
-	adminOB := new(crm.Admin)
-	adminList, e := adminOB.List(adminCond, adminPars)
-	if e != nil {
-		return
-	}
-	sellerMap := make(map[int]*crm.Admin)
-	for i := range adminList {
-		sellerMap[adminList[i].AdminId] = adminList[i]
-	}
+	//adminCond := ``
+	//adminPars := make([]interface{}, 0)
+	//adminOB := new(crm.Admin)
+	//adminList, e := adminOB.List(adminCond, adminPars)
+	//if e != nil {
+	//	return
+	//}
+	//sellerMap := make(map[int]*crm.Admin)
+	//for i := range adminList {
+	//	sellerMap[adminList[i].AdminId] = adminList[i]
+	//}
 
 	list,err := fms.GetIncomeHistoryList()
 	if err != nil {
 		return
 	}
+	sellerList, e := crmService.GetSellerDepartmentListWithEnable()
+	if e != nil {
+		resp.FailData("获取销售失败", "Err:"+e.Error(), nil)
+		return
+	}
+	sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
+	for i := range sellerList {
+		sellerMap[sellerList[i].SellerId] = sellerList[i]
+	}
 	for _, v := range list {
 		fmt.Println("v:", v.IncomeId)
 		if seller, ok := sellerMap[v.SellerId]; ok{
-			v.SellerId = seller.AdminId
+			fmt.Println("更新销售:",v.SellerName)
+			v.SellerId = seller.SellerId
 			v.GroupId  = seller.GroupId
-			v.GroupName  = seller.GroupName
-			if e = v.Update([]string{"GroupId,GroupName"}); e != nil {
+			v.GroupName = seller.GroupName
+			v.DepartmentId = seller.DepartmentId
+			if e = v.Update([]string{"GroupId","GroupName","DepartmentId"}); e != nil {
 				fmt.Println("更新销售失败, Err: " + e.Error())
 				return
 			}

+ 1 - 0
init_serve/task.go

@@ -7,6 +7,7 @@ import (
 func InitTask() {
 	// TODO:执行一次
 	fmt.Println("task start")
+	//crm.FIXIncomeHistorySellerId()
 	//fmsService.FixContractInvoiceSellerInfo()
 	//fmsService.FixContractInvoicePaymentType()
 	//fmsService.FixContractSummaryData()

+ 11 - 10
models/fms/contract_income_history.go

@@ -6,15 +6,16 @@ import (
 )
 
 type ContractIncomeHistory struct {
-	IncomeId    int       `json:"income_id" gorm:"primaryKey;column:income_id"` //
-	Amount      float64   `json:"amount" gorm:"column:amount"`                  // 开票金额
-	InvoiceTime time.Time `json:"invoice_time" gorm:"column:invoice_time"`      // 开票日期/到款月
-	SellerId    int       `json:"seller_id" gorm:"column:seller_id"`            // 销售ID
-	GroupId     int       `json:"group_id" gorm:"column:group_id"`              // 销售ID
-	SellerName  string    `json:"seller_name" gorm:"column:seller_name"`        // 销售名称
-	GroupName   string    `json:"group_name" gorm:"column:group_name"`          // 销售名称
-	NewCompany  int       `json:"new_company" gorm:"column:new_company"`        // 是否为新客户:0-否;1-是
-	CompanyName string    `json:"company_name" gorm:"column:company_name"`      // 客户名称
+	IncomeId     int       `json:"income_id" gorm:"primaryKey;column:income_id"` //
+	Amount       float64   `json:"amount" gorm:"column:amount"`                  // 开票金额
+	InvoiceTime  time.Time `json:"invoice_time" gorm:"column:invoice_time"`      // 开票日期/到款月
+	SellerId     int       `json:"seller_id" gorm:"column:seller_id"`            // 销售ID
+	GroupId      int       `json:"group_id" gorm:"column:group_id"`              // 销售ID
+	SellerName   string    `json:"seller_name" gorm:"column:seller_name"`        // 销售名称
+	GroupName    string    `json:"group_name" gorm:"column:group_name"`          // 销售名称
+	NewCompany   int       `json:"new_company" gorm:"column:new_company"`        // 是否为新客户:0-否;1-是
+	CompanyName  string    `json:"company_name" gorm:"column:company_name"`      // 客户名称
+	DepartmentId int       `json:"department_id" gorm:"column:department_id"`    // 部门ID
 }
 
 func (c *ContractIncomeHistory) List(condition string, pars []interface{}) (list []*ContractIncomeHistory, err error) {
@@ -40,7 +41,7 @@ func GetIncomeHistory(condition string, pars []interface{}) (results []*IncomeSu
 
 // GetIncomeHistoryList
 func GetIncomeHistoryList() (results []*ContractIncomeHistory, err error) {
-	sql := `SELECT * FROM contract_income_history WHERE seller_id=0 `
+	sql := `SELECT * FROM contract_income_history `
 	err = global.DEFAULT_MYSQL.Raw(sql).Find(&results).Error
 	return
 }

+ 15 - 0
models/fms/contract_invoice.go

@@ -832,4 +832,19 @@ func (c CensusSellerGroupList) Less(i, j int) bool {
 
 func (c CensusSellerGroupList) Swap(i, j int) {
 	c[i], c[j] = c[j], c[i]
+}
+
+
+type CensusSellerList []*CensusSellerInvoiceItem
+
+func (c CensusSellerList) Len() int {
+	return len(c)
+}
+
+func (c CensusSellerList) Less(i, j int) bool {
+	return c[i].InvoiceAmount > c[j].InvoiceAmount
+}
+
+func (c CensusSellerList) Swap(i, j int) {
+	c[i], c[j] = c[j], c[i]
 }

+ 11 - 11
models/fms/invoice_payment_summary.go

@@ -192,21 +192,22 @@ func GetInvoicePaymentSummaryByRegisterIdAndInvoiceId(registerId, invocieId int)
 }
 
 type IncomeSummaryItem struct {
-	CompanyName  string    `json:"company_name" description:"客户名称"`
-	ContractType int       `json:"contract_type" description:"是否为新客户: 2-否; 1-是"`
-	InvoiceDate  time.Time `json:"invoice_time" description:"开票日期"`
-	Amount       float64   `json:"amount" description:"金额"`
-	SellerName   string    `json:"seller_name" description:"销售名称"`
-	FinalSellerId int       `json:"final_seller_id" description:"最终销售ID"`
-	SellerGroupId int       `json:"seller_group_id" description:"销售分组ID"`
+	CompanyName     string    `json:"company_name" description:"客户名称"`
+	ContractType    int       `json:"contract_type" description:"是否为新客户: 2-否; 1-是"`
+	InvoiceDate     time.Time `json:"invoice_time" description:"开票日期"`
+	Amount          float64   `json:"amount" description:"金额"`
+	SellerName      string    `json:"seller_name" description:"销售名称"`
+	FinalSellerId   int       `json:"final_seller_id" description:"最终销售ID"`
+	SellerGroupId   int       `json:"seller_group_id" description:"销售分组ID"`
+	SellerGroupName int       `json:"seller_group_name" description:"销售分组名称"`
 }
 
 // GetContractSummaryIncomeAmount 获取汇总金额合计信息
 func GetContractSummaryIncomeAmount(condition string, pars []interface{}) (results []*IncomeSummaryItem, err error) {
 	query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
-		Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount,IF(a.invoice_id=0,d.invoice_time, b.invoice_time) AS invoice_date," +
-			" c.contract_type,c.company_name,IF(a.invoice_id=0,d.seller_name, b.seller_name) AS seller_name," +
-			"IF(a.invoice_id=0,d.seller_id, b.seller_id) AS final_seller_id," +
+		Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount,IF(a.invoice_id=0,d.invoice_time, b.invoice_time) AS invoice_date,"+
+			" c.contract_type,c.company_name,IF(a.invoice_id=0,d.seller_name, b.seller_name) AS seller_name,"+
+			"IF(a.invoice_id=0,d.seller_id, b.seller_id) AS final_seller_id,"+
 			"IF(a.invoice_id=0,d.seller_group_id, b.seller_group_id) AS seller_group_id").
 		Joins("LEFT JOIN contract_invoice AS b ON a.invoice_id = b.contract_invoice_id AND b.is_deleted = 0 ").
 		Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
@@ -378,4 +379,3 @@ func UpdateInvoicePaymentSummaryByRegisterId(newId int, registerIds []int) (err
 	err = global.DEFAULT_MYSQL.Exec(sql, newId, registerIds).Error
 	return
 }
-

+ 1 - 1
routers/census.go

@@ -20,5 +20,5 @@ func InitCensus(rg *gin.RouterGroup) {
 	sel := new(census.SellerController)
 	selGroup := rg.Group("seller/").Use(middleware.Token())
 	selGroup.GET("group_invoice_list", sel.GroupInvoiceListV2)
-	selGroup.GET("invoice_list", sel.InvoiceList)
+	selGroup.GET("invoice_list", sel.InvoiceListV2)
 }