CompanyNotice.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package Company
  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. "time"
  16. )
  17. type CompanyNotice struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  20. T_name string `orm:"size(256);null"` // 分类
  21. T_Notice_wx string `orm:"type(text);null"` //w微信公众号 appid/名字|
  22. T_Notice_wx2 string `orm:"type(text);null"` //w微信公众号 appid/名字|
  23. T_Notice_app string `orm:"type(text);null"` //app
  24. T_Notice_phone string `orm:"type(text);null"` //p手机 1111111|
  25. T_Notice_message string `orm:"type(text);null"` //m短信 1111111|
  26. T_Notice_mailbox string `orm:"type(text);null"` //e邮箱 1111111|
  27. T_Notice_bind string `orm:"type(text);null"` // 绑定T_sn,Tid| 862289056463538,1|8622546456433,1|
  28. T_Notice_mechanism string `orm:"type(text);null"` // 报警机制
  29. // W报警编号,处理,w启用,数量,上限,~|
  30. // W15,0,0,0,0,0,0,0,0,0,0,0,0|
  31. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  32. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  33. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  34. }
  35. type CompanyNotice_R struct {
  36. Id int
  37. T_name string // 分类
  38. T_Notice_wx string //w微信公众号 appid/名字|
  39. T_Notice_wx2 string //w微信公众号 appid/名字|
  40. T_Notice_app string //app
  41. T_Notice_phone string //p手机 1111111|
  42. T_Notice_message string //m短信 1111111|
  43. T_Notice_mailbox string //e邮箱 1111111|
  44. T_Notice_bind string // 绑定T_sn,Tid|
  45. T_Notice_mechanism string // 报警机制
  46. }
  47. func (t *CompanyNotice) TableName() string {
  48. return "company_notice" // 数据库名称 // ************** 替换 DesignClass **************
  49. }
  50. var redisCache_CompanyNotice cache.Cache
  51. func init() {
  52. //注册模型
  53. orm.RegisterModel(new(CompanyNotice))
  54. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  55. "redis_CompanyNotice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  56. fmt.Println(config)
  57. var err error
  58. redisCache_CompanyNotice, err = cache.NewCache("redis", config)
  59. if err != nil || redisCache_CompanyNotice == nil {
  60. errMsg := "failed to init redis"
  61. logs.Error(errMsg, err)
  62. panic(errMsg)
  63. }
  64. }
  65. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  66. func Redis_CompanyNotice_Set(r CompanyNotice) (err error) {
  67. key := strconv.Itoa(r.Id)
  68. //json序列化
  69. str, err := json.Marshal(r)
  70. if err != nil {
  71. logs.Error(lib.FuncName(), err)
  72. return
  73. }
  74. err = redisCache_CompanyNotice.Put(key, str, 2*time.Hour)
  75. if err != nil {
  76. logs.Error("set key:", key, ",value:", str, err)
  77. }
  78. return
  79. }
  80. // if r,is :=Redis_Get(T_sn);is{
  81. // return r,nil
  82. // }
  83. func Redis_CompanyNotice_Get(key string) (CompanyNotice, bool) {
  84. println("找到key:", key)
  85. if redisCache_CompanyNotice.IsExist(key) {
  86. //println("找到key:",key)
  87. v := redisCache_CompanyNotice.Get(key)
  88. var r CompanyNotice
  89. err := json.Unmarshal(v.([]byte), &r)
  90. if err != nil {
  91. logs.Error(lib.FuncName(), err)
  92. return r, false
  93. }
  94. return r, true
  95. }
  96. return CompanyNotice{}, false
  97. }
  98. func Redis_CompanyNotice_DelK(key string) (err error) {
  99. err = redisCache_CompanyNotice.Delete(key)
  100. if err != nil {
  101. logs.Error(lib.FuncName(), err)
  102. }
  103. return
  104. }
  105. // ---------------- 特殊方法 -------------------
  106. func CompanyNoticeToCompanyNotice_R(t CompanyNotice) (r CompanyNotice_R) {
  107. r.Id = t.Id
  108. r.T_name = t.T_name
  109. r.T_Notice_wx = t.T_Notice_wx
  110. r.T_Notice_wx2 = t.T_Notice_wx2
  111. r.T_Notice_app = t.T_Notice_app
  112. r.T_Notice_phone = t.T_Notice_phone
  113. r.T_Notice_message = t.T_Notice_message
  114. r.T_Notice_mailbox = t.T_Notice_mailbox
  115. r.T_Notice_bind = t.T_Notice_bind
  116. r.T_Notice_mechanism = t.T_Notice_mechanism
  117. return r
  118. }
  119. // 获取 ById
  120. func Read_CompanyNotice_ById(id int) (r CompanyNotice, err error) {
  121. key := strconv.Itoa(id)
  122. if r, is := Redis_CompanyNotice_Get(key); is {
  123. //println("Redis_Get OK")
  124. return r, nil
  125. }
  126. o := orm.NewOrm()
  127. r = CompanyNotice{Id: id}
  128. err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  129. if err != nil {
  130. logs.Error(lib.FuncName(), err)
  131. return r, err
  132. }
  133. Redis_CompanyNotice_Set(r)
  134. return r, err
  135. }
  136. // 添加
  137. func Add_CompanyNotice(m CompanyNotice) (id int64, err error) {
  138. o := orm.NewOrm()
  139. id, err = o.Insert(&m)
  140. if err != nil {
  141. logs.Error(lib.FuncName(), err)
  142. return id, err
  143. }
  144. Redis_CompanyNotice_Set(m)
  145. return
  146. }
  147. // 删除
  148. func Delete_CompanyNotice_ById(id int) bool {
  149. o := orm.NewOrm()
  150. v := CompanyNotice{Id: id}
  151. // ascertain id exists in the database
  152. if err := o.Read(&v); err == nil {
  153. var num int64
  154. v.T_State = 0
  155. num, err = o.Update(&v, "T_State")
  156. if err != nil {
  157. logs.Error(lib.FuncName(), err)
  158. return false
  159. }
  160. logs.Info("Number of records updated in database:", num)
  161. key := strconv.Itoa(v.Id)
  162. Redis_CompanyNotice_DelK(key)
  163. return true
  164. }
  165. return false
  166. }
  167. // 修改
  168. func Update_CompanyNotice(m CompanyNotice, cols ...string) error {
  169. o := orm.NewOrm()
  170. num, err := o.Update(&m, cols...)
  171. if err != nil {
  172. logs.Error(lib.FuncName(), err)
  173. return err
  174. }
  175. logs.Info("Number of records updated in database:", num)
  176. Redis_CompanyNotice_Set(m)
  177. return nil
  178. }
  179. // 删除
  180. func Delete_CompanyNotice_ByT_pid_All(T_pid int) {
  181. o := orm.NewOrm()
  182. qs := o.QueryTable(new(CompanyNotice))
  183. var r []CompanyNotice
  184. _, err := qs.Filter("T_pid", T_pid).Filter("T_State", 1).All(&r)
  185. if err != nil {
  186. logs.Error(lib.FuncName(), err)
  187. return
  188. }
  189. for _, v := range r {
  190. v.T_State = 0
  191. if _, err := o.Update(&v, "T_State"); err == nil {
  192. Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
  193. }
  194. }
  195. }
  196. // 获取列表
  197. func Read_CompanyNotice_List(T_pid int, T_name string, page int, page_z int) (r []CompanyNotice_R, cnt int64) {
  198. o := orm.NewOrm()
  199. // 也可以直接使用 Model 结构体作为表名
  200. qs := o.QueryTable(new(CompanyNotice))
  201. var maps []CompanyNotice
  202. var offset int64
  203. if page <= 1 {
  204. offset = 0
  205. } else {
  206. offset = int64((page - 1) * page_z)
  207. }
  208. _, err := qs.Limit(conf.Page_size, offset).Filter("T_pid", T_pid).Filter("T_name__icontains", T_name).Filter("T_State", 1).OrderBy("-Id").All(&maps)
  209. if err != nil {
  210. logs.Error(lib.FuncName(), err)
  211. return
  212. }
  213. cnt, err = qs.Filter("T_pid", T_pid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count()
  214. if err != nil {
  215. logs.Error(lib.FuncName(), err)
  216. return
  217. }
  218. for _, v := range maps {
  219. r = append(r, CompanyNoticeToCompanyNotice_R(v))
  220. }
  221. return r, cnt
  222. }
  223. // 获取全部列表
  224. func Read_CompanyNotice_All(T_pid int, T_name string) (r []CompanyNotice_R) {
  225. o := orm.NewOrm()
  226. // 也可以直接使用 Model 结构体作为表名
  227. var map_r []CompanyNotice
  228. qs := o.QueryTable(new(CompanyNotice))
  229. cond := orm.NewCondition()
  230. cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
  231. if len(T_name) > 0 {
  232. cond1 = cond1.And("T_name", T_name)
  233. }
  234. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  235. if err != nil {
  236. logs.Error(lib.FuncName(), err)
  237. return
  238. }
  239. for _, v := range map_r {
  240. r = append(r, CompanyNoticeToCompanyNotice_R(v))
  241. }
  242. return r
  243. }
  244. func Add_T_Notice_bind(T_sn string, T_id int, T_Notice_id int) (err error) {
  245. o := orm.NewOrm()
  246. v := CompanyNotice{Id: T_Notice_id}
  247. T_Notice_bind := fmt.Sprintf("%s,%d|", T_sn, T_id)
  248. err = o.Read(&v, "Id")
  249. if err != nil {
  250. logs.Error(lib.FuncName(), err)
  251. return err
  252. }
  253. v.T_Notice_bind = strings.Replace(v.T_Notice_bind, T_Notice_bind, "", -1)
  254. v.T_Notice_bind = v.T_Notice_bind + T_Notice_bind
  255. _, err = o.Update(&v, "T_Notice_bind")
  256. if err != nil {
  257. logs.Error(lib.FuncName(), err)
  258. return err
  259. }
  260. Redis_CompanyNotice_Set(v)
  261. return nil
  262. }
  263. func Delete_T_Notice_bind(T_sn string, T_id int, T_Notice_id int) (err error) {
  264. o := orm.NewOrm()
  265. v := CompanyNotice{Id: T_Notice_id}
  266. T_Notice_bind := fmt.Sprintf("%s,%d|", T_sn, T_id)
  267. err = o.Read(&v, "Id")
  268. if err != nil {
  269. logs.Error(lib.FuncName(), err)
  270. return
  271. }
  272. v.T_Notice_bind = strings.Replace(v.T_Notice_bind, T_Notice_bind, "", -1)
  273. _, err = o.Update(&v, "T_Notice_bind")
  274. if err != nil {
  275. logs.Error(lib.FuncName(), err)
  276. return err
  277. }
  278. Redis_CompanyNotice_Set(v)
  279. return nil
  280. }
  281. // 修改用户同时修改报警策略绑定的用户信息
  282. func Update_CompanyNotice_Bind_By_T_uuid(T_uuid, T_name string) bool {
  283. o := orm.NewOrm()
  284. o.Begin()
  285. // 也可以直接使用 Model 结构体作为表名
  286. var map_r []CompanyNotice
  287. qs := o.QueryTable(new(CompanyNotice))
  288. cond := orm.NewCondition()
  289. cond1 := cond.And("T_State", 1).AndCond(
  290. cond.Or("T_Notice_wx__icontains", T_uuid).
  291. Or("T_Notice_wx2__icontains", T_uuid).
  292. Or("T_Notice_app__icontains", T_uuid).
  293. Or("T_Notice_phone__icontains", T_uuid).
  294. Or("T_Notice_message__icontains", T_uuid).
  295. Or("T_Notice_mailbox__icontains", T_uuid))
  296. _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&map_r)
  297. if err != nil {
  298. logs.Error(lib.FuncName(), err)
  299. o.Rollback()
  300. return false
  301. }
  302. T_uuid_name := fmt.Sprintf("%s/%s|", T_uuid, T_name)
  303. for _, v := range map_r {
  304. v.T_Notice_wx = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx, T_uuid_name)
  305. v.T_Notice_wx2 = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx2, T_uuid_name)
  306. v.T_Notice_app = Replice_T_Notice_Bind(T_uuid, v.T_Notice_app, T_uuid_name)
  307. v.T_Notice_phone = Replice_T_Notice_Bind(T_uuid, v.T_Notice_phone, T_uuid_name)
  308. v.T_Notice_message = Replice_T_Notice_Bind(T_uuid, v.T_Notice_message, T_uuid_name)
  309. v.T_Notice_mailbox = Replice_T_Notice_Bind(T_uuid, v.T_Notice_mailbox, T_uuid_name)
  310. _, err = o.Update(&v, "T_Notice_wx", "T_Notice_wx2", "T_Notice_app", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism")
  311. if err != nil {
  312. logs.Error(lib.FuncName(), err)
  313. o.Rollback()
  314. return false
  315. }
  316. Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
  317. }
  318. o.Commit()
  319. return true
  320. }
  321. // 删除用户同时删除报警策略绑定的用户信息
  322. func Delete_CompanyNotice_Bind_By_T_uuid(T_uuid string) bool {
  323. o := orm.NewOrm()
  324. o.Begin()
  325. // 也可以直接使用 Model 结构体作为表名
  326. var map_r []CompanyNotice
  327. qs := o.QueryTable(new(CompanyNotice))
  328. cond := orm.NewCondition()
  329. cond1 := cond.And("T_State", 1).AndCond(
  330. cond.Or("T_Notice_wx__icontains", T_uuid).
  331. Or("T_Notice_wx2__icontains", T_uuid).
  332. Or("T_Notice_app__icontains", T_uuid).
  333. Or("T_Notice_phone__icontains", T_uuid).
  334. Or("T_Notice_message__icontains", T_uuid).
  335. Or("T_Notice_mailbox__icontains", T_uuid))
  336. _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&map_r)
  337. if err != nil {
  338. logs.Error(lib.FuncName(), err)
  339. o.Rollback()
  340. return false
  341. }
  342. for _, v := range map_r {
  343. v.T_Notice_wx = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx, "")
  344. v.T_Notice_wx2 = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx2, "")
  345. v.T_Notice_app = Replice_T_Notice_Bind(T_uuid, v.T_Notice_app, "")
  346. v.T_Notice_phone = Replice_T_Notice_Bind(T_uuid, v.T_Notice_phone, "")
  347. v.T_Notice_message = Replice_T_Notice_Bind(T_uuid, v.T_Notice_message, "")
  348. v.T_Notice_mailbox = Replice_T_Notice_Bind(T_uuid, v.T_Notice_mailbox, "")
  349. _, err := o.Update(&v, "T_Notice_wx", "T_Notice_wx2", "T_Notice_app", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism")
  350. if err != nil {
  351. logs.Error(lib.FuncName(), err)
  352. o.Rollback()
  353. return false
  354. }
  355. Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
  356. }
  357. o.Commit()
  358. return true
  359. }
  360. // 删除报警策略
  361. func Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid string, T_pids []int) bool {
  362. o := orm.NewOrm()
  363. o.Begin()
  364. // 也可以直接使用 Model 结构体作为表名
  365. var map_r []CompanyNotice
  366. qs := o.QueryTable(new(CompanyNotice))
  367. cond := orm.NewCondition()
  368. cond1 := cond.And("T_State", 1).And("T_pid__in", T_pids).AndCond(
  369. cond.Or("T_Notice_wx__icontains", T_uuid).
  370. Or("T_Notice_wx2__icontains", T_uuid).
  371. Or("T_Notice_app__icontains", T_uuid).
  372. Or("T_Notice_phone__icontains", T_uuid).
  373. Or("T_Notice_message__icontains", T_uuid).
  374. Or("T_Notice_mailbox__icontains", T_uuid))
  375. _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&map_r)
  376. if err != nil {
  377. logs.Error(lib.FuncName(), err)
  378. o.Rollback()
  379. return false
  380. }
  381. for _, v := range map_r {
  382. v.T_Notice_wx = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx, "")
  383. v.T_Notice_wx2 = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx2, "")
  384. v.T_Notice_app = Replice_T_Notice_Bind(T_uuid, v.T_Notice_app, "")
  385. v.T_Notice_phone = Replice_T_Notice_Bind(T_uuid, v.T_Notice_phone, "")
  386. v.T_Notice_message = Replice_T_Notice_Bind(T_uuid, v.T_Notice_message, "")
  387. v.T_Notice_mailbox = Replice_T_Notice_Bind(T_uuid, v.T_Notice_mailbox, "")
  388. _, err := o.Update(&v, "T_Notice_wx", "T_Notice_wx2", "T_Notice_app", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism")
  389. if err != nil {
  390. logs.Error(lib.FuncName(), err)
  391. o.Rollback()
  392. return false
  393. }
  394. Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
  395. }
  396. o.Commit()
  397. return true
  398. }
  399. func Replice_T_Notice_Bind(T_uuid, T_Notice_Bind string, new string) string {
  400. if len(T_Notice_Bind) == 0 {
  401. return ""
  402. }
  403. list := strings.Split(T_Notice_Bind, "|")
  404. for _, v := range list {
  405. if strings.Contains(v, T_uuid) {
  406. T_Notice_Bind = strings.Replace(T_Notice_Bind, v+"|", new, -1)
  407. break
  408. }
  409. }
  410. return T_Notice_Bind
  411. }