titans.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package dao
  2. import (
  3. "context"
  4. "github.com/jinzhu/gorm"
  5. "go-common/app/service/live/resource/api/http/v1"
  6. "go-common/app/service/live/resource/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. xtime "time"
  11. )
  12. var (
  13. _statusDel = 4
  14. _statusOn = 1
  15. _statusOff = 2
  16. )
  17. // CheckParams 校验key及team
  18. func (d *Dao) CheckParams(c context.Context, team int64, keyword string) (err error) {
  19. err = nil
  20. if "" == keyword {
  21. err = ecode.ResourceParamErr
  22. }
  23. return err
  24. }
  25. // TeamKeyword 用于封装业务参数结构
  26. type TeamKeyword struct {
  27. Team int64
  28. Keyword string
  29. }
  30. // SelectByTeamIndex 单个key的value查询
  31. func (d *Dao) SelectByTeamIndex(c context.Context, team int64, keyword string, id int64) (res *model.SundryConfig, err error) {
  32. res = &model.SundryConfig{}
  33. resMid := &model.SundyConfigObject{}
  34. if 0 != id {
  35. err = d.rsDB.Model(&model.SundyConfigObject{}).Where("id=?", id).Where("status != ?", _statusDel).Find(&resMid).Error
  36. } else {
  37. err = d.rsDB.Model(&model.SundyConfigObject{}).Where("team=?", team).Where("keyword=?", keyword).Where("status != ?", _statusDel).Find(&resMid).Error
  38. }
  39. if err != nil && err != gorm.ErrRecordNotFound {
  40. log.Error("[live.titans.dao| selectByGroupIndex] d.db.Exec err: %v", err)
  41. return
  42. }
  43. res.Id = resMid.Id
  44. res.Team = resMid.Team
  45. res.Keyword = resMid.Keyword
  46. res.Value = resMid.Value
  47. res.Status = resMid.Status
  48. res.Ctime = resMid.Ctime.Time().Format("2006-01-02 15:04:05")
  49. res.Mtime = resMid.Mtime.Time().Format("2006-01-02 15:04:05")
  50. err = nil
  51. return
  52. }
  53. // SelectByParams 管理后台通过条件查询
  54. func (d *Dao) SelectByParams(c context.Context, id int64, team int64, keyword string, name string, status int64, page int64, pageSize int64) (res []*model.SundryConfig, count int64, err error) {
  55. var (
  56. Items []*model.SundyConfigObject
  57. )
  58. condition := d.rsDB
  59. //模型配置
  60. if -1 == team {
  61. condition = condition.Where("team = ?", 0)
  62. }
  63. //除模型配置外的全部配置
  64. if 0 == team {
  65. condition = condition.Where("team != ?", 0)
  66. }
  67. if 0 != team && -1 != team {
  68. condition = condition.Where("team = ?", team)
  69. }
  70. if 0 != id {
  71. condition = condition.Where("id = ?", id)
  72. }
  73. if "" != keyword {
  74. condition = condition.Where("keyword = ?", keyword)
  75. }
  76. if "" != name {
  77. condition = condition.Where("name like '%" + name + "%'")
  78. }
  79. if 0 != status {
  80. condition = condition.Where("status = ?", status)
  81. }
  82. condition = condition.Where("status != ?", _statusDel)
  83. sModel := condition.Model(&model.SundryConfig{})
  84. sModel.Count(&count)
  85. iOffset := (page - 1) * pageSize
  86. err = sModel.Offset(iOffset).Limit(pageSize).Order("mtime DESC, id DESC").Find(&Items).Error
  87. if err != nil && err != gorm.ErrRecordNotFound {
  88. log.Error("[live.titans.dao| select by params] d.db.Exec err: %v", err)
  89. return
  90. }
  91. for _, v := range Items {
  92. item := &model.SundryConfig{
  93. Id: v.Id,
  94. Team: v.Team,
  95. Keyword: v.Keyword,
  96. Name: v.Name,
  97. Value: v.Value,
  98. Ctime: v.Ctime.Time().Format("2006-01-02 15:04:05"),
  99. Mtime: v.Mtime.Time().Format("2006-01-02 15:04:05"),
  100. Status: v.Status,
  101. }
  102. res = append(res, item)
  103. }
  104. return
  105. }
  106. // InsertRecord 管理后台插入/编辑一条记录
  107. func (d *Dao) InsertRecord(c context.Context, team int64, keyword string, value string, name string, status int64, oid int64) (id int64, count int64, err error) {
  108. //创建模板
  109. if team == -1 {
  110. team = 0
  111. }
  112. //查询唯一索引
  113. err = d.rsDB.Model(&model.SundryConfig{}).Where("team=? and keyword=? and id != ?", team, keyword, oid).Count(&count).Error
  114. if nil != err {
  115. return
  116. }
  117. if 0 != count {
  118. return
  119. }
  120. setMaps := &model.InsertMaps{
  121. Team: team,
  122. Keyword: keyword,
  123. Value: value,
  124. Name: name,
  125. Status: status,
  126. }
  127. newRecord := &model.SundryConfig{}
  128. //编辑
  129. if oid != 0 {
  130. err = d.rsDB.Model(&model.SundryConfig{}).Where("id = ?", oid).Update(setMaps).Error
  131. id = oid
  132. return
  133. }
  134. setMaps.Status = int64(_statusOff)
  135. err = d.rsDB.Create(setMaps).Error
  136. if err != nil {
  137. log.Error("[live.titans.dao| insertRecord] d.db.Exec err: %v", err)
  138. return
  139. }
  140. d.rsDB.Where("keyword = ?", keyword).Find(&newRecord)
  141. id = newRecord.Id
  142. return
  143. }
  144. // SelectByLikes 业务方的请求sql
  145. func (d *Dao) SelectByLikes(c context.Context, teams []int64, teamKeys []*TeamKeyword) (Items []*model.SundyConfigObject, err error) {
  146. Items = []*model.SundyConfigObject{}
  147. if len(teams) == 0 && len(teamKeys) == 0 {
  148. return
  149. }
  150. /** sql 式执行
  151. sql := "select * from ap_sundry_config where status = 1"
  152. if len(teams) != 0 {
  153. teamStr := ""
  154. for i, num := range teams {
  155. teamStr += strconv.Itoa(int(num))
  156. if i+1 != len(teams) {
  157. teamStr += ","
  158. }
  159. }
  160. sql += " and team in (" + teamStr + ") "
  161. }
  162. if len(teamKeys) != 0 {
  163. for _, v := range teamKeys {
  164. sql = sql + " or (team =" + strconv.Itoa(int(v.Team)) + " and keyword = '" + v.Keyword + "') "
  165. }
  166. }
  167. rows, err := d.db.Query(c, sql)
  168. if err != nil {
  169. return
  170. }
  171. for rows.Next() {
  172. resMid := &model.SundyConfigObject{}
  173. rows.Scan(&resMid.Id, &resMid.Team, &resMid.Keyword, &resMid.Name, &resMid.Value, &resMid.Ctime, &resMid.Mtime, &resMid.Status)
  174. Items = append(Items, resMid)
  175. }*/
  176. /** orm **/
  177. condition := d.rsDB.Where("status = ?", 1)
  178. if len(teams) != 0 {
  179. condition = condition.Where("team in (?)", teams)
  180. }
  181. if len(teamKeys) != 0 {
  182. for _, v := range teamKeys {
  183. condition = condition.Or("team = ? and keyword = ?", v.Team, v.Keyword)
  184. }
  185. }
  186. err = condition.Find(&Items).Error
  187. return
  188. }
  189. // FormatTime 时间格式化
  190. func (d *Dao) FormatTime(c context.Context, timeStrUtc string) (timeStr string) {
  191. ctime, _ := xtime.ParseInLocation("2006-01-02T15:04:05+08:00", timeStrUtc, xtime.Local)
  192. timeStr = ctime.Format("2006-01-02 15:04:05")
  193. return timeStr
  194. }
  195. // InsertServiceConfig 插入服务配置
  196. func (d *Dao) InsertServiceConfig(c context.Context, oid int64, treeName string, treePath string, treeId int64, service string, keyword string, template int64, name string, value string, status int64) (id int64, err error) {
  197. checkRes := &model.ServiceConfigObject{}
  198. err = d.rsDB.Model(&model.ServiceConfigObject{}).Where("tree_id=?", treeId).Where("keyword=?", keyword).Find(&checkRes).Error
  199. if err != nil && err != gorm.ErrRecordNotFound {
  200. log.Error("[live.titans.dao| insertServiceRecord] d.db.Exec err: %v", err)
  201. return
  202. }
  203. if 0 != checkRes.Id && oid != checkRes.Id {
  204. id = -1
  205. return
  206. }
  207. if oid != 0 {
  208. //编辑
  209. updateMaps := &model.UpdateServiceConfig{
  210. Service: service,
  211. Keyword: keyword,
  212. Template: template,
  213. Value: value,
  214. Name: name,
  215. Status: status,
  216. }
  217. err = d.rsDB.Model(&model.ServiceConfigObject{}).Where("id=?", oid).Update(updateMaps).Error
  218. id = oid
  219. } else {
  220. setMaps := &model.InsertServiceConfig{
  221. TreeName: treeName,
  222. TreePath: treePath,
  223. TreeId: treeId,
  224. Service: service,
  225. Keyword: keyword,
  226. Template: template,
  227. Value: value,
  228. Name: name,
  229. Status: status,
  230. }
  231. err = d.rsDB.Model(&model.ServiceConfigObject{}).Create(setMaps).Error
  232. newRecord := &model.ServiceConfigObject{}
  233. d.rsDB.Model(&model.ServiceConfigObject{}).Where("tree_id = ?", treeId).Where("keyword=?", keyword).Find(&newRecord)
  234. id = newRecord.Id
  235. }
  236. return
  237. }
  238. // GetServiceConfig 通过tree_id 获取配置
  239. func (d *Dao) GetServiceConfig(c context.Context, treeId int64) (value map[string]string, err error) {
  240. value = make(map[string]string)
  241. res := []*model.ServiceConfigObject{}
  242. err = d.rsDB.Model(&model.ServiceConfigObject{}).Where("tree_id = ?", treeId).Where("status= ?", _statusOn).Find(&res).Error
  243. if err != nil && err != gorm.ErrRecordNotFound {
  244. return
  245. }
  246. for _, v := range res {
  247. value[v.Keyword] = v.Value
  248. }
  249. return
  250. }
  251. // GetServiceConfigList 管理后台获取服务配置列表
  252. func (d *Dao) GetServiceConfigList(c context.Context, treeName string, treeId int64, keyword string, service string, page int64, pageSize int64, name string, status int64) (list []*v1.MList, totalNum int64, err error) {
  253. list = []*v1.MList{}
  254. Items := make([]*model.ServiceConfigObject, 0)
  255. totalNum = 0
  256. condition := d.rsDB
  257. condition = condition.Where("tree_name=?", treeName)
  258. if 1 == status {
  259. condition = condition.Where("status=?", status)
  260. }
  261. if status != 0 && status != int64(_statusOn) {
  262. condition = condition.Where("status !=? ", _statusOn)
  263. }
  264. if 0 != treeId {
  265. condition = condition.Where("tree_id =? ", treeId)
  266. }
  267. if "" != name {
  268. condition = condition.Where("name like '%" + name + "%'")
  269. }
  270. if "" != keyword {
  271. condition = condition.Where("keyword like '%" + keyword + "%'")
  272. }
  273. if "" != service {
  274. condition = condition.Where("service =?", service)
  275. }
  276. sModel := condition.Model(&model.ServiceConfigObject{})
  277. sModel.Count(&totalNum)
  278. iOffset := (page - 1) * pageSize
  279. err = sModel.Offset(iOffset).Limit(pageSize).Order("mtime DESC, id DESC").Find(&Items).Error
  280. if nil != err {
  281. log.Error("sql error select by params")
  282. return
  283. }
  284. for _, v := range Items {
  285. item := &v1.MList{
  286. Id: v.Id,
  287. TreeName: v.TreeName,
  288. TreePath: v.TreePath,
  289. TreeId: v.TreeId,
  290. Service: v.Service,
  291. Keyword: v.Keyword,
  292. Template: v.Template,
  293. Name: v.Name,
  294. Value: v.Value,
  295. Ctime: v.Ctime.Time().Format("2006-01-02 15:04:05"),
  296. Mtime: v.Mtime.Time().Format("2006-01-02 15:04:05"),
  297. Status: v.Status,
  298. }
  299. list = append(list, item)
  300. }
  301. return
  302. }
  303. // GetTreeIds 获取treeName对应的tree_ids
  304. func (d *Dao) GetTreeIds(c context.Context, treeName string) (list []int64, err error) {
  305. list = make([]int64, 0)
  306. query := "select distinct tree_id from ap_services_config where status=1 and tree_name = ?"
  307. rows, err := d.db.Query(c, query, treeName)
  308. if err != nil && err != sql.ErrNoRows {
  309. log.Error("[live.titans.dao| getDiscoveryIds] d.db.Exec err: %v", err)
  310. return
  311. }
  312. defer rows.Close()
  313. for rows.Next() {
  314. item := &model.ServiceModel{}
  315. err = rows.Scan(&item.TreeId)
  316. if err != nil {
  317. return
  318. }
  319. if item.TreeId != 0 {
  320. list = append(list, item.TreeId)
  321. }
  322. }
  323. return
  324. }
  325. // GetEasyRecord 获取运营配置
  326. func (d *Dao) GetEasyRecord(c context.Context, treeName string) (list *v1.MList) {
  327. list = &v1.MList{}
  328. query := "select id, value from ap_services_config where tree_name = ? and tree_id = 0"
  329. rows := d.db.QueryRow(c, query, treeName)
  330. err := rows.Scan(&list.Id, &list.Value)
  331. if err != nil {
  332. log.Error("[live.titans.dao| GetEasyRecord] d.db.Exec err: %v", err)
  333. return
  334. }
  335. return
  336. }
  337. // SetEasyRecord 设置运营配置
  338. func (d *Dao) SetEasyRecord(c context.Context, treeName string, value string, id int64) (nId int64, err error) {
  339. record := &model.InsertServiceConfig{
  340. TreeName: treeName,
  341. TreePath: treeName,
  342. TreeId: 0,
  343. Name: treeName + "运营操作列表",
  344. Value: value,
  345. Status: int64(_statusOn),
  346. }
  347. nId = id
  348. if id != 0 {
  349. err = d.rsDB.Model(&model.ServiceConfigObject{}).Where("id = ?", id).Update(record).Error
  350. } else {
  351. err = d.rsDB.Model(&model.ServiceConfigObject{}).Create(record).Error
  352. if err != nil {
  353. return
  354. }
  355. newRecord := &model.ServiceConfigObject{}
  356. err = d.rsDB.Model(&model.ServiceConfigObject{}).Where("tree_name = ?", treeName).Where("tree_id = ?", 0).Find(&newRecord).Error
  357. if err != nil {
  358. return
  359. }
  360. nId = newRecord.Id
  361. }
  362. return
  363. }
  364. // GetListByIds 通过ids获取配置列表
  365. func (d *Dao) GetListByIds(c context.Context, ids []int64) (list []*model.ServiceModel, err error) {
  366. list = []*model.ServiceModel{}
  367. Items := make([]*model.ServiceConfigObject, 0)
  368. condition := d.rsDB
  369. condition = condition.Where("id in (?)", ids)
  370. sModel := condition.Model(&model.ServiceConfigObject{})
  371. err = sModel.Find(&Items).Error
  372. if nil != err {
  373. log.Error("sql error select by params")
  374. return
  375. }
  376. for _, v := range Items {
  377. item := &model.ServiceModel{
  378. Id: v.Id,
  379. TreeName: v.TreeName,
  380. TreePath: v.TreePath,
  381. TreeId: v.TreeId,
  382. Service: v.Service,
  383. Keyword: v.Keyword,
  384. Template: v.Template,
  385. Name: v.Name,
  386. Value: v.Value,
  387. Ctime: v.Ctime.Time().Format("2006-01-02 15:04:05"),
  388. Mtime: v.Mtime.Time().Format("2006-01-02 15:04:05"),
  389. Status: v.Status,
  390. }
  391. list = append(list, item)
  392. }
  393. return
  394. }