Data.go 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Device"
  8. "encoding/base64"
  9. "errors"
  10. "fmt"
  11. "github.com/beego/beego/v2/core/logs"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "github.com/signintech/gopdf"
  14. "github.com/xuri/excelize/v2"
  15. "math"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. type DataController struct {
  22. beego.Controller
  23. Admin_r Account.Admin // 登陆的用户
  24. T_pid int // 公司id
  25. }
  26. func (c *DataController) Prepare() {
  27. GetCookie := c.Ctx.GetCookie("User_tokey")
  28. GetString := c.GetString("User_tokey")
  29. User_tokey := GetCookie
  30. if len(User_tokey) == 0 {
  31. User_tokey = GetString
  32. }
  33. if Account.Admin_r == nil {
  34. return
  35. }
  36. c.Admin_r = *Account.Admin_r
  37. T_pid := c.Admin_r.T_pid
  38. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  39. if EntryPid > 0 {
  40. T_pid = EntryPid
  41. }
  42. c.T_pid = T_pid
  43. }
  44. // 获取传感器
  45. func (c *DataController) Device_Sensor_Get() {
  46. T_sn := c.GetString("T_sn")
  47. T_id, _ := c.GetInt("T_id")
  48. //c.Data["Class_List"] = Device.Read_Class_All_1()
  49. DeviceSensor_r, err := Device.Read_DeviceSensor_ByTsn_Tid(T_sn, T_id)
  50. if err != nil {
  51. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取失败!"}
  52. c.ServeJSON()
  53. return
  54. }
  55. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Device.DeviceSensorToDeviceSensor_R(DeviceSensor_r)}
  56. c.ServeJSON()
  57. return
  58. }
  59. // 传感器列表
  60. func (c *DataController) Device_Sensor_List() {
  61. type R_JSONS struct {
  62. //必须的大写开头
  63. DeviceSensor_lite []Device.DeviceSensor_R
  64. Num int
  65. Page int
  66. Page_size int
  67. Pages []lib.Page_T
  68. }
  69. var r_jsons R_JSONS
  70. page, _ := c.GetInt("page")
  71. if page < 1 {
  72. page = 1
  73. }
  74. page_z, _ := c.GetInt("page_z")
  75. if page_z == 0 {
  76. page_z = conf.Page_size
  77. }
  78. T_Class_id, _ := c.GetInt("T_calssid")
  79. T_type, T_type_err := c.GetInt("T_type")
  80. if T_type_err != nil {
  81. T_type = -1
  82. }
  83. T_name := c.GetString("T_name")
  84. // 数据展示页面
  85. T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
  86. if T_RealTime_err != nil {
  87. T_RealTime = 0
  88. }
  89. // 轨迹展示页面
  90. T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
  91. if T_MapShow_err != nil {
  92. T_MapShow = 0
  93. }
  94. T_abandon, _ := c.GetInt("T_abandon")
  95. var cnt int64
  96. //Device.Read_DeviceSensorParameter_All_Map("", 0)
  97. bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
  98. if err != nil {
  99. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  100. c.ServeJSON()
  101. return
  102. }
  103. r_jsons.DeviceSensor_lite, cnt = Device.Read_DeviceSensor_List_For_Data(bindSN, c.T_pid, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, T_abandon, page, page_z)
  104. page_size := math.Ceil(float64(cnt) / float64(page_z))
  105. r_jsons.Page = page
  106. r_jsons.Page_size = int(page_size)
  107. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  108. r_jsons.Num = int(cnt)
  109. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  110. c.ServeJSON()
  111. return
  112. }
  113. func (c *DataController) Company_Device_Sensor_List() {
  114. var r_jsons lib.R_JSONS
  115. page, _ := c.GetInt("page")
  116. if page < 1 {
  117. page = 1
  118. }
  119. page_z, _ := c.GetInt("page_z")
  120. if page_z == 0 {
  121. page_z = conf.Page_size
  122. }
  123. T_Class_id, _ := c.GetInt("T_calssid")
  124. T_type, T_type_err := c.GetInt("T_type")
  125. if T_type_err != nil {
  126. T_type = -1
  127. }
  128. T_name := c.GetString("T_name")
  129. // 数据展示页面
  130. T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
  131. if T_RealTime_err != nil {
  132. T_RealTime = 0
  133. }
  134. // 轨迹展示页面
  135. T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
  136. if T_MapShow_err != nil {
  137. T_MapShow = 0
  138. }
  139. // 查询公司
  140. Company_r, err := Account.Read_Company_ById(c.T_pid)
  141. if err != nil {
  142. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. // 查询公司下面所有子公司id
  147. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  148. var cnt int64
  149. Account.Read_Company_All_Maps()
  150. r_jsons.Data, cnt = Device.Read_CompanyDeviceSensor_List_For_Data(T_pids, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, page, page_z)
  151. page_size := math.Ceil(float64(cnt) / float64(page_z))
  152. r_jsons.Page = page
  153. r_jsons.Page_size = int(page_size)
  154. r_jsons.Num = cnt
  155. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  156. c.ServeJSON()
  157. return
  158. }
  159. func (c *DataController) Company_Device_Sensor_List_ByKey() {
  160. var r_jsons lib.R_JSONS
  161. key := c.GetString("key")
  162. T_sn := c.GetString("T_sn")
  163. // 查询公司
  164. Company_r, err := Account.Read_Company_ByKey(key)
  165. if err != nil {
  166. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  167. c.ServeJSON()
  168. return
  169. }
  170. // 查询公司下面所有子公司id
  171. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  172. Account.Read_Company_All_Maps()
  173. r_jsons.Data, r_jsons.Num = Device.Read_CompanyDeviceSensor_List_For_Data_ByKey(T_pids, T_sn)
  174. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  175. c.ServeJSON()
  176. return
  177. }
  178. // 传感器数据列表
  179. func (c *DataController) Device_Sensor_Data() {
  180. page, _ := c.GetInt("page")
  181. page_z, _ := c.GetInt("page_z")
  182. T_id, _ := c.GetInt("T_id")
  183. SN := c.GetString("T_sn")
  184. Time_start := c.GetString("Time_start")
  185. Time_end := c.GetString("Time_end")
  186. type R_JSONS struct {
  187. //必须的大写开头
  188. DeviceSensor_data []Device.DeviceData_R
  189. Num int
  190. Page int
  191. Page_size int
  192. Pages []lib.Page_T
  193. }
  194. var r_jsons R_JSONS
  195. var cnt int
  196. Device.Read_DeviceSensorParameter_All_Map(SN, T_id)
  197. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(SN, T_id, Time_start, Time_end, page, page_z)
  198. page_size := math.Ceil(float64(cnt) / float64(page_z))
  199. r_jsons.Page = page
  200. r_jsons.Page_size = int(page_size)
  201. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  202. r_jsons.Num = cnt
  203. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  204. c.ServeJSON()
  205. return
  206. }
  207. // 实时轨迹
  208. func (c *DataController) Device_Sensor_Data_Real() {
  209. page, _ := c.GetInt("page")
  210. page_z, _ := c.GetInt("page_z")
  211. T_id, _ := c.GetInt("T_id")
  212. T_sn := c.GetString("T_sn")
  213. TaskList, num := Device.Read_DeviceTask_All(T_sn)
  214. if num == 0 {
  215. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂时没有轨迹信息"}
  216. c.ServeJSON()
  217. return
  218. }
  219. Time_start := TaskList[0].T_Ut_start
  220. Time_end := TaskList[0].T_Ut_end
  221. type R_JSONS struct {
  222. //必须的大写开头
  223. DeviceSensor_data []Device.DeviceData_R
  224. Num int
  225. Page int
  226. Page_size int
  227. Pages []lib.Page_T
  228. }
  229. var r_jsons R_JSONS
  230. var cnt int
  231. Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
  232. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
  233. page_size := math.Ceil(float64(cnt) / float64(page_z))
  234. r_jsons.Page = page
  235. r_jsons.Page_size = int(page_size)
  236. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  237. r_jsons.Num = cnt
  238. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  239. c.ServeJSON()
  240. return
  241. }
  242. // 设备数据
  243. func (c *DataController) Device_Sensor_Data_More() {
  244. page, _ := c.GetInt("page")
  245. if page < 1 {
  246. page = 1
  247. }
  248. page_z, _ := c.GetInt("page_z")
  249. if page_z < 1 {
  250. page_z = conf.Page_size
  251. }
  252. T_snid := c.GetString("T_snid")
  253. Time_start := c.GetString("Time_start")
  254. Time_end := c.GetString("Time_end")
  255. if len(T_snid) < 10 {
  256. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. type R_JSONS struct {
  261. //必须的大写开头
  262. Data []Device.DeviceData_R
  263. Num int64
  264. Page int
  265. Page_size int
  266. }
  267. var r_jsons R_JSONS
  268. r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, page, page_z)
  269. r_jsons.Page = page
  270. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  271. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  272. c.ServeJSON()
  273. return
  274. }
  275. // -------------------------------------
  276. // 导出传感器数据列表
  277. func (c *DataController) Device_Sensor_Data_Excel() {
  278. T_snid := c.GetString("T_snid")
  279. Time_start := c.GetString("Time_start")
  280. Time_end := c.GetString("Time_end")
  281. if len(T_snid) < 10 {
  282. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  283. c.ServeJSON()
  284. return
  285. }
  286. var DeviceSensor_data []Device.DeviceData_R
  287. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  288. // 按照传感器T_sort排序
  289. //var snids string
  290. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  291. //// 创建名称到权重的映射
  292. //orderMap := make(map[int]int)
  293. //for i, v := range dsList {
  294. // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
  295. // orderMap[v.T_id] = i
  296. //}
  297. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  298. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  299. // // 如果时间相同,则按预设顺序排序
  300. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  301. // }
  302. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  303. //})
  304. f := excelize.NewFile() // 设置单元格的值
  305. // 这里设置表头
  306. f.SetCellValue("Sheet1", "A1", "编号")
  307. f.SetCellValue("Sheet1", "B1", "传感器名称")
  308. f.SetCellValue("Sheet1", "C1", "温度℃")
  309. f.SetCellValue("Sheet1", "D1", "湿度%")
  310. f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
  311. f.SetCellValue("Sheet1", "F1", "湿度范围 %")
  312. f.SetCellValue("Sheet1", "G1", "记录时间")
  313. f.SetCellValue("Sheet1", "H1", "备注")
  314. // 设置列宽
  315. f.SetColWidth("Sheet1", "A", "A", 10)
  316. f.SetColWidth("Sheet1", "B", "B", 15)
  317. f.SetColWidth("Sheet1", "C", "D", 10)
  318. f.SetColWidth("Sheet1", "E", "F", 15)
  319. f.SetColWidth("Sheet1", "G", "G", 22)
  320. f.SetColWidth("Sheet1", "H", "H", 22)
  321. line := 1
  322. headStyleLower, _ := f.NewStyle(
  323. &excelize.Style{
  324. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  325. })
  326. headStyleUpper, _ := f.NewStyle(
  327. &excelize.Style{
  328. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  329. })
  330. // 循环写入数据
  331. for _, v := range DeviceSensor_data {
  332. line++
  333. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  334. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  335. if v.T_ist == 1 {
  336. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  337. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
  338. if (v.T_t < v.T_tl || v.T_t > v.T_tu) && v.T_free == 0 {
  339. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  340. }
  341. }
  342. if v.T_ish == 1 {
  343. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  344. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
  345. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu && v.T_free == 0 {
  346. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  347. }
  348. }
  349. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  350. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_remark)
  351. //if v.T_ist == 1 && (v.T_t < v.T_tl || v.T_t > v.T_tu) {
  352. // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  353. //}
  354. //if v.T_ish == 1 && (v.T_rh < v.T_rhl || v.T_rh > v.T_rhu) {
  355. // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  356. //}
  357. }
  358. timeStr := time.Now().Format("20060102150405")
  359. // 保存文件
  360. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  361. logs.Error(err)
  362. }
  363. // 上传 OSS
  364. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  365. if !is {
  366. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  367. c.ServeJSON()
  368. return
  369. }
  370. //删除目录
  371. err := os.Remove("ofile/" + timeStr + ".xlsx")
  372. if err != nil {
  373. logs.Error(err)
  374. }
  375. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  376. c.ServeJSON()
  377. return
  378. }
  379. // 列表 - 接口
  380. func (c *DataController) Device_Sensor_Data_Excel_m() {
  381. T_snid := c.GetString("T_snid")
  382. Time_start := c.GetString("Time_start")
  383. Time_end := c.GetString("Time_end")
  384. t_x, _ := c.GetInt("t_x")
  385. if len(T_snid) < 10 {
  386. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  387. c.ServeJSON()
  388. return
  389. }
  390. var DeviceSensor_data []Device.DeviceData_R
  391. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  392. f := excelize.NewFile() // 设置单元格的值
  393. // 这里设置表头
  394. f.SetCellValue("Sheet1", "A1", "编号")
  395. f.SetCellValue("Sheet1", "B1", "传感器名称")
  396. f.SetCellValue("Sheet1", "C1", "温度℃")
  397. f.SetCellValue("Sheet1", "D1", "湿度%")
  398. f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
  399. f.SetCellValue("Sheet1", "F1", "湿度范围 %")
  400. f.SetCellValue("Sheet1", "G1", "记录时间")
  401. // 设置列宽
  402. f.SetColWidth("Sheet1", "A", "A", 10)
  403. f.SetColWidth("Sheet1", "B", "B", 15)
  404. f.SetColWidth("Sheet1", "C", "D", 10)
  405. f.SetColWidth("Sheet1", "E", "F", 15)
  406. f.SetColWidth("Sheet1", "G", "G", 22)
  407. line := 1
  408. headStyleLower, _ := f.NewStyle(
  409. &excelize.Style{
  410. Font: &excelize.Font{Size: 11, Color: "#a8f7ff", Family: "arial"},
  411. })
  412. headStyleUpper, _ := f.NewStyle(
  413. &excelize.Style{
  414. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  415. })
  416. atime, _ := time.ParseDuration("-1m") // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
  417. // 循环写入数据
  418. for v_i, v := range DeviceSensor_data {
  419. line++
  420. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  421. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  422. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  423. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  424. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
  425. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
  426. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  427. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  428. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  429. }
  430. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  431. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  432. }
  433. if len(DeviceSensor_data)-1 == v_i {
  434. break
  435. }
  436. cha_v := ((DeviceSensor_data[v_i+1].T_t - v.T_t) / 3)
  437. println("t_x:", t_x)
  438. for i := 0; i < t_x; i++ {
  439. println("// -------", i)
  440. v.T_t = float32(lib.Decimal(float64(v.T_t + cha_v)))
  441. fTime, _ := time.Parse("2006-01-02 15:04:05", v.T_time)
  442. fTime = fTime.Add(atime)
  443. v.T_time = fTime.Format("2006-01-02 15:04:05")
  444. line++
  445. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  446. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  447. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  448. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  449. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
  450. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
  451. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  452. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  453. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  454. }
  455. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  456. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  457. }
  458. }
  459. }
  460. timeStr := time.Now().Format("20060102150405")
  461. // 保存文件
  462. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  463. logs.Error(err)
  464. }
  465. // 上传 OSS
  466. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  467. if !is {
  468. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  469. c.ServeJSON()
  470. return
  471. }
  472. //删除目录
  473. //err := os.Remove("ofile/" + timeStr + ".xlsx")
  474. //if err != nil {
  475. // logs.Error(err)
  476. //}
  477. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  478. c.ServeJSON()
  479. return
  480. }
  481. // 小程序 - 打印
  482. func (c *DataController) DeviceSensor_Data_Print() {
  483. SN := c.GetString("T_sn")
  484. Time_start := c.GetString("Time_start")
  485. Time_end := c.GetString("Time_end")
  486. type R_JSONS struct {
  487. //必须的大写开头
  488. T_time string
  489. T1 interface{}
  490. T2 interface{}
  491. T3 interface{}
  492. T4 interface{}
  493. }
  494. r_jsons := make(map[string]R_JSONS)
  495. type JSONS struct {
  496. //必须的大写开头
  497. Code int16
  498. Msg string
  499. DeviceSensorNum int
  500. DeviceSensor Device.DeviceSensor_Applet
  501. AdminNane string
  502. Data interface{} // 泛型
  503. }
  504. GROUP_BY_t_time := Device.Read_DeviceData_List_GROUP_BY_t_time(SN, Time_start, Time_end)
  505. // 时间 MAP
  506. for _, vt := range GROUP_BY_t_time {
  507. stamp, _ := time.Parse("2006-1-02 15:04:05", lib.To_string(vt[0]))
  508. T_time := stamp.Format("2006-01-02 15:04:05")
  509. r_jsons[T_time] = R_JSONS{T_time: T_time, T1: nil, T2: nil, T3: nil, T4: nil}
  510. }
  511. dsList := Device.Read_DeviceSensor_List_T_sn_T_datashow(SN)
  512. if len(dsList) == 0 {
  513. c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), Data: r_jsons}
  514. c.ServeJSON()
  515. return
  516. }
  517. company, _ := Account.Read_Company_ById(c.T_pid)
  518. for i, v := range dsList {
  519. if i >= 4 {
  520. break
  521. }
  522. // 记录传感器
  523. maps, num := Device.Read_DeviceData_ById_List(SN, v.T_id, Time_start, Time_end, 1, 9999)
  524. if num > 0 {
  525. for _, v_map := range maps {
  526. ws, ok := r_jsons[v_map.T_time]
  527. if ok {
  528. switch i {
  529. case 0:
  530. ws.T1 = v_map
  531. case 1:
  532. ws.T2 = v_map
  533. case 2:
  534. ws.T3 = v_map
  535. case 3:
  536. ws.T4 = v_map
  537. }
  538. r_jsons[v_map.T_time] = ws
  539. }
  540. }
  541. }
  542. }
  543. c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), DeviceSensor: Device.DeviceSensorToDeviceSensor_Applet(dsList[0]), AdminNane: company.T_name, Data: r_jsons}
  544. c.ServeJSON()
  545. return
  546. }
  547. // 列表 - 接口
  548. func (c *DataController) Device_Sensor_Data_PDF() {
  549. T_snid := c.GetString("T_snid")
  550. Time_start := c.GetString("Time_start")
  551. Time_end := c.GetString("Time_end")
  552. if len(T_snid) < 10 {
  553. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  554. c.ServeJSON()
  555. return
  556. }
  557. //T_ist // 温度 1开启 2关闭
  558. //T_ish // 湿度 1开启 2关闭
  559. T_ist_list := make([]int, 0)
  560. T_ish_list := make([]int, 0)
  561. T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|")
  562. for _, v := range T_snid_list {
  563. sn_id := strings.Split(v, ",")
  564. if len(sn_id) == 2 {
  565. device, _ := Device.Read_Device_ByT_sn(sn_id[0])
  566. T_ist_list = append(T_ist_list, device.T_ist)
  567. T_ish_list = append(T_ish_list, device.T_ish)
  568. }
  569. }
  570. istHasOne := false
  571. istHasTwo := false
  572. T_ist := 1
  573. for _, v := range T_ist_list {
  574. if v == 1 {
  575. istHasOne = true
  576. }
  577. if v == 2 {
  578. istHasTwo = true
  579. }
  580. }
  581. // 全部都为2 则不显示温度
  582. if !istHasOne && istHasTwo {
  583. T_ist = 2
  584. }
  585. ishHasOne := false
  586. ishHasTwo := false
  587. T_ish := 1
  588. for _, v := range T_ish_list {
  589. if v == 1 {
  590. ishHasOne = true
  591. }
  592. if v == 2 {
  593. ishHasTwo = true
  594. }
  595. }
  596. // 全部都为2 则不显示湿度
  597. if !ishHasOne && ishHasTwo {
  598. T_ish = 2
  599. }
  600. var DeviceSensor_data []Device.DeviceData_R
  601. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  602. // 按照传感器T_sort排序
  603. //var snids string
  604. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  605. //// 创建名称到权重的映射
  606. //orderMap := make(map[int]int)
  607. //for i, v := range dsList {
  608. // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
  609. // orderMap[v.T_id] = i
  610. //}
  611. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  612. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  613. // // 如果时间相同,则按预设顺序排序
  614. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  615. // }
  616. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  617. //})
  618. // -------------------获取最高温湿度、最低温湿度、平均温湿度
  619. // 最高温度、最低温度
  620. var maxTemp, minTemp, maxRH, minRH float32
  621. // 最高温度时间、最低温度时间
  622. var maxTempTime, minTempTime, maxRHTime, minRHTime string
  623. // 总温度
  624. var totalTemp, totalRH float32
  625. // 平均温度
  626. var avgTemp, avgRH float32
  627. var TempCount, RHCount int
  628. if len(DeviceSensor_data) > 0 {
  629. // 最高温度及时刻
  630. firstTempFlag := false
  631. //var maxTemp,minTemp float32
  632. //var maxTempTime,minTempTime string
  633. //maxTemp = DeviceSensor_data[0].T_t
  634. //maxTempTime = DeviceSensor_data[0].T_time
  635. //// 最低温度及时刻
  636. //minTemp = DeviceSensor_data[0].T_t
  637. //minTempTime = DeviceSensor_data[0].T_time
  638. // 最高湿度度及时刻
  639. firstRHFlag := false
  640. //maxRH = DeviceSensor_data[0].T_rh
  641. //maxRHTime = DeviceSensor_data[0].T_time
  642. //// 最低湿度及时刻
  643. //minRH = DeviceSensor_data[0].T_rh
  644. //minRHTime = DeviceSensor_data[0].T_time
  645. for _, data := range DeviceSensor_data {
  646. if data.T_ist == 1 {
  647. if firstTempFlag {
  648. if data.T_t > maxTemp {
  649. maxTemp = data.T_t
  650. maxTempTime = data.T_time
  651. }
  652. if data.T_t < minTemp {
  653. minTemp = data.T_t
  654. minTempTime = data.T_time
  655. }
  656. totalTemp += data.T_t
  657. TempCount++
  658. } else {
  659. maxTemp = data.T_t
  660. maxTempTime = data.T_time
  661. // 最低温度及时刻
  662. minTemp = data.T_t
  663. minTempTime = data.T_time
  664. firstTempFlag = true
  665. totalTemp += data.T_t
  666. TempCount = 1
  667. }
  668. }
  669. if data.T_ish == 1 {
  670. if firstRHFlag {
  671. if data.T_rh > maxRH {
  672. maxRH = data.T_rh
  673. maxRHTime = data.T_time
  674. }
  675. if data.T_rh < minRH {
  676. minRH = data.T_rh
  677. minRHTime = data.T_time
  678. }
  679. totalRH += data.T_rh
  680. RHCount++
  681. } else {
  682. maxRH = data.T_rh
  683. maxRHTime = data.T_time
  684. // 最低温度及时刻
  685. minRH = data.T_rh
  686. minRHTime = data.T_time
  687. firstRHFlag = true
  688. totalRH += data.T_rh
  689. RHCount = 1
  690. }
  691. }
  692. }
  693. if TempCount > 0 {
  694. // 平均温度
  695. avgTemp = totalTemp / float32(TempCount)
  696. }
  697. // 平均湿度
  698. if RHCount > 0 {
  699. avgRH = totalRH / float32(RHCount)
  700. }
  701. }
  702. // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
  703. maxTempTime = maxTempTime
  704. minTempTime = minTempTime
  705. maxRHTime = maxRHTime
  706. minRHTime = minRHTime
  707. var err error
  708. pdf := &gopdf.GoPdf{}
  709. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  710. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  711. //if err != nil {
  712. // log.Fatalln(err)
  713. //}
  714. //err = pdf.SetFont("Ubuntu-L", "", 14)
  715. //if err != nil {
  716. // log.Fatalln(err)
  717. //}
  718. err = pdf.AddTTFFont("simsun", "static/fonts/MiSans-Medium.ttf")
  719. if err != nil {
  720. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  721. c.ServeJSON()
  722. return
  723. }
  724. err = pdf.SetFont("simsun", "", 24)
  725. if err != nil {
  726. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  727. c.ServeJSON()
  728. return
  729. }
  730. pdf.SetGrayFill(0.5)
  731. pdf.SetMargins(0, 20, 0, 20)
  732. pdf.AddPage()
  733. company, _ := Account.Read_Company_ById(c.T_pid)
  734. textw, _ := pdf.MeasureTextWidth(company.T_name)
  735. pdf.SetX((595 / 2) - (textw / 2))
  736. pdf.SetY(40)
  737. pdf.Text(company.T_name)
  738. // 线
  739. pdf.SetLineWidth(2)
  740. pdf.SetLineType("dashed")
  741. pdf.Line(10, 60, 585, 60)
  742. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  743. if err != nil {
  744. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  745. c.ServeJSON()
  746. return
  747. }
  748. err = pdf.SetFont("wts", "", 12)
  749. if err != nil {
  750. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  751. c.ServeJSON()
  752. return
  753. }
  754. var s_time, e_time string
  755. if len(DeviceSensor_data) > 0 {
  756. s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
  757. e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
  758. }
  759. lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  760. var y float64 = 120
  761. if T_ist == 1 {
  762. lib.RectFillColor(pdf, fmt.Sprintf("最高温度: %.1f ", maxTemp), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  763. lib.RectFillColor(pdf, fmt.Sprintf("最低温度: %.1f ", minTemp), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  764. lib.RectFillColor(pdf, fmt.Sprintf("平均温度: %.1f ", avgTemp), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  765. y += 20
  766. }
  767. if T_ish == 1 {
  768. lib.RectFillColor(pdf, fmt.Sprintf("最高湿度: %.1f ", maxRH), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  769. lib.RectFillColor(pdf, fmt.Sprintf("最低湿度: %.1f ", minRH), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  770. lib.RectFillColor(pdf, fmt.Sprintf("平均湿度: %.1f ", avgRH), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  771. y += 20
  772. }
  773. y += 5
  774. lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  775. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  776. lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  777. lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  778. lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  779. lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  780. lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  781. lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  782. y += 20
  783. err = pdf.SetFont("wts", "", 10)
  784. if err != nil {
  785. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  786. c.ServeJSON()
  787. return
  788. }
  789. for i, v := range DeviceSensor_data {
  790. text := fmt.Sprintf(" %d ", i+1)
  791. var textH float64 = 25 // if text height is 25px.
  792. pdf.SetNewY(y, textH)
  793. y = pdf.GetY()
  794. // newY = 20
  795. if y < 30 {
  796. lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  797. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  798. lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  799. lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  800. lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  801. lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  802. lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  803. lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  804. y += 20
  805. }
  806. //pdf.SetX(x) // must after pdf.SetNewY() called.
  807. //err = pdf.Text(text)
  808. //if err != nil {
  809. // log.Fatalln(err)
  810. //}
  811. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  812. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  813. T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
  814. T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
  815. T_time := fmt.Sprintf("%s", v.T_time)
  816. if v.T_ist == 1 {
  817. lib.RectFillColor(pdf, T_t, 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  818. lib.RectFillColor(pdf, T_Tlu, 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  819. } else {
  820. lib.RectFillColor(pdf, "", 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  821. lib.RectFillColor(pdf, "", 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  822. }
  823. if v.T_ish == 1 {
  824. lib.RectFillColor(pdf, T_rh, 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  825. lib.RectFillColor(pdf, T_Rlu, 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  826. } else {
  827. lib.RectFillColor(pdf, "", 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  828. lib.RectFillColor(pdf, "", 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  829. }
  830. lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  831. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  832. lib.RectFillColor(pdf, T_time, 10, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  833. lib.RectFillColor(pdf, v.T_remark, 10, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  834. y += 20
  835. }
  836. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  837. err = pdf.WritePdf(timeStr)
  838. if err != nil {
  839. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  840. c.ServeJSON()
  841. return
  842. }
  843. // 上传 OSS
  844. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  845. if !is {
  846. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  847. c.ServeJSON()
  848. return
  849. }
  850. //删除目录
  851. err = os.Remove(timeStr)
  852. if err != nil {
  853. logs.Error(err)
  854. }
  855. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  856. c.ServeJSON()
  857. return
  858. }
  859. func (c *DataController) Device_Sensor_Data_ChartShow_PDF() {
  860. T_snid := c.GetString("T_snid")
  861. Time_start := c.GetString("Time_start")
  862. Time_end := c.GetString("Time_end")
  863. T_forwarding_unit := c.GetString("T_forwarding_unit")
  864. T_consignee_unit := c.GetString("T_consignee_unit")
  865. T_remark := c.GetString("T_remark")
  866. T_temp_show, _ := c.GetInt("T_temp_show")
  867. T_Humidity_show, _ := c.GetInt("T_Humidity_show")
  868. T_template := c.GetString("T_template")
  869. if T_temp_show == 0 && T_Humidity_show == 0 {
  870. c.Data["json"] = lib.JSONS{Code: 202, Msg: "温湿度不能同时不显示"}
  871. c.ServeJSON()
  872. return
  873. }
  874. if len(T_snid) < 10 {
  875. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  876. c.ServeJSON()
  877. return
  878. }
  879. // 获取图片信息
  880. base64String := c.GetString("file")
  881. imageBytes, err := base64.StdEncoding.DecodeString(base64String)
  882. if err != nil {
  883. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解析base64失败"}
  884. c.ServeJSON()
  885. return
  886. }
  887. company, _ := Account.Read_Company_ById(c.T_pid)
  888. if T_template == "1" {
  889. url, err := Device_Sensor_Data_ChartShow_PDF1(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
  890. if err != nil {
  891. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  892. c.ServeJSON()
  893. return
  894. }
  895. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  896. c.ServeJSON()
  897. return
  898. }
  899. url, err := Device_Sensor_Data_ChartShow_PDF0(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
  900. if err != nil {
  901. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  902. c.ServeJSON()
  903. return
  904. }
  905. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  906. c.ServeJSON()
  907. return
  908. }
  909. // 图表展示 下载PDF 箱/车
  910. func Device_Sensor_Data_ChartShow_PDF1(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
  911. T_temp_show, T_Humidity_show int) (url string, err error) {
  912. snidNum := strings.Split(strings.Trim(T_snid, "|"), "|")
  913. if len(snidNum) > 2 {
  914. err = errors.New("车/箱最多只能选择2个探头!")
  915. return
  916. }
  917. var sn1, sn2 string
  918. var id1, id2 int
  919. var r_maps_sn1, r_maps_sn2 []Device.DeviceData_R
  920. var DeviceSensor_data []Device.DeviceData_R
  921. var Pdf_data []Device.DeviceData_Pdf
  922. if len(snidNum) == 1 {
  923. sn_id := strings.Split(snidNum[0], ",")
  924. sn1, id1 = sn_id[0], lib.To_int(sn_id[1])
  925. r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
  926. DeviceSensor_data = r_maps_sn1
  927. Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1}, Time_start, Time_end)
  928. }
  929. if len(snidNum) == 2 {
  930. sn_id1 := strings.Split(snidNum[0], ",")
  931. sn_id2 := strings.Split(snidNum[1], ",")
  932. sn1, id1 = sn_id1[0], lib.To_int(sn_id1[1])
  933. sn2, id2 = sn_id2[0], lib.To_int(sn_id2[1])
  934. if sn1 != sn2 {
  935. err = errors.New("请选择相同设备编号的探头导出!")
  936. return
  937. }
  938. r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
  939. r_maps_sn2, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn2, id2, Time_start, Time_end, 0, 9999)
  940. DeviceSensor_data = append(DeviceSensor_data, r_maps_sn1...)
  941. DeviceSensor_data = append(DeviceSensor_data, r_maps_sn2...)
  942. Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1, id2}, Time_start, Time_end)
  943. }
  944. device, err := Device.Read_Device_ByT_sn(sn1)
  945. if err != nil {
  946. err = errors.New("查询主机信息失败!")
  947. return
  948. }
  949. // -------------------获取最高温湿度、最低温湿度、平均温湿度
  950. // 最高温度、最低温度
  951. var maxTemp, minTemp float32
  952. // 最高温度时间、最低温度时间
  953. var maxTempTime, minTempTime string
  954. // 总温度
  955. var totalTemp float32
  956. // 平均温度
  957. var avgTemp float32
  958. if len(DeviceSensor_data) > 0 {
  959. // 最高温度及时刻
  960. maxTemp = DeviceSensor_data[0].T_t
  961. maxTempTime = DeviceSensor_data[0].T_time
  962. // 最低温度及时刻
  963. minTemp = DeviceSensor_data[0].T_t
  964. minTempTime = DeviceSensor_data[0].T_time
  965. for _, data := range DeviceSensor_data {
  966. if data.T_t > maxTemp {
  967. maxTemp = data.T_t
  968. maxTempTime = data.T_time
  969. }
  970. if data.T_t < minTemp {
  971. minTemp = data.T_t
  972. minTempTime = data.T_time
  973. }
  974. totalTemp += data.T_t
  975. }
  976. // 平均温度
  977. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  978. // 平均湿度
  979. //avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  980. }
  981. // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
  982. pdf := &gopdf.GoPdf{}
  983. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  984. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  985. if err != nil {
  986. return
  987. }
  988. err = pdf.SetFont("wts", "", 20)
  989. if err != nil {
  990. return
  991. }
  992. pdf.SetGrayFill(0.5)
  993. pdf.SetMargins(0, 20, 0, 20)
  994. pdf.AddPage()
  995. title := companyName + "冷链记录"
  996. var y float64 = 40
  997. textw, _ := pdf.MeasureTextWidth(title)
  998. pdf.SetX((595 / 2) - (textw / 2))
  999. pdf.SetY(y)
  1000. pdf.Text(title)
  1001. y += 30
  1002. pdf.SetFont("wts", "", 16)
  1003. pdf.SetXY(10, y)
  1004. pdf.Text("实施设备信息")
  1005. // 线
  1006. y += 10
  1007. pdf.SetLineWidth(0.5)
  1008. pdf.SetStrokeColor(169, 169, 169)
  1009. pdf.Line(10, y, 585, y)
  1010. pdf.SetFont("wts", "", 10)
  1011. y += 20
  1012. pdf.SetXY(10, y)
  1013. pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1014. pdf.SetXY(300, y)
  1015. pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1016. // 0 仓库 1 车厢
  1017. y += 20
  1018. ds1, _ := Device.Read_DeviceSensor_ByT_sn(sn1, id1)
  1019. pdf.SetXY(10, y)
  1020. pdf.Text(fmt.Sprintf("测点信息T1:%s", ds1.T_name))
  1021. if len(snidNum) > 1 {
  1022. ds2, _ := Device.Read_DeviceSensor_ByT_sn(sn2, id2)
  1023. pdf.SetXY(300, y)
  1024. pdf.Text(fmt.Sprintf("测点信息T2:%s", ds2.T_name))
  1025. }
  1026. y += 35
  1027. pdf.SetFont("wts", "", 16)
  1028. pdf.SetXY(10, y)
  1029. pdf.Text("记录概要信息")
  1030. // 线
  1031. y += 10
  1032. pdf.SetLineWidth(0.5)
  1033. pdf.SetStrokeColor(169, 169, 169)
  1034. pdf.Line(10, y, 585, y)
  1035. y += 20
  1036. var s_time, e_time string
  1037. if len(r_maps_sn1) > 0 {
  1038. s_time = fmt.Sprintf("%s", Pdf_data[0].T_time)
  1039. e_time = fmt.Sprintf("%s", Pdf_data[len(Pdf_data)-1].T_time)
  1040. }
  1041. pdf.SetFont("wts", "", 10)
  1042. pdf.SetXY(10, y)
  1043. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1044. pdf.SetXY(240, y)
  1045. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1046. sTime, _ := lib.TimeStrToTime(s_time)
  1047. eTime, _ := lib.TimeStrToTime(e_time)
  1048. pdf.SetXY(470, y)
  1049. minutes := int(eTime.Sub(sTime).Minutes())
  1050. hours := minutes / 60
  1051. remainingMinutes := minutes % 60
  1052. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1053. if T_temp_show == 1 {
  1054. y += 15
  1055. pdf.SetXY(10, y)
  1056. pdf.Text(fmt.Sprintf("最高温度:%s【%.1f℃】", maxTempTime, lib.RoundToDecimal(float64(maxTemp), 1)))
  1057. pdf.SetXY(240, y)
  1058. pdf.Text(fmt.Sprintf("最低温度:%s【%.1f℃】", minTempTime, lib.RoundToDecimal(float64(minTemp), 1)))
  1059. pdf.SetXY(470, y)
  1060. pdf.Text(fmt.Sprintf("平均温度:%.2f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1061. }
  1062. y += 15
  1063. pdf.SetXY(10, y)
  1064. var start_temp, end_temp string
  1065. if len(r_maps_sn1) > 0 {
  1066. start_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[0].T_t)
  1067. end_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[len(r_maps_sn1)-1].T_t)
  1068. }
  1069. if len(r_maps_sn2) > 0 {
  1070. start_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[0].T_t)
  1071. end_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[len(r_maps_sn2)-1].T_t)
  1072. }
  1073. pdf.Text(fmt.Sprintf("启运温度:%s", start_temp))
  1074. pdf.SetXY(240, y)
  1075. pdf.Text(fmt.Sprintf("到达温度:%s", end_temp))
  1076. var temp_range string
  1077. if len(DeviceSensor_data) > 0 {
  1078. temp_range = fmt.Sprintf("%.1f ~ %.1f℃", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1079. }
  1080. pdf.SetXY(470, y)
  1081. pdf.Text(fmt.Sprintf("温度范围:%s", temp_range))
  1082. y += 15
  1083. pdf.SetXY(10, y)
  1084. T_forwarding_unit_temp := []rune(T_forwarding_unit)
  1085. if len(T_forwarding_unit_temp) > 34 {
  1086. err = errors.New("发货单位长度超过限制(34个字符)")
  1087. return
  1088. }
  1089. if len(T_forwarding_unit_temp) > 17 {
  1090. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
  1091. pdf.SetXY(60, y+15)
  1092. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1093. } else {
  1094. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
  1095. }
  1096. pdf.SetXY(240, y)
  1097. T_consignee_unit_temp := []rune(T_consignee_unit)
  1098. if len(T_consignee_unit_temp) > 34 {
  1099. err = errors.New("收货单位长度超过限制(34个字符)")
  1100. return
  1101. }
  1102. if len(T_consignee_unit_temp) > 17 {
  1103. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
  1104. pdf.SetXY(290, y+15)
  1105. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1106. } else {
  1107. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
  1108. }
  1109. pdf.SetXY(470, y)
  1110. T_remark_temp := []rune(T_remark)
  1111. if len(T_remark_temp) > 34 {
  1112. err = errors.New("备注长度超过限制(16个字符)")
  1113. return
  1114. }
  1115. if len(T_remark) > 8 {
  1116. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
  1117. pdf.SetXY(500, y+15)
  1118. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
  1119. } else {
  1120. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1121. }
  1122. y += 35
  1123. pdf.SetFont("wts", "", 16)
  1124. pdf.SetXY(10, y)
  1125. pdf.Text("记录曲线信息")
  1126. // 线
  1127. y += 10
  1128. pdf.SetLineWidth(0.5)
  1129. pdf.SetStrokeColor(169, 169, 169)
  1130. pdf.Line(10, y, 585, y)
  1131. y += 1
  1132. // ----------------绘制图片
  1133. //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
  1134. imgH, err := gopdf.ImageHolderByBytes(imageBytes)
  1135. if T_temp_show == 1 && T_Humidity_show == 1 {
  1136. //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
  1137. pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
  1138. y += 562
  1139. } else {
  1140. pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1141. y += 315
  1142. }
  1143. y += 35
  1144. if y > 841.89 {
  1145. // 图片结束直接分页
  1146. pdf.AddPage()
  1147. y = 40
  1148. }
  1149. pdf.SetFont("wts", "", 16)
  1150. pdf.SetXY(10, y)
  1151. pdf.Text("记录数据信息")
  1152. // 线
  1153. y += 10
  1154. pdf.SetLineWidth(0.5)
  1155. pdf.SetStrokeColor(169, 169, 169)
  1156. pdf.Line(10, y, 585, y)
  1157. y += 10
  1158. pdf.SetFont("wts", "", 10)
  1159. if len(snidNum) == 1 {
  1160. var x float64 = 19
  1161. var w float64 = 112
  1162. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1163. x = x + w
  1164. w = 74
  1165. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1166. x = x + w
  1167. w = 110
  1168. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1169. x = x + w
  1170. w = 74
  1171. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1172. x = x + w
  1173. w = 110
  1174. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1175. x = x + w
  1176. w = 74
  1177. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1178. for i, v := range Pdf_data {
  1179. if i%3 == 0 {
  1180. y += 20
  1181. x, w = 19, 112
  1182. var textH float64 = 25 // if text height is 25px.
  1183. pdf.SetNewY(y, textH)
  1184. y = pdf.GetY()
  1185. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1186. x = x + w
  1187. w = 74
  1188. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1189. }
  1190. if i%3 == 1 {
  1191. x = x + w
  1192. w = 110
  1193. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1194. x = x + w
  1195. w = 74
  1196. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1197. }
  1198. if i%3 == 2 {
  1199. x = x + w
  1200. w = 110
  1201. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1202. x = x + w
  1203. w = 74
  1204. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1205. }
  1206. }
  1207. }
  1208. if len(snidNum) == 2 {
  1209. var x float64 = 19
  1210. var w float64 = 112
  1211. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1212. x = x + w
  1213. w = 37
  1214. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1215. x = x + w
  1216. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1217. x = x + w
  1218. w = 112
  1219. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1220. x = x + w
  1221. w = 37
  1222. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1223. x = x + w
  1224. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1225. x = x + w
  1226. w = 112
  1227. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1228. x = x + w
  1229. w = 37
  1230. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1231. x = x + w
  1232. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1233. for i, v := range Pdf_data {
  1234. if i%3 == 0 {
  1235. y += 20
  1236. var textH float64 = 25 // if text height is 25px.
  1237. pdf.SetNewY(y, textH)
  1238. y = pdf.GetY()
  1239. x, w = 19, 112
  1240. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1241. x = x + w
  1242. w = 37
  1243. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1244. x = x + w
  1245. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1246. }
  1247. if i%3 == 1 {
  1248. x = x + w
  1249. w = 112
  1250. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1251. x = x + w
  1252. w = 37
  1253. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1254. x = x + w
  1255. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1256. }
  1257. if i%3 == 2 {
  1258. x = x + w
  1259. w = 112
  1260. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1261. x = x + w
  1262. w = 37
  1263. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1264. x = x + w
  1265. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1266. }
  1267. }
  1268. }
  1269. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  1270. err = pdf.WritePdf(timeStr)
  1271. if err != nil {
  1272. return
  1273. }
  1274. // 上传 OSS
  1275. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  1276. if !is {
  1277. err = errors.New("oss!")
  1278. return
  1279. }
  1280. //删除目录
  1281. err = os.Remove(timeStr)
  1282. if err != nil {
  1283. logs.Error(err)
  1284. }
  1285. return url, nil
  1286. }
  1287. // 图表展示 下载PDF 库房
  1288. func Device_Sensor_Data_ChartShow_PDF0(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
  1289. T_temp_show, T_Humidity_show int) (url string, err error) {
  1290. sn, ids, isSameSn := lib.IsSNAllSame(T_snid)
  1291. if !isSameSn {
  1292. err = errors.New("请选择相同设备编号的探头导出!")
  1293. return
  1294. }
  1295. device, err := Device.Read_Device_ByT_sn(sn)
  1296. if err != nil {
  1297. err = errors.New("查询主机信息失败!")
  1298. return
  1299. }
  1300. var DeviceSensor_data []Device.DeviceData_R
  1301. Device.Read_DeviceSensorParameter_All_Map(sn, 0)
  1302. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度
  1303. //DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  1304. DeviceSensor_data, _ = Device.Read_DeviceData_ByIds(sn, ids, Time_start, Time_end, 0, 9999)
  1305. // 根据传感器T_sort排序
  1306. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  1307. //// 创建名称到权重的映射
  1308. //orderMap := make(map[int]int)
  1309. //for i, v := range dsList {
  1310. // orderMap[v.T_id] = i
  1311. //}
  1312. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  1313. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  1314. // // 如果时间相同,则按预设顺序排序
  1315. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  1316. // }
  1317. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  1318. //})
  1319. // 最高温度、最低温度、最高湿度、最低湿度
  1320. var maxTemp, minTemp, maxHumidity, minHumidity float32
  1321. // 最高温度时间、最低温度时间、最高湿度时间、最低湿度时间
  1322. var maxTempTime, minTempTime, maxHumidityTime, minHumidityTime string
  1323. // 总温度 总湿度
  1324. var totalTemp, totalHumidity float32
  1325. // 平均温度 平均湿度
  1326. var avgTemp, avgHumidity float32
  1327. // 温度阈值,湿度阈值
  1328. var tempThreshold, humidityThreshold string
  1329. // 记录开始时间,记录结束时间
  1330. var s_time, e_time string
  1331. var lastTime string
  1332. var isFirst, isSecond = true, false
  1333. var first_column, second_column []Device.DeviceData_R
  1334. if len(DeviceSensor_data) > 0 {
  1335. tempThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1336. humidityThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_rhl, DeviceSensor_data[0].T_rhu)
  1337. e_time = DeviceSensor_data[0].T_time
  1338. s_time = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  1339. // 最高温度及时刻
  1340. maxTemp = DeviceSensor_data[0].T_t
  1341. maxTempTime = DeviceSensor_data[0].T_time
  1342. // 最低温度及时刻
  1343. minTemp = DeviceSensor_data[0].T_t
  1344. minTempTime = DeviceSensor_data[0].T_time
  1345. // 最高湿度及时刻
  1346. maxHumidity = DeviceSensor_data[0].T_rh
  1347. maxHumidityTime = DeviceSensor_data[0].T_time
  1348. // 获取最低湿度及时刻
  1349. minHumidity = DeviceSensor_data[0].T_rh
  1350. minHumidityTime = DeviceSensor_data[0].T_time
  1351. for i := 0; i < len(DeviceSensor_data); i++ {
  1352. data := DeviceSensor_data[i]
  1353. if data.T_t > maxTemp {
  1354. maxTemp = data.T_t
  1355. maxTempTime = data.T_time
  1356. }
  1357. if data.T_t < minTemp {
  1358. minTemp = data.T_t
  1359. minTempTime = data.T_time
  1360. }
  1361. totalTemp += data.T_t
  1362. if data.T_rh > maxHumidity {
  1363. maxHumidity = data.T_rh
  1364. maxHumidityTime = data.T_time
  1365. }
  1366. if data.T_rh < minHumidity {
  1367. minHumidity = data.T_rh
  1368. minHumidityTime = data.T_time
  1369. }
  1370. totalHumidity += data.T_rh
  1371. if len(lastTime) > 0 {
  1372. if lastTime != data.T_time && isFirst == true {
  1373. isFirst = false
  1374. isSecond = true
  1375. } else if lastTime != data.T_time && isSecond == true {
  1376. isFirst = true
  1377. isSecond = false
  1378. }
  1379. }
  1380. if isFirst {
  1381. first_column = append(first_column, data)
  1382. lastTime = data.T_time
  1383. }
  1384. if isSecond {
  1385. second_column = append(second_column, data)
  1386. lastTime = data.T_time
  1387. }
  1388. }
  1389. // 平均温度
  1390. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  1391. // 平均湿度
  1392. avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  1393. }
  1394. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度结束
  1395. pdf := &gopdf.GoPdf{}
  1396. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1397. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1398. if err != nil {
  1399. return
  1400. }
  1401. err = pdf.SetFont("wts", "", 20)
  1402. if err != nil {
  1403. return
  1404. }
  1405. pdf.SetGrayFill(0.5)
  1406. pdf.SetMargins(0, 20, 0, 20)
  1407. pdf.AddPage()
  1408. title := companyName + "冷链记录"
  1409. var y float64 = 40
  1410. textw, _ := pdf.MeasureTextWidth(title)
  1411. pdf.SetX((595 / 2) - (textw / 2))
  1412. pdf.SetY(y)
  1413. pdf.Text(title)
  1414. y += 30
  1415. pdf.SetFont("wts", "", 16)
  1416. pdf.SetXY(10, y)
  1417. pdf.Text("实施设备信息")
  1418. // 线
  1419. y += 10
  1420. pdf.SetLineWidth(0.5)
  1421. pdf.SetStrokeColor(169, 169, 169)
  1422. pdf.Line(10, y, 585, y)
  1423. pdf.SetFont("wts", "", 10)
  1424. y += 20
  1425. pdf.SetXY(10, y)
  1426. pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1427. pdf.SetXY(300, y)
  1428. pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1429. y += 35
  1430. pdf.SetFont("wts", "", 16)
  1431. pdf.SetXY(10, y)
  1432. pdf.Text("记录概要信息")
  1433. // 线
  1434. y += 10
  1435. pdf.SetLineWidth(0.5)
  1436. pdf.SetStrokeColor(169, 169, 169)
  1437. pdf.Line(10, y, 585, y)
  1438. y += 20
  1439. pdf.SetFont("wts", "", 10)
  1440. pdf.SetXY(10, y)
  1441. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1442. pdf.SetXY(240, y)
  1443. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1444. sTime, _ := lib.TimeStrToTime(s_time)
  1445. eTime, _ := lib.TimeStrToTime(e_time)
  1446. pdf.SetXY(470, y)
  1447. minutes := int(eTime.Sub(sTime).Minutes())
  1448. hours := minutes / 60
  1449. remainingMinutes := minutes % 60
  1450. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1451. // -------------最高温/湿度 最低温/湿度 平均温/湿度
  1452. if T_temp_show == 1 {
  1453. y += 15
  1454. pdf.SetXY(10, y)
  1455. pdf.Text(fmt.Sprintf("最高温度:%.1f℃,%s", lib.RoundToDecimal(float64(maxTemp), 1), maxTempTime))
  1456. pdf.SetXY(240, y)
  1457. pdf.Text(fmt.Sprintf("最低温度:%.1f℃,%s", lib.RoundToDecimal(float64(minTemp), 1), minTempTime))
  1458. pdf.SetXY(470, y)
  1459. pdf.Text(fmt.Sprintf("平均温度:%.1f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1460. }
  1461. if T_Humidity_show == 1 {
  1462. y += 15
  1463. pdf.SetXY(10, y)
  1464. pdf.Text(fmt.Sprintf("最高湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(maxHumidity), 1), maxHumidityTime))
  1465. pdf.SetXY(240, y)
  1466. pdf.Text(fmt.Sprintf("最低湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(minHumidity), 1), minHumidityTime))
  1467. pdf.SetXY(470, y)
  1468. pdf.Text(fmt.Sprintf("平均湿度:%.1f%%RH", lib.RoundToDecimal(float64(avgHumidity), 1)))
  1469. }
  1470. // -------------温/湿度阈值
  1471. if T_temp_show == 1 {
  1472. y += 15
  1473. pdf.SetXY(10, y)
  1474. pdf.Text(fmt.Sprintf("温度阈值:%s℃", tempThreshold))
  1475. }
  1476. if T_Humidity_show == 1 {
  1477. if T_temp_show != 1 {
  1478. y += 15
  1479. }
  1480. pdf.SetXY(240, y)
  1481. pdf.Text(fmt.Sprintf("湿度阈值:%s%%", humidityThreshold))
  1482. }
  1483. //-------------发货单位,收货单位,备注
  1484. y += 15
  1485. pdf.SetXY(10, y)
  1486. T_forwarding_unit_temp := []rune(T_forwarding_unit)
  1487. if len(T_forwarding_unit_temp) > 34 {
  1488. err = errors.New("发货单位长度超过限制(34个字符)")
  1489. return
  1490. }
  1491. if len(T_forwarding_unit_temp) > 17 {
  1492. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
  1493. pdf.SetXY(60, y+15)
  1494. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1495. } else {
  1496. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
  1497. }
  1498. pdf.SetXY(240, y)
  1499. T_consignee_unit_temp := []rune(T_consignee_unit)
  1500. if len(T_consignee_unit_temp) > 34 {
  1501. err = errors.New("收货单位长度超过限制(34个字符)")
  1502. return
  1503. }
  1504. if len(T_consignee_unit_temp) > 17 {
  1505. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
  1506. pdf.SetXY(290, y+15)
  1507. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1508. } else {
  1509. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
  1510. }
  1511. pdf.SetXY(470, y)
  1512. T_remark_temp := []rune(T_remark)
  1513. if len(T_remark_temp) > 34 {
  1514. err = errors.New("备注长度超过限制(16个字符)")
  1515. return
  1516. }
  1517. if len(T_remark) > 8 {
  1518. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
  1519. pdf.SetXY(500, y+15)
  1520. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
  1521. } else {
  1522. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1523. }
  1524. y += 35
  1525. pdf.SetFont("wts", "", 16)
  1526. pdf.SetXY(10, y)
  1527. pdf.Text("记录曲线信息")
  1528. // 线
  1529. y += 10
  1530. pdf.SetLineWidth(0.5)
  1531. pdf.SetStrokeColor(169, 169, 169)
  1532. pdf.Line(10, y, 585, y)
  1533. y += 1
  1534. // ----------------绘制图片
  1535. //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
  1536. imgH, err := gopdf.ImageHolderByBytes(imageBytes)
  1537. if T_temp_show == 1 && T_Humidity_show == 1 {
  1538. //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
  1539. pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
  1540. y += 562
  1541. } else {
  1542. pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1543. y += 315
  1544. }
  1545. y += 35
  1546. if y > 841.89 {
  1547. // 图片结束直接分页
  1548. pdf.AddPage()
  1549. y = 40
  1550. }
  1551. pdf.SetFont("wts", "", 16)
  1552. pdf.SetXY(10, y)
  1553. pdf.Text("记录数据信息")
  1554. // 线
  1555. y += 10
  1556. pdf.SetLineWidth(0.5)
  1557. pdf.SetStrokeColor(169, 169, 169)
  1558. pdf.Line(10, y, 585, y)
  1559. y += 10
  1560. pdf.SetFont("wts", "", 10)
  1561. var x float64 = 10
  1562. var w float64 = 112
  1563. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1564. x = x + w
  1565. w = 101
  1566. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1567. if T_temp_show == 1 && T_Humidity_show == 1 {
  1568. x = x + w
  1569. w = 37
  1570. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1571. x = x + w
  1572. w = 37
  1573. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1574. }
  1575. if T_temp_show == 1 && T_Humidity_show == 0 {
  1576. x = x + w
  1577. w = 74
  1578. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1579. }
  1580. if T_temp_show == 0 && T_Humidity_show == 1 {
  1581. x = x + w
  1582. w = 74
  1583. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1584. }
  1585. x = x + w
  1586. w = 112
  1587. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1588. x = x + w
  1589. w = 101
  1590. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1591. if T_temp_show == 1 && T_Humidity_show == 1 {
  1592. x = x + w
  1593. w = 37
  1594. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1595. x = x + w
  1596. w = 37
  1597. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1598. }
  1599. if T_temp_show == 1 && T_Humidity_show == 0 {
  1600. x = x + w
  1601. w = 74
  1602. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1603. }
  1604. if T_temp_show == 0 && T_Humidity_show == 1 {
  1605. x = x + w
  1606. w = 74
  1607. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1608. }
  1609. y += 20
  1610. var textH float64 = 25 // if text height is 25px.
  1611. for i, v := range first_column {
  1612. pdf.SetNewY(y, textH)
  1613. y = pdf.GetY()
  1614. x, w = 10, 112
  1615. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1616. x = x + w
  1617. w = 101
  1618. lib.RectFillColor(pdf, v.T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1619. // 显示温湿度
  1620. if T_temp_show == 1 && T_Humidity_show == 1 {
  1621. x = x + w
  1622. w = 37
  1623. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1624. x = x + w
  1625. w = 37
  1626. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1627. }
  1628. // 只显示温度
  1629. if T_temp_show == 1 && T_Humidity_show == 0 {
  1630. x = x + w
  1631. w = 74
  1632. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1633. }
  1634. // 只显示湿度
  1635. if T_temp_show == 0 && T_Humidity_show == 1 {
  1636. x = x + w
  1637. w = 74
  1638. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1639. }
  1640. if i < len(second_column) {
  1641. x = x + w
  1642. w = 112
  1643. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1644. x = x + w
  1645. w = 101
  1646. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1647. if T_temp_show == 1 && T_Humidity_show == 1 {
  1648. x = x + w
  1649. w = 37
  1650. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1651. x = x + w
  1652. w = 37
  1653. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1654. }
  1655. if T_temp_show == 1 && T_Humidity_show == 0 {
  1656. x = x + w
  1657. w = 74
  1658. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1659. }
  1660. if T_temp_show == 0 && T_Humidity_show == 1 {
  1661. x = x + w
  1662. w = 74
  1663. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1664. }
  1665. }
  1666. y += 20
  1667. }
  1668. if len(second_column) > len(first_column) {
  1669. for i := len(first_column); i < len(second_column); i++ {
  1670. pdf.SetNewY(y, textH)
  1671. y = pdf.GetY()
  1672. x, w = 297, 112
  1673. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1674. x = x + w
  1675. w = 101
  1676. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1677. if T_temp_show == 1 && T_Humidity_show == 1 {
  1678. x = x + w
  1679. w = 37
  1680. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1681. x = x + w
  1682. w = 37
  1683. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1684. }
  1685. if T_temp_show == 1 && T_Humidity_show == 0 {
  1686. x = x + w
  1687. w = 74
  1688. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1689. }
  1690. if T_temp_show == 0 && T_Humidity_show == 1 {
  1691. x = x + w
  1692. w = 74
  1693. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1694. }
  1695. y += 20
  1696. }
  1697. }
  1698. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  1699. err = pdf.WritePdf(timeStr)
  1700. if err != nil {
  1701. return
  1702. }
  1703. // 上传 OSS
  1704. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  1705. if !is {
  1706. err = errors.New("oss!")
  1707. return
  1708. }
  1709. //删除目录
  1710. err = os.Remove(timeStr)
  1711. if err != nil {
  1712. logs.Error(err)
  1713. }
  1714. return url, nil
  1715. }
  1716. // 数据备份
  1717. func (c *DataController) Device_Sensor_Data_BackUp() {
  1718. page, _ := c.GetInt("page")
  1719. if page < 1 {
  1720. page = 1
  1721. }
  1722. page_z, _ := c.GetInt("page_z")
  1723. if page_z < 1 {
  1724. page_z = conf.Page_size
  1725. }
  1726. T_snid := c.GetString("T_snid")
  1727. Time_start := c.GetString("Time_start")
  1728. Time_end := c.GetString("Time_end")
  1729. if len(T_snid) < 10 {
  1730. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  1731. c.ServeJSON()
  1732. return
  1733. }
  1734. if _, is := lib.DateStrToDate(Time_start); !is {
  1735. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1736. c.ServeJSON()
  1737. return
  1738. }
  1739. if _, is := lib.DateStrToDate(Time_end); !is {
  1740. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1741. c.ServeJSON()
  1742. return
  1743. }
  1744. Time_start += " 00:00:00"
  1745. Time_end += " 23:59:59"
  1746. var r_jsons lib.R_JSONS
  1747. r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_BackUp_List(T_snid, Time_start, Time_end, page, page_z)
  1748. r_jsons.Page = page
  1749. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1750. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1751. c.ServeJSON()
  1752. return
  1753. }
  1754. // 数据备份pdf
  1755. func (c *DataController) Device_Sensor_Data_BackUp_PDF() {
  1756. T_sn := c.GetString("T_sn")
  1757. T_id, _ := c.GetInt("T_id")
  1758. T_date := c.GetString("T_date")
  1759. T_snid := fmt.Sprintf("%s,%d", T_sn, T_id)
  1760. date, is := lib.DateStrToDate(T_date)
  1761. if !is {
  1762. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1763. c.ServeJSON()
  1764. return
  1765. }
  1766. Time_start := T_date + " 00:00:00"
  1767. Time_end := T_date + " 23:59:59"
  1768. var DeviceSensor_data []Device.DeviceData_R
  1769. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  1770. var err error
  1771. pdf := &gopdf.GoPdf{}
  1772. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1773. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  1774. //if err != nil {
  1775. // log.Fatalln(err)
  1776. //}
  1777. //err = pdf.SetFont("Ubuntu-L", "", 14)
  1778. //if err != nil {
  1779. // log.Fatalln(err)
  1780. //}
  1781. err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
  1782. if err != nil {
  1783. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1784. c.ServeJSON()
  1785. return
  1786. }
  1787. err = pdf.SetFont("simsun", "", 24)
  1788. if err != nil {
  1789. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1790. c.ServeJSON()
  1791. return
  1792. }
  1793. pdf.SetGrayFill(0.5)
  1794. pdf.SetMargins(0, 20, 0, 20)
  1795. pdf.AddPage()
  1796. //use path
  1797. //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
  1798. company, _ := Account.Read_Company_ById(c.T_pid)
  1799. textw, _ := pdf.MeasureTextWidth(company.T_name)
  1800. pdf.SetX((595 / 2) - (textw / 2))
  1801. pdf.SetY(40)
  1802. pdf.Text(company.T_name)
  1803. // 线
  1804. pdf.SetLineWidth(2)
  1805. pdf.SetLineType("dashed")
  1806. pdf.Line(10, 60, 585, 60)
  1807. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1808. if err != nil {
  1809. c.Data["json"] = lib.JSONS{Code: 202, Msg: "ok!", Data: err}
  1810. c.ServeJSON()
  1811. return
  1812. }
  1813. err = pdf.SetFont("wts", "", 12)
  1814. if err != nil {
  1815. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1816. c.ServeJSON()
  1817. return
  1818. }
  1819. var s_time, e_time string
  1820. if len(DeviceSensor_data) > 0 {
  1821. s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
  1822. e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
  1823. }
  1824. //fmt.Sprintf(" %.1f ", v.T_t)
  1825. lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1826. lib.RectFillColor(pdf, "序号", 12, 22, 120, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1827. lib.RectFillColor(pdf, "传感器名称", 12, 52, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1828. lib.RectFillColor(pdf, "温度℃", 12, 152, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1829. lib.RectFillColor(pdf, "湿度%", 12, 212, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1830. lib.RectFillColor(pdf, "温度范围", 12, 272, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1831. lib.RectFillColor(pdf, "湿度范围", 12, 362, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1832. lib.RectFillColor(pdf, "记录时间", 12, 452, 120, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1833. var y float64 = 140
  1834. err = pdf.SetFont("wts", "", 10)
  1835. if err != nil {
  1836. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1837. c.ServeJSON()
  1838. return
  1839. }
  1840. for i, v := range DeviceSensor_data {
  1841. text := fmt.Sprintf(" %d ", i+1)
  1842. var textH float64 = 25 // if text height is 25px.
  1843. pdf.SetNewY(y, textH)
  1844. y = pdf.GetY()
  1845. //pdf.SetX(x) // must after pdf.SetNewY() called.
  1846. //err = pdf.Text(text)
  1847. //if err != nil {
  1848. // log.Fatalln(err)
  1849. //}
  1850. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  1851. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  1852. T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
  1853. T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
  1854. T_time := fmt.Sprintf("%s", v.T_time)
  1855. //if user_r.Admin_rh == 0 && strings.Contains(v.T_sn, "YD") {
  1856. // T_rh = "-"
  1857. // T_Rlu = "-"
  1858. //}
  1859. lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1860. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1861. lib.RectFillColor(pdf, T_t, 10, 152, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1862. lib.RectFillColor(pdf, T_rh, 10, 212, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1863. lib.RectFillColor(pdf, T_Tlu, 10, 272, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1864. lib.RectFillColor(pdf, T_Rlu, 10, 362, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1865. lib.RectFillColor(pdf, T_time, 10, 452, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1866. y += 20
  1867. }
  1868. deviceSensor, _ := Device.Read_DeviceSensor_ByT_sn(T_sn, T_id)
  1869. rand_x := int64(lib.Random(0, 10000))
  1870. filenameStr := fmt.Sprintf("backup/sn/%s_%s(%s).pdf", lib.GetRandstring(8, "0123456789", rand_x), deviceSensor.T_name, date.Format("2006-01-02"))
  1871. err = pdf.WritePdf(filenameStr)
  1872. if err != nil {
  1873. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1874. c.ServeJSON()
  1875. return
  1876. }
  1877. // 上传 OSS
  1878. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+filenameStr, filenameStr)
  1879. if !is {
  1880. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1881. c.ServeJSON()
  1882. return
  1883. }
  1884. //删除目录
  1885. err = os.Remove(filenameStr)
  1886. if err != nil {
  1887. logs.Error(lib.FuncName(), err)
  1888. }
  1889. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1890. c.ServeJSON()
  1891. return
  1892. }
  1893. // 对接电信实时数据
  1894. func (c *DataController) Docking_Real_Data() {
  1895. key := c.GetString("T_key")
  1896. // 查询公司
  1897. Company_r, err := Account.Read_Company_ByKey(key)
  1898. if err != nil {
  1899. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
  1900. c.ServeJSON()
  1901. return
  1902. }
  1903. // 查询公司下面所有子公司id
  1904. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1905. type R_JSONS struct {
  1906. //必须的大写开头
  1907. Data []Device.DeviceData_Docking
  1908. }
  1909. var r_jsons R_JSONS
  1910. r_jsons.Data = Device.Read_Docking_Real_Data(T_pids)
  1911. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1912. c.ServeJSON()
  1913. return
  1914. }
  1915. // 对接电信历史数据
  1916. func (c *DataController) Docking_Note_Data() {
  1917. key := c.GetString("T_key")
  1918. // 查询公司,验证T_key
  1919. _, err := Account.Read_Company_ByKey(key)
  1920. if err != nil {
  1921. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
  1922. c.ServeJSON()
  1923. return
  1924. }
  1925. page, _ := c.GetInt("page")
  1926. if page < 1 {
  1927. page = 1
  1928. }
  1929. page_z, _ := c.GetInt("page_z")
  1930. if page_z < 1 {
  1931. page_z = conf.Page_size
  1932. }
  1933. if page_z > 50 {
  1934. c.Data["json"] = lib.JSONS{Code: 202, Msg: "page_z 超出限制"}
  1935. c.ServeJSON()
  1936. return
  1937. }
  1938. T_sn := c.GetString("T_sn")
  1939. T_id, T_id_Err := c.GetInt("T_id")
  1940. Time_start := c.GetString("Time_start")
  1941. Time_end := c.GetString("Time_end")
  1942. if len(T_sn) == 0 {
  1943. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 格式错误"}
  1944. c.ServeJSON()
  1945. return
  1946. }
  1947. if T_id_Err != nil {
  1948. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id 格式错误"}
  1949. c.ServeJSON()
  1950. return
  1951. }
  1952. st, is := lib.TimeStrToTime(Time_start)
  1953. if !is {
  1954. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
  1955. c.ServeJSON()
  1956. return
  1957. }
  1958. if st.Add(40 * 24 * time.Hour).Before(time.Now()) {
  1959. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
  1960. c.ServeJSON()
  1961. return
  1962. }
  1963. et, is := lib.TimeStrToTime(Time_end)
  1964. if !is {
  1965. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
  1966. c.ServeJSON()
  1967. return
  1968. }
  1969. if et.Add(40 * 24 * time.Hour).Before(time.Now()) {
  1970. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
  1971. c.ServeJSON()
  1972. return
  1973. }
  1974. type R_JSONS struct {
  1975. //必须的大写开头
  1976. DeviceSensor_data []Device.DeviceData_R
  1977. Num int
  1978. Page int
  1979. Page_size int
  1980. Pages []lib.Page_T
  1981. }
  1982. var r_jsons R_JSONS
  1983. var cnt int
  1984. Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
  1985. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
  1986. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1987. r_jsons.Page = page
  1988. r_jsons.Page_size = int(page_size)
  1989. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1990. r_jsons.Num = cnt
  1991. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1992. c.ServeJSON()
  1993. return
  1994. }