123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package Nats
- import (
- "ERP_project/conf"
- "ERP_project/models/Account"
- "fmt"
- "github.com/astaxie/beego/logs"
- "github.com/beego/beego/v2/adapter/orm"
- "github.com/nats-io/nats.go"
- "github.com/vmihailenco/msgpack/v5"
- menulibs "gogs.baozhida.cn/zoie/ERP_libs/Menu"
- powerlibs "gogs.baozhida.cn/zoie/ERP_libs/Power"
- "gogs.baozhida.cn/zoie/ERP_libs/lib"
- )
- var Nats *nats.Conn
- func init() {
- fmt.Println("============Nats init============")
- var err error
- // 连接Nats服务器
- Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
- if err != nil {
- fmt.Println("nats 连接失败!")
- panic(err)
- }
- fmt.Println("nats OK!")
- go NatsInit()
- }
- func NatsInit() {
- // 发布-订阅 模式,异步订阅 test1
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Read_Menu_List", func(m *nats.Msg) {
- var t_R lib.JSONS
- o := orm.NewOrm()
- MenuDao := menulibs.NewMenu(o)
- menu, err := MenuDao.Read_Menu_List()
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = "获取失败!"
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = menu
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Read_User_Bind_Menu_List", func(m *nats.Msg) {
- var t_R lib.JSONS
- o := orm.NewOrm()
- powerDao := powerlibs.NewPower(o)
- power, err := powerDao.Read_Power_ByT_id(string(m.Data))
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- MenuDao := menulibs.NewMenu(o)
- menu, err := MenuDao.Read_Menu_List_ByPower_T_Menu(power.T_menu)
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = menu
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Add_Power", func(m *nats.Msg) {
- var t_Req powerlibs.Power
- var t_R lib.JSONS
- err := msgpack.Unmarshal(m.Data, &t_Req)
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- fmt.Printf("ERP_project_Add_Power message: %+v\n", t_Req)
- o := orm.NewOrm()
- powerDao := powerlibs.NewPower(o)
- id, err := powerDao.Add_Power(t_Req)
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = id
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Read_Power_ByT_id", func(m *nats.Msg) {
- fmt.Printf("ERP_project_Read_Power_ByT_id message: %+v\n", string(m.Data))
- var t_R lib.JSONS
- o := orm.NewOrm()
- powerDao := powerlibs.NewPower(o)
- power, err := powerDao.Read_Power_ByT_id(string(m.Data))
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = power
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Update_Power", func(m *nats.Msg) {
- var t_Req powerlibs.Power
- var t_R lib.JSONS
- err := msgpack.Unmarshal(m.Data, &t_Req)
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- fmt.Printf("ERP_project_Update_Power message: %+v\n", t_Req)
- o := orm.NewOrm()
- powerDao := powerlibs.NewPower(o)
- id, err := powerDao.Update_Power(t_Req)
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- APIDao := menulibs.NewAPI(o, Account.RedisCache_API)
- APIDao.Redis_API_DelK(t_Req.T_id)
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = id
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Delete_Power", func(m *nats.Msg) {
- var t_Req powerlibs.Power
- var t_R lib.JSONS
- err := msgpack.Unmarshal(m.Data, &t_Req)
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- fmt.Printf("ERP_project_Delete_Power message: %+v\n", t_Req)
- o := orm.NewOrm()
- powerDao := powerlibs.NewPower(o)
- id, err := powerDao.Delete_Power(t_Req)
- if err != nil {
- logs.Error("Mats", lib.FuncName(), err)
- t_R.Code = 202
- t_R.Msg = err.Error()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- APIDao := menulibs.NewAPI(o, Account.RedisCache_API)
- APIDao.Redis_API_DelK(t_Req.T_id)
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = id
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- }
|