123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365 |
- 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
- //} else if req.SellerType == 0 {
- // resp.Fail("请选择销售类型", c)
- // return
- //} else {
- // resp.Fail("请选择正确的销售类型", 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.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, ",")
- cond += ` AND (a.seller_id in ? ) `
- historyCond += ` AND (seller_id in ? ) `
- pars = append(pars, sellerIds)
- 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)
- //}
- 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, ",")
- // 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 := 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.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
- }
- 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.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 != "" {
- 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.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
- }
- 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))
- 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)
- if !req.ShowResign {
- adminCond += ` AND enabled = 1 `
- }
- 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,
- })
- }
- }
- 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)
- }
|