income_list.go 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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/tealeg/xlsx"
  8. "hongze/fms_api/controller/resp"
  9. "hongze/fms_api/global"
  10. "hongze/fms_api/models"
  11. "hongze/fms_api/models/base"
  12. "hongze/fms_api/models/fms"
  13. "hongze/fms_api/services/alarm_msg"
  14. "hongze/fms_api/utils"
  15. "net/http"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // IncomeList
  21. // @Title 业务收入统计表
  22. // @Description 获取业务收入统计表接口
  23. // @Param ListParam query int false "套餐类型: 0-月度; 1-季度; 2-半年度;3-年度;4-月度累计"
  24. // @Success 200 {object} fms.CensusIncomeChartResp
  25. // @router /census/income/chart_list [get]
  26. func (this *InvoicePaymentController) IncomeChartList(c *gin.Context) {
  27. var req fms.IncomeChartListReq
  28. if e := c.BindQuery(&req); e != nil {
  29. err, ok := e.(validator.ValidationErrors)
  30. if !ok {
  31. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  32. return
  33. }
  34. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  35. return
  36. }
  37. //收入统计
  38. var incomeList models.CensusIncomeChartResp
  39. ch := make(chan models.CensusIncomeChartResp, 1)
  40. if req.IncomeType == 0 {
  41. go getCensusIncomeChartList(ch, req)
  42. } else {
  43. go getPaymentIncomeChartList(ch, req)
  44. }
  45. for v := range ch {
  46. incomeList = v
  47. close(ch)
  48. }
  49. resp.OkData("获取成功", incomeList, c)
  50. }
  51. func getCensusIncomeChartList(ch chan models.CensusIncomeChartResp, req fms.IncomeChartListReq) (incomeChart models.CensusIncomeChartResp, err error) {
  52. defer func() {
  53. if err != nil {
  54. global.LOG.Error(err)
  55. if err != utils.ErrNoRow {
  56. go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
  57. }
  58. }
  59. ch <- incomeChart
  60. }()
  61. //todayStr := utils.GetToday("20060102")
  62. //key := "admin:home:fmsIncomeList:" + todayStr
  63. //
  64. //redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
  65. //if redisErr != nil {
  66. //获取最新的开票到款日期
  67. cond := ``
  68. historyCond := ``
  69. pars := make([]interface{}, 0)
  70. historyPars := make([]interface{}, 0)
  71. if req.SellerIds != "" {
  72. sellerIds := strings.Split(req.SellerIds, ",")
  73. cond += ` AND (a.seller_id in ? ) `
  74. historyCond += ` AND (seller_id in ? ) `
  75. pars = append(pars, sellerIds)
  76. historyPars = append(historyPars, sellerIds)
  77. }
  78. if req.CompanyType == 1 {
  79. cond += ` AND b.contract_type = 1 `
  80. historyCond += ` AND new_company = 1 `
  81. } else if req.CompanyType == 2 {
  82. cond += ` AND b.contract_type IN (2,3,4) `
  83. historyCond += ` AND new_company = 0 `
  84. }
  85. var latestTime time.Time
  86. invoiceItem, err := fms.GetLatestIncome(cond, pars)
  87. if err != nil && err != utils.ErrNoRow {
  88. err = fmt.Errorf("获取最新的开票或到款日期, Err: %s", err.Error())
  89. return
  90. }
  91. latestTime = invoiceItem.InvoiceDate
  92. if err == utils.ErrNoRow {
  93. historyItem, e := fms.GetLatestHistoryIncome(historyCond, historyPars)
  94. if e != nil && e != utils.ErrNoRow {
  95. err = fmt.Errorf("获取最新的历史开票或到款日期, Err: %s", e.Error())
  96. return
  97. }
  98. latestTime = historyItem.InvoiceDate
  99. }
  100. if latestTime.IsZero() {
  101. latestTime = time.Now()
  102. }
  103. if req.CompanyType == 3 {
  104. cond += ` AND b.contract_type IN (2,3,4) `
  105. historyCond += ` AND new_company = 0 `
  106. }
  107. latestTime = latestTime.AddDate(0, 0, -latestTime.Day()+1)
  108. addMonth := 0
  109. if req.ListParam == "1" {
  110. addMonth = int(3 - latestTime.Month()%3)
  111. }
  112. if req.ListParam == "2" {
  113. addMonth = int(6 - latestTime.Month()%6)
  114. }
  115. if req.ListParam == "3" {
  116. addMonth = int(12 - latestTime.Month()%12)
  117. }
  118. latestTime = latestTime.AddDate(0, addMonth, 0)
  119. var dateSlice []string
  120. var totalMoneySlice, prevTotalMoneySlice []float64
  121. var yoySlice []string
  122. var yearNum, monthNum int
  123. var reqStartDate, reqEndDate time.Time
  124. historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  125. // 套餐筛选
  126. if req.ServiceTypes != "" {
  127. serviceTypes := strings.Split(req.ServiceTypes, ",")
  128. registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  129. if e != nil {
  130. err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
  131. return
  132. }
  133. if len(registerIds) > 0 {
  134. cond += ` AND b.contract_register_id IN ?`
  135. pars = append(pars, registerIds)
  136. } else {
  137. cond += ` AND 1 = 2`
  138. }
  139. // 勾选套餐筛选时忽略导入的历史数据
  140. if req.StartDate == "" {
  141. req.StartDate = "2023-04"
  142. req.EndDate = "3023-04"
  143. } else {
  144. startDateTime, _ := time.Parse(utils.FormatMonth, req.StartDate)
  145. if startDateTime.Before(historyTime) {
  146. req.StartDate = "2023-04"
  147. }
  148. }
  149. }
  150. if req.StartDate != "" && req.EndDate != "" {
  151. st := fmt.Sprint(req.StartDate, "-01 00:00:00")
  152. ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
  153. reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
  154. reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
  155. if reqEndDate.After(latestTime) {
  156. yearNum = latestTime.Year() - reqStartDate.Year()
  157. monthNum = int(latestTime.Month() - reqStartDate.Month())
  158. } else {
  159. yearNum = reqEndDate.Year() - reqStartDate.Year()
  160. monthNum = int(reqEndDate.Month() - reqStartDate.Month())
  161. }
  162. } else {
  163. yearNum = latestTime.Year() - 2020
  164. monthNum = int(latestTime.Month() - 1)
  165. }
  166. //if yearNum < 0 {
  167. // yearNum = -yearNum
  168. //}
  169. //if monthNum < 0 {
  170. // monthNum = -monthNum
  171. //}
  172. numMonth := yearNum*12 + monthNum //共存在多少个月
  173. if numMonth < 0 {
  174. numMonth = -numMonth
  175. }
  176. //if req.ListParam == "1" {
  177. // numMonth = numMonth / 3
  178. //} else if req.ListParam == "2" {
  179. // numMonth = numMonth / 6
  180. //} else if req.ListParam == "3" {
  181. // numMonth = numMonth / 12
  182. //}
  183. // 累计值
  184. var accumulate float64
  185. var partAccumulate float64
  186. var historyAccumulate float64
  187. var partHistoryAccumulate float64
  188. //dataList := make([]*fms.IncomeSummaryItem, 0)
  189. //historydataList := make([]*fms.IncomeSummaryItem, 0)
  190. fmt.Println("numMonth:", numMonth)
  191. fmt.Println("InvoiceDate:", latestTime)
  192. var j int
  193. for i := 0; i <= numMonth; i++ {
  194. //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  195. var endDateTime time.Time
  196. var prevEndDateTime time.Time
  197. var prevStartDate, prevEndDate string
  198. var startDate, endDate string
  199. //开始日期
  200. if req.StartDate != "" && req.EndDate != "" {
  201. startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
  202. prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
  203. } else {
  204. startDate = latestTime.AddDate(0, i-numMonth, 0).Format("2006-01")
  205. prevStartDate = latestTime.AddDate(-1, i-numMonth, 0).Format("2006-01")
  206. }
  207. startDate = fmt.Sprint(startDate, "-01")
  208. prevStartDate = fmt.Sprint(prevStartDate, "-01")
  209. startDateTime, _ := time.Parse(utils.FormatDate, startDate)
  210. prevStartDateTime, _ := time.Parse(utils.FormatDate, prevStartDate)
  211. //结束日期
  212. if req.StartDate != "" && req.EndDate != "" {
  213. endDateTime = reqStartDate.AddDate(0, i+1, -1)
  214. prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
  215. if reqEndDate.After(latestTime) {
  216. endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
  217. prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
  218. }
  219. } else {
  220. endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
  221. prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
  222. }
  223. endDate = endDateTime.Format(utils.FormatDate)
  224. prevEndDate = prevEndDateTime.Format(utils.FormatDate)
  225. cond := `1 = 1`
  226. histrtyCond := `1 = 1`
  227. pars := make([]interface{}, 0)
  228. historyPars := make([]interface{}, 0)
  229. prevCond := `1 = 1`
  230. prevHistoryCond := `1 = 1`
  231. prevPars := make([]interface{}, 0)
  232. prevHistoryPars := make([]interface{}, 0)
  233. if req.CompanyType == 1 {
  234. cond += ` AND b.contract_type = 1 `
  235. prevCond += ` AND b.contract_type = 1 `
  236. histrtyCond += ` AND new_company = 1 `
  237. prevHistoryCond += ` AND new_company = 1 `
  238. } else if req.CompanyType == 2 {
  239. cond += ` AND b.contract_type IN (2,3,4) `
  240. prevCond += ` AND b.contract_type IN (2,3,4) `
  241. histrtyCond += ` AND new_company = 0 `
  242. prevHistoryCond += ` AND new_company = 0 `
  243. } else if req.CompanyType == 3 {
  244. //未续约收入金额=去年同期总收入金额(新客户+老客户)-当期老客户收入金额
  245. cond += ` AND b.contract_type IN (2,3,4) `
  246. //prevCond += ` AND b.contract_type IN (2,3,4) `
  247. histrtyCond += ` AND new_company = 0 `
  248. //prevHistoryCond += ` AND new_company = 0 `
  249. }
  250. if req.SellerIds != "" {
  251. sellerIds := strings.Split(req.SellerIds, ",")
  252. cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  253. pars = append(pars, sellerIds, sellerIds)
  254. prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  255. prevPars = append(prevPars, sellerIds, sellerIds)
  256. histrtyCond += ` AND seller_id in ? `
  257. prevHistoryCond += ` AND seller_id in ? `
  258. historyPars = append(historyPars, sellerIds)
  259. prevHistoryPars = append(prevHistoryPars, sellerIds)
  260. }
  261. // 套餐筛选
  262. if req.ServiceTypes != "" {
  263. serviceTypes := strings.Split(req.ServiceTypes, ",")
  264. registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  265. if e != nil {
  266. err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
  267. return
  268. }
  269. if len(registerIds) > 0 {
  270. cond += ` AND a.register_id IN ?`
  271. prevCond += ` AND a.register_id IN ?`
  272. pars = append(pars, registerIds)
  273. prevPars = append(pars, registerIds)
  274. } else {
  275. cond += ` AND 1 = 2`
  276. prevCond += ` AND 1 = 2`
  277. }
  278. }
  279. //fmt.Println("i:",i)
  280. //fmt.Println("j:",j)
  281. {
  282. //本期
  283. st := fmt.Sprint(startDate, " 00:00:00")
  284. ed := fmt.Sprint(endDate, " 23:59:59")
  285. //校验日期,分段查询
  286. if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  287. //全部走新查询
  288. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  289. pars = append(pars, st, ed, st, ed)
  290. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  291. if e != nil {
  292. return
  293. }
  294. // 开票到款金额合计(换算后)
  295. var amountTotal float64
  296. if len(summaryIds) > 0 {
  297. amountCond := `a.id IN ? `
  298. amountPars := make([]interface{}, 0)
  299. amountPars = append(amountPars, summaryIds)
  300. if req.SellerIds != "" {
  301. sellerIds := strings.Split(req.SellerIds, ",")
  302. amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  303. amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  304. amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  305. } else {
  306. amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  307. amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  308. amountPars = append(amountPars, st, ed, st, ed)
  309. }
  310. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  311. if e != nil {
  312. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  313. return
  314. }
  315. //dataList = append(dataList, results...)
  316. var amountSum float64
  317. for _, result := range results {
  318. incomeChart.DataList = append(incomeChart.DataList, result)
  319. amountSum += result.Amount
  320. fmt.Println("result.Amount:", result.Amount)
  321. }
  322. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  323. accumulate += amountTotal
  324. partAccumulate += amountTotal
  325. }
  326. if i == j || i == numMonth {
  327. if req.ListParam == "4" {
  328. totalMoneySlice = append(totalMoneySlice, accumulate)
  329. if startDateTime.Month() == 12 {
  330. accumulate = 0
  331. }
  332. } else if req.ListParam == "0" {
  333. totalMoneySlice = append(totalMoneySlice, amountTotal)
  334. } else if i > 0 || i == numMonth {
  335. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  336. fmt.Println("partAccumulate:", partAccumulate)
  337. partAccumulate = 0.0
  338. }
  339. }
  340. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  341. //全部走旧查询
  342. //fmt.Println("旧查询")
  343. //fmt.Println("st:",st)
  344. //fmt.Println("ed:",ed)
  345. histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  346. historyPars = append(historyPars, st, ed)
  347. //fmt.Println("st:",st)
  348. //fmt.Println("ed:",ed)
  349. // 开票到款金额合计(换算后)
  350. var amountTotal float64
  351. results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
  352. if e != nil {
  353. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  354. return
  355. }
  356. var amountSum float64
  357. //dataList = append(dataList, results...)
  358. for _, result := range results {
  359. incomeChart.DataList = append(incomeChart.DataList, result)
  360. amountSum += result.Amount
  361. }
  362. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  363. accumulate += amountTotal
  364. partAccumulate += amountTotal
  365. if i == j || i == numMonth {
  366. if req.ListParam == "4" {
  367. totalMoneySlice = append(totalMoneySlice, accumulate)
  368. if startDateTime.Month() == 12 {
  369. accumulate = 0
  370. }
  371. } else if req.ListParam == "0" {
  372. totalMoneySlice = append(totalMoneySlice, amountTotal)
  373. } else if i > 0 || i == numMonth {
  374. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  375. partAccumulate = 0.0
  376. }
  377. }
  378. //fmt.Println("partAccumulate:",partAccumulate)
  379. }
  380. }
  381. { // 去年同期,用于计算同比值
  382. prevSt := fmt.Sprint(prevStartDate, " 00:00:00") // 格式化上一年同一天的开始时间
  383. prevEd := fmt.Sprint(prevEndDate, " 23:59:59") // 格式化上一年同一天的结束时间
  384. // 校验日期,分段查询
  385. if prevStartDateTime.After(historyTime) || prevStartDateTime.Equal(historyTime) {
  386. // 全部走新查询
  387. prevCond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))` // 查询条件加入新的时间范围
  388. prevPars = append(prevPars, prevSt, prevEd, prevSt, prevEd) // 添加时间参数到参数列表
  389. prevSummaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(prevCond, prevPars) // 获取摘要ID列表
  390. if e != nil {
  391. return
  392. }
  393. // 开票到款金额合计(换算后)
  394. var prevAmountTotal float64
  395. if len(prevSummaryIds) > 0 {
  396. amountCond := `a.id IN ? ` // 查询条件为ID在给定的摘要ID列表中
  397. amountPars := make([]interface{}, 0)
  398. amountPars = append(amountPars, prevSummaryIds) // 将摘要ID列表添加到参数列表
  399. if req.SellerIds != "" {
  400. sellerIds := strings.Split(req.SellerIds, ",")
  401. amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  402. amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) ` // 根据卖家ID和发票/支付状态筛选时间范围
  403. amountPars = append(amountPars, sellerIds, prevSt, prevEd, sellerIds, prevSt, prevEd) // 将卖家ID列表添加到参数列表
  404. } else {
  405. amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  406. amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))` // 根据发票/支付状态筛选时间范围
  407. amountPars = append(amountPars, prevSt, prevEd, prevSt, prevEd)
  408. }
  409. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars) // 获取合同摘要收入金额数据
  410. if e != nil {
  411. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  412. return
  413. }
  414. var amountSum float64
  415. //historydataList = append(historydataList, results...)
  416. for _, result := range results {
  417. amountSum += result.Amount // 累计收入金额
  418. }
  419. prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64) // 将累计金额转换为浮点数
  420. historyAccumulate += prevAmountTotal // 累计历史金额
  421. partHistoryAccumulate += prevAmountTotal // 累计部分历史金额
  422. }
  423. if i == j || i == numMonth {
  424. if req.ListParam == "4" {
  425. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate) // 将累计金额添加到总金额切片中
  426. if prevStartDateTime.Month() == 12 {
  427. historyAccumulate = 0 // 如果是去年的12月份,则将历史金额清零
  428. }
  429. } else if req.ListParam == "0" {
  430. prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal) // 将部分历史金额添加到总金额切片中
  431. } else if i > 0 || i == numMonth {
  432. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate) // 将部分历史金额添加到总金额切片中
  433. fmt.Println("partHistoryAccumulate:", partHistoryAccumulate)
  434. partHistoryAccumulate = 0.0 // 清零部分历史金额
  435. }
  436. }
  437. } else if prevEndDateTime.Before(historyTime) || prevEndDateTime.Equal(historyTime) {
  438. // 全部走旧查询
  439. fmt.Println("prevSt:", prevSt)
  440. fmt.Println("prevEd:", prevEd)
  441. prevHistoryCond += ` AND (invoice_time BETWEEN ? AND ?)` // 查询条件加入旧的时间范围
  442. prevHistoryPars = append(prevHistoryPars, prevSt, prevEd) // 添加时间参数到参数列表
  443. // 开票到款金额合计(换算后)
  444. var amountTotal float64
  445. results, e := fms.GetIncomeHistory(prevHistoryCond, prevHistoryPars) // 获取历史收入数据
  446. if e != nil {
  447. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  448. return
  449. }
  450. //historydataList = append(historydataList, results...)
  451. var amountSum float64
  452. for _, result := range results {
  453. amountSum += result.Amount // 累计收入金额
  454. }
  455. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64) // 将累计金额转换为浮点数
  456. historyAccumulate += amountTotal // 累计历史金额
  457. partHistoryAccumulate += amountTotal // 累计部分历史金额
  458. if i == j || i == numMonth {
  459. if req.ListParam == "4" {
  460. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate) // 将累计金额添加到总金额切片中
  461. if prevStartDateTime.Month() == 12 {
  462. historyAccumulate = 0 // 如果是去年的12月份,则将历史金额清零
  463. }
  464. } else if req.ListParam == "0" {
  465. prevTotalMoneySlice = append(prevTotalMoneySlice, amountTotal) // 将历史金额添加到总金额切片中
  466. } else if i > 0 || i == numMonth {
  467. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate) // 将部分历史金额添加到总金额切片中
  468. fmt.Println("partHistoryAccumulate:", partHistoryAccumulate)
  469. partHistoryAccumulate = 0.0 // 清零部分历史金额
  470. }
  471. }
  472. if req.ListParam == "1" && i == j {
  473. if i == 0 {
  474. dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01")) // 添加下一个月份的开始日期到日期切片中
  475. j = j + 2
  476. } else {
  477. dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01")) // 添加下一季度的开始日期到日期切片中
  478. j = j + 3
  479. }
  480. } else if req.ListParam == "2" && i == j {
  481. if i == 0 {
  482. dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01")) // 添加下五个月份的开始日期到日期切片中
  483. j = j + 5
  484. } else {
  485. dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01")) // 添加下半年的开始日期到日期切片中
  486. j = j + 6
  487. }
  488. } else if req.ListParam == "3" && i == j {
  489. if i == 0 {
  490. dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01")) // 添加下十一个月份的开始日期到日期切片中
  491. j = j + 11
  492. } else {
  493. dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01")) // 添加下一季度的开始日期到日期切片中
  494. j = j + 12
  495. }
  496. } else if i == j {
  497. dateSlice = append(dateSlice, startDateTime.Format("06/01")) // 添加当月开始日期到日期切片中
  498. j++
  499. }
  500. }
  501. if req.ListParam == "1" && i == j {
  502. if i == 0 {
  503. dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01")) // 添加下一个月份的开始日期到日期切片中
  504. j = j + 2
  505. } else {
  506. dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01")) // 添加下一季度的开始日期到日期切片中
  507. j = j + 3
  508. }
  509. } else if req.ListParam == "2" && i == j {
  510. if i == 0 {
  511. dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01")) // 添加下五个月份的开始日期到日期切片中
  512. j = j + 5
  513. } else {
  514. dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01")) // 添加下半年的开始日期到日期切片中
  515. j = j + 6
  516. }
  517. } else if req.ListParam == "3" && i == j {
  518. if i == 0 {
  519. dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
  520. j = j + 11
  521. } else {
  522. dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
  523. j = j + 12
  524. }
  525. } else if i == j {
  526. dateSlice = append(dateSlice, startDateTime.Format("06/01"))
  527. j++
  528. }
  529. }
  530. }
  531. fmt.Println("prevTotalMoneySlice:", len(prevTotalMoneySlice))
  532. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  533. if req.CompanyType != 3 {
  534. //计算同比值
  535. for i := range prevTotalMoneySlice {
  536. var yoy float64
  537. var yoyStr string
  538. //fmt.Println("1:", prevTotalMoneySlice[i])
  539. //fmt.Println("2:", totalMoneySlice[i])
  540. //fmt.Println("3:", totalMoneySlice[i]-prevTotalMoneySlice[i])
  541. totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalMoneySlice[i]), 64)
  542. if prevTotalMoneySlice[i] != 0 && totalMoneySlice[i] != 0 {
  543. yoy = (totalMoneySlice[i] - prevTotalMoneySlice[i]) / prevTotalMoneySlice[i]
  544. yoyStr = fmt.Sprintf("%.4f", yoy)
  545. if i == len(prevTotalMoneySlice)-1 && i > 0 && req.ListParam == "3" {
  546. fmt.Println("totalMoneySlice[i-1]:", totalMoneySlice[i-1])
  547. yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
  548. yoyStr = fmt.Sprintf("%.4f", yoy)
  549. }
  550. }
  551. yoySlice = append(yoySlice, yoyStr)
  552. }
  553. } else {
  554. // 当筛选条件是”月度“时,并且客户是”未续约“客户,展示柱形图和曲线图
  555. // 当筛选条件是”季度“、”半年度“、”年度“、”月度统计“时,并且客户是”未续约“时,仅展示柱形图,无曲线图
  556. // 未续约收入金额=去年同期总收入金额(新客户+老客户)-当期老客户收入金额
  557. // 移动平均值:若是10月份,计算8月,9月,10月,这三个月的收入金额平均值
  558. for i := range prevTotalMoneySlice {
  559. totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", prevTotalMoneySlice[i]-totalMoneySlice[i]), 64)
  560. var yoy float64
  561. var yoyStr string
  562. if i > 1 && req.ListParam == "0" {
  563. // 前俩月没有
  564. yoy = (totalMoneySlice[i] + totalMoneySlice[i-1] + totalMoneySlice[i-2]) / 3
  565. yoyStr = fmt.Sprintf("%.4f", yoy)
  566. yoySlice = append(yoySlice, yoyStr)
  567. } else {
  568. yoySlice = append(yoySlice, "")
  569. }
  570. }
  571. }
  572. incomeChart.Title = "开票到款统计图"
  573. incomeChart.Date = dateSlice[:len(totalMoneySlice)]
  574. incomeChart.TotalMoney = totalMoneySlice
  575. incomeChart.PrevTotalMoney = prevTotalMoneySlice
  576. incomeChart.Yoy = yoySlice
  577. //redisJsonData, err := json.Marshal(incomeChart)
  578. //if err == nil {
  579. // global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  580. //}
  581. //} else {
  582. // err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  583. // if err != nil {
  584. // fmt.Println("近两年的收入统计数据,json转换失败")
  585. // }
  586. //}
  587. return
  588. }
  589. func getPaymentIncomeChartList(ch chan models.CensusIncomeChartResp, req fms.IncomeChartListReq) (incomeChart models.CensusIncomeChartResp, err error) {
  590. defer func() {
  591. if err != nil {
  592. global.LOG.Error(err)
  593. if err != utils.ErrNoRow {
  594. go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
  595. }
  596. }
  597. ch <- incomeChart
  598. }()
  599. //todayStr := utils.GetToday("20060102")
  600. //key := "admin:home:fmsIncomeList:" + todayStr
  601. //
  602. //redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
  603. //if redisErr != nil {
  604. //获取最新的开票到款日期
  605. cond := ``
  606. historyCond := ``
  607. pars := make([]interface{}, 0)
  608. historyPars := make([]interface{}, 0)
  609. if req.SellerIds != "" {
  610. sellerIds := strings.Split(req.SellerIds, ",")
  611. cond += ` AND (a.seller_id in ? ) `
  612. historyCond += ` AND (seller_id in ? ) `
  613. pars = append(pars, sellerIds)
  614. historyPars = append(historyPars, sellerIds)
  615. }
  616. if req.CompanyType == 1 {
  617. cond += ` AND b.contract_type = 1 `
  618. historyCond += ` AND new_company = 1 `
  619. } else if req.CompanyType == 2 {
  620. cond += ` AND b.contract_type IN (2,3,4) `
  621. historyCond += ` AND new_company = 0 `
  622. }
  623. var latestTime time.Time
  624. invoiceItem, err := fms.GetLatestIncome(cond, pars)
  625. if err != nil && err != utils.ErrNoRow {
  626. err = fmt.Errorf("获取最新的开票或到款日期, Err: %s", err.Error())
  627. return
  628. }
  629. latestTime = invoiceItem.InvoiceDate
  630. if latestTime.IsZero() {
  631. latestTime = time.Now()
  632. }
  633. if req.CompanyType == 3 {
  634. cond += ` AND b.contract_type IN (2,3,4) `
  635. historyCond += ` AND new_company = 0 `
  636. }
  637. latestTime = latestTime.AddDate(0, 0, -latestTime.Day()+1)
  638. addMonth := 0
  639. if req.ListParam == "1" {
  640. addMonth = int(3 - latestTime.Month()%3)
  641. }
  642. if req.ListParam == "2" {
  643. addMonth = int(6 - latestTime.Month()%6)
  644. }
  645. if req.ListParam == "3" {
  646. addMonth = int(12 - latestTime.Month()%12)
  647. }
  648. latestTime = latestTime.AddDate(0, addMonth, 0)
  649. var dateSlice []string
  650. var totalMoneySlice, prevTotalMoneySlice []float64
  651. var yoySlice []string
  652. var yearNum, monthNum int
  653. var reqStartDate, reqEndDate time.Time
  654. // 套餐筛选
  655. if req.ServiceTypes != "" {
  656. serviceTypes := strings.Split(req.ServiceTypes, ",")
  657. registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  658. if e != nil {
  659. err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
  660. return
  661. }
  662. if len(registerIds) > 0 {
  663. cond += ` AND b.contract_register_id IN ?`
  664. pars = append(pars, registerIds)
  665. } else {
  666. cond += ` AND 1 = 2`
  667. }
  668. }
  669. if req.StartDate != "" && req.EndDate != "" {
  670. st := fmt.Sprint(req.StartDate, "-01 00:00:00")
  671. ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
  672. reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
  673. reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
  674. if reqEndDate.After(latestTime) {
  675. yearNum = latestTime.Year() - reqStartDate.Year()
  676. monthNum = int(latestTime.Month() - reqStartDate.Month())
  677. } else {
  678. yearNum = reqEndDate.Year() - reqStartDate.Year()
  679. monthNum = int(reqEndDate.Month() - reqStartDate.Month())
  680. }
  681. } else {
  682. yearNum = latestTime.Year() - 2020
  683. monthNum = int(latestTime.Month() - 1)
  684. }
  685. numMonth := yearNum*12 + monthNum //共存在多少个月
  686. if numMonth < 0 {
  687. numMonth = -numMonth
  688. }
  689. // 累计值
  690. var accumulate float64
  691. var partAccumulate float64
  692. var historyAccumulate float64
  693. var partHistoryAccumulate float64
  694. //dataList := make([]*fms.IncomeSummaryItem, 0)
  695. //historydataList := make([]*fms.IncomeSummaryItem, 0)
  696. fmt.Println("numMonth:", numMonth)
  697. fmt.Println("InvoiceDate:", latestTime)
  698. var j int
  699. for i := 0; i <= numMonth; i++ {
  700. //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  701. var endDateTime time.Time
  702. var prevEndDateTime time.Time
  703. var prevStartDate, prevEndDate string
  704. var startDate, endDate string
  705. //开始日期
  706. if req.StartDate != "" && req.EndDate != "" {
  707. startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
  708. prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
  709. } else {
  710. startDate = latestTime.AddDate(0, i-numMonth, 0).Format("2006-01")
  711. prevStartDate = latestTime.AddDate(-1, i-numMonth, 0).Format("2006-01")
  712. }
  713. startDate = fmt.Sprint(startDate, "-01")
  714. prevStartDate = fmt.Sprint(prevStartDate, "-01")
  715. startDateTime, _ := time.Parse(utils.FormatDate, startDate)
  716. prevStartDateTime, _ := time.Parse(utils.FormatDate, prevStartDate)
  717. //结束日期
  718. if req.StartDate != "" && req.EndDate != "" {
  719. endDateTime = reqStartDate.AddDate(0, i+1, -1)
  720. prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
  721. if reqEndDate.After(latestTime) {
  722. endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
  723. prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
  724. }
  725. } else {
  726. endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
  727. prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
  728. }
  729. endDate = endDateTime.Format(utils.FormatDate)
  730. prevEndDate = prevEndDateTime.Format(utils.FormatDate)
  731. cond := `1 = 1`
  732. histrtyCond := `1 = 1`
  733. pars := make([]interface{}, 0)
  734. historyPars := make([]interface{}, 0)
  735. prevCond := `1 = 1`
  736. prevHistoryCond := `1 = 1`
  737. prevPars := make([]interface{}, 0)
  738. prevHistoryPars := make([]interface{}, 0)
  739. if req.CompanyType == 1 {
  740. cond += ` AND b.contract_type = 1 `
  741. prevCond += ` AND b.contract_type = 1 `
  742. histrtyCond += ` AND new_company = 1 `
  743. prevHistoryCond += ` AND new_company = 1 `
  744. } else if req.CompanyType == 2 {
  745. cond += ` AND b.contract_type IN (2,3,4) `
  746. prevCond += ` AND b.contract_type IN (2,3,4) `
  747. histrtyCond += ` AND new_company = 0 `
  748. prevHistoryCond += ` AND new_company = 0 `
  749. } else if req.CompanyType == 3 {
  750. //未续约收入金额=去年同期总收入金额(新客户+老客户)-当期老客户收入金额
  751. cond += ` AND b.contract_type IN (2,3,4) `
  752. //prevCond += ` AND b.contract_type IN (2,3,4) `
  753. histrtyCond += ` AND new_company = 0 `
  754. //prevHistoryCond += ` AND new_company = 0 `
  755. }
  756. if req.SellerIds != "" {
  757. sellerIds := strings.Split(req.SellerIds, ",")
  758. cond += ` AND d.seller_id in ? `
  759. pars = append(pars, sellerIds)
  760. prevCond += ` AND d.seller_id in ?`
  761. prevPars = append(prevPars, sellerIds)
  762. histrtyCond += ` AND seller_id in ? `
  763. prevHistoryCond += ` AND seller_id in ? `
  764. historyPars = append(historyPars, sellerIds)
  765. prevHistoryPars = append(prevHistoryPars, sellerIds)
  766. }
  767. // 套餐筛选
  768. if req.ServiceTypes != "" {
  769. serviceTypes := strings.Split(req.ServiceTypes, ",")
  770. registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  771. if e != nil {
  772. err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
  773. return
  774. }
  775. if len(registerIds) > 0 {
  776. cond += ` AND a.register_id IN ?`
  777. prevCond += ` AND a.register_id IN ?`
  778. pars = append(pars, registerIds)
  779. prevPars = append(pars, registerIds)
  780. } else {
  781. cond += ` AND 1 = 2`
  782. prevCond += ` AND 1 = 2`
  783. }
  784. }
  785. //fmt.Println("i:",i)
  786. //fmt.Println("j:",j)
  787. {
  788. //本期
  789. st := fmt.Sprint(startDate, " 00:00:00")
  790. ed := fmt.Sprint(endDate, " 23:59:59")
  791. //校验日期
  792. //全部走新查询
  793. cond += ` AND (d.invoice_time BETWEEN ? AND ?)`
  794. pars = append(pars, st, ed, st, ed)
  795. summaryIds, e := fms.GetPaymentCensusSummaryDataIds(cond, pars)
  796. if e != nil {
  797. return
  798. }
  799. // 开票到款金额合计(换算后)
  800. var amountTotal float64
  801. if len(summaryIds) > 0 {
  802. amountCond := `a.id IN ? `
  803. amountPars := make([]interface{}, 0)
  804. amountPars = append(amountPars, summaryIds)
  805. if req.SellerIds != "" {
  806. sellerIds := strings.Split(req.SellerIds, ",")
  807. amountCond += `AND ( d.seller_id IN ? AND a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?) `
  808. amountPars = append(amountPars, sellerIds, st, ed)
  809. } else {
  810. amountCond += `AND (a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?)`
  811. amountPars = append(amountPars, st, ed, st, ed)
  812. }
  813. results, e := fms.GetContractSummaryPaymentIncomeAmount(amountCond, amountPars)
  814. if e != nil {
  815. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  816. return
  817. }
  818. //dataList = append(dataList, results...)
  819. var amountSum float64
  820. for _, result := range results {
  821. incomeChart.DataList = append(incomeChart.DataList, result)
  822. amountSum += result.Amount
  823. fmt.Println("result.Amount:", result.Amount)
  824. }
  825. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  826. accumulate += amountTotal
  827. partAccumulate += amountTotal
  828. }
  829. if i == j || i == numMonth {
  830. if req.ListParam == "4" {
  831. totalMoneySlice = append(totalMoneySlice, accumulate)
  832. if startDateTime.Month() == 12 {
  833. accumulate = 0
  834. }
  835. } else if req.ListParam == "0" {
  836. totalMoneySlice = append(totalMoneySlice, amountTotal)
  837. } else if i > 0 || i == numMonth {
  838. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  839. fmt.Println("partAccumulate:", partAccumulate)
  840. partAccumulate = 0.0
  841. }
  842. }
  843. }
  844. { // 去年同期,用于计算同比值
  845. prevSt := fmt.Sprint(prevStartDate, " 00:00:00") // 格式化上一年同一天的开始时间
  846. prevEd := fmt.Sprint(prevEndDate, " 23:59:59") // 格式化上一年同一天的结束时间
  847. // 校验日期,分段查询
  848. // 全部走新查询
  849. prevCond += ` AND (d.invoice_time BETWEEN ? AND ?)` // 查询条件加入新的时间范围
  850. prevPars = append(prevPars, prevSt, prevEd, prevSt, prevEd) // 添加时间参数到参数列表
  851. prevSummaryIds, e := fms.GetPaymentCensusSummaryDataIds(prevCond, prevPars) // 获取摘要ID列表
  852. if e != nil {
  853. return
  854. }
  855. // 开票到款金额合计(换算后)
  856. var prevAmountTotal float64
  857. if len(prevSummaryIds) > 0 {
  858. amountCond := `a.id IN ? ` // 查询条件为ID在给定的摘要ID列表中
  859. amountPars := make([]interface{}, 0)
  860. amountPars = append(amountPars, prevSummaryIds) // 将摘要ID列表添加到参数列表
  861. if req.SellerIds != "" {
  862. sellerIds := strings.Split(req.SellerIds, ",")
  863. amountCond += `AND ( d.seller_id IN ? AND a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?) ` // 根据卖家ID和发票/支付状态筛选时间范围
  864. amountPars = append(amountPars, sellerIds, prevSt, prevEd, sellerIds, prevSt, prevEd) // 将卖家ID列表添加到参数列表
  865. } else {
  866. amountCond += `AND (a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?)` // 根据发票/支付状态筛选时间范围
  867. amountPars = append(amountPars, prevSt, prevEd, prevSt, prevEd)
  868. }
  869. results, e := fms.GetContractSummaryPaymentIncomeAmount(amountCond, amountPars) // 获取合同摘要收入金额数据
  870. if e != nil {
  871. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  872. return
  873. }
  874. var amountSum float64
  875. //historydataList = append(historydataList, results...)
  876. for _, result := range results {
  877. amountSum += result.Amount // 累计收入金额
  878. }
  879. prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64) // 将累计金额转换为浮点数
  880. historyAccumulate += prevAmountTotal // 累计历史金额
  881. partHistoryAccumulate += prevAmountTotal // 累计部分历史金额
  882. }
  883. if i == j || i == numMonth {
  884. if req.ListParam == "4" {
  885. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate) // 将累计金额添加到总金额切片中
  886. if prevStartDateTime.Month() == 12 {
  887. historyAccumulate = 0 // 如果是去年的12月份,则将历史金额清零
  888. }
  889. } else if req.ListParam == "0" {
  890. prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal) // 将部分历史金额添加到总金额切片中
  891. } else if i > 0 || i == numMonth {
  892. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate) // 将部分历史金额添加到总金额切片中
  893. fmt.Println("partHistoryAccumulate:", partHistoryAccumulate)
  894. partHistoryAccumulate = 0.0 // 清零部分历史金额
  895. }
  896. }
  897. if req.ListParam == "1" && i == j {
  898. if i == 0 {
  899. dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01")) // 添加下一个月份的开始日期到日期切片中
  900. j = j + 2
  901. } else {
  902. dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01")) // 添加下一季度的开始日期到日期切片中
  903. j = j + 3
  904. }
  905. } else if req.ListParam == "2" && i == j {
  906. if i == 0 {
  907. dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01")) // 添加下五个月份的开始日期到日期切片中
  908. j = j + 5
  909. } else {
  910. dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01")) // 添加下半年的开始日期到日期切片中
  911. j = j + 6
  912. }
  913. } else if req.ListParam == "3" && i == j {
  914. if i == 0 {
  915. dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
  916. j = j + 11
  917. } else {
  918. dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
  919. j = j + 12
  920. }
  921. } else if i == j {
  922. dateSlice = append(dateSlice, startDateTime.Format("06/01"))
  923. j++
  924. }
  925. }
  926. }
  927. fmt.Println("prevTotalMoneySlice:", len(prevTotalMoneySlice))
  928. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  929. if req.CompanyType != 3 {
  930. //计算同比值
  931. for i := range prevTotalMoneySlice {
  932. var yoy float64
  933. var yoyStr string
  934. //fmt.Println("1:", prevTotalMoneySlice[i])
  935. //fmt.Println("2:", totalMoneySlice[i])
  936. //fmt.Println("3:", totalMoneySlice[i]-prevTotalMoneySlice[i])
  937. totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalMoneySlice[i]), 64)
  938. if prevTotalMoneySlice[i] != 0 && totalMoneySlice[i] != 0 {
  939. yoy = (totalMoneySlice[i] - prevTotalMoneySlice[i]) / prevTotalMoneySlice[i]
  940. yoyStr = fmt.Sprintf("%.4f", yoy)
  941. if i == len(prevTotalMoneySlice)-1 && i > 0 && req.ListParam == "3" {
  942. fmt.Println("totalMoneySlice[i-1]:", totalMoneySlice[i-1])
  943. yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
  944. yoyStr = fmt.Sprintf("%.4f", yoy)
  945. }
  946. }
  947. yoySlice = append(yoySlice, yoyStr)
  948. }
  949. } else {
  950. // 当筛选条件是”月度“时,并且客户是”未续约“客户,展示柱形图和曲线图
  951. // 当筛选条件是”季度“、”半年度“、”年度“、”月度统计“时,并且客户是”未续约“时,仅展示柱形图,无曲线图
  952. // 未续约收入金额=去年同期总收入金额(新客户+老客户)-当期老客户收入金额
  953. // 移动平均值:若是10月份,计算8月,9月,10月,这三个月的收入金额平均值
  954. for i := range prevTotalMoneySlice {
  955. totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", prevTotalMoneySlice[i]-totalMoneySlice[i]), 64)
  956. var yoy float64
  957. var yoyStr string
  958. if i > 1 && req.ListParam == "0" {
  959. // 前俩月没有
  960. yoy = (totalMoneySlice[i] + totalMoneySlice[i-1] + totalMoneySlice[i-2]) / 3
  961. yoyStr = fmt.Sprintf("%.4f", yoy)
  962. yoySlice = append(yoySlice, yoyStr)
  963. } else {
  964. yoySlice = append(yoySlice, "")
  965. }
  966. }
  967. }
  968. incomeChart.Title = "开票到款统计图"
  969. incomeChart.Date = dateSlice[:len(totalMoneySlice)]
  970. incomeChart.TotalMoney = totalMoneySlice
  971. incomeChart.PrevTotalMoney = prevTotalMoneySlice
  972. incomeChart.Yoy = yoySlice
  973. //redisJsonData, err := json.Marshal(incomeChart)
  974. //if err == nil {
  975. // global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  976. //}
  977. //} else {
  978. // err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  979. // if err != nil {
  980. // fmt.Println("近两年的收入统计数据,json转换失败")
  981. // }
  982. //}
  983. return
  984. }
  985. // IncomeList
  986. // @Title 业务收入统计表
  987. // @Description 获取业务收入统计表接口
  988. // @Param ListParam query int false "套餐类型: 0-月度; 1-季度; 2-半年度;3-年度;4-月度累计"
  989. // @Success 200 {object} fms.CensusIncomeChartResp
  990. // @router /census/income/list [get]
  991. func (this *InvoicePaymentController) IncomeList(c *gin.Context) {
  992. var req fms.IncomeListReq
  993. if e := c.BindQuery(&req); e != nil {
  994. err, ok := e.(validator.ValidationErrors)
  995. if !ok {
  996. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  997. return
  998. }
  999. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1000. return
  1001. }
  1002. //收入统计
  1003. var incomeList base.BaseData
  1004. ch := make(chan base.BaseData, 1)
  1005. if req.IncomeType == 0 {
  1006. go getPaymentIncomeList(ch, req)
  1007. } else {
  1008. go getPaymentIncomeList(ch, req)
  1009. }
  1010. for v := range ch {
  1011. incomeList = v
  1012. close(ch)
  1013. }
  1014. // 是否导出
  1015. if req.IsExport == 1 {
  1016. ExportIncomeList(c, incomeList.List, req.IncomeType)
  1017. return
  1018. }
  1019. resp.OkData("获取成功", incomeList, c)
  1020. }
  1021. func getPaymentIncomeList(ch chan base.BaseData, req fms.IncomeListReq) (baseData base.BaseData, err error) {
  1022. defer func() {
  1023. if err != nil {
  1024. global.LOG.Error(err)
  1025. if err != utils.ErrNoRow {
  1026. go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
  1027. }
  1028. }
  1029. ch <- baseData
  1030. }()
  1031. var reqStartDate, reqEndDate time.Time
  1032. st := fmt.Sprint(req.StartDate, "-01 00:00:00")
  1033. ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
  1034. reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
  1035. reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
  1036. var startDate, endDate string
  1037. //开始日期
  1038. if req.StartDate != "" && req.EndDate != "" {
  1039. startDate = reqStartDate.Format(utils.FormatDate)
  1040. }
  1041. //结束日期
  1042. if req.StartDate != "" && req.EndDate != "" {
  1043. endDate = reqEndDate.Format(utils.FormatDate)
  1044. }
  1045. cond := `1 = 1`
  1046. pars := make([]interface{}, 0)
  1047. if req.CompanyType == 1 {
  1048. cond += ` AND b.contract_type = 1 `
  1049. } else if req.CompanyType == 2 {
  1050. cond += ` AND b.contract_type IN (2,3,4) `
  1051. } else if req.CompanyType == 3 {
  1052. //未续约收入金额=去年同期总收入金额(新客户+老客户)-当期老客户收入金额
  1053. cond += ` AND b.contract_type IN (2,3,4) `
  1054. }
  1055. if req.SellerIds != "" {
  1056. sellerIds := strings.Split(req.SellerIds, ",")
  1057. cond += ` AND d.seller_id in ?`
  1058. pars = append(pars, sellerIds)
  1059. }
  1060. // 套餐筛选
  1061. if req.ServiceTypes != "" {
  1062. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1063. registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1064. if e != nil {
  1065. err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
  1066. return
  1067. }
  1068. if len(registerIds) > 0 {
  1069. cond += ` AND a.register_id IN ?`
  1070. pars = append(pars, registerIds)
  1071. } else {
  1072. cond += ` AND 1 = 2`
  1073. }
  1074. }
  1075. {
  1076. if startDate != "" && endDate != "" {
  1077. st := fmt.Sprint(startDate, " 00:00:00")
  1078. ed := fmt.Sprint(endDate, " 23:59:59")
  1079. cond += ` AND (d.invoice_time BETWEEN ? AND ?)`
  1080. pars = append(pars, st, ed)
  1081. }
  1082. if req.Keyword != "" {
  1083. cond += ` AND b.company_name LIKE ?`
  1084. pars = append(pars, "%"+req.Keyword+"%")
  1085. }
  1086. summaryIds, e := fms.GetPaymentCensusSummaryDataIds(cond, pars)
  1087. if e != nil {
  1088. return
  1089. }
  1090. if len(summaryIds) > 0 {
  1091. amountCond := `a.id IN ? `
  1092. amountPars := make([]interface{}, 0)
  1093. amountPars = append(amountPars, summaryIds)
  1094. if req.SellerIds != "" {
  1095. sellerIds := strings.Split(req.SellerIds, ",")
  1096. amountCond += `AND ( d.seller_id IN ? AND a.payment_id <> 0 ) `
  1097. amountPars = append(amountPars, sellerIds)
  1098. } else {
  1099. amountCond += `AND (a.payment_id <> 0)`
  1100. }
  1101. page := new(base.Page)
  1102. page.SetPageSize(req.PageSize)
  1103. page.SetCurrent(req.Current)
  1104. if req.IsExport == 1 {
  1105. page.SetPageSize(10000)
  1106. page.SetCurrent(1)
  1107. }
  1108. if req.SortType == "asc" {
  1109. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: true})
  1110. } else {
  1111. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: false})
  1112. }
  1113. results, total, e := fms.GetContractSummaryPaymentIncomeAmountPage(amountCond, amountPars, page)
  1114. if e != nil {
  1115. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1116. return
  1117. }
  1118. // 货币列表
  1119. currencyOB := new(fms.CurrencyUnit)
  1120. currencyCond := `enable = 1`
  1121. currencyPars := make([]interface{}, 0)
  1122. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1123. if e != nil {
  1124. err = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1125. return
  1126. }
  1127. unitMap := make(map[string]string)
  1128. for i := range currencyList {
  1129. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1130. }
  1131. respItems := make([]*fms.IncomeSummaryRespItem, 0)
  1132. for _, v := range results {
  1133. respItem := &fms.IncomeSummaryRespItem{
  1134. CompanyName: v.CompanyName,
  1135. ContractType: v.ContractType,
  1136. InvoiceDate: v.InvoiceDate.Format(utils.FormatDate),
  1137. Amount: v.Amount,
  1138. SellerName: v.SellerName,
  1139. FinalSellerId: v.FinalSellerId,
  1140. OriginAmount: v.OriginAmount,
  1141. UnitName: unitMap[v.CurrencyUnit],
  1142. ServicesName: v.ServicesName,
  1143. }
  1144. respItems = append(respItems, respItem)
  1145. }
  1146. page.SetTotal(total)
  1147. baseData.SetPage(page)
  1148. baseData.SetList(respItems)
  1149. }
  1150. }
  1151. return
  1152. }
  1153. //func getCensusIncomeList(ch chan base.BaseData, req fms.IncomeListReq) (baseData base.BaseData, err error) {
  1154. // defer func() {
  1155. // if err != nil {
  1156. // global.LOG.Error(err)
  1157. // if err != utils.ErrNoRow {
  1158. // go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
  1159. // }
  1160. // }
  1161. // ch <- baseData
  1162. // }()
  1163. //
  1164. // var reqStartDate, reqEndDate time.Time
  1165. // historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  1166. //
  1167. // // 套餐筛选
  1168. // if req.ServiceTypes != "" {
  1169. // // 勾选套餐筛选时忽略导入的历史数据
  1170. // if req.StartDate == "" {
  1171. // req.StartDate = "2023-04"
  1172. // req.EndDate = "3023-04"
  1173. // } else {
  1174. // startDateTime, _ := time.Parse(utils.FormatMonth, req.StartDate)
  1175. // if startDateTime.Before(historyTime) {
  1176. // req.StartDate = "2023-04"
  1177. // }
  1178. // }
  1179. // }
  1180. //
  1181. // if req.StartDate != "" && req.EndDate != "" {
  1182. // st := fmt.Sprint(req.StartDate, "-01 00:00:00")
  1183. // ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
  1184. // reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
  1185. // reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
  1186. // }
  1187. //
  1188. // //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  1189. //
  1190. // var endDateTime time.Time
  1191. // var startDate, endDate string
  1192. // //开始日期
  1193. // if req.StartDate != "" && req.EndDate != "" {
  1194. // startDate = reqStartDate.Format("2006-01")
  1195. // startDate = fmt.Sprint(startDate, "-01")
  1196. // }
  1197. //
  1198. // startDateTime, _ := time.Parse(utils.FormatDate, startDate)
  1199. //
  1200. // //结束日期
  1201. // if req.StartDate != "" && req.EndDate != "" {
  1202. // endDate = reqEndDate.Format(utils.FormatDate)
  1203. // }
  1204. //
  1205. // cond := `1 = 1`
  1206. // histrtyCond := `1 = 1`
  1207. // pars := make([]interface{}, 0)
  1208. // historyPars := make([]interface{}, 0)
  1209. //
  1210. // if req.CompanyType == 1 {
  1211. // cond += ` AND b.contract_type = 1 `
  1212. // histrtyCond += ` AND new_company = 1 `
  1213. // } else if req.CompanyType == 2 {
  1214. // cond += ` AND b.contract_type IN (2,3,4) `
  1215. // histrtyCond += ` AND new_company = 0 `
  1216. // } else if req.CompanyType == 3 {
  1217. // //未续约收入金额=去年同期总收入金额(新客户+老客户)-当期老客户收入金额
  1218. // cond += ` AND b.contract_type IN (2,3,4) `
  1219. // histrtyCond += ` AND new_company = 0 `
  1220. // }
  1221. //
  1222. // if req.SellerIds != "" {
  1223. // sellerIds := strings.Split(req.SellerIds, ",")
  1224. // cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  1225. // pars = append(pars, sellerIds, sellerIds)
  1226. //
  1227. // histrtyCond += ` AND seller_id in ? `
  1228. // historyPars = append(historyPars, sellerIds)
  1229. // }
  1230. //
  1231. // // 套餐筛选
  1232. // if req.ServiceTypes != "" {
  1233. // serviceTypes := strings.Split(req.ServiceTypes, ",")
  1234. // registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1235. // if e != nil {
  1236. // err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
  1237. // return
  1238. // }
  1239. // if len(registerIds) > 0 {
  1240. // cond += ` AND a.register_id IN ?`
  1241. // pars = append(pars, registerIds)
  1242. // } else {
  1243. // cond += ` AND 1 = 2`
  1244. // }
  1245. // }
  1246. //
  1247. // {
  1248. // st := fmt.Sprint(startDate, " 00:00:00")
  1249. // ed := fmt.Sprint(endDate, " 23:59:59")
  1250. // //校验日期,分段查询
  1251. // if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  1252. // //全部走新查询
  1253. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1254. // pars = append(pars, st, ed, st, ed)
  1255. // summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1256. // if e != nil {
  1257. // return
  1258. // }
  1259. //
  1260. // // 开票到款金额合计(换算后)
  1261. // var amountTotal float64
  1262. //
  1263. // if len(summaryIds) > 0 {
  1264. // amountCond := `a.id IN ? `
  1265. // amountPars := make([]interface{}, 0)
  1266. // amountPars = append(amountPars, summaryIds)
  1267. // if req.SellerIds != "" {
  1268. // sellerIds := strings.Split(req.SellerIds, ",")
  1269. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1270. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  1271. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  1272. // } else {
  1273. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1274. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)`
  1275. // amountPars = append(amountPars, st, ed, st, ed)
  1276. // }
  1277. // results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1278. // if e != nil {
  1279. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1280. // return
  1281. // }
  1282. // //dataList = append(dataList, results...)
  1283. // var amountSum float64
  1284. // for _, result := range results {
  1285. // incomeChart.DataList = append(incomeChart.DataList, result)
  1286. // amountSum += result.Amount
  1287. // fmt.Println("result.Amount:", result.Amount)
  1288. // }
  1289. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1290. // accumulate += amountTotal
  1291. // partAccumulate += amountTotal
  1292. // }
  1293. // } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  1294. // //全部走旧查询
  1295. // //fmt.Println("旧查询")
  1296. // //fmt.Println("st:",st)
  1297. // //fmt.Println("ed:",ed)
  1298. // histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1299. // historyPars = append(historyPars, st, ed)
  1300. // //fmt.Println("st:",st)
  1301. // //fmt.Println("ed:",ed)
  1302. // // 开票到款金额合计(换算后)
  1303. // var amountTotal float64
  1304. // results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
  1305. // if e != nil {
  1306. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1307. // return
  1308. // }
  1309. // var amountSum float64
  1310. // //dataList = append(dataList, results...)
  1311. // for _, result := range results {
  1312. // incomeChart.DataList = append(incomeChart.DataList, result)
  1313. // amountSum += result.Amount
  1314. // }
  1315. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1316. // accumulate += amountTotal
  1317. // partAccumulate += amountTotal
  1318. //
  1319. // if i == j || i == numMonth {
  1320. // if req.ListParam == "4" {
  1321. // totalMoneySlice = append(totalMoneySlice, accumulate)
  1322. // if startDateTime.Month() == 12 {
  1323. // accumulate = 0
  1324. // }
  1325. // } else if req.ListParam == "0" {
  1326. // totalMoneySlice = append(totalMoneySlice, amountTotal)
  1327. // } else if i > 0 || i == numMonth {
  1328. // totalMoneySlice = append(totalMoneySlice, partAccumulate)
  1329. // partAccumulate = 0.0
  1330. // }
  1331. // }
  1332. //
  1333. // //fmt.Println("partAccumulate:",partAccumulate)
  1334. // }
  1335. //
  1336. // }
  1337. //
  1338. // return
  1339. //}
  1340. // ExportIncomeList 导出业务收入统计列表
  1341. func ExportIncomeList(c *gin.Context, results interface{}, incomeType int) {
  1342. list := results.([]*fms.IncomeSummaryRespItem)
  1343. // 生成Excel文件
  1344. xlsxFile := xlsx.NewFile()
  1345. style := xlsx.NewStyle()
  1346. alignment := xlsx.Alignment{
  1347. Horizontal: "center",
  1348. Vertical: "center",
  1349. WrapText: true,
  1350. }
  1351. style.Alignment = alignment
  1352. style.ApplyAlignment = true
  1353. sheetName := "业务收入统计表"
  1354. sheet, err := xlsxFile.AddSheet(sheetName)
  1355. if err != nil {
  1356. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1357. return
  1358. }
  1359. // 数据表头
  1360. rowTitle := make([]string, 0)
  1361. if incomeType == 0 {
  1362. rowTitle = []string{"客户名称", "新客户(1)", "开票日期", "开票金额", "金额单位", "开票金额(换算后)", "销售名称", "套餐名称"}
  1363. } else {
  1364. rowTitle = []string{"客户名称", "新客户(1)", "到款日期", "到款金额", "金额单位", "到款金额(换算后)", "销售名称", "套餐名称"}
  1365. }
  1366. titleRow := sheet.AddRow()
  1367. for i := range rowTitle {
  1368. v := titleRow.AddCell()
  1369. v.SetString(rowTitle[i])
  1370. v.SetStyle(style)
  1371. }
  1372. incomeSummaryItemList := make([]*fms.IncomeSummaryRespItem, 0)
  1373. incomeSummaryItemList = list
  1374. //sort.Sort(incomeSummaryItemList)
  1375. // 填充数据
  1376. for _, v := range incomeSummaryItemList {
  1377. dataRow := sheet.AddRow()
  1378. dataRow.AddCell().SetString(v.CompanyName) // 客户名称
  1379. newCompany := 0
  1380. if v.ContractType == 1 {
  1381. newCompany = 1
  1382. }
  1383. dataRow.AddCell().SetString(strconv.Itoa(newCompany)) // 新客户
  1384. dataRow.AddCell().SetString(v.InvoiceDate) // 开票到款日期
  1385. dataRow.AddCell().SetString(fmt.Sprintf("%.2f", v.OriginAmount)) // 开票到款金额
  1386. dataRow.AddCell().SetString(v.UnitName) // 金额单位
  1387. dataRow.AddCell().SetString(fmt.Sprintf("%.2f", v.Amount)) // 开票到款金额 换算后
  1388. dataRow.AddCell().SetString(v.SellerName) // 销售员
  1389. dataRow.AddCell().SetString(v.ServicesName) // 套餐名称
  1390. }
  1391. // 输出文件
  1392. var buffer bytes.Buffer
  1393. _ = xlsxFile.Write(&buffer)
  1394. content := bytes.NewReader(buffer.Bytes())
  1395. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1396. fileName := sheetName + randStr + ".xlsx"
  1397. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1398. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1399. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1400. }