eba75dc84e26f5a4314eeb881a4b82dbd0a64ffd.svn-base 3.7 KB

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