i2bill_user_model.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package model
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "git.i2edu.net/i2/i2-bill-erp/transform"
  9. "git.i2edu.net/i2/i2-bill-erp/transformclient"
  10. "github.com/xormplus/xorm"
  11. "git.i2edu.net/i2/go-zero/core/stores/cache"
  12. "git.i2edu.net/i2/go-zero/core/stores/sqlc"
  13. "git.i2edu.net/i2/go-zero/core/stores/sqlx"
  14. "git.i2edu.net/i2/go-zero/core/stringx"
  15. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/builderx"
  16. )
  17. var (
  18. UserFieldNames = builderx.RawFieldNames(&User{})
  19. UserRows = strings.Join(UserFieldNames, ",")
  20. UserRowsExpectAutoSet = strings.Join(stringx.Remove(UserFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
  21. UserRowsWithPlaceHolder = strings.Join(stringx.Remove(UserFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
  22. cacheUserIdPrefix = "cache:user:id:"
  23. cacheUserUsernamePrefix = "cache:user:username:"
  24. cacheUserWeiXinOpenIdPrefix = "cache:user:weixin_openid:"
  25. )
  26. type (
  27. Cache interface {
  28. SetCache(key string, v interface{}) error
  29. GetCache(key string, v interface{}) error
  30. SetWithExpire(key string, v interface{}, expire time.Duration) error
  31. }
  32. UserModel interface {
  33. Insert(data User) (sql.Result, error)
  34. FindOne(id int64) (*User, error)
  35. FindOneByUsername(username string) (*User, error)
  36. FindOneByWeiXinOpenId(openid string) (*User, error)
  37. Update(data User) error
  38. Delete(id int64) error
  39. CacheStorage() Cache
  40. }
  41. defaultUserModel struct {
  42. sqlc.CachedConn
  43. table string
  44. }
  45. User struct {
  46. Mobile string `db:"mobile"`
  47. Avatar string `db:"avatar"`
  48. WeixinOpenid string `db:"weixin_openid"`
  49. Password string `db:"password"`
  50. Birthday int64 `db:"birthday"`
  51. RegisterTime int64 `db:"register_time"`
  52. LastLoginTime int64 `db:"last_login_time"`
  53. Nickname string `db:"nickname"`
  54. Id int64 `db:"id"`
  55. ErpId string `db:"erp_id"`
  56. Username string `db:"username"`
  57. Gender int64 `db:"gender"`
  58. UserLevelId int64 `db:"user_level_id"`
  59. RegisterIp string `db:"register_ip"`
  60. LastLoginIp string `db:"last_login_ip"`
  61. }
  62. )
  63. func NewUserModel(conn sqlx.SqlConn, c cache.CacheConf) UserModel {
  64. return &defaultUserModel{
  65. CachedConn: sqlc.NewConn(conn, c),
  66. table: "`i2bill_user`",
  67. }
  68. }
  69. type UserXorm struct {
  70. Mobile string `json:"mobile"`
  71. Avatar string `json:"avatar"`
  72. WeixinOpenid string `json:"weixin_openid"`
  73. Password string `json:"password"`
  74. Birthday int64 `json:"birthday"`
  75. RegisterTime int64 `json:"register_time"`
  76. LastLoginTime int64 `json:"last_login_time"`
  77. Nickname string `json:"nickname"`
  78. Id int64 `json:"id"`
  79. ErpId string `json:"erp_id"`
  80. Username string `json:"username"`
  81. Gender int64 `json:"gender"`
  82. UserLevelId int64 `json:"user_level_id"`
  83. RegisterIp string `json:"register_ip"`
  84. LastLoginIp string `json:"last_login_ip"`
  85. }
  86. func (t *UserXorm) TableName() string {
  87. return "i2bill_user"
  88. }
  89. func (m *defaultUserModel) Insert(data User) (sql.Result, error) {
  90. userUsernameKey := fmt.Sprintf("%s%v", cacheUserUsernamePrefix, data.Username)
  91. ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  92. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, UserRowsExpectAutoSet)
  93. return conn.Exec(query, data.Mobile, data.Avatar, data.WeixinOpenid, data.Password, data.Birthday, data.RegisterTime, data.LastLoginTime, data.Nickname, data.Username, data.Gender, data.UserLevelId, data.RegisterIp, data.LastLoginIp)
  94. }, userUsernameKey)
  95. return ret, err
  96. }
  97. func (m *defaultUserModel) FindOne(id int64) (*User, error) {
  98. userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
  99. var resp User
  100. err := m.QueryRow(&resp, userIdKey, func(conn sqlx.SqlConn, v interface{}) error {
  101. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", UserRows, m.table)
  102. return conn.QueryRow(v, query, id)
  103. })
  104. switch err {
  105. case nil:
  106. return &resp, nil
  107. case sqlc.ErrNotFound:
  108. return nil, ErrNotFound
  109. default:
  110. return nil, err
  111. }
  112. }
  113. func (m *defaultUserModel) FindOneByWeiXinOpenId(id string) (*User, error) {
  114. userIdKey := fmt.Sprintf("%s%v", cacheUserWeiXinOpenIdPrefix, id)
  115. var resp User
  116. err := m.QueryRow(&resp, userIdKey, func(conn sqlx.SqlConn, v interface{}) error {
  117. query := fmt.Sprintf("select %s from %s where `weixin_openid` = ? limit 1", UserRows, m.table)
  118. return conn.QueryRow(v, query, id)
  119. })
  120. switch err {
  121. case nil:
  122. return &resp, nil
  123. case sqlc.ErrNotFound:
  124. return nil, ErrNotFound
  125. default:
  126. return nil, err
  127. }
  128. }
  129. func (m *defaultUserModel) FindOneByUsername(username string) (*User, error) {
  130. userUsernameKey := fmt.Sprintf("%s%v", cacheUserUsernamePrefix, username)
  131. var resp User
  132. err := m.QueryRowIndex(&resp, userUsernameKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
  133. query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", UserRows, m.table)
  134. if err := conn.QueryRow(&resp, query, username); err != nil {
  135. return nil, err
  136. }
  137. return resp.Id, nil
  138. }, m.queryPrimary)
  139. switch err {
  140. case nil:
  141. return &resp, nil
  142. case sqlc.ErrNotFound:
  143. return nil, ErrNotFound
  144. default:
  145. return nil, err
  146. }
  147. }
  148. func (m *defaultUserModel) Update(data User) error {
  149. userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)
  150. userUsernameKey := fmt.Sprintf("%s%v", cacheUserUsernamePrefix, data.Username)
  151. _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  152. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, UserRowsWithPlaceHolder)
  153. return conn.Exec(query, data.Mobile, data.Avatar, data.WeixinOpenid, data.Password, data.Birthday, data.RegisterTime, data.LastLoginTime, data.Nickname, data.Username, data.Gender, data.UserLevelId, data.RegisterIp, data.LastLoginIp, data.Id)
  154. }, userIdKey, userUsernameKey)
  155. return err
  156. }
  157. func (m *defaultUserModel) Delete(id int64) error {
  158. data, err := m.FindOne(id)
  159. if err != nil {
  160. return err
  161. }
  162. userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)
  163. userUsernameKey := fmt.Sprintf("%s%v", cacheUserUsernamePrefix, data.Username)
  164. _, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  165. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  166. return conn.Exec(query, id)
  167. }, userUsernameKey, userIdKey)
  168. return err
  169. }
  170. func (m *defaultUserModel) formatPrimary(primary interface{}) string {
  171. return fmt.Sprintf("%s%v", cacheUserIdPrefix, primary)
  172. }
  173. func (m *defaultUserModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
  174. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", UserRows, m.table)
  175. return conn.QueryRow(v, query, primary)
  176. }
  177. func (m *defaultUserModel) SetCache(key string, v interface{}) error {
  178. return m.CachedConn.SetCache(key, v)
  179. }
  180. func (m *defaultUserModel) GetCache(key string, v interface{}) error {
  181. err := m.CachedConn.GetCache(key, v)
  182. if err == sql.ErrNoRows {
  183. err = ErrRdsNotFound
  184. }
  185. return err
  186. }
  187. func (m *defaultUserModel) SetWithExpire(key string, v interface{}, expire time.Duration) error {
  188. return m.CachedConn.SetWithExpire(key, v, expire)
  189. }
  190. func (m *defaultUserModel) CacheStorage() Cache {
  191. return m
  192. }
  193. //获取用户信息
  194. func GetI2bilUserInfo(userId int64, engine *xorm.Engine) (*UserXorm, error) {
  195. user := new(UserXorm)
  196. _, err := engine.SQL("select * from i2bill_user where id = ?", userId).Get(user)
  197. if err != nil {
  198. return nil, err
  199. }
  200. return user, nil
  201. }
  202. func GetAcquirePerm(userId int64, rpcClient transformclient.Transform, engine *xorm.Engine, ctx context.Context) (*transform.GetErpRoleRes, error) {
  203. var res = new(transform.GetErpRoleRes)
  204. partUser, err := GetPartTimeXormByUserId(userId, engine)
  205. if err != nil {
  206. return nil, err
  207. }
  208. if partUser.Id != 0 && partUser.MkId != "" {
  209. res, err = GetErpUser("", partUser.MkId, rpcClient, ctx)
  210. if err != nil {
  211. return nil, err
  212. }
  213. }
  214. user, err := GetI2bilUserInfo(userId, engine)
  215. if err != nil {
  216. return nil, err
  217. }
  218. resMobile, err := GetErpUser(user.Mobile, "", rpcClient, ctx)
  219. if err != nil {
  220. return nil, err
  221. }
  222. if resMobile != nil && resMobile.UserId != "" {
  223. res = resMobile
  224. }
  225. return res, nil
  226. }
  227. type AcquirePermInfo struct {
  228. *transform.GetErpRoleRes
  229. Role int64 // 1 兼职,2mk
  230. }
  231. func GetAcquirePermInfo(userId int64, rpcClient transformclient.Transform, engine *xorm.Engine, ctx context.Context) (*AcquirePermInfo, error) {
  232. //mk
  233. user, err := GetI2bilUserInfo(userId, engine)
  234. if err != nil {
  235. return nil, err
  236. }
  237. erpInfo, err := GetErpUser(user.Mobile, "", rpcClient, ctx)
  238. if err != nil {
  239. return nil, err
  240. }
  241. if erpInfo != nil && erpInfo.UserId != "" {
  242. return &AcquirePermInfo{erpInfo, 2}, nil
  243. }
  244. //兼职
  245. partUser, err := GetPartTimeXormByUserId(userId, engine)
  246. if err != nil {
  247. return nil, err
  248. }
  249. if partUser.Id != 0 && partUser.MkId != "" {
  250. erpInfo, err := GetErpUser("", partUser.MkId, rpcClient, ctx)
  251. if err != nil {
  252. return nil, err
  253. }
  254. if erpInfo != nil && erpInfo.UserId != "" {
  255. return &AcquirePermInfo{erpInfo, 1}, nil
  256. }
  257. }
  258. return nil, nil
  259. }
  260. func GetErpUser(mobile string, erpUserId string, rpcClient transformclient.Transform, ctx context.Context) (*transform.GetErpRoleRes, error) {
  261. in := new(transform.GetErpRoleReq)
  262. in.Mobile = mobile
  263. in.UserId = erpUserId
  264. erpRoles, err := rpcClient.GetErpRole(ctx, in)
  265. if err != nil {
  266. return nil, err
  267. }
  268. if erpRoles.UserId != "" {
  269. roles := strings.Split(erpRoles.Role, ",")
  270. for _, r := range roles {
  271. if r == "LAMK" || r == "LAMKM" || r == "student/readingStudent" {
  272. return erpRoles, nil
  273. }
  274. }
  275. }
  276. return nil, nil
  277. }
  278. //获取校区权限
  279. func GetUserSchoolPerm(erpUserId string, rpcClient transformclient.Transform, engine *xorm.Engine, ctx context.Context) ([]*transform.OrganSchool, error) {
  280. in := erpUserId
  281. result, err := rpcClient.GetErpOrganSchPerByUserId(ctx, &transform.GetErpOrganSchPerByUserIdReq{UserId: in})
  282. if err != nil {
  283. return nil, err
  284. }
  285. return result.School, nil
  286. }