123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- package Account
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- "github.com/beego/beego/v2/core/logs"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "sync"
- "time"
- )
- type Company struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_mid int `orm:"size(200);null"` // 上一级 ID
- T_name string `orm:"size(256);null"` // 公司名称
- T_key string `orm:"size(256);index;null"` // 公司密钥
- T_type int `orm:"size(256);default(1)"` // 公司类型 1-医药公司 2-运输企业
- T_plan string `orm:"type(text);null"` // 平面图
- T_data string `orm:"type(text);null"` // 大数据
- T_v3d string `orm:"type(text);null"` // 3D 视图
- T_path string `orm:"size(256);null"` // 公司路径 /0/1/5/
- T_money float32 `orm:"digits(12);decimals(2)"`
- T_State int `orm:"size(200);default(1)"` // 0删除 1正常
- T_warning int `orm:"size(20);default(1)"` // 是否处理报警信息 1处理 2不处理
- T_Charging int `orm:"size(11);default(0)"` // 记账扣费 公司ID 默认为:0 (自己)
- 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 第一次保存时才设置时间
- Children []Company `orm:"-"`
- }
- type Company_R struct {
- Id int
- T_mid int // 上一级 ID
- T_name string // 公司名称
- T_plan string // 平面图
- T_data string // 大数据
- T_v3d string // 3D 视图
- T_money float32 // 余额
- T_warning int // 报警统计
- T_key string
- T_type int // 公司类型 1-医药公司 2-运输企业
- T_Charging int
- Children []Company_R
- }
- func CompanyToCompany_R(r Company) (v Company_R) {
- v.Id = r.Id
- v.T_mid = r.T_mid
- v.T_name = r.T_name
- v.T_plan = r.T_plan
- v.T_data = r.T_data
- v.T_v3d = r.T_v3d
- v.T_money = r.T_money
- v.T_warning = r.T_warning
- v.T_key = r.T_key
- v.T_Charging = r.T_Charging
- v.T_type = r.T_type
- return v
- }
- func (t *Company) TableName() string {
- return "company" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_Company cache.Cache
- var Company_map *sync.Map // 泛型
- func init() {
- //注册模型
- Company_map = new(sync.Map)
- //注册模型
- orm.RegisterModel(new(Company))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_User_Company", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_Company, err = cache.NewCache("redis", config)
- if err != nil || redisCache_Company == nil {
- errMsg := "failed to init redis"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // ---------------- Redis -------------------
- func Redis_Company_Set(key string, r Company) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_Company.Put(key, str, 24*time.Hour)
- if err != nil {
- logs.Error("set key:", key, ",value:", str, err)
- }
- return
- }
- func Redis_Company_Get(key string) (r Company, is bool) {
- if redisCache_Company.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_Company.Get(key)
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return Company{}, false
- }
- return r, true
- }
- //println("没有 找到key:",key)
- return Company{}, false
- }
- func Redis_Company_DelK(key string) (err error) {
- err = redisCache_Company.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- // 添加
- func Add_Company(var_ Company) (id int64, err error) {
- o := orm.NewOrm()
- o.Begin()
- var rand_x int64
- for true {
- var_.T_key = lib.GetRandstring(16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", rand_x)
- err = o.Read(&var_, "T_key") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- break
- }
- rand_x++
- }
- id, err = o.Insert(&var_)
- if err != nil {
- o.Rollback()
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- T_Path := "/0/" + strconv.Itoa(int(id)) + "/"
- if var_.T_mid != 0 {
- Company_r, err := Read_Company_ById(var_.T_mid)
- if err != nil {
- o.Rollback()
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- T_Path = Company_r.T_path + strconv.Itoa(int(id)) + "/"
- }
- var_.Id = int(id)
- var_.T_path = T_Path
- _, err = o.Update(&var_, "T_path")
- if err != nil {
- o.Rollback()
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- o.Commit()
- Redis_Company_Set(strconv.Itoa(var_.Id), var_)
- return id, err
- }
- // 修改
- func Update_Company(m Company, cols ...string) bool {
- o := orm.NewOrm()
- num, err := o.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- logs.Info("Number of records updated in database:", num)
- Redis_Company_Set(strconv.Itoa(m.Id), m) // Redis 更新缓存
- return true
- }
- // 删除
- func Delete_Company(orm orm.Ormer, id int) bool {
- var m = Company{
- Id: id,
- T_State: 0,
- }
- num, err := orm.Update(&m, "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- logs.Info("Number of records updated in database:", num)
- Redis_Company_DelK(strconv.Itoa(m.Id))
- return true
- }
- // 获取 ById
- func Read_Company_ById(Id int) (r Company, e error) {
- if r, is := Redis_Company_Get(strconv.Itoa(Id)); is {
- //println("Redis_Get OK")
- return r, nil
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- e = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
- if e != nil {
- logs.Error(lib.FuncName(), e)
- return
- }
- Redis_Company_Set(strconv.Itoa(r.Id), r) // Redis 更新缓存
- return r, e
- }
- func Read_Company_ByKey(key string) (r Company, e error) {
- if r, is := Redis_Company_Get(key); is {
- //println("Redis_Get OK")
- return r, nil
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- e = qs.Filter("T_key", key).Filter("T_State", 1).One(&r)
- if e != nil {
- logs.Error(lib.FuncName(), e)
- return
- }
- Redis_Company_Set(key, r) // Redis 更新缓存
- return r, e
- }
- // 获取列表
- func Read_Company_Tree(admin_r Admin, T_name string) (CompanyList []Company_R) {
- // 内部用户未绑定公司
- if admin_r.T_pid == 0 && len(admin_r.T_pids) == 0 {
- return CompanyList
- }
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Company))
- var maps []Company
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1)
- if admin_r.T_pid > 0 {
- cond1 = cond1.And("T_path__icontains", fmt.Sprintf("/%d/", admin_r.T_pid))
- }
- // 内部用户已绑定公司,* 绑定所有公司
- if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" {
- T_pids := lib.SplitStringToIntIds(admin_r.T_pids, "P")
- if len(T_pids) > 0 {
- cond1 = cond1.And("Id__in", ReadCompanyIds_T_pids(T_pids))
- }
- }
- if len(T_name) > 0 {
- cond1 = cond1.And("T_name__icontains", T_name)
- }
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return CompanyList
- }
- parentMap, flag := getCompanyParent(maps)
- if flag {
- for i := 0; i < len(maps); i++ {
- if parentMap[maps[i].T_mid] {
- continue
- }
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- T_plan: maps[i].T_plan,
- T_data: maps[i].T_data,
- T_v3d: maps[i].T_v3d,
- T_money: maps[i].T_money,
- T_warning: maps[i].T_warning,
- T_Charging: maps[i].T_Charging,
- T_key: maps[i].T_key,
- T_type: maps[i].T_type,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- } else {
- for i := 0; i < len(maps); i++ {
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- T_plan: maps[i].T_plan,
- T_data: maps[i].T_data,
- T_v3d: maps[i].T_v3d,
- T_money: maps[i].T_money,
- T_warning: maps[i].T_warning,
- T_Charging: maps[i].T_Charging,
- T_key: maps[i].T_key,
- T_type: maps[i].T_type,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- }
- return CompanyList
- }
- func ReadCompanyIds_T_pids(T_pids []int) []int {
- var companyIds []int
- if len(T_pids) == 0 {
- return companyIds
- }
- var companyList []Company
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- _, err := qs.Filter("Id__in", T_pids).Filter("T_State", 1).All(&companyList)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return companyIds
- }
- for _, company := range companyList {
- subIds := ReadCompanyIds_T_path(company.T_path)
- companyIds = append(companyIds, subIds...)
- }
- return lib.IntIdsDistinct(companyIds)
- }
- // 通过T_pid查询所有子id
- func ReadCompanyIds_T_path(T_path string) (companyIds []int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- var CompanyList []Company
- _, err := qs.Filter("T_path__startswith", T_path).Filter("T_State", 1).All(&CompanyList)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return companyIds
- }
- for _, v := range CompanyList {
- companyIds = append(companyIds, v.Id)
- }
- return companyIds
- }
- func ReadCompanyWarningIds_T_pids(T_pids []int) []int {
- var companyList []Company
- var companyIds []int
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- if len(T_pids) == 0 {
- return companyIds
- }
- _, err := qs.Filter("Id__in", T_pids).Filter("T_State", 1).Filter("T_warning", 1).All(&companyList)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return companyIds
- }
- for _, company := range companyList {
- subIds := ReadCompanyWarningIds_T_path(company.T_path)
- companyIds = append(companyIds, subIds...)
- }
- return lib.IntIdsDistinct(companyIds)
- }
- // 通过T_pid查询所有子id
- func ReadCompanyWarningIds_T_path(T_path string) (companyIds []int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Company))
- var CompanyList []Company
- _, err := qs.Filter("T_path__startswith", T_path).Filter("T_State", 1).Filter("T_warning", 1).All(&CompanyList)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return companyIds
- }
- for _, v := range CompanyList {
- companyIds = append(companyIds, v.Id)
- }
- return companyIds
- }
- func Read_Company_List(T_name string) (CompanyList []Company_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Company))
- var maps []Company
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1)
- if len(T_name) > 0 {
- cond1 = cond1.And("T_name__icontains", T_name)
- }
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return CompanyList
- }
- parentMap, flag := getCompanyParent(maps)
- if flag {
- for i := 0; i < len(maps); i++ {
- if parentMap[maps[i].T_mid] {
- continue
- }
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- T_plan: maps[i].T_plan,
- T_data: maps[i].T_data,
- T_v3d: maps[i].T_v3d,
- T_money: maps[i].T_money,
- T_warning: maps[i].T_warning,
- T_type: maps[i].T_type,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- } else {
- for i := 0; i < len(maps); i++ {
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- T_plan: maps[i].T_plan,
- T_data: maps[i].T_data,
- T_v3d: maps[i].T_v3d,
- T_money: maps[i].T_money,
- T_warning: maps[i].T_warning,
- T_type: maps[i].T_type,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- }
- return CompanyList
- }
- // 获取运输企业公司列表
- func Read_Transport_Company_List(T_name string) (CompanyList []Company_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Company))
- var maps []Company
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_type", 2)
- if len(T_name) > 0 {
- cond1 = cond1.And("T_name__icontains", T_name)
- }
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return CompanyList
- }
- parentMap, flag := getCompanyParent(maps)
- if flag {
- for i := 0; i < len(maps); i++ {
- if parentMap[maps[i].T_mid] {
- continue
- }
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- T_plan: maps[i].T_plan,
- T_data: maps[i].T_data,
- T_v3d: maps[i].T_v3d,
- T_money: maps[i].T_money,
- T_warning: maps[i].T_warning,
- T_Charging: maps[i].T_Charging,
- T_key: maps[i].T_key,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- } else {
- for i := 0; i < len(maps); i++ {
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- T_plan: maps[i].T_plan,
- T_data: maps[i].T_data,
- T_v3d: maps[i].T_v3d,
- T_money: maps[i].T_money,
- T_warning: maps[i].T_warning,
- T_Charging: maps[i].T_Charging,
- T_key: maps[i].T_key,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- }
- return CompanyList
- }
- func CompanyCall(CompanyList []Company, company Company_R) Company_R {
- list := CompanyList
- min := make([]Company_R, 0)
- for j := 0; j < len(list); j++ {
- if company.Id != list[j].T_mid {
- continue
- }
- mi := Company_R{}
- mi.Id = list[j].Id
- mi.T_mid = list[j].T_mid
- mi.T_name = list[j].T_name
- mi.T_plan = list[j].T_plan
- mi.T_data = list[j].T_data
- mi.T_v3d = list[j].T_v3d
- mi.T_money = list[j].T_money
- mi.T_warning = list[j].T_warning
- mi.T_Charging = list[j].T_Charging
- mi.T_key = list[j].T_key
- mi.T_type = list[j].T_type
- mi.Children = []Company_R{}
- ms := CompanyCall(CompanyList, mi)
- min = append(min, ms)
- }
- company.Children = min
- return company
- }
- func getCompanyParent(CompanyList []Company) (map[int]bool, bool) {
- list := CompanyList
- var flag = false
- var parentMap = map[int]bool{}
- for j := 0; j < len(list); j++ {
- parentMap[list[j].T_mid] = false
- }
- for j := 0; j < len(list); j++ {
- if _, ok := parentMap[list[j].Id]; !ok {
- continue
- }
- parentMap[list[j].Id] = true
- flag = true
- }
- return parentMap, flag
- }
- func Read_Company_List_All_ByT_name(T_name string) (maps []Company) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Company))
- cond := orm.NewCondition()
- if len(T_name) > 0 {
- cond = cond.And("T_name__icontains", T_name)
- }
- _, err := qs.SetCond((*orm2.Condition)(cond)).All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return maps
- }
- return maps
- }
- // 获取内部用户绑定的公司列表
- func Read_Company_List_ByT_pids(T_pids string) (CompanyList []Company_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Company))
- var maps []Company
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1)
- if len(T_pids) == 0 {
- return
- }
- if T_pids == "*" {
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return CompanyList
- }
- } else {
- list := lib.SplitStringIds(T_pids, "P")
- cond1 = cond1.And("Id__in", list)
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return CompanyList
- }
- }
- parentMap, flag := getCompanyParent(maps)
- if flag {
- for i := 0; i < len(maps); i++ {
- if parentMap[maps[i].T_mid] {
- continue
- }
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- } else {
- for i := 0; i < len(maps); i++ {
- r := Company_R{
- Id: maps[i].Id,
- T_mid: maps[i].T_mid,
- T_name: maps[i].T_name,
- Children: nil,
- }
- info := CompanyCall(maps, r)
- CompanyList = append(CompanyList, info)
- }
- }
- return CompanyList
- }
- func Read_Company_All_Maps() {
- o := orm.NewOrm()
- var r []Company
- qs := o.QueryTable(new(Company))
- _, err := qs.All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- for _, v := range r {
- Company_map.Store(v.Id, v.T_name)
- }
- }
- func Read_Company_Get(id int) string {
- v, ok := Company_map.Load(id) /*如果确定是真实的,则存在,否则不存在 */
- if ok {
- return v.(string)
- } else {
- return ""
- }
- }
|