12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352 |
- package census
- import (
- "bytes"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "github.com/shopspring/decimal"
- "github.com/tealeg/xlsx"
- "hongze/fms_api/controller/resp"
- "hongze/fms_api/global"
- "hongze/fms_api/models/base"
- "hongze/fms_api/models/crm"
- "hongze/fms_api/models/fms"
- crmService "hongze/fms_api/services/crm"
- "hongze/fms_api/utils"
- "net/http"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- // SellerController 销售统计
- type SellerController struct{}
- // 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 (ct *SellerController) GroupInvoiceList(c *gin.Context) {
- // var req fms.CensusSellerGroupInvoiceListReq
- // 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
- // }
- // 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
- // }
- // outCond := ` department_id = %d AND parent_id = 0 `
- // outCond = fmt.Sprintf(outCond, departmentId)
- // cond := ` (invoice_type = %d OR invoice_type = %d) AND a.is_deleted = 0 AND a.seller_id != 0 `
- // cond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
- //
- // //adminCond := ` (invoice_type = %d OR invoice_type = %d) AND is_deleted = 0 AND seller_group_id != 0 `
- // //adminCond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
- // pars := make([]interface{}, 0)
- // // 开票日期
- // if req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
- // cond = fmt.Sprintf(cond, st, ed)
- //
- // //adminCond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
- // //adminCond = fmt.Sprintf(cond, st, ed)
- // }
- //
- // if req.CompanyType == 1 {
- // cond += ` AND b.contract_type = 1 `
- // //historyCond += ` AND new_company = 1 `
- // } else if req.CompanyType == 2 {
- // cond += ` AND b.contract_type IN (2,3,4) `
- // //historyCond += ` AND new_company = 0 `
- // }
- //
- // page := new(base.Page)
- // page.SetPageSize(req.PageSize)
- // page.SetCurrent(req.Current)
- // // 排序, 默认开票金额倒序
- // sortFieldMap := map[int]string{0: "invoice_amount", 1: "invoice_amount", 2: "group_rate"}
- // sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
- // page.AddOrderItem(base.OrderItem{Column: sortFieldMap[req.SortField], Asc: sortTypeMap[req.SortType]})
- // if req.IsExport == 1 {
- // page.SetPageSize(10000)
- // page.SetCurrent(1)
- // }
- //
- // // 查询开票金额总和(减少子查询)
- // invOB := new(fms.ContractInvoice)
- // sumCond := cond
- // sumPars := make([]interface{}, 0)
- // invSum, e := invOB.Sum("amount", sumCond, sumPars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取开票金额总和失败, Err: "+e.Error(), c)
- // return
- // }
- //
- // // 查询列表
- // groupOB := new(crm.SysGroup)
- // //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)
- // return
- // }
- // //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, ",")
- // total, 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 {
- // d := decimal.NewFromFloat(list[i].GroupRate)
- // d = d.Mul(mulNum).Round(2)
- // a, _ := d.Float64()
- // list[i].GroupRate = a
- // }
- //
- // // 是否导出
- // if req.IsExport == 1 {
- // ExportGroupInvoiceList(c, list, req)
- // return
- // }
- // page.SetTotal(int64(total))
- // baseData := new(base.BaseData)
- // baseData.SetPage(page)
- // baseData.SetList(list)
- // resp.OkData("获取成功", baseData, c)
- //}
- // ExportGroupInvoiceList 导出销售组开票统计列表
- func ExportGroupInvoiceList(c *gin.Context, list []*fms.CensusSellerGroupInvoiceItem, req fms.CensusSellerGroupInvoiceListReq) {
- // 生成Excel文件
- xlsxFile := xlsx.NewFile()
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheetName := "销售组开票统计"
- sheet, err := xlsxFile.AddSheet(sheetName)
- if err != nil {
- resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
- return
- }
- // 存在筛选则前两行显示时间信息
- if req.StartDate != "" && req.EndDate != "" {
- timeData := fmt.Sprintf("时间:%s至%s", req.StartDate, req.EndDate)
- rowA := sheet.AddRow()
- cellAA := rowA.AddCell()
- cellAA.SetString("销售统计表")
- cellAA.SetStyle(style)
- rowB := sheet.AddRow()
- rowB.AddCell().SetString(timeData)
- // 第三行空出
- sheet.AddRow()
- }
- // 数据表头
- rowTitle := []string{"排名", "销售组别", "收入金额(元)", "组别占比"}
- titleRow := sheet.AddRow()
- for i := range rowTitle {
- v := titleRow.AddCell()
- v.SetString(rowTitle[i])
- v.SetStyle(style)
- }
- // 填充数据
- for k, v := range list {
- dataRow := sheet.AddRow()
- dataRow.AddCell().SetString(fmt.Sprint(k + 1)) // 排名
- dataRow.AddCell().SetString(v.GroupName) // 销售组别
- dataRow.AddCell().SetString(fmt.Sprintf("%.2f", v.InvoiceAmount)) // 开票金额
- dataRow.AddCell().SetString(fmt.Sprint(v.GroupRate, "%")) // 组别占比
- }
- // 输出文件
- var buffer bytes.Buffer
- _ = xlsxFile.Write(&buffer)
- content := bytes.NewReader(buffer.Bytes())
- randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- fileName := sheetName + randStr + ".xlsx"
- c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
- c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
- }
- // InvoiceList
- // @Title 销售开票统计列表-弃用
- // @Description 销售开票统计列表-弃用
- // @Param GroupId query int false "组别ID"
- // @Param StartDate query string false "开始日期"
- // @Param EndDate query string false "结束日期"
- // @Param SortField query int false "排序字段: 1-开票金额; 2-组别占比; 3-全员占比"
- // @Param SortType query int false "排序方式: 1-正序; 2-倒序"
- // @Param IsExport query int false "是否导出: 0-否; 1-是"
- // @Success 200 {object} fms.CensusSellerInvoiceListReq
- // @router /census/seller/invoice_list [get]
- //func (ct *SellerController) InvoiceList(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
- // }
- // 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
- // }
- //
- // 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)
- //
- // if !req.ShowResign {
- // adminCond += ` AND enabled = 1 `
- // }
- // if req.GroupId > 0 {
- // // 筛选组别时, 查询当前组别的下级组(因为admin表存的group_id, 有三级的存的是子ID, 只有二级的存的才是父ID =_=!)
- // groupCond := `parent_id = ?`
- // groupPars := make([]interface{}, 0)
- // groupPars = append(groupPars, req.GroupId)
- // groupOB := new(crm.SysGroup)
- // groupList, e := groupOB.List(groupCond, groupPars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取组别下级组列表失败, Err: "+e.Error(), c)
- // return
- // }
- // groupIds := make([]int, 0)
- // groupIds = append(groupIds, req.GroupId)
- // for i := range groupList {
- // groupIds = append(groupIds, groupList[i].GroupId)
- // }
- // 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 = ? OR invoice_type = ? ) AND a.is_deleted = 0 AND a.seller_id != 0 `
- // sumPars := make([]interface{}, 0)
- // sumPars = append(sumPars, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
- // cond := ` (c.invoice_type = %d OR c.invoice_type = %d) AND c.is_deleted = 0 AND c.seller_id != 0 `
- // inCond := ` (invoice_type = %d OR invoice_type = %d) AND is_deleted = 0 AND seller_id != 0 `
- // cond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
- // inCond = fmt.Sprintf(inCond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
- //
- // if req.CompanyType == 1 {
- // cond += ` AND b.contract_type = 1 `
- // //historyCond += ` AND new_company = 1 `
- // } else if req.CompanyType == 2 {
- // cond += ` AND b.contract_type IN (2,3,4) `
- // //historyCond += ` AND new_company = 0 `
- // }
- //
- // // 开票日期
- // if req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (c.invoice_time BETWEEN '%s' AND '%s')`
- // inCond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
- // cond = fmt.Sprintf(cond, st, ed)
- // inCond = fmt.Sprintf(inCond, st, ed)
- // sumCond += ` AND (invoice_time BETWEEN ? AND ?)`
- // sumPars = append(sumPars, st, ed)
- // }
- //
- // page := new(base.Page)
- // page.SetPageSize(req.PageSize)
- // page.SetCurrent(req.Current)
- // // 排序, 默认开票金额倒序
- // sortFieldMap := map[int]string{0: "invoice_amount", 1: "invoice_amount", 2: "group_rate", 3: "seller_rate"}
- // sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
- // page.AddOrderItem(base.OrderItem{Column: sortFieldMap[req.SortField], Asc: sortTypeMap[req.SortType]})
- // if req.IsExport == 1 {
- // page.SetPageSize(10000)
- // page.SetCurrent(1)
- // }
- //
- // // 查询开票金额总和(减少子查询)
- // invOB := new(fms.ContractInvoice)
- // invSum, e := invOB.Sum("amount", sumCond, sumPars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取开票金额总和失败, Err: "+e.Error(), c)
- // return
- // }
- //
- // // 查询列表
- // adminOB := new(crm.Admin)
- // //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)
- // return
- // }
- // //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, inCond, 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 {
- // resp.FailMsg("获取失败", "获取组别对应信息失败, Err: "+e.Error(), c)
- // return
- // }
- //
- // mulNum := decimal.NewFromFloat(100)
- // for i := range list {
- // g := groupMap[list[i].GroupId]
- // if g != nil {
- // list[i].GroupId = g.GroupId
- // list[i].GroupName = g.GroupName
- // }
- // // 处理百分比, 乘100并保留两位小数
- // d := decimal.NewFromFloat(list[i].GroupRate)
- // d = d.Mul(mulNum).Round(2)
- // a, _ := d.Float64()
- // list[i].GroupRate = a
- // d2 := decimal.NewFromFloat(list[i].SellerRate)
- // d2 = d2.Mul(mulNum).Round(2)
- // a2, _ := d2.Float64()
- // list[i].SellerRate = a2
- // }
- //
- // // 是否导出
- // if req.IsExport == 1 {
- // ExportInvoiceList(c, list, req)
- // return
- // }
- // page.SetTotal(int64(total))
- // baseData := new(base.BaseData)
- // baseData.SetPage(page)
- // baseData.SetList(list)
- // 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)
- }
- var departmentId int
- if req.SellerType == 1 {
- departmentId = crm.SellerDepartmentId
- } else if req.SellerType == 2 {
- departmentId = crm.RaiSellerDepartmentId
- }
- var totalMoneySlice []float64
- historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
- // 累计值
- var accumulate float64
- var startDate, endDate string
- //开始日期
- if req.StartDate != "" && req.EndDate != "" {
- startDate = fmt.Sprint(req.StartDate, " 00:00:00")
- endDate = fmt.Sprint(req.EndDate, " 23:59:59")
- }
- startDateTime, _ := time.Parse(utils.FormatDateTime, startDate)
- endDateTime, _ := time.Parse(utils.FormatDateTime, endDate)
- cond := ` 1 = 1 `
- historyCond := ` 1=1 `
- pars := make([]interface{}, 0)
- historyPars := make([]interface{}, 0)
- if req.CompanyType == 1 {
- cond += ` AND b.contract_type = 1 `
- historyCond += ` AND new_company = 1 `
- } else if req.CompanyType == 2 {
- cond += ` AND b.contract_type IN (2,3,4) `
- historyCond += ` AND new_company = 0 `
- }
- //if req.SellerIds != "" {
- // sellerIds := strings.Split(req.SellerIds, ",")
- // historyCond += ` AND (seller_id in ? ) `
- // historyPars = append(historyPars, sellerIds)
- //}
- //groupIds := make([]int, 0)
- //if req.GroupId > 0 {
- // // 筛选组别时, 查询当前组别的下级组(因为admin表存的group_id, 有三级的存的是子ID, 只有二级的存的才是父ID =_=!)
- // groupCond := `parent_id = ?`
- // groupPars := make([]interface{}, 0)
- // groupPars = append(groupPars, req.GroupId)
- // groupOB := new(crm.SysGroup)
- // groupList, e := groupOB.List(groupCond, groupPars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取组别下级组列表失败, Err: "+e.Error(), c)
- // return
- // }
- // groupIds = append(groupIds, req.GroupId)
- // for i := range groupList {
- // groupIds = append(groupIds, groupList[i].GroupId)
- // }
- // cond += ` AND (c.seller_group_id IN (?) OR d.seller_group_id IN (?))`
- // pars = append(pars, groupIds, groupIds)
- // historyCond += ` AND group_id IN (?) `
- // historyPars = append(historyPars, groupIds)
- //}
- // 获取销售分组信息
- sellerList, e := crmService.GetSellerDepartmentListWithEnable()
- if e != nil {
- resp.FailData("获取销售失败", "Err:"+e.Error(), c)
- return
- }
- ficcSellerIds := make([]string, 0)
- raiSellerIds := make([]string, 0)
- sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
- for i := range sellerList {
- sellerMap[sellerList[i].SellerId] = sellerList[i]
- if sellerList[i].DepartmentId == crm.SellerDepartmentId {
- ficcSellerIds = append(ficcSellerIds, strconv.Itoa(sellerList[i].SellerId))
- } else if sellerList[i].DepartmentId == crm.RaiSellerDepartmentId {
- raiSellerIds = append(raiSellerIds, strconv.Itoa(sellerList[i].SellerId))
- }
- }
- sellerIds := make([]string, 0)
- if req.SellerType == 1 {
- //cond += ` AND b.contract_type = 1 `
- //historyCond += ` AND new_company = 1 `
- sellerIds = ficcSellerIds
- } else if req.SellerType == 2 {
- //cond += ` AND b.contract_type IN (2,3,4) `
- //historyCond += ` AND new_company = 0 `
- sellerIds = raiSellerIds
- }
- st := startDate
- ed := endDate
- 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
- var amountSum float64
- if len(summaryIds) > 0 {
- amountCond := `a.id IN ? `
- amountPars := make([]interface{}, 0)
- amountPars = append(amountPars, summaryIds)
- //if len(groupIds) > 0 {
- // amountCond += ` AND IF ( a.invoice_id = 0, d.seller_group_id, b.seller_group_id ) IN (?) `
- // amountPars = append(amountPars, groupIds)
- //}
- if req.SellerType != 0 {
- //sellerIds := strings.Split(req.SellerIds, ",")
- amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time > "2023-04-01")`
- amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time > "2023-04-01")) `
- amountPars = append(amountPars, sellerIds, sellerIds)
- } else {
- amountCond += ` AND ((a.invoice_id <> 0 )`
- amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 ))`
- amountPars = append(amountPars)
- }
- results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
- if e != nil {
- resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
- return
- }
- //dataList = append(dataList, results...)
- 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
- amountSum = 0
- }
- results, e := fms.GetIncomeHistory(historyCond, historyPars)
- if e != nil {
- resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
- return
- }
- 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 len(groupIds) > 0 {
- // amountCond += ` AND IF ( a.invoice_id = 0, d.seller_group_id, b.seller_group_id ) IN (?) `
- // amountPars = append(amountPars, groupIds)
- //}
- if req.SellerType != 0 {
- //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 != "" {
- historyCond += ` 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(historyCond, 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)
- } else {
- 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
- var amountSum float64
- if len(summaryIds) > 0 {
- amountCond := `a.id IN ? `
- amountPars := make([]interface{}, 0)
- amountPars = append(amountPars, summaryIds)
- //if len(groupIds) > 0 {
- // amountCond += ` AND IF ( a.invoice_id = 0, d.seller_group_id, b.seller_group_id ) IN (?) `
- // amountPars = append(amountPars, groupIds)
- //}
- if req.SellerType != 0 {
- //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, historyTime, ed, sellerIds, historyTime, 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, historyTime, ed, historyTime, ed)
- }
- results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
- if e != nil {
- resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
- return
- }
- //dataList = append(dataList, results...)
- 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
- amountSum = 0
- }
- if st != "" && ed != "" {
- historyCond += ` AND (invoice_time BETWEEN ? AND ? )`
- historyPars = append(historyPars, st, historyTime)
- }
- results, e := fms.GetIncomeHistory(historyCond, historyPars)
- if e != nil {
- resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
- return
- }
- 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("totalMoneySlice:", len(totalMoneySlice))
- adminOB := new(crm.Admin)
- adminPars := make([]interface{}, 0)
- adminCond := ` 1=1 `
- if req.SellerType != 0 {
- adminCond += ` AND department_id = ? `
- adminPars = append(adminPars, departmentId)
- }
- if req.SellerIds != "" {
- adminSellerIds := strings.Split(req.SellerIds, ",")
- adminCond += ` AND admin_id IN ? `
- adminPars = append(adminPars, adminSellerIds)
- }
- if !req.ShowResign {
- adminCond += ` AND enabled = 1 `
- }
- adminList, e := adminOB.List(adminCond, adminPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取销售列表失败, Err: "+e.Error(), c)
- return
- }
- 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,
- })
- }
- }
- sort.Slice(list, func(i, j int) bool {
- if req.SortField == 2 {
- if req.SortType == 2 {
- return list[i].GroupRate < list[j].GroupRate
- } else {
- return list[i].GroupRate > list[j].GroupRate
- }
- } else {
- if req.SortType == 2 {
- return list[i].InvoiceAmount < list[j].InvoiceAmount
- } else {
- return list[i].InvoiceAmount > list[j].InvoiceAmount
- }
- }
- })
- //var respList fmsService.CensusSellerListByAmountAsc
- //respList = list
- //sort.Sort(list)
- // 是否导出
- if req.IsExport == 1 {
- ExportInvoiceList(c, list, req)
- return
- }
- page.SetTotal(int64(len(list)))
- baseData := new(base.BaseData)
- baseData.SetPage(page)
- if req.Current*req.PageSize > int64(len(list)) {
- baseData.SetList(list[(req.Current-1)*req.PageSize : len(list)])
- } else {
- baseData.SetList(list[(req.Current-1)*req.PageSize : req.Current*req.PageSize])
- }
- resp.OkData("获取成功", baseData, c)
- }
- // ExportInvoiceList 导出销售开票统计列表
- func ExportInvoiceList(c *gin.Context, list []*fms.CensusSellerInvoiceItem, req fms.CensusSellerInvoiceListReq) {
- // 生成Excel文件
- xlsxFile := xlsx.NewFile()
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheetName := "销售开票统计"
- sheet, err := xlsxFile.AddSheet(sheetName)
- if err != nil {
- resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
- return
- }
- // 存在筛选则前两行显示时间信息
- if req.StartDate != "" && req.EndDate != "" {
- timeData := fmt.Sprintf("时间:%s至%s", req.StartDate, req.EndDate)
- rowA := sheet.AddRow()
- cellAA := rowA.AddCell()
- cellAA.SetString("销售统计表")
- cellAA.SetStyle(style)
- rowB := sheet.AddRow()
- rowB.AddCell().SetString(timeData)
- // 第三行空出
- sheet.AddRow()
- }
- // 数据表头
- rowTitle := []string{"排名", "销售员", "销售组别", "收入金额(元)", "小组占比", "全员占比"}
- titleRow := sheet.AddRow()
- for i := range rowTitle {
- v := titleRow.AddCell()
- v.SetString(rowTitle[i])
- v.SetStyle(style)
- }
- // 填充数据
- for k, v := range list {
- dataRow := sheet.AddRow()
- dataRow.AddCell().SetString(fmt.Sprint(k + 1)) // 排名
- dataRow.AddCell().SetString(v.SellerName) // 销售员
- dataRow.AddCell().SetString(v.GroupName) // 销售组别
- dataRow.AddCell().SetString(fmt.Sprintf("%.2f", v.InvoiceAmount)) // 开票金额
- dataRow.AddCell().SetString(fmt.Sprint(v.GroupRate, "%")) // 小组占比
- dataRow.AddCell().SetString(fmt.Sprint(v.SellerRate, "%")) // 全员占比
- }
- // 输出文件
- var buffer bytes.Buffer
- _ = xlsxFile.Write(&buffer)
- content := bytes.NewReader(buffer.Bytes())
- randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- fileName := sheetName + randStr + ".xlsx"
- c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
- c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- 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 {
- err, ok := e.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+e.Error(), c)
- return
- }
- resp.FailData("参数解析失败", err.Translate(global.Trans), c)
- return
- }
- //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)
- //}
- var totalMoneySlice []float64
- historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
- // 累计值
- var accumulate float64
- //dataList := make([]*fms.IncomeSummaryItem, 0)
- //historydataList := make([]*fms.IncomeSummaryItem, 0)
- //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
- var startDate, endDate string
- //开始日期
- if req.StartDate != "" && req.EndDate != "" {
- startDate = fmt.Sprint(req.StartDate, " 00:00:00")
- endDate = fmt.Sprint(req.EndDate, " 23:59:59")
- }
- startDateTime, _ := time.Parse(utils.FormatDate, req.StartDate)
- endDateTime, _ := time.Parse(utils.FormatDate, req.EndDate)
- cond := `1 = 1`
- histortyCond := `1 = 1`
- pars := make([]interface{}, 0)
- historyPars := make([]interface{}, 0)
- if req.CompanyType == 1 {
- cond += ` AND b.contract_type = 1 `
- histortyCond += ` AND new_company = 1 `
- } else if req.CompanyType == 2 {
- cond += ` AND b.contract_type IN (2,3,4) `
- histortyCond += ` 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)
- //校验日期,分段查询
- if req.StartDate == "" && req.EndDate == "" {
- summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
- if e != nil {
- return
- }
- // 开票到款金额合计(换算后)
- var amountTotal float64
- var amountSum 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...)
- 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
- }
- }
- amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
- accumulate += amountTotal
- amountSum = 0
- }
- //var amountTotal float64
- results, e := fms.GetIncomeHistory(histortyCond, 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
- }
- }
- 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
- }
- }
- 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 != "" {
- histortyCond += ` 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(histortyCond, 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
- }
- }
- amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
- accumulate += amountTotal
- //fmt.Println("partAccumulate:",partAccumulate)
- } else {
- // 有时间,分段查询
- // 新查询
- 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
- var amountSum 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...)
- 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
- }
- }
- amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
- accumulate += amountTotal
- amountSum = 0
- //var amountTotal float64
- // 旧查询
- }
- if st != "" && ed != "" {
- histortyCond += ` AND (invoice_time BETWEEN ? AND ? )`
- historyPars = append(historyPars, st, ed)
- }
- results, e := fms.GetIncomeHistory(histortyCond, 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
- }
- }
- amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
- accumulate += amountTotal
- }
- fmt.Println("totalMoneySlice:", len(totalMoneySlice))
- var departmentId int
- 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
- }
- groupCond := ` department_id = %d AND parent_id = 0 `
- groupCond = fmt.Sprintf(groupCond, departmentId)
- groupPars := make([]interface{}, 0)
- groupOB := new(crm.SysGroup)
- groupList, e := groupOB.List(groupCond, groupPars)
- if e != nil {
- resp.FailMsg("查询错误", fmt.Sprintf("获取组别列表失败, Err: %s", e.Error()), c)
- return
- }
- //total := len(groupList)
- groupMap := make(map[int]*crm.SysGroup)
- for i := range groupList {
- groupMap[groupList[i].GroupId] = groupList[i]
- }
- list := make([]*fms.CensusSellerGroupInvoiceItem, 0)
- for _, group := range groupList {
- if v, ok := groupInvoiceMap[group.GroupId]; ok {
- rate := v / accumulate
- mulNum := decimal.NewFromFloat(100)
- newRate := decimal.NewFromFloat(rate)
- newRate = newRate.Mul(mulNum).Round(2)
- finalRate, _ := newRate.Float64()
- amuont, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v), 64)
- groupName := ""
- if groupItem, ok := groupMap[group.GroupId]; ok {
- groupName = groupItem.GroupName
- }
- list = append(list, &fms.CensusSellerGroupInvoiceItem{
- GroupId: group.GroupId,
- GroupName: groupName,
- InvoiceAmount: amuont,
- GroupRate: finalRate,
- })
- }
- }
- sort.Slice(list, func(i, j int) bool {
- if req.SortField == 2 {
- if req.SortType == 2 {
- return list[i].GroupRate < list[j].GroupRate
- } else {
- return list[i].GroupRate > list[j].GroupRate
- }
- } else {
- if req.SortType == 2 {
- return list[i].InvoiceAmount < list[j].InvoiceAmount
- } else {
- return list[i].InvoiceAmount > list[j].InvoiceAmount
- }
- }
- })
- //var respList fms.CensusSellerGroupList
- //respList = list
- //sort.Sort(respList)
- // 是否导出
- if req.IsExport == 1 {
- ExportGroupInvoiceList(c, list, req)
- return
- }
- resp.OkData("获取成功", list, c)
- }
|