1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485 |
- package util
- import (
- "fmt"
- "log"
- "net/smtp"
- "os"
- "strconv"
- "strings"
- "time"
- "github.com/360EntSecGroup-Skylar/excelize"
- "github.com/jordan-wright/email"
- "github.com/robfig/cron"
- "kpt.xdmy/apiserver/routers/restful"
- "kpt.xdmy/pkg/logging"
- "kpt.xdmy/pkg/setting"
- )
- // 邮件相关(现代牧业暂未启用)
- func SendMail() {
- defer func() { // 必须要先声明defer
- if err := recover(); err != nil {
- logging.Error(" SendMail pnic err", err) // 这里的err其实就是panic传入的内容,55
- }
- }()
- log.Println("******** ******* *******", time.Now())
- e := email.NewEmail()
- // 发件人
- fromName := ""
- pas := ""
- mails := make([]string, 0)
- //
- subject := ""
- note := ""
- mailreport, err := restful.MapStrList("select * from mailreport where enable = 1")
- if err != nil {
- println("err sql", err.Error())
- } else {
- for _, value := range mailreport {
- //GetMailDataByPasture("集团设备指标",value["name"],value["id"],A,f)
- //GetPastureDeptData(value["name"],value["id"],f)
- if value["type"] == "1" {
- fromName = value["addr"]
- pas = value["pas"]
- subject = value["title"]
- note = value["note"]
- } else {
- mails = append(mails, value["addr"])
- }
- println(value["addr"])
- }
- }
- // 发件人
- //e.From = "864919639@qq.com"
- e.From = fromName
- // 收件人(可以有多个)
- e.To = mails
- //e.To = []string{"18322596935@163.com","1406599862@qq.com"} //"1406599862@qq.com"
- // 邮件主题
- //e.Subject = "集团设备指标报表"
- e.Subject = subject
- // 解析html模板
- //t,err := template.ParseFiles("email-template.html")
- //body := new(bytes.Buffer)
- // Execute方法将解析好的模板应用到匿名结构体上,并将输出写入body中
- e.Text = []byte(note)
- // html形式的消息
- //body.Write([]byte("测试"))
- //// 从缓冲中将内容作为附件到邮件中
- //e.Attach(body, "email-template.html", "text/html")
- // 以路径将文件作为附件添加到邮件中
- //_,err := GetMailData()
- filename := setting.CurrentPath + "uploads/mailreport/" + time.Now().Format("2006-01-02") + "集团设备指标.xlsx"
- e.AttachFile(filename)
- // 发送邮件(如果使用QQ邮箱发送邮件的话,passwd不是邮箱密码而是授权码)
- err = e.Send("smtp.qq.com:587", smtp.PlainAuth("", fromName, pas, "smtp.qq.com"))
- if err != nil {
- println("sent err", err.Error())
- } else {
- println("发送成功")
- }
- }
- func CronMail() {
- log.Println("Starting Cron...")
- defer func() { // 必须要先声明defer
- if err := recover(); err != nil {
- fmt.Printf("CronTest pnic err%+v \n", err)
- logging.Error("panic recover err ", err)
- //println("pnic err",err.(error).Error()) // 这里的err其实就是panic传入的内容,55
- }
- }()
- c := cron.New()
- err := c.AddFunc(setting.ServerSetting.CronFormula, SendMail) //2 * * * * *, 2 表示每分钟的第2s执行一次
- if err != nil {
- println("cron1 err", err.Error())
- err = c.AddFunc("0 9 * * *", SendMail) //2 * * * * *, 2 表示每分钟的第2s执行一次
- if err != nil {
- println("cron2 err", err.Error())
- } else {
- println("开始启动定时任务发送邮件 0 9 * * *")
- }
- } else {
- println("开始启动定时任务发送邮件 ", setting.ServerSetting.CronFormula)
- }
- err = c.AddFunc("0 1 * * *", GetMailData) //2 * * * * *, 2 表示每分钟的第2s执行一次
- if err != nil {
- println("cron3 err", err.Error())
- }
- //_, err = c.AddFunc("30 * * * *", GetMailData) //2 * * * * *, 2 表示每分钟的第2s执行一次
- //if err != nil {
- // println("cron4 err", err.Error())
- //}
- c.Start()
- }
- func GetMailData() {
- defer func() { // 必须要先声明defer
- if err := recover(); err != nil {
- logging.Error("GetMailData pnic err", err) // 这里的err其实就是panic传入的内容,55
- }
- }()
- f := excelize.NewFile()
- f.SetSheetName("Sheet1", "集团设备指标")
- //f.GetSheetMap()
- //f.NewSheet("集团设备指标")
- //f.SetSheetVisible(,false)
- //隐藏名称为 Sheet1 的工作表中的 D 至 F 列:
- //f.SetColVisible("Sheet1", "D:F", false)
- //根据给定的工作表名称(大小写敏感)、列范围和宽度值设置单个或多个列的宽度。
- //例如设置名为 Sheet1 工作表上 A 到 D 列的宽度为 20:
- // 设置单元格的值
- //style, err := f.NewStyle(`{"fill":{"type":"center"}}`)
- //if err != nil {
- // fmt.Println(err)
- //}
- //f.SetCellStyle("集团设备指标", "B1", "M1", style)
- //
- //f.MergeCell("集团设备指标", "N1", "T1")
- //err := SetSheetRow(f,"集团设备指标", "B1", &[]interface{}{"1月", nil,nil,nil,nil,nil,nil,"2月", nil,nil,nil,nil,nil,nil,
- // "3月", nil,nil,nil,nil,nil,nil,"4月", nil,nil,nil,nil,nil,nil,"5月", nil,nil,nil,nil,nil,nil,"6月", nil,nil,nil,nil,nil,nil,"1月", nil,nil,nil,nil,nil,nil,
- // "1月", nil,nil,nil,nil,nil,nil,"1月", nil,nil,nil,nil,nil,nil,"1月", nil,nil,nil,nil,nil,nil,"1月", nil,nil,nil,nil,nil,nil,"1月", nil,nil,nil,nil,nil,nil})
- tem := make([]interface{}, 0)
- tem1 := make([]interface{}, 0)
- c := []interface{}{}
- d := []interface{}{}
- e := []interface{}{}
- g := []interface{}{}
- h := []interface{}{}
- o := []interface{}{}
- p := []interface{}{}
- r := []interface{}{}
- for i := 1; i <= 12; i++ {
- a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
- b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
- c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- tem = append(tem, a...)
- tem1 = append(tem1, b...)
- }
- //d := c
- //e := c
- //g := c
- //h := c
- //o := c
- //p := c
- if err := SetSheetRow(f, "集团设备指标", "N1", &tem); err != nil {
- println(err.Error())
- }
- year := time.Now().Add(-24 * time.Hour).Format("2006")
- f.SetCellValue("集团设备指标", "A1", "时间")
- f.MergeCell("集团设备指标", "B1", "M1")
- f.MergeCell("集团设备指标", "N1", "T1")
- f.MergeCell("集团设备指标", "U1", "AA1")
- f.MergeCell("集团设备指标", "AB1", "AH1")
- f.MergeCell("集团设备指标", "AI1", "AO1")
- f.MergeCell("集团设备指标", "AP1", "AV1")
- f.MergeCell("集团设备指标", "AW1", "BC1")
- f.MergeCell("集团设备指标", "BD1", "BJ1")
- f.MergeCell("集团设备指标", "BK1", "BQ1")
- f.MergeCell("集团设备指标", "BR1", "BX1")
- f.MergeCell("集团设备指标", "BY1", "CE1")
- f.MergeCell("集团设备指标", "CF1", "CL1")
- f.MergeCell("集团设备指标", "CM1", "CS1")
- f.SetCellValue("集团设备指标", "H1", year+"年")
- SetSheetRow(f, "集团设备指标", "A2", &[]interface{}{"牧场", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率", "迄今饲养头日", "迄今产量(公斤)", "迄今实际单头牛成本", "迄今实际公斤奶成本(元/公斤)"})
- if err := SetSheetRow(f, "集团设备指标", "N2", &tem1); err != nil {
- println(err.Error())
- }
- //
- A := 3
- pastures, err := restful.MapStrList("select id,name from pasture where isdel = 0 AND id <> 18 order by sort")
- if err != nil {
- println("err sql", err.Error())
- } else {
- for _, value := range pastures {
- GetMailDataByPasture("集团设备指标", value["name"], time.Now().Format("2006-01-02"), value["id"], A, f)
- GetPastureDeptData(value["name"], value["id"], f)
- println(value["name"])
- A = A + 8
- }
- }
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJY")
- valuesMap1, err1 := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJM")
- sqls := ""
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = time.Now().Format("2006-01-02")
- s_params := make([]interface{}, 0)
- if err == nil && len(valuesMap) > 0 {
- sqls = valuesMap["sqlstr"]
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- tem = []interface{}{}
- values, err := restful.MapStrList(sqls, s_params...)
- B := A
- temMap := make(map[string][]interface{}, 0)
- if err == nil {
- for _, value := range values {
- tem = append(tem, value["ftype"])
- tem = append(tem, value["monthBudget"])
- tem = append(tem, value["monthLimit"])
- tem = append(tem, value["qmonthBudget"])
- tem = append(tem, value["sumPrice"])
- tem = append(tem, value["asave"])
- tem = append(tem, value["dac"])
- tem = append(tem, value["dacY"])
- tem = append(tem, value["cowNum"])
- tem = append(tem, value["saleNum"])
- tem = append(tem, value["cowprice"])
- tem = append(tem, value["milkprice"])
- temMap[value["ftype"]] = tem
- tem = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if m, ok := temMap["维修费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"维修费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["燃动费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"燃动费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["水费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"水费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["设备指标(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"设备指标(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电量(度)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电量(度)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["用水量(吨)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"用水量(吨)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["柴油用量(L)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"柴油用量(L)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- s_params = make([]interface{}, 0)
- if err1 == nil && len(valuesMap1) > 0 {
- sqls = valuesMap1["sqlstr"]
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = time.Now().Format("2006-01-02")
- paramslist := strings.Split(valuesMap1["params"], ",")
- if len(paramslist) > 0 && valuesMap1["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- f.SetCellValue("集团设备指标", "A"+strconv.Itoa(A), "集团")
- f.MergeCell("集团设备指标", "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
- values, err = restful.MapStrList(sqls, s_params...)
- tem = []interface{}{}
- tem2 := []interface{}{}
- if err == nil {
- for _, value := range values {
- tem2 = append(tem2, value["monthBudget"])
- tem2 = append(tem2, value["monthLimit"])
- tem2 = append(tem2, value["qmonthBudget"])
- tem2 = append(tem2, value["sumPrice"])
- tem2 = append(tem2, value["asave"])
- tem2 = append(tem2, value["dac"])
- tem2 = append(tem2, value["dacY"])
- num, _ := strconv.Atoi(value["num"])
- switch value["ftype"] {
- case "维修费(万元)":
- c[(num-1)*7] = tem2[0]
- c[(num-1)*7+1] = tem2[1]
- c[(num-1)*7+2] = tem2[2]
- c[(num-1)*7+3] = tem2[3]
- c[(num-1)*7+4] = tem2[4]
- c[(num-1)*7+5] = tem2[5]
- c[(num-1)*7+6] = tem2[6]
- case "电费(万元)":
- d[(num-1)*7] = tem2[0]
- d[(num-1)*7+1] = tem2[1]
- d[(num-1)*7+2] = tem2[2]
- d[(num-1)*7+3] = tem2[3]
- d[(num-1)*7+4] = tem2[4]
- d[(num-1)*7+5] = tem2[5]
- d[(num-1)*7+6] = tem2[6]
- case "燃动费(万元)":
- e[(num-1)*7] = tem2[0]
- e[(num-1)*7+1] = tem2[1]
- e[(num-1)*7+2] = tem2[2]
- e[(num-1)*7+3] = tem2[3]
- e[(num-1)*7+4] = tem2[4]
- e[(num-1)*7+5] = tem2[5]
- e[(num-1)*7+6] = tem2[6]
- case "水费(万元)":
- g[(num-1)*7] = tem2[0]
- g[(num-1)*7+1] = tem2[1]
- g[(num-1)*7+2] = tem2[2]
- g[(num-1)*7+3] = tem2[3]
- g[(num-1)*7+4] = tem2[4]
- g[(num-1)*7+5] = tem2[5]
- g[(num-1)*7+6] = tem2[6]
- case "电量(度)":
- h[(num-1)*7] = tem2[0]
- h[(num-1)*7+1] = tem2[1]
- h[(num-1)*7+2] = tem2[2]
- h[(num-1)*7+3] = tem2[3]
- h[(num-1)*7+4] = tem2[4]
- h[(num-1)*7+5] = tem2[5]
- h[(num-1)*7+6] = tem2[6]
- case "柴油用量(L)":
- o[(num-1)*7] = tem2[0]
- o[(num-1)*7+1] = tem2[1]
- o[(num-1)*7+2] = tem2[2]
- o[(num-1)*7+3] = tem2[3]
- o[(num-1)*7+4] = tem2[4]
- o[(num-1)*7+5] = tem2[5]
- o[(num-1)*7+6] = tem2[6]
- case "用水量(吨)":
- p[(num-1)*7] = tem2[0]
- p[(num-1)*7+1] = tem2[1]
- p[(num-1)*7+2] = tem2[2]
- p[(num-1)*7+3] = tem2[3]
- p[(num-1)*7+4] = tem2[4]
- p[(num-1)*7+5] = tem2[5]
- p[(num-1)*7+6] = tem2[6]
- case "设备指标(万元)":
- r[(num-1)*7] = tem2[0]
- r[(num-1)*7+1] = tem2[1]
- r[(num-1)*7+2] = tem2[2]
- r[(num-1)*7+3] = tem2[3]
- r[(num-1)*7+4] = tem2[4]
- r[(num-1)*7+5] = tem2[5]
- r[(num-1)*7+6] = tem2[6]
- }
- tem2 = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A), &c); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+1), &d); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+2), &e); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+3), &g); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+4), &r); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+5), &h); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+6), &p); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+7), &o); err != nil {
- println(err.Error())
- }
- //sums[2] = sums[2].(int)+value["monthLimit"].(int)
- //sums[1] = sums[1].(int)+value["monthBudget"].(int)
- // 设置月份单元格字体
- monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
- if err != nil {
- println("monthStyle err", err.Error())
- }
- f.SetCellStyle("集团设备指标", "A1", "CS154", monthStyle)
- // 根据指定路径保存文件
- filename := setting.CurrentPath + "uploads/mailreport/" + time.Now().Format("2006-01-02") + "集团设备指标.xlsx"
- err = PathCheck(setting.CurrentPath + "uploads/mailreport") //检查路径并创建
- os.Remove(filename)
- if err != nil {
- println("PathCheck err", err)
- }
- if err := f.SaveAs(filename); err != nil {
- println(err.Error())
- }
- }
- func GetMailDataByPasture(sheetName, pastureName, date string, pastureId interface{}, B int, f *excelize.File) {
- tem := make([]interface{}, 0)
- tem1 := make([]interface{}, 0)
- c := []interface{}{}
- d := []interface{}{}
- e := []interface{}{}
- g := []interface{}{}
- h := []interface{}{}
- o := []interface{}{}
- p := []interface{}{}
- r := []interface{}{}
- for i := 1; i <= 12; i++ {
- a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
- b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
- c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- tem = append(tem, a...)
- tem1 = append(tem1, b...)
- }
- //
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMY")
- valuesMap1, err1 := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMM")
- sqls := ""
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- parammaps["pastureId"] = pastureId
- s_params := make([]interface{}, 0)
- if err == nil && len(valuesMap) > 0 {
- sqls = valuesMap["sqlstr"]
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- f.SetCellValue(sheetName, "A"+strconv.Itoa(B), pastureName)
- f.MergeCell(sheetName, "A"+strconv.Itoa(B), "A"+strconv.Itoa(B+7))
- tem = []interface{}{}
- temMap := make(map[string][]interface{}, 0)
- //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
- values, err := restful.MapStrList(sqls, s_params...)
- Bt := B
- if err == nil {
- for _, value := range values {
- tem = append(tem, value["ftype"])
- tem = append(tem, value["monthBudget"])
- tem = append(tem, value["monthLimit"])
- tem = append(tem, value["qmonthBudget"])
- tem = append(tem, value["sumPrice"])
- tem = append(tem, value["asave"])
- tem = append(tem, value["dac"])
- tem = append(tem, value["dacY"])
- tem = append(tem, value["cowNum"])
- tem = append(tem, value["saleNum"])
- tem = append(tem, value["cowprice"])
- tem = append(tem, value["milkprice"])
- temMap[value["ftype"]] = tem
- //if err := SetSheetRow(f,sheetName, "B"+strconv.Itoa(Bt),&tem); err != nil {
- // println(err.Error())
- //}
- tem = []interface{}{}
- //Bt++
- }
- } else {
- println(err.Error())
- }
- if m, ok := temMap["维修费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"维修费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["电费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"电费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["燃动费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"燃动费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["水费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"水费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["设备指标(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"设备指标(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["电量(度)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"电量(度)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["用水量(吨)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"用水量(吨)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["柴油用量(L)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"柴油用量(L)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- s_params = make([]interface{}, 0)
- if err1 == nil && len(valuesMap1) > 0 {
- sqls = valuesMap1["sqlstr"]
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- parammaps["pastureId"] = pastureId
- paramslist := strings.Split(valuesMap1["params"], ",")
- if len(paramslist) > 0 && valuesMap1["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- values, err = restful.MapStrList(sqls, s_params...)
- tem = []interface{}{}
- tem2 := []interface{}{}
- if err == nil {
- for _, value := range values {
- tem2 = append(tem2, value["monthBudget"])
- tem2 = append(tem2, value["monthLimit"])
- tem2 = append(tem2, value["qmonthBudget"])
- tem2 = append(tem2, value["sumPrice"])
- tem2 = append(tem2, value["asave"])
- tem2 = append(tem2, value["dac"])
- tem2 = append(tem2, value["dacY"])
- num, _ := strconv.Atoi(value["num"])
- switch value["ftype"] {
- case "维修费(万元)":
- c[(num-1)*7] = tem2[0]
- c[(num-1)*7+1] = tem2[1]
- c[(num-1)*7+2] = tem2[2]
- c[(num-1)*7+3] = tem2[3]
- c[(num-1)*7+4] = tem2[4]
- c[(num-1)*7+5] = tem2[5]
- c[(num-1)*7+6] = tem2[6]
- case "电费(万元)":
- d[(num-1)*7] = tem2[0]
- d[(num-1)*7+1] = tem2[1]
- d[(num-1)*7+2] = tem2[2]
- d[(num-1)*7+3] = tem2[3]
- d[(num-1)*7+4] = tem2[4]
- d[(num-1)*7+5] = tem2[5]
- d[(num-1)*7+6] = tem2[6]
- case "燃动费(万元)":
- e[(num-1)*7] = tem2[0]
- e[(num-1)*7+1] = tem2[1]
- e[(num-1)*7+2] = tem2[2]
- e[(num-1)*7+3] = tem2[3]
- e[(num-1)*7+4] = tem2[4]
- e[(num-1)*7+5] = tem2[5]
- e[(num-1)*7+6] = tem2[6]
- case "水费(万元)":
- g[(num-1)*7] = tem2[0]
- g[(num-1)*7+1] = tem2[1]
- g[(num-1)*7+2] = tem2[2]
- g[(num-1)*7+3] = tem2[3]
- g[(num-1)*7+4] = tem2[4]
- g[(num-1)*7+5] = tem2[5]
- g[(num-1)*7+6] = tem2[6]
- case "电量(度)":
- h[(num-1)*7] = tem2[0]
- h[(num-1)*7+1] = tem2[1]
- h[(num-1)*7+2] = tem2[2]
- h[(num-1)*7+3] = tem2[3]
- h[(num-1)*7+4] = tem2[4]
- h[(num-1)*7+5] = tem2[5]
- h[(num-1)*7+6] = tem2[6]
- case "柴油用量(L)":
- o[(num-1)*7] = tem2[0]
- o[(num-1)*7+1] = tem2[1]
- o[(num-1)*7+2] = tem2[2]
- o[(num-1)*7+3] = tem2[3]
- o[(num-1)*7+4] = tem2[4]
- o[(num-1)*7+5] = tem2[5]
- o[(num-1)*7+6] = tem2[6]
- case "用水量(吨)":
- p[(num-1)*7] = tem2[0]
- p[(num-1)*7+1] = tem2[1]
- p[(num-1)*7+2] = tem2[2]
- p[(num-1)*7+3] = tem2[3]
- p[(num-1)*7+4] = tem2[4]
- p[(num-1)*7+5] = tem2[5]
- p[(num-1)*7+6] = tem2[6]
- case "设备指标(万元)":
- r[(num-1)*7] = tem2[0]
- r[(num-1)*7+1] = tem2[1]
- r[(num-1)*7+2] = tem2[2]
- r[(num-1)*7+3] = tem2[3]
- r[(num-1)*7+4] = tem2[4]
- r[(num-1)*7+5] = tem2[5]
- r[(num-1)*7+6] = tem2[6]
- }
- tem2 = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B), &c); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B+1), &d); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B+2), &e); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B+3), &g); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B+4), &r); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B+5), &h); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B+6), &p); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "N"+strconv.Itoa(B+7), &o); err != nil {
- println(err.Error())
- }
- }
- func GetMailDataByDept(sheetName, DeptName, date string, DeptId, pastureId interface{}, B int, f *excelize.File) {
- tem := make([]interface{}, 0)
- tem1 := make([]interface{}, 0)
- c := []interface{}{}
- d := []interface{}{}
- e := []interface{}{}
- g := []interface{}{}
- h := []interface{}{}
- o := []interface{}{}
- p := []interface{}{}
- r := []interface{}{}
- for i := 1; i <= 12; i++ {
- a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
- b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
- c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- tem = append(tem, a...)
- tem1 = append(tem1, b...)
- }
- //
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportBY")
- valuesMap1, err1 := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportBM")
- sqls := ""
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- parammaps["pastureId"] = pastureId
- parammaps["departmentId"] = DeptId
- s_params := make([]interface{}, 0)
- if err == nil && len(valuesMap) > 0 {
- sqls = valuesMap["sqlstr"]
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- f.SetCellValue(sheetName, "A"+strconv.Itoa(B), DeptName)
- f.MergeCell(sheetName, "A"+strconv.Itoa(B), "A"+strconv.Itoa(B+7))
- tem = []interface{}{}
- temMap := make(map[string][]interface{}, 0)
- //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
- values, err := restful.MapStrList(sqls, s_params...)
- Bt := B
- if err == nil {
- for _, value := range values {
- tem = append(tem, value["ftype"])
- tem = append(tem, value["monthBudget"])
- tem = append(tem, value["monthLimit"])
- tem = append(tem, value["qmonthBudget"])
- tem = append(tem, value["sumPrice"])
- tem = append(tem, value["asave"])
- tem = append(tem, value["dac"])
- tem = append(tem, value["dacY"])
- tem = append(tem, value["cowNum"])
- tem = append(tem, value["saleNum"])
- tem = append(tem, value["cowprice"])
- tem = append(tem, value["milkprice"])
- temMap[value["ftype"]] = tem
- tem = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if m, ok := temMap["维修费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"维修费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["电费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"电费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["燃动费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"燃动费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["水费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"水费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["设备指标(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"设备指标(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["电量(度)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"电量(度)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["用水量(吨)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"用水量(吨)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- if m, ok := temMap["柴油用量(L)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
- println(err.Error())
- }
- Bt++
- } else {
- temw := []interface{}{"柴油用量(L)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
- println(err.Error())
- }
- Bt++
- }
- s_params = make([]interface{}, 0)
- if err1 == nil && len(valuesMap1) > 0 {
- sqls = valuesMap1["sqlstr"]
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- parammaps["pastureId"] = pastureId
- parammaps["departmentId"] = DeptId
- paramslist := strings.Split(valuesMap1["params"], ",")
- if len(paramslist) > 0 && valuesMap1["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- values, err = restful.MapStrList(sqls, s_params...)
- tem = []interface{}{}
- tem2 := []interface{}{}
- if err == nil {
- for _, value := range values {
- //fmt.Printf("va============== %+v\n",value)
- tem2 = append(tem2, value["monthBudget"])
- tem2 = append(tem2, value["monthLimit"])
- tem2 = append(tem2, value["qmonthBudget"])
- tem2 = append(tem2, value["sumPrice"])
- tem2 = append(tem2, value["asave"])
- tem2 = append(tem2, value["dac"])
- tem2 = append(tem2, value["dacY"])
- num := 1
- if _, ok := value["num"]; ok {
- num, _ = strconv.Atoi(value["num"])
- }
- switch value["ftype"] {
- case "维修费(万元)":
- c[(num-1)*7] = tem2[0]
- c[(num-1)*7+1] = tem2[1]
- c[(num-1)*7+2] = tem2[2]
- c[(num-1)*7+3] = tem2[3]
- c[(num-1)*7+4] = tem2[4]
- c[(num-1)*7+5] = tem2[5]
- c[(num-1)*7+6] = tem2[6]
- case "电费(万元)":
- d[(num-1)*7] = tem2[0]
- d[(num-1)*7+1] = tem2[1]
- d[(num-1)*7+2] = tem2[2]
- d[(num-1)*7+3] = tem2[3]
- d[(num-1)*7+4] = tem2[4]
- d[(num-1)*7+5] = tem2[5]
- d[(num-1)*7+6] = tem2[6]
- case "燃动费(万元)":
- e[(num-1)*7] = tem2[0]
- e[(num-1)*7+1] = tem2[1]
- e[(num-1)*7+2] = tem2[2]
- e[(num-1)*7+3] = tem2[3]
- e[(num-1)*7+4] = tem2[4]
- e[(num-1)*7+5] = tem2[5]
- e[(num-1)*7+6] = tem2[6]
- case "水费(万元)":
- g[(num-1)*7] = tem2[0]
- g[(num-1)*7+1] = tem2[1]
- g[(num-1)*7+2] = tem2[2]
- g[(num-1)*7+3] = tem2[3]
- g[(num-1)*7+4] = tem2[4]
- g[(num-1)*7+5] = tem2[5]
- g[(num-1)*7+6] = tem2[6]
- case "电量(度)":
- h[(num-1)*7] = tem2[0]
- h[(num-1)*7+1] = tem2[1]
- h[(num-1)*7+2] = tem2[2]
- h[(num-1)*7+3] = tem2[3]
- h[(num-1)*7+4] = tem2[4]
- h[(num-1)*7+5] = tem2[5]
- h[(num-1)*7+6] = tem2[6]
- case "柴油用量(L)":
- o[(num-1)*7] = tem2[0]
- o[(num-1)*7+1] = tem2[1]
- o[(num-1)*7+2] = tem2[2]
- o[(num-1)*7+3] = tem2[3]
- o[(num-1)*7+4] = tem2[4]
- o[(num-1)*7+5] = tem2[5]
- o[(num-1)*7+6] = tem2[6]
- case "用水量(吨)":
- p[(num-1)*7] = tem2[0]
- p[(num-1)*7+1] = tem2[1]
- p[(num-1)*7+2] = tem2[2]
- p[(num-1)*7+3] = tem2[3]
- p[(num-1)*7+4] = tem2[4]
- p[(num-1)*7+5] = tem2[5]
- p[(num-1)*7+6] = tem2[6]
- case "设备指标(万元)":
- r[(num-1)*7] = tem2[0]
- r[(num-1)*7+1] = tem2[1]
- r[(num-1)*7+2] = tem2[2]
- r[(num-1)*7+3] = tem2[3]
- r[(num-1)*7+4] = tem2[4]
- r[(num-1)*7+5] = tem2[5]
- r[(num-1)*7+6] = tem2[6]
- }
- tem2 = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B), &c); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B+1), &d); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B+2), &e); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B+3), &g); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B+4), &r); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B+5), &h); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B+6), &p); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(B+7), &o); err != nil {
- println(err.Error())
- }
- }
- func GetPastureDeptData(sheetName string, pastureId interface{}, f *excelize.File) {
- f.NewSheet(sheetName)
- tem := make([]interface{}, 0)
- tem1 := make([]interface{}, 0)
- c := []interface{}{}
- d := []interface{}{}
- e := []interface{}{}
- g := []interface{}{}
- h := []interface{}{}
- o := []interface{}{}
- p := []interface{}{}
- r := []interface{}{}
- for i := 1; i <= 12; i++ {
- a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
- b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
- c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- tem = append(tem, a...)
- tem1 = append(tem1, b...)
- }
- //d := c
- //e := c
- //g := c
- //h := c
- //o := c
- //p := c
- if err := SetSheetRow(f, sheetName, "J1", &tem); err != nil {
- println(err.Error())
- }
- year := time.Now().Add(-24 * time.Hour).Format("2006")
- f.SetCellValue(sheetName, "A1", "时间")
- f.MergeCell(sheetName, "B1", "I1")
- f.MergeCell(sheetName, "J1", "P1")
- f.MergeCell(sheetName, "Q1", "W1")
- f.MergeCell(sheetName, "X1", "AD1")
- f.MergeCell(sheetName, "AE1", "AK1")
- f.MergeCell(sheetName, "AL1", "AR1")
- f.MergeCell(sheetName, "AS1", "AY1")
- f.MergeCell(sheetName, "AZ1", "BF1")
- f.MergeCell(sheetName, "BG1", "BM1")
- f.MergeCell(sheetName, "BN1", "BT1")
- f.MergeCell(sheetName, "BU1", "CA1")
- f.MergeCell(sheetName, "CB1", "CH1")
- f.MergeCell(sheetName, "CI1", "CO1")
- f.SetCellValue(sheetName, "H1", year+"年")
- SetSheetRow(f, sheetName, "A2", &[]interface{}{"部门", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率"})
- if err := SetSheetRow(f, sheetName, "J2", &tem1); err != nil {
- println(err.Error())
- }
- //
- A := 3
- pastures, err := restful.MapStrList("select id,name from department where pastureId=? and isDel = 0 AND enable =1 and id <> ?", pastureId, pastureId)
- if err != nil {
- println("err sql", err.Error())
- } else {
- for _, value := range pastures {
- GetMailDataByDept(sheetName, value["name"], time.Now().Format("2006-01-02"), value["id"], pastureId, A, f)
- println(value["name"])
- A = A + 8
- }
- }
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMY")
- valuesMap1, err1 := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMM")
- sqls := ""
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = time.Now().Format("2006-01-02")
- parammaps["pastureId"] = pastureId
- s_params := make([]interface{}, 0)
- if err == nil && len(valuesMap) > 0 {
- sqls = valuesMap["sqlstr"]
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- tem = []interface{}{}
- //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
- values, err := restful.MapStrList(sqls, s_params...)
- B := A
- temMap := make(map[string][]interface{}, 0)
- if err == nil {
- for _, value := range values {
- tem = append(tem, value["ftype"])
- tem = append(tem, value["monthBudget"])
- tem = append(tem, value["monthLimit"])
- tem = append(tem, value["qmonthBudget"])
- tem = append(tem, value["sumPrice"])
- tem = append(tem, value["asave"])
- tem = append(tem, value["dac"])
- tem = append(tem, value["dacY"])
- temMap[value["ftype"]] = tem
- //if err := SetSheetRow(f,sheetName, "B"+strconv.Itoa(B),&tem); err != nil {
- // println(err.Error())
- //}
- tem = []interface{}{}
- //B++
- }
- } else {
- println(err.Error())
- }
- if m, ok := temMap["维修费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"维修费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["燃动费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"燃动费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["水费(万元)"]; ok {
- if err :=
- SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"水费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["设备指标(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"设备指标(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电量(度)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电量(度)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["用水量(吨)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"用水量(吨)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["柴油用量(L)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"柴油用量(L)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- s_params = make([]interface{}, 0)
- if err1 == nil && len(valuesMap1) > 0 {
- sqls = valuesMap1["sqlstr"]
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = time.Now().Format("2006-01-02")
- parammaps["pastureId"] = pastureId
- paramslist := strings.Split(valuesMap1["params"], ",")
- if len(paramslist) > 0 && valuesMap1["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- f.SetCellValue(sheetName, "A"+strconv.Itoa(A), sheetName)
- f.MergeCell(sheetName, "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
- values, err = restful.MapStrList(sqls, s_params...)
- tem = []interface{}{}
- //d := append(c,nil)
- //e := append(c,nil)
- //g := append(c,nil)
- //h := append(c,nil)
- //o := append(c,nil)
- //p := append(c,nil)
- tem2 := []interface{}{}
- if err == nil {
- for _, value := range values {
- tem2 = append(tem2, value["monthBudget"])
- tem2 = append(tem2, value["monthLimit"])
- tem2 = append(tem2, value["qmonthBudget"])
- tem2 = append(tem2, value["sumPrice"])
- tem2 = append(tem2, value["asave"])
- tem2 = append(tem2, value["dac"])
- tem2 = append(tem2, value["dacY"])
- num, _ := strconv.Atoi(value["num"])
- switch value["ftype"] {
- case "维修费(万元)":
- c[(num-1)*7] = tem2[0]
- c[(num-1)*7+1] = tem2[1]
- c[(num-1)*7+2] = tem2[2]
- c[(num-1)*7+3] = tem2[3]
- c[(num-1)*7+4] = tem2[4]
- c[(num-1)*7+5] = tem2[5]
- c[(num-1)*7+6] = tem2[6]
- case "电费(万元)":
- d[(num-1)*7] = tem2[0]
- d[(num-1)*7+1] = tem2[1]
- d[(num-1)*7+2] = tem2[2]
- d[(num-1)*7+3] = tem2[3]
- d[(num-1)*7+4] = tem2[4]
- d[(num-1)*7+5] = tem2[5]
- d[(num-1)*7+6] = tem2[6]
- case "燃动费(万元)":
- e[(num-1)*7] = tem2[0]
- e[(num-1)*7+1] = tem2[1]
- e[(num-1)*7+2] = tem2[2]
- e[(num-1)*7+3] = tem2[3]
- e[(num-1)*7+4] = tem2[4]
- e[(num-1)*7+5] = tem2[5]
- e[(num-1)*7+6] = tem2[6]
- case "水费(万元)":
- g[(num-1)*7] = tem2[0]
- g[(num-1)*7+1] = tem2[1]
- g[(num-1)*7+2] = tem2[2]
- g[(num-1)*7+3] = tem2[3]
- g[(num-1)*7+4] = tem2[4]
- g[(num-1)*7+5] = tem2[5]
- g[(num-1)*7+6] = tem2[6]
- case "电量(度)":
- h[(num-1)*7] = tem2[0]
- h[(num-1)*7+1] = tem2[1]
- h[(num-1)*7+2] = tem2[2]
- h[(num-1)*7+3] = tem2[3]
- h[(num-1)*7+4] = tem2[4]
- h[(num-1)*7+5] = tem2[5]
- h[(num-1)*7+6] = tem2[6]
- case "柴油用量(L)":
- o[(num-1)*7] = tem2[0]
- o[(num-1)*7+1] = tem2[1]
- o[(num-1)*7+2] = tem2[2]
- o[(num-1)*7+3] = tem2[3]
- o[(num-1)*7+4] = tem2[4]
- o[(num-1)*7+5] = tem2[5]
- o[(num-1)*7+6] = tem2[6]
- case "用水量(吨)":
- p[(num-1)*7] = tem2[0]
- p[(num-1)*7+1] = tem2[1]
- p[(num-1)*7+2] = tem2[2]
- p[(num-1)*7+3] = tem2[3]
- p[(num-1)*7+4] = tem2[4]
- p[(num-1)*7+5] = tem2[5]
- p[(num-1)*7+6] = tem2[6]
- case "设备指标(万元)":
- r[(num-1)*7] = tem2[0]
- r[(num-1)*7+1] = tem2[1]
- r[(num-1)*7+2] = tem2[2]
- r[(num-1)*7+3] = tem2[3]
- r[(num-1)*7+4] = tem2[4]
- r[(num-1)*7+5] = tem2[5]
- r[(num-1)*7+6] = tem2[6]
- }
- tem2 = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A), &c); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+1), &d); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+2), &e); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+3), &g); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+4), &r); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+5), &h); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+6), &p); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+7), &o); err != nil {
- println(err.Error())
- }
- //sums[2] = sums[2].(int)+value["monthLimit"].(int)
- //sums[1] = sums[1].(int)+value["monthBudget"].(int)
- // 设置月份单元格字体
- monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
- if err != nil {
- println("monthStyle err", err.Error())
- }
- f.SetCellStyle(sheetName, "A1", "CS300", monthStyle)
- // 根据指定路径保存文件
- }
- func PathCheck(path string) (err error) {
- b, err := PathExists(path)
- if err != nil {
- println("exist err", err)
- }
- if !b {
- err = os.Mkdir(path, 0777)
- if err != nil {
- println("Mkdir err", err)
- }
- }
- return
- }
- func PathExists(path string) (bool, error) {
- _, err := os.Stat(path)
- if err == nil {
- return true, nil
- }
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
- }
- func DownloadDataJT(date string) (string, error) {
- filename := setting.CurrentPath + "uploads/Downloadmailreport/" + date + "集团设备指标.xlsx"
- exist, _ := FileExists(filename)
- println(filename, exist)
- if exist {
- //return filename, nil
- }
- f := excelize.NewFile()
- f.SetSheetName("Sheet1", "集团设备指标")
- tem := make([]interface{}, 0)
- tem1 := make([]interface{}, 0)
- c := []interface{}{}
- d := []interface{}{}
- e := []interface{}{}
- g := []interface{}{}
- h := []interface{}{}
- o := []interface{}{}
- p := []interface{}{}
- r := []interface{}{}
- for i := 1; i <= 12; i++ {
- a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
- b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
- c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- tem = append(tem, a...)
- tem1 = append(tem1, b...)
- }
- if err := SetSheetRow(f, "集团设备指标", "N1", &tem); err != nil {
- println(err.Error())
- }
- //year :=time.Now().Add(-24*time.Hour).Format("2006")
- year := ""
- datetem, _ := strconv.Atoi(date[:4])
- if date > time.Now().Add(-24*time.Hour).Format("2006")+"-01-01" {
- year = strconv.Itoa(datetem)
- } else {
- year = strconv.Itoa(datetem - 1)
- }
- f.SetCellValue("集团设备指标", "A1", "时间")
- f.MergeCell("集团设备指标", "B1", "M1")
- f.MergeCell("集团设备指标", "N1", "T1")
- f.MergeCell("集团设备指标", "U1", "AA1")
- f.MergeCell("集团设备指标", "AB1", "AH1")
- f.MergeCell("集团设备指标", "AI1", "AO1")
- f.MergeCell("集团设备指标", "AP1", "AV1")
- f.MergeCell("集团设备指标", "AW1", "BC1")
- f.MergeCell("集团设备指标", "BD1", "BJ1")
- f.MergeCell("集团设备指标", "BK1", "BQ1")
- f.MergeCell("集团设备指标", "BR1", "BX1")
- f.MergeCell("集团设备指标", "BY1", "CE1")
- f.MergeCell("集团设备指标", "CF1", "CL1")
- f.MergeCell("集团设备指标", "CM1", "CS1")
- f.SetCellValue("集团设备指标", "H1", year+"年")
- SetSheetRow(f, "集团设备指标", "A2", &[]interface{}{"牧场", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率", "迄今饲养头日", "迄今产量(公斤)", "迄今实际单头牛成本", "迄今实际公斤奶成本(元/公斤)"})
- if err := SetSheetRow(f, "集团设备指标", "N2", &tem1); err != nil {
- println(err.Error())
- }
- A := 3
- pastures, err := restful.MapStrList("select id,name from pasture where isdel = 0 AND id <> 18 order by sort")
- if err != nil {
- println("err sql", err.Error())
- } else {
- for _, value := range pastures {
- GetMailDataByPasture("集团设备指标", value["name"], date, value["id"], A, f)
- println(value["name"])
- A = A + 8
- }
- }
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJY")
- valuesMap1, err1 := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJM")
- sqls := ""
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- s_params := make([]interface{}, 0)
- if err == nil && len(valuesMap) > 0 {
- sqls = valuesMap["sqlstr"]
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- tem = []interface{}{}
- values, err := restful.MapStrList(sqls, s_params...)
- B := A
- temMap := make(map[string][]interface{}, 0)
- if err == nil {
- for _, value := range values {
- tem = append(tem, value["ftype"])
- tem = append(tem, value["monthBudget"])
- tem = append(tem, value["monthLimit"])
- tem = append(tem, value["qmonthBudget"])
- tem = append(tem, value["sumPrice"])
- tem = append(tem, value["asave"])
- tem = append(tem, value["dac"])
- tem = append(tem, value["dacY"])
- tem = append(tem, value["cowNum"])
- tem = append(tem, value["saleNum"])
- tem = append(tem, value["cowprice"])
- tem = append(tem, value["milkprice"])
- temMap[value["ftype"]] = tem
- tem = []interface{}{}
- //B++
- }
- } else {
- println(err.Error())
- }
- if m, ok := temMap["维修费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"维修费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["燃动费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"燃动费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["水费(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"水费(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["设备指标(万元)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"设备指标(万元)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电量(度)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电量(度)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["用水量(吨)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"用水量(吨)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["柴油用量(L)"]; ok {
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"柴油用量(L)"}
- if err := SetSheetRow(f, "集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- s_params = make([]interface{}, 0)
- if err1 == nil && len(valuesMap1) > 0 {
- sqls = valuesMap1["sqlstr"]
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- paramslist := strings.Split(valuesMap1["params"], ",")
- if len(paramslist) > 0 && valuesMap1["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- f.SetCellValue("集团设备指标", "A"+strconv.Itoa(A), "集团")
- f.MergeCell("集团设备指标", "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
- values, err = restful.MapStrList(sqls, s_params...)
- tem = []interface{}{}
- tem2 := []interface{}{}
- if err == nil {
- for _, value := range values {
- tem2 = append(tem2, value["monthBudget"])
- tem2 = append(tem2, value["monthLimit"])
- tem2 = append(tem2, value["qmonthBudget"])
- tem2 = append(tem2, value["sumPrice"])
- tem2 = append(tem2, value["asave"])
- tem2 = append(tem2, value["dac"])
- tem2 = append(tem2, value["dacY"])
- num, _ := strconv.Atoi(value["num"])
- switch value["ftype"] {
- case "维修费(万元)":
- c[(num-1)*7] = tem2[0]
- c[(num-1)*7+1] = tem2[1]
- c[(num-1)*7+2] = tem2[2]
- c[(num-1)*7+3] = tem2[3]
- c[(num-1)*7+4] = tem2[4]
- c[(num-1)*7+5] = tem2[5]
- c[(num-1)*7+6] = tem2[6]
- case "电费(万元)":
- d[(num-1)*7] = tem2[0]
- d[(num-1)*7+1] = tem2[1]
- d[(num-1)*7+2] = tem2[2]
- d[(num-1)*7+3] = tem2[3]
- d[(num-1)*7+4] = tem2[4]
- d[(num-1)*7+5] = tem2[5]
- d[(num-1)*7+6] = tem2[6]
- case "燃动费(万元)":
- e[(num-1)*7] = tem2[0]
- e[(num-1)*7+1] = tem2[1]
- e[(num-1)*7+2] = tem2[2]
- e[(num-1)*7+3] = tem2[3]
- e[(num-1)*7+4] = tem2[4]
- e[(num-1)*7+5] = tem2[5]
- e[(num-1)*7+6] = tem2[6]
- case "水费(万元)":
- g[(num-1)*7] = tem2[0]
- g[(num-1)*7+1] = tem2[1]
- g[(num-1)*7+2] = tem2[2]
- g[(num-1)*7+3] = tem2[3]
- g[(num-1)*7+4] = tem2[4]
- g[(num-1)*7+5] = tem2[5]
- g[(num-1)*7+6] = tem2[6]
- case "电量(度)":
- h[(num-1)*7] = tem2[0]
- h[(num-1)*7+1] = tem2[1]
- h[(num-1)*7+2] = tem2[2]
- h[(num-1)*7+3] = tem2[3]
- h[(num-1)*7+4] = tem2[4]
- h[(num-1)*7+5] = tem2[5]
- h[(num-1)*7+6] = tem2[6]
- case "柴油用量(L)":
- o[(num-1)*7] = tem2[0]
- o[(num-1)*7+1] = tem2[1]
- o[(num-1)*7+2] = tem2[2]
- o[(num-1)*7+3] = tem2[3]
- o[(num-1)*7+4] = tem2[4]
- o[(num-1)*7+5] = tem2[5]
- o[(num-1)*7+6] = tem2[6]
- case "用水量(吨)":
- p[(num-1)*7] = tem2[0]
- p[(num-1)*7+1] = tem2[1]
- p[(num-1)*7+2] = tem2[2]
- p[(num-1)*7+3] = tem2[3]
- p[(num-1)*7+4] = tem2[4]
- p[(num-1)*7+5] = tem2[5]
- p[(num-1)*7+6] = tem2[6]
- case "设备指标(万元)":
- r[(num-1)*7] = tem2[0]
- r[(num-1)*7+1] = tem2[1]
- r[(num-1)*7+2] = tem2[2]
- r[(num-1)*7+3] = tem2[3]
- r[(num-1)*7+4] = tem2[4]
- r[(num-1)*7+5] = tem2[5]
- r[(num-1)*7+6] = tem2[6]
- }
- tem2 = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A), &c); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+1), &d); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+2), &e); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+3), &g); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+4), &r); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+5), &h); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+6), &p); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, "集团设备指标", "N"+strconv.Itoa(A+7), &o); err != nil {
- println(err.Error())
- }
- // 设置月份单元格字体
- monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
- if err != nil {
- println("monthStyle err", err.Error())
- }
- f.SetCellStyle("集团设备指标", "A1", "CS154", monthStyle)
- // 根据指定路径保存文件
- err = PathCheck("uploads/Downloadmailreport") //检查路径并创建
- //os.Remove(filename)
- if err != nil {
- println("PathCheck err", err)
- }
- if err := f.SaveAs(filename); err != nil {
- println(err.Error())
- return "", err
- }
- return filename, err
- }
- func DownloadDataMC(sheetName, date string, pastureId interface{}) (string, error) {
- filename := setting.CurrentPath + "uploads/Downloadmailreport/" + date + sheetName + "设备指标.xlsx"
- exist, _ := FileExists(filename)
- if exist {
- //return filename, nil
- }
- f := excelize.NewFile()
- f.SetSheetName("Sheet1", sheetName)
- tem := make([]interface{}, 0)
- tem1 := make([]interface{}, 0)
- c := []interface{}{}
- d := []interface{}{}
- e := []interface{}{}
- g := []interface{}{}
- h := []interface{}{}
- o := []interface{}{}
- p := []interface{}{}
- r := []interface{}{}
- for i := 1; i <= 12; i++ {
- a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
- b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
- c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
- tem = append(tem, a...)
- tem1 = append(tem1, b...)
- }
- if err := SetSheetRow(f, sheetName, "J1", &tem); err != nil {
- println(err.Error())
- }
- year := ""
- datetem, _ := strconv.Atoi(date[:4])
- if date > time.Now().Add(-24*time.Hour).Format("2006")+"-01-01" {
- year = strconv.Itoa(datetem)
- } else {
- year = strconv.Itoa(datetem - 1)
- }
- f.SetCellValue(sheetName, "A1", "时间")
- f.MergeCell(sheetName, "B1", "I1")
- f.MergeCell(sheetName, "J1", "P1")
- f.MergeCell(sheetName, "Q1", "W1")
- f.MergeCell(sheetName, "X1", "AD1")
- f.MergeCell(sheetName, "AE1", "AK1")
- f.MergeCell(sheetName, "AL1", "AR1")
- f.MergeCell(sheetName, "AS1", "AY1")
- f.MergeCell(sheetName, "AZ1", "BF1")
- f.MergeCell(sheetName, "BG1", "BM1")
- f.MergeCell(sheetName, "BN1", "BT1")
- f.MergeCell(sheetName, "BU1", "CA1")
- f.MergeCell(sheetName, "CB1", "CH1")
- f.MergeCell(sheetName, "CI1", "CO1")
- f.SetCellValue(sheetName, "H1", year+"年")
- SetSheetRow(f, sheetName, "A2", &[]interface{}{"部门", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率"})
- if err := SetSheetRow(f, sheetName, "J2", &tem1); err != nil {
- println(err.Error())
- }
- A := 3
- pastures, err := restful.MapStrList("select id,name from department where pastureId=? and isDel = 0 AND enable =1 and id <> ?", pastureId, pastureId)
- if err != nil {
- println("err sql", err.Error())
- } else {
- for _, value := range pastures {
- GetMailDataByDept(sheetName, value["name"], date, value["id"], pastureId, A, f)
- println(value["name"])
- A = A + 8
- }
- }
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMY")
- valuesMap1, err1 := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMM")
- sqls := ""
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- parammaps["pastureId"] = pastureId
- s_params := make([]interface{}, 0)
- if err == nil && len(valuesMap) > 0 {
- sqls = valuesMap["sqlstr"]
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- tem = []interface{}{}
- //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
- values, err := restful.MapStrList(sqls, s_params...)
- B := A
- temMap := make(map[string][]interface{}, 0)
- if err == nil {
- for _, value := range values {
- tem = append(tem, value["ftype"])
- tem = append(tem, value["monthBudget"])
- tem = append(tem, value["monthLimit"])
- tem = append(tem, value["qmonthBudget"])
- tem = append(tem, value["sumPrice"])
- tem = append(tem, value["asave"])
- tem = append(tem, value["dac"])
- tem = append(tem, value["dacY"])
- temMap[value["ftype"]] = tem
- //if err := SetSheetRow(f,sheetName, "B"+strconv.Itoa(B),&tem); err != nil {
- // println(err.Error())
- //}
- tem = []interface{}{}
- //B++
- }
- } else {
- println(err.Error())
- }
- if m, ok := temMap["维修费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"维修费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["燃动费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"燃动费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["水费(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"水费(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["设备指标(万元)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"设备指标(万元)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["电量(度)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"电量(度)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["用水量(吨)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"用水量(吨)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- if m, ok := temMap["柴油用量(L)"]; ok {
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &m); err != nil {
- println(err.Error())
- }
- B++
- } else {
- temw := []interface{}{"柴油用量(L)"}
- if err := SetSheetRow(f, sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
- println(err.Error())
- }
- B++
- }
- s_params = make([]interface{}, 0)
- if err1 == nil && len(valuesMap1) > 0 {
- sqls = valuesMap1["sqlstr"]
- parammaps := make(map[string]interface{}, 0)
- parammaps["month"] = date
- parammaps["pastureId"] = pastureId
- paramslist := strings.Split(valuesMap1["params"], ",")
- if len(paramslist) > 0 && valuesMap1["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- f.SetCellValue(sheetName, "A"+strconv.Itoa(A), sheetName)
- f.MergeCell(sheetName, "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
- values, err = restful.MapStrList(sqls, s_params...)
- tem = []interface{}{}
- //d := append(c,nil)
- //e := append(c,nil)
- //g := append(c,nil)
- //h := append(c,nil)
- //o := append(c,nil)
- //p := append(c,nil)
- tem2 := []interface{}{}
- if err == nil {
- for _, value := range values {
- tem2 = append(tem2, value["monthBudget"])
- tem2 = append(tem2, value["monthLimit"])
- tem2 = append(tem2, value["qmonthBudget"])
- tem2 = append(tem2, value["sumPrice"])
- tem2 = append(tem2, value["asave"])
- tem2 = append(tem2, value["dac"])
- tem2 = append(tem2, value["dacY"])
- num, _ := strconv.Atoi(value["num"])
- switch value["ftype"] {
- case "维修费(万元)":
- c[(num-1)*7] = tem2[0]
- c[(num-1)*7+1] = tem2[1]
- c[(num-1)*7+2] = tem2[2]
- c[(num-1)*7+3] = tem2[3]
- c[(num-1)*7+4] = tem2[4]
- c[(num-1)*7+5] = tem2[5]
- c[(num-1)*7+6] = tem2[6]
- case "电费(万元)":
- d[(num-1)*7] = tem2[0]
- d[(num-1)*7+1] = tem2[1]
- d[(num-1)*7+2] = tem2[2]
- d[(num-1)*7+3] = tem2[3]
- d[(num-1)*7+4] = tem2[4]
- d[(num-1)*7+5] = tem2[5]
- d[(num-1)*7+6] = tem2[6]
- case "燃动费(万元)":
- e[(num-1)*7] = tem2[0]
- e[(num-1)*7+1] = tem2[1]
- e[(num-1)*7+2] = tem2[2]
- e[(num-1)*7+3] = tem2[3]
- e[(num-1)*7+4] = tem2[4]
- e[(num-1)*7+5] = tem2[5]
- e[(num-1)*7+6] = tem2[6]
- case "水费(万元)":
- g[(num-1)*7] = tem2[0]
- g[(num-1)*7+1] = tem2[1]
- g[(num-1)*7+2] = tem2[2]
- g[(num-1)*7+3] = tem2[3]
- g[(num-1)*7+4] = tem2[4]
- g[(num-1)*7+5] = tem2[5]
- g[(num-1)*7+6] = tem2[6]
- case "电量(度)":
- h[(num-1)*7] = tem2[0]
- h[(num-1)*7+1] = tem2[1]
- h[(num-1)*7+2] = tem2[2]
- h[(num-1)*7+3] = tem2[3]
- h[(num-1)*7+4] = tem2[4]
- h[(num-1)*7+5] = tem2[5]
- h[(num-1)*7+6] = tem2[6]
- case "柴油用量(L)":
- o[(num-1)*7] = tem2[0]
- o[(num-1)*7+1] = tem2[1]
- o[(num-1)*7+2] = tem2[2]
- o[(num-1)*7+3] = tem2[3]
- o[(num-1)*7+4] = tem2[4]
- o[(num-1)*7+5] = tem2[5]
- o[(num-1)*7+6] = tem2[6]
- case "用水量(吨)":
- p[(num-1)*7] = tem2[0]
- p[(num-1)*7+1] = tem2[1]
- p[(num-1)*7+2] = tem2[2]
- p[(num-1)*7+3] = tem2[3]
- p[(num-1)*7+4] = tem2[4]
- p[(num-1)*7+5] = tem2[5]
- p[(num-1)*7+6] = tem2[6]
- case "设备指标(万元)":
- r[(num-1)*7] = tem2[0]
- r[(num-1)*7+1] = tem2[1]
- r[(num-1)*7+2] = tem2[2]
- r[(num-1)*7+3] = tem2[3]
- r[(num-1)*7+4] = tem2[4]
- r[(num-1)*7+5] = tem2[5]
- r[(num-1)*7+6] = tem2[6]
- }
- tem2 = []interface{}{}
- }
- } else {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A), &c); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+1), &d); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+2), &e); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+3), &g); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+4), &r); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+5), &h); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+6), &p); err != nil {
- println(err.Error())
- }
- if err := SetSheetRow(f, sheetName, "J"+strconv.Itoa(A+7), &o); err != nil {
- println(err.Error())
- }
- //sums[2] = sums[2].(int)+value["monthLimit"].(int)
- //sums[1] = sums[1].(int)+value["monthBudget"].(int)
- // 设置月份单元格字体
- monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
- if err != nil {
- println("monthStyle err", err.Error())
- }
- f.SetCellStyle(sheetName, "A1", "CS300", monthStyle)
- // 根据指定路径保存文件
- //filename :="uploads/Downloadmailreport/" + date +sheetName+"设备指标.xlsx"
- err = PathCheck("uploads/Downloadmailreport") //检查路径并创建
- //os.Remove(filename)
- if err != nil {
- println("PathCheck err", err)
- }
- if err := f.SaveAs(filename); err != nil {
- println(err.Error())
- return "", err
- }
- return filename, err
- }
- func FileExists(path string) (bool, error) {
- _, err := os.Stat(path)
- if err == nil {
- return true, nil
- }
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
- }
- //测试用的
- func Sssss() {
- sqls := "SELECT bbd.`buyeCode`,p.`providerName`,bd.receiveAmount,bbd.`matchCode`,bd.`amount`,bd.`sumAmount`,bd.departmentName,pa.name pastureName," +
- "bd.note,c.id,c.bigId,c.pastureId,ps.id partId,ifnull(ps.name,c.partName) partName,ifnull(ps.partCode,c.partCode) partCode," +
- "ifnull(ps.specification,c.specification) specification,ifnull(brand.brandName,c.brand) brand,ifnull(ps.unit,c.unit) unit," +
- "ifnull(TRIM(TRAILING '.' FROM (TRIM(TRAILING '0' FROM bd.price))),TRIM(TRAILING '.' FROM (TRIM(TRAILING '0' FROM c.price))))" +
- "price,brand.id brandId,c.inventoryType,c.planAmount,c.remark,c.enable,c.isZeroStock,e.`empname`," +
- "DATE_FORMAT(bbd.buyerDate,'%Y-%m-%d')buyerDate,DATE_FORMAT(bbd.DoneDate, '%Y-%m-%d') DoneDate " +
- "FROM buydetail bd INNER JOIN bigbuydetail bbd ON bbd.id = bd.`bigId` LEFT JOIN provider p ON p.id = bbd.`providerId`" +
- "LEFT JOIN contract c ON c.id = bd.`contractId` LEFT JOIN emp e ON e.id = bbd.`buyerPerson` " +
- "LEFT JOIN bigpartdeal bpd ON bpd.`matchCode` = bbd.`matchCode` inner join pasture pa on pa.id = bbd.pastureId " +
- "LEFT JOIN parts ps ON ps.partCode =bd.partCode LEFT JOIN brand ON brand.id=bd.brandId " +
- "where bbd.`enable`=0 and bd.amount > 0 and bbd.buyerDate < ? and bbd.buyerDate > ? and pa.name='通辽牧场' "
- queryData, err := restful.MapStrList(sqls, "2021-08-27", "2021-06-30")
- //s_params := make([]interface{}, 0)
- //queryData, err := restful.QueryByListListT(sql, 0, 0, s_params, tx)
- if err != nil {
- fmt.Println(err)
- } else {
- f := excelize.NewFile()
- // 创建一个工作表
- //供应商 单价 申购数量 采购数量 入库数 申购部门 采购人 配单日期 入库时间
- index := f.NewSheet("采购明细")
- f.SetColWidth("采购明细", "A", "R", 20)
- columns := []string{"备件品牌", "计量单位", "供应商", "单价", "申购数量", "采购数量", "入库数", "申购部门", "采购人", "配单日期",
- "入库时间"}
- letters := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
- for i, v := range columns {
- f.SetCellValue("采购明细", letters[i]+"1", v)
- }
- fmt.Println(len(queryData))
- for i, v := range queryData {
- // 设置单元格的值
- value := []interface{}{v["matchCode"], v["unit"], v["providerName"], v["price"], v["sumAmount"], v["amount"], v["receiveAmount"],
- v["departmentName"], v["empname"], v["buyerDate"], v["DoneDate"]}
- for a, b := range value {
- f.SetCellValue("采购明细", letters[a]+strconv.Itoa(i+2), b)
- }
- }
- // 设置工作簿的默认工作表
- f.SetActiveSheet(index)
- // 根据指定路径保存文件
- if err := f.SaveAs("D:/project/go/src/KPTcomm/test/采购明细.xlsx"); err != nil {
- fmt.Println(err)
- }
- }
- }
- // 测试
- func DownloadEquipment(parammaps string) (string, error) {
- //appG := app.Gin{C: context}
- data := make(map[string]interface{}, 0)
- filename := setting.CurrentPath + "uploads/Downloadmailreport/" + "设备指标.xlsx"
- exist, _ := FileExists(filename)
- if exist {
- //return filename, nil
- }
- f := excelize.NewFile()
- Style, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
- columns := []string{"设备指标", "维修费", "水费", "电费", "柴油费", "水量", "电量", "柴油用量"}
- //序号 牧场 部门 预算 内控 迄今内控 迄今实际 实际节约 实际达成率 单头牛预算 单头牛内控 单头牛迄今内控 单头牛迄今实际 单头牛实际节约
- columns1 := []string{"序号", "牧场", "部门", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "实际达成率", "单头牛预算",
- "单头牛内控", "单头牛迄今内控", "单头牛迄今实际", "单头牛实际节约"}
- letters := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
- for i, v := range columns {
- f.NewSheet("Sheet" + strconv.Itoa(i+1))
- f.SetSheetName("Sheet"+strconv.Itoa(i+1), v)
- f.MergeCell(v, "A1", "C1")
- f.MergeCell(v, "D1", "I1")
- f.MergeCell(v, "J1", "N1")
- f.SetCellValue(v, "A1", "设备指标")
- f.SetCellValue(v, "D1", "设备指标")
- f.SetCellValue(v, "J1", "单头牛指标")
- f.SetCellStyle(v, "A1", "CS154", Style)
- f.SetCellStyle(v, "D1", "CS154", Style)
- f.SetCellStyle(v, "J1", "CS154", Style)
- f.SetColWidth(v, "A", "N", 20)
- for i1, v1 := range columns1 {
- f.SetCellValue(v, letters[i1]+"2", v1)
- }
- }
- sqlnames := []string{"getChooseEqMonthBudgetListSumAll", "getChooseMonthWX", "getChooseElectricityPrice", "getChooseMonthiolPrice",
- "getChooseWaterPrice", "getChooseMonthElectricity", "getChooseMonthWater", "getChooseMonthOilT"}
- xx := 0
- for z, sqlname := range sqlnames {
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", sqlname)
- parammap := make(map[string]interface{}, 0)
- json.Unmarshal([]byte(parammaps), ¶mmap)
- s_params := make([]interface{}, 0)
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammap[strings.Trim(value, " ")])
- }
- }
- values, err := restful.MapStrList(valuesMap["sqlstr"], s_params...)
- if err != nil {
- fmt.Println(err)
- } else {
- for i, v := range values {
- value := []interface{}{columns[z], v["pastureName"], "全厂区", v["monthBudget"], v["monthLimit"], v["qmonthBudget"], v["sumPrice"],
- v["asave"], v["realReason"], v["oneCowBudget"], v["oneCowLimit"], v["oneCowQmonthBudget"], v["oneCowSumPrice"], v["oneCowAsave"]}
- for a, b := range value {
- f.SetCellValue("设备指标", letters[a]+strconv.Itoa(i+xx+3), b)
- }
- }
- }
- xx += len(values)
- }
- //"维修费","水费","电费","柴油费","水量","电量","柴油用量"
- // 其他的表格
- sqlnamesDepartment := []string{
- "getChooseMonthWX",
- "getChooseWaterPrice",
- "getChooseElectricityPrice",
- "getChooseMonthiolPrice",
- "getChooseMonthWater",
- "getChooseMonthElectricity",
- "getChooseMonthOilT"}
- // sqlnamesDepartment := []string{"getChooseMonthWXDepartment", "getChooseWaterPriceDepartment", "getChooseElectricityPriceDepartment", "getChooseMonthiolPriceDepartment",
- // "getChooseMonthWaterDepartment", "getChooseMonthElectricityDepartment", "getChooseMonthOilTDepartment"}
- columnss := []string{"维修费", "水费", "电费", "柴油费", "水量", "电量", "柴油用量"}
- for z, sqlname := range sqlnamesDepartment {
- valuesMap, err := restful.MapStr("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", sqlname)
- parammap := make(map[string]interface{}, 0)
- json.Unmarshal([]byte(parammaps), ¶mmap)
- s_params := make([]interface{}, 0)
- paramslist := strings.Split(valuesMap["params"], ",")
- if len(paramslist) > 0 && valuesMap["params"] != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammap[strings.Trim(value, " ")])
- }
- }
- values, err := restful.MapStrList(valuesMap["sqlstr"], s_params...)
- if err != nil {
- fmt.Println(err)
- } else {
- for i, v := range values {
- value := []interface{}{v["sort"], v["pastureName"], v["deptname"], v["monthBudget"], v["monthLimit"], v["qmonthBudget"], v["sumPrice"],
- v["asave"], v["realReason"], v["oneCowBudget"], v["oneCowLimit"], v["oneCowQmonthBudget"], v["oneCowSumPrice"], v["oneCowAsave"]}
- for a, b := range value {
- f.SetCellValue(columnss[z], letters[a]+strconv.Itoa(i+3), b)
- }
- }
- }
- }
- if err != nil {
- data["err_count"] = err
- logging.Error("设备指标导出__error:", err)
- } else {
- err = PathCheck("uploads/Downloadmailreport") //检查路径并创建
- //os.Remove(filename)
- if err != nil {
- println("PathCheck err", err)
- }
- if err := f.SaveAs(filename); err != nil {
- println(err.Error())
- return "", err
- }
- //data["success"] = "ok"
- //context.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- //context.Header("Content-Type","application/octet-stream")
- //context.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
- //context.Header("Content-Transfer-Encoding", "binary")
- //_ = f.Write(context.Writer)
- }
- return filename, err
- //appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- func SetSheetRow(f *excelize.File, sheet, axis string, slice interface{}) error {
- SetSheetRow(f, sheet, axis, slice)
- return nil
- }
|