Admin.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. package Account
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. "github.com/beego/beego/v2/core/logs"
  12. _ "github.com/go-sql-driver/mysql"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18. type Admin struct {
  19. Id int `orm:"column(ID);size(11);auto;pk"`
  20. T_uuid string `orm:"size(256);null"` // 用户编号
  21. T_pid int `orm:"size(200);null"` // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
  22. T_pids string `orm:"type(text);null"` // 绑定公司管理 Pid| 如 P1|P2
  23. T_power int `orm:"size(20);default(0)"` // 权限 (关联权限表)
  24. T_user string `orm:"size(256);null"` // 用户名 (唯一)
  25. T_pass string `orm:"size(256);null"` // MD5
  26. T_name string `orm:"size(256);null"` // 姓名
  27. T_phone string `orm:"size(256);null"` // 电话
  28. T_mail string `orm:"size(200);null"` // 邮箱
  29. T_wx string `orm:"type(text);null"` // 微信
  30. T_State int `orm:"size(2);default(1)"` // 0删除 1 正常
  31. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  32. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  33. }
  34. type Admin_R struct {
  35. T_uuid string // 用户编号
  36. T_pid int // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
  37. T_pids string // 绑定公司管理 Pid| 如 P1|P2
  38. T_power int // 权限 (关联权限表)
  39. T_power_name string // 权限名称
  40. T_user string // 用户名 (唯一)
  41. T_pass string // MD5,密码
  42. T_name string // 姓名
  43. T_phone string // 电话
  44. T_mail string // 邮箱
  45. T_wx string // 微信
  46. }
  47. type Admin_ struct {
  48. T_name string // 姓名
  49. T_phone string // 电话
  50. }
  51. func AdminToAdmin_R(r Admin) (v Admin_R) {
  52. v.T_uuid = r.T_uuid
  53. v.T_pid = r.T_pid
  54. v.T_pids = r.T_pids
  55. v.T_power = r.T_power
  56. v.T_power_name = Read_Power_Get(r.T_power)
  57. v.T_user = r.T_user
  58. v.T_pass = ""
  59. v.T_name = r.T_name
  60. v.T_phone = r.T_phone
  61. v.T_mail = r.T_mail
  62. v.T_wx = r.T_wx
  63. return v
  64. }
  65. func Admin_RToAdmin_(r Admin_R) (v Admin_) {
  66. v.T_name = r.T_name
  67. v.T_phone = r.T_phone
  68. return v
  69. }
  70. func (t *Admin) TableName() string {
  71. return "admin" // 数据库名称 // ************** 替换 FormulaList **************
  72. }
  73. var redisCache_Admin cache.Cache
  74. var AdminMap *sync.Map // 泛型
  75. func init() {
  76. //注册模型
  77. orm.RegisterModel(new(Admin))
  78. AdminMap = new(sync.Map)
  79. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  80. "redis_User_Admin", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  81. fmt.Println(config)
  82. var err error
  83. redisCache_Admin, err = cache.NewCache("redis", config)
  84. if err != nil || redisCache_Admin == nil {
  85. errMsg := "failed to init redis"
  86. logs.Error(errMsg, err)
  87. panic(errMsg)
  88. }
  89. }
  90. // ---------------- Redis -------------------
  91. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  92. func Redis_Admin_Set(r Admin) (err error) {
  93. //json序列化
  94. str, err := json.Marshal(r)
  95. if err != nil {
  96. logs.Error(lib.FuncName(), err)
  97. return
  98. }
  99. err = redisCache_Admin.Put(r.T_uuid, str, 24*time.Hour)
  100. if err != nil {
  101. logs.Error("Redis_Admin_Set", "set key:", r.T_uuid, ",value:", str, err)
  102. }
  103. return
  104. }
  105. // if r,is :=Redis_Get(T_sn);is{
  106. // return r,nil
  107. // }
  108. func Redis_Admin_Get(key string) (r Admin, is bool) {
  109. if redisCache_Admin.IsExist(key) {
  110. //println("找到key:",key)
  111. v := redisCache_Admin.Get(key)
  112. err := json.Unmarshal(v.([]byte), &r)
  113. if err != nil {
  114. logs.Error(lib.FuncName(), err)
  115. return Admin{}, false
  116. }
  117. return r, true
  118. }
  119. //println("没有 找到key:",key)
  120. return Admin{}, false
  121. }
  122. func Redis_Admin_DelK(key string) (err error) {
  123. err = redisCache_Admin.Delete(key)
  124. if err != nil {
  125. logs.Error(lib.FuncName(), err)
  126. }
  127. return
  128. }
  129. // ---------------- 特殊方法 -------------------
  130. // 添加
  131. func Add_Admin(m Admin) (id int64, err error) {
  132. o := orm.NewOrm()
  133. id, err = o.Insert(&m)
  134. if err != nil {
  135. logs.Error(lib.FuncName(), err)
  136. return id, err
  137. }
  138. m.Id = int(id)
  139. Redis_Admin_Set(m)
  140. return id, err
  141. }
  142. //// 修改
  143. //func Update_Delete_Admin_ByT_uuid(T_uuid string) (err error) {
  144. // o := orm.NewOrm()
  145. // v := Admin{T_uuid: T_uuid}
  146. // // ascertain id exists in the database
  147. // if err = o.Read(&v, "T_uuid"); err == nil {
  148. // var num int64
  149. // v.T_State = 0
  150. //
  151. // if num, err = o.Update(&v, "T_State"); err == nil {
  152. // logs.Info("Number of records updated in database:", num)
  153. // }
  154. //
  155. // Redis_Admin_Set(v)
  156. // }
  157. // return err
  158. //}
  159. func Read_Admin_T_user(T_user string) (Admin, error) {
  160. o := orm.NewOrm()
  161. r := Admin{T_user: T_user, T_State: 1}
  162. err := o.Read(&r, "T_user", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  163. if err != nil {
  164. logs.Error(lib.FuncName(), err)
  165. }
  166. return r, err
  167. }
  168. func Read_AdminLogin_verification(T_user string, T_pass string) (error, Admin) {
  169. o := orm.NewOrm()
  170. r := Admin{T_user: T_user, T_pass: T_pass, T_State: 1}
  171. err := o.Read(&r, "T_user", "T_pass", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  172. if err != nil {
  173. logs.Error(lib.FuncName(), err)
  174. return err, r
  175. }
  176. Redis_Admin_Set(r) // Redis 更新缓存
  177. return err, r
  178. }
  179. // 修改
  180. func Update_Admin(m Admin, cols ...string) bool {
  181. o := orm.NewOrm()
  182. _, err := o.Update(&m, cols...)
  183. if err != nil {
  184. logs.Error(lib.FuncName(), err)
  185. return false
  186. }
  187. Redis_Admin_Set(m) // Redis 更新缓存
  188. return true
  189. }
  190. // 删除
  191. func Delete_Admin(v Admin) bool {
  192. o := orm.NewOrm()
  193. v.T_State = 0
  194. _, err := o.Update(&v, "T_State")
  195. if err != nil {
  196. logs.Error(lib.FuncName(), err)
  197. return false
  198. }
  199. Redis_Admin_DelK(v.T_uuid) // Redis 更新缓存
  200. return true
  201. }
  202. // 获取 ById
  203. func Read_Admin_ById(Id int) (r Admin, err error) {
  204. o := orm.NewOrm()
  205. r = Admin{Id: Id, T_State: 1}
  206. err = o.Read(&r, "Id", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  207. if err != nil {
  208. logs.Error(lib.FuncName(), err)
  209. }
  210. return r, err
  211. }
  212. // 获取 ByT_uuid
  213. func Read_Admin_ByUuid(T_uuid string) (r Admin, err error) {
  214. if r, is := Redis_Admin_Get(T_uuid); is {
  215. //println("Redis_Get OK")
  216. return r, nil
  217. }
  218. o := orm.NewOrm()
  219. r = Admin{T_uuid: T_uuid, T_State: 1}
  220. err = o.Read(&r, "T_uuid", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  221. if err != nil {
  222. logs.Error(lib.FuncName(), err)
  223. }
  224. Redis_Admin_Set(r)
  225. return r, err
  226. }
  227. // 获取列表
  228. func Read_Admin_List(T_pid int, T_name string, page int, page_z int) (AdminList []Admin_R, cnt int64) {
  229. o := orm.NewOrm()
  230. // 也可以直接使用 Model 结构体作为表名
  231. qs := o.QueryTable(new(Admin))
  232. var maps []Admin
  233. var offset int64
  234. if page <= 1 {
  235. offset = 0
  236. } else {
  237. offset = int64((page - 1) * page_z)
  238. }
  239. cond := orm.NewCondition()
  240. cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
  241. if len(T_name) > 0 {
  242. cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
  243. }
  244. var err error
  245. if page_z == 9999 {
  246. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&maps)
  247. } else {
  248. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&maps)
  249. }
  250. if err != nil {
  251. logs.Error(lib.FuncName(), err)
  252. }
  253. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  254. if err != nil {
  255. logs.Error(lib.FuncName(), err)
  256. }
  257. for _, v := range maps {
  258. AdminList = append(AdminList, AdminToAdmin_R(v))
  259. }
  260. return AdminList, cnt
  261. }
  262. // 获取所有管理员用户列表
  263. func Read_Admin_List_All() (AdminList []Admin_R) {
  264. o := orm.NewOrm()
  265. // 也可以直接使用 Model 结构体作为表名
  266. qs := o.QueryTable(new(Admin))
  267. var maps []Admin
  268. cond := orm.NewCondition()
  269. cond1 := cond.And("T_State", 1).And("T_pid", 0).AndNot("T_pids", "")
  270. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  271. if err != nil {
  272. logs.Error(lib.FuncName(), err)
  273. return AdminList
  274. }
  275. for _, v := range maps {
  276. AdminList = append(AdminList, AdminToAdmin_R(v))
  277. }
  278. return AdminList
  279. }
  280. func Read_Admin_List_T_pids(T_pid int) (AdminList []Admin_R) {
  281. o := orm.NewOrm()
  282. // 也可以直接使用 Model 结构体作为表名
  283. qs := o.QueryTable(new(Admin))
  284. var maps []Admin
  285. cond := orm.NewCondition()
  286. cond1 := cond.And("T_State", 1).And("T_pid", 0).And("T_pids__icontains", "P"+strconv.Itoa(T_pid)+"|")
  287. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  288. if err != nil {
  289. logs.Error(lib.FuncName(), err)
  290. return AdminList
  291. }
  292. for _, v := range maps {
  293. AdminList = append(AdminList, AdminToAdmin_R(v))
  294. }
  295. return AdminList
  296. }
  297. func Read_Admin_List_T_pid(T_pid int) (AdminList []Admin_R) {
  298. o := orm.NewOrm()
  299. // 也可以直接使用 Model 结构体作为表名
  300. qs := o.QueryTable(new(Admin))
  301. var maps []Admin
  302. cond := orm.NewCondition()
  303. cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
  304. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  305. if err != nil {
  306. logs.Error(lib.FuncName(), err)
  307. return AdminList
  308. }
  309. for _, v := range maps {
  310. AdminList = append(AdminList, AdminToAdmin_R(v))
  311. }
  312. return AdminList
  313. }
  314. func Read_Parent_Admin_List(T_pid []int) (AdminList []Admin_R) {
  315. o := orm.NewOrm()
  316. // 也可以直接使用 Model 结构体作为表名
  317. qs := o.QueryTable(new(Admin))
  318. var maps []Admin
  319. cond := orm.NewCondition()
  320. cond1 := cond.And("T_State", 1).And("T_pid__in", T_pid)
  321. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  322. if err != nil {
  323. logs.Error(lib.FuncName(), err)
  324. return AdminList
  325. }
  326. for _, v := range maps {
  327. AdminList = append(AdminList, AdminToAdmin_R(v))
  328. }
  329. return AdminList
  330. }
  331. func Read_Admin_Num_ByT_power(T_power int) int {
  332. o := orm.NewOrm()
  333. // 也可以直接使用 Model 结构体作为表名
  334. qs := o.QueryTable(new(Admin))
  335. cnt, err := qs.Filter("T_State", 1).Filter("T_power", T_power).Count()
  336. if err != nil {
  337. logs.Error(lib.FuncName(), err)
  338. return 0
  339. }
  340. return int(cnt)
  341. }
  342. // 查询 用户关联的公司总数
  343. func Read_Admin_Num_ByT_pid(T_pids []int) int {
  344. if len(T_pids) == 0 {
  345. return 0
  346. }
  347. o := orm.NewOrm()
  348. // 也可以直接使用 Model 结构体作为表名
  349. qs := o.QueryTable(new(Admin))
  350. cnt, err := qs.Filter("T_State", 1).Filter("T_pid__in", T_pids).Count()
  351. if err != nil {
  352. logs.Error(lib.FuncName(), err)
  353. return 0
  354. }
  355. return int(cnt)
  356. }
  357. // 获取所有用户列表
  358. func Read_User_List_All() (AdminList []Admin_R) {
  359. o := orm.NewOrm()
  360. // 也可以直接使用 Model 结构体作为表名
  361. qs := o.QueryTable(new(Admin))
  362. var maps []Admin
  363. _, err := qs.All(&maps)
  364. if err != nil {
  365. logs.Error(lib.FuncName(), err)
  366. return AdminList
  367. }
  368. for _, v := range maps {
  369. AdminList = append(AdminList, AdminToAdmin_R(v))
  370. }
  371. return AdminList
  372. }
  373. func Add_Company_bind(Admin_r Admin, T_pids []int) (err error) {
  374. o := orm.NewOrm()
  375. for _, T_pid := range T_pids {
  376. T_Pid_bind := "P" + strconv.Itoa(T_pid) + "|"
  377. Admin_r.T_pids = strings.Replace(Admin_r.T_pids, T_Pid_bind, "", -1)
  378. Admin_r.T_pids = Admin_r.T_pids + T_Pid_bind
  379. }
  380. _, err = o.Update(&Admin_r, "T_pids")
  381. if err != nil {
  382. logs.Error(lib.FuncName(), err)
  383. return err
  384. }
  385. Redis_Admin_Set(Admin_r) // Redis 更新缓存
  386. return nil
  387. }
  388. func Delete_Company_bind(Admin_r Admin, T_pids []int) (err error) {
  389. o := orm.NewOrm()
  390. for _, T_pid := range T_pids {
  391. T_Pid_bind := "P" + strconv.Itoa(T_pid) + "|"
  392. Admin_r.T_pids = strings.Replace(Admin_r.T_pids, T_Pid_bind, "", -1)
  393. }
  394. _, err = o.Update(&Admin_r, "T_pids")
  395. if err != nil {
  396. logs.Error(lib.FuncName(), err)
  397. return err
  398. }
  399. Redis_Admin_Set(Admin_r) // Redis 更新缓存
  400. return nil
  401. }
  402. // 删除公司后将管理员绑定的公司删除
  403. func Delete_Company_bind_T_pids(o orm.Ormer, T_pid int) (err error) {
  404. qs := o.QueryTable(new(Admin))
  405. var maps []Admin
  406. T_Pid_bind := "P" + strconv.Itoa(T_pid) + "|"
  407. cnt, err := qs.Filter("T_State", 1).Filter("T_pids__icontains", T_Pid_bind).All(&maps)
  408. if err != nil {
  409. return err
  410. }
  411. if cnt == 0 {
  412. return nil
  413. }
  414. for _, v := range maps {
  415. if err = o.Read(&v, "Id"); err == nil {
  416. v.T_pids = strings.Replace(v.T_pids, T_Pid_bind, "", -1)
  417. _, err = o.Update(&v, "T_pids")
  418. if err != nil {
  419. logs.Error("Delete_Company_bind_T_pids", v.T_uuid, T_pid, err)
  420. return err
  421. }
  422. Redis_Admin_DelK(v.T_uuid) // Redis 更新缓存
  423. }
  424. }
  425. return err
  426. }
  427. func Read_Admin_All_Map(A []Admin_R) {
  428. for _, v := range A {
  429. AdminMap.Store(v.T_uuid, v)
  430. }
  431. }
  432. func Read_Admin_T_name_Get(T_uuid string) string {
  433. // 有先加入 给全部人发消息
  434. v, ok := AdminMap.Load(T_uuid) /*如果确定是真实的,则存在,否则不存在 */
  435. if ok {
  436. return v.(Admin_R).T_name
  437. } else {
  438. return ""
  439. }
  440. }