Device.go 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Company"
  7. "Cold_Api/models/Device"
  8. "Cold_Api/models/System"
  9. "Cold_Api/models/Warning"
  10. "fmt"
  11. beego "github.com/beego/beego/v2/server/web"
  12. "github.com/xuri/excelize/v2"
  13. "math"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // Handle
  19. type DeviceController struct {
  20. beego.Controller
  21. }
  22. // 设备 ------------------------------------------
  23. func (c *DeviceController) Device_List() {
  24. // 验证登录
  25. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  26. if !b_ {
  27. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  28. c.ServeJSON()
  29. return
  30. }
  31. type R_JSONS struct {
  32. //必须的大写开头
  33. Device_lite []Device.Device_R
  34. Num int64
  35. Page int
  36. Page_size int
  37. }
  38. var r_jsons R_JSONS
  39. page, _ := c.GetInt("page")
  40. println(page)
  41. if page < 1 {
  42. page = 1
  43. }
  44. page_z, _ := c.GetInt("page_z")
  45. if page_z < 1 {
  46. page_z = conf.Page_size
  47. }
  48. Name := c.GetString("T_name")
  49. T_pid, T_pid_err := c.GetInt("T_pid")
  50. T_online := c.GetString("T_online")
  51. T_monitor := c.GetString("T_monitor")
  52. if admin_r.T_pid > 0 {
  53. T_pid = admin_r.T_pid
  54. } else {
  55. if T_pid_err != nil || T_pid <= 0 {
  56. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  57. c.ServeJSON()
  58. return
  59. }
  60. }
  61. r_jsons.Device_lite, r_jsons.Num = Device.Read_Device_List(T_pid, Name, T_monitor, T_online, page, page_z)
  62. r_jsons.Page = page
  63. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  64. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  65. c.ServeJSON()
  66. return
  67. }
  68. func (c *DeviceController) Device_Post() {
  69. /// 验证登录
  70. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  71. if !b_ {
  72. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  73. c.ServeJSON()
  74. return
  75. }
  76. var err error
  77. T_sn := c.GetString("T_sn")
  78. T_MSISDN := c.GetString("T_MSISDN")
  79. var_ := Device.Device{
  80. T_sn: T_sn,
  81. T_MSISDN: T_MSISDN,
  82. T_give: 1,
  83. T_monitor: 1,
  84. T_State: 1,
  85. }
  86. if len(T_sn) < 8 {
  87. c.Data["json"] = lib.JSONS{Code: 303, Msg: "SN 太短 必须8个字符以上"}
  88. c.ServeJSON()
  89. return
  90. }
  91. if !(strings.Contains(T_sn, "KF") || strings.Contains(T_sn, "YD")) {
  92. c.Data["json"] = lib.JSONS{Code: 303, Msg: "必须包含 KF或YD!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. _, err = Device.Read_Device_ByT_sn(T_sn)
  97. if err == nil {
  98. c.Data["json"] = lib.JSONS{Code: 303, Msg: "重复SN!"}
  99. c.ServeJSON()
  100. return
  101. }
  102. var_.T_devName = "等待设备上线.."
  103. // 创建数据库
  104. if !Device.CREATE_DeviceData(T_sn) {
  105. c.Data["json"] = lib.JSONS{Code: 302, Msg: "添加失败!"}
  106. c.ServeJSON()
  107. return
  108. }
  109. err = Device.Add_Device(var_)
  110. if err != nil {
  111. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  112. c.ServeJSON()
  113. return
  114. }
  115. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "设备添加", var_)
  116. //MqttServer.Get_Device_Realtime(T_sn) // 更新传感器 !!!
  117. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  118. c.ServeJSON()
  119. return
  120. }
  121. func (c *DeviceController) Device_Del() {
  122. // 验证登录
  123. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  124. if !b_ {
  125. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  126. c.ServeJSON()
  127. return
  128. }
  129. if admin_r.Id != 1 {
  130. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
  131. c.ServeJSON()
  132. return
  133. }
  134. T_sn := c.GetString("T_sn")
  135. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  136. if err != nil {
  137. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  138. c.ServeJSON()
  139. return
  140. }
  141. Device.Delete_Device(Device_r)
  142. DeviceSensor_list, _ := Device.Read_DeviceSensor_ByTsn(Device_r.T_sn)
  143. for _, v := range DeviceSensor_list {
  144. Device.Delete_DeviceSensor_ById(Device_r.T_sn, v.T_id)
  145. }
  146. Device.DELETE_DeviceSensor(Device_r.T_sn)
  147. Device.DELETE_DeviceDatar(Device_r.T_sn)
  148. Device.DELETE_DeviceParameter(Device_r.T_sn)
  149. Device.DELETE_DeviceSensorParameter(Device_r.T_sn)
  150. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "设备删除", T_sn)
  151. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  152. c.ServeJSON()
  153. return
  154. }
  155. func (c *DeviceController) Device_Edit() {
  156. // 验证登录
  157. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  158. if !b_ {
  159. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  160. c.ServeJSON()
  161. return
  162. }
  163. T_SN := c.GetString("T_sn")
  164. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  165. if err != nil {
  166. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  167. c.ServeJSON()
  168. return
  169. }
  170. T_VerifyTime := c.GetString("T_VerifyTime")
  171. if len(T_VerifyTime) > 5 {
  172. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_VerifyTime, time.Local)
  173. if err == nil {
  174. Device_r.T_VerifyTime = stamp
  175. Device.Update_Device(Device_r, "T_VerifyTime")
  176. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "验证时间操作", "验证时间:"+T_VerifyTime)
  177. } else {
  178. c.Data["json"] = lib.JSONS{Code: 203, Msg: "时间解析错误!" + T_VerifyTime}
  179. c.ServeJSON()
  180. return
  181. }
  182. }
  183. T_CalibrationTime := c.GetString("T_CalibrationTime")
  184. if len(T_CalibrationTime) > 5 {
  185. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_CalibrationTime, time.Local)
  186. if err == nil {
  187. Device_r.T_CalibrationTime = stamp
  188. Device.Update_Device(Device_r, "T_CalibrationTime")
  189. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "校准时间操作", "校准时间:"+T_CalibrationTime)
  190. } else {
  191. c.Data["json"] = lib.JSONS{Code: 204, Msg: "时间解析错误!" + T_CalibrationTime}
  192. c.ServeJSON()
  193. return
  194. }
  195. }
  196. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  197. c.ServeJSON()
  198. return
  199. }
  200. // 设备参数 ------------------------------------------
  201. // 设备参数列表
  202. func (c *DeviceController) Device_Parameter_List() {
  203. // 验证登录
  204. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  205. if !b_ {
  206. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  207. c.ServeJSON()
  208. return
  209. }
  210. Sn := c.GetString("T_sn")
  211. DeviceParameter_lite := Device.Read_DeviceParameter_SN_List(Sn)
  212. if len(DeviceParameter_lite) == 0 {
  213. NatsServer.Read_DeviceParameter(Sn)
  214. c.Data["json"] = lib.JSONS{Code: 203, Msg: "未同步参数,请检查设备是否正常!"}
  215. c.ServeJSON()
  216. return
  217. }
  218. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceParameter_lite}
  219. c.ServeJSON()
  220. return
  221. }
  222. // 修改设备参数
  223. func (c *DeviceController) Device_Parameter_Pu() {
  224. // 验证登录
  225. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  226. if !b_ {
  227. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  228. c.ServeJSON()
  229. return
  230. }
  231. T_SN := c.GetString("T_sn")
  232. T_devName := c.GetString("T_devName")
  233. T_uploadTime, _ := c.GetInt("T_uploadTime")
  234. T_saveTime, _ := c.GetInt("T_saveTime")
  235. T_overrunSave, _ := c.GetInt("T_overrunSave")
  236. T_overrunAlarm, _ := c.GetInt("T_overrunAlarm")
  237. T_outageAlarm, _ := c.GetInt("T_outageAlarm")
  238. T_warningDelay, _ := c.GetInt("T_warningDelay")
  239. T_lostAlarm, _ := c.GetInt("T_lostAlarm")
  240. T_batteryLimit, _ := c.GetInt("T_batteryLimit")
  241. T_warningTime, _ := c.GetInt("T_warningTime")
  242. T_enwarning, _ := c.GetInt("T_enwarning")
  243. T_decTotal, _ := c.GetInt("T_decTotal")
  244. T_chDecTotal := c.GetString("T_chDecTotal")
  245. T_company := c.GetString("T_company")
  246. T_btname := c.GetString("T_btname")
  247. T_btserverID, _ := c.GetInt("T_btserverID")
  248. T_btchar, _ := c.GetInt("T_btchar")
  249. T_Msid, _ := c.GetInt("T_Msid")
  250. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  251. if err != nil {
  252. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  253. c.ServeJSON()
  254. return
  255. }
  256. Deviceparameter := Device.DeviceParameter{
  257. T_uuid: admin_r.T_uuid,
  258. T_uploadTime: T_uploadTime,
  259. T_SendState: 0,
  260. T_State: 2,
  261. T_sn: T_SN,
  262. T_devName: T_devName,
  263. T_saveTime: T_saveTime,
  264. T_overrunSave: T_overrunSave,
  265. T_overrunAlarm: T_overrunAlarm,
  266. T_outageAlarm: T_outageAlarm,
  267. T_lostAlarm: T_lostAlarm,
  268. T_warningTime: T_warningTime,
  269. T_warningDelay: T_warningDelay,
  270. T_batteryLimit: T_batteryLimit,
  271. T_chDecTotal: T_chDecTotal,
  272. T_decTotal: strconv.Itoa(T_decTotal),
  273. T_enwarning: T_enwarning,
  274. T_company: T_company,
  275. T_btname: T_btname,
  276. T_btserverID: strconv.Itoa(T_btserverID),
  277. T_btchar: strconv.Itoa(T_btchar),
  278. T_Msid: T_Msid,
  279. }
  280. Deviceparameter, is := Device.Add_DeviceParameter(Deviceparameter)
  281. if !is {
  282. c.Data["json"] = lib.JSONS{Code: 203, Msg: "Add_DeviceParameter ERR"}
  283. c.ServeJSON()
  284. return
  285. }
  286. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "设备参数操作", Deviceparameter)
  287. NatsServer.Pu_DeviceParameter(Deviceparameter)
  288. Device_r.T_devName = Deviceparameter.T_devName
  289. Device.Update_Device(Device_r, "T_devName")
  290. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  291. c.ServeJSON()
  292. return
  293. }
  294. // 删除设备 传感器
  295. func (c *DeviceController) Device_Parameter_Del_Device() {
  296. // 验证登录
  297. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  298. if !b_ {
  299. c.Ctx.Redirect(302, "Login")
  300. return
  301. }
  302. Sn := c.GetString("Sn")
  303. Id, _ := c.GetInt("Id")
  304. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  305. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  306. Device.Delete_DeviceSensor_ById(Sn, Id)
  307. NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
  308. T_sn: Sn, T_id: Id,
  309. })
  310. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  311. c.ServeJSON()
  312. return
  313. }
  314. // 设备参数 1库房 2移动
  315. func (c *DeviceController) Pu_DeviceParameter_T_type() {
  316. // 验证登录
  317. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  318. if !b_ {
  319. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  320. c.ServeJSON()
  321. return
  322. }
  323. T_SN := c.GetString("SN")
  324. T_type, _ := c.GetInt("T_l_p")
  325. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  326. if err != nil {
  327. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  328. c.ServeJSON()
  329. return
  330. }
  331. Device_r.T_type = T_type
  332. Device.Update_Device(Device_r, "T_l_p")
  333. Device.DeviceSensor_T_type_ALL(T_SN, T_type)
  334. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "设备参数操作", "类型选择(1库房 2移动):"+string(T_type))
  335. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  336. c.ServeJSON()
  337. return
  338. }
  339. // 设备是否弃用 0屏蔽 1正常
  340. func (c *DeviceController) Pu_DeviceParameter_T_give() {
  341. // 验证登录
  342. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  343. if !b_ {
  344. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  345. c.ServeJSON()
  346. return
  347. }
  348. T_SN := c.GetString("SN")
  349. T_give, _ := c.GetInt("T_give")
  350. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  351. if err != nil {
  352. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  353. c.ServeJSON()
  354. return
  355. }
  356. // // 1物流端 2药店端
  357. Device_r.T_give = T_give
  358. Device.Update_Device(Device_r, "T_give")
  359. Device.DeviceSensor_t_give_ALL(T_SN, T_give)
  360. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "设备丢弃参数操作", "设备丢弃( 0 丢弃 1 正常):"+string(T_give))
  361. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  362. c.ServeJSON()
  363. return
  364. }
  365. // 获取传感器参数
  366. func (c *DeviceController) Read_DeviceSensorParameter() {
  367. // 验证登录
  368. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  369. if !b_ {
  370. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  371. c.ServeJSON()
  372. return
  373. }
  374. T_SN := c.GetString("T_sn")
  375. _, err := Device.Read_Device_ByT_sn(T_SN)
  376. if err != nil {
  377. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  378. c.ServeJSON()
  379. return
  380. }
  381. NatsServer.Read_DeviceSensorParameter(T_SN)
  382. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  383. c.ServeJSON()
  384. return
  385. }
  386. // 设备-传感器 ------------------------------------------
  387. // 传感器列表
  388. func (c *DeviceController) DeviceSensor_List() {
  389. // 验证登录
  390. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  391. if !b_ {
  392. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  393. c.ServeJSON()
  394. return
  395. }
  396. type R_JSONS struct {
  397. //必须的大写开头
  398. Data []Device.DeviceSensor_R
  399. Num int64
  400. Page int
  401. Page_size int
  402. }
  403. var r_jsons R_JSONS
  404. page, _ := c.GetInt("page")
  405. println(page)
  406. if page < 1 {
  407. page = 1
  408. }
  409. page_z, _ := c.GetInt("page_z")
  410. if page_z < 1 {
  411. page_z = conf.Page_size
  412. }
  413. T_sn := c.GetString("T_sn")
  414. Name := c.GetString("T_name") // 包含 T_name、T_sn
  415. T_calss_id, _ := c.GetInt("T_calssid")
  416. T_give, T_give_err := c.GetInt("T_give") //0 :丢弃 1:正常 空:所有
  417. if T_give_err != nil {
  418. T_give = -1
  419. }
  420. T_datashow, T_datashow_err := c.GetInt("T_datashow") //空:正常显示 1:强制显示 屏蔽数据展示
  421. if T_datashow_err != nil {
  422. T_datashow = -1
  423. }
  424. var T_pid int
  425. if admin_r.T_pid == 0 {
  426. T_pid, _ = c.GetInt("T_pid")
  427. }
  428. if T_calss_id > 0 {
  429. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_calss_id)
  430. if err != nil {
  431. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  432. c.ServeJSON()
  433. return
  434. }
  435. if R_DeviceClass.T_pid != T_pid {
  436. c.Data["json"] = lib.JSONS{Code: 205, Msg: "R_DeviceClass.T_pid != T_pid Err!"}
  437. c.ServeJSON()
  438. return
  439. }
  440. }
  441. r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensorList(T_pid, T_sn, Name, T_calss_id, T_give, T_datashow, page, page_z)
  442. r_jsons.Page = page
  443. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  444. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  445. c.ServeJSON()
  446. return
  447. }
  448. // 修改传感器
  449. func (c *DeviceController) DeviceSensor_Edit() {
  450. // 验证登录
  451. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  452. if !b_ {
  453. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey!"}
  454. c.ServeJSON()
  455. return
  456. }
  457. Sn := c.GetString("T_sn")
  458. Id, _ := c.GetInt("T_id")
  459. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  460. if !is {
  461. c.Data["json"] = lib.JSONS{Code: 203, Msg: "信息错误!"}
  462. c.ServeJSON()
  463. return
  464. }
  465. // 实时数据显示排序
  466. if v, err := c.GetInt("T_sort"); err == nil {
  467. DeviceSensor.T_sort = v
  468. Device.Update_DeviceSensor(DeviceSensor, "T_sort")
  469. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 排序操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
  470. }
  471. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  472. c.ServeJSON()
  473. return
  474. }
  475. // 删除传感器
  476. func (c *DeviceController) DeviceSensor_Del() {
  477. // 验证登录
  478. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  479. if !b_ {
  480. c.Ctx.Redirect(302, "Login")
  481. return
  482. }
  483. Sn := c.GetString("T_sn")
  484. Id, _ := c.GetInt("T_id")
  485. _, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  486. if !is {
  487. c.Data["json"] = lib.JSONS{Code: 205, Msg: "信息错误!"}
  488. c.ServeJSON()
  489. return
  490. }
  491. // 0默认:从数据库删除 1:从设备删除
  492. T_mode, _ := c.GetInt("T_mode")
  493. if T_mode == 1 {
  494. Device.Delete_DeviceSensor_ById(Sn, Id)
  495. NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
  496. T_sn: Sn, T_id: Id,
  497. })
  498. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  499. } else {
  500. Device.Delete_DeviceSensor_ById(Sn, Id)
  501. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  502. }
  503. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  504. c.ServeJSON()
  505. return
  506. }
  507. // 传感器参数列表
  508. func (c *DeviceController) DeviceSensor_Parameter_List() {
  509. // 验证登录
  510. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  511. if !b_ {
  512. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  513. c.ServeJSON()
  514. return
  515. }
  516. Sn := c.GetString("T_sn")
  517. Id, _ := c.GetInt("T_id")
  518. //c.Data["DeviceSensor"], _ = Device.Read_DeviceSensor_ByTsn_Tid(Sn, Id)
  519. DeviceSensorParameter_lite := Device.Read_V2DeviceSensorParameter_List(Sn, Id, 2, 10)
  520. if len(DeviceSensorParameter_lite) == 0 {
  521. NatsServer.Read_DeviceSensorParameter(Sn)
  522. c.Data["json"] = lib.JSONS{Code: 203, Msg: "未同步参数,请检查设备是否正常!"}
  523. c.ServeJSON()
  524. return
  525. }
  526. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceSensorParameter_lite}
  527. c.ServeJSON()
  528. return
  529. }
  530. // 修改传感器参数
  531. func (c *DeviceController) DeviceSensor_Parameter_Pu() {
  532. // 验证登录
  533. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  534. if !b_ {
  535. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  536. c.ServeJSON()
  537. return
  538. }
  539. T_SN := c.GetString("T_sn")
  540. T_name := c.GetString("T_name")
  541. T_id, _ := c.GetInt("T_id")
  542. T_Tlower, _ := c.GetFloat("T_Tlower")
  543. T_Tupper, _ := c.GetFloat("T_Tupper")
  544. T_RHlower, _ := c.GetFloat("T_RHlower")
  545. T_RHupper, _ := c.GetFloat("T_RHupper")
  546. T_en, _ := c.GetInt("T_en")
  547. T_free, _ := c.GetInt("T_free")
  548. T_enprel, _ := c.GetInt("T_enprel")
  549. T_tprel, _ := c.GetFloat("T_tprel")
  550. T_tpreu, _ := c.GetFloat("T_tpreu")
  551. T_hprel, _ := c.GetFloat("T_hprel")
  552. T_hpreu, _ := c.GetFloat("T_hpreu")
  553. _, err := Device.Read_Device_ByT_sn(T_SN)
  554. if err != nil {
  555. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  556. c.ServeJSON()
  557. return
  558. }
  559. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(T_SN, T_id)
  560. if !is {
  561. c.Data["json"] = lib.JSONS{Code: 208, Msg: "T_sn T_id Err!"}
  562. c.ServeJSON()
  563. return
  564. }
  565. Devicesensorparameter := Device.DeviceSensorParameter{
  566. T_sn: T_SN,
  567. T_id: T_id,
  568. T_Tlower: float32(T_Tlower),
  569. T_Tupper: float32(T_Tupper),
  570. T_RHlower: float32(T_RHlower),
  571. T_RHupper: float32(T_RHupper),
  572. T_en: T_en,
  573. T_free: T_free,
  574. T_enprel: T_enprel,
  575. T_tprel: float32(T_tprel),
  576. T_tpreu: float32(T_tpreu),
  577. T_hprel: float32(T_hprel),
  578. T_hpreu: float32(T_hpreu),
  579. T_uuid: admin_r.T_uuid,
  580. T_SendState: 0,
  581. T_State: 2,
  582. }
  583. dsp_id, is := Device.Add_DeviceSensorParameter(Devicesensorparameter)
  584. if !is {
  585. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  586. c.ServeJSON()
  587. return
  588. }
  589. Devicesensorparameter.Id = int(dsp_id)
  590. // 更新名称
  591. DeviceSensor.T_name = T_name
  592. Device.Update_DeviceSensor(DeviceSensor, "T_name")
  593. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器参数操作", Devicesensorparameter)
  594. NatsServer.Pu_DeviceParameter_Sensor(Devicesensorparameter)
  595. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  596. c.ServeJSON()
  597. return
  598. }
  599. // 传感器 实时数据显示排序
  600. func (c *DeviceController) UpDeviceSensor_Tsort() {
  601. // 验证登录
  602. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  603. if !b_ {
  604. c.Ctx.Redirect(302, "Login")
  605. return
  606. }
  607. Sn := c.GetString("Sn")
  608. Id, _ := c.GetInt("Id")
  609. T_sort, _ := c.GetInt("T_sort")
  610. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  611. if !is {
  612. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  613. c.ServeJSON()
  614. return
  615. }
  616. DeviceSensor.T_sort = T_sort
  617. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  618. Device.Update_DeviceSensor(DeviceSensor, "T_sort")
  619. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 排序操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(T_sort))
  620. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  621. c.ServeJSON()
  622. return
  623. }
  624. // 传感器 3D视图
  625. func (c *DeviceController) UpDeviceSensor_T3dview() {
  626. // 验证登录
  627. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  628. if !b_ {
  629. c.Ctx.Redirect(302, "Login")
  630. return
  631. }
  632. Sn := c.GetString("Sn")
  633. Id, _ := c.GetInt("Id")
  634. T_3dview := c.GetString("T_3dview")
  635. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  636. if !is {
  637. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  638. c.ServeJSON()
  639. return
  640. }
  641. DeviceSensor.T_3dview = T_3dview
  642. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  643. Device.Update_DeviceSensor(DeviceSensor, "T_3dview")
  644. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "3D 视图ID操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+T_3dview)
  645. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  646. c.ServeJSON()
  647. return
  648. }
  649. // 传感器 屏蔽数据展示 0 屏蔽数据展示 1 正常数据展示
  650. func (c *DeviceController) UpDeviceSensor_T_datashow() {
  651. // 验证登录
  652. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  653. if !b_ {
  654. c.Ctx.Redirect(302, "Login")
  655. return
  656. }
  657. Sn := c.GetString("Sn")
  658. Id, _ := c.GetInt("Id")
  659. T_datashow, _ := c.GetInt("T_datashow")
  660. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  661. if !is {
  662. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  663. c.ServeJSON()
  664. return
  665. }
  666. DeviceSensor.T_datashow = T_datashow
  667. Device.Update_DeviceSensor(DeviceSensor, "T_datashow")
  668. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 数据展示(0 屏蔽数据展示 1 正常数据展示)", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(T_datashow))
  669. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  670. c.ServeJSON()
  671. return
  672. }
  673. // 设备任务 ------------------------------------------
  674. // 任务列表
  675. func (c *DeviceController) DeviceTask_List() {
  676. // 验证登录
  677. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  678. if !b_ {
  679. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  680. c.ServeJSON()
  681. return
  682. }
  683. type R_JSONS struct {
  684. //必须的大写开头
  685. Data []Device.DeviceTask
  686. Num int64
  687. Page int
  688. Page_size int
  689. }
  690. var r_jsons R_JSONS
  691. page, _ := c.GetInt("page")
  692. println(page)
  693. if page < 1 {
  694. page = 1
  695. }
  696. page_z, _ := c.GetInt("page_z")
  697. if page_z < 1 {
  698. page_z = conf.Page_size
  699. }
  700. T_sn := c.GetString("T_sn")
  701. Time_start := c.GetString("Time_start")
  702. Time_end := c.GetString("Time_end")
  703. if len(T_sn) < 6 {
  704. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn!!!"}
  705. c.ServeJSON()
  706. return
  707. }
  708. r_jsons.Data, r_jsons.Num = Device.Read_DeviceTask_All(T_sn, Time_start, Time_end, page, page_z)
  709. r_jsons.Page = page
  710. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  711. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  712. c.ServeJSON()
  713. return
  714. }
  715. // 任务启停
  716. func (c *DeviceController) DeviceTask_Post() {
  717. // 验证登录
  718. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  719. if !b_ {
  720. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  721. c.ServeJSON()
  722. return
  723. }
  724. T_sn := c.GetString("T_sn")
  725. T_task := c.GetString("T_task")
  726. NatsServer.Set_DeviceTask(Device.Device_task{
  727. T_sn: T_sn,
  728. T_task: T_task,
  729. })
  730. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "远程启停", T_sn+"-"+T_task)
  731. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  732. c.ServeJSON()
  733. return
  734. }
  735. // 设备告警 ------------------------------------------
  736. // 告警列表
  737. func (c *DeviceController) DeviceWarning_List() {
  738. // 验证登录
  739. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  740. if !b_ {
  741. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  742. c.ServeJSON()
  743. return
  744. }
  745. type R_JSONS struct {
  746. //必须的大写开头
  747. Data []Warning.Warning_R
  748. Num int64
  749. Page int
  750. Page_size int
  751. }
  752. var r_jsons R_JSONS
  753. page, _ := c.GetInt("page")
  754. println(page)
  755. if page < 1 {
  756. page = 1
  757. }
  758. page_z, _ := c.GetInt("page_z")
  759. if page_z < 1 {
  760. page_z = conf.Page_size
  761. }
  762. T_sn := c.GetString("T_sn")
  763. T_id := c.GetString("T_id")
  764. T_title := c.GetString("T_title")
  765. Time_start := c.GetString("Time_start")
  766. Time_end := c.GetString("Time_end")
  767. T_handle, _ := c.GetInt("T_handle")
  768. T_year, _ := c.GetInt("T_year")
  769. T_month, _ := c.GetInt("T_month")
  770. T_pid, _ := c.GetInt("T_pid")
  771. if admin_r.T_pid > 0 {
  772. T_pid = admin_r.T_pid
  773. }
  774. if T_year > 0 && T_month > 0 {
  775. // 获取备份
  776. r_jsons.Data, r_jsons.Num = Warning.Read_Warning_Backups(T_pid, strconv.Itoa(T_year), strconv.Itoa(T_month), T_sn, T_id, T_title, T_handle, Time_start, Time_end, page, page_z)
  777. } else {
  778. // 获取最新
  779. r_jsons.Data, r_jsons.Num = Warning.Read_Warning(T_pid, T_sn, T_id, T_title, T_handle, Time_start, Time_end, page, page_z)
  780. }
  781. r_jsons.Page = page
  782. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  783. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  784. c.ServeJSON()
  785. return
  786. }
  787. // 查询告警
  788. func (c *DeviceController) DeviceWarning_Get() {
  789. // 验证登录
  790. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  791. if !b_ {
  792. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  793. c.ServeJSON()
  794. return
  795. }
  796. id, _ := c.GetInt("T_id")
  797. T := Warning.Read_Warning_ById(id)
  798. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.WarningToWarning_R(T)}
  799. c.ServeJSON()
  800. return
  801. }
  802. // 编辑告警
  803. func (c *DeviceController) DeviceWarning_Post() {
  804. // 验证登录
  805. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  806. if !b_ {
  807. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  808. c.ServeJSON()
  809. return
  810. }
  811. Id, _ := c.GetInt("T_id")
  812. T_Text := c.GetString("T_Text")
  813. T := Warning.Read_Warning_ById(Id)
  814. if T.Id == 0 {
  815. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_id Err!"}
  816. c.ServeJSON()
  817. return
  818. }
  819. T.T_Text = T_Text
  820. T.T_State = 1
  821. Warning.Update_Warning(T, "T_Text", "T_State")
  822. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "报警处理操作", strconv.Itoa(Id)+"->"+T_Text)
  823. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  824. c.ServeJSON()
  825. return
  826. }
  827. // 删除告警
  828. func (c *DeviceController) DeviceWarning_Del() {
  829. // 验证登录
  830. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  831. if !b_ {
  832. c.Ctx.Redirect(302, "Login")
  833. return
  834. }
  835. Id, _ := c.GetInt("T_id")
  836. T := Warning.Read_Warning_ById(Id)
  837. if T.Id == 0 {
  838. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_id Err!"}
  839. c.ServeJSON()
  840. return
  841. }
  842. T.T_State = 0
  843. Warning.Update_Warning(T, "T_State")
  844. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "报警删除操作", strconv.Itoa(Id))
  845. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  846. c.ServeJSON()
  847. return
  848. }
  849. // 导出告警
  850. func (c *DeviceController) DeviceWarning_Data_Excel() {
  851. // 验证登录
  852. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  853. if !b_ {
  854. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  855. c.ServeJSON()
  856. return
  857. }
  858. //T_sn := c.GetString("T_sn")
  859. T_sn := c.GetString("T_sn")
  860. T_id := c.GetString("T_id")
  861. T_title := c.GetString("T_title")
  862. Time_start := c.GetString("Time_start")
  863. Time_end := c.GetString("Time_end")
  864. T_handle, _ := c.GetInt("T_handle")
  865. T_pid, _ := c.GetInt("T_pid")
  866. if admin_r.T_pid > 0 {
  867. T_pid = admin_r.T_pid
  868. }
  869. Device_data, _ := Warning.Read_Warning(T_pid, T_sn, T_id, T_title, T_handle, Time_start, Time_end, 0, 9999)
  870. f := excelize.NewFile() // 设置单元格的值
  871. // 这里设置表头
  872. f.SetCellValue("Sheet1", "A1", "报警类型")
  873. f.SetCellValue("Sheet1", "B1", "Sn")
  874. f.SetCellValue("Sheet1", "C1", "设备名称")
  875. f.SetCellValue("Sheet1", "D1", "传感器")
  876. f.SetCellValue("Sheet1", "E1", "报警内容")
  877. f.SetCellValue("Sheet1", "F1", "记录时间")
  878. f.SetCellValue("Sheet1", "G1", "处理")
  879. f.SetCellValue("Sheet1", "H1", "处理时间")
  880. // 设置列宽
  881. f.SetColWidth("Sheet1", "A", "A", 20)
  882. f.SetColWidth("Sheet1", "B", "B", 25)
  883. f.SetColWidth("Sheet1", "C", "C", 30)
  884. f.SetColWidth("Sheet1", "D", "D", 30)
  885. f.SetColWidth("Sheet1", "G", "E", 30)
  886. f.SetColWidth("Sheet1", "H", "F", 15)
  887. f.SetColWidth("Sheet1", "I", "G", 30)
  888. f.SetColWidth("Sheet1", "J", "H", 15)
  889. line := 1
  890. // 循环写入数据
  891. for _, v := range Device_data {
  892. line++
  893. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_tp)
  894. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn+"["+strconv.Itoa(v.Id)+"]")
  895. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  896. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  897. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  898. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  899. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  900. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.CreateTime)
  901. }
  902. timeStr := time.Now().Format("20060102150405")
  903. // 保存文件
  904. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  905. fmt.Println(err)
  906. }
  907. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  908. if !is {
  909. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  910. c.ServeJSON()
  911. return
  912. }
  913. //删除文件
  914. //err := os.Remove("ofile/" + timeStr + ".xlsx")
  915. //if err != nil {
  916. // fmt.Println(err)
  917. //}
  918. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  919. c.ServeJSON()
  920. return
  921. }
  922. // 告警提示列表
  923. func (c *DeviceController) DeviceWarningList_T_Tips() {
  924. // 验证登录
  925. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  926. if !b_ {
  927. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  928. c.ServeJSON()
  929. return
  930. }
  931. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All_T_Notice_mechanism()}
  932. c.ServeJSON()
  933. return
  934. }
  935. // 设备分类 ------------------------------------------
  936. func (c *DeviceController) CompanyClass_List() {
  937. // 验证登录
  938. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  939. if !b_ {
  940. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  941. c.ServeJSON()
  942. return
  943. }
  944. type R_JSONS struct {
  945. //必须的大写开头
  946. Data []Company.CompanyClass_R
  947. Num int64
  948. Page int
  949. Page_size int
  950. }
  951. var r_jsons R_JSONS
  952. page, _ := c.GetInt("page")
  953. println(page)
  954. if page < 1 {
  955. page = 1
  956. }
  957. page_z, _ := c.GetInt("page_z")
  958. if page_z < 1 {
  959. page_z = conf.Page_size
  960. }
  961. T_name := c.GetString("T_name")
  962. T_pid := admin_r.T_pid
  963. if T_pid == 0 {
  964. T_pid, _ = c.GetInt("T_pid")
  965. }
  966. r_jsons.Data, r_jsons.Num = Company.Read_CompanyClass_List(T_pid, T_name, page, page_z)
  967. r_jsons.Page = page
  968. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  969. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  970. c.ServeJSON()
  971. return
  972. }
  973. func (c *DeviceController) CompanyClass_All() {
  974. // 验证登录
  975. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  976. if !b_ {
  977. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  978. c.ServeJSON()
  979. return
  980. }
  981. type R_JSONS struct {
  982. //必须的大写开头
  983. Data []Company.CompanyClass_R
  984. Num int64
  985. Page int
  986. Page_size int
  987. }
  988. var r_jsons R_JSONS
  989. T_name := c.GetString("T_name")
  990. T_pid := admin_r.T_pid
  991. if T_pid == 0 {
  992. T_pid, _ = c.GetInt("T_pid")
  993. }
  994. r_jsons.Data = Company.Read_CompanyClass_All(T_pid, T_name)
  995. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  996. c.ServeJSON()
  997. return
  998. }
  999. func (c *DeviceController) CompanyClass_Get() {
  1000. // 验证登录
  1001. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1002. if !b_ {
  1003. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1004. c.ServeJSON()
  1005. return
  1006. }
  1007. id, _ := c.GetInt("T_id")
  1008. T_pid := admin_r.T_pid
  1009. if T_pid == 0 {
  1010. T_pid, _ = c.GetInt("T_pid")
  1011. }
  1012. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1013. if err != nil {
  1014. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1015. c.ServeJSON()
  1016. return
  1017. }
  1018. if R_DeviceClass.T_pid != T_pid {
  1019. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1020. c.ServeJSON()
  1021. return
  1022. }
  1023. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyClassToCompanyClass_R(R_DeviceClass)}
  1024. c.ServeJSON()
  1025. return
  1026. }
  1027. func (c *DeviceController) CompanyClass_Add() {
  1028. // 验证登录
  1029. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1030. if !b_ {
  1031. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1032. c.ServeJSON()
  1033. return
  1034. }
  1035. T_name := c.GetString("T_name")
  1036. T_pid := admin_r.T_pid
  1037. if T_pid == 0 {
  1038. T_pid, _ = c.GetInt("T_pid")
  1039. }
  1040. t_c := Company.CompanyClass{
  1041. T_pid: T_pid,
  1042. T_name: T_name,
  1043. T_State: 1,
  1044. }
  1045. Id, err := Company.Add_CompanyClass(t_c)
  1046. if err != nil {
  1047. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  1048. c.ServeJSON()
  1049. return
  1050. }
  1051. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "分类添加", t_c)
  1052. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  1053. c.ServeJSON()
  1054. return
  1055. }
  1056. func (c *DeviceController) CompanyClass_Edit() {
  1057. // 验证登录
  1058. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1059. if !b_ {
  1060. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1061. c.ServeJSON()
  1062. return
  1063. }
  1064. T_name := c.GetString("T_name")
  1065. id, _ := c.GetInt("T_id")
  1066. T_pid := admin_r.T_pid
  1067. if T_pid == 0 {
  1068. T_pid, _ = c.GetInt("T_pid")
  1069. }
  1070. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1071. if err != nil {
  1072. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1073. c.ServeJSON()
  1074. return
  1075. }
  1076. if R_DeviceClass.T_pid != T_pid {
  1077. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1078. c.ServeJSON()
  1079. return
  1080. }
  1081. if len(T_name) > 0 {
  1082. R_DeviceClass.T_name = T_name
  1083. }
  1084. if is := Company.Update_CompanyClass(R_DeviceClass, "T_name"); !is {
  1085. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  1086. c.ServeJSON()
  1087. return
  1088. }
  1089. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "分类修改", R_DeviceClass)
  1090. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1091. c.ServeJSON()
  1092. return
  1093. }
  1094. func (c *DeviceController) CompanyClass_Del() {
  1095. // 验证登录
  1096. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1097. if !b_ {
  1098. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1099. c.ServeJSON()
  1100. return
  1101. }
  1102. id, _ := c.GetInt("T_id")
  1103. T_pid := admin_r.T_pid
  1104. if T_pid == 0 {
  1105. T_pid, _ = c.GetInt("T_pid")
  1106. }
  1107. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1108. if err != nil {
  1109. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1110. c.ServeJSON()
  1111. return
  1112. }
  1113. if R_DeviceClass.T_pid != T_pid {
  1114. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1115. c.ServeJSON()
  1116. return
  1117. }
  1118. if is := Company.Delete_CompanyClass_ById(id); !is {
  1119. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除失败"}
  1120. c.ServeJSON()
  1121. return
  1122. }
  1123. // 将传感器T_Class绑定的分类id替换为空字符串
  1124. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Class_Id(id)
  1125. for _, v := range DeviceSensor_class_list {
  1126. Device.DeviceSensor_Bind_T_Class_Del(v.T_sn, v.T_id, id)
  1127. }
  1128. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "分类删除", strconv.Itoa(id))
  1129. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1130. c.ServeJSON()
  1131. return
  1132. }
  1133. // 设备分类绑定
  1134. func (c *DeviceController) DeviceClassBind_List() {
  1135. // 验证登录
  1136. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1137. if !b_ {
  1138. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1139. c.ServeJSON()
  1140. return
  1141. }
  1142. type R_JSONS struct {
  1143. //必须的大写开头
  1144. DeviceSensor_lite []Device.DeviceSensor_R
  1145. Num int
  1146. Page int
  1147. Page_size int
  1148. Pages []lib.Page_T
  1149. }
  1150. var r_jsons R_JSONS
  1151. page, _ := c.GetInt("page")
  1152. Name := c.GetString("Name")
  1153. T_sn := c.GetString("T_sn")
  1154. T_class_id, _ := c.GetInt("T_class_id")
  1155. page_z, _ := c.GetInt("page_z")
  1156. T_pid := admin_r.T_pid
  1157. if T_pid == 0 {
  1158. T_pid, _ = c.GetInt("T_pid")
  1159. }
  1160. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  1161. if err != nil {
  1162. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1163. c.ServeJSON()
  1164. return
  1165. }
  1166. if R_DeviceClass.T_pid != T_pid {
  1167. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1168. c.ServeJSON()
  1169. return
  1170. }
  1171. var cnt int64
  1172. Device_lite, cnt := Device.Read_DeviceSensor_class_ALL_1(T_pid, T_class_id, page, page_z, T_sn, Name, "")
  1173. r_jsons.DeviceSensor_lite = Device_lite
  1174. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1175. r_jsons.Page = page
  1176. r_jsons.Page_size = int(page_size)
  1177. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1178. r_jsons.Num = int(cnt)
  1179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1180. c.ServeJSON()
  1181. return
  1182. }
  1183. func (c *DeviceController) ClassBind_Add() {
  1184. // 验证登录
  1185. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1186. if !b_ {
  1187. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1188. c.ServeJSON()
  1189. return
  1190. }
  1191. T_class_id, _ := c.GetInt("T_class_id")
  1192. T_sn := c.GetString("T_sn")
  1193. T_id, _ := c.GetInt("T_id")
  1194. T_pid := admin_r.T_pid
  1195. if T_pid == 0 {
  1196. T_pid, _ = c.GetInt("T_pid")
  1197. }
  1198. if T_class_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1199. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1200. c.ServeJSON()
  1201. return
  1202. }
  1203. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1204. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1205. c.ServeJSON()
  1206. return
  1207. }
  1208. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_class_id)
  1209. if err != nil {
  1210. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1211. c.ServeJSON()
  1212. return
  1213. }
  1214. if R_DeviceClass.T_pid != T_pid {
  1215. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1216. c.ServeJSON()
  1217. return
  1218. }
  1219. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_class_1(T_sn, T_id, T_class_id)
  1220. if len(DeviceSensor_class_list) > 0 {
  1221. c.Data["json"] = lib.JSONS{Code: 203, Msg: "重复绑定!"}
  1222. c.ServeJSON()
  1223. return
  1224. }
  1225. if err = Device.DeviceSensor_T_Class_Add(T_sn, T_id, T_class_id); err != nil {
  1226. c.Data["json"] = lib.JSONS{Code: 200, Msg: "添加失败"}
  1227. c.ServeJSON()
  1228. return
  1229. }
  1230. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "分类绑定操作", strconv.Itoa(T_class_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
  1231. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1232. c.ServeJSON()
  1233. return
  1234. }
  1235. func (c *DeviceController) ClassBind_Del() {
  1236. // 验证登录
  1237. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1238. if !b_ {
  1239. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1240. c.ServeJSON()
  1241. return
  1242. }
  1243. T_class_id, _ := c.GetInt("T_class_id")
  1244. T_sn := c.GetString("T_sn")
  1245. T_id, _ := c.GetInt("T_id")
  1246. T_pid := admin_r.T_pid
  1247. if T_pid == 0 {
  1248. T_pid, _ = c.GetInt("T_pid")
  1249. }
  1250. if T_class_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1251. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1252. c.ServeJSON()
  1253. return
  1254. }
  1255. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1256. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1257. c.ServeJSON()
  1258. return
  1259. }
  1260. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_class_id)
  1261. if err != nil {
  1262. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1263. c.ServeJSON()
  1264. return
  1265. }
  1266. if R_DeviceClass.T_pid != T_pid {
  1267. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1268. c.ServeJSON()
  1269. return
  1270. }
  1271. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_class_1(T_sn, T_id, T_class_id)
  1272. if len(DeviceSensor_class_list) == 0 {
  1273. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1274. c.ServeJSON()
  1275. return
  1276. }
  1277. if err = Device.DeviceSensor_Bind_T_Class_Del(T_sn, T_id, T_class_id); err != nil {
  1278. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"}
  1279. c.ServeJSON()
  1280. return
  1281. }
  1282. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "分类删除绑定操作", strconv.Itoa(T_class_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
  1283. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1284. c.ServeJSON()
  1285. return
  1286. }
  1287. // 设备分类绑定二维码
  1288. func (c *DeviceController) Get_DeviceClassId_QRCode() {
  1289. // 验证登录
  1290. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1291. if !b_ {
  1292. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1293. c.ServeJSON()
  1294. return
  1295. }
  1296. T_calss_id := c.GetString("T_class_id")
  1297. if len(T_calss_id) == 0 {
  1298. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_calss_id Err!"}
  1299. c.ServeJSON()
  1300. return
  1301. }
  1302. T_calss_id_str, _ := NatsServer.Wx_GenerateQRCode("-|" + lib.AesEncryptCBC(T_calss_id, "0123456789012345") + "|- @宝智达 微信公众号通知")
  1303. if len(T_calss_id_str) == 0 {
  1304. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Err!"}
  1305. c.ServeJSON()
  1306. return
  1307. }
  1308. type Text struct {
  1309. QR string
  1310. Code string
  1311. }
  1312. T_calss_id_str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + T_calss_id_str
  1313. Text_r := Text{
  1314. QR: T_calss_id_str,
  1315. Code: "请将本内容发送到 深圳市宝智达科技有限公司 微信公众号-|" + lib.AesEncryptCBC(T_calss_id, "0123456789012345") + "|- @宝智达 微信公众号通知 ",
  1316. }
  1317. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Text_r}
  1318. c.ServeJSON()
  1319. return
  1320. }
  1321. func (c *DeviceController) Get_DeviceClassId_QRCode2() {
  1322. // 验证登录
  1323. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1324. if !b_ {
  1325. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1326. c.ServeJSON()
  1327. return
  1328. }
  1329. T_calss_id := c.GetString("T_class_id")
  1330. if len(T_calss_id) == 0 {
  1331. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_calss_id Err!"}
  1332. c.ServeJSON()
  1333. return
  1334. }
  1335. T_calss_id_str, _ := NatsServer.Wx_GenerateQRCode2("-|" + lib.AesEncryptCBC(T_calss_id, "0123456789012345") + "|- @宝智达 微信公众号通知")
  1336. if len(T_calss_id_str) == 0 {
  1337. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Err!"}
  1338. c.ServeJSON()
  1339. return
  1340. }
  1341. type Text struct {
  1342. QR string
  1343. Code string
  1344. }
  1345. T_calss_id_str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + T_calss_id_str
  1346. Text_r := Text{
  1347. QR: T_calss_id_str,
  1348. Code: "请将本内容发送到 宝智达冷链科技有限公司 微信公众号-|" + lib.AesEncryptCBC(T_calss_id, "0123456789012345") + "|- @宝智达 微信公众号通知 ",
  1349. }
  1350. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Text_r}
  1351. c.ServeJSON()
  1352. return
  1353. }
  1354. // 设备通知 ------------------------------------------
  1355. func (c *DeviceController) CompanyNotice_List() {
  1356. // 验证登录
  1357. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1358. if !b_ {
  1359. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1360. c.ServeJSON()
  1361. return
  1362. }
  1363. type R_JSONS struct {
  1364. //必须的大写开头
  1365. Data []Company.CompanyNotice_R
  1366. Num int64
  1367. Page int
  1368. Page_size int
  1369. }
  1370. var r_jsons R_JSONS
  1371. page, _ := c.GetInt("page")
  1372. println(page)
  1373. if page < 1 {
  1374. page = 1
  1375. }
  1376. page_z, _ := c.GetInt("page_z")
  1377. if page_z < 1 {
  1378. page_z = conf.Page_size
  1379. }
  1380. T_name := c.GetString("T_name")
  1381. T_pid := admin_r.T_pid
  1382. if T_pid == 0 {
  1383. T_pid, _ = c.GetInt("T_pid")
  1384. }
  1385. r_jsons.Data, r_jsons.Num = Company.Read_CompanyNotice_List(T_pid, T_name, page, page_z)
  1386. r_jsons.Page = page
  1387. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1388. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1389. c.ServeJSON()
  1390. return
  1391. }
  1392. func (c *DeviceController) CompanyNotice_All() {
  1393. // 验证登录
  1394. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1395. if !b_ {
  1396. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1397. c.ServeJSON()
  1398. return
  1399. }
  1400. type R_JSONS struct {
  1401. //必须的大写开头
  1402. Data []Company.CompanyNotice_R
  1403. Num int64
  1404. Page int
  1405. Page_size int
  1406. }
  1407. var r_jsons R_JSONS
  1408. T_name := c.GetString("T_name")
  1409. T_pid := admin_r.T_pid
  1410. if T_pid == 0 {
  1411. T_pid, _ = c.GetInt("T_pid")
  1412. }
  1413. r_jsons.Data = Company.Read_CompanyNotice_All(T_pid, T_name)
  1414. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1415. c.ServeJSON()
  1416. return
  1417. }
  1418. func (c *DeviceController) CompanyNotice_Get() {
  1419. // 验证登录
  1420. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1421. if !b_ {
  1422. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1423. c.ServeJSON()
  1424. return
  1425. }
  1426. id, _ := c.GetInt("T_id")
  1427. T_pid := admin_r.T_pid
  1428. if T_pid == 0 {
  1429. T_pid, _ = c.GetInt("T_pid")
  1430. }
  1431. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1432. if err != nil {
  1433. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1434. c.ServeJSON()
  1435. return
  1436. }
  1437. if R_DeviceNotice.T_pid != T_pid {
  1438. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1439. c.ServeJSON()
  1440. return
  1441. }
  1442. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyNoticeToCompanyNotice_R(R_DeviceNotice)}
  1443. c.ServeJSON()
  1444. return
  1445. }
  1446. func (c *DeviceController) CompanyNotice_Add() {
  1447. // 验证登录
  1448. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1449. if !b_ {
  1450. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1451. c.ServeJSON()
  1452. return
  1453. }
  1454. T_name := c.GetString("T_name")
  1455. T_Notice_wx := c.GetString("T_Notice_wx")
  1456. T_Notice_wx2 := c.GetString("T_Notice_wx2")
  1457. T_Notice_phone := c.GetString("T_Notice_phone")
  1458. T_Notice_message := c.GetString("T_Notice_message")
  1459. T_Notice_mailbox := c.GetString("T_Notice_mailbox")
  1460. T_Notice_mechanism := c.GetString("T_Notice_mechanism")
  1461. T_pid := admin_r.T_pid
  1462. if T_pid == 0 {
  1463. T_pid, _ = c.GetInt("T_pid")
  1464. }
  1465. t_c := Company.CompanyNotice{
  1466. T_pid: T_pid,
  1467. T_name: T_name,
  1468. T_Notice_wx: T_Notice_wx,
  1469. T_Notice_wx2: T_Notice_wx2,
  1470. T_Notice_phone: T_Notice_phone,
  1471. T_Notice_message: T_Notice_message,
  1472. T_Notice_mailbox: T_Notice_mailbox,
  1473. T_Notice_mechanism: T_Notice_mechanism,
  1474. T_State: 1,
  1475. }
  1476. Id, err := Company.Add_CompanyNotice(t_c)
  1477. if err != nil {
  1478. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  1479. c.ServeJSON()
  1480. return
  1481. }
  1482. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "通知添加", t_c)
  1483. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  1484. c.ServeJSON()
  1485. return
  1486. }
  1487. func (c *DeviceController) CompanyNotice_Edit() {
  1488. // 验证登录
  1489. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1490. if !b_ {
  1491. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1492. c.ServeJSON()
  1493. return
  1494. }
  1495. id, _ := c.GetInt("T_id")
  1496. T_name := c.GetString("T_name")
  1497. T_Notice_wx := c.GetString("T_Notice_wx")
  1498. T_Notice_wx2 := c.GetString("T_Notice_wx2")
  1499. T_Notice_phone := c.GetString("T_Notice_phone")
  1500. T_Notice_message := c.GetString("T_Notice_message")
  1501. T_Notice_mailbox := c.GetString("T_Notice_mailbox")
  1502. T_Notice_mechanism := c.GetString("T_Notice_mechanism")
  1503. T_pid := admin_r.T_pid
  1504. if T_pid == 0 {
  1505. T_pid, _ = c.GetInt("T_pid")
  1506. }
  1507. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1508. if err != nil {
  1509. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1510. c.ServeJSON()
  1511. return
  1512. }
  1513. if R_DeviceNotice.T_pid != T_pid {
  1514. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1515. c.ServeJSON()
  1516. return
  1517. }
  1518. R_DeviceNotice.Id = id
  1519. if len(T_name) > 0 {
  1520. R_DeviceNotice.T_name = T_name
  1521. }
  1522. if len(T_Notice_wx) > 0 {
  1523. R_DeviceNotice.T_Notice_wx = T_Notice_wx
  1524. }
  1525. if len(T_Notice_wx2) > 0 {
  1526. R_DeviceNotice.T_Notice_wx2 = T_Notice_wx2
  1527. }
  1528. if len(T_Notice_phone) > 0 {
  1529. R_DeviceNotice.T_Notice_phone = T_Notice_phone
  1530. }
  1531. if len(T_Notice_message) > 0 {
  1532. R_DeviceNotice.T_Notice_message = T_Notice_message
  1533. }
  1534. if len(T_Notice_mailbox) > 0 {
  1535. R_DeviceNotice.T_Notice_mailbox = T_Notice_mailbox
  1536. }
  1537. if len(T_Notice_mechanism) > 0 {
  1538. R_DeviceNotice.T_Notice_mechanism = T_Notice_mechanism
  1539. }
  1540. if is := Company.Update_CompanyNotice(R_DeviceNotice, "T_name"); !is {
  1541. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  1542. c.ServeJSON()
  1543. return
  1544. }
  1545. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "通知修改", R_DeviceNotice)
  1546. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1547. c.ServeJSON()
  1548. return
  1549. }
  1550. func (c *DeviceController) CompanyNotice_Del() {
  1551. // 验证登录
  1552. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1553. if !b_ {
  1554. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1555. c.ServeJSON()
  1556. return
  1557. }
  1558. id, _ := c.GetInt("T_id")
  1559. T_pid := admin_r.T_pid
  1560. if T_pid == 0 {
  1561. T_pid, _ = c.GetInt("T_pid")
  1562. }
  1563. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1564. if err != nil {
  1565. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1566. c.ServeJSON()
  1567. return
  1568. }
  1569. if R_DeviceNotice.T_pid != T_pid {
  1570. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1571. c.ServeJSON()
  1572. return
  1573. }
  1574. if is := Company.Delete_CompanyNotice_ById(id); !is {
  1575. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除失败"}
  1576. c.ServeJSON()
  1577. return
  1578. }
  1579. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Notice_Id(id)
  1580. for _, v := range DeviceSensor_class_list {
  1581. Device.DeviceSensor_Bind_T_Notice_Del(v.T_sn, v.T_id, id)
  1582. }
  1583. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "通知删除", strconv.Itoa(id))
  1584. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1585. c.ServeJSON()
  1586. return
  1587. }
  1588. // 设备通知绑定
  1589. func (c *DeviceController) DeviceNoticeBind_List() {
  1590. // 验证登录
  1591. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1592. if !b_ {
  1593. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1594. c.ServeJSON()
  1595. return
  1596. }
  1597. type R_JSONS struct {
  1598. //必须的大写开头
  1599. DeviceSensor_lite []Device.DeviceSensor_R
  1600. Num int
  1601. Page int
  1602. Page_size int
  1603. Pages []lib.Page_T
  1604. }
  1605. var r_jsons R_JSONS
  1606. page, _ := c.GetInt("page")
  1607. Name := c.GetString("Name")
  1608. T_sn := c.GetString("T_sn")
  1609. T_notice_id, _ := c.GetInt("T_notice_id")
  1610. page_z, _ := c.GetInt("page_z")
  1611. T_pid := admin_r.T_pid
  1612. if T_pid == 0 {
  1613. T_pid, _ = c.GetInt("T_pid")
  1614. }
  1615. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1616. if err != nil {
  1617. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1618. c.ServeJSON()
  1619. return
  1620. }
  1621. if R_DeviceNotice.T_pid != T_pid {
  1622. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1623. c.ServeJSON()
  1624. return
  1625. }
  1626. var cnt int64
  1627. Device_lite, cnt := Device.Read_DeviceSensor_Notice_ALL_1(T_pid, T_notice_id, page, page_z, T_sn, Name, "")
  1628. r_jsons.DeviceSensor_lite = Device_lite
  1629. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1630. r_jsons.Page = page
  1631. r_jsons.Page_size = int(page_size)
  1632. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1633. r_jsons.Num = int(cnt)
  1634. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1635. c.ServeJSON()
  1636. return
  1637. }
  1638. func (c *DeviceController) NoticeBind_Add() {
  1639. // 验证登录
  1640. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1641. if !b_ {
  1642. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1643. c.ServeJSON()
  1644. return
  1645. }
  1646. T_notice_id, _ := c.GetInt("T_notice_id")
  1647. T_sn := c.GetString("T_sn")
  1648. T_id, _ := c.GetInt("T_id")
  1649. T_pid := admin_r.T_pid
  1650. if T_pid == 0 {
  1651. T_pid, _ = c.GetInt("T_pid")
  1652. }
  1653. if T_notice_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1654. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1655. c.ServeJSON()
  1656. return
  1657. }
  1658. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1659. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1660. c.ServeJSON()
  1661. return
  1662. }
  1663. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1664. if err != nil {
  1665. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1666. c.ServeJSON()
  1667. return
  1668. }
  1669. if R_DeviceClass.T_pid != T_pid {
  1670. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1671. c.ServeJSON()
  1672. return
  1673. }
  1674. DeviceSensor_Notice_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
  1675. if len(DeviceSensor_Notice_list) > 0 {
  1676. c.Data["json"] = lib.JSONS{Code: 203, Msg: "重复绑定!"}
  1677. c.ServeJSON()
  1678. return
  1679. }
  1680. if err = Company.Add_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
  1681. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
  1682. c.ServeJSON()
  1683. return
  1684. }
  1685. if err = Device.DeviceSensor_T_Notice_Add(T_sn, T_id, T_notice_id); err != nil {
  1686. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
  1687. c.ServeJSON()
  1688. return
  1689. }
  1690. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "通知绑定操作", strconv.Itoa(T_notice_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
  1691. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1692. c.ServeJSON()
  1693. return
  1694. }
  1695. func (c *DeviceController) NoticeBind_Del() {
  1696. // 验证登录
  1697. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1698. if !b_ {
  1699. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1700. c.ServeJSON()
  1701. return
  1702. }
  1703. T_notice_id, _ := c.GetInt("T_notice_id")
  1704. T_sn := c.GetString("T_sn")
  1705. T_id, _ := c.GetInt("T_id")
  1706. T_pid := admin_r.T_pid
  1707. if T_pid == 0 {
  1708. T_pid, _ = c.GetInt("T_pid")
  1709. }
  1710. if T_notice_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1711. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1712. c.ServeJSON()
  1713. return
  1714. }
  1715. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1716. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1717. c.ServeJSON()
  1718. return
  1719. }
  1720. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1721. if err != nil {
  1722. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1723. c.ServeJSON()
  1724. return
  1725. }
  1726. if R_DeviceClass.T_pid != T_pid {
  1727. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1728. c.ServeJSON()
  1729. return
  1730. }
  1731. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
  1732. if len(DeviceSensor_class_list) == 0 {
  1733. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1734. c.ServeJSON()
  1735. return
  1736. }
  1737. if err = Company.Delete_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
  1738. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
  1739. c.ServeJSON()
  1740. return
  1741. }
  1742. if err = Device.DeviceSensor_Bind_T_Notice_Del(T_sn, T_id, T_notice_id); err != nil {
  1743. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
  1744. c.ServeJSON()
  1745. return
  1746. }
  1747. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "通知删除绑定操作", strconv.Itoa(T_notice_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
  1748. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1749. c.ServeJSON()
  1750. return
  1751. }
  1752. // 设备日志 ------------------------------------------
  1753. func (c *DeviceController) DeviceLogs() {
  1754. type R_JSONS struct {
  1755. //必须的大写开头
  1756. Data []Device.DeviceLogs
  1757. Num int64
  1758. Page int
  1759. Page_size int
  1760. }
  1761. var r_jsons R_JSONS
  1762. page, _ := c.GetInt("page")
  1763. println(page)
  1764. if page < 1 {
  1765. page = 1
  1766. }
  1767. page_z, _ := c.GetInt("page_z")
  1768. if page_z < 1 {
  1769. page_z = conf.Page_size
  1770. }
  1771. T_sn := c.GetString("T_sn")
  1772. r_jsons.Data, r_jsons.Num = Device.Read_V2DeviceLogs_ALL(T_sn, page, page_z)
  1773. r_jsons.Page = page
  1774. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1775. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1776. c.ServeJSON()
  1777. return
  1778. }