user.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package restful
  2. import (
  3. "github.com/pkg/errors"
  4. )
  5. type User struct {
  6. Id int `xorm:"not null pk autoincr INT(10)"`
  7. Username string `xorm:"default '''' comment('账号') VARCHAR(50)"`
  8. Password string `xorm:"default '''' comment('密码') VARCHAR(50)"`
  9. Empid int `xorm:"default -1 INT(11)"`
  10. Sort int `xorm:"default 0 INT(11)"`
  11. Enable int `xorm:"default 1 TINYINT(4)"`
  12. CreatedOn int `xorm:"default NULL comment('创建时间') INT(11)"`
  13. ModifiedOn int `xorm:"default NULL comment('更新时间') INT(11)"`
  14. DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"`
  15. // Imei string `xorm:"imei"`
  16. }
  17. /*
  18. type UserRole struct {
  19. Id int `xorm:"not null pk autoincr INT(10)"`
  20. UserId int `xorm:"default NULL comment('用户ID') INT(11)"`
  21. RoleId int `xorm:"default NULL comment('角色ID') INT(11)"`
  22. DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"`
  23. }
  24. type Role struct {
  25. Id int `xorm:"not null pk autoincr INT(10)"`
  26. Name string `xorm:"default '''' comment('名字') VARCHAR(50)"`
  27. Sort int `xorm:"default 0 INT(11)"`
  28. Enable int `xorm:"default 1 TINYINT(4)"`
  29. CreatedOn int `xorm:"default NULL comment('创建时间') INT(11)"`
  30. ModifiedOn int `xorm:"default NULL comment('更新时间') INT(11)"`
  31. DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"`
  32. }
  33. */
  34. func CheckUser(username, password string) (bool, error) {
  35. has, err := Engine.Exist(&User{Username: username, Password: password})
  36. return has, err
  37. }
  38. func GetUserInfo(username string) map[string]interface{} {
  39. valuesMap, err := Engine.SQL("SELECT * FROM `user` WHERE `username` = ? and `enable` >0 ", username).Query().List()
  40. if err != nil {
  41. return nil
  42. }
  43. if len(valuesMap) > 0 {
  44. valuesMap2, err := Engine.SQL("SELECT r.`name`,r.`id` FROM `user` u JOIN `role` r ON r.id=u.`roleid` WHERE r.`enable`>0 AND u.username=?", username).Query().List()
  45. if err == nil {
  46. valuesMap[0]["role"] = valuesMap2
  47. }
  48. return valuesMap[0]
  49. }
  50. return nil
  51. }
  52. func CheckUserFace(username, imei string, typein int) error {
  53. tx := Engine.NewSession()
  54. err := tx.Begin()
  55. defer func() {
  56. switch {
  57. case err != nil:
  58. if tx != nil {
  59. tx.Rollback()
  60. }
  61. default:
  62. if tx != nil {
  63. err = tx.Commit()
  64. }
  65. }
  66. if tx != nil {
  67. tx.Close()
  68. }
  69. }()
  70. valuesMap1, err := tx.SQL("SELECT * FROM `tmr` WHERE `imei` = ?", imei).Query().List()
  71. if err != nil {
  72. return err
  73. }
  74. if len(valuesMap1) == 0 {
  75. return errors.New("TMR设备不存在")
  76. }
  77. valuesMap, err := tx.SQL("SELECT * FROM `user` WHERE `username` = ? and `enable` >0 and pastureid = (SELECT pastureid FROM tmr WHERE imei = ?) ", username, imei).Query().List()
  78. if err != nil {
  79. return err
  80. }
  81. if len(valuesMap) == 0 {
  82. return errors.New("用户不存在")
  83. }
  84. if len(valuesMap) > 0 && len(valuesMap1) > 0 {
  85. _, err := Engine.SQL(`INSERT INTO emprecord(pastureid,imei,empid,empcode,empname,TYPE,recordtime)
  86. SELECT tmr.pastureid,tmr.imei, driver.id,driver.drivercode,driver.drivername,?,NOW() FROM tmr
  87. JOIN driver ON driver.drivercode=? AND driver.pastureid=tmr.pastureid AND tmr.imei=?`, typein, username, imei).Query().List()
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. return err
  94. }
  95. /*
  96. func GetByName(username string) (User, error) {
  97. user := &User{Username:username}
  98. has, err := engine.Get(user)
  99. var valuesMap1 = make(map[string]string)
  100. engine.Find(&User{Username: username,Password:password})
  101. has, err := db.Sql("select * from article where id=?", 2).Get(&valuesMap1)
  102. has, err := engine.Exist(&User{Username: username})
  103. err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users)
  104. has, err := engine.Where("Username = ?", username).Get(&user)
  105. return has, err
  106. }*/
  107. // func CheckImei(username, imei string) (bool, error) {
  108. // has, err := Engine.Exist(&User{Username: username, Imei: imei})
  109. // return has, err
  110. // }