seller.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. package census
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/go-playground/validator/v10"
  7. "github.com/shopspring/decimal"
  8. "github.com/tealeg/xlsx"
  9. "hongze/fms_api/controller/resp"
  10. "hongze/fms_api/global"
  11. "hongze/fms_api/models/base"
  12. "hongze/fms_api/models/crm"
  13. "hongze/fms_api/models/fms"
  14. crmService "hongze/fms_api/services/crm"
  15. "hongze/fms_api/utils"
  16. "net/http"
  17. "sort"
  18. "strconv"
  19. "time"
  20. )
  21. // SellerController 销售统计
  22. type SellerController struct{}
  23. // GroupInvoiceList
  24. // @Title 销售组开票统计列表-弃用
  25. // @Description 销售组开票统计列表-弃用
  26. // @Param StartDate query string false "开始日期"
  27. // @Param EndDate query string false "结束日期"
  28. // @Param SortField query int false "排序字段: 1-开票金额; 2-组别占比"
  29. // @Param SortType query int false "排序方式: 1-正序; 2-倒序"
  30. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  31. // @Success 200 {object} fms.CensusSellerGroupInvoiceItem
  32. // @router /census/seller/group_invoice_list [get]
  33. //func (ct *SellerController) GroupInvoiceList(c *gin.Context) {
  34. // var req fms.CensusSellerGroupInvoiceListReq
  35. // if e := c.BindQuery(&req); e != nil {
  36. // err, ok := e.(validator.ValidationErrors)
  37. // if !ok {
  38. // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  39. // return
  40. // }
  41. // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  42. // return
  43. // }
  44. // var departmentId int
  45. // if req.SellerType == 1 {
  46. // departmentId = crm.SellerDepartmentId
  47. // } else if req.SellerType == 2 {
  48. // departmentId = crm.RaiSellerDepartmentId
  49. // } else if req.SellerType == 0 {
  50. // resp.Fail("请选择销售类型", c)
  51. // return
  52. // } else {
  53. // resp.Fail("请选择正确的销售类型", c)
  54. // return
  55. // }
  56. // outCond := ` department_id = %d AND parent_id = 0 `
  57. // outCond = fmt.Sprintf(outCond, departmentId)
  58. // cond := ` (invoice_type = %d OR invoice_type = %d) AND a.is_deleted = 0 AND a.seller_id != 0 `
  59. // cond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  60. //
  61. // //adminCond := ` (invoice_type = %d OR invoice_type = %d) AND is_deleted = 0 AND seller_group_id != 0 `
  62. // //adminCond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  63. // pars := make([]interface{}, 0)
  64. // // 开票日期
  65. // if req.StartDate != "" && req.EndDate != "" {
  66. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  67. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  68. // cond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
  69. // cond = fmt.Sprintf(cond, st, ed)
  70. //
  71. // //adminCond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
  72. // //adminCond = fmt.Sprintf(cond, st, ed)
  73. // }
  74. //
  75. // if req.CompanyType == 1 {
  76. // cond += ` AND b.contract_type = 1 `
  77. // //historyCond += ` AND new_company = 1 `
  78. // } else if req.CompanyType == 2 {
  79. // cond += ` AND b.contract_type IN (2,3,4) `
  80. // //historyCond += ` AND new_company = 0 `
  81. // }
  82. //
  83. // page := new(base.Page)
  84. // page.SetPageSize(req.PageSize)
  85. // page.SetCurrent(req.Current)
  86. // // 排序, 默认开票金额倒序
  87. // sortFieldMap := map[int]string{0: "invoice_amount", 1: "invoice_amount", 2: "group_rate"}
  88. // sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
  89. // page.AddOrderItem(base.OrderItem{Column: sortFieldMap[req.SortField], Asc: sortTypeMap[req.SortType]})
  90. // if req.IsExport == 1 {
  91. // page.SetPageSize(10000)
  92. // page.SetCurrent(1)
  93. // }
  94. //
  95. // // 查询开票金额总和(减少子查询)
  96. // invOB := new(fms.ContractInvoice)
  97. // sumCond := cond
  98. // sumPars := make([]interface{}, 0)
  99. // invSum, e := invOB.Sum("amount", sumCond, sumPars)
  100. // if e != nil {
  101. // resp.FailMsg("获取失败", "获取开票金额总和失败, Err: "+e.Error(), c)
  102. // return
  103. // }
  104. //
  105. // // 查询列表
  106. // groupOB := new(crm.SysGroup)
  107. // //totalCond := outCond
  108. // //totalPars := make([]interface{}, 0)
  109. // //total, e := groupOB.Count(totalCond, totalPars)
  110. // //if e != nil {
  111. // // resp.FailMsg("获取失败", "获取销售组开票统计列表总数失败, Err: "+e.Error(), c)
  112. // // return
  113. // //}
  114. // //list, e := fms.GetCensusSellerGroupInvoicePageList(page, cond, outCond, pars, invSum)
  115. // //if e != nil {
  116. // // resp.FailMsg("获取失败", "获取销售组开票统计列表失败, Err: "+e.Error(), c)
  117. // // return
  118. // //}
  119. // groupCond := ` department_id = %d AND parent_id = 0 `
  120. // groupCond = fmt.Sprintf(groupCond, departmentId)
  121. // groupPars := make([]interface{}, 0)
  122. // groupList, e := groupOB.List(groupCond, groupPars)
  123. // if e != nil {
  124. // resp.FailMsg("获取失败", "获取组别列表失败, Err: "+e.Error(), c)
  125. // return
  126. // }
  127. // //total := len(groupList)
  128. // groupMap := make(map[int]*crm.SysGroup)
  129. // groupIdSlice := make([]string, 0)
  130. // for i := range groupList {
  131. // groupMap[groupList[i].GroupId] = groupList[i]
  132. // groupIdSlice = append(groupIdSlice, strconv.Itoa(groupList[i].GroupId))
  133. // }
  134. //
  135. // groupStr := strings.Join(groupIdSlice, ",")
  136. // total, list, e := fms.GetCensusSellerGroupInvoicePageListV2(page, groupStr, cond, pars, invSum)
  137. // if e != nil {
  138. // resp.FailMsg("获取失败", "获取销售组开票统计列表失败, Err: "+e.Error(), c)
  139. // return
  140. // }
  141. // for _, v := range list {
  142. // if group, ok := groupMap[v.GroupId]; ok {
  143. // v.GroupName = group.GroupName
  144. // }
  145. // }
  146. // // 处理百分比, 乘100并保留两位小数
  147. // mulNum := decimal.NewFromFloat(100)
  148. // for i := range list {
  149. // d := decimal.NewFromFloat(list[i].GroupRate)
  150. // d = d.Mul(mulNum).Round(2)
  151. // a, _ := d.Float64()
  152. // list[i].GroupRate = a
  153. // }
  154. //
  155. // // 是否导出
  156. // if req.IsExport == 1 {
  157. // ExportGroupInvoiceList(c, list, req)
  158. // return
  159. // }
  160. // page.SetTotal(int64(total))
  161. // baseData := new(base.BaseData)
  162. // baseData.SetPage(page)
  163. // baseData.SetList(list)
  164. // resp.OkData("获取成功", baseData, c)
  165. //}
  166. // ExportGroupInvoiceList 导出销售组开票统计列表
  167. func ExportGroupInvoiceList(c *gin.Context, list []*fms.CensusSellerGroupInvoiceItem, req fms.CensusSellerGroupInvoiceListReq) {
  168. // 生成Excel文件
  169. xlsxFile := xlsx.NewFile()
  170. style := xlsx.NewStyle()
  171. alignment := xlsx.Alignment{
  172. Horizontal: "center",
  173. Vertical: "center",
  174. WrapText: true,
  175. }
  176. style.Alignment = alignment
  177. style.ApplyAlignment = true
  178. sheetName := "销售组开票统计"
  179. sheet, err := xlsxFile.AddSheet(sheetName)
  180. if err != nil {
  181. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  182. return
  183. }
  184. // 存在筛选则前两行显示时间信息
  185. if req.StartDate != "" && req.EndDate != "" {
  186. timeData := fmt.Sprintf("时间:%s至%s", req.StartDate, req.EndDate)
  187. rowA := sheet.AddRow()
  188. cellAA := rowA.AddCell()
  189. cellAA.SetString("销售统计表")
  190. cellAA.SetStyle(style)
  191. rowB := sheet.AddRow()
  192. rowB.AddCell().SetString(timeData)
  193. // 第三行空出
  194. sheet.AddRow()
  195. }
  196. // 数据表头
  197. rowTitle := []string{"排名", "销售组别", "收入金额(元)", "组别占比"}
  198. titleRow := sheet.AddRow()
  199. for i := range rowTitle {
  200. v := titleRow.AddCell()
  201. v.SetString(rowTitle[i])
  202. v.SetStyle(style)
  203. }
  204. // 填充数据
  205. for k, v := range list {
  206. dataRow := sheet.AddRow()
  207. dataRow.AddCell().SetString(fmt.Sprint(k + 1)) // 排名
  208. dataRow.AddCell().SetString(v.GroupName) // 销售组别
  209. dataRow.AddCell().SetString(fmt.Sprint(v.InvoiceAmount)) // 开票金额
  210. dataRow.AddCell().SetString(fmt.Sprint(v.GroupRate, "%")) // 组别占比
  211. }
  212. // 输出文件
  213. var buffer bytes.Buffer
  214. _ = xlsxFile.Write(&buffer)
  215. content := bytes.NewReader(buffer.Bytes())
  216. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  217. fileName := sheetName + randStr + ".xlsx"
  218. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  219. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  220. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  221. }
  222. // InvoiceList
  223. // @Title 销售开票统计列表-弃用
  224. // @Description 销售开票统计列表-弃用
  225. // @Param GroupId query int false "组别ID"
  226. // @Param StartDate query string false "开始日期"
  227. // @Param EndDate query string false "结束日期"
  228. // @Param SortField query int false "排序字段: 1-开票金额; 2-组别占比; 3-全员占比"
  229. // @Param SortType query int false "排序方式: 1-正序; 2-倒序"
  230. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  231. // @Success 200 {object} fms.CensusSellerInvoiceListReq
  232. // @router /census/seller/invoice_list [get]
  233. //func (ct *SellerController) InvoiceList(c *gin.Context) {
  234. // var req fms.CensusSellerInvoiceListReq
  235. // if e := c.BindQuery(&req); e != nil {
  236. // err, ok := e.(validator.ValidationErrors)
  237. // if !ok {
  238. // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  239. // return
  240. // }
  241. // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  242. // return
  243. // }
  244. // var departmentId int
  245. // if req.SellerType == 1 {
  246. // departmentId = crm.SellerDepartmentId
  247. // } else if req.SellerType == 2 {
  248. // departmentId = crm.RaiSellerDepartmentId
  249. // } else if req.SellerType == 0 {
  250. // resp.Fail("请选择销售类型", c)
  251. // return
  252. // } else {
  253. // resp.Fail("请选择正确的销售类型", c)
  254. // return
  255. // }
  256. //
  257. // pars := make([]interface{}, 0)
  258. // adminPars := make([]interface{}, 0)
  259. // outCond := ` a.department_id = %d `
  260. // outCond = fmt.Sprintf(outCond, departmentId)
  261. // adminCond := ` department_id = %d `
  262. // adminCond = fmt.Sprintf(adminCond, departmentId)
  263. // totalCond := ` department_id = %d `
  264. // totalCond = fmt.Sprintf(totalCond, departmentId)
  265. // totalPars := make([]interface{}, 0)
  266. //
  267. // if !req.ShowResign {
  268. // adminCond += ` AND enabled = 1 `
  269. // }
  270. // if req.GroupId > 0 {
  271. // // 筛选组别时, 查询当前组别的下级组(因为admin表存的group_id, 有三级的存的是子ID, 只有二级的存的才是父ID =_=!)
  272. // groupCond := `parent_id = ?`
  273. // groupPars := make([]interface{}, 0)
  274. // groupPars = append(groupPars, req.GroupId)
  275. // groupOB := new(crm.SysGroup)
  276. // groupList, e := groupOB.List(groupCond, groupPars)
  277. // if e != nil {
  278. // resp.FailMsg("获取失败", "获取组别下级组列表失败, Err: "+e.Error(), c)
  279. // return
  280. // }
  281. // groupIds := make([]int, 0)
  282. // groupIds = append(groupIds, req.GroupId)
  283. // for i := range groupList {
  284. // groupIds = append(groupIds, groupList[i].GroupId)
  285. // }
  286. // outCond += ` AND a.group_id IN (?) `
  287. // pars = append(pars, groupIds)
  288. // adminCond += ` AND group_id IN (?) `
  289. // adminPars = append(adminPars, groupIds)
  290. // totalCond += ` AND group_id IN (?) `
  291. // totalPars = append(totalPars, groupIds)
  292. // }
  293. //
  294. // sumCond := ` (invoice_type = ? OR invoice_type = ? ) AND a.is_deleted = 0 AND a.seller_id != 0 `
  295. // sumPars := make([]interface{}, 0)
  296. // sumPars = append(sumPars, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  297. // cond := ` (c.invoice_type = %d OR c.invoice_type = %d) AND c.is_deleted = 0 AND c.seller_id != 0 `
  298. // inCond := ` (invoice_type = %d OR invoice_type = %d) AND is_deleted = 0 AND seller_id != 0 `
  299. // cond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  300. // inCond = fmt.Sprintf(inCond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  301. //
  302. // if req.CompanyType == 1 {
  303. // cond += ` AND b.contract_type = 1 `
  304. // //historyCond += ` AND new_company = 1 `
  305. // } else if req.CompanyType == 2 {
  306. // cond += ` AND b.contract_type IN (2,3,4) `
  307. // //historyCond += ` AND new_company = 0 `
  308. // }
  309. //
  310. // // 开票日期
  311. // if req.StartDate != "" && req.EndDate != "" {
  312. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  313. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  314. // cond += ` AND (c.invoice_time BETWEEN '%s' AND '%s')`
  315. // inCond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
  316. // cond = fmt.Sprintf(cond, st, ed)
  317. // inCond = fmt.Sprintf(inCond, st, ed)
  318. // sumCond += ` AND (invoice_time BETWEEN ? AND ?)`
  319. // sumPars = append(sumPars, st, ed)
  320. // }
  321. //
  322. // page := new(base.Page)
  323. // page.SetPageSize(req.PageSize)
  324. // page.SetCurrent(req.Current)
  325. // // 排序, 默认开票金额倒序
  326. // sortFieldMap := map[int]string{0: "invoice_amount", 1: "invoice_amount", 2: "group_rate", 3: "seller_rate"}
  327. // sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
  328. // page.AddOrderItem(base.OrderItem{Column: sortFieldMap[req.SortField], Asc: sortTypeMap[req.SortType]})
  329. // if req.IsExport == 1 {
  330. // page.SetPageSize(10000)
  331. // page.SetCurrent(1)
  332. // }
  333. //
  334. // // 查询开票金额总和(减少子查询)
  335. // invOB := new(fms.ContractInvoice)
  336. // invSum, e := invOB.Sum("amount", sumCond, sumPars)
  337. // if e != nil {
  338. // resp.FailMsg("获取失败", "获取开票金额总和失败, Err: "+e.Error(), c)
  339. // return
  340. // }
  341. //
  342. // // 查询列表
  343. // adminOB := new(crm.Admin)
  344. // //total, e := adminOB.Count(totalCond, totalPars)
  345. // //if e != nil {
  346. // // resp.FailMsg("获取失败", "获取销售开票统计列表总数失败, Err: "+e.Error(), c)
  347. // // return
  348. // //}
  349. // //list, e := fms.GetCensusSellerInvoicePageList(page, cond, outCond, pars, invSum)
  350. // //if e != nil {
  351. // // resp.FailMsg("获取失败", "获取销售开票统计列表失败, Err: "+e.Error(), c)
  352. // // return
  353. // //}
  354. //
  355. // adminList, e := adminOB.List(adminCond, adminPars)
  356. // if e != nil {
  357. // resp.FailMsg("获取失败", "获取销售列表失败, Err: "+e.Error(), c)
  358. // return
  359. // }
  360. // //total := len(adminList)
  361. // adminIdSlice := make([]string, 0)
  362. // sellerMap := make(map[int]*crm.Admin)
  363. // for i := range adminList {
  364. // sellerMap[adminList[i].AdminId] = adminList[i]
  365. // adminIdSlice = append(adminIdSlice, strconv.Itoa(adminList[i].AdminId))
  366. // }
  367. //
  368. // adminStr := strings.Join(adminIdSlice, ",")
  369. //
  370. // total, list, e := fms.GetCensusSellerInvoicePageListV2(page, adminStr, inCond, cond, pars, invSum)
  371. // if e != nil {
  372. // resp.FailMsg("获取失败", "获取销售开票统计列表失败, Err: "+e.Error(), c)
  373. // return
  374. // }
  375. //
  376. // for _, v := range list {
  377. // if admin, ok := sellerMap[v.SellerId]; ok {
  378. // v.GroupName = admin.GroupName
  379. // v.SellerName = admin.RealName
  380. // v.GroupId = admin.GroupId
  381. // }
  382. // }
  383. // // 分组信息, list的groupId可能是三级的ID, 要转为对应的二级
  384. // groupMap, e := crmService.GetSellerTeamGroupMap()
  385. // if e != nil {
  386. // resp.FailMsg("获取失败", "获取组别对应信息失败, Err: "+e.Error(), c)
  387. // return
  388. // }
  389. //
  390. // mulNum := decimal.NewFromFloat(100)
  391. // for i := range list {
  392. // g := groupMap[list[i].GroupId]
  393. // if g != nil {
  394. // list[i].GroupId = g.GroupId
  395. // list[i].GroupName = g.GroupName
  396. // }
  397. // // 处理百分比, 乘100并保留两位小数
  398. // d := decimal.NewFromFloat(list[i].GroupRate)
  399. // d = d.Mul(mulNum).Round(2)
  400. // a, _ := d.Float64()
  401. // list[i].GroupRate = a
  402. // d2 := decimal.NewFromFloat(list[i].SellerRate)
  403. // d2 = d2.Mul(mulNum).Round(2)
  404. // a2, _ := d2.Float64()
  405. // list[i].SellerRate = a2
  406. // }
  407. //
  408. // // 是否导出
  409. // if req.IsExport == 1 {
  410. // ExportInvoiceList(c, list, req)
  411. // return
  412. // }
  413. // page.SetTotal(int64(total))
  414. // baseData := new(base.BaseData)
  415. // baseData.SetPage(page)
  416. // baseData.SetList(list)
  417. // resp.OkData("获取成功", baseData, c)
  418. //}
  419. func (this *SellerController) InvoiceListV2(c *gin.Context) {
  420. var req fms.CensusSellerInvoiceListReq
  421. if e := c.BindQuery(&req); e != nil {
  422. err, ok := e.(validator.ValidationErrors)
  423. if !ok {
  424. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  425. return
  426. }
  427. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  428. return
  429. }
  430. page := new(base.Page)
  431. page.SetPageSize(req.PageSize)
  432. page.SetCurrent(req.Current)
  433. if req.IsExport == 1 {
  434. page.SetPageSize(10000)
  435. page.SetCurrent(1)
  436. }
  437. //if req.SellerIds != "" {
  438. // sellerIds := strings.Split(req.SellerIds, ",")
  439. // cond += ` AND (a.seller_id in ? ) `
  440. // historyCond += ` AND (seller_id in ? ) `
  441. // pars = append(pars, sellerIds)
  442. // historyPars = append(historyPars, sellerIds)
  443. //}
  444. var departmentId int
  445. if req.SellerType == 1 {
  446. departmentId = crm.SellerDepartmentId
  447. } else if req.SellerType == 2 {
  448. departmentId = crm.RaiSellerDepartmentId
  449. } else if req.SellerType == 0 {
  450. resp.Fail("请选择销售类型", c)
  451. return
  452. } else {
  453. resp.Fail("请选择正确的销售类型", c)
  454. return
  455. }
  456. var totalMoneySlice []float64
  457. historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  458. // 累计值
  459. var accumulate float64
  460. var startDate, endDate string
  461. //开始日期
  462. if req.StartDate != "" && req.EndDate != "" {
  463. startDate = fmt.Sprint(req.StartDate, " 00:00:00")
  464. endDate = fmt.Sprint(req.EndDate, " 23:59:59")
  465. }
  466. startDateTime, _ := time.Parse(utils.FormatDateTime, startDate)
  467. endDateTime, _ := time.Parse(utils.FormatDateTime, endDate)
  468. cond := ` 1 = 1 `
  469. historyCond := ` 1=1 `
  470. pars := make([]interface{}, 0)
  471. historyPars := make([]interface{}, 0)
  472. if req.CompanyType == 1 {
  473. cond += ` AND b.contract_type = 1 `
  474. historyCond += ` AND new_company = 1 `
  475. } else if req.CompanyType == 2 {
  476. cond += ` AND b.contract_type IN (2,3,4) `
  477. historyCond += ` AND new_company = 0 `
  478. }
  479. if req.GroupId > 0 {
  480. // 筛选组别时, 查询当前组别的下级组(因为admin表存的group_id, 有三级的存的是子ID, 只有二级的存的才是父ID =_=!)
  481. groupCond := `parent_id = ?`
  482. groupPars := make([]interface{}, 0)
  483. groupPars = append(groupPars, req.GroupId)
  484. groupOB := new(crm.SysGroup)
  485. groupList, e := groupOB.List(groupCond, groupPars)
  486. if e != nil {
  487. resp.FailMsg("获取失败", "获取组别下级组列表失败, Err: "+e.Error(), c)
  488. return
  489. }
  490. groupIds := make([]int, 0)
  491. groupIds = append(groupIds, req.GroupId)
  492. for i := range groupList {
  493. groupIds = append(groupIds, groupList[i].GroupId)
  494. }
  495. cond += ` AND (c.seller_group_id IN (?) OR d.seller_group_id IN (?))`
  496. pars = append(pars, groupIds, groupIds)
  497. historyCond += ` AND group_id IN (?) `
  498. historyPars = append(historyPars, groupIds)
  499. }
  500. if req.CompanyType == 1 {
  501. cond += ` AND b.contract_type = 1 `
  502. historyCond += ` AND new_company = 1 `
  503. } else if req.CompanyType == 2 {
  504. cond += ` AND b.contract_type IN (2,3,4) `
  505. historyCond += ` AND new_company = 0 `
  506. }
  507. //if req.SellerIds != "" {
  508. // sellerIds := strings.Split(req.SellerIds, ",")
  509. // cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  510. // pars = append(pars, sellerIds, sellerIds)
  511. // prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  512. // prevPars = append(prevPars, sellerIds, sellerIds)
  513. // histrtyCond += ` AND seller_id in ? `
  514. // prevHistoryCond += ` AND seller_id in ? `
  515. // historyPars = append(historyPars, sellerIds)
  516. // prevHistoryPars = append(prevHistoryPars, sellerIds)
  517. //}
  518. st := startDate
  519. ed := endDate
  520. groupInvoiceMap := make(map[int]float64)
  521. sellerInvoiceMap := make(map[int]float64)
  522. sellerGroupMap := make(map[int]int)
  523. //校验日期,分段查询
  524. if req.StartDate == "" && req.EndDate == "" {
  525. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  526. if e != nil {
  527. return
  528. }
  529. // 开票到款金额合计(换算后)
  530. var amountTotal float64
  531. if len(summaryIds) > 0 {
  532. amountCond := `a.id IN ? `
  533. amountPars := make([]interface{}, 0)
  534. amountPars = append(amountPars, summaryIds)
  535. //if req.SellerIds != "" {
  536. // sellerIds := strings.Split(req.SellerIds, ",")
  537. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  538. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  539. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  540. //} else {
  541. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  542. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  543. // amountPars = append(amountPars, st, ed, st, ed)
  544. //}
  545. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  546. if e != nil {
  547. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  548. return
  549. }
  550. //dataList = append(dataList, results...)
  551. var amountSum float64
  552. for _, result := range results {
  553. amountSum += result.Amount
  554. fmt.Println("result.Amount:", result.Amount)
  555. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  556. groupInvoiceMap[result.SellerGroupId] += result.Amount
  557. } else {
  558. groupInvoiceMap[result.SellerGroupId] = result.Amount
  559. }
  560. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  561. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  562. } else {
  563. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  564. }
  565. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  566. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  567. }
  568. }
  569. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  570. accumulate += amountTotal
  571. results, e = fms.GetIncomeHistory(historyCond, historyPars)
  572. if e != nil {
  573. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  574. return
  575. }
  576. for _, result := range results {
  577. amountSum += result.Amount
  578. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  579. groupInvoiceMap[result.SellerGroupId] += result.Amount
  580. } else {
  581. groupInvoiceMap[result.SellerGroupId] = result.Amount
  582. }
  583. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  584. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  585. } else {
  586. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  587. }
  588. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  589. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  590. }
  591. }
  592. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  593. accumulate += amountTotal
  594. }
  595. } else if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  596. //全部走新查询
  597. //fmt.Println("新查询")
  598. if st != "" && ed != "" {
  599. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  600. pars = append(pars, st, ed, st, ed)
  601. }
  602. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  603. if e != nil {
  604. return
  605. }
  606. // 开票到款金额合计(换算后)
  607. var amountTotal float64
  608. if len(summaryIds) > 0 {
  609. amountCond := `a.id IN ? `
  610. amountPars := make([]interface{}, 0)
  611. amountPars = append(amountPars, summaryIds)
  612. //if req.SellerIds != "" {
  613. // sellerIds := strings.Split(req.SellerIds, ",")
  614. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  615. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  616. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  617. //} else {
  618. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  619. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  620. // amountPars = append(amountPars, st, ed, st, ed)
  621. //}
  622. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  623. if e != nil {
  624. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  625. return
  626. }
  627. //dataList = append(dataList, results...)
  628. var amountSum float64
  629. for _, result := range results {
  630. amountSum += result.Amount
  631. fmt.Println("result.Amount:", result.Amount)
  632. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  633. groupInvoiceMap[result.SellerGroupId] += result.Amount
  634. } else {
  635. groupInvoiceMap[result.SellerGroupId] = result.Amount
  636. }
  637. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  638. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  639. } else {
  640. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  641. }
  642. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  643. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  644. }
  645. }
  646. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  647. accumulate += amountTotal
  648. }
  649. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  650. //全部走旧查询
  651. //fmt.Println("旧查询")
  652. //fmt.Println("st:",st)
  653. //fmt.Println("ed:",ed)
  654. if st != "" && ed != "" {
  655. historyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  656. historyPars = append(historyPars, st, ed)
  657. }
  658. //fmt.Println("st:",st)
  659. //fmt.Println("ed:",ed)
  660. // 开票到款金额合计(换算后)
  661. var amountTotal float64
  662. results, e := fms.GetIncomeHistory(historyCond, historyPars)
  663. if e != nil {
  664. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  665. return
  666. }
  667. var amountSum float64
  668. //dataList = append(dataList, results...)
  669. for _, result := range results {
  670. amountSum += result.Amount
  671. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  672. groupInvoiceMap[result.SellerGroupId] += result.Amount
  673. } else {
  674. groupInvoiceMap[result.SellerGroupId] = result.Amount
  675. }
  676. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  677. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  678. } else {
  679. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  680. }
  681. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  682. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  683. }
  684. }
  685. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  686. accumulate += amountTotal
  687. //fmt.Println("partAccumulate:",partAccumulate)
  688. } else {
  689. if st != "" && ed != "" {
  690. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  691. pars = append(pars, st, ed, st, ed)
  692. }
  693. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  694. if e != nil {
  695. return
  696. }
  697. // 开票到款金额合计(换算后)
  698. var amountTotal float64
  699. if len(summaryIds) > 0 {
  700. amountCond := `a.id IN ? `
  701. amountPars := make([]interface{}, 0)
  702. amountPars = append(amountPars, summaryIds)
  703. //if req.SellerIds != "" {
  704. // sellerIds := strings.Split(req.SellerIds, ",")
  705. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  706. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  707. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  708. //} else {
  709. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  710. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  711. // amountPars = append(amountPars, st, ed, st, ed)
  712. //}
  713. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  714. if e != nil {
  715. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  716. return
  717. }
  718. //dataList = append(dataList, results...)
  719. var amountSum float64
  720. for _, result := range results {
  721. amountSum += result.Amount
  722. fmt.Println("result.Amount:", result.Amount)
  723. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  724. groupInvoiceMap[result.SellerGroupId] += result.Amount
  725. } else {
  726. groupInvoiceMap[result.SellerGroupId] = result.Amount
  727. }
  728. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  729. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  730. } else {
  731. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  732. }
  733. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  734. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  735. }
  736. }
  737. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  738. accumulate += amountTotal
  739. if st != "" && ed != "" {
  740. historyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  741. historyPars = append(historyPars, st, ed)
  742. }
  743. results, e = fms.GetIncomeHistory(historyCond, historyPars)
  744. if e != nil {
  745. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  746. return
  747. }
  748. for _, result := range results {
  749. amountSum += result.Amount
  750. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  751. groupInvoiceMap[result.SellerGroupId] += result.Amount
  752. } else {
  753. groupInvoiceMap[result.SellerGroupId] = result.Amount
  754. }
  755. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  756. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  757. } else {
  758. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  759. }
  760. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  761. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  762. }
  763. }
  764. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  765. accumulate += amountTotal
  766. }
  767. }
  768. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  769. if req.SellerType == 1 {
  770. departmentId = crm.SellerDepartmentId
  771. } else if req.SellerType == 2 {
  772. departmentId = crm.RaiSellerDepartmentId
  773. } else if req.SellerType == 0 {
  774. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  775. return
  776. } else {
  777. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  778. return
  779. }
  780. adminOB := new(crm.Admin)
  781. adminPars := make([]interface{}, 0)
  782. adminCond := ` department_id = %d `
  783. adminCond = fmt.Sprintf(adminCond, departmentId)
  784. adminList, e := adminOB.List(adminCond, adminPars)
  785. if e != nil {
  786. resp.FailMsg("获取失败", "获取销售列表失败, Err: "+e.Error(), c)
  787. return
  788. }
  789. // 获取销售分组信息
  790. sellerList, e := crmService.GetSellerDepartmentListWithEnable()
  791. if e != nil {
  792. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  793. return
  794. }
  795. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  796. for i := range sellerList {
  797. sellerMap[sellerList[i].SellerId] = sellerList[i]
  798. }
  799. list := make([]*fms.CensusSellerInvoiceItem, 0)
  800. for _, v := range adminList {
  801. if vv, ok := sellerInvoiceMap[v.AdminId]; ok {
  802. rate := vv / accumulate
  803. mulNum := decimal.NewFromFloat(100)
  804. newRate := decimal.NewFromFloat(rate)
  805. newRate = newRate.Mul(mulNum).Round(2)
  806. finalRate, _ := newRate.Float64()
  807. groupId := sellerGroupMap[v.AdminId]
  808. groupRate := vv / groupInvoiceMap[groupId]
  809. newGroupRate := decimal.NewFromFloat(groupRate)
  810. newGroupRate = newGroupRate.Mul(mulNum).Round(2)
  811. finalGroupRate, _ := newGroupRate.Float64()
  812. amuont, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", vv), 64)
  813. var sellerName, groupName string
  814. if seller, ok := sellerMap[v.AdminId]; ok {
  815. sellerName = seller.SellerName
  816. groupName = seller.GroupName
  817. }
  818. list = append(list, &fms.CensusSellerInvoiceItem{
  819. SellerId: v.AdminId,
  820. SellerName: sellerName,
  821. GroupId: groupId,
  822. GroupName: groupName,
  823. InvoiceAmount: amuont,
  824. GroupRate: finalGroupRate,
  825. SellerRate: finalRate,
  826. })
  827. }
  828. }
  829. var respList fms.CensusSellerList
  830. respList = list
  831. sort.Sort(respList)
  832. // 是否导出
  833. if req.IsExport == 1 {
  834. ExportInvoiceList(c, respList, req)
  835. return
  836. }
  837. page.SetTotal(int64(len(list)))
  838. baseData := new(base.BaseData)
  839. baseData.SetPage(page)
  840. if req.Current*req.PageSize > int64(len(list)) {
  841. baseData.SetList(respList[(req.Current-1)*req.PageSize:len(list)])
  842. } else {
  843. baseData.SetList(respList[(req.Current-1)*req.PageSize:req.Current*req.PageSize])
  844. }
  845. resp.OkData("获取成功", baseData, c)
  846. }
  847. // ExportInvoiceList 导出销售开票统计列表
  848. func ExportInvoiceList(c *gin.Context, list []*fms.CensusSellerInvoiceItem, req fms.CensusSellerInvoiceListReq) {
  849. // 生成Excel文件
  850. xlsxFile := xlsx.NewFile()
  851. style := xlsx.NewStyle()
  852. alignment := xlsx.Alignment{
  853. Horizontal: "center",
  854. Vertical: "center",
  855. WrapText: true,
  856. }
  857. style.Alignment = alignment
  858. style.ApplyAlignment = true
  859. sheetName := "销售开票统计"
  860. sheet, err := xlsxFile.AddSheet(sheetName)
  861. if err != nil {
  862. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  863. return
  864. }
  865. // 存在筛选则前两行显示时间信息
  866. if req.StartDate != "" && req.EndDate != "" {
  867. timeData := fmt.Sprintf("时间:%s至%s", req.StartDate, req.EndDate)
  868. rowA := sheet.AddRow()
  869. cellAA := rowA.AddCell()
  870. cellAA.SetString("销售统计表")
  871. cellAA.SetStyle(style)
  872. rowB := sheet.AddRow()
  873. rowB.AddCell().SetString(timeData)
  874. // 第三行空出
  875. sheet.AddRow()
  876. }
  877. // 数据表头
  878. rowTitle := []string{"排名", "销售员", "销售组别", "收入金额(元)", "小组占比", "全员占比"}
  879. titleRow := sheet.AddRow()
  880. for i := range rowTitle {
  881. v := titleRow.AddCell()
  882. v.SetString(rowTitle[i])
  883. v.SetStyle(style)
  884. }
  885. // 填充数据
  886. for k, v := range list {
  887. dataRow := sheet.AddRow()
  888. dataRow.AddCell().SetString(fmt.Sprint(k + 1)) // 排名
  889. dataRow.AddCell().SetString(v.SellerName) // 销售员
  890. dataRow.AddCell().SetString(v.GroupName) // 销售组别
  891. dataRow.AddCell().SetString(fmt.Sprint(v.InvoiceAmount)) // 开票金额
  892. dataRow.AddCell().SetString(fmt.Sprint(v.GroupRate, "%")) // 小组占比
  893. dataRow.AddCell().SetString(fmt.Sprint(v.SellerRate, "%")) // 全员占比
  894. }
  895. // 输出文件
  896. var buffer bytes.Buffer
  897. _ = xlsxFile.Write(&buffer)
  898. content := bytes.NewReader(buffer.Bytes())
  899. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  900. fileName := sheetName + randStr + ".xlsx"
  901. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  902. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  903. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  904. }
  905. // GroupInvoiceList
  906. // @Title 销售组开票统计列表
  907. // @Description 销售组开票统计列表
  908. // @Param StartDate query string false "开始日期"
  909. // @Param EndDate query string false "结束日期"
  910. // @Param SortField query int false "排序字段: 1-开票金额; 2-组别占比"
  911. // @Param SortType query int false "排序方式: 1-正序; 2-倒序"
  912. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  913. // @Success 200 {object} fms.CensusSellerGroupInvoiceItem
  914. // @router /census/seller/group_invoice_list [get]
  915. func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
  916. var req fms.CensusSellerGroupInvoiceListReq
  917. if e := c.BindQuery(&req); e != nil {
  918. err, ok := e.(validator.ValidationErrors)
  919. if !ok {
  920. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  921. return
  922. }
  923. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  924. return
  925. }
  926. //if req.SellerIds != "" {
  927. // sellerIds := strings.Split(req.SellerIds, ",")
  928. // cond += ` AND (a.seller_id in ? ) `
  929. // historyCond += ` AND (seller_id in ? ) `
  930. // pars = append(pars, sellerIds)
  931. // historyPars = append(historyPars, sellerIds)
  932. //}
  933. var totalMoneySlice []float64
  934. historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  935. // 累计值
  936. var accumulate float64
  937. //dataList := make([]*fms.IncomeSummaryItem, 0)
  938. //historydataList := make([]*fms.IncomeSummaryItem, 0)
  939. //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  940. var startDate, endDate string
  941. //开始日期
  942. if req.StartDate != "" && req.EndDate != "" {
  943. startDate = fmt.Sprint(req.StartDate, " 00:00:00")
  944. endDate = fmt.Sprint(req.EndDate, " 23:59:59")
  945. }
  946. startDateTime, _ := time.Parse(utils.FormatDate, req.StartDate)
  947. endDateTime, _ := time.Parse(utils.FormatDate, req.EndDate)
  948. cond := `1 = 1`
  949. histortyCond := `1 = 1`
  950. pars := make([]interface{}, 0)
  951. historyPars := make([]interface{}, 0)
  952. if req.CompanyType == 1 {
  953. cond += ` AND b.contract_type = 1 `
  954. histortyCond += ` AND new_company = 1 `
  955. } else if req.CompanyType == 2 {
  956. cond += ` AND b.contract_type IN (2,3,4) `
  957. histortyCond += ` AND new_company = 0 `
  958. }
  959. //if req.SellerIds != "" {
  960. // sellerIds := strings.Split(req.SellerIds, ",")
  961. // cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  962. // pars = append(pars, sellerIds, sellerIds)
  963. // prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  964. // prevPars = append(prevPars, sellerIds, sellerIds)
  965. // histrtyCond += ` AND seller_id in ? `
  966. // prevHistoryCond += ` AND seller_id in ? `
  967. // historyPars = append(historyPars, sellerIds)
  968. // prevHistoryPars = append(prevHistoryPars, sellerIds)
  969. //}
  970. st := fmt.Sprint(startDate, " 00:00:00")
  971. ed := fmt.Sprint(endDate, " 23:59:59")
  972. groupInvoiceMap := make(map[int]float64)
  973. //校验日期,分段查询
  974. if req.StartDate == "" && req.EndDate == "" {
  975. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  976. if e != nil {
  977. return
  978. }
  979. // 开票到款金额合计(换算后)
  980. var amountTotal float64
  981. if len(summaryIds) > 0 {
  982. amountCond := `a.id IN ? `
  983. amountPars := make([]interface{}, 0)
  984. amountPars = append(amountPars, summaryIds)
  985. //if req.SellerIds != "" {
  986. // sellerIds := strings.Split(req.SellerIds, ",")
  987. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  988. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  989. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  990. //} else {
  991. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  992. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  993. // amountPars = append(amountPars, st, ed, st, ed)
  994. //}
  995. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  996. if e != nil {
  997. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  998. return
  999. }
  1000. //dataList = append(dataList, results...)
  1001. var amountSum float64
  1002. for _, result := range results {
  1003. amountSum += result.Amount
  1004. fmt.Println("result.Amount:", result.Amount)
  1005. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1006. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1007. } else {
  1008. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1009. }
  1010. }
  1011. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1012. accumulate += amountTotal
  1013. //var amountTotal float64
  1014. results, e = fms.GetIncomeHistory(histortyCond, historyPars)
  1015. if e != nil {
  1016. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1017. return
  1018. }
  1019. //dataList = append(dataList, results...)
  1020. for _, result := range results {
  1021. amountSum += result.Amount
  1022. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1023. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1024. } else {
  1025. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1026. }
  1027. }
  1028. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1029. accumulate += amountTotal
  1030. }
  1031. } else if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  1032. //全部走新查询
  1033. //fmt.Println("新查询")
  1034. if st != "" && ed != "" {
  1035. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1036. pars = append(pars, st, ed, st, ed)
  1037. }
  1038. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1039. if e != nil {
  1040. return
  1041. }
  1042. // 开票到款金额合计(换算后)
  1043. var amountTotal float64
  1044. if len(summaryIds) > 0 {
  1045. amountCond := `a.id IN ? `
  1046. amountPars := make([]interface{}, 0)
  1047. amountPars = append(amountPars, summaryIds)
  1048. //if req.SellerIds != "" {
  1049. // sellerIds := strings.Split(req.SellerIds, ",")
  1050. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1051. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  1052. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  1053. //} else {
  1054. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1055. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  1056. // amountPars = append(amountPars, st, ed, st, ed)
  1057. //}
  1058. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1059. if e != nil {
  1060. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1061. return
  1062. }
  1063. //dataList = append(dataList, results...)
  1064. var amountSum float64
  1065. for _, result := range results {
  1066. amountSum += result.Amount
  1067. fmt.Println("result.Amount:", result.Amount)
  1068. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1069. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1070. } else {
  1071. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1072. }
  1073. }
  1074. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1075. accumulate += amountTotal
  1076. }
  1077. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  1078. //全部走旧查询
  1079. //fmt.Println("旧查询")
  1080. //fmt.Println("st:",st)
  1081. //fmt.Println("ed:",ed)
  1082. if st != "" && ed != "" {
  1083. histortyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1084. historyPars = append(historyPars, st, ed)
  1085. }
  1086. //fmt.Println("st:",st)
  1087. //fmt.Println("ed:",ed)
  1088. // 开票到款金额合计(换算后)
  1089. var amountTotal float64
  1090. results, e := fms.GetIncomeHistory(histortyCond, historyPars)
  1091. if e != nil {
  1092. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1093. return
  1094. }
  1095. var amountSum float64
  1096. //dataList = append(dataList, results...)
  1097. for _, result := range results {
  1098. amountSum += result.Amount
  1099. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1100. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1101. } else {
  1102. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1103. }
  1104. }
  1105. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1106. accumulate += amountTotal
  1107. //fmt.Println("partAccumulate:",partAccumulate)
  1108. } else {
  1109. // 有时间,分段查询
  1110. // 新查询
  1111. if st != "" && ed != "" {
  1112. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1113. pars = append(pars, st, ed, st, ed)
  1114. }
  1115. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1116. if e != nil {
  1117. return
  1118. }
  1119. // 开票到款金额合计(换算后)
  1120. var amountTotal float64
  1121. if len(summaryIds) > 0 {
  1122. amountCond := `a.id IN ? `
  1123. amountPars := make([]interface{}, 0)
  1124. amountPars = append(amountPars, summaryIds)
  1125. //if req.SellerIds != "" {
  1126. // sellerIds := strings.Split(req.SellerIds, ",")
  1127. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1128. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  1129. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  1130. //} else {
  1131. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1132. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  1133. // amountPars = append(amountPars, st, ed, st, ed)
  1134. //}
  1135. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1136. if e != nil {
  1137. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1138. return
  1139. }
  1140. //dataList = append(dataList, results...)
  1141. var amountSum float64
  1142. for _, result := range results {
  1143. amountSum += result.Amount
  1144. fmt.Println("result.Amount:", result.Amount)
  1145. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1146. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1147. } else {
  1148. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1149. }
  1150. }
  1151. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1152. accumulate += amountTotal
  1153. //var amountTotal float64
  1154. // 旧查询
  1155. if st != "" && ed != "" {
  1156. histortyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1157. historyPars = append(historyPars, st, ed)
  1158. }
  1159. results, e = fms.GetIncomeHistory(histortyCond, historyPars)
  1160. if e != nil {
  1161. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1162. return
  1163. }
  1164. //dataList = append(dataList, results...)
  1165. for _, result := range results {
  1166. amountSum += result.Amount
  1167. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1168. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1169. } else {
  1170. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1171. }
  1172. }
  1173. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1174. accumulate += amountTotal
  1175. }
  1176. }
  1177. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  1178. var departmentId int
  1179. if req.SellerType == 1 {
  1180. departmentId = crm.SellerDepartmentId
  1181. } else if req.SellerType == 2 {
  1182. departmentId = crm.RaiSellerDepartmentId
  1183. } else if req.SellerType == 0 {
  1184. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  1185. return
  1186. } else {
  1187. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  1188. return
  1189. }
  1190. groupCond := ` department_id = %d AND parent_id = 0 `
  1191. groupCond = fmt.Sprintf(groupCond, departmentId)
  1192. groupPars := make([]interface{}, 0)
  1193. groupOB := new(crm.SysGroup)
  1194. groupList, e := groupOB.List(groupCond, groupPars)
  1195. if e != nil {
  1196. resp.FailMsg("查询错误", fmt.Sprintf("获取组别列表失败, Err: %s", e.Error()), c)
  1197. return
  1198. }
  1199. //total := len(groupList)
  1200. groupMap := make(map[int]*crm.SysGroup)
  1201. for i := range groupList {
  1202. groupMap[groupList[i].GroupId] = groupList[i]
  1203. }
  1204. list := make([]*fms.CensusSellerGroupInvoiceItem, 0)
  1205. for _, group := range groupList {
  1206. if v, ok := groupInvoiceMap[group.GroupId]; ok {
  1207. rate := v / accumulate
  1208. mulNum := decimal.NewFromFloat(100)
  1209. newRate := decimal.NewFromFloat(rate)
  1210. newRate = newRate.Mul(mulNum).Round(2)
  1211. finalRate, _ := newRate.Float64()
  1212. amuont, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v), 64)
  1213. groupName := ""
  1214. if groupItem, ok := groupMap[group.GroupId]; ok {
  1215. groupName = groupItem.GroupName
  1216. }
  1217. list = append(list, &fms.CensusSellerGroupInvoiceItem{
  1218. GroupId: group.GroupId,
  1219. GroupName: groupName,
  1220. InvoiceAmount: amuont,
  1221. GroupRate: finalRate,
  1222. })
  1223. }
  1224. }
  1225. var respList fms.CensusSellerGroupList
  1226. respList = list
  1227. sort.Sort(respList)
  1228. // 是否导出
  1229. if req.IsExport == 1 {
  1230. ExportGroupInvoiceList(c, respList, req)
  1231. return
  1232. }
  1233. resp.OkData("获取成功", respList, c)
  1234. }