1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354 |
- 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"
- )
- type SellerController struct{}
- func ExportGroupInvoiceList(c *gin.Context, list []*fms.CensusSellerGroupInvoiceItem, req fms.CensusSellerGroupInvoiceListReq) {
-
- 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)
- }
- 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 `
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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 {
-
-
- sellerIds = ficcSellerIds
- } else if req.SellerType == 2 {
-
-
- 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 req.SellerType != 0 {
-
- 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
- }
-
- 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) {
-
-
- 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.SellerType != 0 {
-
- 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
- }
-
- 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) {
-
-
-
-
- if st != "" && ed != "" {
- historyCond += ` AND (invoice_time BETWEEN ? AND ? )`
- historyPars = append(historyPars, st, 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
-
- 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 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.SellerType != 0 {
-
- 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
- }
-
- 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, ed)
- }
- 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
- }
- }
- })
-
-
-
-
- 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)
- }
- func ExportInvoiceList(c *gin.Context, list []*fms.CensusSellerInvoiceItem, req fms.CensusSellerInvoiceListReq) {
-
- 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)
- }
- 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
- }
-
-
-
-
-
-
-
- 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.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 `
- }
-
-
-
-
-
-
-
-
-
-
-
- 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
- }
-
- 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
- }
-
- results, e := fms.GetIncomeHistory(histortyCond, 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
- }
- }
- amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
- accumulate += amountTotal
- } else if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
-
-
- 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
- }
-
- 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) {
-
-
-
-
- if st != "" && ed != "" {
- histortyCond += ` AND (invoice_time BETWEEN ? AND ? )`
- historyPars = append(historyPars, st, 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
-
- 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 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
- }
-
- 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
-
-
- }
- 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
- }
-
- 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
- }
-
- 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
- }
- }
- })
-
-
-
-
- if req.IsExport == 1 {
- ExportGroupInvoiceList(c, list, req)
- return
- }
- resp.OkData("获取成功", list, c)
- }
|