ziwen 1 年之前
父節點
當前提交
aaecaaa2d9
共有 2 個文件被更改,包括 125 次插入12 次删除
  1. 74 12
      controller/census/seller.go
  2. 51 0
      models/fms/contract_invoice.go

+ 74 - 12
controller/census/seller.go

@@ -15,6 +15,8 @@ import (
 	crmService "hongze/fms_api/services/crm"
 	"hongze/fms_api/utils"
 	"net/http"
+	"strconv"
+	"strings"
 	"time"
 )
 
@@ -91,19 +93,45 @@ func (ct *SellerController) GroupInvoiceList(c *gin.Context) {
 
 	// 查询列表
 	groupOB := new(crm.SysGroup)
-	totalCond := outCond
-	totalPars := make([]interface{}, 0)
-	total, e := groupOB.Count(totalCond, totalPars)
+	//totalCond := outCond
+	//totalPars := make([]interface{}, 0)
+	//total, e := groupOB.Count(totalCond, totalPars)
+	//if e != nil {
+	//	resp.FailMsg("获取失败", "获取销售组开票统计列表总数失败, Err: "+e.Error(), c)
+	//	return
+	//}
+	//list, e := fms.GetCensusSellerGroupInvoicePageList(page, cond, outCond, pars, invSum)
+	//if e != nil {
+	//	resp.FailMsg("获取失败", "获取销售组开票统计列表失败, Err: "+e.Error(), c)
+	//	return
+	//}
+	groupCond := ` department_id = %d AND parent_id = 0 `
+	groupCond = fmt.Sprintf(groupCond, departmentId)
+	groupPars := make([]interface{}, 0)
+	groupList, e := groupOB.List(groupCond, groupPars)
 	if e != nil {
-		resp.FailMsg("获取失败", "获取销售组开票统计列表总数失败, Err: "+e.Error(), c)
+		resp.FailMsg("获取失败", "获取组别列表失败, Err: "+e.Error(), c)
 		return
 	}
-	list, e := fms.GetCensusSellerGroupInvoicePageList(page, cond, outCond, pars, invSum)
+	total := len(groupList)
+	groupMap := make(map[int]*crm.SysGroup)
+	groupIdSlice := make([]string,0)
+	for i := range groupList {
+		groupMap[groupList[i].GroupId] = groupList[i]
+		groupIdSlice = append(groupIdSlice, strconv.Itoa(groupList[i].GroupId))
+	}
+
+	groupStr := strings.Join(groupIdSlice, ",")
+	list, e := fms.GetCensusSellerGroupInvoicePageListV2(page, groupStr, cond, pars, invSum)
 	if e != nil {
 		resp.FailMsg("获取失败", "获取销售组开票统计列表失败, Err: "+e.Error(), c)
 		return
 	}
-
+	for _, v := range list {
+		if group, ok := groupMap[v.GroupId]; ok{
+			v.GroupName = group.GroupName
+		}
+	}
 	// 处理百分比, 乘100并保留两位小数
 	mulNum := decimal.NewFromFloat(100)
 	for i := range list {
@@ -118,7 +146,7 @@ func (ct *SellerController) GroupInvoiceList(c *gin.Context) {
 		ExportGroupInvoiceList(c, list, req)
 		return
 	}
-	page.SetTotal(total)
+	page.SetTotal(int64(total))
 	baseData := new(base.BaseData)
 	baseData.SetPage(page)
 	baseData.SetList(list)
@@ -223,8 +251,11 @@ func (ct *SellerController) InvoiceList(c *gin.Context) {
 		return
 	}
 	pars := make([]interface{}, 0)
+	adminPars := make([]interface{}, 0)
 	outCond := ` a.department_id = %d `
 	outCond = fmt.Sprintf(outCond, departmentId)
+	adminCond := ` department_id = %d `
+	adminCond = fmt.Sprintf(adminCond, departmentId)
 	totalCond := ` department_id = %d `
 	totalCond = fmt.Sprintf(totalCond, departmentId)
 	totalPars := make([]interface{}, 0)
@@ -246,13 +277,15 @@ func (ct *SellerController) InvoiceList(c *gin.Context) {
 		}
 		outCond += ` AND a.group_id IN (?) `
 		pars = append(pars, groupIds)
+		adminCond += ` AND group_id IN (?) `
+		adminPars = append(adminPars, groupIds)
 		totalCond += ` AND group_id IN (?) `
 		totalPars = append(totalPars, groupIds)
 	}
 	sumCond := ` invoice_type = ? AND is_deleted = 0 AND seller_id != 0 `
 	sumPars := make([]interface{}, 0)
 	sumPars = append(sumPars, fms.ContractInvoiceTypeMake)
-	cond := ` c.invoice_type = %d AND c.is_deleted = 0 AND c.seller_id != 0 `
+	cond := ` invoice_type = %d AND is_deleted = 0 AND seller_id != 0 `
 	cond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake)
 	// 开票日期
 	if req.StartDate != "" && req.EndDate != "" {
@@ -286,17 +319,46 @@ func (ct *SellerController) InvoiceList(c *gin.Context) {
 
 	// 查询列表
 	adminOB := new(crm.Admin)
-	total, e := adminOB.Count(totalCond, totalPars)
+	//total, e := adminOB.Count(totalCond, totalPars)
+	//if e != nil {
+	//	resp.FailMsg("获取失败", "获取销售开票统计列表总数失败, Err: "+e.Error(), c)
+	//	return
+	//}
+	//list, e := fms.GetCensusSellerInvoicePageList(page, cond, outCond, pars, invSum)
+	//if e != nil {
+	//	resp.FailMsg("获取失败", "获取销售开票统计列表失败, Err: "+e.Error(), c)
+	//	return
+	//}
+
+	adminList, e := adminOB.List(adminCond, adminPars)
 	if e != nil {
-		resp.FailMsg("获取失败", "获取销售开票统计列表总数失败, Err: "+e.Error(), c)
+		resp.FailMsg("获取失败", "获取销售列表失败, Err: "+e.Error(), c)
 		return
 	}
-	list, e := fms.GetCensusSellerInvoicePageList(page, cond, outCond, pars, invSum)
+	//total := len(adminList)
+	adminIdSlice := make([]string,0)
+	sellerMap := make(map[int]*crm.Admin)
+	for i := range adminList {
+		sellerMap[adminList[i].AdminId] = adminList[i]
+		adminIdSlice = append(adminIdSlice, strconv.Itoa(adminList[i].AdminId))
+	}
+
+
+	adminStr := strings.Join(adminIdSlice, ",")
+
+	total, list, e := fms.GetCensusSellerInvoicePageListV2(page, adminStr, cond, pars, invSum)
 	if e != nil {
 		resp.FailMsg("获取失败", "获取销售开票统计列表失败, Err: "+e.Error(), c)
 		return
 	}
 
+	for _, v := range list {
+		if admin, ok := sellerMap[v.SellerId]; ok{
+			v.GroupName = admin.GroupName
+			v.SellerName = admin.RealName
+			v.GroupId = admin.GroupId
+		}
+	}
 	// 分组信息, list的groupId可能是三级的ID, 要转为对应的二级
 	groupMap, e := crmService.GetSellerTeamGroupMap()
 	if e != nil {
@@ -327,7 +389,7 @@ func (ct *SellerController) InvoiceList(c *gin.Context) {
 		ExportInvoiceList(c, list, req)
 		return
 	}
-	page.SetTotal(total)
+	page.SetTotal(int64(total))
 	baseData := new(base.BaseData)
 	baseData.SetPage(page)
 	baseData.SetList(list)

+ 51 - 0
models/fms/contract_invoice.go

@@ -719,3 +719,54 @@ type NotPaymentCensusInfo struct {
 	ServiceProductId  int                                 `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
 	UnitName          string                              `json:"unit_name" description:"单位名称"`
 }
+
+// GetCensusSellerGroupInvoicePageListV2 获取销售组开票统计列表-分页
+func GetCensusSellerGroupInvoicePageListV2(page base.IPage,groupStr, condition string, pars []interface{}, amountSum float64) (results []*CensusSellerGroupInvoiceItem, err error) {
+	sql := ` 
+				SELECT
+					seller_group_id AS group_id,
+					SUM(amount) AS invoice_amount,
+					SUM(amount) / %f AS group_rate
+				FROM
+					contract_invoice
+				WHERE
+					%s
+				AND seller_group_id IN (`+groupStr+`)
+				GROUP BY
+					seller_group_id  ORDER BY %s LIMIT %d,%d
+			`
+
+	sql = fmt.Sprintf(sql, amountSum, condition, page.GetOrderItemsString(), page.Offset(), page.GetPageSize())
+	err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&results).Error
+	return
+}
+
+// GetCensusSellerInvoicePageList 获取销售开票统计列表-分页
+func GetCensusSellerInvoicePageListV2(page base.IPage, adminStr, condition string, pars []interface{}, amountSum float64) (count int, results []*CensusSellerInvoiceItem, err error) {
+	//sql := `SELECT a.admin_id AS seller_id, a.real_name AS seller_name, a.group_id, b.invoice_amount, b.group_rate, b.seller_rate FROM %s AS a LEFT JOIN `
+	sql := ` SELECT seller_id , SUM(amount) AS invoice_amount, 
+				SUM(amount) / (
+					SELECT SUM(amount) FROM contract_invoice WHERE %s
+				) AS group_rate,
+				SUM(amount) / %f AS seller_rate
+				FROM contract_invoice
+				WHERE %s
+				AND seller_id IN (`+adminStr+`)
+				GROUP BY seller_id ORDER BY %s LIMIT %d,%d `
+
+	sqlCount := ` select count(1) as count from (SELECT * 
+				FROM contract_invoice
+				WHERE %s
+				AND seller_id IN (`+adminStr+`) GROUP BY seller_id ) as a
+				 `
+	sqlCount = fmt.Sprintf(sqlCount, condition)
+	err = global.DEFAULT_MYSQL.Raw(sqlCount, pars...).Find(&count).Error
+
+	//query := global.DEFAULT_MYSQL.Table("contract_invoice").
+	//	Where(condition, pars...).Where("AND seller_id IN (`+adminStr+`)")
+	//query.Count(&count)
+
+	sql = fmt.Sprintf(sql, condition, amountSum, condition, page.GetOrderItemsString(), page.Offset(), page.GetPageSize())
+	err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&results).Error
+	return
+}