mail.go 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  1. package util
  2. import (
  3. "../../models"
  4. "../../pkg/logging"
  5. "../../pkg/setting"
  6. "fmt"
  7. "github.com/360EntSecGroup-Skylar/excelize"
  8. "github.com/jordan-wright/email"
  9. "github.com/robfig/cron"
  10. "log"
  11. "net/smtp"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // 邮件相关(现代牧业暂未启用)
  18. func SendMail() {
  19. defer func(){ // 必须要先声明defer
  20. if err:=recover();err!=nil{
  21. logging.Error(" SendMail pnic err",err) // 这里的err其实就是panic传入的内容,55
  22. }
  23. }()
  24. log.Println("******** ******* *******", time.Now())
  25. e := email.NewEmail()
  26. // 发件人
  27. fromName := ""
  28. pas := ""
  29. mails := make([]string, 0)
  30. Engine := models.Engine
  31. subject := ""
  32. note := ""
  33. mailreport, err := Engine.SQL("select * from mailreport where enable = 1").Query().List()
  34. if err != nil {
  35. println("err sql", err.Error())
  36. } else {
  37. for _, value := range mailreport {
  38. //GetMailDataByPasture("集团设备指标",value["name"].(string),value["id"],A,f)
  39. //GetPastureDeptData(value["name"].(string),value["id"],f)
  40. if value["type"].(string) == "1" {
  41. fromName = value["addr"].(string)
  42. pas = value["pas"].(string)
  43. subject = value["title"].(string)
  44. note = value["note"].(string)
  45. } else {
  46. mails = append(mails, value["addr"].(string))
  47. }
  48. println(value["addr"].(string))
  49. }
  50. }
  51. // 发件人
  52. //e.From = "864919639@qq.com"
  53. e.From = fromName
  54. // 收件人(可以有多个)
  55. e.To = mails
  56. //e.To = []string{"18322596935@163.com","1406599862@qq.com"} //"1406599862@qq.com"
  57. // 邮件主题
  58. //e.Subject = "集团设备指标报表"
  59. e.Subject = subject
  60. // 解析html模板
  61. //t,err := template.ParseFiles("email-template.html")
  62. //body := new(bytes.Buffer)
  63. // Execute方法将解析好的模板应用到匿名结构体上,并将输出写入body中
  64. e.Text = []byte(note)
  65. // html形式的消息
  66. //body.Write([]byte("测试"))
  67. //// 从缓冲中将内容作为附件到邮件中
  68. //e.Attach(body, "email-template.html", "text/html")
  69. // 以路径将文件作为附件添加到邮件中
  70. //_,err := GetMailData()
  71. filename :=setting.CurrentPath+ "uploads/mailreport/" + time.Now().Format("2006-01-02") + "集团设备指标.xlsx"
  72. e.AttachFile(filename)
  73. // 发送邮件(如果使用QQ邮箱发送邮件的话,passwd不是邮箱密码而是授权码)
  74. err = e.Send("smtp.qq.com:587", smtp.PlainAuth("", fromName, pas, "smtp.qq.com"))
  75. if err != nil {
  76. println("sent err", err.Error())
  77. } else {
  78. println("发送成功")
  79. }
  80. }
  81. func CronMail() {
  82. log.Println("Starting Cron...")
  83. defer func() { // 必须要先声明defer
  84. if err := recover(); err != nil {
  85. fmt.Printf("CronTest pnic err%+v \n", err)
  86. logging.Error("panic recover err ", err)
  87. //println("pnic err",err.(error).Error()) // 这里的err其实就是panic传入的内容,55
  88. }
  89. }()
  90. c := cron.New()
  91. _, err := c.AddFunc(setting.ServerSetting.CronFormula, SendMail) //2 * * * * *, 2 表示每分钟的第2s执行一次
  92. if err != nil {
  93. println("cron1 err", err.Error())
  94. _, err = c.AddFunc("0 9 * * *", SendMail) //2 * * * * *, 2 表示每分钟的第2s执行一次
  95. if err != nil {
  96. println("cron2 err", err.Error())
  97. } else {
  98. println("开始启动定时任务发送邮件 0 9 * * *")
  99. }
  100. } else {
  101. println("开始启动定时任务发送邮件 ", setting.ServerSetting.CronFormula)
  102. }
  103. _, err = c.AddFunc("0 1 * * *", GetMailData) //2 * * * * *, 2 表示每分钟的第2s执行一次
  104. if err != nil {
  105. println("cron3 err", err.Error())
  106. }
  107. //_, err = c.AddFunc("30 * * * *", GetMailData) //2 * * * * *, 2 表示每分钟的第2s执行一次
  108. //if err != nil {
  109. // println("cron4 err", err.Error())
  110. //}
  111. c.Start()
  112. }
  113. func GetMailData() {
  114. defer func(){ // 必须要先声明defer
  115. if err:=recover();err!=nil{
  116. logging.Error("GetMailData pnic err",err) // 这里的err其实就是panic传入的内容,55
  117. }
  118. }()
  119. f := excelize.NewFile()
  120. f.SetSheetName("Sheet1", "集团设备指标")
  121. //f.GetSheetMap()
  122. //f.NewSheet("集团设备指标")
  123. //f.SetSheetVisible(,false)
  124. //隐藏名称为 Sheet1 的工作表中的 D 至 F 列:
  125. //f.SetColVisible("Sheet1", "D:F", false)
  126. //根据给定的工作表名称(大小写敏感)、列范围和宽度值设置单个或多个列的宽度。
  127. //例如设置名为 Sheet1 工作表上 A 到 D 列的宽度为 20:
  128. // 设置单元格的值
  129. //style, err := f.NewStyle(`{"fill":{"type":"center"}}`)
  130. //if err != nil {
  131. // fmt.Println(err)
  132. //}
  133. //f.SetCellStyle("集团设备指标", "B1", "M1", style)
  134. //
  135. //f.MergeCell("集团设备指标", "N1", "T1")
  136. //err := f.SetSheetRow("集团设备指标", "B1", &[]interface{}{"1月", nil,nil,nil,nil,nil,nil,"2月", nil,nil,nil,nil,nil,nil,
  137. // "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,
  138. // "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})
  139. tem := make([]interface{}, 0)
  140. tem1 := make([]interface{}, 0)
  141. c := []interface{}{}
  142. d := []interface{}{}
  143. e := []interface{}{}
  144. g := []interface{}{}
  145. h := []interface{}{}
  146. o := []interface{}{}
  147. p := []interface{}{}
  148. r := []interface{}{}
  149. for i := 1; i <= 12; i++ {
  150. a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
  151. b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
  152. c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  153. d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  154. e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  155. g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  156. h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  157. o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  158. p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  159. r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  160. tem = append(tem, a...)
  161. tem1 = append(tem1, b...)
  162. }
  163. //d := c
  164. //e := c
  165. //g := c
  166. //h := c
  167. //o := c
  168. //p := c
  169. if err := f.SetSheetRow("集团设备指标", "N1", &tem); err != nil {
  170. println(err.Error())
  171. }
  172. year := time.Now().Add(-24 * time.Hour).Format("2006")
  173. f.SetCellValue("集团设备指标", "A1", "时间")
  174. f.MergeCell("集团设备指标", "B1", "M1")
  175. f.MergeCell("集团设备指标", "N1", "T1")
  176. f.MergeCell("集团设备指标", "U1", "AA1")
  177. f.MergeCell("集团设备指标", "AB1", "AH1")
  178. f.MergeCell("集团设备指标", "AI1", "AO1")
  179. f.MergeCell("集团设备指标", "AP1", "AV1")
  180. f.MergeCell("集团设备指标", "AW1", "BC1")
  181. f.MergeCell("集团设备指标", "BD1", "BJ1")
  182. f.MergeCell("集团设备指标", "BK1", "BQ1")
  183. f.MergeCell("集团设备指标", "BR1", "BX1")
  184. f.MergeCell("集团设备指标", "BY1", "CE1")
  185. f.MergeCell("集团设备指标", "CF1", "CL1")
  186. f.MergeCell("集团设备指标", "CM1", "CS1")
  187. f.SetCellValue("集团设备指标", "H1", year+"年")
  188. f.SetSheetRow("集团设备指标", "A2", &[]interface{}{"牧场", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率", "迄今饲养头日", "迄今产量(公斤)", "迄今实际单头牛成本", "迄今实际公斤奶成本(元/公斤)"})
  189. if err := f.SetSheetRow("集团设备指标", "N2", &tem1); err != nil {
  190. println(err.Error())
  191. }
  192. Engine := models.Engine
  193. A := 3
  194. pastures, err := Engine.SQL("select id,name from pasture where isdel = 0 AND id <> 18 order by sort").Query().List()
  195. if err != nil {
  196. println("err sql", err.Error())
  197. } else {
  198. for _, value := range pastures {
  199. GetMailDataByPasture("集团设备指标", value["name"].(string), time.Now().Format("2006-01-02"), value["id"], A, f)
  200. GetPastureDeptData(value["name"].(string), value["id"], f)
  201. println(value["name"].(string))
  202. A = A + 8
  203. }
  204. }
  205. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJY").QueryString()
  206. valuesMap1, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJM").QueryString()
  207. sqls := ""
  208. parammaps := make(map[string]interface{}, 0)
  209. parammaps["month"] = time.Now().Format("2006-01-02")
  210. s_params := make([]interface{}, 0)
  211. if err == nil && len(valuesMap) > 0 {
  212. sqls = valuesMap[0]["sqlstr"]
  213. paramslist := strings.Split(valuesMap[0]["params"], ",")
  214. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  215. for _, value := range paramslist {
  216. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  217. }
  218. }
  219. }
  220. tem = []interface{}{}
  221. //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
  222. values, err := Engine.SQL(sqls, s_params...).Query().List()
  223. B := A
  224. //if err==nil {
  225. // for _, value := range values {
  226. // tem = append(tem,value["ftype"])
  227. // tem = append(tem,value["monthBudget"])
  228. // tem = append(tem,value["monthLimit"])
  229. // tem = append(tem,value["qmonthBudget"])
  230. // tem = append(tem,value["sumPrice"])
  231. // tem = append(tem,value["asave"])
  232. // tem = append(tem,value["dac"])
  233. // tem = append(tem,value["dacY"])
  234. // tem = append(tem,value["cowNum"])
  235. // tem = append(tem,value["saleNum"])
  236. // tem = append(tem,value["cowprice"])
  237. // tem = append(tem,value["milkprice"])
  238. // if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B),&tem); err != nil {
  239. // println(err.Error())
  240. // }
  241. // tem = []interface{}{}
  242. // B++
  243. // }
  244. //}else {
  245. // println(err.Error())
  246. //}
  247. temMap := make(map[string][]interface{}, 0)
  248. if err == nil {
  249. for _, value := range values {
  250. tem = append(tem, value["ftype"])
  251. tem = append(tem, value["monthBudget"])
  252. tem = append(tem, value["monthLimit"])
  253. tem = append(tem, value["qmonthBudget"])
  254. tem = append(tem, value["sumPrice"])
  255. tem = append(tem, value["asave"])
  256. tem = append(tem, value["dac"])
  257. tem = append(tem, value["dacY"])
  258. tem = append(tem, value["cowNum"])
  259. tem = append(tem, value["saleNum"])
  260. tem = append(tem, value["cowprice"])
  261. tem = append(tem, value["milkprice"])
  262. temMap[value["ftype"].(string)] = tem
  263. //if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B),&tem); err != nil {
  264. // println(err.Error())
  265. //}
  266. tem = []interface{}{}
  267. //B++
  268. }
  269. } else {
  270. println(err.Error())
  271. }
  272. if m, ok := temMap["维修费(万元)"]; ok {
  273. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  274. println(err.Error())
  275. }
  276. B++
  277. } else {
  278. temw := []interface{}{"维修费(万元)"}
  279. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  280. println(err.Error())
  281. }
  282. B++
  283. }
  284. if m, ok := temMap["电费(万元)"]; ok {
  285. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  286. println(err.Error())
  287. }
  288. B++
  289. } else {
  290. temw := []interface{}{"电费(万元)"}
  291. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  292. println(err.Error())
  293. }
  294. B++
  295. }
  296. if m, ok := temMap["燃动费(万元)"]; ok {
  297. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  298. println(err.Error())
  299. }
  300. B++
  301. } else {
  302. temw := []interface{}{"燃动费(万元)"}
  303. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  304. println(err.Error())
  305. }
  306. B++
  307. }
  308. if m, ok := temMap["水费(万元)"]; ok {
  309. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  310. println(err.Error())
  311. }
  312. B++
  313. } else {
  314. temw := []interface{}{"水费(万元)"}
  315. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  316. println(err.Error())
  317. }
  318. B++
  319. }
  320. if m, ok := temMap["设备指标(万元)"]; ok {
  321. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  322. println(err.Error())
  323. }
  324. B++
  325. } else {
  326. temw := []interface{}{"设备指标(万元)"}
  327. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  328. println(err.Error())
  329. }
  330. B++
  331. }
  332. if m, ok := temMap["电量(度)"]; ok {
  333. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  334. println(err.Error())
  335. }
  336. B++
  337. } else {
  338. temw := []interface{}{"电量(度)"}
  339. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  340. println(err.Error())
  341. }
  342. B++
  343. }
  344. if m, ok := temMap["用水量(吨)"]; ok {
  345. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  346. println(err.Error())
  347. }
  348. B++
  349. } else {
  350. temw := []interface{}{"用水量(吨)"}
  351. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  352. println(err.Error())
  353. }
  354. B++
  355. }
  356. if m, ok := temMap["柴油用量(L)"]; ok {
  357. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  358. println(err.Error())
  359. }
  360. B++
  361. } else {
  362. temw := []interface{}{"柴油用量(L)"}
  363. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  364. println(err.Error())
  365. }
  366. B++
  367. }
  368. s_params = make([]interface{}, 0)
  369. if err == nil && len(valuesMap1) > 0 {
  370. sqls = valuesMap1[0]["sqlstr"]
  371. parammaps := make(map[string]interface{}, 0)
  372. parammaps["month"] = time.Now().Format("2006-01-02")
  373. paramslist := strings.Split(valuesMap1[0]["params"], ",")
  374. if len(paramslist) > 0 && valuesMap1[0]["params"] != "" {
  375. for _, value := range paramslist {
  376. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  377. }
  378. }
  379. }
  380. f.SetCellValue("集团设备指标", "A"+strconv.Itoa(A), "集团")
  381. f.MergeCell("集团设备指标", "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
  382. values, err = Engine.SQL(sqls, s_params...).Query().List()
  383. tem = []interface{}{}
  384. //d := append(c,nil)
  385. //e := append(c,nil)
  386. //g := append(c,nil)
  387. //h := append(c,nil)
  388. //o := append(c,nil)
  389. //p := append(c,nil)
  390. tem2 := []interface{}{}
  391. if err == nil {
  392. for _, value := range values {
  393. tem2 = append(tem2, value["monthBudget"])
  394. tem2 = append(tem2, value["monthLimit"])
  395. tem2 = append(tem2, value["qmonthBudget"])
  396. tem2 = append(tem2, value["sumPrice"])
  397. tem2 = append(tem2, value["asave"])
  398. tem2 = append(tem2, value["dac"])
  399. tem2 = append(tem2, value["dacY"])
  400. num, _ := strconv.Atoi(value["num"].(string))
  401. switch value["ftype"] {
  402. case "维修费(万元)":
  403. c[(num-1)*7] = tem2[0]
  404. c[(num-1)*7+1] = tem2[1]
  405. c[(num-1)*7+2] = tem2[2]
  406. c[(num-1)*7+3] = tem2[3]
  407. c[(num-1)*7+4] = tem2[4]
  408. c[(num-1)*7+5] = tem2[5]
  409. c[(num-1)*7+6] = tem2[6]
  410. case "电费(万元)":
  411. d[(num-1)*7] = tem2[0]
  412. d[(num-1)*7+1] = tem2[1]
  413. d[(num-1)*7+2] = tem2[2]
  414. d[(num-1)*7+3] = tem2[3]
  415. d[(num-1)*7+4] = tem2[4]
  416. d[(num-1)*7+5] = tem2[5]
  417. d[(num-1)*7+6] = tem2[6]
  418. case "燃动费(万元)":
  419. e[(num-1)*7] = tem2[0]
  420. e[(num-1)*7+1] = tem2[1]
  421. e[(num-1)*7+2] = tem2[2]
  422. e[(num-1)*7+3] = tem2[3]
  423. e[(num-1)*7+4] = tem2[4]
  424. e[(num-1)*7+5] = tem2[5]
  425. e[(num-1)*7+6] = tem2[6]
  426. case "水费(万元)":
  427. g[(num-1)*7] = tem2[0]
  428. g[(num-1)*7+1] = tem2[1]
  429. g[(num-1)*7+2] = tem2[2]
  430. g[(num-1)*7+3] = tem2[3]
  431. g[(num-1)*7+4] = tem2[4]
  432. g[(num-1)*7+5] = tem2[5]
  433. g[(num-1)*7+6] = tem2[6]
  434. case "电量(度)":
  435. h[(num-1)*7] = tem2[0]
  436. h[(num-1)*7+1] = tem2[1]
  437. h[(num-1)*7+2] = tem2[2]
  438. h[(num-1)*7+3] = tem2[3]
  439. h[(num-1)*7+4] = tem2[4]
  440. h[(num-1)*7+5] = tem2[5]
  441. h[(num-1)*7+6] = tem2[6]
  442. case "柴油用量(L)":
  443. o[(num-1)*7] = tem2[0]
  444. o[(num-1)*7+1] = tem2[1]
  445. o[(num-1)*7+2] = tem2[2]
  446. o[(num-1)*7+3] = tem2[3]
  447. o[(num-1)*7+4] = tem2[4]
  448. o[(num-1)*7+5] = tem2[5]
  449. o[(num-1)*7+6] = tem2[6]
  450. case "用水量(吨)":
  451. p[(num-1)*7] = tem2[0]
  452. p[(num-1)*7+1] = tem2[1]
  453. p[(num-1)*7+2] = tem2[2]
  454. p[(num-1)*7+3] = tem2[3]
  455. p[(num-1)*7+4] = tem2[4]
  456. p[(num-1)*7+5] = tem2[5]
  457. p[(num-1)*7+6] = tem2[6]
  458. case "设备指标(万元)":
  459. r[(num-1)*7] = tem2[0]
  460. r[(num-1)*7+1] = tem2[1]
  461. r[(num-1)*7+2] = tem2[2]
  462. r[(num-1)*7+3] = tem2[3]
  463. r[(num-1)*7+4] = tem2[4]
  464. r[(num-1)*7+5] = tem2[5]
  465. r[(num-1)*7+6] = tem2[6]
  466. }
  467. tem2 = []interface{}{}
  468. }
  469. } else {
  470. println(err.Error())
  471. }
  472. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A), &c); err != nil {
  473. println(err.Error())
  474. }
  475. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+1), &d); err != nil {
  476. println(err.Error())
  477. }
  478. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+2), &e); err != nil {
  479. println(err.Error())
  480. }
  481. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+3), &g); err != nil {
  482. println(err.Error())
  483. }
  484. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+4), &r); err != nil {
  485. println(err.Error())
  486. }
  487. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+5), &h); err != nil {
  488. println(err.Error())
  489. }
  490. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+6), &p); err != nil {
  491. println(err.Error())
  492. }
  493. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+7), &o); err != nil {
  494. println(err.Error())
  495. }
  496. //sums[2] = sums[2].(int)+value["monthLimit"].(int)
  497. //sums[1] = sums[1].(int)+value["monthBudget"].(int)
  498. // 设置月份单元格字体
  499. monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
  500. if err != nil {
  501. println("monthStyle err", err.Error())
  502. }
  503. f.SetCellStyle("集团设备指标", "A1", "CS154", monthStyle)
  504. // 根据指定路径保存文件
  505. filename := setting.CurrentPath+ "uploads/mailreport/" + time.Now().Format("2006-01-02") + "集团设备指标.xlsx"
  506. err = PathCheck(setting.CurrentPath+ "uploads/mailreport") //检查路径并创建
  507. os.Remove(filename)
  508. if err != nil {
  509. println("PathCheck err", err)
  510. }
  511. if err := f.SaveAs(filename); err != nil {
  512. println(err.Error())
  513. }
  514. }
  515. func GetMailDataByPasture(sheetName, pastureName, date string, pastureId interface{}, B int, f *excelize.File) {
  516. tem := make([]interface{}, 0)
  517. tem1 := make([]interface{}, 0)
  518. c := []interface{}{}
  519. d := []interface{}{}
  520. e := []interface{}{}
  521. g := []interface{}{}
  522. h := []interface{}{}
  523. o := []interface{}{}
  524. p := []interface{}{}
  525. r := []interface{}{}
  526. for i := 1; i <= 12; i++ {
  527. a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
  528. b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
  529. c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  530. d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  531. e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  532. g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  533. h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  534. o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  535. p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  536. r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  537. tem = append(tem, a...)
  538. tem1 = append(tem1, b...)
  539. }
  540. Engine := models.Engine
  541. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMY").QueryString()
  542. valuesMap1, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMM").QueryString()
  543. sqls := ""
  544. parammaps := make(map[string]interface{}, 0)
  545. parammaps["month"] = date
  546. parammaps["pastureId"] = pastureId
  547. s_params := make([]interface{}, 0)
  548. if err == nil && len(valuesMap) > 0 {
  549. sqls = valuesMap[0]["sqlstr"]
  550. paramslist := strings.Split(valuesMap[0]["params"], ",")
  551. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  552. for _, value := range paramslist {
  553. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  554. }
  555. }
  556. }
  557. f.SetCellValue(sheetName, "A"+strconv.Itoa(B), pastureName)
  558. f.MergeCell(sheetName, "A"+strconv.Itoa(B), "A"+strconv.Itoa(B+7))
  559. tem = []interface{}{}
  560. temMap := make(map[string][]interface{}, 0)
  561. //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
  562. values, err := Engine.SQL(sqls, s_params...).Query().List()
  563. Bt := B
  564. if err == nil {
  565. for _, value := range values {
  566. tem = append(tem, value["ftype"])
  567. tem = append(tem, value["monthBudget"])
  568. tem = append(tem, value["monthLimit"])
  569. tem = append(tem, value["qmonthBudget"])
  570. tem = append(tem, value["sumPrice"])
  571. tem = append(tem, value["asave"])
  572. tem = append(tem, value["dac"])
  573. tem = append(tem, value["dacY"])
  574. tem = append(tem, value["cowNum"])
  575. tem = append(tem, value["saleNum"])
  576. tem = append(tem, value["cowprice"])
  577. tem = append(tem, value["milkprice"])
  578. temMap[value["ftype"].(string)] = tem
  579. //if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt),&tem); err != nil {
  580. // println(err.Error())
  581. //}
  582. tem = []interface{}{}
  583. //Bt++
  584. }
  585. } else {
  586. println(err.Error())
  587. }
  588. if m, ok := temMap["维修费(万元)"]; ok {
  589. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  590. println(err.Error())
  591. }
  592. Bt++
  593. } else {
  594. temw := []interface{}{"维修费(万元)"}
  595. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  596. println(err.Error())
  597. }
  598. Bt++
  599. }
  600. if m, ok := temMap["电费(万元)"]; ok {
  601. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  602. println(err.Error())
  603. }
  604. Bt++
  605. } else {
  606. temw := []interface{}{"电费(万元)"}
  607. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  608. println(err.Error())
  609. }
  610. Bt++
  611. }
  612. if m, ok := temMap["燃动费(万元)"]; ok {
  613. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  614. println(err.Error())
  615. }
  616. Bt++
  617. } else {
  618. temw := []interface{}{"燃动费(万元)"}
  619. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  620. println(err.Error())
  621. }
  622. Bt++
  623. }
  624. if m, ok := temMap["水费(万元)"]; ok {
  625. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  626. println(err.Error())
  627. }
  628. Bt++
  629. } else {
  630. temw := []interface{}{"水费(万元)"}
  631. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  632. println(err.Error())
  633. }
  634. Bt++
  635. }
  636. if m, ok := temMap["设备指标(万元)"]; ok {
  637. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  638. println(err.Error())
  639. }
  640. Bt++
  641. } else {
  642. temw := []interface{}{"设备指标(万元)"}
  643. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  644. println(err.Error())
  645. }
  646. Bt++
  647. }
  648. if m, ok := temMap["电量(度)"]; ok {
  649. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  650. println(err.Error())
  651. }
  652. Bt++
  653. } else {
  654. temw := []interface{}{"电量(度)"}
  655. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  656. println(err.Error())
  657. }
  658. Bt++
  659. }
  660. if m, ok := temMap["用水量(吨)"]; ok {
  661. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  662. println(err.Error())
  663. }
  664. Bt++
  665. } else {
  666. temw := []interface{}{"用水量(吨)"}
  667. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  668. println(err.Error())
  669. }
  670. Bt++
  671. }
  672. if m, ok := temMap["柴油用量(L)"]; ok {
  673. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  674. println(err.Error())
  675. }
  676. Bt++
  677. } else {
  678. temw := []interface{}{"柴油用量(L)"}
  679. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  680. println(err.Error())
  681. }
  682. Bt++
  683. }
  684. s_params = make([]interface{}, 0)
  685. if err == nil && len(valuesMap1) > 0 {
  686. sqls = valuesMap1[0]["sqlstr"]
  687. parammaps := make(map[string]interface{}, 0)
  688. parammaps["month"] = date
  689. parammaps["pastureId"] = pastureId
  690. paramslist := strings.Split(valuesMap1[0]["params"], ",")
  691. if len(paramslist) > 0 && valuesMap1[0]["params"] != "" {
  692. for _, value := range paramslist {
  693. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  694. }
  695. }
  696. }
  697. values, err = Engine.SQL(sqls, s_params...).Query().List()
  698. tem = []interface{}{}
  699. //d := append(c,nil)
  700. //e := append(c,nil)
  701. //g := append(c,nil)
  702. //h := append(c,nil)
  703. //o := append(c,nil)
  704. //p := append(c,nil)
  705. tem2 := []interface{}{}
  706. if err == nil {
  707. for _, value := range values {
  708. tem2 = append(tem2, value["monthBudget"])
  709. tem2 = append(tem2, value["monthLimit"])
  710. tem2 = append(tem2, value["qmonthBudget"])
  711. tem2 = append(tem2, value["sumPrice"])
  712. tem2 = append(tem2, value["asave"])
  713. tem2 = append(tem2, value["dac"])
  714. tem2 = append(tem2, value["dacY"])
  715. num, _ := strconv.Atoi(value["num"].(string))
  716. switch value["ftype"] {
  717. case "维修费(万元)":
  718. c[(num-1)*7] = tem2[0]
  719. c[(num-1)*7+1] = tem2[1]
  720. c[(num-1)*7+2] = tem2[2]
  721. c[(num-1)*7+3] = tem2[3]
  722. c[(num-1)*7+4] = tem2[4]
  723. c[(num-1)*7+5] = tem2[5]
  724. c[(num-1)*7+6] = tem2[6]
  725. case "电费(万元)":
  726. d[(num-1)*7] = tem2[0]
  727. d[(num-1)*7+1] = tem2[1]
  728. d[(num-1)*7+2] = tem2[2]
  729. d[(num-1)*7+3] = tem2[3]
  730. d[(num-1)*7+4] = tem2[4]
  731. d[(num-1)*7+5] = tem2[5]
  732. d[(num-1)*7+6] = tem2[6]
  733. case "燃动费(万元)":
  734. e[(num-1)*7] = tem2[0]
  735. e[(num-1)*7+1] = tem2[1]
  736. e[(num-1)*7+2] = tem2[2]
  737. e[(num-1)*7+3] = tem2[3]
  738. e[(num-1)*7+4] = tem2[4]
  739. e[(num-1)*7+5] = tem2[5]
  740. e[(num-1)*7+6] = tem2[6]
  741. case "水费(万元)":
  742. g[(num-1)*7] = tem2[0]
  743. g[(num-1)*7+1] = tem2[1]
  744. g[(num-1)*7+2] = tem2[2]
  745. g[(num-1)*7+3] = tem2[3]
  746. g[(num-1)*7+4] = tem2[4]
  747. g[(num-1)*7+5] = tem2[5]
  748. g[(num-1)*7+6] = tem2[6]
  749. case "电量(度)":
  750. h[(num-1)*7] = tem2[0]
  751. h[(num-1)*7+1] = tem2[1]
  752. h[(num-1)*7+2] = tem2[2]
  753. h[(num-1)*7+3] = tem2[3]
  754. h[(num-1)*7+4] = tem2[4]
  755. h[(num-1)*7+5] = tem2[5]
  756. h[(num-1)*7+6] = tem2[6]
  757. case "柴油用量(L)":
  758. o[(num-1)*7] = tem2[0]
  759. o[(num-1)*7+1] = tem2[1]
  760. o[(num-1)*7+2] = tem2[2]
  761. o[(num-1)*7+3] = tem2[3]
  762. o[(num-1)*7+4] = tem2[4]
  763. o[(num-1)*7+5] = tem2[5]
  764. o[(num-1)*7+6] = tem2[6]
  765. case "用水量(吨)":
  766. p[(num-1)*7] = tem2[0]
  767. p[(num-1)*7+1] = tem2[1]
  768. p[(num-1)*7+2] = tem2[2]
  769. p[(num-1)*7+3] = tem2[3]
  770. p[(num-1)*7+4] = tem2[4]
  771. p[(num-1)*7+5] = tem2[5]
  772. p[(num-1)*7+6] = tem2[6]
  773. case "设备指标(万元)":
  774. r[(num-1)*7] = tem2[0]
  775. r[(num-1)*7+1] = tem2[1]
  776. r[(num-1)*7+2] = tem2[2]
  777. r[(num-1)*7+3] = tem2[3]
  778. r[(num-1)*7+4] = tem2[4]
  779. r[(num-1)*7+5] = tem2[5]
  780. r[(num-1)*7+6] = tem2[6]
  781. }
  782. tem2 = []interface{}{}
  783. }
  784. } else {
  785. println(err.Error())
  786. }
  787. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B), &c); err != nil {
  788. println(err.Error())
  789. }
  790. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B+1), &d); err != nil {
  791. println(err.Error())
  792. }
  793. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B+2), &e); err != nil {
  794. println(err.Error())
  795. }
  796. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B+3), &g); err != nil {
  797. println(err.Error())
  798. }
  799. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B+4), &r); err != nil {
  800. println(err.Error())
  801. }
  802. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B+5), &h); err != nil {
  803. println(err.Error())
  804. }
  805. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B+6), &p); err != nil {
  806. println(err.Error())
  807. }
  808. if err := f.SetSheetRow(sheetName, "N"+strconv.Itoa(B+7), &o); err != nil {
  809. println(err.Error())
  810. }
  811. }
  812. func GetMailDataByDept(sheetName, DeptName, date string, DeptId, pastureId interface{}, B int, f *excelize.File) {
  813. tem := make([]interface{}, 0)
  814. tem1 := make([]interface{}, 0)
  815. c := []interface{}{}
  816. d := []interface{}{}
  817. e := []interface{}{}
  818. g := []interface{}{}
  819. h := []interface{}{}
  820. o := []interface{}{}
  821. p := []interface{}{}
  822. r := []interface{}{}
  823. for i := 1; i <= 12; i++ {
  824. a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
  825. b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
  826. c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  827. d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  828. e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  829. g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  830. h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  831. o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  832. p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  833. r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  834. tem = append(tem, a...)
  835. tem1 = append(tem1, b...)
  836. }
  837. Engine := models.Engine
  838. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportBY").QueryString()
  839. valuesMap1, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportBM").QueryString()
  840. sqls := ""
  841. parammaps := make(map[string]interface{}, 0)
  842. parammaps["month"] = date
  843. parammaps["pastureId"] = pastureId
  844. parammaps["departmentId"] = DeptId
  845. s_params := make([]interface{}, 0)
  846. if err == nil && len(valuesMap) > 0 {
  847. sqls = valuesMap[0]["sqlstr"]
  848. paramslist := strings.Split(valuesMap[0]["params"], ",")
  849. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  850. for _, value := range paramslist {
  851. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  852. }
  853. }
  854. }
  855. f.SetCellValue(sheetName, "A"+strconv.Itoa(B), DeptName)
  856. f.MergeCell(sheetName, "A"+strconv.Itoa(B), "A"+strconv.Itoa(B+7))
  857. tem = []interface{}{}
  858. temMap := make(map[string][]interface{}, 0)
  859. //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
  860. values, err := Engine.SQL(sqls, s_params...).Query().List()
  861. Bt := B
  862. if err == nil {
  863. for _, value := range values {
  864. tem = append(tem, value["ftype"])
  865. tem = append(tem, value["monthBudget"])
  866. tem = append(tem, value["monthLimit"])
  867. tem = append(tem, value["qmonthBudget"])
  868. tem = append(tem, value["sumPrice"])
  869. tem = append(tem, value["asave"])
  870. tem = append(tem, value["dac"])
  871. tem = append(tem, value["dacY"])
  872. tem = append(tem, value["cowNum"])
  873. tem = append(tem, value["saleNum"])
  874. tem = append(tem, value["cowprice"])
  875. tem = append(tem, value["milkprice"])
  876. temMap[value["ftype"].(string)] = tem
  877. //if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt),&tem); err != nil {
  878. // println(err.Error())
  879. //}
  880. tem = []interface{}{}
  881. //Bt++
  882. }
  883. } else {
  884. println(err.Error())
  885. }
  886. if m, ok := temMap["维修费(万元)"]; ok {
  887. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  888. println(err.Error())
  889. }
  890. Bt++
  891. } else {
  892. temw := []interface{}{"维修费(万元)"}
  893. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  894. println(err.Error())
  895. }
  896. Bt++
  897. }
  898. if m, ok := temMap["电费(万元)"]; ok {
  899. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  900. println(err.Error())
  901. }
  902. Bt++
  903. } else {
  904. temw := []interface{}{"电费(万元)"}
  905. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  906. println(err.Error())
  907. }
  908. Bt++
  909. }
  910. if m, ok := temMap["燃动费(万元)"]; ok {
  911. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  912. println(err.Error())
  913. }
  914. Bt++
  915. } else {
  916. temw := []interface{}{"燃动费(万元)"}
  917. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  918. println(err.Error())
  919. }
  920. Bt++
  921. }
  922. if m, ok := temMap["水费(万元)"]; ok {
  923. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  924. println(err.Error())
  925. }
  926. Bt++
  927. } else {
  928. temw := []interface{}{"水费(万元)"}
  929. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  930. println(err.Error())
  931. }
  932. Bt++
  933. }
  934. if m, ok := temMap["设备指标(万元)"]; ok {
  935. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  936. println(err.Error())
  937. }
  938. Bt++
  939. } else {
  940. temw := []interface{}{"设备指标(万元)"}
  941. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  942. println(err.Error())
  943. }
  944. Bt++
  945. }
  946. if m, ok := temMap["电量(度)"]; ok {
  947. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  948. println(err.Error())
  949. }
  950. Bt++
  951. } else {
  952. temw := []interface{}{"电量(度)"}
  953. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  954. println(err.Error())
  955. }
  956. Bt++
  957. }
  958. if m, ok := temMap["用水量(吨)"]; ok {
  959. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  960. println(err.Error())
  961. }
  962. Bt++
  963. } else {
  964. temw := []interface{}{"用水量(吨)"}
  965. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  966. println(err.Error())
  967. }
  968. Bt++
  969. }
  970. if m, ok := temMap["柴油用量(L)"]; ok {
  971. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &m); err != nil {
  972. println(err.Error())
  973. }
  974. Bt++
  975. } else {
  976. temw := []interface{}{"柴油用量(L)"}
  977. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(Bt), &temw); err != nil {
  978. println(err.Error())
  979. }
  980. Bt++
  981. }
  982. s_params = make([]interface{}, 0)
  983. if err == nil && len(valuesMap1) > 0 {
  984. sqls = valuesMap1[0]["sqlstr"]
  985. parammaps := make(map[string]interface{}, 0)
  986. parammaps["month"] = date
  987. parammaps["pastureId"] = pastureId
  988. parammaps["departmentId"] = DeptId
  989. paramslist := strings.Split(valuesMap1[0]["params"], ",")
  990. if len(paramslist) > 0 && valuesMap1[0]["params"] != "" {
  991. for _, value := range paramslist {
  992. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  993. }
  994. }
  995. }
  996. values, err = Engine.SQL(sqls, s_params...).Query().List()
  997. tem = []interface{}{}
  998. //d := append(c,nil)
  999. //e := append(c,nil)
  1000. //g := append(c,nil)
  1001. //h := append(c,nil)
  1002. //o := append(c,nil)
  1003. //p := append(c,nil)
  1004. tem2 := []interface{}{}
  1005. if err == nil {
  1006. for _, value := range values {
  1007. //fmt.Printf("va============== %+v\n",value)
  1008. tem2 = append(tem2, value["monthBudget"])
  1009. tem2 = append(tem2, value["monthLimit"])
  1010. tem2 = append(tem2, value["qmonthBudget"])
  1011. tem2 = append(tem2, value["sumPrice"])
  1012. tem2 = append(tem2, value["asave"])
  1013. tem2 = append(tem2, value["dac"])
  1014. tem2 = append(tem2, value["dacY"])
  1015. num := 1
  1016. if _, ok := value["num"]; ok {
  1017. num, _ = strconv.Atoi(value["num"].(string))
  1018. }
  1019. switch value["ftype"] {
  1020. case "维修费(万元)":
  1021. c[(num-1)*7] = tem2[0]
  1022. c[(num-1)*7+1] = tem2[1]
  1023. c[(num-1)*7+2] = tem2[2]
  1024. c[(num-1)*7+3] = tem2[3]
  1025. c[(num-1)*7+4] = tem2[4]
  1026. c[(num-1)*7+5] = tem2[5]
  1027. c[(num-1)*7+6] = tem2[6]
  1028. case "电费(万元)":
  1029. d[(num-1)*7] = tem2[0]
  1030. d[(num-1)*7+1] = tem2[1]
  1031. d[(num-1)*7+2] = tem2[2]
  1032. d[(num-1)*7+3] = tem2[3]
  1033. d[(num-1)*7+4] = tem2[4]
  1034. d[(num-1)*7+5] = tem2[5]
  1035. d[(num-1)*7+6] = tem2[6]
  1036. case "燃动费(万元)":
  1037. e[(num-1)*7] = tem2[0]
  1038. e[(num-1)*7+1] = tem2[1]
  1039. e[(num-1)*7+2] = tem2[2]
  1040. e[(num-1)*7+3] = tem2[3]
  1041. e[(num-1)*7+4] = tem2[4]
  1042. e[(num-1)*7+5] = tem2[5]
  1043. e[(num-1)*7+6] = tem2[6]
  1044. case "水费(万元)":
  1045. g[(num-1)*7] = tem2[0]
  1046. g[(num-1)*7+1] = tem2[1]
  1047. g[(num-1)*7+2] = tem2[2]
  1048. g[(num-1)*7+3] = tem2[3]
  1049. g[(num-1)*7+4] = tem2[4]
  1050. g[(num-1)*7+5] = tem2[5]
  1051. g[(num-1)*7+6] = tem2[6]
  1052. case "电量(度)":
  1053. h[(num-1)*7] = tem2[0]
  1054. h[(num-1)*7+1] = tem2[1]
  1055. h[(num-1)*7+2] = tem2[2]
  1056. h[(num-1)*7+3] = tem2[3]
  1057. h[(num-1)*7+4] = tem2[4]
  1058. h[(num-1)*7+5] = tem2[5]
  1059. h[(num-1)*7+6] = tem2[6]
  1060. case "柴油用量(L)":
  1061. o[(num-1)*7] = tem2[0]
  1062. o[(num-1)*7+1] = tem2[1]
  1063. o[(num-1)*7+2] = tem2[2]
  1064. o[(num-1)*7+3] = tem2[3]
  1065. o[(num-1)*7+4] = tem2[4]
  1066. o[(num-1)*7+5] = tem2[5]
  1067. o[(num-1)*7+6] = tem2[6]
  1068. case "用水量(吨)":
  1069. p[(num-1)*7] = tem2[0]
  1070. p[(num-1)*7+1] = tem2[1]
  1071. p[(num-1)*7+2] = tem2[2]
  1072. p[(num-1)*7+3] = tem2[3]
  1073. p[(num-1)*7+4] = tem2[4]
  1074. p[(num-1)*7+5] = tem2[5]
  1075. p[(num-1)*7+6] = tem2[6]
  1076. case "设备指标(万元)":
  1077. r[(num-1)*7] = tem2[0]
  1078. r[(num-1)*7+1] = tem2[1]
  1079. r[(num-1)*7+2] = tem2[2]
  1080. r[(num-1)*7+3] = tem2[3]
  1081. r[(num-1)*7+4] = tem2[4]
  1082. r[(num-1)*7+5] = tem2[5]
  1083. r[(num-1)*7+6] = tem2[6]
  1084. }
  1085. tem2 = []interface{}{}
  1086. }
  1087. } else {
  1088. println(err.Error())
  1089. }
  1090. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B), &c); err != nil {
  1091. println(err.Error())
  1092. }
  1093. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B+1), &d); err != nil {
  1094. println(err.Error())
  1095. }
  1096. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B+2), &e); err != nil {
  1097. println(err.Error())
  1098. }
  1099. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B+3), &g); err != nil {
  1100. println(err.Error())
  1101. }
  1102. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B+4), &r); err != nil {
  1103. println(err.Error())
  1104. }
  1105. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B+5), &h); err != nil {
  1106. println(err.Error())
  1107. }
  1108. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B+6), &p); err != nil {
  1109. println(err.Error())
  1110. }
  1111. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(B+7), &o); err != nil {
  1112. println(err.Error())
  1113. }
  1114. }
  1115. func GetPastureDeptData(sheetName string, pastureId interface{}, f *excelize.File) {
  1116. f.NewSheet(sheetName)
  1117. tem := make([]interface{}, 0)
  1118. tem1 := make([]interface{}, 0)
  1119. c := []interface{}{}
  1120. d := []interface{}{}
  1121. e := []interface{}{}
  1122. g := []interface{}{}
  1123. h := []interface{}{}
  1124. o := []interface{}{}
  1125. p := []interface{}{}
  1126. r := []interface{}{}
  1127. for i := 1; i <= 12; i++ {
  1128. a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
  1129. b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
  1130. c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1131. d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1132. e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1133. g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1134. h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1135. o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1136. p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1137. r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1138. tem = append(tem, a...)
  1139. tem1 = append(tem1, b...)
  1140. }
  1141. //d := c
  1142. //e := c
  1143. //g := c
  1144. //h := c
  1145. //o := c
  1146. //p := c
  1147. if err := f.SetSheetRow(sheetName, "J1", &tem); err != nil {
  1148. println(err.Error())
  1149. }
  1150. year := time.Now().Add(-24 * time.Hour).Format("2006")
  1151. f.SetCellValue(sheetName, "A1", "时间")
  1152. f.MergeCell(sheetName, "B1", "I1")
  1153. f.MergeCell(sheetName, "J1", "P1")
  1154. f.MergeCell(sheetName, "Q1", "W1")
  1155. f.MergeCell(sheetName, "X1", "AD1")
  1156. f.MergeCell(sheetName, "AE1", "AK1")
  1157. f.MergeCell(sheetName, "AL1", "AR1")
  1158. f.MergeCell(sheetName, "AS1", "AY1")
  1159. f.MergeCell(sheetName, "AZ1", "BF1")
  1160. f.MergeCell(sheetName, "BG1", "BM1")
  1161. f.MergeCell(sheetName, "BN1", "BT1")
  1162. f.MergeCell(sheetName, "BU1", "CA1")
  1163. f.MergeCell(sheetName, "CB1", "CH1")
  1164. f.MergeCell(sheetName, "CI1", "CO1")
  1165. f.SetCellValue(sheetName, "H1", year+"年")
  1166. f.SetSheetRow(sheetName, "A2", &[]interface{}{"部门", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率"})
  1167. if err := f.SetSheetRow(sheetName, "J2", &tem1); err != nil {
  1168. println(err.Error())
  1169. }
  1170. Engine := models.Engine
  1171. A := 3
  1172. pastures, err := Engine.SQL("select id,name from department where pastureId=? and isDel = 0 AND enable =1 and id <> ?", pastureId, pastureId).Query().List()
  1173. if err != nil {
  1174. println("err sql", err.Error())
  1175. } else {
  1176. for _, value := range pastures {
  1177. GetMailDataByDept(sheetName, value["name"].(string), time.Now().Format("2006-01-02"), value["id"], pastureId, A, f)
  1178. println(value["name"].(string))
  1179. A = A + 8
  1180. }
  1181. }
  1182. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMY").QueryString()
  1183. valuesMap1, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMM").QueryString()
  1184. sqls := ""
  1185. parammaps := make(map[string]interface{}, 0)
  1186. parammaps["month"] = time.Now().Format("2006-01-02")
  1187. parammaps["pastureId"] = pastureId
  1188. s_params := make([]interface{}, 0)
  1189. if err == nil && len(valuesMap) > 0 {
  1190. sqls = valuesMap[0]["sqlstr"]
  1191. paramslist := strings.Split(valuesMap[0]["params"], ",")
  1192. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  1193. for _, value := range paramslist {
  1194. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  1195. }
  1196. }
  1197. }
  1198. tem = []interface{}{}
  1199. //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
  1200. values, err := Engine.SQL(sqls, s_params...).Query().List()
  1201. B := A
  1202. temMap := make(map[string][]interface{}, 0)
  1203. if err == nil {
  1204. for _, value := range values {
  1205. tem = append(tem, value["ftype"])
  1206. tem = append(tem, value["monthBudget"])
  1207. tem = append(tem, value["monthLimit"])
  1208. tem = append(tem, value["qmonthBudget"])
  1209. tem = append(tem, value["sumPrice"])
  1210. tem = append(tem, value["asave"])
  1211. tem = append(tem, value["dac"])
  1212. tem = append(tem, value["dacY"])
  1213. temMap[value["ftype"].(string)] = tem
  1214. //if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B),&tem); err != nil {
  1215. // println(err.Error())
  1216. //}
  1217. tem = []interface{}{}
  1218. //B++
  1219. }
  1220. } else {
  1221. println(err.Error())
  1222. }
  1223. if m, ok := temMap["维修费(万元)"]; ok {
  1224. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1225. println(err.Error())
  1226. }
  1227. B++
  1228. } else {
  1229. temw := []interface{}{"维修费(万元)"}
  1230. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1231. println(err.Error())
  1232. }
  1233. B++
  1234. }
  1235. if m, ok := temMap["电费(万元)"]; ok {
  1236. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1237. println(err.Error())
  1238. }
  1239. B++
  1240. } else {
  1241. temw := []interface{}{"电费(万元)"}
  1242. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1243. println(err.Error())
  1244. }
  1245. B++
  1246. }
  1247. if m, ok := temMap["燃动费(万元)"]; ok {
  1248. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1249. println(err.Error())
  1250. }
  1251. B++
  1252. } else {
  1253. temw := []interface{}{"燃动费(万元)"}
  1254. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1255. println(err.Error())
  1256. }
  1257. B++
  1258. }
  1259. if m, ok := temMap["水费(万元)"]; ok {
  1260. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1261. println(err.Error())
  1262. }
  1263. B++
  1264. } else {
  1265. temw := []interface{}{"水费(万元)"}
  1266. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1267. println(err.Error())
  1268. }
  1269. B++
  1270. }
  1271. if m, ok := temMap["设备指标(万元)"]; ok {
  1272. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1273. println(err.Error())
  1274. }
  1275. B++
  1276. } else {
  1277. temw := []interface{}{"设备指标(万元)"}
  1278. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1279. println(err.Error())
  1280. }
  1281. B++
  1282. }
  1283. if m, ok := temMap["电量(度)"]; ok {
  1284. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1285. println(err.Error())
  1286. }
  1287. B++
  1288. } else {
  1289. temw := []interface{}{"电量(度)"}
  1290. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1291. println(err.Error())
  1292. }
  1293. B++
  1294. }
  1295. if m, ok := temMap["用水量(吨)"]; ok {
  1296. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1297. println(err.Error())
  1298. }
  1299. B++
  1300. } else {
  1301. temw := []interface{}{"用水量(吨)"}
  1302. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1303. println(err.Error())
  1304. }
  1305. B++
  1306. }
  1307. if m, ok := temMap["柴油用量(L)"]; ok {
  1308. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1309. println(err.Error())
  1310. }
  1311. B++
  1312. } else {
  1313. temw := []interface{}{"柴油用量(L)"}
  1314. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1315. println(err.Error())
  1316. }
  1317. B++
  1318. }
  1319. s_params = make([]interface{}, 0)
  1320. if err == nil && len(valuesMap1) > 0 {
  1321. sqls = valuesMap1[0]["sqlstr"]
  1322. parammaps := make(map[string]interface{}, 0)
  1323. parammaps["month"] = time.Now().Format("2006-01-02")
  1324. parammaps["pastureId"] = pastureId
  1325. paramslist := strings.Split(valuesMap1[0]["params"], ",")
  1326. if len(paramslist) > 0 && valuesMap1[0]["params"] != "" {
  1327. for _, value := range paramslist {
  1328. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  1329. }
  1330. }
  1331. }
  1332. f.SetCellValue(sheetName, "A"+strconv.Itoa(A), sheetName)
  1333. f.MergeCell(sheetName, "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
  1334. values, err = Engine.SQL(sqls, s_params...).Query().List()
  1335. tem = []interface{}{}
  1336. //d := append(c,nil)
  1337. //e := append(c,nil)
  1338. //g := append(c,nil)
  1339. //h := append(c,nil)
  1340. //o := append(c,nil)
  1341. //p := append(c,nil)
  1342. tem2 := []interface{}{}
  1343. if err == nil {
  1344. for _, value := range values {
  1345. tem2 = append(tem2, value["monthBudget"])
  1346. tem2 = append(tem2, value["monthLimit"])
  1347. tem2 = append(tem2, value["qmonthBudget"])
  1348. tem2 = append(tem2, value["sumPrice"])
  1349. tem2 = append(tem2, value["asave"])
  1350. tem2 = append(tem2, value["dac"])
  1351. tem2 = append(tem2, value["dacY"])
  1352. num, _ := strconv.Atoi(value["num"].(string))
  1353. switch value["ftype"] {
  1354. case "维修费(万元)":
  1355. c[(num-1)*7] = tem2[0]
  1356. c[(num-1)*7+1] = tem2[1]
  1357. c[(num-1)*7+2] = tem2[2]
  1358. c[(num-1)*7+3] = tem2[3]
  1359. c[(num-1)*7+4] = tem2[4]
  1360. c[(num-1)*7+5] = tem2[5]
  1361. c[(num-1)*7+6] = tem2[6]
  1362. case "电费(万元)":
  1363. d[(num-1)*7] = tem2[0]
  1364. d[(num-1)*7+1] = tem2[1]
  1365. d[(num-1)*7+2] = tem2[2]
  1366. d[(num-1)*7+3] = tem2[3]
  1367. d[(num-1)*7+4] = tem2[4]
  1368. d[(num-1)*7+5] = tem2[5]
  1369. d[(num-1)*7+6] = tem2[6]
  1370. case "燃动费(万元)":
  1371. e[(num-1)*7] = tem2[0]
  1372. e[(num-1)*7+1] = tem2[1]
  1373. e[(num-1)*7+2] = tem2[2]
  1374. e[(num-1)*7+3] = tem2[3]
  1375. e[(num-1)*7+4] = tem2[4]
  1376. e[(num-1)*7+5] = tem2[5]
  1377. e[(num-1)*7+6] = tem2[6]
  1378. case "水费(万元)":
  1379. g[(num-1)*7] = tem2[0]
  1380. g[(num-1)*7+1] = tem2[1]
  1381. g[(num-1)*7+2] = tem2[2]
  1382. g[(num-1)*7+3] = tem2[3]
  1383. g[(num-1)*7+4] = tem2[4]
  1384. g[(num-1)*7+5] = tem2[5]
  1385. g[(num-1)*7+6] = tem2[6]
  1386. case "电量(度)":
  1387. h[(num-1)*7] = tem2[0]
  1388. h[(num-1)*7+1] = tem2[1]
  1389. h[(num-1)*7+2] = tem2[2]
  1390. h[(num-1)*7+3] = tem2[3]
  1391. h[(num-1)*7+4] = tem2[4]
  1392. h[(num-1)*7+5] = tem2[5]
  1393. h[(num-1)*7+6] = tem2[6]
  1394. case "柴油用量(L)":
  1395. o[(num-1)*7] = tem2[0]
  1396. o[(num-1)*7+1] = tem2[1]
  1397. o[(num-1)*7+2] = tem2[2]
  1398. o[(num-1)*7+3] = tem2[3]
  1399. o[(num-1)*7+4] = tem2[4]
  1400. o[(num-1)*7+5] = tem2[5]
  1401. o[(num-1)*7+6] = tem2[6]
  1402. case "用水量(吨)":
  1403. p[(num-1)*7] = tem2[0]
  1404. p[(num-1)*7+1] = tem2[1]
  1405. p[(num-1)*7+2] = tem2[2]
  1406. p[(num-1)*7+3] = tem2[3]
  1407. p[(num-1)*7+4] = tem2[4]
  1408. p[(num-1)*7+5] = tem2[5]
  1409. p[(num-1)*7+6] = tem2[6]
  1410. case "设备指标(万元)":
  1411. r[(num-1)*7] = tem2[0]
  1412. r[(num-1)*7+1] = tem2[1]
  1413. r[(num-1)*7+2] = tem2[2]
  1414. r[(num-1)*7+3] = tem2[3]
  1415. r[(num-1)*7+4] = tem2[4]
  1416. r[(num-1)*7+5] = tem2[5]
  1417. r[(num-1)*7+6] = tem2[6]
  1418. }
  1419. tem2 = []interface{}{}
  1420. }
  1421. } else {
  1422. println(err.Error())
  1423. }
  1424. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A), &c); err != nil {
  1425. println(err.Error())
  1426. }
  1427. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+1), &d); err != nil {
  1428. println(err.Error())
  1429. }
  1430. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+2), &e); err != nil {
  1431. println(err.Error())
  1432. }
  1433. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+3), &g); err != nil {
  1434. println(err.Error())
  1435. }
  1436. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+4), &r); err != nil {
  1437. println(err.Error())
  1438. }
  1439. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+5), &h); err != nil {
  1440. println(err.Error())
  1441. }
  1442. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+6), &p); err != nil {
  1443. println(err.Error())
  1444. }
  1445. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+7), &o); err != nil {
  1446. println(err.Error())
  1447. }
  1448. //sums[2] = sums[2].(int)+value["monthLimit"].(int)
  1449. //sums[1] = sums[1].(int)+value["monthBudget"].(int)
  1450. // 设置月份单元格字体
  1451. monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
  1452. if err != nil {
  1453. println("monthStyle err", err.Error())
  1454. }
  1455. f.SetCellStyle(sheetName, "A1", "CS300", monthStyle)
  1456. // 根据指定路径保存文件
  1457. }
  1458. func PathCheck(path string) (err error) {
  1459. b, err := PathExists(path)
  1460. if err != nil {
  1461. println("exist err", err)
  1462. }
  1463. if !b {
  1464. err = os.Mkdir(path, 0777)
  1465. if err != nil {
  1466. println("Mkdir err", err)
  1467. }
  1468. }
  1469. return
  1470. }
  1471. func PathExists(path string) (bool, error) {
  1472. _, err := os.Stat(path)
  1473. if err == nil {
  1474. return true, nil
  1475. }
  1476. if os.IsNotExist(err) {
  1477. return false, nil
  1478. }
  1479. return false, err
  1480. }
  1481. func DownloadDataJT(date string) (string, error) {
  1482. filename := setting.CurrentPath+"uploads/Downloadmailreport/" + date + "集团设备指标.xlsx"
  1483. exist, _ := FileExists(filename)
  1484. println(filename,exist)
  1485. if exist {
  1486. //return filename, nil
  1487. }
  1488. f := excelize.NewFile()
  1489. f.SetSheetName("Sheet1", "集团设备指标")
  1490. tem := make([]interface{}, 0)
  1491. tem1 := make([]interface{}, 0)
  1492. c := []interface{}{}
  1493. d := []interface{}{}
  1494. e := []interface{}{}
  1495. g := []interface{}{}
  1496. h := []interface{}{}
  1497. o := []interface{}{}
  1498. p := []interface{}{}
  1499. r := []interface{}{}
  1500. for i := 1; i <= 12; i++ {
  1501. a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
  1502. b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
  1503. c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1504. d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1505. e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1506. g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1507. h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1508. o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1509. p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1510. r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1511. tem = append(tem, a...)
  1512. tem1 = append(tem1, b...)
  1513. }
  1514. if err := f.SetSheetRow("集团设备指标", "N1", &tem); err != nil {
  1515. println(err.Error())
  1516. }
  1517. //year :=time.Now().Add(-24*time.Hour).Format("2006")
  1518. year := ""
  1519. datetem, _ := strconv.Atoi(date[:4])
  1520. if date > time.Now().Add(-24*time.Hour).Format("2006")+"-01-01" {
  1521. year = strconv.Itoa(datetem)
  1522. } else {
  1523. year = strconv.Itoa(datetem - 1)
  1524. }
  1525. f.SetCellValue("集团设备指标", "A1", "时间")
  1526. f.MergeCell("集团设备指标", "B1", "M1")
  1527. f.MergeCell("集团设备指标", "N1", "T1")
  1528. f.MergeCell("集团设备指标", "U1", "AA1")
  1529. f.MergeCell("集团设备指标", "AB1", "AH1")
  1530. f.MergeCell("集团设备指标", "AI1", "AO1")
  1531. f.MergeCell("集团设备指标", "AP1", "AV1")
  1532. f.MergeCell("集团设备指标", "AW1", "BC1")
  1533. f.MergeCell("集团设备指标", "BD1", "BJ1")
  1534. f.MergeCell("集团设备指标", "BK1", "BQ1")
  1535. f.MergeCell("集团设备指标", "BR1", "BX1")
  1536. f.MergeCell("集团设备指标", "BY1", "CE1")
  1537. f.MergeCell("集团设备指标", "CF1", "CL1")
  1538. f.MergeCell("集团设备指标", "CM1", "CS1")
  1539. f.SetCellValue("集团设备指标", "H1", year+"年")
  1540. f.SetSheetRow("集团设备指标", "A2", &[]interface{}{"牧场", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率", "迄今饲养头日", "迄今产量(公斤)", "迄今实际单头牛成本", "迄今实际公斤奶成本(元/公斤)"})
  1541. if err := f.SetSheetRow("集团设备指标", "N2", &tem1); err != nil {
  1542. println(err.Error())
  1543. }
  1544. Engine := models.Engine
  1545. A := 3
  1546. pastures, err := Engine.SQL("select id,name from pasture where isdel = 0 AND id <> 18 order by sort").Query().List()
  1547. if err != nil {
  1548. println("err sql", err.Error())
  1549. } else {
  1550. for _, value := range pastures {
  1551. GetMailDataByPasture("集团设备指标", value["name"].(string), date, value["id"], A, f)
  1552. println(value["name"].(string))
  1553. A = A + 8
  1554. }
  1555. }
  1556. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJY").QueryString()
  1557. valuesMap1, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportJM").QueryString()
  1558. sqls := ""
  1559. parammaps := make(map[string]interface{}, 0)
  1560. parammaps["month"] = date
  1561. s_params := make([]interface{}, 0)
  1562. if err == nil && len(valuesMap) > 0 {
  1563. sqls = valuesMap[0]["sqlstr"]
  1564. paramslist := strings.Split(valuesMap[0]["params"], ",")
  1565. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  1566. for _, value := range paramslist {
  1567. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  1568. }
  1569. }
  1570. }
  1571. tem = []interface{}{}
  1572. values, err := Engine.SQL(sqls, s_params...).Query().List()
  1573. B := A
  1574. temMap := make(map[string][]interface{}, 0)
  1575. if err == nil {
  1576. for _, value := range values {
  1577. tem = append(tem, value["ftype"])
  1578. tem = append(tem, value["monthBudget"])
  1579. tem = append(tem, value["monthLimit"])
  1580. tem = append(tem, value["qmonthBudget"])
  1581. tem = append(tem, value["sumPrice"])
  1582. tem = append(tem, value["asave"])
  1583. tem = append(tem, value["dac"])
  1584. tem = append(tem, value["dacY"])
  1585. tem = append(tem, value["cowNum"])
  1586. tem = append(tem, value["saleNum"])
  1587. tem = append(tem, value["cowprice"])
  1588. tem = append(tem, value["milkprice"])
  1589. temMap[value["ftype"].(string)] = tem
  1590. tem = []interface{}{}
  1591. //B++
  1592. }
  1593. } else {
  1594. println(err.Error())
  1595. }
  1596. if m, ok := temMap["维修费(万元)"]; ok {
  1597. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1598. println(err.Error())
  1599. }
  1600. B++
  1601. } else {
  1602. temw := []interface{}{"维修费(万元)"}
  1603. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1604. println(err.Error())
  1605. }
  1606. B++
  1607. }
  1608. if m, ok := temMap["电费(万元)"]; ok {
  1609. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1610. println(err.Error())
  1611. }
  1612. B++
  1613. } else {
  1614. temw := []interface{}{"电费(万元)"}
  1615. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1616. println(err.Error())
  1617. }
  1618. B++
  1619. }
  1620. if m, ok := temMap["燃动费(万元)"]; ok {
  1621. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1622. println(err.Error())
  1623. }
  1624. B++
  1625. } else {
  1626. temw := []interface{}{"燃动费(万元)"}
  1627. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1628. println(err.Error())
  1629. }
  1630. B++
  1631. }
  1632. if m, ok := temMap["水费(万元)"]; ok {
  1633. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1634. println(err.Error())
  1635. }
  1636. B++
  1637. } else {
  1638. temw := []interface{}{"水费(万元)"}
  1639. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1640. println(err.Error())
  1641. }
  1642. B++
  1643. }
  1644. if m, ok := temMap["设备指标(万元)"]; ok {
  1645. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1646. println(err.Error())
  1647. }
  1648. B++
  1649. } else {
  1650. temw := []interface{}{"设备指标(万元)"}
  1651. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1652. println(err.Error())
  1653. }
  1654. B++
  1655. }
  1656. if m, ok := temMap["电量(度)"]; ok {
  1657. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1658. println(err.Error())
  1659. }
  1660. B++
  1661. } else {
  1662. temw := []interface{}{"电量(度)"}
  1663. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1664. println(err.Error())
  1665. }
  1666. B++
  1667. }
  1668. if m, ok := temMap["用水量(吨)"]; ok {
  1669. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1670. println(err.Error())
  1671. }
  1672. B++
  1673. } else {
  1674. temw := []interface{}{"用水量(吨)"}
  1675. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1676. println(err.Error())
  1677. }
  1678. B++
  1679. }
  1680. if m, ok := temMap["柴油用量(L)"]; ok {
  1681. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &m); err != nil {
  1682. println(err.Error())
  1683. }
  1684. B++
  1685. } else {
  1686. temw := []interface{}{"柴油用量(L)"}
  1687. if err := f.SetSheetRow("集团设备指标", "B"+strconv.Itoa(B), &temw); err != nil {
  1688. println(err.Error())
  1689. }
  1690. B++
  1691. }
  1692. s_params = make([]interface{}, 0)
  1693. if err == nil && len(valuesMap1) > 0 {
  1694. sqls = valuesMap1[0]["sqlstr"]
  1695. parammaps := make(map[string]interface{}, 0)
  1696. parammaps["month"] = date
  1697. paramslist := strings.Split(valuesMap1[0]["params"], ",")
  1698. if len(paramslist) > 0 && valuesMap1[0]["params"] != "" {
  1699. for _, value := range paramslist {
  1700. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  1701. }
  1702. }
  1703. }
  1704. f.SetCellValue("集团设备指标", "A"+strconv.Itoa(A), "集团")
  1705. f.MergeCell("集团设备指标", "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
  1706. values, err = Engine.SQL(sqls, s_params...).Query().List()
  1707. tem = []interface{}{}
  1708. tem2 := []interface{}{}
  1709. if err == nil {
  1710. for _, value := range values {
  1711. tem2 = append(tem2, value["monthBudget"])
  1712. tem2 = append(tem2, value["monthLimit"])
  1713. tem2 = append(tem2, value["qmonthBudget"])
  1714. tem2 = append(tem2, value["sumPrice"])
  1715. tem2 = append(tem2, value["asave"])
  1716. tem2 = append(tem2, value["dac"])
  1717. tem2 = append(tem2, value["dacY"])
  1718. num, _ := strconv.Atoi(value["num"].(string))
  1719. switch value["ftype"] {
  1720. case "维修费(万元)":
  1721. c[(num-1)*7] = tem2[0]
  1722. c[(num-1)*7+1] = tem2[1]
  1723. c[(num-1)*7+2] = tem2[2]
  1724. c[(num-1)*7+3] = tem2[3]
  1725. c[(num-1)*7+4] = tem2[4]
  1726. c[(num-1)*7+5] = tem2[5]
  1727. c[(num-1)*7+6] = tem2[6]
  1728. case "电费(万元)":
  1729. d[(num-1)*7] = tem2[0]
  1730. d[(num-1)*7+1] = tem2[1]
  1731. d[(num-1)*7+2] = tem2[2]
  1732. d[(num-1)*7+3] = tem2[3]
  1733. d[(num-1)*7+4] = tem2[4]
  1734. d[(num-1)*7+5] = tem2[5]
  1735. d[(num-1)*7+6] = tem2[6]
  1736. case "燃动费(万元)":
  1737. e[(num-1)*7] = tem2[0]
  1738. e[(num-1)*7+1] = tem2[1]
  1739. e[(num-1)*7+2] = tem2[2]
  1740. e[(num-1)*7+3] = tem2[3]
  1741. e[(num-1)*7+4] = tem2[4]
  1742. e[(num-1)*7+5] = tem2[5]
  1743. e[(num-1)*7+6] = tem2[6]
  1744. case "水费(万元)":
  1745. g[(num-1)*7] = tem2[0]
  1746. g[(num-1)*7+1] = tem2[1]
  1747. g[(num-1)*7+2] = tem2[2]
  1748. g[(num-1)*7+3] = tem2[3]
  1749. g[(num-1)*7+4] = tem2[4]
  1750. g[(num-1)*7+5] = tem2[5]
  1751. g[(num-1)*7+6] = tem2[6]
  1752. case "电量(度)":
  1753. h[(num-1)*7] = tem2[0]
  1754. h[(num-1)*7+1] = tem2[1]
  1755. h[(num-1)*7+2] = tem2[2]
  1756. h[(num-1)*7+3] = tem2[3]
  1757. h[(num-1)*7+4] = tem2[4]
  1758. h[(num-1)*7+5] = tem2[5]
  1759. h[(num-1)*7+6] = tem2[6]
  1760. case "柴油用量(L)":
  1761. o[(num-1)*7] = tem2[0]
  1762. o[(num-1)*7+1] = tem2[1]
  1763. o[(num-1)*7+2] = tem2[2]
  1764. o[(num-1)*7+3] = tem2[3]
  1765. o[(num-1)*7+4] = tem2[4]
  1766. o[(num-1)*7+5] = tem2[5]
  1767. o[(num-1)*7+6] = tem2[6]
  1768. case "用水量(吨)":
  1769. p[(num-1)*7] = tem2[0]
  1770. p[(num-1)*7+1] = tem2[1]
  1771. p[(num-1)*7+2] = tem2[2]
  1772. p[(num-1)*7+3] = tem2[3]
  1773. p[(num-1)*7+4] = tem2[4]
  1774. p[(num-1)*7+5] = tem2[5]
  1775. p[(num-1)*7+6] = tem2[6]
  1776. case "设备指标(万元)":
  1777. r[(num-1)*7] = tem2[0]
  1778. r[(num-1)*7+1] = tem2[1]
  1779. r[(num-1)*7+2] = tem2[2]
  1780. r[(num-1)*7+3] = tem2[3]
  1781. r[(num-1)*7+4] = tem2[4]
  1782. r[(num-1)*7+5] = tem2[5]
  1783. r[(num-1)*7+6] = tem2[6]
  1784. }
  1785. tem2 = []interface{}{}
  1786. }
  1787. } else {
  1788. println(err.Error())
  1789. }
  1790. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A), &c); err != nil {
  1791. println(err.Error())
  1792. }
  1793. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+1), &d); err != nil {
  1794. println(err.Error())
  1795. }
  1796. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+2), &e); err != nil {
  1797. println(err.Error())
  1798. }
  1799. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+3), &g); err != nil {
  1800. println(err.Error())
  1801. }
  1802. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+4), &r); err != nil {
  1803. println(err.Error())
  1804. }
  1805. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+5), &h); err != nil {
  1806. println(err.Error())
  1807. }
  1808. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+6), &p); err != nil {
  1809. println(err.Error())
  1810. }
  1811. if err := f.SetSheetRow("集团设备指标", "N"+strconv.Itoa(A+7), &o); err != nil {
  1812. println(err.Error())
  1813. }
  1814. // 设置月份单元格字体
  1815. monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
  1816. if err != nil {
  1817. println("monthStyle err", err.Error())
  1818. }
  1819. f.SetCellStyle("集团设备指标", "A1", "CS154", monthStyle)
  1820. // 根据指定路径保存文件
  1821. err = PathCheck("uploads/Downloadmailreport") //检查路径并创建
  1822. //os.Remove(filename)
  1823. if err != nil {
  1824. println("PathCheck err", err)
  1825. }
  1826. if err := f.SaveAs(filename); err != nil {
  1827. println(err.Error())
  1828. return "", err
  1829. }
  1830. return filename, err
  1831. }
  1832. func DownloadDataMC(sheetName, date string, pastureId interface{}) (string, error) {
  1833. filename := setting.CurrentPath+ "uploads/Downloadmailreport/" + date + sheetName + "设备指标.xlsx"
  1834. exist, _ := FileExists(filename)
  1835. if exist {
  1836. //return filename, nil
  1837. }
  1838. f := excelize.NewFile()
  1839. f.SetSheetName("Sheet1", sheetName)
  1840. tem := make([]interface{}, 0)
  1841. tem1 := make([]interface{}, 0)
  1842. c := []interface{}{}
  1843. d := []interface{}{}
  1844. e := []interface{}{}
  1845. g := []interface{}{}
  1846. h := []interface{}{}
  1847. o := []interface{}{}
  1848. p := []interface{}{}
  1849. r := []interface{}{}
  1850. for i := 1; i <= 12; i++ {
  1851. a := []interface{}{strconv.Itoa(i) + "月", nil, nil, nil, nil, nil, nil}
  1852. b := []interface{}{"预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "月度达成率"}
  1853. c = append(c, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1854. d = append(d, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1855. e = append(e, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1856. g = append(g, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1857. h = append(h, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1858. o = append(o, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1859. p = append(p, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1860. r = append(r, []interface{}{0, 0, 0, 0, 0, 0, 0}...)
  1861. tem = append(tem, a...)
  1862. tem1 = append(tem1, b...)
  1863. }
  1864. if err := f.SetSheetRow(sheetName, "J1", &tem); err != nil {
  1865. println(err.Error())
  1866. }
  1867. year := ""
  1868. datetem, _ := strconv.Atoi(date[:4])
  1869. if date > time.Now().Add(-24*time.Hour).Format("2006")+"-01-01" {
  1870. year = strconv.Itoa(datetem)
  1871. } else {
  1872. year = strconv.Itoa(datetem - 1)
  1873. }
  1874. f.SetCellValue(sheetName, "A1", "时间")
  1875. f.MergeCell(sheetName, "B1", "I1")
  1876. f.MergeCell(sheetName, "J1", "P1")
  1877. f.MergeCell(sheetName, "Q1", "W1")
  1878. f.MergeCell(sheetName, "X1", "AD1")
  1879. f.MergeCell(sheetName, "AE1", "AK1")
  1880. f.MergeCell(sheetName, "AL1", "AR1")
  1881. f.MergeCell(sheetName, "AS1", "AY1")
  1882. f.MergeCell(sheetName, "AZ1", "BF1")
  1883. f.MergeCell(sheetName, "BG1", "BM1")
  1884. f.MergeCell(sheetName, "BN1", "BT1")
  1885. f.MergeCell(sheetName, "BU1", "CA1")
  1886. f.MergeCell(sheetName, "CB1", "CH1")
  1887. f.MergeCell(sheetName, "CI1", "CO1")
  1888. f.SetCellValue(sheetName, "H1", year+"年")
  1889. f.SetSheetRow(sheetName, "A2", &[]interface{}{"部门", "成本项目", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "迄今达成率", "年度达成率"})
  1890. if err := f.SetSheetRow(sheetName, "J2", &tem1); err != nil {
  1891. println(err.Error())
  1892. }
  1893. Engine := models.Engine
  1894. A := 3
  1895. pastures, err := Engine.SQL("select id,name from department where pastureId=? and isDel = 0 AND enable =1 and id <> ?", pastureId, pastureId).Query().List()
  1896. if err != nil {
  1897. println("err sql", err.Error())
  1898. } else {
  1899. for _, value := range pastures {
  1900. GetMailDataByDept(sheetName, value["name"].(string), date, value["id"], pastureId, A, f)
  1901. println(value["name"].(string))
  1902. A = A + 8
  1903. }
  1904. }
  1905. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMY").QueryString()
  1906. valuesMap1, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", "getMailReportMM").QueryString()
  1907. sqls := ""
  1908. parammaps := make(map[string]interface{}, 0)
  1909. parammaps["month"] = date
  1910. parammaps["pastureId"] = pastureId
  1911. s_params := make([]interface{}, 0)
  1912. if err == nil && len(valuesMap) > 0 {
  1913. sqls = valuesMap[0]["sqlstr"]
  1914. paramslist := strings.Split(valuesMap[0]["params"], ",")
  1915. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  1916. for _, value := range paramslist {
  1917. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  1918. }
  1919. }
  1920. }
  1921. tem = []interface{}{}
  1922. //sums := []interface{}{"设备指标(万元)",0,0,0,0,0,0,0,0}
  1923. values, err := Engine.SQL(sqls, s_params...).Query().List()
  1924. B := A
  1925. temMap := make(map[string][]interface{}, 0)
  1926. if err == nil {
  1927. for _, value := range values {
  1928. tem = append(tem, value["ftype"])
  1929. tem = append(tem, value["monthBudget"])
  1930. tem = append(tem, value["monthLimit"])
  1931. tem = append(tem, value["qmonthBudget"])
  1932. tem = append(tem, value["sumPrice"])
  1933. tem = append(tem, value["asave"])
  1934. tem = append(tem, value["dac"])
  1935. tem = append(tem, value["dacY"])
  1936. temMap[value["ftype"].(string)] = tem
  1937. //if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B),&tem); err != nil {
  1938. // println(err.Error())
  1939. //}
  1940. tem = []interface{}{}
  1941. //B++
  1942. }
  1943. } else {
  1944. println(err.Error())
  1945. }
  1946. if m, ok := temMap["维修费(万元)"]; ok {
  1947. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1948. println(err.Error())
  1949. }
  1950. B++
  1951. } else {
  1952. temw := []interface{}{"维修费(万元)"}
  1953. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1954. println(err.Error())
  1955. }
  1956. B++
  1957. }
  1958. if m, ok := temMap["电费(万元)"]; ok {
  1959. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1960. println(err.Error())
  1961. }
  1962. B++
  1963. } else {
  1964. temw := []interface{}{"电费(万元)"}
  1965. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1966. println(err.Error())
  1967. }
  1968. B++
  1969. }
  1970. if m, ok := temMap["燃动费(万元)"]; ok {
  1971. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1972. println(err.Error())
  1973. }
  1974. B++
  1975. } else {
  1976. temw := []interface{}{"燃动费(万元)"}
  1977. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1978. println(err.Error())
  1979. }
  1980. B++
  1981. }
  1982. if m, ok := temMap["水费(万元)"]; ok {
  1983. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1984. println(err.Error())
  1985. }
  1986. B++
  1987. } else {
  1988. temw := []interface{}{"水费(万元)"}
  1989. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  1990. println(err.Error())
  1991. }
  1992. B++
  1993. }
  1994. if m, ok := temMap["设备指标(万元)"]; ok {
  1995. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  1996. println(err.Error())
  1997. }
  1998. B++
  1999. } else {
  2000. temw := []interface{}{"设备指标(万元)"}
  2001. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  2002. println(err.Error())
  2003. }
  2004. B++
  2005. }
  2006. if m, ok := temMap["电量(度)"]; ok {
  2007. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  2008. println(err.Error())
  2009. }
  2010. B++
  2011. } else {
  2012. temw := []interface{}{"电量(度)"}
  2013. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  2014. println(err.Error())
  2015. }
  2016. B++
  2017. }
  2018. if m, ok := temMap["用水量(吨)"]; ok {
  2019. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  2020. println(err.Error())
  2021. }
  2022. B++
  2023. } else {
  2024. temw := []interface{}{"用水量(吨)"}
  2025. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  2026. println(err.Error())
  2027. }
  2028. B++
  2029. }
  2030. if m, ok := temMap["柴油用量(L)"]; ok {
  2031. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &m); err != nil {
  2032. println(err.Error())
  2033. }
  2034. B++
  2035. } else {
  2036. temw := []interface{}{"柴油用量(L)"}
  2037. if err := f.SetSheetRow(sheetName, "B"+strconv.Itoa(B), &temw); err != nil {
  2038. println(err.Error())
  2039. }
  2040. B++
  2041. }
  2042. s_params = make([]interface{}, 0)
  2043. if err == nil && len(valuesMap1) > 0 {
  2044. sqls = valuesMap1[0]["sqlstr"]
  2045. parammaps := make(map[string]interface{}, 0)
  2046. parammaps["month"] = date
  2047. parammaps["pastureId"] = pastureId
  2048. paramslist := strings.Split(valuesMap1[0]["params"], ",")
  2049. if len(paramslist) > 0 && valuesMap1[0]["params"] != "" {
  2050. for _, value := range paramslist {
  2051. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  2052. }
  2053. }
  2054. }
  2055. f.SetCellValue(sheetName, "A"+strconv.Itoa(A), sheetName)
  2056. f.MergeCell(sheetName, "A"+strconv.Itoa(A), "A"+strconv.Itoa(A+7))
  2057. values, err = Engine.SQL(sqls, s_params...).Query().List()
  2058. tem = []interface{}{}
  2059. //d := append(c,nil)
  2060. //e := append(c,nil)
  2061. //g := append(c,nil)
  2062. //h := append(c,nil)
  2063. //o := append(c,nil)
  2064. //p := append(c,nil)
  2065. tem2 := []interface{}{}
  2066. if err == nil {
  2067. for _, value := range values {
  2068. tem2 = append(tem2, value["monthBudget"])
  2069. tem2 = append(tem2, value["monthLimit"])
  2070. tem2 = append(tem2, value["qmonthBudget"])
  2071. tem2 = append(tem2, value["sumPrice"])
  2072. tem2 = append(tem2, value["asave"])
  2073. tem2 = append(tem2, value["dac"])
  2074. tem2 = append(tem2, value["dacY"])
  2075. num, _ := strconv.Atoi(value["num"].(string))
  2076. switch value["ftype"] {
  2077. case "维修费(万元)":
  2078. c[(num-1)*7] = tem2[0]
  2079. c[(num-1)*7+1] = tem2[1]
  2080. c[(num-1)*7+2] = tem2[2]
  2081. c[(num-1)*7+3] = tem2[3]
  2082. c[(num-1)*7+4] = tem2[4]
  2083. c[(num-1)*7+5] = tem2[5]
  2084. c[(num-1)*7+6] = tem2[6]
  2085. case "电费(万元)":
  2086. d[(num-1)*7] = tem2[0]
  2087. d[(num-1)*7+1] = tem2[1]
  2088. d[(num-1)*7+2] = tem2[2]
  2089. d[(num-1)*7+3] = tem2[3]
  2090. d[(num-1)*7+4] = tem2[4]
  2091. d[(num-1)*7+5] = tem2[5]
  2092. d[(num-1)*7+6] = tem2[6]
  2093. case "燃动费(万元)":
  2094. e[(num-1)*7] = tem2[0]
  2095. e[(num-1)*7+1] = tem2[1]
  2096. e[(num-1)*7+2] = tem2[2]
  2097. e[(num-1)*7+3] = tem2[3]
  2098. e[(num-1)*7+4] = tem2[4]
  2099. e[(num-1)*7+5] = tem2[5]
  2100. e[(num-1)*7+6] = tem2[6]
  2101. case "水费(万元)":
  2102. g[(num-1)*7] = tem2[0]
  2103. g[(num-1)*7+1] = tem2[1]
  2104. g[(num-1)*7+2] = tem2[2]
  2105. g[(num-1)*7+3] = tem2[3]
  2106. g[(num-1)*7+4] = tem2[4]
  2107. g[(num-1)*7+5] = tem2[5]
  2108. g[(num-1)*7+6] = tem2[6]
  2109. case "电量(度)":
  2110. h[(num-1)*7] = tem2[0]
  2111. h[(num-1)*7+1] = tem2[1]
  2112. h[(num-1)*7+2] = tem2[2]
  2113. h[(num-1)*7+3] = tem2[3]
  2114. h[(num-1)*7+4] = tem2[4]
  2115. h[(num-1)*7+5] = tem2[5]
  2116. h[(num-1)*7+6] = tem2[6]
  2117. case "柴油用量(L)":
  2118. o[(num-1)*7] = tem2[0]
  2119. o[(num-1)*7+1] = tem2[1]
  2120. o[(num-1)*7+2] = tem2[2]
  2121. o[(num-1)*7+3] = tem2[3]
  2122. o[(num-1)*7+4] = tem2[4]
  2123. o[(num-1)*7+5] = tem2[5]
  2124. o[(num-1)*7+6] = tem2[6]
  2125. case "用水量(吨)":
  2126. p[(num-1)*7] = tem2[0]
  2127. p[(num-1)*7+1] = tem2[1]
  2128. p[(num-1)*7+2] = tem2[2]
  2129. p[(num-1)*7+3] = tem2[3]
  2130. p[(num-1)*7+4] = tem2[4]
  2131. p[(num-1)*7+5] = tem2[5]
  2132. p[(num-1)*7+6] = tem2[6]
  2133. case "设备指标(万元)":
  2134. r[(num-1)*7] = tem2[0]
  2135. r[(num-1)*7+1] = tem2[1]
  2136. r[(num-1)*7+2] = tem2[2]
  2137. r[(num-1)*7+3] = tem2[3]
  2138. r[(num-1)*7+4] = tem2[4]
  2139. r[(num-1)*7+5] = tem2[5]
  2140. r[(num-1)*7+6] = tem2[6]
  2141. }
  2142. tem2 = []interface{}{}
  2143. }
  2144. } else {
  2145. println(err.Error())
  2146. }
  2147. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A), &c); err != nil {
  2148. println(err.Error())
  2149. }
  2150. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+1), &d); err != nil {
  2151. println(err.Error())
  2152. }
  2153. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+2), &e); err != nil {
  2154. println(err.Error())
  2155. }
  2156. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+3), &g); err != nil {
  2157. println(err.Error())
  2158. }
  2159. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+4), &r); err != nil {
  2160. println(err.Error())
  2161. }
  2162. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+5), &h); err != nil {
  2163. println(err.Error())
  2164. }
  2165. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+6), &p); err != nil {
  2166. println(err.Error())
  2167. }
  2168. if err := f.SetSheetRow(sheetName, "J"+strconv.Itoa(A+7), &o); err != nil {
  2169. println(err.Error())
  2170. }
  2171. //sums[2] = sums[2].(int)+value["monthLimit"].(int)
  2172. //sums[1] = sums[1].(int)+value["monthBudget"].(int)
  2173. // 设置月份单元格字体
  2174. monthStyle, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
  2175. if err != nil {
  2176. println("monthStyle err", err.Error())
  2177. }
  2178. f.SetCellStyle(sheetName, "A1", "CS300", monthStyle)
  2179. // 根据指定路径保存文件
  2180. //filename :="uploads/Downloadmailreport/" + date +sheetName+"设备指标.xlsx"
  2181. err = PathCheck("uploads/Downloadmailreport") //检查路径并创建
  2182. //os.Remove(filename)
  2183. if err != nil {
  2184. println("PathCheck err", err)
  2185. }
  2186. if err := f.SaveAs(filename); err != nil {
  2187. println(err.Error())
  2188. return "", err
  2189. }
  2190. return filename, err
  2191. }
  2192. func FileExists(path string) (bool, error) {
  2193. _, err := os.Stat(path)
  2194. if err == nil {
  2195. return true, nil
  2196. }
  2197. if os.IsNotExist(err) {
  2198. return false, nil
  2199. }
  2200. return false, err
  2201. }
  2202. //测试用的
  2203. func Sssss(){
  2204. sqls :="SELECT bbd.`buyeCode`,p.`providerName`,bd.receiveAmount,bbd.`matchCode`,bd.`amount`,bd.`sumAmount`,bd.departmentName,pa.name pastureName," +
  2205. "bd.note,c.id,c.bigId,c.pastureId,ps.id partId,ifnull(ps.name,c.partName) partName,ifnull(ps.partCode,c.partCode) partCode,"+
  2206. "ifnull(ps.specification,c.specification) specification,ifnull(brand.brandName,c.brand) brand,ifnull(ps.unit,c.unit) unit,"+
  2207. "ifnull(TRIM(TRAILING '.' FROM (TRIM(TRAILING '0' FROM bd.price))),TRIM(TRAILING '.' FROM (TRIM(TRAILING '0' FROM c.price))))"+
  2208. "price,brand.id brandId,c.inventoryType,c.planAmount,c.remark,c.enable,c.isZeroStock,e.`empname`,"+
  2209. "DATE_FORMAT(bbd.buyerDate,'%Y-%m-%d')buyerDate,DATE_FORMAT(bbd.DoneDate, '%Y-%m-%d') DoneDate "+
  2210. "FROM buydetail bd INNER JOIN bigbuydetail bbd ON bbd.id = bd.`bigId` LEFT JOIN provider p ON p.id = bbd.`providerId`"+
  2211. "LEFT JOIN contract c ON c.id = bd.`contractId` LEFT JOIN emp e ON e.id = bbd.`buyerPerson` " +
  2212. "LEFT JOIN bigpartdeal bpd ON bpd.`matchCode` = bbd.`matchCode` inner join pasture pa on pa.id = bbd.pastureId " +
  2213. "LEFT JOIN parts ps ON ps.partCode =bd.partCode LEFT JOIN brand ON brand.id=bd.brandId " +
  2214. "where bbd.`enable`=0 and bd.amount > 0 and bbd.buyerDate < ? and bbd.buyerDate > ? and pa.name='通辽牧场' "
  2215. Engine := models.Engine
  2216. queryData, err := Engine.SQL(sqls,"2021-08-27","2021-06-30").Query().List()
  2217. //s_params := make([]interface{}, 0)
  2218. //queryData, err := restful.QueryByListListT(sql, 0, 0, s_params, tx)
  2219. if err != nil{
  2220. fmt.Println(err)
  2221. }else {
  2222. f := excelize.NewFile()
  2223. // 创建一个工作表
  2224. //供应商 单价 申购数量 采购数量 入库数 申购部门 采购人 配单日期 入库时间
  2225. index := f.NewSheet("采购明细")
  2226. f.SetColWidth("采购明细","A","R",20)
  2227. columns := []string{"备件品牌","计量单位","供应商","单价","申购数量","采购数量","入库数","申购部门","采购人","配单日期",
  2228. "入库时间"}
  2229. 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"}
  2230. for i,v :=range columns{
  2231. f.SetCellValue("采购明细",letters[i]+"1",v)
  2232. }
  2233. fmt.Println(len(queryData))
  2234. for i,v := range(queryData){
  2235. // 设置单元格的值
  2236. value :=[]interface{}{v["matchCode"],v["unit"],v["providerName"],v["price"],v["sumAmount"],v["amount"],v["receiveAmount"],
  2237. v["departmentName"],v["empname"],v["buyerDate"],v["DoneDate"]}
  2238. for a,b :=range(value){
  2239. f.SetCellValue("采购明细", letters[a]+strconv.Itoa(i+2),b )
  2240. }
  2241. }
  2242. // 设置工作簿的默认工作表
  2243. f.SetActiveSheet(index)
  2244. // 根据指定路径保存文件
  2245. if err := f.SaveAs("D:/project/go/src/KPTcomm/test/采购明细.xlsx"); err != nil {
  2246. fmt.Println(err)
  2247. }
  2248. }
  2249. }
  2250. // 测试
  2251. func DownloadEquipment(parammaps string)(string, error) {
  2252. //appG := app.Gin{C: context}
  2253. data := make(map[string]interface{}, 0)
  2254. filename := setting.CurrentPath + "uploads/Downloadmailreport/" + "设备指标.xlsx"
  2255. exist, _ := FileExists(filename)
  2256. if exist {
  2257. //return filename, nil
  2258. }
  2259. f := excelize.NewFile()
  2260. Style, err := f.NewStyle("{\"Alignment\":{\"Vertical\": \"center\", \"Horizontal\":\"center\"}}")
  2261. columns := []string{"设备指标", "维修费", "水费", "电费", "柴油费", "水量", "电量", "柴油用量"}
  2262. //序号 牧场 部门 预算 内控 迄今内控 迄今实际 实际节约 实际达成率 单头牛预算 单头牛内控 单头牛迄今内控 单头牛迄今实际 单头牛实际节约
  2263. columns1 := []string{"序号", "牧场", "部门", "预算", "内控", "迄今内控", "迄今实际", "实际节约", "实际达成率", "单头牛预算",
  2264. "单头牛内控", "单头牛迄今内控", "单头牛迄今实际", "单头牛实际节约"}
  2265. 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"}
  2266. for i, v := range (columns) {
  2267. f.NewSheet("Sheet" + strconv.Itoa(i+1))
  2268. f.SetSheetName("Sheet"+strconv.Itoa(i+1), v)
  2269. f.MergeCell(v, "A1", "C1")
  2270. f.MergeCell(v, "D1", "I1")
  2271. f.MergeCell(v, "J1", "N1")
  2272. f.SetCellValue(v, "A1", "设备指标")
  2273. f.SetCellValue(v, "D1", "设备指标")
  2274. f.SetCellValue(v, "J1", "单头牛指标")
  2275. f.SetCellStyle(v, "A1", "CS154", Style)
  2276. f.SetCellStyle(v, "D1", "CS154", Style)
  2277. f.SetCellStyle(v, "J1", "CS154", Style)
  2278. f.SetColWidth(v, "A", "N", 20)
  2279. for i1, v1 := range (columns1) {
  2280. f.SetCellValue(v, letters[i1]+"2", v1)
  2281. }
  2282. }
  2283. Engine := models.Engine
  2284. sqlnames := []string{"getChooseEqMonthBudgetListSumAll", "getChooseMonthWX", "getChooseElectricityPrice", "getChooseMonthiolPrice",
  2285. "getChooseWaterPrice", "getChooseMonthElectricity", "getChooseMonthWater", "getChooseMonthOilT"}
  2286. xx := 0
  2287. for z, sqlname := range (sqlnames) {
  2288. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", sqlname).QueryString()
  2289. parammap := make(map[string]interface{}, 0)
  2290. json.Unmarshal([]byte(parammaps), &parammap)
  2291. s_params := make([]interface{}, 0)
  2292. paramslist := strings.Split(valuesMap[0]["params"], ",")
  2293. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  2294. for _, value := range paramslist {
  2295. s_params = append(s_params, parammap[strings.Trim(value, " ")])
  2296. }
  2297. }
  2298. values, err := Engine.SQL(valuesMap[0]["sqlstr"], s_params...).Query().List()
  2299. if err != nil {
  2300. fmt.Println(err)
  2301. } else {
  2302. for i, v := range (values) {
  2303. value := []interface{}{columns[z], v["pastureName"], "全厂区", v["monthBudget"], v["monthLimit"], v["qmonthBudget"], v["sumPrice"],
  2304. v["asave"], v["realReason"], v["oneCowBudget"], v["oneCowLimit"], v["oneCowQmonthBudget"], v["oneCowSumPrice"], v["oneCowAsave"]}
  2305. for a, b := range (value) {
  2306. f.SetCellValue("设备指标", letters[a]+strconv.Itoa(i+xx+3), b)
  2307. }
  2308. }
  2309. }
  2310. xx += len(values)
  2311. }
  2312. //"维修费","水费","电费","柴油费","水量","电量","柴油用量"
  2313. // 其他的表格
  2314. sqlnamesDepartment := []string{"getChooseMonthWXDepartment", "getChooseWaterPriceDepartment", "getChooseElectricityPriceDepartment", "getChooseMonthiolPriceDepartment",
  2315. "getChooseMonthWaterDepartment", "getChooseMonthElectricityDepartment", "getChooseMonthOilTDepartment"}
  2316. columnss := []string{"维修费", "水费", "电费", "柴油费", "水量", "电量", "柴油用量"}
  2317. for z, sqlname := range (sqlnamesDepartment) {
  2318. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", sqlname).QueryString()
  2319. parammap := make(map[string]interface{}, 0)
  2320. json.Unmarshal([]byte(parammaps), &parammap)
  2321. s_params := make([]interface{}, 0)
  2322. paramslist := strings.Split(valuesMap[0]["params"], ",")
  2323. if len(paramslist) > 0 && valuesMap[0]["params"] != "" {
  2324. for _, value := range paramslist {
  2325. s_params = append(s_params, parammap[strings.Trim(value, " ")])
  2326. }
  2327. }
  2328. values, err := Engine.SQL(valuesMap[0]["sqlstr"], s_params...).Query().List()
  2329. if err != nil {
  2330. fmt.Println(err)
  2331. } else {
  2332. for i, v := range (values) {
  2333. value := []interface{}{v["sort"], v["pastureName"], v["deptname"], v["monthBudget"], v["monthLimit"], v["qmonthBudget"], v["sumPrice"],
  2334. v["asave"], v["realReason"], v["oneCowBudget"], v["oneCowLimit"], v["oneCowQmonthBudget"], v["oneCowSumPrice"], v["oneCowAsave"]}
  2335. for a, b := range (value) {
  2336. f.SetCellValue(columnss[z], letters[a]+strconv.Itoa(i+3), b)
  2337. }
  2338. }
  2339. }
  2340. }
  2341. if err != nil {
  2342. data["err_count"] = err
  2343. logging.Error("设备指标导出__error:", err)
  2344. } else {
  2345. err = PathCheck("uploads/Downloadmailreport") //检查路径并创建
  2346. //os.Remove(filename)
  2347. if err != nil {
  2348. println("PathCheck err", err)
  2349. }
  2350. if err := f.SaveAs(filename); err != nil {
  2351. println(err.Error())
  2352. return "", err
  2353. }
  2354. //data["success"] = "ok"
  2355. //context.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2356. //context.Header("Content-Type","application/octet-stream")
  2357. //context.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  2358. //context.Header("Content-Transfer-Encoding", "binary")
  2359. //_ = f.Write(context.Writer)
  2360. }
  2361. return filename, err
  2362. //appG.Response(http.StatusOK, e.SUCCESS, data)
  2363. }