User.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package Account
  2. import (
  3. "ColdP_server/conf"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. "github.com/beego/beego/v2/client/orm"
  9. "log"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type ColdpUser struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_uuid string `orm:"size(256);null"` // 用户编号
  17. T_pid int `orm:"size(200);null"` // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
  18. T_pids string `orm:"size(200);null"` // 绑定公司管理 Pid| 如 P1|P2
  19. T_power int `orm:"size(2);default(0)"` // 权限 (关联权限表)
  20. T_user string `orm:"size(256);null"` // 用户名 (唯一)
  21. T_pass string `orm:"size(256);null"` // MD5
  22. T_name string `orm:"size(256);null"` // 姓名
  23. T_phone string `orm:"size(256);null"` // 电话
  24. T_mail string `orm:"size(200);null"` // 邮箱
  25. T_wx string `orm:"size(256);null"` // 微信
  26. T_State int `orm:"size(2);default(1)"` // 0禁用 1 正常
  27. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  28. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  29. }
  30. type ColdpUser_R struct {
  31. Id int
  32. T_uuid string // 用户编号
  33. T_pid int // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
  34. T_pids string // 绑定公司管理 Pid| 如 P1|P2
  35. T_power int // 权限 (关联权限表)
  36. T_user string // 用户名 (唯一)
  37. T_name string // 姓名
  38. T_phone string // 电话
  39. T_mail string // 邮箱
  40. T_wx string // 微信
  41. T_State int // 0删除 1 正常
  42. T_Company string //公司名称
  43. }
  44. type AddUser struct {
  45. T_name string // 姓名
  46. T_user string
  47. T_State string
  48. T_pid string
  49. T_pass string
  50. }
  51. type UpdateUser struct {
  52. Id string
  53. T_name string // 姓名
  54. T_user string
  55. T_State string
  56. T_pid string
  57. T_pass string
  58. }
  59. func ColdpUserToColdpUser_R(r ColdpUser) (v ColdpUser_R) {
  60. v.T_uuid = r.T_uuid
  61. v.T_pid = r.T_pid
  62. v.T_pids = r.T_pids
  63. v.T_power = r.T_power
  64. v.T_user = r.T_user
  65. v.T_name = r.T_name
  66. v.T_phone = r.T_phone
  67. v.T_mail = r.T_mail
  68. v.T_wx = r.T_wx
  69. v.T_State = r.T_State
  70. v.Id = r.Id
  71. return v
  72. }
  73. type ColdpUCompany struct {
  74. Id int
  75. T_name string
  76. }
  77. func (t *ColdpUser) TableName() string {
  78. return "cold_user" // 数据库名称 // ************** 替换 FormulaList **************
  79. }
  80. var redisCache_ColdpUser cache.Cache
  81. func init() {
  82. //注册模型
  83. orm.RegisterModel(new(ColdpUser))
  84. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  85. "redis_ColdPColdpUser", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  86. fmt.Println(config)
  87. var err error
  88. redisCache_ColdpUser, err = cache.NewCache("redis", config)
  89. if err != nil || redisCache_ColdpUser == nil {
  90. errMsg := "failed to init redis"
  91. fmt.Println(errMsg, err)
  92. }
  93. }
  94. func RedisColdpUserGet(key string) (r ColdpUser, is bool) {
  95. if redisCache_Admin.IsExist(key) {
  96. //println("找到key:",key)
  97. v := redisCache_ColdpUser.Get(key)
  98. json.Unmarshal(v.([]byte), &r)
  99. return r, true
  100. }
  101. //println("没有 找到key:",key)
  102. return ColdpUser{}, false
  103. }
  104. func RedisColdpUserSet(r ColdpUser) (err error) {
  105. //json序列化
  106. str, err := json.Marshal(r)
  107. if err != nil {
  108. fmt.Print(err)
  109. return
  110. }
  111. err = redisCache_ColdpUser.Put(r.T_uuid, str, 24*time.Hour)
  112. if err != nil {
  113. fmt.Println("set key:", r.T_uuid, ",value:", str, err)
  114. }
  115. return
  116. }
  117. func ReadAdminLoginVerification(T_user string, T_pass string) (error, ColdpUser) {
  118. o := orm.NewOrm()
  119. r := ColdpUser{T_user: T_user, T_pass: T_pass, T_State: 1}
  120. err := o.Read(&r, "t_user", "t_pass", "t__state") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  121. if err != nil {
  122. fmt.Println(err)
  123. }
  124. RedisColdpUserSet(r) // Redis 更新缓存
  125. return err, r
  126. }
  127. // AddColdpUser 添加用户
  128. func AddColdpUser(m ColdpUser) (id int64, err error) {
  129. o := orm.NewOrm()
  130. id, err = o.Insert(&m)
  131. if err != nil {
  132. fmt.Println(err)
  133. }
  134. m.Id = int(id)
  135. RedisColdpUserSet(m)
  136. return id, err
  137. }
  138. // GetCompanyList 获取公司列表
  139. func GetCompanyList(serch string) ([]ColdpUCompany, error) {
  140. var comoany []ColdpUCompany
  141. o := orm.NewOrm()
  142. var sql string
  143. log.Println(serch)
  144. if len(serch) == 0 {
  145. sql = "SELECT id,t_name FROM `company`"
  146. } else {
  147. sprintf := fmt.Sprint("%" + serch + "%")
  148. sql = fmt.Sprintf("SELECT id,t_name FROM `company` WHERE t_name LIKE '%s'", sprintf)
  149. }
  150. rows, err := o.Raw(sql).QueryRows(&comoany)
  151. if err != nil {
  152. return nil, errors.New("查询失败")
  153. }
  154. if rows == 0 {
  155. return nil, errors.New("没有数据")
  156. }
  157. return comoany, nil
  158. }
  159. func Read_ColdUser_List(T_pid int, T_name string, page int, page_z int) (AdminList []ColdpUser_R, cnt int64) {
  160. o := orm.NewOrm()
  161. // 也可以直接使用 Model 结构体作为表名
  162. qs := o.QueryTable(new(ColdpUser))
  163. var maps []ColdpUser
  164. var offset int64
  165. if page <= 1 {
  166. offset = 0
  167. } else {
  168. offset = int64((page - 1) * page_z)
  169. }
  170. cond := orm.NewCondition()
  171. //cond1 := cond.And("T_State", 1)
  172. var cond1 *orm.Condition
  173. if T_pid > 0 {
  174. cond1 = cond.AndCond(cond.And("T_pid", T_pid))
  175. }
  176. if len(T_name) > 0 {
  177. cond1 = cond.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
  178. }
  179. qs.Limit(page_z, offset).SetCond((cond1)).OrderBy("Id").All(&maps)
  180. cnt, _ = qs.SetCond((cond1)).Count()
  181. for i, v := range maps {
  182. r, err := Read_Company_ById(v.T_pid)
  183. if err != nil {
  184. //logs.Error(lib.FuncName(), err)
  185. }
  186. AdminList = append(AdminList, ColdpUserToColdpUser_R(v))
  187. AdminList[i].T_Company = r
  188. }
  189. return AdminList, cnt
  190. }
  191. func ReadColdpUserByUuid(T_uuid string) (r ColdpUser, err error) {
  192. if r, is := RedisColdpUserGet(T_uuid); is {
  193. //println("Redis_Get OK")
  194. return r, nil
  195. }
  196. o := orm.NewOrm()
  197. //redis 保存方式 uuid|公司pid
  198. arr := strings.Split(T_uuid, "|")
  199. r = ColdpUser{T_uuid: arr[0], T_State: 1}
  200. err = o.Read(&r, "T_uuid", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  201. if err != nil {
  202. fmt.Println(err)
  203. }
  204. //设置为redis中存储的pid
  205. pid, _ := strconv.ParseInt(arr[1], 10, 64)
  206. r.T_pid = int(pid)
  207. return r, err
  208. }
  209. func Read_Company_ById(Id int) (r string, e error) {
  210. o := orm.NewOrm()
  211. sql := fmt.Sprintf("SELECT t_name FROM `company` WHERE id = %d", Id)
  212. err := o.Raw(sql).QueryRow(&r)
  213. if err != nil {
  214. //logs.Error(lib.FuncName(), e)
  215. return "", errors.New("查询失败")
  216. }
  217. return r, e
  218. }
  219. func Read_Company_ByUser(user string) bool {
  220. o := orm.NewOrm()
  221. var count int64
  222. sql := fmt.Sprintf("SELECT count(*) FROM `cold_user` WHERE t_user = '%v'", user)
  223. err := o.Raw(sql).QueryRow(&count)
  224. if err != nil {
  225. //logs.Error(lib.FuncName(), e)
  226. return false
  227. }
  228. if count > 0 {
  229. return false
  230. }
  231. return true
  232. }
  233. func UpdateByIdState(m ColdpUser, cols ...string) bool {
  234. o := orm.NewOrm()
  235. if num, err := o.Update(&m, cols...); err == nil {
  236. fmt.Println("Number of records updated in database:", num)
  237. return true
  238. }
  239. return false
  240. }
  241. func GetByIdUser(userID string) (error, ColdpUser) {
  242. o := orm.NewOrm()
  243. var user ColdpUser
  244. sql := fmt.Sprintf("SELECT * FROM `cold_user` WHERE ID = '%v'", userID)
  245. err := o.Raw(sql).QueryRow(&user)
  246. if err != nil {
  247. fmt.Println(err)
  248. return err, user
  249. }
  250. log.Println(user)
  251. return nil, user
  252. }
  253. func DeleteById(m ColdpUser) bool {
  254. o := orm.NewOrm()
  255. i, err := o.Delete(&m)
  256. if err != nil {
  257. return false
  258. }
  259. if i > 0 {
  260. return true
  261. }
  262. return false
  263. }