123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- package Account
- import (
- "ColdP_server/conf"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/astaxie/beego/cache"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- "log"
- "strconv"
- "strings"
- "time"
- )
- type ColdpUser struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_uuid string `orm:"size(256);null"` // 用户编号
- T_pid int `orm:"size(200);null"` // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
- T_pids string `orm:"size(200);null"` // 绑定公司管理 Pid| 如 P1|P2
- T_power int `orm:"size(2);default(0)"` // 权限 (关联权限表)
- T_user string `orm:"size(256);null"` // 用户名 (唯一)
- T_pass string `orm:"size(256);null"` // MD5
- T_name string `orm:"size(256);null"` // 姓名
- T_phone string `orm:"size(256);null"` // 电话
- T_mail string `orm:"size(200);null"` // 邮箱
- T_wx string `orm:"size(256);null"` // 微信
- T_State int `orm:"size(2);default(1)"` // 0禁用 1 正常
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
- }
- type ColdpUser_R struct {
- Id int
- T_uuid string // 用户编号
- T_pid int // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
- T_pids string // 绑定公司管理 Pid| 如 P1|P2
- T_power int // 权限 (关联权限表)
- T_user string // 用户名 (唯一)
- T_name string // 姓名
- T_phone string // 电话
- T_mail string // 邮箱
- T_wx string // 微信
- T_State int // 0删除 1 正常
- T_Company string //公司名称
- }
- type AddUser struct {
- T_name string // 姓名
- T_user string
- T_State string
- T_pid string
- T_pass string
- }
- type UpdateUser struct {
- Id string
- T_name string // 姓名
- T_user string
- T_State string
- T_pid string
- T_pass string
- }
- func ColdpUserToColdpUser_R(r ColdpUser) (v ColdpUser_R) {
- v.T_uuid = r.T_uuid
- v.T_pid = r.T_pid
- v.T_pids = r.T_pids
- v.T_power = r.T_power
- v.T_user = r.T_user
- v.T_name = r.T_name
- v.T_phone = r.T_phone
- v.T_mail = r.T_mail
- v.T_wx = r.T_wx
- v.T_State = r.T_State
- v.Id = r.Id
- return v
- }
- type ColdpUCompany struct {
- Id int
- T_name string
- }
- func (t *ColdpUser) TableName() string {
- return "cold_user" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_ColdpUser cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(ColdpUser))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_ColdPColdpUser", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_ColdpUser, err = cache.NewCache("redis", config)
- if err != nil || redisCache_ColdpUser == nil {
- errMsg := "failed to init redis"
- fmt.Println(errMsg, err)
- }
- }
- func RedisColdpUserGet(key string) (r ColdpUser, is bool) {
- if redisCache_Admin.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_ColdpUser.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- //println("没有 找到key:",key)
- return ColdpUser{}, false
- }
- func RedisColdpUserSet(r ColdpUser) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- fmt.Print(err)
- return
- }
- err = redisCache_ColdpUser.Put(r.T_uuid, str, 24*time.Hour)
- if err != nil {
- fmt.Println("set key:", r.T_uuid, ",value:", str, err)
- }
- return
- }
- func ReadAdminLoginVerification(T_user string, T_pass string) (error, ColdpUser) {
- o := orm.NewOrm()
- r := ColdpUser{T_user: T_user, T_pass: T_pass, T_State: 1}
- err := o.Read(&r, "t_user", "t_pass", "t__state") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- }
- RedisColdpUserSet(r) // Redis 更新缓存
- return err, r
- }
- // AddColdpUser 添加用户
- func AddColdpUser(m ColdpUser) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- fmt.Println(err)
- }
- m.Id = int(id)
- RedisColdpUserSet(m)
- return id, err
- }
- // GetCompanyList 获取公司列表
- func GetCompanyList(serch string) ([]ColdpUCompany, error) {
- var comoany []ColdpUCompany
- o := orm.NewOrm()
- var sql string
- log.Println(serch)
- if len(serch) == 0 {
- sql = "SELECT id,t_name FROM `company`"
- } else {
- sprintf := fmt.Sprint("%" + serch + "%")
- sql = fmt.Sprintf("SELECT id,t_name FROM `company` WHERE t_name LIKE '%s'", sprintf)
- }
- rows, err := o.Raw(sql).QueryRows(&comoany)
- if err != nil {
- return nil, errors.New("查询失败")
- }
- if rows == 0 {
- return nil, errors.New("没有数据")
- }
- return comoany, nil
- }
- func Read_ColdUser_List(T_pid int, T_name string, page int, page_z int) (AdminList []ColdpUser_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ColdpUser))
- var maps []ColdpUser
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- //cond1 := cond.And("T_State", 1)
- var cond1 *orm.Condition
- if T_pid > 0 {
- cond1 = cond.AndCond(cond.And("T_pid", T_pid))
- }
- if len(T_name) > 0 {
- cond1 = cond.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&maps)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- for i, v := range maps {
- r, err := Read_Company_ById(v.T_pid)
- if err != nil {
- //logs.Error(lib.FuncName(), err)
- }
- AdminList = append(AdminList, ColdpUserToColdpUser_R(v))
- AdminList[i].T_Company = r
- }
- return AdminList, cnt
- }
- func ReadColdpUserByUuid(T_uuid string) (r ColdpUser, err error) {
- if r, is := RedisColdpUserGet(T_uuid); is {
- //println("Redis_Get OK")
- return r, nil
- }
- o := orm.NewOrm()
- //redis 保存方式 uuid|公司pid
- arr := strings.Split(T_uuid, "|")
- r = ColdpUser{T_uuid: arr[0], T_State: 1}
- err = o.Read(&r, "T_uuid", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- }
- //设置为redis中存储的pid
- pid, _ := strconv.ParseInt(arr[1], 10, 64)
- r.T_pid = int(pid)
- return r, err
- }
- func Read_Company_ById(Id int) (r string, e error) {
- o := orm.NewOrm()
- sql := fmt.Sprintf("SELECT t_name FROM `company` WHERE id = %d", Id)
- err := o.Raw(sql).QueryRow(&r)
- if err != nil {
- //logs.Error(lib.FuncName(), e)
- return "", errors.New("查询失败")
- }
- return r, e
- }
- func Read_Company_ByUser(user string) bool {
- o := orm.NewOrm()
- var count int64
- sql := fmt.Sprintf("SELECT count(*) FROM `cold_user` WHERE t_user = '%v'", user)
- err := o.Raw(sql).QueryRow(&count)
- if err != nil {
- //logs.Error(lib.FuncName(), e)
- return false
- }
- if count > 0 {
- return false
- }
- return true
- }
- func UpdateByIdState(m ColdpUser, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- return true
- }
- return false
- }
- func GetByIdUser(userID string) (error, ColdpUser) {
- o := orm.NewOrm()
- var user ColdpUser
- sql := fmt.Sprintf("SELECT * FROM `cold_user` WHERE ID = '%v'", userID)
- err := o.Raw(sql).QueryRow(&user)
- if err != nil {
- fmt.Println(err)
- return err, user
- }
- log.Println(user)
- return nil, user
- }
- func DeleteById(m ColdpUser) bool {
- o := orm.NewOrm()
- i, err := o.Delete(&m)
- if err != nil {
- return false
- }
- if i > 0 {
- return true
- }
- return false
- }
|