智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package hdl
  2. import (
  3. enum2 "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. svc3 "applet/app/admin/svc"
  7. "applet/app/db"
  8. model2 "applet/app/db/model"
  9. "applet/app/e"
  10. "github.com/gin-gonic/gin"
  11. "time"
  12. )
  13. func DeviceList(c *gin.Context) {
  14. var req md.DeviceListReq
  15. err := c.ShouldBindJSON(&req)
  16. if err != nil {
  17. err = validate.HandleValidateErr(err)
  18. err1 := err.(e.E)
  19. e.OutErr(c, err1.Code, err1.Error())
  20. return
  21. }
  22. if req.Limit == 0 {
  23. req.Limit = 10
  24. }
  25. if req.Page == 0 {
  26. req.Page = 10
  27. }
  28. sess := db.Db.Desc("device.id")
  29. admin := svc3.GetUser(c)
  30. if admin.IsSuperAdministrator != enum2.IsSuperAdministratorTure {
  31. adminWithEnterpriseDb := db.AdminWithEnterpriseDb{}
  32. adminWithEnterpriseDb.Set()
  33. adminWithEnterprise, err1 := adminWithEnterpriseDb.FindAdminWithEnterprise(admin.AdmId)
  34. if err1 != nil {
  35. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  36. return
  37. }
  38. var enterPriseIds []int
  39. for _, v1 := range *adminWithEnterprise {
  40. enterPriseIds = append(enterPriseIds, v1.EnterpriseId)
  41. }
  42. if len(enterPriseIds) == 0 {
  43. return
  44. }
  45. sess.In("device.enterprise_id", enterPriseIds)
  46. }
  47. if req.Name != "" {
  48. sess.And("device.name like ?", "%"+req.Name+"%")
  49. }
  50. if req.EnterpriseName != "" {
  51. sess.And("enterprise.name like ?", "%"+req.EnterpriseName+"%")
  52. }
  53. if req.StartDate != "" {
  54. sess.And("device.create_at >=?", req.StartDate)
  55. }
  56. if req.EndDate != "" {
  57. sess.And("device.create_at <=?", req.EndDate)
  58. }
  59. var m []*db.DeviceWithEnterprise
  60. total, err := sess.
  61. Join("LEFT", "enterprise", "enterprise.id = device.enterprise_id").Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  62. if err != nil {
  63. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  64. return
  65. }
  66. var resp []interface{}
  67. for _, v := range m {
  68. resp = append(resp, map[string]interface{}{
  69. "id": v.Device.Id,
  70. "name": v.Device.Name,
  71. "device_sn": v.Device.DeviceSn,
  72. "enterprise_id": v.Device.EnterpriseId,
  73. "memo": v.Device.Memo,
  74. "create_at": v.Device.CreateAt,
  75. "update_at": v.Device.UpdateAt,
  76. "enterprise_name": v.Enterprise.Name,
  77. })
  78. }
  79. e.OutSuc(c, map[string]interface{}{
  80. "list": resp,
  81. "total": total,
  82. }, nil)
  83. return
  84. }
  85. func DeviceSave(c *gin.Context) {
  86. var req md.DeviceSaveReq
  87. err := c.ShouldBindJSON(&req)
  88. if err != nil {
  89. err = validate.HandleValidateErr(err)
  90. err1 := err.(e.E)
  91. e.OutErr(c, err1.Code, err1.Error())
  92. return
  93. }
  94. deviceDd := db.Device{}
  95. deviceDd.Set(req.DeviceSn)
  96. //查找 device_sn
  97. device, err := deviceDd.GetDevice()
  98. if err != nil {
  99. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  100. return
  101. }
  102. if device == nil {
  103. insertAffected, err1 := deviceDd.DeviceInsert(&model2.Device{
  104. Name: req.Name,
  105. DeviceSn: req.DeviceSn,
  106. EnterpriseId: req.EnterpriseId,
  107. Memo: req.Memo,
  108. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  109. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  110. })
  111. if err1 != nil {
  112. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  113. return
  114. }
  115. if insertAffected <= 0 {
  116. e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
  117. return
  118. }
  119. } else {
  120. device.EnterpriseId = req.EnterpriseId
  121. device.Name = req.Name
  122. device.Memo = req.Memo
  123. device.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  124. updateAffected, err1 := deviceDd.DeviceUpdate(device, "enterprise_id", "name", "memo", "update_at")
  125. if err1 != nil {
  126. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  127. return
  128. }
  129. if updateAffected <= 0 {
  130. e.OutErr(c, e.ERR_DB_ORM, "更新数据失败")
  131. return
  132. }
  133. }
  134. e.OutSuc(c, "success", nil)
  135. return
  136. }
  137. func DeviceDelete(c *gin.Context) {
  138. deviceSn := c.Param("device_sn")
  139. deviceDd := db.Device{}
  140. deviceDd.Set(deviceSn)
  141. deviceDdAffected, err := deviceDd.DeviceDelete()
  142. if err != nil {
  143. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  144. return
  145. }
  146. if deviceDdAffected <= 0 {
  147. e.OutErr(c, e.ERR_DB_ORM, "删除数据失败")
  148. return
  149. }
  150. e.OutSuc(c, "success", nil)
  151. return
  152. }
  153. func FactoryDeviceSave(c *gin.Context) {
  154. var req md.FactoryDeviceSaveReq
  155. err := c.ShouldBindJSON(&req)
  156. if err != nil {
  157. err = validate.HandleValidateErr(err)
  158. err1 := err.(e.E)
  159. e.OutErr(c, err1.Code, err1.Error())
  160. return
  161. }
  162. deviceDd := db.FactoryDevice{}
  163. deviceDd.Set(req.TerminalId)
  164. //查找 device_sn
  165. device, err := deviceDd.GetFactoryDevice()
  166. if err != nil {
  167. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  168. return
  169. }
  170. if device == nil {
  171. insertAffected, err1 := deviceDd.FactoryDeviceInsert(&model2.FactoryDevice{
  172. Name: req.Name,
  173. TerminalId: req.TerminalId,
  174. EnterpriseId: req.EnterpriseId,
  175. Memo: req.Memo,
  176. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  177. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  178. })
  179. if err1 != nil {
  180. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  181. return
  182. }
  183. if insertAffected <= 0 {
  184. e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
  185. return
  186. }
  187. } else {
  188. device.EnterpriseId = req.EnterpriseId
  189. device.Name = req.Name
  190. device.Memo = req.Memo
  191. device.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  192. updateAffected, err1 := deviceDd.FactoryDeviceUpdate(device, "enterprise_id", "name", "memo", "update_at")
  193. if err1 != nil {
  194. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  195. return
  196. }
  197. if updateAffected <= 0 {
  198. e.OutErr(c, e.ERR_DB_ORM, "更新数据失败")
  199. return
  200. }
  201. }
  202. e.OutSuc(c, "success", nil)
  203. return
  204. }
  205. func FactoryDeviceList(c *gin.Context) {
  206. var req md.FactoryDeviceListReq
  207. err := c.ShouldBindJSON(&req)
  208. if err != nil {
  209. err = validate.HandleValidateErr(err)
  210. err1 := err.(e.E)
  211. e.OutErr(c, err1.Code, err1.Error())
  212. return
  213. }
  214. if req.Limit == 0 {
  215. req.Limit = 10
  216. }
  217. if req.Page == 0 {
  218. req.Page = 10
  219. }
  220. sess := db.Db.Desc("factory_device.id")
  221. if req.Name != "" {
  222. sess.And("factory_device.name like ?", "%"+req.Name+"%")
  223. }
  224. if req.EnterpriseName != "" {
  225. sess.And("enterprise.name like ?", "%"+req.EnterpriseName+"%")
  226. }
  227. if req.StartDate != "" {
  228. sess.And("factory_device.create_at >=?", req.StartDate)
  229. }
  230. if req.EndDate != "" {
  231. sess.And("factory_device.create_at <=?", req.EndDate)
  232. }
  233. var m []*db.FactoryDeviceWithEnterprise
  234. total, err := sess.
  235. Join("LEFT", "enterprise", "enterprise.id = factory_device.enterprise_id").Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  236. if err != nil {
  237. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  238. return
  239. }
  240. var resp []interface{}
  241. for _, v := range m {
  242. resp = append(resp, map[string]interface{}{
  243. "id": v.FactoryDevice.Id,
  244. "name": v.FactoryDevice.Name,
  245. "terminal_id": v.FactoryDevice.TerminalId,
  246. "enterprise_id": v.FactoryDevice.EnterpriseId,
  247. "memo": v.FactoryDevice.Memo,
  248. "create_at": v.FactoryDevice.CreateAt,
  249. "update_at": v.FactoryDevice.UpdateAt,
  250. "enterprise_name": v.Enterprise.Name,
  251. })
  252. }
  253. e.OutSuc(c, map[string]interface{}{
  254. "list": resp,
  255. "total": total,
  256. }, nil)
  257. return
  258. }
  259. func FactoryDeviceDelete(c *gin.Context) {
  260. deviceSn := c.Param("terminal_id")
  261. deviceDd := db.FactoryDevice{}
  262. deviceDd.Set(deviceSn)
  263. deviceDdAffected, err := deviceDd.FactoryDeviceDelete()
  264. if err != nil {
  265. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  266. return
  267. }
  268. if deviceDdAffected <= 0 {
  269. e.OutErr(c, e.ERR_DB_ORM, "删除数据失败")
  270. return
  271. }
  272. e.OutSuc(c, "success", nil)
  273. return
  274. }