|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369 |
- package svc
-
- import (
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/enum"
- "applet/app/utils"
- "applet/app/utils/logx"
- "archive/zip"
- "encoding/json"
- "fmt"
- "github.com/360EntSecGroup-Skylar/excelize"
- "github.com/syyongx/php2go"
- "io"
- "os"
- "strconv"
- "time"
- )
-
- func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
- var titleList []string
- var fileName string
- var now = time.Now()
- xlsx := excelize.NewFile()
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- switch req.Kind {
- case 1:
- //订单列表
- fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
-
- //查找订单数据
- sess := db.Db.Desc("nursing_home_package_ord.id")
- if req.EnterpriseId != 0 {
- sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
- }
- var m []*db.NursingHomePackageOrdWithUserIdentity
- _, err := sess.
- Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
- Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
- return
- }
- titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- j := 2 //表头被第一行用了,只能从第二行开始
- for _, v := range m {
- var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
- switch v.NursingHomePackageOrd.Kind {
- case 1:
- kind = "按年购买"
- break
- case 2:
- kind = "按月购买"
- break
- case 3:
- kind = "按天购买"
- break
- case 4:
- kind = "补餐"
- break
- }
-
- var state string //0:待支付 1:支付成功 2:支付失败
- switch v.NursingHomePackageOrd.State {
- case enum.NursingHomePackageOrdStateForWait:
- state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
- break
- case enum.NursingHomePackageOrdStateForSuccess:
- state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
- break
- case enum.NursingHomePackageOrdStateForFail:
- state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
- break
- }
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
- v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
- j++
- }
- break
- case 2:
- //退款申请表
- fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- var m []*db.NursingHomeUserRefundDayWithData
- sess := db.Db.Desc("nursing_home_user_refund_day.id")
- if req.EnterpriseId != 0 {
- sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
- }
- _, err := sess.
- Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
- Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
- Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
- Join("LEFT", "user", "user_identity.uid = user.id").
- Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
- Desc("nursing_home_user_refund_day.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
- return
- }
-
- titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- j := 2 //表头被第一行用了,只能从第二行开始
- for _, v := range m {
- var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
- switch v.NursingHomeUserWithDay.Kind {
- case enum.NursingHomeUserWithDayKindForBreakfast:
- kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
- break
- case enum.NursingHomeUserWithDayKindForLunch:
- kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
- break
- case enum.NursingHomeUserWithDayKindForDinner:
- kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
- break
- }
-
- var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
- switch v.NursingHomeUserRefundDay.State {
- case enum.NursingHomeUserRefundDayStateForAuditing:
- state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
- break
- case enum.NursingHomeUserRefundDayStateForAuditPass:
- state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
- break
- case enum.NursingHomeUserRefundDayStateForAuditReject:
- state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
- break
- case enum.NursingHomeUserRefundDayStateForAuditComplete:
- state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
- break
- }
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
- v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
- j++
- }
- xlsx.SaveAs("./static" + fileName + ".xlsx")
- break
- case 3:
- //预定统计表
- fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
- sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
- if req.EnterpriseId != 0 {
- sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
- }
- _, err := sess.
- Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
- Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
- return
- }
-
- titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- j := 2 //表头被第一行用了,只能从第二行开始
- for _, v := range m {
- var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
- switch v.NursingHomePackageOrdForReserve.Kind {
- case enum.NursingHomeUserWithDayKindForBreakfast:
- kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
- break
- case enum.NursingHomeUserWithDayKindForLunch:
- kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
- break
- case enum.NursingHomeUserWithDayKindForDinner:
- kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
- break
- }
-
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
- v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
- j++
- }
- xlsx.SaveAs("./static" + fileName + ".xlsx")
- break
- }
- //新增数据
- nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
- nursingHomeExportRecordsDb.Set()
- marshal, _ := json.Marshal(req)
- nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
- Name: fileName,
- DownloadPath: fileName,
- Kind: req.Kind,
- ReqContent: string(marshal),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
-
- func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
- var titleList []string
- var fileName, downloadPath string
- var now = time.Now()
- switch req.Kind {
- case 1:
- //教师消费统计表
- xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
- fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/self_support_for_school/" + fileName
-
- //查询数据
- var m []*db.SelfSupportForSchoolOrdWithUserIdentity
- sess := db.Db.Desc("self_support_for_school_ord.id")
- if req.EnterpriseId != 0 {
- sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
-
- sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
- _, err := sess.
- Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
- return
- }
-
- //整合数据
- var data = map[string]map[string]map[string]map[string]interface{}{}
- for _, vv := range m {
- if data[vv.Enterprise.Name] == nil {
- data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
- }
- if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
- data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
- }
- mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
- if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
- data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
- }
- data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
- "id": vv.UserIdentity.IdNo,
- }
- if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
- data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
- }
- data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
- 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)
- data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
- data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
- }
-
- xlsx.SetSheetRow("", "A1", &titleList)
- sheetIndex := 2 //第一个表是模板表
- j := 3 //表头前两行被占用了,只能从第三行开始
- for k, v := range data {
- xlsx.NewSheet(k) //1、新建表格
- xlsx.CopySheet(1, sheetIndex)
- for kk, vv := range v {
- xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
- vv["早餐"]["num"], vv["早餐"]["amount"],
- vv["午餐"]["num"], vv["午餐"]["amount"],
- vv["晚餐"]["num"], vv["晚餐"]["amount"],
- vv["夜宵"]["num"], vv["夜宵"]["amount"],
- vv["未知"]["num"], vv["未知"]["amount"],
- vv["合计"]["num"], vv["合计"]["amount"],
- })
- j++
- }
- sheetIndex++
- }
- xlsx.DeleteSheet("template") //删除模板表
- //将文件保存至服务器
- xlsx.SaveAs("./static" + downloadPath)
- break
- case 2:
- //商家分餐数据统计表
- xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
- fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/self_support_for_school/" + fileName
-
- //查询出所有的设备
- var deviceList []*model.Device
- err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
- return
- }
- var deviceListMapping = map[string]model.Device{}
- for _, v := range deviceList {
- deviceListMapping[v.DeviceSn] = *v
- }
-
- var m []*db.SelfSupportForSchoolOrdWithUserIdentity
- sess := db.Db.Desc("self_support_for_school_ord.id")
- if req.EnterpriseId != 0 {
- sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- _, err = sess.
- Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
- return
- }
-
- //整合数据
- var data = map[string]map[string]map[string]map[string]float64{}
- for _, vv := range m {
- shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
- if data[vv.Enterprise.Name] == nil {
- data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
- }
- if data[vv.Enterprise.Name][shopName] == nil {
- data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
- }
- mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
- if data[vv.Enterprise.Name][shopName][mealZh] == nil {
- data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
- }
- if data[vv.Enterprise.Name][shopName]["合计"] == nil {
- data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
- }
- data[vv.Enterprise.Name][shopName][mealZh]["num"]++
- data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
- data[vv.Enterprise.Name][shopName]["合计"]["num"]++
- data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
- }
-
- xlsx.SetSheetRow("", "A1", &titleList)
- sheetIndex := 2 //第一个表是模板表
- for k, v := range data {
- if k == "" {
- k = "未知"
- }
- xlsx.NewSheet(k) //1、新建表格
- xlsx.CopySheet(1, sheetIndex)
- j := 4 //表头前三行被占用了,只能从第四行开始
- for kk, vv := range v {
- if kk == "" {
- kk = k
- }
- xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
- vv["早餐"]["num"], vv["早餐"]["amount"],
- vv["午餐"]["num"], vv["午餐"]["amount"],
- vv["晚餐"]["num"], vv["晚餐"]["amount"],
- vv["夜宵"]["num"], vv["夜宵"]["amount"],
- vv["合计"]["num"], vv["合计"]["amount"],
- })
- j++
- }
- sheetIndex++
- }
- xlsx.DeleteSheet("template") //删除模板表
- //将文件保存至服务器
- xlsx.SaveAs("./static" + downloadPath)
- break
- case 3:
- //就餐数据统计表
- xlsx, _ := excelize.OpenFile("./static/template/自营学校(就餐数据统计表).xlsx")
- fileName = "就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/self_support_for_school/" + fileName
-
- //查询出所有的设备
- //var deviceList []*model.Device
- //err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
- //if err != nil {
- // logx.Error(err)
- // println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
- // return
- //}
- //var deviceListMapping = map[string]model.Device{}
- //for _, v := range deviceList {
- // deviceListMapping[v.DeviceSn] = *v
- //}
-
- var m []*db.SelfSupportForSchoolOrdWithUserIdentity
- sess := db.Db.Desc("self_support_for_school_ord.id")
- if req.EnterpriseId != 0 {
- sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- _, err := sess.
- Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
-
- //整合数据
- var data = map[string]map[string]map[string]map[string]float64{}
-
- date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- diffDays := int(date2.Sub(date1).Hours() / 24)
- var dates []string
- for i := 0; i <= diffDays; i++ {
- date := date1.AddDate(0, 0, i)
- shopName := date.Format("2006-01-02")
- dates = append(dates, shopName)
- for _, vv := range m {
- tmpDate, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
- return
- }
- fmt.Println(tmpDate.Format("2006-01-02"))
- if shopName == tmpDate.Format("2006-01-02") {
- if data[vv.Enterprise.Name] == nil {
- data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
- }
- if data[vv.Enterprise.Name][shopName] == nil {
- data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
- }
- mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
- if data[vv.Enterprise.Name][shopName][mealZh] == nil {
- data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
- }
- if data[vv.Enterprise.Name][shopName]["合计"] == nil {
- data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
- }
- data[vv.Enterprise.Name][shopName][mealZh]["num"]++
- data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
- data[vv.Enterprise.Name][shopName]["合计"]["num"]++
- data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
- }
- }
- }
-
- xlsx.SetSheetRow("", "A1", &titleList)
- sheetIndex := 2 //第一个表是模板表
- for k, v := range data {
- if k == "" {
- k = "未知"
- }
- xlsx.NewSheet(k) //1、新建表格
- xlsx.CopySheet(1, sheetIndex)
- j := 4 //表头前三行被占用了,只能从第四行开始
- for _, date := range dates {
- for kk, vv := range v {
- if date == kk {
- xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
- vv["早餐"]["num"], vv["早餐"]["amount"],
- vv["午餐"]["num"], vv["午餐"]["amount"],
- vv["晚餐"]["num"], vv["晚餐"]["amount"],
- vv["夜宵"]["num"], vv["夜宵"]["amount"],
- vv["合计"]["num"], vv["合计"]["amount"],
- })
- j++
- }
- sheetIndex++
- }
- }
- }
- xlsx.DeleteSheet("template") //删除模板表
- //将文件保存至服务器
- xlsx.SaveAs("./static" + downloadPath)
- break
- case 4:
- //设备就餐数据统计表
- fileName = "设备就餐数据统计_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
- downloadPath = "/export/self_support_for_school/" + fileName
-
- //查询出所有的设备
- var deviceList []*model.Device
- if req.EnterpriseId == 0 {
- err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
- return
- }
- } else {
- err := db.Db.Where("enterprise_id = ?", req.EnterpriseId).Find(&deviceList)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
- return
- }
- }
-
- date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- return
- }
- date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- return
- }
- diffDays := int(date2.Sub(date1).Hours() / 24)
-
- var deviceListMapping = map[string]model.Device{}
- //整合数据
- var data = map[string]map[string]map[string]map[string]float64{}
- var xlsFileName, xlsDownloadPath []string
- for _, v := range deviceList {
- deviceListMapping[v.DeviceSn] = *v
- var m []*db.SelfSupportForSchoolOrdWithUserIdentityV2
- sess := db.Db.Where("device_sn = ?", v.DeviceSn).And("enterprise_id =?", req.EnterpriseId).Desc("self_support_for_school_ord.id")
- if req.EnterpriseId != 0 {
- sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
- }
- _, err = sess.Join("LEFT", "enterprise", "self_support_for_school_ord.enterprise_id = enterprise.id").FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- return
- }
- for _, vv := range m {
- tmpTime, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- return
- }
- tmpDate := tmpTime.Format("2006-01-02")
- if data[vv.Enterprise.Name] == nil {
- data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
- }
- if data[vv.Enterprise.Name][v.Name] == nil {
- data[vv.Enterprise.Name][v.Name] = make(map[string]map[string]float64)
- }
- if data[vv.Enterprise.Name][v.Name][tmpDate] == nil {
- data[vv.Enterprise.Name][v.Name][tmpDate] = make(map[string]float64)
- }
- data[vv.Enterprise.Name][v.Name][tmpDate]["num"]++
- data[vv.Enterprise.Name][v.Name][tmpDate]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
- }
- }
-
- for k1, v := range data {
- tmpFileName := k1 + "设备就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- tmpDownloadPath := "./static/export/self_support_for_school/" + tmpFileName
- xlsx := excelize.NewFile()
-
- var totalData = map[string]map[string]float64{}
- for k2, vv := range v {
- var sumNum, sumAmount = float64(0), float64(0)
- xlsx.NewSheet(k2)
- titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
- xlsx.SetSheetRow(k2, "A1", &titleList)
- titleList = []string{"日 期", "消费笔数", "消费金额"}
- xlsx.SetSheetRow(k2, "A2", &titleList)
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.MergeCell(k2, "A1", "C1") //合并单元格
- xlsx.SetCellStyle(k2, "A1", "C1", style)
- xlsx.SetColWidth(k2, "A", "C", 20)
- j := 3 //表头前两行用了,只能从第三行开始
-
- for i := 0; i <= diffDays; i++ {
- date := date1.AddDate(0, 0, i).Format("2006-01-02")
- if vv[date] != nil {
- xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, vv[date]["num"], vv[date]["amount"]})
- sumNum += vv[date]["num"]
- sumAmount += vv[date]["amount"]
- } else {
- xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, 0, 0})
- }
- j++
- }
-
- xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{
- "合计", sumNum, sumAmount})
- if totalData[k2] == nil {
- totalData[k2] = map[string]float64{}
- }
- totalData[k2]["num"] = sumNum
- totalData[k2]["amount"] = sumAmount
- }
-
- //xlsx.NewSheet("所有档口")
- xlsx.SetSheetName("Sheet1", "所有档口")
- titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
- xlsx.SetSheetRow("所有档口", "A1", &titleList)
- titleList = []string{"名 称", "消费笔数", "消费金额"}
- xlsx.SetSheetRow("所有档口", "A2", &titleList)
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.MergeCell("所有档口", "A1", "C1") //合并单元格
- xlsx.SetCellStyle("所有档口", "A1", "C1", style)
- xlsx.SetColWidth("所有档口", "A", "C", 20)
- j := 3 //表头前两行用了,只能从第三行开始
- var totalNum, totalAmount float64
- for key, val := range totalData {
- totalNum += val["num"]
- totalAmount += val["amount"]
- xlsx.SetSheetRow("所有档口", "A"+strconv.Itoa(j), &[]interface{}{key, val["num"], val["amount"]})
- j++
- }
- xlsx.SetSheetRow("所有档口", "A"+strconv.Itoa(j), &[]interface{}{"合计", totalNum, totalAmount})
-
- //xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
- //将文件保存至服务器
- err1 := xlsx.SaveAs(tmpDownloadPath)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
- xlsFileName = append(xlsFileName, tmpFileName)
- }
-
- archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- panic(err)
- }
- defer archive.Close()
-
- zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
- for k, v := range xlsDownloadPath {
- w, err1 := zipWriter.Create("设备就餐数据统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
- if err1 != nil {
- logx.Error(err1)
- println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- f, err1 := os.Open(v) //打开待压缩的文件
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- if _, err1 = io.Copy(w, f); err != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
- f.Close()
- os.RemoveAll(v) //TODO::移除源文件
- }
-
- zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
- if err != nil {
- panic(err)
- }
- break
- }
-
- //新增数据
- selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
- selfSupportForSchoolExportRecordsDb.Set()
- marshal, _ := json.Marshal(req)
- selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
- Name: fileName,
- DownloadPath: downloadPath,
- Kind: req.Kind,
- ReqContent: string(marshal),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
-
- func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
- var titleList []string
- var fileName, downloadPath string
- var now = time.Now()
- for _, kind := range req.Kinds {
- switch kind {
- case 1:
- xlsx := excelize.NewFile()
- //订单列表
- fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
-
- //查找订单数据
- sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
- sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
- if req.EnterpriseId != 0 {
- sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
- _, err := sess.
- Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
- return
- }
-
- //查找预定数据
- var outTradeNoList []string
- for _, v := range m {
- outTradeNoList = append(outTradeNoList, v.OutTradeNo)
- }
- var mm []model.CentralKitchenForSchoolPackageOrdForReserve
- sess1 := db.Db.Desc("id")
- if req.EnterpriseId != 0 {
- sess1.And("enterprise_id = ?", req.EnterpriseId)
- }
- if len(req.MealKindList) > 0 {
- sess1.In("kind", req.MealKindList)
- }
- err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
- return
- }
- var exportOutTradeNoList []string
- for _, v := range mm {
- exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
- }
-
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
- xlsx.SetSheetRow("Sheet1", "A2", &titleList)
-
- xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
- xlsx.SetColWidth("Sheet1", "A", "k", 20)
-
- j := 3 //表头前两行用了,只能从第三行开始
- for _, v := range m {
- if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
- continue
- }
- var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
- switch v.CentralKitchenForSchoolPackageOrd.OrdState {
- case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
- ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
- break
- case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
- ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
- break
- case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
- ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
- break
- case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
- ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
- break
- case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
- ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
- break
- case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
- ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
- break
- }
- //预定餐数、预定日期
- var list []*model.CentralKitchenForSchoolUserWithDay
- err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
- if err1 != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
- return
- }
- var reserveDays string
- for _, v1 := range list {
- reserveDays += v1.Date + ", "
- }
- if v.Grade.Name == "" {
- v.Grade.Name = "教师"
- }
- if v.Class.Name == "" {
- v.Class.Name = "教师"
- }
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
- v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
- j++
- }
- //将文件保存至服务器
- xlsx.SaveAs("./static" + downloadPath)
- break
- case 2:
- xlsx := excelize.NewFile()
- //退款申请表
- fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
-
- //查找退款申请数据
- sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
- if req.EnterpriseId != 0 {
- sess.And("enterprise.id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
- }
- if len(req.MealKindList) > 0 {
- sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- var m []*db.CentralKitchenForSchoolUserRefundDayWithData
- _, err := sess.
- 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").
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
- Join("LEFT", "user", "user_identity.uid = user.id").
- Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
- Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- Desc("central_kitchen_for_school_user_refund_day.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
- return
- }
-
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
- xlsx.SetSheetRow("Sheet1", "A2", &titleList)
-
- xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
- xlsx.SetColWidth("Sheet1", "A", "k", 20)
-
- j := 3 //表头前两行用了,只能从第三行开始
- for _, v := range m {
- var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
- switch v.CentralKitchenForSchoolUserWithDay.Kind {
- case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
- kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
- break
- case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
- kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
- break
- case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
- kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
- break
- }
-
- var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
- switch v.CentralKitchenForSchoolUserRefundDay.State {
- case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
- state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
- break
- case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
- state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
- break
- case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
- state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
- break
- case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
- state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
- break
- }
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
- v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
- j++
- }
- //将文件保存至服务器
- xlsx.SaveAs("./static" + downloadPath)
- break
- case 3:
- xlsx := excelize.NewFile()
- //学校预定统计表
- fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
-
- var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
- sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
- if req.EnterpriseId != 0 {
- sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
- }
-
- var months []time.Month
- if req.StartDate != "" {
- sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- _, sMonth, _ := sDate.Date()
- _, eMonth, _ := eDate.Date()
- diffMonth := sMonth - eMonth
- if diffMonth == 0 {
- //TODO::查询一个月
- months = append(months, sMonth)
- } else {
- for i := sMonth; i <= eMonth; i++ {
- months = append(months, i)
- }
- }
- }
- if len(months) > 0 {
- sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
- }
- //if req.EndDate != "" {
- // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
- //}
- if len(req.MealKindList) > 0 {
- sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- _, err := sess.
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
-
- centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- var list = map[string]map[string]map[string]map[string]int64{}
- var xlsFileName, xlsDownloadPath []string
- for _, v := range m {
- if list[v.Enterprise.Name] == nil {
- list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
- }
- if list[v.Enterprise.Name][v.Grade.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
- }
-
- count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
- v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
- enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
- req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind})
- if err2 != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- time.Sleep(20 * time.Millisecond) // 20毫秒
-
- if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
- }
- if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
- }
- if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
- }
- }
-
- for k1, v1 := range list {
- tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
-
- xlsx.SetSheetRow("Sheet1", "A2", &titleList)
- xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
- xlsx.SetColWidth("Sheet1", "A", "E", 20)
-
- j := 3 //表头前两行用了,只能从第三行开始
- var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
-
- tmpEnterpriseDb := db.EnterpriseDb{}
- tmpEnterpriseDb.Set()
- tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
- if err11 != nil {
- logx.Error(err11)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
- panic(err11)
- }
- tmpClassDb := db.ClassDb{}
- tmpClassDb.Set(0)
- classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- panic(err)
- }
- for _, class := range *classList {
- for k2, v2 := range v1 {
- if k2 == "" {
- //若年级为空,则为教师餐
- k2 = "教师餐"
- }
-
- for k3, v3 := range v2 {
- if class.Name != k3 {
- continue
- }
- if k3 == "" {
- //若班级为空,则为教师餐
- k3 = "教师餐"
- }
-
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
- k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
- sumClassNum++
- sumBreakfast += v3["breakfast"]
- sumLunch += v3["lunch"]
- sumDinner += v3["dinner"]
- j++
- }
- }
- }
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
- "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
- //将文件保存至服务器
- err1 := xlsx.SaveAs(tmpDownloadPath)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
- panic(err1)
- }
- xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
- xlsFileName = append(xlsFileName, tmpFileName)
- }
-
- archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- defer archive.Close()
-
- zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
- for k, v := range xlsDownloadPath {
- w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- f, err1 := os.Open(v) //打开待压缩的文件
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- if _, err1 = io.Copy(w, f); err != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
- panic(err1)
- }
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
- panic(err1)
- }
- f.Close()
- os.RemoveAll(v) //TODO::移除源文件
- }
-
- zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
- if err != nil {
- panic(err)
- }
- break
- case 4:
- xlsx := excelize.NewFile()
- //班级明细表
- fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
- var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
- sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
- if req.EnterpriseId != 0 {
- sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
- }
- var months []time.Month
- if req.StartDate != "" {
- sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- _, sMonth, _ := sDate.Date()
- _, eMonth, _ := eDate.Date()
- diffMonth := sMonth - eMonth
- if diffMonth == 0 {
- //TODO::查询一个月
- months = append(months, sMonth)
- } else {
- for i := sMonth; i <= eMonth; i++ {
- months = append(months, i)
- }
- }
- }
- if len(months) > 0 {
- sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
- }
- //if req.EndDate != "" {
- // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
- //}
- if len(req.MealKindList) > 0 {
- sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- _, err := sess.
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- Asc("class.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- return
- }
-
- var list = map[string]map[string]map[string]map[string]map[string]int64{}
- var xlsFileName, xlsDownloadPath []string
- centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- for _, v := range m {
- if list[v.Enterprise.Name] == nil {
- list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
- }
- if list[v.Enterprise.Name][v.Grade.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
- }
-
- count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
- v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
- enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
- req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind})
- if err2 != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- if count == 0 {
- continue
- }
- time.Sleep(2 * time.Millisecond) // 2毫秒
-
- if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
- }
- if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
- }
- if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
- }
- }
-
- for k1, v1 := range list {
- tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
-
- //查询当前学校下的班级升序
- tmpEnterpriseDb := db.EnterpriseDb{}
- tmpEnterpriseDb.Set()
- tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
- if err11 != nil {
- logx.Error(err11)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
- panic(err11)
- }
- tmpClassDb := db.ClassDb{}
- tmpClassDb.Set(0)
- classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- panic(err)
- }
- for _, class := range *classList {
- for k2, v2 := range v1 {
- if k2 == "" {
- //若年级为空,则为教师餐
- continue
- }
- for k3, v3 := range v2 {
- if class.Name != k3 {
- continue
- }
- xlsx.NewSheet(k3)
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
- xlsx.SetSheetRow(k3, "A1", &titleList)
- titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
- xlsx.SetSheetRow(k3, "A2", &titleList)
- xlsx.MergeCell(k3, "A1", "D1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle(k3, "A1", "D1", style)
- xlsx.SetColWidth(k3, "A", "D", 20)
-
- var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
- j := 3 //表头前两行用了,只能从第三行开始
- for k4, v4 := range v3 {
- if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
- continue
- }
- xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
- k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
- sumBreakfast += v4["breakfast"]
- sumLunch += v4["lunch"]
- sumDinner += v4["dinner"]
- j++
- }
- xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
- "合计", sumBreakfast, sumLunch, sumDinner})
- }
- }
- }
-
- //将文件保存至服务器
- xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
- err1 := xlsx.SaveAs(tmpDownloadPath)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- panic(err1)
- }
- xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
- xlsFileName = append(xlsFileName, tmpFileName)
- }
-
- archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- panic(err)
- }
- defer archive.Close()
-
- zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
- for k, v := range xlsDownloadPath {
- w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- f, err1 := os.Open(v) //打开待压缩的文件
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- if _, err1 = io.Copy(w, f); err != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- panic(err1)
- }
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
- panic(err1)
- }
- f.Close()
- os.RemoveAll(v) //TODO::移除源文件
- }
-
- zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
- if err != nil {
- panic(err)
- }
- break
- case 5:
- xlsx := excelize.NewFile()
- //退款明细表
- fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
- sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
- if req.EnterpriseId != 0 {
- sess.And("enterprise.id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
- }
- if len(req.MealKindList) > 0 {
- sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- var m []*db.CentralKitchenForSchoolUserRefundDayWithData
- _, err := sess.
- 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").
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
- Join("LEFT", "user", "user_identity.uid = user.id").
- Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
- Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- Desc("central_kitchen_for_school_user_refund_day.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
- return
- }
-
- var list = map[string]map[string]map[string]map[string]map[string]int{}
- var xlsFileName, xlsDownloadPath []string
- for _, v := range m {
- if list[v.Enterprise.Name] == nil {
- list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
- }
- if list[v.Enterprise.Name][v.Grade.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
- }
-
- //就餐类型(1:早餐 2:午餐 3:晚餐)
- if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
- }
- if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
- }
- if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
- }
- }
-
- for k1, v1 := range list {
- tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
-
- for k2, v2 := range v1 {
- if k2 == "" {
- //若年级为空,则为教师餐
- continue
- }
-
- for k3, v3 := range v2 {
- xlsx.NewSheet(k3)
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
- xlsx.SetSheetRow(k3, "A1", &titleList)
- titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
- xlsx.SetSheetRow(k3, "A2", &titleList)
- xlsx.MergeCell(k3, "A1", "D1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle(k3, "A1", "D1", style)
- xlsx.SetColWidth(k3, "A", "D", 20)
-
- var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
- j := 3 //表头前两行用了,只能从第三行开始
- for k4, v4 := range v3 {
- xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
- k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
- sumBreakfast += v4["breakfast"]
- sumLunch += v4["lunch"]
- sumDinner += v4["dinner"]
- j++
- }
- xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
- "合计", sumBreakfast, sumLunch, sumDinner})
- }
- }
-
- //将文件保存至服务器
- xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
- err1 := xlsx.SaveAs(tmpDownloadPath)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
- panic(err1)
- }
- xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
- xlsFileName = append(xlsFileName, tmpFileName)
- }
-
- archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
- panic(err)
- }
- defer archive.Close()
-
- zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
- for k, v := range xlsDownloadPath {
- w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- f, err1 := os.Open(v) //打开待压缩的文件
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- if _, err1 = io.Copy(w, f); err != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
- panic(err1)
- }
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
- panic(err1)
- }
- f.Close()
- os.RemoveAll(v) //TODO::移除源文件
- }
-
- zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
- if err != nil {
- panic(err)
- }
- break
- case 6:
- xlsx := excelize.NewFile()
- //收款统计表
- fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
-
- //查找数据
- var m []model.Enterprise
- sess := db.Db.Desc("id")
- if req.EnterPriseKind != 0 {
- sess.And("kind = ?", req.EnterPriseKind)
- }
- if req.EnterpriseId != 0 {
- sess.And("id = ?", req.EnterpriseId)
- }
- err := sess.Find(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
- return
- }
-
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
- xlsx.SetSheetRow("Sheet1", "A2", &titleList)
-
- xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
- xlsx.SetColWidth("Sheet1", "A", "E", 20)
-
- j := 3 //表头前两行用了,只能从第三行开始
- for _, v := range m {
- var income, refund string
- //统计收入
- 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)
- mapArr1, err := db.QueryNativeString(db.Db, sql1)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
- return
- }
- if mapArr1 != nil {
- income = mapArr1[0]["total"]
- }
-
- //统计退款
- //查询出当前学校下所有的学生
- userIdentityDb := db.UserIdentityDb{}
- userIdentityDb.Set(0)
- userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
- var userIdentityIds []string
- for _, v := range *userIdentities {
- userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
- }
- if len(userIdentityIds) != 0 {
- userIdentityStr := php2go.Implode(",", userIdentityIds)
- 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'",
- enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
- mapArr2, err := db.QueryNativeString(db.Db, sql2)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
- return
- }
- if mapArr2 != nil {
- refund = mapArr2[0]["total"]
- }
- }
- realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
- rateFee := realityAmount * 0.0038
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
- j++
- }
- //将文件保存至服务器
- xlsx.SaveAs("./static" + downloadPath)
- break
- case 7:
- xlsx := excelize.NewFile()
- //班级收款明细表
- fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
- //查找订单数据
- sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
- sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
- if req.EnterpriseId != 0 {
- sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
- _, err := sess.
- Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
- return
- }
-
- 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")
- if req.EnterpriseId != 0 {
- sess1.And("enterprise.id =?", req.EnterpriseId)
- }
- if req.StartDate != "" {
- sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
- }
- if req.EndDate != "" {
- sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
- }
-
- var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
- _, err = sess1.
- 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").
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
- Join("LEFT", "user", "user_identity.uid = user.id").
- Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
- Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- Desc("central_kitchen_for_school_user_refund_day.id").
- FindAndCount(&mm)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
- return
- }
-
- var list = map[string]map[string]map[string]map[string]float64{}
- for _, v := range m {
- if list[v.Enterprise.Name] == nil {
- list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
- }
- if list[v.Enterprise.Name][v.Grade.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
- }
-
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
- }
- for _, v := range mm {
- if list[v.Enterprise.Name] == nil {
- list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
- }
- if list[v.Enterprise.Name][v.Grade.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
- }
-
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
- }
-
- var xlsFileName, xlsDownloadPath []string
- for k1, v1 := range list {
- tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
- titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
- xlsx.SetSheetRow("Sheet1", "A2", &titleList)
-
- xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
- xlsx.SetColWidth("Sheet1", "A", "D", 20)
-
- j := 3 //表头前两行用了,只能从第三行开始
- var sumClassNum = int64(0)
- var sumIncome, sumRefund = float64(0), float64(0)
- for k2, v2 := range v1 {
- if k2 == "" {
- //若年级为空,则为教师餐
- continue
- }
- for k3, v3 := range v2 {
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
- sumClassNum++
- sumIncome += v3["income"]
- sumRefund += v3["refund"]
- j++
- }
- }
-
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
- "合计", sumClassNum, sumIncome, sumRefund})
- //将文件保存至服务器
- err1 := xlsx.SaveAs(tmpDownloadPath)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
- xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
- xlsFileName = append(xlsFileName, tmpFileName)
- }
-
- archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
- panic(err)
- }
- defer archive.Close()
-
- zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
- for k, v := range xlsDownloadPath {
- w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- f, err1 := os.Open(v) //打开待压缩的文件
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- if _, err1 = io.Copy(w, f); err != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
- panic(err1)
- }
- f.Close()
- os.RemoveAll(v) //TODO::移除源文件
- }
-
- zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
- if err != nil {
- panic(err)
- }
- break
- case 8:
- xlsx := excelize.NewFile()
- //学校预定数量统计表
- fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
-
- var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
- sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
- if req.EnterpriseId != 0 {
- sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
- }
- var months []time.Month
- if req.StartDate != "" {
- sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- _, sMonth, _ := sDate.Date()
- _, eMonth, _ := eDate.Date()
- diffMonth := sMonth - eMonth
- if diffMonth == 0 {
- //TODO::查询一个月
- months = append(months, sMonth)
- } else {
- for i := sMonth; i <= eMonth; i++ {
- months = append(months, i)
- }
- }
- }
- if len(months) > 0 {
- sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
- }
- //if req.EndDate != "" {
- // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
- //}
- if len(req.MealKindList) > 0 {
- sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
- _, err := sess.
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
-
- var list = map[string]map[string]int64{}
- centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- for _, v := range m {
- if list[v.Enterprise.Name] == nil {
- list[v.Enterprise.Name] = map[string]int64{}
- }
- count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
- v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
- enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
- req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind})
- if err2 != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
-
- for _, value := range req.MealKindList {
- if value == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
- count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate,
- req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast,
- enum.CentralKitchenForSchoolUserWithDayStateForCancel,
- enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
- )
- if err2 != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
- }
- if value == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
- count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate,
- enum.CentralKitchenForSchoolUserWithDayKindForLunch,
- enum.CentralKitchenForSchoolUserWithDayStateForCancel,
- enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
- )
- if err2 != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
- }
- if value == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
- count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate,
- enum.CentralKitchenForSchoolUserWithDayKindForDinner,
- enum.CentralKitchenForSchoolUserWithDayStateForCancel,
- enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
- )
- if err2 != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
- }
- }
- list[v.Enterprise.Name]["count"] += count
- time.Sleep(20 * time.Millisecond) // 20毫秒
- }
-
- titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
- xlsx.SetSheetRow("Sheet1", "A1", &titleList)
-
- titleList = []string{"学校名称", "预定总数",
- enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
- enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
- enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
- }
- xlsx.SetSheetRow("Sheet1", "A2", &titleList)
-
- xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
- xlsx.SetColWidth("Sheet1", "A", "A", 30)
- xlsx.SetColWidth("Sheet1", "B", "E", 15)
- j := 3 //表头前两行用了,只能从第三行开始
- total := int64(0)
- total1 := int64(0)
- total2 := int64(0)
- total3 := int64(0)
- for k, v := range list {
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
- v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
- v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
- v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
- total += v["count"]
- total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
- total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
- total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
- j++
- }
- xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
- "合计", total, total1, total2, total3})
- //将文件保存至服务器
- xlsx.SaveAs("./static" + downloadPath)
- break
- case 10:
- //就餐数据统计表
- sTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
- eTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
- xls, _ := excelize.OpenFile("./static/template/央厨学校(就餐数据统计表).xlsx")
- fileName = "【" + sTime.Format("2006-01-02") + "~" + eTime.Format("2006-01-02") + "】就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
-
- var list = map[string]map[string]map[string]float64{}
- var dates []string
-
- if req.StartDate != "" {
- centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
- centralKitchenForSchoolUserWithDayDb.Set(0)
- diffDays := int(eTime.Sub(sTime).Hours() / 24)
- for i := 0; i <= diffDays; i++ {
- date := sTime.AddDate(0, 0, i)
- dateName := date.Format("2006-01-02")
- dates = append(dates, dateName)
- for _, vv := range req.MealKindList {
- tmpSum, err := centralKitchenForSchoolUserWithDayDb.SumCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
- panic(err)
- }
- tmpCount, err := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
- panic(err)
- }
- if list[dateName] == nil {
- list[dateName] = map[string]map[string]float64{}
- }
- if list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] == nil {
- list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] = map[string]float64{}
- }
- if list[dateName]["合计"] == nil {
- list[dateName]["合计"] = make(map[string]float64)
- }
-
- list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["sum"] = tmpSum
- list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["count"] = float64(tmpCount)
- list[dateName]["合计"]["sum"] += tmpSum
- list[dateName]["合计"]["count"] += float64(tmpCount)
- }
- }
- }
-
- //xls.SetSheetRow("", "A1", &titleList)
- //sheetIndex := 2 //第一个表是模板表
- //xls.NewSheet("sheet1") //1、新建表格
- //xls.CopySheet(1, sheetIndex)
- j := 4 //表头前三"早餐"行被占用了,只能从第四行开始
- var totalMap = map[string]float64{}
- for _, date := range dates {
- for kk, v := range list {
- if date == kk {
- xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{kk,
- v["早餐"]["count"], v["早餐"]["sum"],
- v["午餐"]["count"], v["午餐"]["sum"],
- v["晚餐"]["count"], v["晚餐"]["sum"],
- v["合计"]["count"], v["合计"]["sum"],
- })
- totalMap["breakfast_count"] += v["早餐"]["count"]
- totalMap["breakfast_sum"] += v["早餐"]["sum"]
- totalMap["lunch_count"] += v["午餐"]["count"]
- totalMap["lunch_sum"] += v["午餐"]["sum"]
- totalMap["dinner_count"] += v["晚餐"]["count"]
- totalMap["dinner_sum"] += v["晚餐"]["sum"]
- totalMap["total_count"] += v["合计"]["count"]
- totalMap["total_sum"] += v["合计"]["sum"]
- j++
- }
- }
- }
- //xls.DeleteSheet("template") //删除模板表
-
- xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{"总计",
- totalMap["breakfast_count"], totalMap["breakfast_sum"],
- totalMap["lunch_count"], totalMap["lunch_sum"],
- totalMap["dinner_count"], totalMap["dinner_sum"],
- totalMap["total_count"], totalMap["total_sum"],
- })
- //将文件保存至服务器
- xls.SaveAs("./static" + downloadPath)
- break
- }
-
- //新增数据
- centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
- centralKitchenForSchoolExportRecordsDb.Set()
- marshal, _ := json.Marshal(req)
- centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
- Name: fileName,
- DownloadPath: downloadPath,
- Kind: kind,
- ReqContent: string(marshal),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
- }
-
- func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
- var titleList []string
- var fileName, downloadPath string
- var now = time.Now()
-
- xlsx := excelize.NewFile()
- //数据对比
- fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
- downloadPath = "/export/central_kitchen_for_school/" + fileName
-
- var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
- sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
- if req.EnterpriseId != 0 {
- sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
- }
- if req.Date1[0] != "" {
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
- sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
- }
- if req.Date1[1] != "" {
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
- sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
- }
- if len(req.MealKindList) > 0 {
- sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
- }
- if req.EnterPriseKind != 0 {
- sess.And("enterprise.kind = ?", req.EnterPriseKind)
- }
-
- _, err := sess.
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
- return
- }
-
- var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
- sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
- if req.EnterpriseId != 0 {
- sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
- }
- if req.Date2[0] != "" {
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
- sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
- }
- if req.Date2[1] != "" {
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
- sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
- }
- if len(req.MealKindList) > 0 {
- sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
- }
- _, err1 := sess1.
- 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").
- Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
- Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
- Join("LEFT", "user", "user.id = user_identity.uid").
- Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
- Join("LEFT", "class", "class_with_user.class_id = class.id").
- Join("LEFT", "grade", "class.grade_id = grade.id").
- FindAndCount(&m1)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
- return
- }
-
- var list = map[string]map[string]map[string]map[string]string{}
- for _, v := range m {
- if list[v.Enterprise.Name] == nil {
- list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
- }
- if list[v.Enterprise.Name][v.Grade.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
- }
- if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
- }
- list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
- }
-
- var list1 = map[string]map[string]map[string]map[string]string{}
- for _, v := range m1 {
- if list1[v.Enterprise.Name] == nil {
- list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
- }
- if list1[v.Enterprise.Name][v.Grade.Name] == nil {
- list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
- }
- if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
- list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
- }
- list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
- }
-
- var xlsFileName, xlsDownloadPath []string
- for k1, v1 := range list {
- tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
- tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
- startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
- endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
-
- //查询当前学校下的班级升序
- tmpEnterpriseDb := db.EnterpriseDb{}
- tmpEnterpriseDb.Set()
- tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
- if err11 != nil {
- logx.Error(err11)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
- panic(err11)
- }
- tmpClassDb := db.ClassDb{}
- tmpClassDb.Set(0)
- classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
- panic(err)
- }
- for _, class := range *classList {
- for k2, v2 := range v1 {
- if k2 == "" {
- //若年级为空,则为教师
- continue
- }
- for k3, v3 := range v2 {
- if class.Name != k3 {
- continue
- }
- xlsx.NewSheet(k3)
- titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
- xlsx.SetSheetRow(k3, "A1", &titleList)
- titleList = []string{"姓名", "家长联系电话"}
- xlsx.SetSheetRow(k3, "A2", &titleList)
- xlsx.MergeCell(k3, "A1", "B1") //合并单元格
- style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- panic(err)
- }
- xlsx.SetCellStyle(k3, "A1", "B1", style)
- xlsx.SetColWidth(k3, "A", "B", 40)
-
- j := 2 //表头被第一行用了,只能从第二行开始
- for k4, v4 := range v3 {
- //TODO::判断当前用户是否在新的月份里面
- fmt.Println("11111111111111", list1[k1])
- fmt.Println("22222222222222", list1[k1][k2])
- fmt.Println("33333333333333", list1[k1][k2][k3])
- fmt.Println("44444444444444", list1[k1][k2][k3][k4])
- if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
- xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
- k4, v4})
- j++
- }
- }
- }
- }
- }
-
- //将文件保存至服务器
- xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
- err1 := xlsx.SaveAs(tmpDownloadPath)
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
- panic(err1)
- }
- xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
- xlsFileName = append(xlsFileName, tmpFileName)
- }
-
- archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
- if err != nil {
- logx.Error(err)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
- panic(err)
- }
- defer archive.Close()
-
- zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
- for k, v := range xlsDownloadPath {
- w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- f, err1 := os.Open(v) //打开待压缩的文件
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
- panic(err1)
- }
-
- if _, err1 = io.Copy(w, f); err != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
- panic(err1)
- }
- if err1 != nil {
- logx.Error(err1)
- println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
- panic(err1)
- }
- f.Close()
- os.RemoveAll(v) //TODO::移除源文件
- }
-
- zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
- if err != nil {
- panic(err)
- }
-
- //新增数据
- centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
- centralKitchenForSchoolExportRecordsDb.Set()
- marshal, _ := json.Marshal(req)
- centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
- Name: fileName,
- DownloadPath: downloadPath,
- Kind: 9, //数据对比
- ReqContent: string(marshal),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- }
-
- func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
- mealZh = "未知"
- mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
- for _, v := range md.SelfSupportForMealTimeMapping {
- //判断时间大小
- tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
- tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
- fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
- fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
- if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
- mealZh = v["name"]
- }
- }
- return
- }
|