User.go 7.5 KB

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