invoice_payment.go 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  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/crm"
  13. "hongze/fms_api/models/fms"
  14. "hongze/fms_api/services/alarm_msg"
  15. crmService "hongze/fms_api/services/crm"
  16. fmsService "hongze/fms_api/services/fms"
  17. "hongze/fms_api/utils"
  18. "net/http"
  19. "sort"
  20. "strconv"
  21. "strings"
  22. "sync"
  23. "time"
  24. )
  25. // InvoicePaymentController 商品到款统计
  26. type InvoicePaymentController struct{}
  27. // List2 原版(已废弃)
  28. //func (ct *InvoicePaymentController) List2(c *gin.Context) {
  29. // var req fms.InvoicePaymentCensusListReq
  30. // if e := c.BindQuery(&req); e != nil {
  31. // err, ok := e.(validator.ValidationErrors)
  32. // if !ok {
  33. // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  34. // return
  35. // }
  36. // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  37. // return
  38. // }
  39. //
  40. // cond := `1 = 1`
  41. // pars := make([]interface{}, 0)
  42. // // 合同编号/客户姓名/销售
  43. // if req.Keyword != "" {
  44. // kw := "%" + req.Keyword + "%"
  45. // cond += ` AND b.company_name LIKE ? OR a.seller_name LIKE ?`
  46. // pars = append(pars, kw, kw)
  47. // }
  48. // if req.SellGroupId > 0 {
  49. // cond += ` AND a.seller_group_id = ?`
  50. // pars = append(pars, req.SellGroupId)
  51. // }
  52. // // 套餐筛选
  53. // if req.ServiceType != 0 {
  54. // registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  55. // if e != nil {
  56. // resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  57. // return
  58. // }
  59. // if len(registerIds) > 0 {
  60. // cond += ` AND b.contract_register_id IN ?`
  61. // pars = append(pars, registerIds)
  62. // } else {
  63. // cond += ` AND 1 = 2`
  64. // }
  65. // }
  66. // // 开票到款日期
  67. // if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
  68. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  69. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  70. // cond += ` AND a.invoice_type = ? AND (a.invoice_time BETWEEN ? AND ?)`
  71. // pars = append(pars, req.TimeType, st, ed)
  72. // }
  73. // // 已开票
  74. // if req.HasInvoice == 1 && req.HasPayment == 0 {
  75. // cond += ` AND a.invoice_type = 1`
  76. // }
  77. // // 已到款
  78. // if req.HasInvoice == 0 && req.HasPayment == 1 {
  79. // cond += ` AND a.invoice_type = 2`
  80. // }
  81. //
  82. // page := new(base.Page)
  83. // page.SetPageSize(req.PageSize)
  84. // page.SetCurrent(req.Current)
  85. // page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  86. // page.AddOrderItem(base.OrderItem{Column: "b.contract_register_id", Asc: true})
  87. // if req.IsExport == 1 {
  88. // page.SetPageSize(10000)
  89. // page.SetCurrent(1)
  90. // }
  91. //
  92. // list, allRegisterIds, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  93. // if e != nil {
  94. // resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
  95. // return
  96. // }
  97. // total := int64(len(allRegisterIds))
  98. // registerIds := make([]int, 0)
  99. // for i := range list {
  100. // registerIds = append(registerIds, list[i].ContractRegisterId)
  101. // }
  102. //
  103. // results := new(fms.InvoicePaymentCensusResp)
  104. // if len(registerIds) > 0 {
  105. // // 获取开票到款列表
  106. // ivCond := `contract_register_id IN ?`
  107. // ivPars := make([]interface{}, 0)
  108. // ivPars = append(ivPars, registerIds)
  109. // iv := new(fms.ContractInvoice)
  110. // invoiceList, e := iv.List(ivCond, ivPars, "")
  111. // if e != nil {
  112. // resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
  113. // return
  114. // }
  115. // // 取出开票、到款
  116. // invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
  117. // paymentMap := make(map[int][]*fms.ContractInvoice, 0)
  118. // paymentIds := make([]int, 0)
  119. // for i := range invoiceList {
  120. // if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  121. // invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  122. // }
  123. // if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  124. // paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  125. // }
  126. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  127. // invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  128. // }
  129. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  130. // paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  131. // paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
  132. // }
  133. // }
  134. //
  135. // // 合同套餐
  136. // contractServiceCond := `contract_register_id IN ?`
  137. // contractServicePars := make([]interface{}, 0)
  138. // contractServicePars = append(contractServicePars, registerIds)
  139. // contractServiceOB := new(fms.ContractService)
  140. // contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  141. // if e != nil {
  142. // resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
  143. // return
  144. // }
  145. // contractServiceMap := make(map[int][]*fms.ContractService, 0)
  146. // servicesNameMap := make(map[int][]string, 0)
  147. // for i := range contractServiceList {
  148. // if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  149. // contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  150. // }
  151. // contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  152. // servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  153. // }
  154. //
  155. // // 到款套餐分配
  156. // serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  157. // if len(paymentIds) > 0 {
  158. // serviceAmountCond := `contract_payment_id IN ?`
  159. // serviceAmountPars := make([]interface{}, 0)
  160. // serviceAmountPars = append(serviceAmountPars, paymentIds)
  161. // serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  162. // serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  163. // if e != nil {
  164. // resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
  165. // return
  166. // }
  167. // for i := range serviceAmountList {
  168. // if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
  169. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
  170. // }
  171. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
  172. // }
  173. // }
  174. //
  175. // // 整合响应数据
  176. // respList := make([]*fms.InvoicePaymentCensusItem, 0)
  177. // for i := range list {
  178. // v := new(fms.InvoicePaymentCensusItem)
  179. // v.ContractRegisterId = list[i].ContractRegisterId
  180. // v.CompanyName = list[i].CompanyName
  181. // v.NewCompany = list[i].NewCompany
  182. // v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  183. // v.EndDate = list[i].EndDate.Format(utils.FormatDate)
  184. // svList := servicesNameMap[list[i].ContractRegisterId]
  185. // v.ServicesName = strings.Join(svList, ",")
  186. // // 格式化(合并)开票到款数据
  187. // v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
  188. // contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
  189. // respList = append(respList, v)
  190. // }
  191. //
  192. // // 开票到款金额合计(换算后)
  193. // amountTotalCond := `contract_register_id IN ?`
  194. // amountTotalPars := make([]interface{}, 0)
  195. // amountTotalPars = append(amountTotalPars, allRegisterIds)
  196. // amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
  197. // if e != nil {
  198. // resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
  199. // return
  200. // }
  201. // amountTotalMap := make(map[int]float64)
  202. // for i := range amountTotalList {
  203. // amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
  204. // }
  205. // invoiceTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypeMake]), 64)
  206. // paymentTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypePay]), 64)
  207. //
  208. // // 分币种金额统计
  209. // currencyOB := new(fms.CurrencyUnit)
  210. // currencyCond := `enable = 1`
  211. // currencyPars := make([]interface{}, 0)
  212. // currencyList, e := currencyOB.List(currencyCond, currencyPars)
  213. // if e != nil {
  214. // resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  215. // return
  216. // }
  217. // unitMap := make(map[string]string)
  218. // invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  219. // paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  220. // for i := range currencyList {
  221. // unitMap[currencyList[i].Code] = currencyList[i].UnitName
  222. // invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  223. // Name: currencyList[i].Name,
  224. // UnitName: currencyList[i].UnitName,
  225. // Code: currencyList[i].Code,
  226. // FlagImg: currencyList[i].FlagImg,
  227. // })
  228. // paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  229. // Name: currencyList[i].Name,
  230. // UnitName: currencyList[i].UnitName,
  231. // Code: currencyList[i].Code,
  232. // FlagImg: currencyList[i].FlagImg,
  233. // })
  234. // }
  235. // sumList, e := fms.GetInvoiceListCurrencySum(amountTotalCond, amountTotalPars, "currency_unit, invoice_type")
  236. // if e != nil {
  237. // resp.FailMsg("获取失败", "获取商品到款统计货币合计金额失败, Err: "+e.Error(), c)
  238. // return
  239. // }
  240. // invoiceSumMap := make(map[string]float64)
  241. // paymentSumMap := make(map[string]float64)
  242. // for i := range sumList {
  243. // if sumList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  244. // invoiceSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  245. // continue
  246. // }
  247. // if sumList[i].InvoiceType == fms.ContractInvoiceTypePay {
  248. // paymentSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  249. // }
  250. // }
  251. // for i := range invoiceCurrencyTotals {
  252. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  253. // invoiceCurrencyTotals[i].Amount = a
  254. // }
  255. // for i := range paymentCurrencyTotals {
  256. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  257. // paymentCurrencyTotals[i].Amount = a
  258. // }
  259. //
  260. // results.DataList = respList
  261. // results.InvoiceTotal = invoiceTotal
  262. // results.PaymentTotal = paymentTotal
  263. // results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  264. // results.PaymentCurrencyTotal = paymentCurrencyTotals
  265. // }
  266. //
  267. // // 是否导出
  268. // if req.IsExport == 1 {
  269. // ExportInvoicePaymentCensusList(c, results)
  270. // return
  271. // }
  272. // page.SetTotal(total)
  273. // baseData := new(base.BaseData)
  274. // baseData.SetPage(page)
  275. // baseData.SetList(results)
  276. // resp.OkData("获取成功", baseData, c)
  277. //}
  278. // ExportInvoicePaymentCensusList 导出商品到款统计列表
  279. func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentCensusResp) {
  280. list := results.DataList
  281. if len(list) == 0 {
  282. resp.Fail("列表数据为空", c)
  283. return
  284. }
  285. // 生成Excel文件
  286. xlsxFile := xlsx.NewFile()
  287. style := xlsx.NewStyle()
  288. alignment := xlsx.Alignment{
  289. Horizontal: "center",
  290. Vertical: "center",
  291. WrapText: true,
  292. }
  293. style.Alignment = alignment
  294. style.ApplyAlignment = true
  295. sheet, err := xlsxFile.AddSheet("商品到款统计")
  296. if err != nil {
  297. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  298. return
  299. }
  300. _ = sheet.SetColWidth(1, 1, 30)
  301. _ = sheet.SetColWidth(3, 3, 30)
  302. // 前三行-开票金额合计
  303. rowA := sheet.AddRow()
  304. cellAA := rowA.AddCell()
  305. cellAA.SetString(fmt.Sprintf("已开票合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  306. rowBData := "已开票金额:"
  307. for _, v := range results.InvoiceCurrencyTotal {
  308. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  309. }
  310. rowB := sheet.AddRow()
  311. rowB.AddCell().SetString(rowBData)
  312. sheet.AddRow()
  313. // 四至六-到款金额合计
  314. rowD := sheet.AddRow()
  315. cellDA := rowD.AddCell()
  316. cellDA.SetString(fmt.Sprintf("已到款合计金额(换算后):%.2f(元)", results.PaymentTotal))
  317. rowEData := "已到款金额:"
  318. for _, v := range results.PaymentCurrencyTotal {
  319. rowEData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  320. }
  321. rowE := sheet.AddRow()
  322. rowE.AddCell().SetString(rowEData)
  323. sheet.AddRow()
  324. // 表头, 套餐动态获取
  325. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  326. "销售组别", "销售类型"}
  327. serviceTempCond := ``
  328. serviceTempPars := make([]interface{}, 0)
  329. serviceTempOB := new(fms.ContractServiceTemplate)
  330. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  331. if e != nil {
  332. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  333. return
  334. }
  335. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  336. var serviceTempShow []*fms.ContractServiceTemplate
  337. for i := range serviceTempList {
  338. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  339. }
  340. for i := range serviceTempList {
  341. if serviceTempList[i].ProductId == 1 {
  342. rowTitle = append(rowTitle, serviceTempList[i].Title)
  343. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  344. } else if serviceTempList[i].ProductId == 2 {
  345. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid == 0) {
  346. } else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  347. } else {
  348. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  349. }
  350. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  351. continue
  352. }
  353. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  354. continue
  355. }
  356. rowTitle = append(rowTitle, serviceTempList[i].Title)
  357. }
  358. }
  359. hi := 0
  360. insertHi := 0
  361. titleRow := sheet.AddRow()
  362. for i := range rowTitle {
  363. v := titleRow.AddCell()
  364. v.SetString(rowTitle[i])
  365. v.SetStyle(style)
  366. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  367. v.VMerge = 1
  368. hi++
  369. } else {
  370. if insertHi == 0 {
  371. insertHi = hi
  372. }
  373. v.HMerge = 1
  374. titleRow.AddCell().SetString("")
  375. }
  376. }
  377. // 新增一行放主观和客观
  378. specialRow := sheet.AddRow()
  379. for i := 0; i < hi; i++ {
  380. if i == insertHi {
  381. v1 := specialRow.AddCell()
  382. v1.SetString("45万")
  383. v1.SetStyle(style)
  384. v1 = specialRow.AddCell()
  385. v1.SetString("70万")
  386. v1.SetStyle(style)
  387. for j := 0; j < 8; j++ {
  388. if j%2 == 0 {
  389. v1 = specialRow.AddCell()
  390. v1.SetString("主观")
  391. v1.SetStyle(style)
  392. } else {
  393. v1 = specialRow.AddCell()
  394. v1.SetString("客观")
  395. v1.SetStyle(style)
  396. }
  397. }
  398. } else {
  399. v := specialRow.AddCell()
  400. v.SetString("")
  401. }
  402. }
  403. newCompanyMap := map[int]string{0: "/", 1: "是", 2: "否"}
  404. sellerTypeMap := map[int]string{1: "FICC销售", 2: "权益销售"}
  405. for k, v := range list {
  406. dataRow := sheet.AddRow()
  407. // 前四个单元格根据每行开票到款条数向下合并
  408. l := len(v.InvoicePaymentList)
  409. mergeRowNum := l - 1
  410. // 序号
  411. sortNum := k + 1
  412. colA := dataRow.AddCell()
  413. colA.VMerge = mergeRowNum
  414. colA.SetString(fmt.Sprint(sortNum))
  415. // 客户名称
  416. colB := dataRow.AddCell()
  417. colB.VMerge = mergeRowNum
  418. colB.SetString(v.CompanyName)
  419. // 是否新客户
  420. colC := dataRow.AddCell()
  421. colC.VMerge = mergeRowNum
  422. colC.SetString(newCompanyMap[v.ContractType])
  423. // 合同有效期
  424. colD := dataRow.AddCell()
  425. colD.VMerge = mergeRowNum
  426. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  427. // 开票到款信息
  428. for k2, v2 := range v.InvoicePaymentList {
  429. rowData := []string{
  430. v2.InvoiceDate, // 开票日
  431. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  432. v2.PaymentDate, // 到款日
  433. fmt.Sprint(v2.PaymentAmount), // 到款金额
  434. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  435. v2.SellerName, // 销售
  436. v2.SellerGroupName, // 组别
  437. sellerTypeMap[v2.SellerType], // 销售类型
  438. }
  439. // 套餐金额信息
  440. serviceTempShowAmount := make(map[int]string)
  441. for i := range serviceTempShow {
  442. for s2 := range v2.ServiceAmountList {
  443. item := v2.ServiceAmountList[s2]
  444. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  445. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  446. break
  447. } else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  448. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount / 2)
  449. break
  450. }
  451. }
  452. }
  453. for i := range serviceTempShow {
  454. sa := ""
  455. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  456. sa = am
  457. }
  458. rowData = append(rowData, sa)
  459. }
  460. // 首行开票到款
  461. if k2 == 0 {
  462. for i := range rowData {
  463. dataRow.AddCell().SetString(rowData[i])
  464. }
  465. continue
  466. }
  467. // 其他行开票到款, 加四列空的单元格用于合并
  468. dataRowExtra := sheet.AddRow()
  469. for i := 0; i < 4; i++ {
  470. dataRowExtra.AddCell()
  471. }
  472. for i := range rowData {
  473. dataRowExtra.AddCell().SetString(rowData[i])
  474. }
  475. }
  476. }
  477. // 输出文件
  478. var buffer bytes.Buffer
  479. _ = xlsxFile.Write(&buffer)
  480. content := bytes.NewReader(buffer.Bytes())
  481. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  482. fileName := "商品到款统计_" + randStr + ".xlsx"
  483. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  484. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  485. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  486. }
  487. // List
  488. // @Title 商品到款统计列表
  489. // @Description 商品到款统计列表
  490. // @Param Keyword query string false "关键词"
  491. // @Param SellGroupId query int false "销售组别ID"
  492. // @Param ServiceType query int false "套餐类型"
  493. // @Param StartDate query string false "合同开始日期"
  494. // @Param EndDate query string false "合同结束日期"
  495. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  496. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  497. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  498. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  499. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  500. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  501. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  502. // @Success 200 {object} fms.ContractRegisterItem
  503. // @router /census/invoice_payment/list [get]
  504. func (ct *InvoicePaymentController) List(c *gin.Context) {
  505. var req fms.InvoicePaymentCensusListReq
  506. if e := c.BindQuery(&req); e != nil {
  507. err, ok := e.(validator.ValidationErrors)
  508. if !ok {
  509. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  510. return
  511. }
  512. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  513. return
  514. }
  515. cond := `1 = 1`
  516. pars := make([]interface{}, 0)
  517. // 客户姓名/销售
  518. if req.Keyword != "" {
  519. kw := "%" + req.Keyword + "%"
  520. cond += ` AND b.company_name LIKE ?`
  521. pars = append(pars, kw)
  522. }
  523. if req.SellerIds != "" {
  524. sellerIds := strings.Split(req.SellerIds, ",")
  525. cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  526. pars = append(pars, sellerIds, sellerIds)
  527. }
  528. // 套餐筛选
  529. if req.ServiceTypes != "" {
  530. serviceTypes := strings.Split(req.ServiceTypes, ",")
  531. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  532. if e != nil {
  533. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  534. return
  535. }
  536. if len(tempRegisterIds) > 0 {
  537. cond += ` AND a.register_id IN ?`
  538. pars = append(pars, tempRegisterIds)
  539. } else {
  540. cond += ` AND 1 = 2`
  541. }
  542. }
  543. if req.ListParam == 1 {
  544. cond += ` AND a.service_product_id = 1 `
  545. } else if req.ListParam == 2 {
  546. cond += ` AND a.service_product_id = 2 `
  547. }
  548. // 开票到款日期
  549. if req.TimeType != 0 {
  550. if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  551. st := fmt.Sprint(req.StartDate, " 00:00:00")
  552. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  553. cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  554. pars = append(pars, st, ed)
  555. } else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  556. st := fmt.Sprint(req.StartDate, " 00:00:00")
  557. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  558. cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  559. pars = append(pars, st, ed)
  560. } else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  561. st := fmt.Sprint(req.StartDate, " 00:00:00")
  562. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  563. cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  564. pars = append(pars, st, ed, st, ed)
  565. }
  566. } else if req.StartDate != "" && req.EndDate != "" {
  567. st := fmt.Sprint(req.StartDate, " 00:00:00")
  568. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  569. if req.SellerIds != "" {
  570. sellerIds := strings.Split(req.SellerIds, ",")
  571. cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  572. pars = append(pars, sellerIds, sellerIds)
  573. cond += ` AND ((c.seller_id in ? AND a.invoice_id <> 0 AND c.invoice_time BETWEEN ? AND ?)`
  574. cond += `OR (d.seller_id in ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  575. pars = append(pars, sellerIds, st, ed, sellerIds, st, ed)
  576. } else {
  577. cond += ` AND ((a.invoice_id <> 0 AND c.invoice_time BETWEEN ? AND ?)`
  578. cond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  579. pars = append(pars, st, ed, st, ed)
  580. }
  581. }
  582. if req.HasInvoice == "1" {
  583. cond += ` AND a.invoice_id > 0 `
  584. } else if req.HasInvoice == "0" {
  585. cond += ` AND a.invoice_id = 0 `
  586. }
  587. if req.HasPayment == "1" {
  588. cond += ` AND a.payment_id > 0 `
  589. } else if req.HasPayment == "0" {
  590. cond += ` AND a.payment_id = 0 `
  591. }
  592. page := new(base.Page)
  593. page.SetPageSize(req.PageSize)
  594. page.SetCurrent(req.Current)
  595. //排序
  596. if req.SortType == "" {
  597. req.SortType = "desc"
  598. }
  599. if req.SortType != "desc" && req.SortType != "asc" {
  600. resp.Fail("排序类型不正确", c)
  601. return
  602. }
  603. if req.SortParam == "" {
  604. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  605. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  606. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  607. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  608. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  609. } else if req.SortParam == "invoice_time" {
  610. if req.SortType == "asc" {
  611. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: false})
  612. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
  613. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: true})
  614. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  615. } else {
  616. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  617. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  618. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  619. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  620. }
  621. } else if req.SortParam == "payment_date" {
  622. if req.SortType == "asc" {
  623. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  624. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: true})
  625. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: true})
  626. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  627. } else {
  628. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: true})
  629. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: false})
  630. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: false})
  631. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  632. }
  633. } else {
  634. resp.Fail("排序字段不正确", c)
  635. return
  636. }
  637. if req.IsExport == 1 {
  638. page.SetPageSize(10000)
  639. page.SetCurrent(1)
  640. }
  641. registerList, total, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  642. if e != nil {
  643. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  644. return
  645. }
  646. queryRegisterIds := make([]int, 0)
  647. for i := range registerList {
  648. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  649. }
  650. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  651. results := new(fms.InvoicePaymentCensusResp)
  652. if len(queryRegisterIds) > 0 {
  653. // 获取汇总数据IDs, 用于查询合计数据
  654. summaryIdsCond := cond
  655. summaryIdsPars := pars
  656. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  657. if e != nil {
  658. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  659. return
  660. }
  661. //fmt.Println("summaryIds:",summaryIds)
  662. var listErr, totalErr, totalGroupErr error
  663. wg := sync.WaitGroup{}
  664. // 响应列表
  665. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  666. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  667. wg.Add(1)
  668. go func() {
  669. defer wg.Done()
  670. // 获取汇总数据
  671. summaryCond := cond
  672. summaryCond += ` AND a.register_id IN ?`
  673. summaryPars := pars
  674. summaryPars = append(summaryPars, queryRegisterIds)
  675. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  676. if e != nil {
  677. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  678. return
  679. }
  680. summaryList = summaryData
  681. //summaryIds := make([]int, 0)
  682. paymentIds := make([]int, 0)
  683. for i := range summaryList {
  684. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  685. if summaryList[i].PaymentId > 0 {
  686. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  687. }
  688. }
  689. // 合同套餐
  690. /*contractServiceCond := `contract_register_id IN ?`
  691. contractServicePars := make([]interface{}, 0)
  692. contractServicePars = append(contractServicePars, queryRegisterIds)
  693. contractServiceOB := new(fms.ContractService)
  694. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  695. if e != nil {
  696. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  697. return
  698. }
  699. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  700. servicesNameMap := make(map[int][]string, 0)
  701. for i := range contractServiceList {
  702. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  703. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  704. }
  705. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  706. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  707. }
  708. */
  709. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  710. if e != nil {
  711. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  712. return
  713. }
  714. // 到款套餐分配
  715. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  716. if len(paymentIds) > 0 {
  717. serviceAmountCond := `contract_payment_id IN ?`
  718. serviceAmountPars := make([]interface{}, 0)
  719. serviceAmountPars = append(serviceAmountPars, paymentIds)
  720. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  721. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  722. if e != nil {
  723. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  724. return
  725. }
  726. for i := range serviceAmountList {
  727. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  728. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  729. }
  730. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  731. }
  732. }
  733. // 重组汇总数据
  734. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  735. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  736. for i := range summaryList {
  737. v := new(fms.InvoicePaymentCensusInfo)
  738. v.InvoiceId = summaryList[i].InvoiceId
  739. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  740. v.InvoiceAmount = summaryList[i].InvoiceAmount
  741. v.SellerId = summaryList[i].SellerId
  742. v.SellerName = summaryList[i].SellerName
  743. v.SellerGroupId = summaryList[i].SellerGroupId
  744. v.SellerGroupName = summaryList[i].SellerGroupName
  745. v.SellerType = summaryList[i].ServiceProductId
  746. v.PaymentId = summaryList[i].PaymentId
  747. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  748. v.PaymentAmount = summaryList[i].PaymentAmount
  749. v.PayType = summaryList[i].PayType
  750. // 套餐到款分配
  751. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  752. amountList := serviceAmountMap[summaryList[i].PaymentId]
  753. if amountList != nil {
  754. for i := range amountList {
  755. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  756. amountMap[k] = amountList[i]
  757. }
  758. }
  759. // 合同对应的所有套餐
  760. svList := serviceFormatMap[summaryList[i].RegisterId]
  761. if svList != nil {
  762. for ii := range svList {
  763. vv := new(fms.ContractPaymentServiceAmountItem)
  764. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  765. vv.ServiceTemplateName = svList[ii].FormatTitle
  766. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  767. vv.ServiceProductId = svList[ii].ServiceProductId
  768. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  769. a := amountMap[k2]
  770. if a != nil {
  771. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  772. vv.ContractPaymentId = a.ContractPaymentId
  773. vv.Amount = a.Amount
  774. }
  775. svaList = append(svaList, vv)
  776. }
  777. }
  778. v.ServiceAmountList = svaList
  779. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  780. }
  781. // 响应列表
  782. for i := range registerList {
  783. v := new(fms.InvoicePaymentCensusItem)
  784. v.SummaryId = registerList[i].SummaryId
  785. v.ContractRegisterId = registerList[i].ContractRegisterId
  786. v.CompanyName = registerList[i].CompanyName
  787. v.NewCompany = registerList[i].NewCompany
  788. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  789. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  790. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  791. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  792. v.ContractType = registerList[i].ContractType
  793. v.ActualPayCompanies = registerList[i].ActualPayCompanies
  794. respList = append(respList, v)
  795. }
  796. }()
  797. // 开票到款金额合计(换算后)
  798. var invoiceTotal, paymentTotal, amountTotal float64
  799. wg.Add(1)
  800. go func() {
  801. defer wg.Done()
  802. if len(summaryIds) == 0 {
  803. return
  804. }
  805. amountTotalCond := `a.id IN ?`
  806. amountTotalPars := make([]interface{}, 0)
  807. amountTotalPars = append(amountTotalPars, summaryIds)
  808. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  809. if e != nil {
  810. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  811. return
  812. }
  813. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  814. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  815. if e != nil {
  816. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  817. return
  818. }
  819. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  820. amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  821. amountPars := make([]interface{}, 0)
  822. amountPars = append(amountPars, summaryIds)
  823. amountSum, e := fms.GetContractSummaryInvoicePaymentAmount(amountCond, amountPars)
  824. if e != nil {
  825. totalErr = fmt.Errorf("获取汇总金额合计失败, Err: %s", e.Error())
  826. return
  827. }
  828. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  829. }()
  830. // 分币种金额统计
  831. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  832. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  833. wg.Add(1)
  834. go func() {
  835. defer wg.Done()
  836. currencyOB := new(fms.CurrencyUnit)
  837. currencyCond := `enable = 1`
  838. currencyPars := make([]interface{}, 0)
  839. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  840. if e != nil {
  841. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  842. return
  843. }
  844. unitMap := make(map[string]string)
  845. for i := range currencyList {
  846. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  847. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  848. Name: currencyList[i].Name,
  849. UnitName: currencyList[i].UnitName,
  850. Code: currencyList[i].Code,
  851. FlagImg: currencyList[i].FlagImg,
  852. })
  853. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  854. Name: currencyList[i].Name,
  855. UnitName: currencyList[i].UnitName,
  856. Code: currencyList[i].Code,
  857. FlagImg: currencyList[i].FlagImg,
  858. })
  859. }
  860. if len(summaryIds) == 0 {
  861. return
  862. }
  863. totalGroupCond := `a.id IN ?`
  864. totalGroupPars := make([]interface{}, 0)
  865. totalGroupPars = append(totalGroupPars, summaryIds)
  866. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  867. if e != nil {
  868. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  869. return
  870. }
  871. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  872. if e != nil {
  873. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  874. return
  875. }
  876. invoiceSumMap := make(map[string]float64)
  877. paymentSumMap := make(map[string]float64)
  878. for i := range invoiceSumGroup {
  879. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  880. continue
  881. }
  882. for i := range paymentSumGroup {
  883. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  884. continue
  885. }
  886. for i := range invoiceCurrencyTotals {
  887. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  888. invoiceCurrencyTotals[i].Amount = a
  889. }
  890. for i := range paymentCurrencyTotals {
  891. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  892. paymentCurrencyTotals[i].Amount = a
  893. }
  894. }()
  895. wg.Wait()
  896. if listErr != nil {
  897. resp.FailMsg("获取失败", listErr.Error(), c)
  898. return
  899. }
  900. if totalErr != nil {
  901. resp.FailMsg("获取失败", totalErr.Error(), c)
  902. return
  903. }
  904. if totalGroupErr != nil {
  905. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  906. return
  907. }
  908. results.DataList = respList
  909. results.InvoiceTotal = invoiceTotal
  910. results.PaymentTotal = paymentTotal
  911. results.AmountTotal = amountTotal
  912. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  913. results.PaymentCurrencyTotal = paymentCurrencyTotals
  914. }
  915. // 是否导出
  916. if req.IsExport == 1 {
  917. ExportInvoicePaymentCensusList(c, results)
  918. return
  919. }
  920. page.SetTotal(total)
  921. baseData := new(base.BaseData)
  922. baseData.SetPage(page)
  923. baseData.SetList(results)
  924. resp.OkData("获取成功", baseData, c)
  925. }
  926. // IncomeList
  927. // @Title 业务收入统计表
  928. // @Description 获取业务收入统计表接口
  929. // @Param ListParam query int false "套餐类型: 0-月度; 1-季度; 2-半年度;3-年度;4-月度累计"
  930. // @Success 200 {object} fms.CensusIncomeChartResp
  931. // @router /census/income/list [get]
  932. func (this *InvoicePaymentController) IncomeList(c *gin.Context) {
  933. var req fms.IncomeListReq
  934. if e := c.BindQuery(&req); e != nil {
  935. err, ok := e.(validator.ValidationErrors)
  936. if !ok {
  937. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  938. return
  939. }
  940. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  941. return
  942. }
  943. //收入统计
  944. var incomeList models.CensusIncomeChartResp
  945. ch := make(chan models.CensusIncomeChartResp, 1)
  946. go getCensusIncomeListV2(ch, req)
  947. for v := range ch {
  948. incomeList = v
  949. close(ch)
  950. }
  951. // 是否导出
  952. if req.IsExport == 1 {
  953. ExportIncomeList(c, incomeList.DataList)
  954. return
  955. }
  956. resp.OkData("获取成功", incomeList, c)
  957. }
  958. func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeListReq) (incomeChart models.CensusIncomeChartResp, err error) {
  959. defer func() {
  960. if err != nil {
  961. global.LOG.Error(err)
  962. if err != utils.ErrNoRow {
  963. go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
  964. }
  965. }
  966. ch <- incomeChart
  967. }()
  968. //todayStr := utils.GetToday("20060102")
  969. //key := "admin:home:fmsIncomeList:" + todayStr
  970. //
  971. //redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
  972. //if redisErr != nil {
  973. //获取最新的开票到款日期
  974. cond := ``
  975. historyCond := ``
  976. pars := make([]interface{}, 0)
  977. historyPars := make([]interface{}, 0)
  978. if req.SellerIds != "" {
  979. sellerIds := strings.Split(req.SellerIds, ",")
  980. cond += ` AND (a.seller_id in ? )`
  981. historyCond += ` AND (seller_id in ? )`
  982. pars = append(pars, sellerIds)
  983. historyPars = append(historyPars, sellerIds)
  984. }
  985. if req.CompanyType == 1 {
  986. cond += ` AND b.contract_type = 1 `
  987. historyCond += ` AND new_company = 1`
  988. } else if req.CompanyType == 2 {
  989. cond += ` AND b.contract_type IN (2,3,4) `
  990. historyCond += ` AND new_company = 0`
  991. }
  992. var latestTime time.Time
  993. invoiceItem, err := fms.GetLatestIncome(cond, pars)
  994. if err != nil && err != utils.ErrNoRow {
  995. err = fmt.Errorf("获取最新的开票或到款日期, Err: %s", err.Error())
  996. return
  997. }
  998. latestTime = invoiceItem.InvoiceDate
  999. if err == utils.ErrNoRow {
  1000. historyItem, e := fms.GetLatestHistoryIncome(historyCond, historyPars)
  1001. if e != nil && e != utils.ErrNoRow {
  1002. err = fmt.Errorf("获取最新的历史开票或到款日期, Err: %s", err.Error())
  1003. return
  1004. }
  1005. latestTime = historyItem.InvoiceDate
  1006. }
  1007. if latestTime.IsZero() {
  1008. latestTime = time.Now()
  1009. }
  1010. latestTime = latestTime.AddDate(0, 0, -latestTime.Day()+1)
  1011. var dateSlice []string
  1012. var totalMoneySlice, prevTotalMoneySlice []float64
  1013. var yoySlice []string
  1014. var yearNum, monthNum int
  1015. var reqStartDate, reqEndDate time.Time
  1016. historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  1017. if req.StartDate != "" && req.EndDate != "" {
  1018. st := fmt.Sprint(req.StartDate, "-01 00:00:00")
  1019. ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
  1020. reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
  1021. reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
  1022. if reqEndDate.After(latestTime) {
  1023. yearNum = latestTime.Year() - reqStartDate.Year()
  1024. monthNum = int(latestTime.Month() - reqStartDate.Month())
  1025. } else {
  1026. yearNum = reqEndDate.Year() - reqStartDate.Year()
  1027. monthNum = int(reqEndDate.Month() - reqStartDate.Month())
  1028. }
  1029. } else {
  1030. yearNum = latestTime.Year() - 2020
  1031. monthNum = int(latestTime.Month() - 1)
  1032. }
  1033. if yearNum < 0 {
  1034. yearNum = -yearNum
  1035. }
  1036. if monthNum < 0 {
  1037. monthNum = -monthNum
  1038. }
  1039. numMonth := yearNum*12 + monthNum //共存在多少个月
  1040. //if req.ListParam == "1" {
  1041. // numMonth = numMonth / 3
  1042. //} else if req.ListParam == "2" {
  1043. // numMonth = numMonth / 6
  1044. //} else if req.ListParam == "3" {
  1045. // numMonth = numMonth / 12
  1046. //}
  1047. // 累计值
  1048. var accumulate float64
  1049. var partAccumulate float64
  1050. var historyAccumulate float64
  1051. var partHistoryAccumulate float64
  1052. //dataList := make([]*fms.IncomeSummaryItem, 0)
  1053. //historydataList := make([]*fms.IncomeSummaryItem, 0)
  1054. fmt.Println("numMonth:", numMonth)
  1055. fmt.Println("InvoiceDate:", latestTime)
  1056. var j int
  1057. for i := 0; i <= numMonth; i++ {
  1058. //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  1059. var endDateTime time.Time
  1060. var prevEndDateTime time.Time
  1061. var prevStartDate, prevEndDate string
  1062. var startDate, endDate string
  1063. //开始日期
  1064. if req.StartDate != "" && req.EndDate != "" {
  1065. startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
  1066. prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
  1067. } else {
  1068. startDate = latestTime.AddDate(0, i-numMonth, 0).Format("2006-01")
  1069. prevStartDate = latestTime.AddDate(-1, i-numMonth, 0).Format("2006-01")
  1070. }
  1071. startDate = fmt.Sprint(startDate, "-01")
  1072. prevStartDate = fmt.Sprint(prevStartDate, "-01")
  1073. startDateTime, _ := time.Parse(utils.FormatDate, startDate)
  1074. prevStartDateTime, _ := time.Parse(utils.FormatDate, prevStartDate)
  1075. //结束日期
  1076. if req.StartDate != "" && req.EndDate != "" {
  1077. endDateTime = reqStartDate.AddDate(0, i+1, -1)
  1078. prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
  1079. if reqEndDate.After(latestTime) {
  1080. endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
  1081. prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
  1082. }
  1083. } else {
  1084. endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
  1085. prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
  1086. }
  1087. endDate = endDateTime.Format(utils.FormatDate)
  1088. prevEndDate = prevEndDateTime.Format(utils.FormatDate)
  1089. cond := `1 = 1`
  1090. histrtyCond := `1 = 1`
  1091. pars := make([]interface{}, 0)
  1092. historyPars := make([]interface{}, 0)
  1093. prevCond := `1 = 1`
  1094. prevHistoryCond := `1 = 1`
  1095. prevPars := make([]interface{}, 0)
  1096. prevHistoryPars := make([]interface{}, 0)
  1097. if req.CompanyType == 1 {
  1098. cond += ` AND b.contract_type = 1 `
  1099. prevCond += ` AND b.contract_type = 1 `
  1100. histrtyCond += ` AND new_company = 1 `
  1101. prevHistoryCond += ` AND new_company = 1 `
  1102. } else if req.CompanyType == 2 {
  1103. cond += ` AND b.contract_type IN (2,3,4) `
  1104. prevCond += ` AND b.contract_type IN (2,3,4) `
  1105. histrtyCond += ` AND new_company = 0 `
  1106. prevHistoryCond += ` AND new_company = 0 `
  1107. }
  1108. if req.SellerIds != "" {
  1109. sellerIds := strings.Split(req.SellerIds, ",")
  1110. cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  1111. pars = append(pars, sellerIds, sellerIds)
  1112. prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  1113. prevPars = append(prevPars, sellerIds, sellerIds)
  1114. histrtyCond += ` AND seller_id in ? `
  1115. prevHistoryCond += ` AND seller_id in ? `
  1116. historyPars = append(historyPars, sellerIds)
  1117. prevHistoryPars = append(prevHistoryPars, sellerIds)
  1118. }
  1119. //fmt.Println("i:",i)
  1120. //fmt.Println("j:",j)
  1121. {
  1122. //本期
  1123. st := fmt.Sprint(startDate, " 00:00:00")
  1124. ed := fmt.Sprint(endDate, " 23:59:59")
  1125. //校验日期,分段查询
  1126. if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  1127. //全部走新查询
  1128. //fmt.Println("新查询")
  1129. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1130. pars = append(pars, st, ed, st, ed)
  1131. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1132. if e != nil {
  1133. return
  1134. }
  1135. // 开票到款金额合计(换算后)
  1136. var amountTotal float64
  1137. if len(summaryIds) > 0 {
  1138. amountCond := `a.id IN ? `
  1139. amountPars := make([]interface{}, 0)
  1140. amountPars = append(amountPars, summaryIds)
  1141. if req.SellerIds != "" {
  1142. sellerIds := strings.Split(req.SellerIds, ",")
  1143. amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1144. amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  1145. amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  1146. } else {
  1147. amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1148. amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  1149. amountPars = append(amountPars, st, ed, st, ed)
  1150. }
  1151. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1152. if e != nil {
  1153. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1154. return
  1155. }
  1156. //dataList = append(dataList, results...)
  1157. var amountSum float64
  1158. for _, result := range results {
  1159. incomeChart.DataList = append(incomeChart.DataList, result)
  1160. amountSum += result.Amount
  1161. fmt.Println("result.Amount:", result.Amount)
  1162. }
  1163. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1164. accumulate += amountTotal
  1165. partAccumulate += amountTotal
  1166. }
  1167. if i == j || i == numMonth {
  1168. if req.ListParam == "4" {
  1169. totalMoneySlice = append(totalMoneySlice, accumulate)
  1170. if startDateTime.Month() == 12 {
  1171. accumulate = 0
  1172. }
  1173. } else if req.ListParam == "0" {
  1174. totalMoneySlice = append(totalMoneySlice, amountTotal)
  1175. } else if i > 0 || i == numMonth {
  1176. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  1177. fmt.Println("partAccumulate:", partAccumulate)
  1178. partAccumulate = 0.0
  1179. }
  1180. }
  1181. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  1182. //全部走旧查询
  1183. //fmt.Println("旧查询")
  1184. //fmt.Println("st:",st)
  1185. //fmt.Println("ed:",ed)
  1186. histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1187. historyPars = append(historyPars, st, ed)
  1188. //fmt.Println("st:",st)
  1189. //fmt.Println("ed:",ed)
  1190. // 开票到款金额合计(换算后)
  1191. var amountTotal float64
  1192. results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
  1193. if e != nil {
  1194. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1195. return
  1196. }
  1197. var amountSum float64
  1198. //dataList = append(dataList, results...)
  1199. for _, result := range results {
  1200. incomeChart.DataList = append(incomeChart.DataList, result)
  1201. amountSum += result.Amount
  1202. }
  1203. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1204. accumulate += amountTotal
  1205. partAccumulate += amountTotal
  1206. if i == j || i == numMonth {
  1207. if req.ListParam == "4" {
  1208. totalMoneySlice = append(totalMoneySlice, accumulate)
  1209. if startDateTime.Month() == 12 {
  1210. accumulate = 0
  1211. }
  1212. } else if req.ListParam == "0" {
  1213. totalMoneySlice = append(totalMoneySlice, amountTotal)
  1214. } else if i > 0 || i == numMonth {
  1215. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  1216. partAccumulate = 0.0
  1217. }
  1218. }
  1219. //fmt.Println("partAccumulate:",partAccumulate)
  1220. }
  1221. }
  1222. { //去年同期,用于计算同比值
  1223. prevSt := fmt.Sprint(prevStartDate, " 00:00:00")
  1224. prevEd := fmt.Sprint(prevEndDate, " 23:59:59")
  1225. //校验日期,分段查询
  1226. if prevStartDateTime.After(historyTime) || prevStartDateTime.Equal(historyTime) {
  1227. //全部走新查询
  1228. prevCond += ` AND ((b.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1229. prevPars = append(prevPars, prevSt, prevEd, prevSt, prevEd)
  1230. prevSummaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(prevCond, prevPars)
  1231. if e != nil {
  1232. return
  1233. }
  1234. // 开票到款金额合计(换算后)
  1235. var prevAmountTotal float64
  1236. if len(prevSummaryIds) > 0 {
  1237. amountCond := `a.id IN ? `
  1238. amountPars := make([]interface{}, 0)
  1239. amountPars = append(amountPars, prevSummaryIds)
  1240. if req.SellerIds != "" {
  1241. sellerIds := strings.Split(req.SellerIds, ",")
  1242. amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1243. amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  1244. amountPars = append(amountPars, sellerIds, prevSt, prevEd, sellerIds, prevSt, prevEd)
  1245. } else {
  1246. amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1247. amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  1248. amountPars = append(amountPars, prevSt, prevEd)
  1249. }
  1250. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1251. if e != nil {
  1252. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1253. return
  1254. }
  1255. var amountSum float64
  1256. //historydataList = append(historydataList, results...)
  1257. for _, result := range results {
  1258. amountSum += result.Amount
  1259. }
  1260. prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1261. historyAccumulate += prevAmountTotal
  1262. partHistoryAccumulate += prevAmountTotal
  1263. }
  1264. if i == j || i == numMonth {
  1265. if req.ListParam == "4" {
  1266. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1267. if prevStartDateTime.Month() == 12 {
  1268. historyAccumulate = 0
  1269. }
  1270. } else if req.ListParam == "0" {
  1271. prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal)
  1272. } else if i > 0 || i == numMonth {
  1273. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
  1274. fmt.Println("partHistoryAccumulate:", partHistoryAccumulate)
  1275. partHistoryAccumulate = 0.0
  1276. }
  1277. }
  1278. } else if prevEndDateTime.Before(historyTime) || prevEndDateTime.Equal(historyTime) {
  1279. //全部走旧查询
  1280. fmt.Println("prevSt:", prevSt)
  1281. fmt.Println("prevEd:", prevEd)
  1282. prevHistoryCond += ` AND (invoice_time BETWEEN ? AND ?)`
  1283. prevHistoryPars = append(prevHistoryPars, prevSt, prevEd)
  1284. // 开票到款金额合计(换算后)
  1285. var amountTotal float64
  1286. results, e := fms.GetIncomeHistory(prevHistoryCond, prevHistoryPars)
  1287. if e != nil {
  1288. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1289. return
  1290. }
  1291. //historydataList = append(historydataList, results...)
  1292. var amountSum float64
  1293. for _, result := range results {
  1294. amountSum += result.Amount
  1295. }
  1296. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1297. historyAccumulate += amountTotal
  1298. partHistoryAccumulate += amountTotal
  1299. if i == j || i == numMonth {
  1300. if req.ListParam == "4" {
  1301. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1302. if prevStartDateTime.Month() == 12 {
  1303. historyAccumulate = 0
  1304. }
  1305. } else if req.ListParam == "0" {
  1306. prevTotalMoneySlice = append(prevTotalMoneySlice, amountTotal)
  1307. } else if i > 0 || i == numMonth {
  1308. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
  1309. fmt.Println("partHistoryAccumulate:", partHistoryAccumulate)
  1310. partHistoryAccumulate = 0.0
  1311. }
  1312. }
  1313. //fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
  1314. }
  1315. if req.ListParam == "1" && i == j {
  1316. if i == 0 {
  1317. dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01"))
  1318. j = j + 2
  1319. } else {
  1320. dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01"))
  1321. j = j + 3
  1322. }
  1323. } else if req.ListParam == "2" && i == j {
  1324. if i == 0 {
  1325. dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01"))
  1326. j = j + 5
  1327. } else {
  1328. dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01"))
  1329. j = j + 6
  1330. }
  1331. } else if req.ListParam == "3" && i == j {
  1332. if i == 0 {
  1333. dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
  1334. j = j + 11
  1335. } else {
  1336. dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
  1337. j = j + 12
  1338. }
  1339. } else if i == j {
  1340. dateSlice = append(dateSlice, startDateTime.Format("06/01"))
  1341. j++
  1342. }
  1343. }
  1344. }
  1345. fmt.Println("prevTotalMoneySlice:", len(prevTotalMoneySlice))
  1346. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  1347. //计算同比值
  1348. for i := range prevTotalMoneySlice {
  1349. var yoy float64
  1350. var yoyStr string
  1351. //fmt.Println("1:", prevTotalMoneySlice[i])
  1352. //fmt.Println("2:", totalMoneySlice[i])
  1353. //fmt.Println("3:", totalMoneySlice[i]-prevTotalMoneySlice[i])
  1354. totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalMoneySlice[i]), 64)
  1355. if prevTotalMoneySlice[i] != 0 && totalMoneySlice[i] != 0 {
  1356. yoy = (totalMoneySlice[i] - prevTotalMoneySlice[i]) / prevTotalMoneySlice[i]
  1357. yoyStr = fmt.Sprintf("%.4f", yoy)
  1358. if i == len(prevTotalMoneySlice)-1 && i > 0 && req.ListParam == "3" {
  1359. fmt.Println("totalMoneySlice[i-1]:", totalMoneySlice[i-1])
  1360. yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
  1361. yoyStr = fmt.Sprintf("%.4f", yoy)
  1362. }
  1363. }
  1364. yoySlice = append(yoySlice, yoyStr)
  1365. }
  1366. incomeChart.Title = "开票到款统计图"
  1367. incomeChart.Date = dateSlice
  1368. incomeChart.TotalMoney = totalMoneySlice
  1369. incomeChart.PrevTotalMoney = prevTotalMoneySlice
  1370. incomeChart.Yoy = yoySlice
  1371. //redisJsonData, err := json.Marshal(incomeChart)
  1372. //if err == nil {
  1373. // global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  1374. //}
  1375. //} else {
  1376. // err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  1377. // if err != nil {
  1378. // fmt.Println("近两年的收入统计数据,json转换失败")
  1379. // }
  1380. //}
  1381. return
  1382. }
  1383. // ExportIncomeList 导出业务收入统计列表
  1384. func ExportIncomeList(c *gin.Context, list []*fms.IncomeSummaryItem) {
  1385. // 生成Excel文件
  1386. xlsxFile := xlsx.NewFile()
  1387. style := xlsx.NewStyle()
  1388. alignment := xlsx.Alignment{
  1389. Horizontal: "center",
  1390. Vertical: "center",
  1391. WrapText: true,
  1392. }
  1393. style.Alignment = alignment
  1394. style.ApplyAlignment = true
  1395. sheetName := "业务收入统计表"
  1396. sheet, err := xlsxFile.AddSheet(sheetName)
  1397. if err != nil {
  1398. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1399. return
  1400. }
  1401. // 数据表头
  1402. rowTitle := []string{"开票日期", "新客户(1)", "客户名称", "金额", "销售"}
  1403. titleRow := sheet.AddRow()
  1404. for i := range rowTitle {
  1405. v := titleRow.AddCell()
  1406. v.SetString(rowTitle[i])
  1407. v.SetStyle(style)
  1408. }
  1409. incomeSummaryItemList := make(fms.IncomeSummaryItemList, 0)
  1410. incomeSummaryItemList = list
  1411. sort.Sort(incomeSummaryItemList)
  1412. // 填充数据
  1413. for _, v := range incomeSummaryItemList {
  1414. dataRow := sheet.AddRow()
  1415. dataRow.AddCell().SetString(v.InvoiceDate.Format(utils.FormatDate)) // 开票日期
  1416. newCompany := 0
  1417. if v.ContractType == 1 {
  1418. newCompany = 1
  1419. }
  1420. dataRow.AddCell().SetString(strconv.Itoa(newCompany)) // 新客户
  1421. dataRow.AddCell().SetString(v.CompanyName) // 客户名称
  1422. dataRow.AddCell().SetString(fmt.Sprint(v.Amount)) // 金额
  1423. dataRow.AddCell().SetString(v.SellerName) // 销售员
  1424. }
  1425. // 输出文件
  1426. var buffer bytes.Buffer
  1427. _ = xlsxFile.Write(&buffer)
  1428. content := bytes.NewReader(buffer.Bytes())
  1429. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1430. fileName := sheetName + randStr + ".xlsx"
  1431. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1432. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1433. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1434. }
  1435. // List
  1436. // @Title 未开票统计列表
  1437. // @Description 未开票统计列表
  1438. // @Param Keyword query string false "关键词"
  1439. // @Param SellGroupId query int false "销售组别ID"
  1440. // @Param ServiceType query int false "套餐类型"
  1441. // @Param StartDate query string false "合同开始日期"
  1442. // @Param EndDate query string false "合同结束日期"
  1443. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1444. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1445. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1446. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1447. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1448. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'start_date':开票日 、 'end_date':到款日"
  1449. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1450. // @Success 200 {object} fms.ContractRegisterItem
  1451. // @router /census/invoice_payment/not_invoice/list [get]
  1452. func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
  1453. var req fms.InvoicePaymentCensusListReq
  1454. if e := c.BindQuery(&req); e != nil {
  1455. err, ok := e.(validator.ValidationErrors)
  1456. if !ok {
  1457. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1458. return
  1459. }
  1460. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1461. return
  1462. }
  1463. cond := `1 = 1`
  1464. pars := make([]interface{}, 0)
  1465. noSummaryCond := `1 = 1`
  1466. noSummaryPars := make([]interface{}, 0)
  1467. // 客户姓名/合同编号
  1468. if req.Keyword != "" {
  1469. kw := "%" + req.Keyword + "%"
  1470. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1471. pars = append(pars, kw, kw)
  1472. noSummaryCond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1473. noSummaryPars = append(noSummaryPars, kw, kw)
  1474. }
  1475. if req.SellerIds != "" {
  1476. sellerIds := strings.Split(req.SellerIds, ",")
  1477. cond += ` AND (c.seller_id in ?)`
  1478. pars = append(pars, sellerIds)
  1479. noSummaryCond += ` AND (b.seller_id in ? OR b.rai_seller_id in ?)`
  1480. noSummaryPars = append(noSummaryPars, sellerIds, sellerIds)
  1481. }
  1482. // 套餐筛选
  1483. if req.ServiceTypes != "" {
  1484. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1485. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1486. if e != nil {
  1487. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  1488. return
  1489. }
  1490. if len(tempRegisterIds) > 0 {
  1491. cond += ` AND a.register_id IN ?`
  1492. pars = append(pars, tempRegisterIds)
  1493. noSummaryCond += ` AND b.register_id IN ? `
  1494. noSummaryPars = append(noSummaryPars, tempRegisterIds)
  1495. } else {
  1496. cond += ` AND 1 = 2`
  1497. noSummaryCond += ` AND 1 = 2`
  1498. }
  1499. }
  1500. if req.ListParam == 1 {
  1501. cond += ` AND a.service_product_id = 1 `
  1502. noSummaryCond += ` AND b.product_ids = '1' `
  1503. } else if req.ListParam == 2 {
  1504. cond += ` AND a.service_product_id = 2 `
  1505. noSummaryCond += ` AND b.product_ids = '2' `
  1506. }
  1507. // 开票到款日期
  1508. //if req.TimeType != 0 {
  1509. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  1510. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1511. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1512. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  1513. // pars = append(pars, st, ed)
  1514. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  1515. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1516. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1517. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  1518. // pars = append(pars, st, ed)
  1519. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  1520. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1521. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1522. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  1523. // pars = append(pars, st, ed, st, ed)
  1524. // }
  1525. //} else if req.StartDate != "" && req.EndDate != "" {
  1526. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1527. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1528. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1529. // pars = append(pars, st, ed, st, ed)
  1530. //}
  1531. //if req.HasInvoice == "1" {
  1532. // cond += ` AND a.invoice_id > 0 `
  1533. //}else if req.HasInvoice == "0" {
  1534. // cond += ` AND a.invoice_id = 0 `
  1535. //}
  1536. //
  1537. //if req.HasPayment == "1" {
  1538. // cond += ` AND a.payment_id > 0 `
  1539. //}else if req.HasPayment == "0" {
  1540. // cond += ` AND a.payment_id = 0 `
  1541. //}
  1542. page := new(base.Page)
  1543. page.SetPageSize(req.PageSize)
  1544. page.SetCurrent(req.Current)
  1545. //排序
  1546. if req.SortType == "" {
  1547. req.SortType = "asc"
  1548. }
  1549. if req.SortType != "desc" && req.SortType != "asc" {
  1550. resp.Fail("排序类型不正确", c)
  1551. return
  1552. }
  1553. if req.SortParam == "" {
  1554. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
  1555. } else if req.SortParam == "start_date" {
  1556. if req.SortType == "asc" {
  1557. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
  1558. } else {
  1559. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
  1560. }
  1561. } else if req.SortParam == "end_date" {
  1562. if req.SortType == "asc" {
  1563. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: true})
  1564. } else {
  1565. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: false})
  1566. }
  1567. } else {
  1568. resp.Fail("排序字段不正确", c)
  1569. return
  1570. }
  1571. if req.IsExport == 1 {
  1572. page.SetPageSize(10000)
  1573. page.SetCurrent(1)
  1574. }
  1575. cond += ` AND b.contract_amount <> b.invoiced_amount AND b.is_deleted = 0 `
  1576. registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
  1577. if e != nil {
  1578. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  1579. return
  1580. }
  1581. queryRegisterIds := make([]int, 0)
  1582. for _, v := range registerList {
  1583. queryRegisterIds = append(queryRegisterIds, v.ContractRegisterId)
  1584. }
  1585. fmt.Println("queryRegisterIds:",queryRegisterIds)
  1586. results := new(fms.NotInvoicePaymentCensusResp)
  1587. if len(queryRegisterIds) > 0 {
  1588. // 获取合同的套餐金额信息
  1589. serviceAmountList, e := fms.GetContractServiceAmountByContractRegisterIds(queryRegisterIds)
  1590. if e != nil {
  1591. resp.FailData("获取合同的套餐金额信息失败", "Err:"+e.Error(), c)
  1592. return
  1593. }
  1594. serviceAmountMap := make(map[int]map[int]*fms.ContractServiceAmount, 0)
  1595. for _, v := range serviceAmountList {
  1596. if _, ok := serviceAmountMap[v.ContractRegisterId]; !ok {
  1597. serviceAmountMap[v.ContractRegisterId] = make(map[int]*fms.ContractServiceAmount)
  1598. }
  1599. serviceAmountMap[v.ContractRegisterId][v.ProductId] = v
  1600. fmt.Println("ServiceAmount,ProductId",v.ServiceAmount,v.ProductId,v.ContractRegisterId)
  1601. }
  1602. // 获取汇总数据IDs, 用于查询合计数据
  1603. summaryIdsCond := cond
  1604. summaryIdsPars := pars
  1605. if req.SellerIds != "" {
  1606. sellerIds := strings.Split(req.SellerIds, ",")
  1607. summaryIdsCond += ` AND c.seller_id in ? AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
  1608. summaryIdsPars = append(summaryIdsPars, sellerIds)
  1609. } else {
  1610. summaryIdsCond += ` AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
  1611. summaryIdsPars = append(summaryIdsPars)
  1612. }
  1613. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  1614. if e != nil {
  1615. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  1616. return
  1617. }
  1618. //fmt.Println("summaryIds:",summaryIds)
  1619. var listErr, totalErr, totalGroupErr error
  1620. wg := sync.WaitGroup{}
  1621. // 响应列表
  1622. respList := make([]*fms.NotPaymentCensusItem, 0)
  1623. summaryList := make([]*fms.NotInvoicePaymentSummaryItem, 0)
  1624. wg.Add(1)
  1625. go func() {
  1626. defer wg.Done()
  1627. // 获取汇总数据
  1628. summaryCond := cond
  1629. summaryCond += ` AND a.register_id IN ?`
  1630. summaryPars := pars
  1631. summaryPars = append(summaryPars, queryRegisterIds)
  1632. //if req.SellerIds != "" {
  1633. // sellerIds := strings.Split(req.SellerIds, ",")
  1634. // summaryCond += ` AND c.seller_id in ? AND a.payment_id <> 0 AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
  1635. // summaryPars = append(summaryPars, sellerIds, sellerIds)
  1636. //} else {
  1637. // summaryCond += ` AND a.payment_id <> 0 AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
  1638. // summaryPars = append(summaryPars)
  1639. //}
  1640. summaryData, e := fms.GetNotInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  1641. if e != nil {
  1642. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  1643. return
  1644. }
  1645. notQueryRegisterIds := make([]int, 0)
  1646. for _, v := range summaryData {
  1647. notQueryRegisterIds = append(notQueryRegisterIds, v.ContractRegisterId)
  1648. }
  1649. noSummaryCond += ` AND b.is_deleted = 0`
  1650. noSummaryCond += ` AND b.contract_register_id IN ? AND b.contract_register_id NOT IN ?`
  1651. noSummaryPars = append(noSummaryPars, queryRegisterIds, notQueryRegisterIds)
  1652. noSummaryData, e := fms.GetNoInvoicePaymentCensusData(noSummaryCond, noSummaryPars)
  1653. if e != nil {
  1654. resp.FailMsg("获取失败", "GetNoInvoicePaymentCensusData, Err: "+e.Error(), c)
  1655. return
  1656. }
  1657. fmt.Println("len(summaryData)",len(summaryData))
  1658. fmt.Println("len(noSummaryData)",len(noSummaryData))
  1659. summaryList = summaryData
  1660. NotInvoiceSummaryMap := make(map[int][]*fms.NotInvoicePaymentSummaryItem, 0)
  1661. for _, v := range summaryList {
  1662. fmt.Println("v2.ContractRegisterId",v.ContractRegisterId)
  1663. NotInvoiceSummaryMap[v.RegisterId] = append(NotInvoiceSummaryMap[v.RegisterId], v)
  1664. }
  1665. summaryList = append(summaryList, noSummaryData...)
  1666. //summaryIds := make([]int, 0)
  1667. //paymentIds := make([]int, 0)
  1668. //for i := range summaryList {
  1669. // //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  1670. //}
  1671. fmt.Println("len(NotInvoiceSummaryMap):",len(NotInvoiceSummaryMap))
  1672. for _, v := range summaryList {
  1673. fmt.Println("ProductIds:",v.ProductIds)
  1674. fmt.Println("ContractRegisterId:",v.ContractRegisterId)
  1675. fmt.Println("ServiceProductId:",v.ServiceProductId)
  1676. fmt.Println("RegisterId:",v.RegisterId)
  1677. fmt.Println("len(NotInvoiceSummaryMap[v.RegisterId]):",len(NotInvoiceSummaryMap[v.ContractRegisterId]))
  1678. if (v.ProductIds == "1,2" || v.ProductIds == "2,1") && len(NotInvoiceSummaryMap[v.ContractRegisterId]) == 1 {
  1679. if v.ServiceProductId == 1 {
  1680. item := fms.NotInvoicePaymentSummaryItem{
  1681. NotInvoicedAmountTotal: serviceAmountMap[v.ContractRegisterId][2].ServiceAmount,
  1682. ServiceProductId: 2,
  1683. }
  1684. item.SellerId = v.SellerId
  1685. item.SellerName = v.SellerName
  1686. item.RaiSellerId = v.RaiSellerId
  1687. item.RaiSellerName = v.RaiSellerName
  1688. NotInvoiceSummaryMap[v.ContractRegisterId] = append(NotInvoiceSummaryMap[v.ContractRegisterId], &item)
  1689. } else {
  1690. item := fms.NotInvoicePaymentSummaryItem{
  1691. NotInvoicedAmountTotal: serviceAmountMap[v.ContractRegisterId][1].ServiceAmount,
  1692. ServiceProductId: 1,
  1693. }
  1694. item.SellerId = v.SellerId
  1695. item.SellerName = v.SellerName
  1696. item.RaiSellerId = v.RaiSellerId
  1697. item.RaiSellerName = v.RaiSellerName
  1698. NotInvoiceSummaryMap[v.ContractRegisterId] = append(NotInvoiceSummaryMap[v.ContractRegisterId], &item)
  1699. }
  1700. } else if (v.ProductIds == "1,2" || v.ProductIds == "2,1") && len(NotInvoiceSummaryMap[v.ContractRegisterId]) == 0 {
  1701. item1 := fms.NotInvoicePaymentSummaryItem{
  1702. NotInvoicedAmountTotal: serviceAmountMap[v.ContractRegisterId][2].ServiceAmount,
  1703. ServiceProductId: 2,
  1704. }
  1705. item1.SellerId = v.SellerId
  1706. item1.SellerName = v.SellerName
  1707. item1.RaiSellerId = v.RaiSellerId
  1708. item1.RaiSellerName = v.RaiSellerName
  1709. NotInvoiceSummaryMap[v.ContractRegisterId] = append(NotInvoiceSummaryMap[v.ContractRegisterId], &item1)
  1710. item2 := fms.NotInvoicePaymentSummaryItem{
  1711. NotInvoicedAmountTotal: serviceAmountMap[v.ContractRegisterId][1].ServiceAmount,
  1712. ServiceProductId: 1,
  1713. }
  1714. item2.SellerId = v.SellerId
  1715. item2.SellerName = v.SellerName
  1716. item2.RaiSellerId = v.RaiSellerId
  1717. item2.RaiSellerName = v.RaiSellerName
  1718. NotInvoiceSummaryMap[v.ContractRegisterId] = append(NotInvoiceSummaryMap[v.ContractRegisterId], &item2)
  1719. } else if v.ProductIds == "1" && len(NotInvoiceSummaryMap[v.ContractRegisterId]) == 0 {
  1720. item1 := fms.NotInvoicePaymentSummaryItem{
  1721. NotInvoicedAmountTotal: serviceAmountMap[v.ContractRegisterId][1].ServiceAmount,
  1722. ServiceProductId: 1,
  1723. }
  1724. item1.SellerId = v.SellerId
  1725. item1.SellerName = v.SellerName
  1726. item1.RaiSellerId = v.RaiSellerId
  1727. item1.RaiSellerName = v.RaiSellerName
  1728. NotInvoiceSummaryMap[v.ContractRegisterId] = append(NotInvoiceSummaryMap[v.ContractRegisterId], &item1)
  1729. } else if v.ProductIds == "2" && len(NotInvoiceSummaryMap[v.ContractRegisterId]) == 0 {
  1730. item2 := fms.NotInvoicePaymentSummaryItem{
  1731. NotInvoicedAmountTotal: serviceAmountMap[v.ContractRegisterId][2].ServiceAmount,
  1732. ServiceProductId: 2,
  1733. }
  1734. item2.SellerId = v.SellerId
  1735. item2.SellerName = v.SellerName
  1736. item2.RaiSellerId = v.RaiSellerId
  1737. item2.RaiSellerName = v.RaiSellerName
  1738. NotInvoiceSummaryMap[v.ContractRegisterId] = append(NotInvoiceSummaryMap[v.ContractRegisterId], &item2)
  1739. }
  1740. }
  1741. // 重组汇总数据
  1742. summaryMap := make(map[int][]*fms.NotPaymentCensusInfo)
  1743. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  1744. if e != nil {
  1745. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  1746. return
  1747. }
  1748. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  1749. for i := range sellerList {
  1750. sellerMap[sellerList[i].SellerId] = sellerList[i]
  1751. }
  1752. sellerTypeMap := map[int]string{2: "FICC销售", 5: "权益销售"}
  1753. // 货币列表
  1754. currencyOB := new(fms.CurrencyUnit)
  1755. currencyCond := `enable = 1`
  1756. currencyPars := make([]interface{}, 0)
  1757. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1758. if e != nil {
  1759. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  1760. return
  1761. }
  1762. unitMap := make(map[string]string)
  1763. for i := range currencyList {
  1764. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1765. }
  1766. for registerId, list := range NotInvoiceSummaryMap {
  1767. for _, v := range list {
  1768. fmt.Println("v.ServiceProductId:",v.ServiceProductId)
  1769. fmt.Println("v.NotInvoicedAmountTotal:",v.NotInvoicedAmountTotal)
  1770. if v.NotInvoicedAmountTotal == 0 {
  1771. v.NotInvoicedAmountTotal = serviceAmountMap[registerId][v.ServiceProductId].ServiceAmount - v.InvoiceAmountTotal
  1772. }
  1773. if v.SellerId > 0 && v.RaiSellerId > 0 {
  1774. if v.ServiceProductId == 1 {
  1775. v.SellerGroupId = sellerMap[v.SellerId].GroupId
  1776. v.SellerGroupName = sellerMap[v.SellerId].GroupName
  1777. v.SellerType = sellerTypeMap[sellerMap[v.SellerId].DepartmentId]
  1778. } else {
  1779. v.RaiSellerGroupId = sellerMap[v.RaiSellerId].GroupId
  1780. v.RaiSellerGroupName = sellerMap[v.RaiSellerId].GroupName
  1781. v.SellerType = sellerTypeMap[sellerMap[v.RaiSellerId].DepartmentId]
  1782. }
  1783. } else if v.SellerId > 0 && v.RaiSellerId == 0 {
  1784. v.SellerGroupId = sellerMap[v.SellerId].GroupId
  1785. v.SellerGroupName = sellerMap[v.SellerId].GroupName
  1786. v.SellerType = sellerTypeMap[sellerMap[v.SellerId].DepartmentId]
  1787. } else if v.SellerId == 0 && v.RaiSellerId > 0 {
  1788. v.RaiSellerGroupId = sellerMap[v.RaiSellerId].GroupId
  1789. v.RaiSellerGroupName = sellerMap[v.RaiSellerId].GroupName
  1790. v.SellerType = sellerTypeMap[sellerMap[v.RaiSellerId].DepartmentId]
  1791. }
  1792. }
  1793. }
  1794. // 响应列表
  1795. for i := range registerList {
  1796. v := new(fms.NotPaymentCensusItem)
  1797. v.SummaryId = registerList[i].SummaryId
  1798. v.ContractRegisterId = registerList[i].ContractRegisterId
  1799. v.CompanyName = registerList[i].CompanyName
  1800. v.NewCompany = registerList[i].NewCompany
  1801. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  1802. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  1803. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  1804. v.ContractType = registerList[i].ContractType
  1805. v.ContractCode = registerList[i].ContractCode
  1806. v.ContractAmount = registerList[i].ContractAmount
  1807. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  1808. v.ProductIds = registerList[i].ProductIds
  1809. if registerList[i].InvoicedAmount != 0 {
  1810. v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].InvoicedAmount
  1811. } else {
  1812. v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].PaymentAmount
  1813. }
  1814. v.NotInvoiceList = NotInvoiceSummaryMap[registerList[i].ContractRegisterId]
  1815. respList = append(respList, v)
  1816. }
  1817. }()
  1818. // 开票到款金额合计(换算后)
  1819. var invoiceTotal, paymentTotal, contractAmount float64
  1820. wg.Add(1)
  1821. go func() {
  1822. defer wg.Done()
  1823. if len(summaryIds) == 0 {
  1824. return
  1825. }
  1826. amountTotalCond := `a.id IN ?`
  1827. amountTotalPars := make([]interface{}, 0)
  1828. amountTotalPars = append(amountTotalPars, summaryIds)
  1829. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  1830. if e != nil {
  1831. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  1832. return
  1833. }
  1834. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  1835. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  1836. if e != nil {
  1837. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  1838. return
  1839. }
  1840. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  1841. }()
  1842. // 分币种金额统计
  1843. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1844. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1845. contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1846. wg.Add(1)
  1847. go func() {
  1848. defer wg.Done()
  1849. currencyOB := new(fms.CurrencyUnit)
  1850. currencyCond := `enable = 1`
  1851. currencyPars := make([]interface{}, 0)
  1852. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1853. if e != nil {
  1854. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1855. return
  1856. }
  1857. unitMap := make(map[string]string)
  1858. for i := range currencyList {
  1859. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1860. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1861. Name: currencyList[i].Name,
  1862. UnitName: currencyList[i].UnitName,
  1863. Code: currencyList[i].Code,
  1864. FlagImg: currencyList[i].FlagImg,
  1865. })
  1866. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1867. Name: currencyList[i].Name,
  1868. UnitName: currencyList[i].UnitName,
  1869. Code: currencyList[i].Code,
  1870. FlagImg: currencyList[i].FlagImg,
  1871. })
  1872. contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1873. Name: currencyList[i].Name,
  1874. UnitName: currencyList[i].UnitName,
  1875. Code: currencyList[i].Code,
  1876. FlagImg: currencyList[i].FlagImg,
  1877. })
  1878. }
  1879. if len(summaryIds) == 0 {
  1880. return
  1881. }
  1882. totalGroupCond := `a.id IN ?`
  1883. totalGroupPars := make([]interface{}, 0)
  1884. totalGroupPars = append(totalGroupPars, summaryIds)
  1885. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  1886. if e != nil {
  1887. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  1888. return
  1889. }
  1890. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  1891. if e != nil {
  1892. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  1893. return
  1894. }
  1895. invoiceSumMap := make(map[string]float64)
  1896. paymentSumMap := make(map[string]float64)
  1897. contractSumMap := make(map[string]float64)
  1898. for i := range invoiceSumGroup {
  1899. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  1900. continue
  1901. }
  1902. for i := range paymentSumGroup {
  1903. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  1904. continue
  1905. }
  1906. for i := range invoiceCurrencyTotals {
  1907. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  1908. invoiceCurrencyTotals[i].Amount = a
  1909. }
  1910. for i := range paymentCurrencyTotals {
  1911. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  1912. paymentCurrencyTotals[i].Amount = a
  1913. }
  1914. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  1915. if err != nil {
  1916. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  1917. return
  1918. }
  1919. for _, v := range contractRegisters {
  1920. if v.RMBRate == 0.0 {
  1921. v.RMBRate = 1
  1922. }
  1923. amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
  1924. contractAmount += amount
  1925. contractSumMap[v.CurrencyUnit] = v.ContractAmount
  1926. }
  1927. for _, v := range contractAmountCurrencyTotals {
  1928. v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  1929. }
  1930. }()
  1931. wg.Wait()
  1932. if listErr != nil {
  1933. resp.FailMsg("获取失败", listErr.Error(), c)
  1934. return
  1935. }
  1936. if totalErr != nil {
  1937. resp.FailMsg("获取失败", totalErr.Error(), c)
  1938. return
  1939. }
  1940. if totalGroupErr != nil {
  1941. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  1942. return
  1943. }
  1944. results.DataList = respList
  1945. results.InvoiceTotal = invoiceTotal
  1946. results.PaymentTotal = paymentTotal
  1947. results.NotInvoiceTotal = contractAmount - invoiceTotal
  1948. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  1949. results.PaymentCurrencyTotal = paymentCurrencyTotals
  1950. results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
  1951. }
  1952. // 是否导出
  1953. if req.IsExport == 1 {
  1954. ExportNotInvoiceCensusList(c, results)
  1955. return
  1956. }
  1957. page.SetTotal(total)
  1958. baseData := new(base.BaseData)
  1959. baseData.SetPage(page)
  1960. baseData.SetList(results)
  1961. resp.OkData("获取成功", baseData, c)
  1962. }
  1963. // List
  1964. // @Title 开票未到款统计列表
  1965. // @Description 开票未到款统计列表
  1966. // @Param Keyword query string false "关键词"
  1967. // @Param SellGroupId query int false "销售组别ID"
  1968. // @Param ServiceType query int false "套餐类型"
  1969. // @Param StartDate query string false "合同开始日期"
  1970. // @Param EndDate query string false "合同结束日期"
  1971. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1972. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1973. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1974. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1975. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1976. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  1977. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1978. // @Success 200 {object} fms.ContractRegisterItem
  1979. // @router /census/invoice_payment/not_payment/list [get]
  1980. func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
  1981. var req fms.InvoicePaymentCensusListReq
  1982. if e := c.BindQuery(&req); e != nil {
  1983. err, ok := e.(validator.ValidationErrors)
  1984. if !ok {
  1985. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1986. return
  1987. }
  1988. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1989. return
  1990. }
  1991. cond := `1 = 1`
  1992. pars := make([]interface{}, 0)
  1993. // 客户姓名/销售
  1994. if req.Keyword != "" {
  1995. kw := "%" + req.Keyword + "%"
  1996. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1997. pars = append(pars, kw, kw)
  1998. }
  1999. if req.SellerIds != "" {
  2000. sellerIds := strings.Split(req.SellerIds, ",")
  2001. cond += ` AND (c.seller_id in ?)`
  2002. pars = append(pars, sellerIds)
  2003. }
  2004. // 套餐筛选
  2005. if req.ServiceTypes != "" {
  2006. serviceTypes := strings.Split(req.ServiceTypes, ",")
  2007. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  2008. if e != nil {
  2009. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  2010. return
  2011. }
  2012. if len(tempRegisterIds) > 0 {
  2013. cond += ` AND a.register_id IN ?`
  2014. pars = append(pars, tempRegisterIds)
  2015. } else {
  2016. cond += ` AND 1 = 2`
  2017. }
  2018. }
  2019. if req.ListParam == 1 {
  2020. cond += ` AND a.service_product_id = 1 `
  2021. } else if req.ListParam == 2 {
  2022. cond += ` AND a.service_product_id = 2 `
  2023. }
  2024. // 开票到款日期
  2025. //if req.TimeType != 0 {
  2026. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  2027. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2028. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2029. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  2030. // pars = append(pars, st, ed)
  2031. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  2032. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2033. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2034. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  2035. // pars = append(pars, st, ed)
  2036. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  2037. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2038. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2039. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  2040. // pars = append(pars, st, ed, st, ed)
  2041. // }
  2042. //} else if req.StartDate != "" && req.EndDate != "" {
  2043. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2044. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2045. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  2046. // pars = append(pars, st, ed, st, ed)
  2047. //}
  2048. //if req.HasInvoice == "1" {
  2049. // cond += ` AND a.invoice_id > 0 `
  2050. //}else if req.HasInvoice == "0" {
  2051. // cond += ` AND a.invoice_id = 0 `
  2052. //}
  2053. //
  2054. //if req.HasPayment == "1" {
  2055. // cond += ` AND a.payment_id > 0 `
  2056. //}else if req.HasPayment == "0" {
  2057. // cond += ` AND a.payment_id = 0 `
  2058. //}
  2059. page := new(base.Page)
  2060. page.SetPageSize(req.PageSize)
  2061. page.SetCurrent(req.Current)
  2062. //排序
  2063. if req.SortType == "" {
  2064. req.SortType = "asc"
  2065. }
  2066. if req.SortType != "desc" && req.SortType != "asc" {
  2067. resp.Fail("排序类型不正确", c)
  2068. return
  2069. }
  2070. if req.SortType == "asc" {
  2071. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: true})
  2072. } else {
  2073. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: false})
  2074. }
  2075. if req.IsExport == 1 {
  2076. page.SetPageSize(10000)
  2077. page.SetCurrent(1)
  2078. }
  2079. registerList, total, e := fms.GetNoPaymentCensusPageList(page, cond, pars)
  2080. if e != nil {
  2081. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  2082. return
  2083. }
  2084. queryRegisterIds := make([]int, 0)
  2085. for i := range registerList {
  2086. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  2087. }
  2088. fmt.Println("queryRegisterIds:", queryRegisterIds)
  2089. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  2090. results := new(fms.NotInvoicePaymentCensusResp)
  2091. if len(queryRegisterIds) > 0 {
  2092. // 获取汇总数据IDs, 用于查询合计数据
  2093. summaryIdsCond := cond
  2094. summaryIdsPars := pars
  2095. if req.SellerIds != "" {
  2096. sellerIds := strings.Split(req.SellerIds, ",")
  2097. summaryIdsCond += ` AND c.seller_id in ? AND a.invoice_id <> 0 AND (c.origin_amount > d.origin_amount OR d.origin_amount IS NULL) `
  2098. summaryIdsPars = append(summaryIdsPars, sellerIds)
  2099. } else {
  2100. summaryIdsCond += ` AND a.invoice_id <> 0 AND (c.origin_amount > d.origin_amount OR d.origin_amount IS NULL) `
  2101. summaryIdsPars = append(summaryIdsPars)
  2102. }
  2103. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  2104. if e != nil {
  2105. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  2106. return
  2107. }
  2108. //fmt.Println("summaryIds:",summaryIds)
  2109. var listErr, totalErr, totalGroupErr error
  2110. wg := sync.WaitGroup{}
  2111. // 响应列表
  2112. respList := make([]*fms.NotPaymentCensusItem, 0)
  2113. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  2114. wg.Add(1)
  2115. go func() {
  2116. defer wg.Done()
  2117. // 获取汇总数据
  2118. summaryCond := cond
  2119. summaryCond += ` AND a.register_id IN ?`
  2120. summaryPars := pars
  2121. summaryPars = append(summaryPars, queryRegisterIds)
  2122. if req.SellerIds != "" {
  2123. sellerIds := strings.Split(req.SellerIds, ",")
  2124. summaryCond += ` AND c.seller_id in ? AND a.invoice_id <> 0 AND (c.origin_amount > d.origin_amount OR d.origin_amount IS NULL) `
  2125. summaryPars = append(summaryPars, sellerIds)
  2126. } else {
  2127. summaryCond += ` AND a.invoice_id <> 0 AND (c.origin_amount > d.origin_amount OR d.origin_amount IS NULL) `
  2128. summaryPars = append(summaryPars)
  2129. }
  2130. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  2131. if e != nil {
  2132. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  2133. return
  2134. }
  2135. summaryList = summaryData
  2136. //summaryIds := make([]int, 0)
  2137. //for i := range summaryList {
  2138. // //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  2139. //
  2140. //}
  2141. //// 获取合同的套餐金额信息
  2142. //serviceAmountList, e := fms.GetContractServiceAmountByContractRegisterIds(queryRegisterIds)
  2143. //if e != nil {
  2144. // resp.FailData("获取合同的套餐金额信息失败", "Err:"+e.Error(), c)
  2145. // return
  2146. //}
  2147. //serviceAmountMap := make(map[int]map[int]*fms.ContractServiceAmount, 0)
  2148. //for _, v := range serviceAmountList {
  2149. // if _, ok := serviceAmountMap[v.ContractRegisterId]; !ok {
  2150. // serviceAmountMap[v.ContractRegisterId] = make(map[int]*fms.ContractServiceAmount)
  2151. // }
  2152. // serviceAmountMap[v.ContractRegisterId][v.ProductId] = v
  2153. //}
  2154. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  2155. if e != nil {
  2156. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  2157. return
  2158. }
  2159. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  2160. for i := range sellerList {
  2161. sellerMap[sellerList[i].SellerId] = sellerList[i]
  2162. }
  2163. sellerTypeMap := map[int]int{crm.SellerDepartmentId: 1, crm.RaiSellerDepartmentId: 2}
  2164. // 重组汇总数据
  2165. summaryMap := make(map[int][]*fms.NotPaymentCensusInfo)
  2166. for i := range summaryList {
  2167. v := new(fms.NotPaymentCensusInfo)
  2168. v.InvoiceId = summaryList[i].InvoiceId
  2169. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  2170. v.InvoiceAmount = summaryList[i].InvoiceAmount
  2171. v.SellerId = summaryList[i].SellerId
  2172. v.SellerName = summaryList[i].SellerName
  2173. v.SellerGroupId = summaryList[i].SellerGroupId
  2174. v.SellerGroupName = summaryList[i].SellerGroupName
  2175. v.SellerType = sellerTypeMap[sellerMap[summaryList[i].SellerId].DepartmentId]
  2176. v.PaymentId = summaryList[i].PaymentId
  2177. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  2178. v.PaymentAmount = summaryList[i].PaymentAmount
  2179. v.PayType = summaryList[i].PayType
  2180. v.NotPaymentAmount = summaryList[i].InvoiceAmount - summaryList[i].PaymentAmount
  2181. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  2182. }
  2183. // 货币列表
  2184. currencyOB := new(fms.CurrencyUnit)
  2185. currencyCond := `enable = 1`
  2186. currencyPars := make([]interface{}, 0)
  2187. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  2188. if e != nil {
  2189. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  2190. return
  2191. }
  2192. unitMap := make(map[string]string)
  2193. for i := range currencyList {
  2194. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  2195. }
  2196. // 响应列表
  2197. for i := range registerList {
  2198. v := new(fms.NotPaymentCensusItem)
  2199. v.SummaryId = registerList[i].SummaryId
  2200. v.ContractRegisterId = registerList[i].ContractRegisterId
  2201. v.CompanyName = registerList[i].CompanyName
  2202. v.NewCompany = registerList[i].NewCompany
  2203. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  2204. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  2205. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  2206. v.ContractType = registerList[i].ContractType
  2207. v.ContractCode = registerList[i].ContractCode
  2208. v.ContractAmount = registerList[i].ContractAmount
  2209. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  2210. v.ProductIds = registerList[i].ProductIds
  2211. v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].InvoicedAmount
  2212. respList = append(respList, v)
  2213. }
  2214. }()
  2215. // 开票到款金额合计(换算后)
  2216. var invoiceTotal, paymentTotal float64
  2217. wg.Add(1)
  2218. go func() {
  2219. defer wg.Done()
  2220. if len(summaryIds) == 0 {
  2221. return
  2222. }
  2223. amountTotalCond := `a.id IN ?`
  2224. amountTotalPars := make([]interface{}, 0)
  2225. amountTotalPars = append(amountTotalPars, summaryIds)
  2226. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  2227. if e != nil {
  2228. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  2229. return
  2230. }
  2231. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  2232. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  2233. if e != nil {
  2234. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  2235. return
  2236. }
  2237. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  2238. }()
  2239. // 分币种金额统计
  2240. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2241. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2242. notpaymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2243. //contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2244. wg.Add(1)
  2245. go func() {
  2246. defer wg.Done()
  2247. currencyOB := new(fms.CurrencyUnit)
  2248. currencyCond := `enable = 1`
  2249. currencyPars := make([]interface{}, 0)
  2250. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  2251. if e != nil {
  2252. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  2253. return
  2254. }
  2255. unitMap := make(map[string]string)
  2256. for i := range currencyList {
  2257. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  2258. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2259. Name: currencyList[i].Name,
  2260. UnitName: currencyList[i].UnitName,
  2261. Code: currencyList[i].Code,
  2262. FlagImg: currencyList[i].FlagImg,
  2263. })
  2264. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2265. Name: currencyList[i].Name,
  2266. UnitName: currencyList[i].UnitName,
  2267. Code: currencyList[i].Code,
  2268. FlagImg: currencyList[i].FlagImg,
  2269. })
  2270. notpaymentCurrencyTotals = append(notpaymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2271. Name: currencyList[i].Name,
  2272. UnitName: currencyList[i].UnitName,
  2273. Code: currencyList[i].Code,
  2274. FlagImg: currencyList[i].FlagImg,
  2275. })
  2276. //contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2277. // Name: currencyList[i].Name,
  2278. // UnitName: currencyList[i].UnitName,
  2279. // Code: currencyList[i].Code,
  2280. // FlagImg: currencyList[i].FlagImg,
  2281. //})
  2282. }
  2283. if len(summaryIds) == 0 {
  2284. return
  2285. }
  2286. totalGroupCond := `a.id IN ?`
  2287. totalGroupPars := make([]interface{}, 0)
  2288. totalGroupPars = append(totalGroupPars, summaryIds)
  2289. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  2290. if e != nil {
  2291. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  2292. return
  2293. }
  2294. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  2295. if e != nil {
  2296. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  2297. return
  2298. }
  2299. invoiceSumMap := make(map[string]float64)
  2300. paymentSumMap := make(map[string]float64)
  2301. //contractSumMap := make(map[string]float64)
  2302. for i := range invoiceSumGroup {
  2303. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  2304. continue
  2305. }
  2306. for i := range paymentSumGroup {
  2307. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  2308. continue
  2309. }
  2310. for i := range invoiceCurrencyTotals {
  2311. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  2312. invoiceCurrencyTotals[i].Amount = a
  2313. }
  2314. for i := range paymentCurrencyTotals {
  2315. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  2316. paymentCurrencyTotals[i].Amount = a
  2317. }
  2318. for i := range notpaymentCurrencyTotals {
  2319. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]-paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  2320. notpaymentCurrencyTotals[i].Amount = a
  2321. }
  2322. //contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  2323. //if err != nil {
  2324. // resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  2325. // return
  2326. //}
  2327. //for _, v := range contractRegisters {
  2328. // if v.RMBRate == 0.0 {
  2329. // v.RMBRate = 1
  2330. // }
  2331. // amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
  2332. // contractAmount += amount
  2333. // contractSumMap[v.CurrencyUnit] = v.ContractAmount
  2334. //}
  2335. //for _, v := range contractAmountCurrencyTotals {
  2336. // v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  2337. //}
  2338. }()
  2339. wg.Wait()
  2340. if listErr != nil {
  2341. resp.FailMsg("获取失败", listErr.Error(), c)
  2342. return
  2343. }
  2344. if totalErr != nil {
  2345. resp.FailMsg("获取失败", totalErr.Error(), c)
  2346. return
  2347. }
  2348. if totalGroupErr != nil {
  2349. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  2350. return
  2351. }
  2352. results.DataList = respList
  2353. results.InvoiceTotal = invoiceTotal
  2354. results.PaymentTotal = paymentTotal
  2355. results.NotPaymentTotal = invoiceTotal - paymentTotal
  2356. results.NotPaymentCurrencyTotal = notpaymentCurrencyTotals
  2357. }
  2358. // 是否导出
  2359. if req.IsExport == 1 {
  2360. ExportNotPaymentCensusList(c, results)
  2361. return
  2362. }
  2363. page.SetTotal(total)
  2364. baseData := new(base.BaseData)
  2365. baseData.SetPage(page)
  2366. baseData.SetList(results)
  2367. resp.OkData("获取成功", baseData, c)
  2368. }
  2369. // ExportNotInvoiceCensusList 导出未开票统计列表
  2370. func ExportNotInvoiceCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  2371. list := results.DataList
  2372. if len(list) == 0 {
  2373. resp.Fail("列表数据为空", c)
  2374. return
  2375. }
  2376. // 生成Excel文件
  2377. xlsxFile := xlsx.NewFile()
  2378. style := xlsx.NewStyle()
  2379. alignment := xlsx.Alignment{
  2380. Horizontal: "center",
  2381. Vertical: "center",
  2382. WrapText: true,
  2383. }
  2384. style.Alignment = alignment
  2385. style.ApplyAlignment = true
  2386. sheetName := "未开票统计表"
  2387. sheet, err := xlsxFile.AddSheet(sheetName)
  2388. if err != nil {
  2389. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  2390. return
  2391. }
  2392. // 数据表头
  2393. rowTitle := []string{"序号", "客户名称", "合同编号", "合同开始时间", "合同结束时间", "合同金额", "金额单位", "未开票金额", "套餐类型", "销售", "销售组别", "销售类型"}
  2394. titleRow := sheet.AddRow()
  2395. for i := range rowTitle {
  2396. v := titleRow.AddCell()
  2397. v.SetString(rowTitle[i])
  2398. v.SetStyle(style)
  2399. }
  2400. incomeSummaryItemList := make([]*fms.NotPaymentCensusItem, 0)
  2401. incomeSummaryItemList = list
  2402. // 填充数据
  2403. for i, v := range incomeSummaryItemList {
  2404. dataRow := sheet.AddRow()
  2405. cell := dataRow.AddCell()
  2406. cell.VMerge = 1
  2407. cell.SetString(strconv.Itoa(i + 1)) //序号
  2408. cell.SetString(v.CompanyName) // 客户名称
  2409. cell.SetString(v.ContractCode) // 合同编号
  2410. cell.SetString(v.StartDate) // 合同开始时间
  2411. cell.SetString(v.EndDate) // 合同结束时间
  2412. cell.SetString(v.ContractCode) // 合同金额
  2413. cell.SetString(v.UnitName) // 金额单位
  2414. sheet.AddRow()
  2415. for _, item := range v.NotInvoiceList {
  2416. cell.SetString(fmt.Sprintf("%.2f", item.NotInvoicedAmountTotal)) // 未开票金额
  2417. cell.SetString(strconv.Itoa(item.ServiceProductId)) // 套餐类型
  2418. cell.SetString(item.SellerName) // 销售
  2419. cell.SetString(item.SellerGroupName) // 销售组别
  2420. cell.SetString(item.SellerType) // 销售类型
  2421. }
  2422. }
  2423. // 输出文件
  2424. var buffer bytes.Buffer
  2425. _ = xlsxFile.Write(&buffer)
  2426. content := bytes.NewReader(buffer.Bytes())
  2427. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2428. fileName := sheetName + randStr + ".xlsx"
  2429. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  2430. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2431. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  2432. }
  2433. // ExportNotPaymentCensusList 导出开票未到款统计列表
  2434. func ExportNotPaymentCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  2435. list := results.DataList
  2436. if len(list) == 0 {
  2437. resp.Fail("列表数据为空", c)
  2438. return
  2439. }
  2440. // 生成Excel文件
  2441. xlsxFile := xlsx.NewFile()
  2442. style := xlsx.NewStyle()
  2443. alignment := xlsx.Alignment{
  2444. Horizontal: "center",
  2445. Vertical: "center",
  2446. WrapText: true,
  2447. }
  2448. style.Alignment = alignment
  2449. style.ApplyAlignment = true
  2450. sheet, err := xlsxFile.AddSheet("开票未到款统计")
  2451. if err != nil {
  2452. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  2453. return
  2454. }
  2455. _ = sheet.SetColWidth(1, 1, 30)
  2456. _ = sheet.SetColWidth(3, 3, 30)
  2457. // 前三行-开票金额合计
  2458. rowA := sheet.AddRow()
  2459. cellAA := rowA.AddCell()
  2460. cellAA.SetString(fmt.Sprintf("开票未到款合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  2461. rowBData := "已开票金额:"
  2462. for _, v := range results.InvoiceCurrencyTotal {
  2463. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  2464. }
  2465. rowB := sheet.AddRow()
  2466. rowB.AddCell().SetString(rowBData)
  2467. sheet.AddRow()
  2468. // 表头, 套餐动态获取
  2469. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  2470. "销售组别", "销售类型"}
  2471. serviceTempCond := ``
  2472. serviceTempPars := make([]interface{}, 0)
  2473. serviceTempOB := new(fms.ContractServiceTemplate)
  2474. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  2475. if e != nil {
  2476. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  2477. return
  2478. }
  2479. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  2480. var serviceTempShow []*fms.ContractServiceTemplate
  2481. for i := range serviceTempList {
  2482. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  2483. }
  2484. for i := range serviceTempList {
  2485. if serviceTempList[i].ProductId == 1 {
  2486. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2487. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2488. } else if serviceTempList[i].ProductId == 2 {
  2489. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid == 0) {
  2490. } else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  2491. } else {
  2492. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2493. }
  2494. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  2495. continue
  2496. }
  2497. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  2498. continue
  2499. }
  2500. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2501. }
  2502. }
  2503. hi := 0
  2504. insertHi := 0
  2505. titleRow := sheet.AddRow()
  2506. for i := range rowTitle {
  2507. v := titleRow.AddCell()
  2508. v.SetString(rowTitle[i])
  2509. v.SetStyle(style)
  2510. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  2511. v.VMerge = 1
  2512. hi++
  2513. } else {
  2514. if insertHi == 0 {
  2515. insertHi = hi
  2516. }
  2517. v.HMerge = 1
  2518. titleRow.AddCell().SetString("")
  2519. }
  2520. }
  2521. // 新增一行放主观和客观
  2522. specialRow := sheet.AddRow()
  2523. for i := 0; i < hi; i++ {
  2524. if i == insertHi {
  2525. v1 := specialRow.AddCell()
  2526. v1.SetString("45万")
  2527. v1.SetStyle(style)
  2528. v1 = specialRow.AddCell()
  2529. v1.SetString("70万")
  2530. v1.SetStyle(style)
  2531. for j := 0; j < 8; j++ {
  2532. if j%2 == 0 {
  2533. v1 = specialRow.AddCell()
  2534. v1.SetString("主观")
  2535. v1.SetStyle(style)
  2536. } else {
  2537. v1 = specialRow.AddCell()
  2538. v1.SetString("客观")
  2539. v1.SetStyle(style)
  2540. }
  2541. }
  2542. } else {
  2543. v := specialRow.AddCell()
  2544. v.SetString("")
  2545. }
  2546. }
  2547. newCompanyMap := map[int]string{0: "否", 1: "是"}
  2548. sellerTypeMap := map[int]string{1: "FICC销售", 2: "权益销售"}
  2549. for k, v := range list {
  2550. dataRow := sheet.AddRow()
  2551. // 前四个单元格根据每行开票到款条数向下合并
  2552. l := len(v.InvoicePaymentList)
  2553. mergeRowNum := l - 1
  2554. // 序号
  2555. sortNum := k + 1
  2556. colA := dataRow.AddCell()
  2557. colA.VMerge = mergeRowNum
  2558. colA.SetString(fmt.Sprint(sortNum))
  2559. // 客户名称
  2560. colB := dataRow.AddCell()
  2561. colB.VMerge = mergeRowNum
  2562. colB.SetString(v.CompanyName)
  2563. // 是否新客户
  2564. colC := dataRow.AddCell()
  2565. colC.VMerge = mergeRowNum
  2566. colC.SetString(newCompanyMap[v.NewCompany])
  2567. // 合同有效期
  2568. colD := dataRow.AddCell()
  2569. colD.VMerge = mergeRowNum
  2570. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  2571. // 开票到款信息
  2572. for k2, v2 := range v.InvoicePaymentList {
  2573. rowData := []string{
  2574. v2.InvoiceDate, // 开票日
  2575. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  2576. v2.PaymentDate, // 到款日
  2577. fmt.Sprint(v2.PaymentAmount), // 到款金额
  2578. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  2579. v2.SellerName, // 销售
  2580. v2.SellerGroupName, // 组别
  2581. sellerTypeMap[v2.SellerType], // 销售类型
  2582. }
  2583. // 套餐金额信息
  2584. serviceTempShowAmount := make(map[int]string)
  2585. for i := range serviceTempShow {
  2586. for s2 := range v2.ServiceAmountList {
  2587. item := v2.ServiceAmountList[s2]
  2588. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  2589. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  2590. break
  2591. } else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  2592. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount / 2)
  2593. break
  2594. }
  2595. }
  2596. }
  2597. for i := range serviceTempShow {
  2598. sa := ""
  2599. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  2600. sa = am
  2601. }
  2602. rowData = append(rowData, sa)
  2603. }
  2604. // 首行开票到款
  2605. if k2 == 0 {
  2606. for i := range rowData {
  2607. dataRow.AddCell().SetString(rowData[i])
  2608. }
  2609. continue
  2610. }
  2611. // 其他行开票到款, 加四列空的单元格用于合并
  2612. dataRowExtra := sheet.AddRow()
  2613. for i := 0; i < 4; i++ {
  2614. dataRowExtra.AddCell()
  2615. }
  2616. for i := range rowData {
  2617. dataRowExtra.AddCell().SetString(rowData[i])
  2618. }
  2619. }
  2620. }
  2621. // 输出文件
  2622. var buffer bytes.Buffer
  2623. _ = xlsxFile.Write(&buffer)
  2624. content := bytes.NewReader(buffer.Bytes())
  2625. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2626. fileName := "商品到款统计_" + randStr + ".xlsx"
  2627. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  2628. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2629. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  2630. }