|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046 |
- 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(date1.Sub(date2).Hours() / 24)
- for i := 0; i <= diffDays; i++ {
- date := date1.AddDate(0, 0, i)
- shopName := date.Format("2006-01-02")
-
- 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
- }
-
- 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 kk, vv := range v {
- 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
- }
-
- //新增数据
- 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 {
- xlsx := excelize.NewFile()
- switch kind {
- case 1:
- //订单列表
- 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:
- //退款申请表
- 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:
- //学校预定统计表
- 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,
- req.StartDate, req.EndDate, req.MealKindList)
- 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:
- //班级明细表
- 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,
- req.StartDate, req.EndDate, req.MealKindList)
- if err2 != nil {
- logx.Error(err)
- println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
- return
- }
- if count == 0 {
- continue
- }
- time.Sleep(20 * time.Millisecond) // 20毫秒
-
- 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:
- //退款明细表
- 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:
- //收款统计表
- 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:
- //班级收款明细表
- 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:
- //学校预定数量统计表
- 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,
- req.StartDate, req.EndDate, req.MealKindList)
- 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, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
- 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, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
- 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, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
- 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
- }
-
- //新增数据
- 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
- }
|