智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_data_statisstics.go 78 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/enum"
  7. "applet/app/utils"
  8. "applet/app/utils/logx"
  9. "archive/zip"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/360EntSecGroup-Skylar/excelize"
  13. "github.com/syyongx/php2go"
  14. "io"
  15. "os"
  16. "strconv"
  17. "time"
  18. )
  19. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  20. var titleList []string
  21. var fileName string
  22. var now = time.Now()
  23. xlsx := excelize.NewFile()
  24. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  25. switch req.Kind {
  26. case 1:
  27. //订单列表
  28. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  29. //查找订单数据
  30. sess := db.Db.Desc("nursing_home_package_ord.id")
  31. if req.EnterpriseId != 0 {
  32. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  33. }
  34. if req.StartDate != "" {
  35. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  36. }
  37. if req.EndDate != "" {
  38. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  39. }
  40. var m []*db.NursingHomePackageOrdWithUserIdentity
  41. _, err := sess.
  42. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  43. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  44. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  45. FindAndCount(&m)
  46. if err != nil {
  47. logx.Error(err)
  48. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  49. return
  50. }
  51. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  52. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  53. j := 2 //表头被第一行用了,只能从第二行开始
  54. for _, v := range m {
  55. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  56. switch v.NursingHomePackageOrd.Kind {
  57. case 1:
  58. kind = "按年购买"
  59. break
  60. case 2:
  61. kind = "按月购买"
  62. break
  63. case 3:
  64. kind = "按天购买"
  65. break
  66. case 4:
  67. kind = "补餐"
  68. break
  69. }
  70. var state string //0:待支付 1:支付成功 2:支付失败
  71. switch v.NursingHomePackageOrd.State {
  72. case enum.NursingHomePackageOrdStateForWait:
  73. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  74. break
  75. case enum.NursingHomePackageOrdStateForSuccess:
  76. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  77. break
  78. case enum.NursingHomePackageOrdStateForFail:
  79. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  80. break
  81. }
  82. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  83. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  84. j++
  85. }
  86. break
  87. case 2:
  88. //退款申请表
  89. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  90. var m []*db.NursingHomeUserRefundDayWithData
  91. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  92. if req.EnterpriseId != 0 {
  93. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  94. }
  95. if req.StartDate != "" {
  96. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  97. }
  98. if req.EndDate != "" {
  99. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  100. }
  101. _, err := sess.
  102. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  103. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  104. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  105. Join("LEFT", "user", "user_identity.uid = user.id").
  106. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  107. Desc("nursing_home_user_refund_day.id").
  108. FindAndCount(&m)
  109. if err != nil {
  110. logx.Error(err)
  111. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  112. return
  113. }
  114. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  115. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  116. j := 2 //表头被第一行用了,只能从第二行开始
  117. for _, v := range m {
  118. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  119. switch v.NursingHomeUserWithDay.Kind {
  120. case enum.NursingHomeUserWithDayKindForBreakfast:
  121. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  122. break
  123. case enum.NursingHomeUserWithDayKindForLunch:
  124. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  125. break
  126. case enum.NursingHomeUserWithDayKindForDinner:
  127. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  128. break
  129. }
  130. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  131. switch v.NursingHomeUserRefundDay.State {
  132. case enum.NursingHomeUserRefundDayStateForAuditing:
  133. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  134. break
  135. case enum.NursingHomeUserRefundDayStateForAuditPass:
  136. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  137. break
  138. case enum.NursingHomeUserRefundDayStateForAuditReject:
  139. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  140. break
  141. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  142. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  143. break
  144. }
  145. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  146. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  147. j++
  148. }
  149. xlsx.SaveAs("./static" + fileName + ".xlsx")
  150. break
  151. case 3:
  152. //预定统计表
  153. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  154. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  155. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  156. if req.EnterpriseId != 0 {
  157. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  158. }
  159. if req.StartDate != "" {
  160. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  161. }
  162. if req.EndDate != "" {
  163. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  164. }
  165. _, err := sess.
  166. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  167. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  168. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  169. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  170. if err != nil {
  171. logx.Error(err)
  172. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  173. return
  174. }
  175. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  176. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  177. j := 2 //表头被第一行用了,只能从第二行开始
  178. for _, v := range m {
  179. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  180. switch v.NursingHomePackageOrdForReserve.Kind {
  181. case enum.NursingHomeUserWithDayKindForBreakfast:
  182. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  183. break
  184. case enum.NursingHomeUserWithDayKindForLunch:
  185. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  186. break
  187. case enum.NursingHomeUserWithDayKindForDinner:
  188. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  189. break
  190. }
  191. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  192. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  193. j++
  194. }
  195. xlsx.SaveAs("./static" + fileName + ".xlsx")
  196. break
  197. }
  198. //新增数据
  199. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  200. nursingHomeExportRecordsDb.Set()
  201. marshal, _ := json.Marshal(req)
  202. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  203. Name: fileName,
  204. DownloadPath: fileName,
  205. Kind: req.Kind,
  206. ReqContent: string(marshal),
  207. CreateAt: now.Format("2006-01-02 15:04:05"),
  208. UpdateAt: now.Format("2006-01-02 15:04:05"),
  209. })
  210. }
  211. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  212. var titleList []string
  213. var fileName, downloadPath string
  214. var now = time.Now()
  215. switch req.Kind {
  216. case 1:
  217. //教师消费统计表
  218. xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
  219. fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  220. downloadPath = "/export/self_support_for_school/" + fileName
  221. //查询数据
  222. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  223. sess := db.Db.Desc("self_support_for_school_ord.id")
  224. if req.EnterpriseId != 0 {
  225. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  226. }
  227. if req.StartDate != "" {
  228. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  229. }
  230. if req.EndDate != "" {
  231. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  232. }
  233. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  234. _, err := sess.
  235. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  236. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  237. Join("LEFT", "user", "user.id = user_identity.uid").
  238. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  239. Join("LEFT", "class", "class_with_user.class_id = class.id").
  240. Join("LEFT", "grade", "class.grade_id = grade.id").
  241. FindAndCount(&m)
  242. if err != nil {
  243. logx.Error(err)
  244. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  245. return
  246. }
  247. //整合数据
  248. var data = map[string]map[string]map[string]map[string]interface{}{}
  249. for _, vv := range m {
  250. if data[vv.Enterprise.Name] == nil {
  251. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  252. }
  253. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  254. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  255. }
  256. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  257. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  258. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  259. }
  260. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  261. "id": vv.UserIdentity.IdNo,
  262. }
  263. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  264. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  265. }
  266. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  267. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  268. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  270. }
  271. xlsx.SetSheetRow("", "A1", &titleList)
  272. sheetIndex := 2 //第一个表是模板表
  273. j := 3 //表头前两行被占用了,只能从第三行开始
  274. for k, v := range data {
  275. xlsx.NewSheet(k) //1、新建表格
  276. xlsx.CopySheet(1, sheetIndex)
  277. for kk, vv := range v {
  278. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  279. vv["早餐"]["num"], vv["早餐"]["amount"],
  280. vv["午餐"]["num"], vv["午餐"]["amount"],
  281. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  282. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  283. vv["未知"]["num"], vv["未知"]["amount"],
  284. vv["合计"]["num"], vv["合计"]["amount"],
  285. })
  286. j++
  287. }
  288. sheetIndex++
  289. }
  290. xlsx.DeleteSheet("template") //删除模板表
  291. //将文件保存至服务器
  292. xlsx.SaveAs("./static" + downloadPath)
  293. break
  294. case 2:
  295. //商家分餐数据统计表
  296. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  297. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  298. downloadPath = "/export/self_support_for_school/" + fileName
  299. //查询出所有的设备
  300. var deviceList []*model.Device
  301. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  302. if err != nil {
  303. logx.Error(err)
  304. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  305. return
  306. }
  307. var deviceListMapping = map[string]model.Device{}
  308. for _, v := range deviceList {
  309. deviceListMapping[v.DeviceSn] = *v
  310. }
  311. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  312. sess := db.Db.Desc("self_support_for_school_ord.id")
  313. if req.EnterpriseId != 0 {
  314. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  315. }
  316. if req.StartDate != "" {
  317. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  318. }
  319. if req.EndDate != "" {
  320. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  321. }
  322. _, err = sess.
  323. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  324. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  325. Join("LEFT", "user", "user.id = user_identity.uid").
  326. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  327. Join("LEFT", "class", "class_with_user.class_id = class.id").
  328. Join("LEFT", "grade", "class.grade_id = grade.id").
  329. FindAndCount(&m)
  330. if err != nil {
  331. logx.Error(err)
  332. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  333. return
  334. }
  335. //整合数据
  336. var data = map[string]map[string]map[string]map[string]float64{}
  337. for _, vv := range m {
  338. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  339. if data[vv.Enterprise.Name] == nil {
  340. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  341. }
  342. if data[vv.Enterprise.Name][shopName] == nil {
  343. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  344. }
  345. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  346. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  347. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  348. }
  349. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  350. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  351. }
  352. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  353. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  354. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  355. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  356. }
  357. xlsx.SetSheetRow("", "A1", &titleList)
  358. sheetIndex := 2 //第一个表是模板表
  359. for k, v := range data {
  360. if k == "" {
  361. k = "未知"
  362. }
  363. xlsx.NewSheet(k) //1、新建表格
  364. xlsx.CopySheet(1, sheetIndex)
  365. j := 4 //表头前三行被占用了,只能从第四行开始
  366. for kk, vv := range v {
  367. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  368. vv["早餐"]["num"], vv["早餐"]["amount"],
  369. vv["午餐"]["num"], vv["午餐"]["amount"],
  370. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  371. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  372. vv["合计"]["num"], vv["合计"]["amount"],
  373. })
  374. j++
  375. }
  376. sheetIndex++
  377. }
  378. xlsx.DeleteSheet("template") //删除模板表
  379. //将文件保存至服务器
  380. xlsx.SaveAs("./static" + downloadPath)
  381. break
  382. }
  383. //新增数据
  384. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  385. selfSupportForSchoolExportRecordsDb.Set()
  386. marshal, _ := json.Marshal(req)
  387. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  388. Name: fileName,
  389. DownloadPath: downloadPath,
  390. Kind: req.Kind,
  391. ReqContent: string(marshal),
  392. CreateAt: now.Format("2006-01-02 15:04:05"),
  393. UpdateAt: now.Format("2006-01-02 15:04:05"),
  394. })
  395. }
  396. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  397. var titleList []string
  398. var fileName, downloadPath string
  399. var now = time.Now()
  400. for _, kind := range req.Kinds {
  401. xlsx := excelize.NewFile()
  402. switch kind {
  403. case 1:
  404. //订单列表
  405. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  406. downloadPath = "/export/central_kitchen_for_school/" + fileName
  407. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  408. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  409. //查找订单数据
  410. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  411. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  412. if req.EnterpriseId != 0 {
  413. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  414. }
  415. if req.StartDate != "" {
  416. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  417. }
  418. if req.EndDate != "" {
  419. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  420. }
  421. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  422. _, err := sess.
  423. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  424. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  425. Join("LEFT", "user", "user.id = user_identity.uid").
  426. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  427. Join("LEFT", "class", "class_with_user.class_id = class.id").
  428. Join("LEFT", "grade", "class.grade_id = grade.id").
  429. FindAndCount(&m)
  430. if err != nil {
  431. logx.Error(err)
  432. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  433. return
  434. }
  435. //查找预定数据
  436. var outTradeNoList []string
  437. for _, v := range m {
  438. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  439. }
  440. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  441. sess1 := db.Db.Desc("id")
  442. if req.EnterpriseId != 0 {
  443. sess1.And("enterprise_id = ?", req.EnterpriseId)
  444. }
  445. if len(req.MealKindList) > 0 {
  446. sess1.In("kind", req.MealKindList)
  447. }
  448. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  449. if err != nil {
  450. logx.Error(err)
  451. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  452. return
  453. }
  454. var exportOutTradeNoList []string
  455. for _, v := range mm {
  456. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  457. }
  458. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  459. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  460. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定天数", "订单价格", "支付时间", "订单状态"}
  461. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  462. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  463. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  464. if err != nil {
  465. logx.Error(err)
  466. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  467. panic(err)
  468. }
  469. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  470. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  471. j := 3 //表头前两行用了,只能从第三行开始
  472. for _, v := range m {
  473. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  474. continue
  475. }
  476. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  477. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  478. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  479. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  480. break
  481. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  482. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  483. break
  484. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  485. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  486. break
  487. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  488. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  489. break
  490. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  491. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  492. break
  493. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  494. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  495. break
  496. }
  497. //预定天数、预定日期
  498. var list []*model.CentralKitchenForSchoolUserWithDay
  499. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  500. if err1 != nil {
  501. logx.Error(err)
  502. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  503. return
  504. }
  505. var reserveDays string
  506. for _, v1 := range list {
  507. reserveDays += v1.Date
  508. }
  509. if v.Grade.Name == "" {
  510. v.Grade.Name = "教师"
  511. }
  512. if v.Class.Name == "" {
  513. v.Class.Name = "教师"
  514. }
  515. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  516. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  517. j++
  518. }
  519. //将文件保存至服务器
  520. xlsx.SaveAs("./static" + downloadPath)
  521. break
  522. case 2:
  523. //退款申请表
  524. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  525. downloadPath = "/export/central_kitchen_for_school/" + fileName
  526. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  527. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  528. //查找退款申请数据
  529. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  530. if req.EnterpriseId != 0 {
  531. sess.And("enterprise.id =?", req.EnterpriseId)
  532. }
  533. if req.StartDate != "" {
  534. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  535. }
  536. if req.EndDate != "" {
  537. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  538. }
  539. if len(req.MealKindList) > 0 {
  540. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  541. }
  542. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  543. _, err := sess.
  544. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  545. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  546. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  547. Join("LEFT", "user", "user_identity.uid = user.id").
  548. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  549. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  550. Join("LEFT", "class", "class_with_user.class_id = class.id").
  551. Join("LEFT", "grade", "class.grade_id = grade.id").
  552. Desc("central_kitchen_for_school_user_refund_day.id").
  553. FindAndCount(&m)
  554. if err != nil {
  555. logx.Error(err)
  556. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  557. return
  558. }
  559. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  560. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  561. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  562. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  563. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  564. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  565. if err != nil {
  566. logx.Error(err)
  567. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  568. panic(err)
  569. }
  570. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  571. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  572. j := 3 //表头前两行用了,只能从第三行开始
  573. for _, v := range m {
  574. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  575. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  576. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  577. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  578. break
  579. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  580. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  581. break
  582. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  583. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  584. break
  585. }
  586. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  587. switch v.CentralKitchenForSchoolUserRefundDay.State {
  588. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  589. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  590. break
  591. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  592. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  593. break
  594. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  595. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  596. break
  597. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  598. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  599. break
  600. }
  601. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  602. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  603. j++
  604. }
  605. //将文件保存至服务器
  606. xlsx.SaveAs("./static" + downloadPath)
  607. break
  608. case 3:
  609. //学校预定统计表
  610. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  611. downloadPath = "/export/central_kitchen_for_school/" + fileName
  612. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  613. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  614. if req.EnterpriseId != 0 {
  615. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  616. }
  617. if req.StartDate != "" {
  618. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ? and central_kitchen_for_school_package_ord_for_reserve.meal_time_end >= ?", req.StartDate, req.EndDate)
  619. }
  620. //if req.EndDate != "" {
  621. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  622. //}
  623. if len(req.MealKindList) > 0 {
  624. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  625. }
  626. _, err := sess.
  627. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  628. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  629. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  630. Join("LEFT", "user", "user.id = user_identity.uid").
  631. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  632. Join("LEFT", "class", "class_with_user.class_id = class.id").
  633. Join("LEFT", "grade", "class.grade_id = grade.id").
  634. FindAndCount(&m)
  635. if err != nil {
  636. logx.Error(err)
  637. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  638. return
  639. }
  640. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  641. centralKitchenForSchoolUserWithDayDb.Set(0)
  642. var list = map[string]map[string]map[string]map[string]int64{}
  643. var xlsFileName, xlsDownloadPath []string
  644. for _, v := range m {
  645. if list[v.Enterprise.Name] == nil {
  646. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  647. }
  648. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  649. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  650. }
  651. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  652. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  653. }
  654. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  655. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  656. req.StartDate, req.EndDate, req.MealKindList)
  657. if err2 != nil {
  658. logx.Error(err)
  659. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  660. return
  661. }
  662. time.Sleep(20 * time.Millisecond) // 20毫秒
  663. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  664. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  665. }
  666. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  667. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  668. }
  669. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  670. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  671. }
  672. }
  673. for k1, v1 := range list {
  674. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  675. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  676. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  677. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  678. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  679. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  680. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  681. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  682. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  683. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  684. if err != nil {
  685. logx.Error(err)
  686. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  687. panic(err)
  688. }
  689. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  690. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  691. j := 3 //表头前两行用了,只能从第三行开始
  692. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  693. tmpClassDb := db.ClassDb{}
  694. tmpClassDb.Set(0)
  695. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  696. if err != nil {
  697. logx.Error(err)
  698. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  699. panic(err)
  700. }
  701. for _, class := range *classList {
  702. for k2, v2 := range v1 {
  703. if k2 == "" {
  704. //若年级为空,则为教师餐
  705. k2 = "教师餐"
  706. }
  707. for k3, v3 := range v2 {
  708. if class.Name != k3 {
  709. continue
  710. }
  711. if k3 == "" {
  712. //若班级为空,则为教师餐
  713. k3 = "教师餐"
  714. }
  715. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  716. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  717. sumClassNum++
  718. sumBreakfast += v3["breakfast"]
  719. sumLunch += v3["lunch"]
  720. sumDinner += v3["dinner"]
  721. j++
  722. }
  723. }
  724. }
  725. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  726. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  727. //将文件保存至服务器
  728. err1 := xlsx.SaveAs(tmpDownloadPath)
  729. if err1 != nil {
  730. logx.Error(err1)
  731. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  732. panic(err1)
  733. }
  734. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  735. xlsFileName = append(xlsFileName, tmpFileName)
  736. }
  737. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  738. if err != nil {
  739. logx.Error(err)
  740. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  741. panic(err)
  742. }
  743. defer archive.Close()
  744. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  745. for k, v := range xlsDownloadPath {
  746. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  747. if err1 != nil {
  748. logx.Error(err1)
  749. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  750. panic(err1)
  751. }
  752. f, err1 := os.Open(v) //打开待压缩的文件
  753. if err1 != nil {
  754. logx.Error(err1)
  755. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  756. panic(err1)
  757. }
  758. if _, err1 = io.Copy(w, f); err != nil {
  759. logx.Error(err1)
  760. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  761. panic(err1)
  762. }
  763. if err1 != nil {
  764. logx.Error(err1)
  765. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  766. panic(err1)
  767. }
  768. f.Close()
  769. os.RemoveAll(v) //TODO::移除源文件
  770. }
  771. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  772. if err != nil {
  773. panic(err)
  774. }
  775. break
  776. case 4:
  777. //班级明细表
  778. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  779. downloadPath = "/export/central_kitchen_for_school/" + fileName
  780. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  781. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  782. if req.EnterpriseId != 0 {
  783. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  784. }
  785. if req.StartDate != "" {
  786. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ? and central_kitchen_for_school_package_ord_for_reserve.meal_time_end >= ?", req.StartDate, req.EndDate)
  787. }
  788. //if req.EndDate != "" {
  789. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  790. //}
  791. if len(req.MealKindList) > 0 {
  792. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  793. }
  794. _, err := sess.
  795. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  796. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  797. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  798. Join("LEFT", "user", "user.id = user_identity.uid").
  799. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  800. Join("LEFT", "class", "class_with_user.class_id = class.id").
  801. Join("LEFT", "grade", "class.grade_id = grade.id").
  802. Asc("class.id").
  803. FindAndCount(&m)
  804. if err != nil {
  805. logx.Error(err)
  806. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  807. return
  808. }
  809. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  810. var xlsFileName, xlsDownloadPath []string
  811. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  812. centralKitchenForSchoolUserWithDayDb.Set(0)
  813. for _, v := range m {
  814. if list[v.Enterprise.Name] == nil {
  815. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  816. }
  817. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  818. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  819. }
  820. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  821. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  822. }
  823. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  824. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  825. }
  826. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  827. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  828. req.StartDate, req.EndDate, req.MealKindList)
  829. if err2 != nil {
  830. logx.Error(err)
  831. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  832. return
  833. }
  834. if count == 0 {
  835. continue
  836. }
  837. time.Sleep(20 * time.Millisecond) // 20毫秒
  838. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  839. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  840. }
  841. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  842. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  843. }
  844. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  845. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  846. }
  847. }
  848. for k1, v1 := range list {
  849. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  850. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  851. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  852. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  853. //查询当前学校下的班级升序
  854. tmpEnterpriseDb := db.EnterpriseDb{}
  855. tmpEnterpriseDb.Set()
  856. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  857. if err11 != nil {
  858. logx.Error(err11)
  859. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  860. panic(err11)
  861. }
  862. tmpClassDb := db.ClassDb{}
  863. tmpClassDb.Set(0)
  864. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  865. if err != nil {
  866. logx.Error(err)
  867. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  868. panic(err)
  869. }
  870. for _, class := range *classList {
  871. for k2, v2 := range v1 {
  872. if k2 == "" {
  873. //若年级为空,则为教师餐
  874. continue
  875. }
  876. for k3, v3 := range v2 {
  877. if class.Name != k3 {
  878. continue
  879. }
  880. xlsx.NewSheet(k3)
  881. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  882. xlsx.SetSheetRow(k3, "A1", &titleList)
  883. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  884. xlsx.SetSheetRow(k3, "A2", &titleList)
  885. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  886. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  887. if err != nil {
  888. logx.Error(err)
  889. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  890. panic(err)
  891. }
  892. xlsx.SetCellStyle(k3, "A1", "D1", style)
  893. xlsx.SetColWidth(k3, "A", "D", 20)
  894. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  895. j := 3 //表头前两行用了,只能从第三行开始
  896. for k4, v4 := range v3 {
  897. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  898. continue
  899. }
  900. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  901. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  902. sumBreakfast += v4["breakfast"]
  903. sumLunch += v4["lunch"]
  904. sumDinner += v4["dinner"]
  905. j++
  906. }
  907. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  908. "合计", sumBreakfast, sumLunch, sumDinner})
  909. }
  910. }
  911. }
  912. //将文件保存至服务器
  913. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  914. err1 := xlsx.SaveAs(tmpDownloadPath)
  915. if err1 != nil {
  916. logx.Error(err1)
  917. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  918. panic(err1)
  919. }
  920. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  921. xlsFileName = append(xlsFileName, tmpFileName)
  922. }
  923. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  924. if err != nil {
  925. logx.Error(err)
  926. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  927. panic(err)
  928. }
  929. defer archive.Close()
  930. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  931. for k, v := range xlsDownloadPath {
  932. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  933. if err1 != nil {
  934. logx.Error(err1)
  935. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  936. panic(err1)
  937. }
  938. f, err1 := os.Open(v) //打开待压缩的文件
  939. if err1 != nil {
  940. logx.Error(err1)
  941. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  942. panic(err1)
  943. }
  944. if _, err1 = io.Copy(w, f); err != nil {
  945. logx.Error(err1)
  946. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  947. panic(err1)
  948. }
  949. if err1 != nil {
  950. logx.Error(err1)
  951. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  952. panic(err1)
  953. }
  954. f.Close()
  955. os.RemoveAll(v) //TODO::移除源文件
  956. }
  957. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  958. if err != nil {
  959. panic(err)
  960. }
  961. break
  962. case 5:
  963. //退款明细表
  964. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  965. downloadPath = "/export/central_kitchen_for_school/" + fileName
  966. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  967. if req.EnterpriseId != 0 {
  968. sess.And("enterprise.id =?", req.EnterpriseId)
  969. }
  970. if req.StartDate != "" {
  971. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  972. }
  973. if req.EndDate != "" {
  974. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  975. }
  976. if len(req.MealKindList) > 0 {
  977. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  978. }
  979. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  980. _, err := sess.
  981. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  982. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  983. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  984. Join("LEFT", "user", "user_identity.uid = user.id").
  985. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  986. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  987. Join("LEFT", "class", "class_with_user.class_id = class.id").
  988. Join("LEFT", "grade", "class.grade_id = grade.id").
  989. Desc("central_kitchen_for_school_user_refund_day.id").
  990. FindAndCount(&m)
  991. if err != nil {
  992. logx.Error(err)
  993. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  994. return
  995. }
  996. var list = map[string]map[string]map[string]map[string]map[string]int{}
  997. var xlsFileName, xlsDownloadPath []string
  998. for _, v := range m {
  999. if list[v.Enterprise.Name] == nil {
  1000. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1001. }
  1002. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1003. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1004. }
  1005. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1006. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1007. }
  1008. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1009. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1010. }
  1011. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1012. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1013. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1014. }
  1015. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1016. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1017. }
  1018. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1019. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1020. }
  1021. }
  1022. for k1, v1 := range list {
  1023. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1024. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1025. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1026. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1027. for k2, v2 := range v1 {
  1028. if k2 == "" {
  1029. //若年级为空,则为教师餐
  1030. continue
  1031. }
  1032. for k3, v3 := range v2 {
  1033. xlsx.NewSheet(k3)
  1034. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1035. xlsx.SetSheetRow(k3, "A1", &titleList)
  1036. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1037. xlsx.SetSheetRow(k3, "A2", &titleList)
  1038. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1039. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1040. if err != nil {
  1041. logx.Error(err)
  1042. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1043. panic(err)
  1044. }
  1045. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1046. xlsx.SetColWidth(k3, "A", "D", 20)
  1047. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1048. j := 3 //表头前两行用了,只能从第三行开始
  1049. for k4, v4 := range v3 {
  1050. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1051. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1052. sumBreakfast += v4["breakfast"]
  1053. sumLunch += v4["lunch"]
  1054. sumDinner += v4["dinner"]
  1055. j++
  1056. }
  1057. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1058. "合计", sumBreakfast, sumLunch, sumDinner})
  1059. }
  1060. }
  1061. //将文件保存至服务器
  1062. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1063. err1 := xlsx.SaveAs(tmpDownloadPath)
  1064. if err1 != nil {
  1065. logx.Error(err1)
  1066. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1067. panic(err1)
  1068. }
  1069. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1070. xlsFileName = append(xlsFileName, tmpFileName)
  1071. }
  1072. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1073. if err != nil {
  1074. logx.Error(err)
  1075. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1076. panic(err)
  1077. }
  1078. defer archive.Close()
  1079. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1080. for k, v := range xlsDownloadPath {
  1081. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1082. if err1 != nil {
  1083. logx.Error(err1)
  1084. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1085. panic(err1)
  1086. }
  1087. f, err1 := os.Open(v) //打开待压缩的文件
  1088. if err1 != nil {
  1089. logx.Error(err1)
  1090. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1091. panic(err1)
  1092. }
  1093. if _, err1 = io.Copy(w, f); err != nil {
  1094. logx.Error(err1)
  1095. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1096. panic(err1)
  1097. }
  1098. if err1 != nil {
  1099. logx.Error(err1)
  1100. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1101. panic(err1)
  1102. }
  1103. f.Close()
  1104. os.RemoveAll(v) //TODO::移除源文件
  1105. }
  1106. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1107. if err != nil {
  1108. panic(err)
  1109. }
  1110. break
  1111. case 6:
  1112. //收款统计表
  1113. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1114. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1115. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1116. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1117. //查找数据
  1118. var m []model.Enterprise
  1119. sess := db.Db.Desc("id")
  1120. if req.EnterpriseId != 0 {
  1121. sess.And("id = ?", req.EnterpriseId)
  1122. }
  1123. err := sess.Find(&m)
  1124. if err != nil {
  1125. logx.Error(err)
  1126. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1127. return
  1128. }
  1129. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1130. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1131. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1132. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1133. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1134. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1135. if err != nil {
  1136. logx.Error(err)
  1137. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1138. panic(err)
  1139. }
  1140. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1141. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1142. j := 3 //表头前两行用了,只能从第三行开始
  1143. for _, v := range m {
  1144. var income, refund string
  1145. //统计收入
  1146. sql1 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and enterprise_id = %d and create_at >= '%s' and create_at < '%s'", v.Id, req.StartDate, req.EndDate)
  1147. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1148. if err != nil {
  1149. logx.Error(err)
  1150. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1151. return
  1152. }
  1153. if mapArr1 != nil {
  1154. income = mapArr1[0]["total"]
  1155. }
  1156. //统计退款
  1157. //查询出当前学校下所有的学生
  1158. userIdentityDb := db.UserIdentityDb{}
  1159. userIdentityDb.Set(0)
  1160. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1161. var userIdentityIds []string
  1162. for _, v := range *userIdentities {
  1163. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1164. }
  1165. if len(userIdentityIds) != 0 {
  1166. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1167. sql2 := fmt.Sprintf("SELECT sum(amount) as total FROM central_kitchen_for_school_user_refund_day where state = %d and identity_id IN (%s) and refund_date >= '%s' and refund_date < '%s'",
  1168. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1169. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1170. if err != nil {
  1171. logx.Error(err)
  1172. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1173. return
  1174. }
  1175. if mapArr2 != nil {
  1176. refund = mapArr2[0]["total"]
  1177. }
  1178. }
  1179. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1180. rateFee := realityAmount * 0.0038
  1181. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1182. j++
  1183. }
  1184. //将文件保存至服务器
  1185. xlsx.SaveAs("./static" + downloadPath)
  1186. break
  1187. case 7:
  1188. //班级收款明细表
  1189. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1190. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1191. //查找订单数据
  1192. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1193. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1194. if req.EnterpriseId != 0 {
  1195. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1196. }
  1197. if req.StartDate != "" {
  1198. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1199. }
  1200. if req.EndDate != "" {
  1201. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1202. }
  1203. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1204. _, err := sess.
  1205. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1206. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1207. Join("LEFT", "user", "user.id = user_identity.uid").
  1208. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1209. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1210. Join("LEFT", "grade", "class.grade_id = grade.id").
  1211. FindAndCount(&m)
  1212. if err != nil {
  1213. logx.Error(err)
  1214. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1215. return
  1216. }
  1217. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent).Desc("central_kitchen_for_school_user_refund_day.id")
  1218. if req.EnterpriseId != 0 {
  1219. sess1.And("enterprise.id =?", req.EnterpriseId)
  1220. }
  1221. if req.StartDate != "" {
  1222. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1223. }
  1224. if req.EndDate != "" {
  1225. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1226. }
  1227. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1228. _, err = sess1.
  1229. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  1230. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1231. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1232. Join("LEFT", "user", "user_identity.uid = user.id").
  1233. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1234. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1235. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1236. Join("LEFT", "grade", "class.grade_id = grade.id").
  1237. Desc("central_kitchen_for_school_user_refund_day.id").
  1238. FindAndCount(&mm)
  1239. if err != nil {
  1240. logx.Error(err)
  1241. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1242. return
  1243. }
  1244. var list = map[string]map[string]map[string]map[string]float64{}
  1245. for _, v := range m {
  1246. if list[v.Enterprise.Name] == nil {
  1247. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1248. }
  1249. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1250. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1251. }
  1252. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1253. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1254. }
  1255. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1256. }
  1257. for _, v := range mm {
  1258. if list[v.Enterprise.Name] == nil {
  1259. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1260. }
  1261. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1262. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1263. }
  1264. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1265. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1266. }
  1267. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1268. }
  1269. var xlsFileName, xlsDownloadPath []string
  1270. for k1, v1 := range list {
  1271. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1272. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1273. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1274. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1275. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1276. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1277. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1278. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1279. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1280. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1281. if err != nil {
  1282. logx.Error(err)
  1283. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1284. panic(err)
  1285. }
  1286. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1287. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1288. j := 3 //表头前两行用了,只能从第三行开始
  1289. var sumClassNum = int64(0)
  1290. var sumIncome, sumRefund = float64(0), float64(0)
  1291. for k2, v2 := range v1 {
  1292. if k2 == "" {
  1293. //若年级为空,则为教师餐
  1294. continue
  1295. }
  1296. for k3, v3 := range v2 {
  1297. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1298. sumClassNum++
  1299. sumIncome += v3["income"]
  1300. sumRefund += v3["refund"]
  1301. j++
  1302. }
  1303. }
  1304. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1305. "合计", sumClassNum, sumIncome, sumRefund})
  1306. //将文件保存至服务器
  1307. err1 := xlsx.SaveAs(tmpDownloadPath)
  1308. if err1 != nil {
  1309. logx.Error(err1)
  1310. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1311. panic(err1)
  1312. }
  1313. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1314. xlsFileName = append(xlsFileName, tmpFileName)
  1315. }
  1316. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1317. if err != nil {
  1318. logx.Error(err)
  1319. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1320. panic(err)
  1321. }
  1322. defer archive.Close()
  1323. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1324. for k, v := range xlsDownloadPath {
  1325. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1326. if err1 != nil {
  1327. logx.Error(err1)
  1328. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1329. panic(err1)
  1330. }
  1331. f, err1 := os.Open(v) //打开待压缩的文件
  1332. if err1 != nil {
  1333. logx.Error(err1)
  1334. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1335. panic(err1)
  1336. }
  1337. if _, err1 = io.Copy(w, f); err != nil {
  1338. logx.Error(err1)
  1339. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1340. panic(err1)
  1341. }
  1342. if err1 != nil {
  1343. logx.Error(err1)
  1344. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1345. panic(err1)
  1346. }
  1347. f.Close()
  1348. os.RemoveAll(v) //TODO::移除源文件
  1349. }
  1350. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1351. if err != nil {
  1352. panic(err)
  1353. }
  1354. break
  1355. case 8:
  1356. //学校预定数量统计表
  1357. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1358. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1359. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1360. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1361. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1362. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1363. if req.EnterpriseId != 0 {
  1364. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1365. }
  1366. if req.StartDate != "" {
  1367. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ? and central_kitchen_for_school_package_ord_for_reserve.meal_time_end >= ?", req.StartDate, req.EndDate)
  1368. }
  1369. //if req.EndDate != "" {
  1370. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1371. //}
  1372. if len(req.MealKindList) > 0 {
  1373. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1374. }
  1375. _, err := sess.
  1376. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1377. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1378. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1379. FindAndCount(&m)
  1380. if err != nil {
  1381. logx.Error(err)
  1382. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1383. return
  1384. }
  1385. var list = map[string]map[string]int64{}
  1386. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1387. centralKitchenForSchoolUserWithDayDb.Set(0)
  1388. for _, v := range m {
  1389. if list[v.Enterprise.Name] == nil {
  1390. list[v.Enterprise.Name] = map[string]int64{}
  1391. }
  1392. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1393. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1394. req.StartDate, req.EndDate, req.MealKindList)
  1395. if err2 != nil {
  1396. logx.Error(err)
  1397. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1398. return
  1399. }
  1400. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1401. if err2 != nil {
  1402. logx.Error(err)
  1403. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1404. return
  1405. }
  1406. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1407. if err2 != nil {
  1408. logx.Error(err)
  1409. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1410. return
  1411. }
  1412. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1413. if err2 != nil {
  1414. logx.Error(err)
  1415. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1416. return
  1417. }
  1418. list[v.Enterprise.Name]["count"] += count
  1419. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1420. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1421. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1422. time.Sleep(20 * time.Millisecond) // 20毫秒
  1423. }
  1424. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1425. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1426. titleList = []string{"学校名称", "预定总数",
  1427. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1428. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1429. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1430. }
  1431. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1432. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1433. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1434. if err != nil {
  1435. logx.Error(err)
  1436. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1437. panic(err)
  1438. }
  1439. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1440. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1441. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1442. j := 3 //表头前两行用了,只能从第三行开始
  1443. total := int64(0)
  1444. total1 := int64(0)
  1445. total2 := int64(0)
  1446. total3 := int64(0)
  1447. for k, v := range list {
  1448. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1449. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1450. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1451. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1452. total += v["count"]
  1453. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1454. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1455. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1456. j++
  1457. }
  1458. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1459. "合计", total, total1, total2, total3})
  1460. //将文件保存至服务器
  1461. xlsx.SaveAs("./static" + downloadPath)
  1462. break
  1463. }
  1464. //新增数据
  1465. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1466. centralKitchenForSchoolExportRecordsDb.Set()
  1467. marshal, _ := json.Marshal(req)
  1468. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1469. Name: fileName,
  1470. DownloadPath: downloadPath,
  1471. Kind: kind,
  1472. ReqContent: string(marshal),
  1473. CreateAt: now.Format("2006-01-02 15:04:05"),
  1474. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1475. })
  1476. }
  1477. }
  1478. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1479. var titleList []string
  1480. var fileName, downloadPath string
  1481. var now = time.Now()
  1482. xlsx := excelize.NewFile()
  1483. //数据对比
  1484. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1485. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1486. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1487. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1488. if req.EnterpriseId != 0 {
  1489. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1490. }
  1491. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date1[0])
  1492. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date1[1])
  1493. if len(req.MealKindList) > 0 {
  1494. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1495. }
  1496. _, err := sess.
  1497. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1498. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1499. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1500. Join("LEFT", "user", "user.id = user_identity.uid").
  1501. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1502. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1503. Join("LEFT", "grade", "class.grade_id = grade.id").
  1504. FindAndCount(&m)
  1505. if err != nil {
  1506. logx.Error(err)
  1507. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1508. return
  1509. }
  1510. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1511. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1512. if req.EnterpriseId != 0 {
  1513. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1514. }
  1515. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date2[0])
  1516. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date2[1])
  1517. if len(req.MealKindList) > 0 {
  1518. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1519. }
  1520. _, err1 := sess1.
  1521. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1522. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1523. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1524. Join("LEFT", "user", "user.id = user_identity.uid").
  1525. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1526. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1527. Join("LEFT", "grade", "class.grade_id = grade.id").
  1528. FindAndCount(&m)
  1529. if err1 != nil {
  1530. logx.Error(err1)
  1531. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1532. return
  1533. }
  1534. var list = map[string]map[string]map[string]map[string]string{}
  1535. for _, v := range m {
  1536. if list[v.Enterprise.Name] == nil {
  1537. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1538. }
  1539. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1540. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1541. }
  1542. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1543. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1544. }
  1545. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1546. }
  1547. var list1 = map[string]map[string]map[string]map[string]string{}
  1548. for _, v := range m1 {
  1549. if list1[v.Enterprise.Name] == nil {
  1550. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1551. }
  1552. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1553. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1554. }
  1555. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1556. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1557. }
  1558. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1559. }
  1560. var xlsFileName, xlsDownloadPath []string
  1561. for k1, v1 := range list {
  1562. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1563. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1564. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1565. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1566. for k2, v2 := range v1 {
  1567. if k2 == "" {
  1568. //若年级为空,则为教师
  1569. continue
  1570. }
  1571. for k3, v3 := range v2 {
  1572. xlsx.NewSheet(k3)
  1573. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  1574. xlsx.SetSheetRow(k3, "A1", &titleList)
  1575. titleList = []string{"姓名", "家长联系电话"}
  1576. xlsx.SetSheetRow(k3, "A2", &titleList)
  1577. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  1578. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1579. if err != nil {
  1580. logx.Error(err)
  1581. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1582. panic(err)
  1583. }
  1584. xlsx.SetCellStyle(k3, "A1", "B1", style)
  1585. xlsx.SetColWidth(k3, "A", "B", 40)
  1586. j := 2 //表头被第一行用了,只能从第二行开始
  1587. for k4, v4 := range v3 {
  1588. //TODO::判断当前用户是否在新的月份里面
  1589. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1590. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1591. k4, v4})
  1592. j++
  1593. }
  1594. }
  1595. }
  1596. }
  1597. //将文件保存至服务器
  1598. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1599. err1 := xlsx.SaveAs(tmpDownloadPath)
  1600. if err1 != nil {
  1601. logx.Error(err1)
  1602. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1603. panic(err1)
  1604. }
  1605. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1606. xlsFileName = append(xlsFileName, tmpFileName)
  1607. }
  1608. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1609. if err != nil {
  1610. logx.Error(err)
  1611. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1612. panic(err)
  1613. }
  1614. defer archive.Close()
  1615. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1616. for k, v := range xlsDownloadPath {
  1617. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1618. if err1 != nil {
  1619. logx.Error(err1)
  1620. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1621. panic(err1)
  1622. }
  1623. f, err1 := os.Open(v) //打开待压缩的文件
  1624. if err1 != nil {
  1625. logx.Error(err1)
  1626. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1627. panic(err1)
  1628. }
  1629. if _, err1 = io.Copy(w, f); err != nil {
  1630. logx.Error(err1)
  1631. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1632. panic(err1)
  1633. }
  1634. if err1 != nil {
  1635. logx.Error(err1)
  1636. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1637. panic(err1)
  1638. }
  1639. f.Close()
  1640. os.RemoveAll(v) //TODO::移除源文件
  1641. }
  1642. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1643. if err != nil {
  1644. panic(err)
  1645. }
  1646. //新增数据
  1647. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1648. centralKitchenForSchoolExportRecordsDb.Set()
  1649. marshal, _ := json.Marshal(req)
  1650. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1651. Name: fileName,
  1652. DownloadPath: downloadPath,
  1653. Kind: 9, //数据对比
  1654. ReqContent: string(marshal),
  1655. CreateAt: now.Format("2006-01-02 15:04:05"),
  1656. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1657. })
  1658. }
  1659. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1660. mealZh = "未知"
  1661. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1662. for _, v := range md.SelfSupportForMealTimeMapping {
  1663. //判断时间大小
  1664. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1665. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1666. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1667. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1668. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1669. mealZh = v["name"]
  1670. }
  1671. }
  1672. return
  1673. }