Property.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package controllers
  2. import (
  3. "ERP_storage/Nats/NatsServer"
  4. "ERP_storage/conf"
  5. "ERP_storage/models/Account"
  6. "ERP_storage/models/Property"
  7. userlibs "git.baozhida.cn/ERP_libs/User"
  8. "git.baozhida.cn/ERP_libs/lib"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "math"
  12. "strconv"
  13. )
  14. type PropertyController struct {
  15. beego.Controller
  16. User userlibs.User
  17. }
  18. func (c *PropertyController) Prepare() {
  19. c.User = *Account.User_r
  20. }
  21. func (c *PropertyController) IotCard_List() {
  22. // 分页参数 初始化
  23. page, _ := c.GetInt("page")
  24. if page < 1 {
  25. page = 1
  26. }
  27. page_z, _ := c.GetInt("page_z")
  28. if page_z < 1 {
  29. page_z = conf.Page_size
  30. }
  31. // 查询
  32. T_name := c.GetString("T_name")
  33. T_state, _ := c.GetInt("T_state")
  34. R_List, R_cnt := Property.Read_IotCard_List(T_name, T_state, page, page_z)
  35. var r_jsons lib.R_JSONS
  36. r_jsons.Num = R_cnt
  37. r_jsons.Data = R_List
  38. r_jsons.Page = page
  39. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  40. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  41. c.ServeJSON()
  42. return
  43. }
  44. func (c *PropertyController) IotCard_Add() {
  45. T_iccid := c.GetString("T_iccid")
  46. T_type := c.GetString("T_type")
  47. T_sn := c.GetString("T_sn")
  48. T_state, _ := c.GetInt("T_state")
  49. var_ := Property.IotCard{
  50. T_iccid: T_iccid,
  51. T_type: T_type,
  52. T_sn: T_sn,
  53. T_State: T_state,
  54. }
  55. iotCard, err := Property.Read_IotCard_ByT_iccid(T_iccid)
  56. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  57. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  58. c.ServeJSON()
  59. return
  60. }
  61. if iotCard.Id > 0 {
  62. c.Data["json"] = lib.JSONS{Code: 202, Msg: "物联网卡号重复!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. o := orm.NewOrm()
  67. IotCardDao := Property.NewIotCard(&o)
  68. Id, err := IotCardDao.Add_IotCard(var_)
  69. if err != nil {
  70. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  71. c.ServeJSON()
  72. return
  73. }
  74. NatsServer.AddUserLogs(c.User.T_uuid, "物联网卡", "添加", var_)
  75. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  76. c.ServeJSON()
  77. return
  78. }
  79. func (c *PropertyController) IotCard_Edit() {
  80. T_id, _ := c.GetInt("T_id")
  81. T_iccid := c.GetString("T_iccid")
  82. T_type := c.GetString("T_type")
  83. T_sn := c.GetString("T_sn")
  84. T_State, _ := c.GetInt("T_State")
  85. IotCard_r, err := Property.Read_IotCard_ById(T_id)
  86. if err != nil {
  87. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  88. c.ServeJSON()
  89. return
  90. }
  91. if len(T_iccid) > 0 {
  92. IotCard_r.T_iccid = T_iccid
  93. }
  94. if len(T_type) > 0 {
  95. IotCard_r.T_type = T_type
  96. }
  97. if len(T_sn) > 0 {
  98. IotCard_r.T_sn = T_sn
  99. }
  100. if T_State > 0 {
  101. IotCard_r.T_State = T_State
  102. }
  103. o := orm.NewOrm()
  104. IotCardDao := Property.NewIotCard(&o)
  105. if err = IotCardDao.Update_IotCard(IotCard_r, "T_iccid", "T_type", "T_sn", "T_State"); err != nil {
  106. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  107. c.ServeJSON()
  108. return
  109. }
  110. NatsServer.AddUserLogs(c.User.T_uuid, "物联网卡", "修改", IotCard_r)
  111. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  112. c.ServeJSON()
  113. return
  114. }
  115. func (c *PropertyController) IotCard_Del() {
  116. T_id, _ := c.GetInt("T_id")
  117. IotCard_r, err := Property.Read_IotCard_ById(T_id)
  118. if err != nil {
  119. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  120. c.ServeJSON()
  121. return
  122. }
  123. if IotCard_r.T_State == 2 {
  124. c.Data["json"] = lib.JSONS{Code: 202, Msg: "已使用物联网卡禁止删除!"}
  125. c.ServeJSON()
  126. return
  127. }
  128. o := orm.NewOrm()
  129. IotCardDao := Property.NewIotCard(&o)
  130. if err = IotCardDao.Delete_IotCard(IotCard_r); err != nil {
  131. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  132. c.ServeJSON()
  133. return
  134. }
  135. NatsServer.AddUserLogs(c.User.T_uuid, "物联网卡", "删除", strconv.Itoa(T_id))
  136. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  137. c.ServeJSON()
  138. return
  139. }