package restful import ( "github.com/pkg/errors" ) type User struct { Id int `xorm:"not null pk autoincr INT(10)"` Username string `xorm:"default '''' comment('账号') VARCHAR(50)"` Password string `xorm:"default '''' comment('密码') VARCHAR(50)"` Empid int `xorm:"default -1 INT(11)"` Sort int `xorm:"default 0 INT(11)"` Enable int `xorm:"default 1 TINYINT(4)"` CreatedOn int `xorm:"default NULL comment('创建时间') INT(11)"` ModifiedOn int `xorm:"default NULL comment('更新时间') INT(11)"` DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"` // Imei string `xorm:"imei"` } /* type UserRole struct { Id int `xorm:"not null pk autoincr INT(10)"` UserId int `xorm:"default NULL comment('用户ID') INT(11)"` RoleId int `xorm:"default NULL comment('角色ID') INT(11)"` DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"` } type Role struct { Id int `xorm:"not null pk autoincr INT(10)"` Name string `xorm:"default '''' comment('名字') VARCHAR(50)"` Sort int `xorm:"default 0 INT(11)"` Enable int `xorm:"default 1 TINYINT(4)"` CreatedOn int `xorm:"default NULL comment('创建时间') INT(11)"` ModifiedOn int `xorm:"default NULL comment('更新时间') INT(11)"` DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"` } */ func CheckUser(username, password string) (bool, error) { has, err := Engine.Exist(&User{Username: username, Password: password}) return has, err } func GetUserInfo(username string) map[string]interface{} { valuesMap, err := Engine.SQL("SELECT * FROM `user` WHERE `username` = ? and `enable` >0 ", username).Query().List() if err != nil { return nil } if len(valuesMap) > 0 { 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() if err == nil { valuesMap[0]["role"] = valuesMap2 } return valuesMap[0] } return nil } func CheckUserFace(username, imei string, typein int) error { tx := Engine.NewSession() err := tx.Begin() defer func() { switch { case err != nil: if tx != nil { tx.Rollback() } default: if tx != nil { err = tx.Commit() } } if tx != nil { tx.Close() } }() valuesMap1, err := tx.SQL("SELECT * FROM `tmr` WHERE `imei` = ?", imei).Query().List() if err != nil { return err } if len(valuesMap1) == 0 { return errors.New("TMR设备不存在") } valuesMap, err := tx.SQL("SELECT * FROM `user` WHERE `username` = ? and `enable` >0 and pastureid = (SELECT pastureid FROM tmr WHERE imei = ?) ", username, imei).Query().List() if err != nil { return err } if len(valuesMap) == 0 { return errors.New("用户不存在") } if len(valuesMap) > 0 && len(valuesMap1) > 0 { _, err := Engine.SQL(`INSERT INTO emprecord(pastureid,imei,empid,empcode,empname,TYPE,recordtime) SELECT tmr.pastureid,tmr.imei, driver.id,driver.drivercode,driver.drivername,?,NOW() FROM tmr JOIN driver ON driver.drivercode=? AND driver.pastureid=tmr.pastureid AND tmr.imei=?`, typein, username, imei).Query().List() if err != nil { return err } return nil } return err } /* func GetByName(username string) (User, error) { user := &User{Username:username} has, err := engine.Get(user) var valuesMap1 = make(map[string]string) engine.Find(&User{Username: username,Password:password}) has, err := db.Sql("select * from article where id=?", 2).Get(&valuesMap1) has, err := engine.Exist(&User{Username: username}) err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users) has, err := engine.Where("Username = ?", username).Get(&user) return has, err }*/ // func CheckImei(username, imei string) (bool, error) { // has, err := Engine.Exist(&User{Username: username, Imei: imei}) // return has, err // }