mng.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/search/model"
  10. sqlx "go-common/library/database/sql"
  11. )
  12. const (
  13. _mngBusinessListSQL = `select id,business,description,app_ids from digger_business where business like '%%%s%%' limit ?,?`
  14. _mngBusinessListTotalSQL = `select count(*) from digger_business where business like '%%%s%%'`
  15. _mngBusinessAllSQL = `select id,business,description,app_ids from digger_business`
  16. _mngAddBusinessSQL = `insert into digger_business (business,description,app_ids) values (?,?,?)`
  17. _mngUpdateBusinessSQL = `update digger_business set business=?,description=?,app_ids=? where id=?`
  18. _mngBusinessInfoSQL = `select id,business,description,app_ids from digger_business where id=?`
  19. _mngBusinessInfoByNameSQL = `select id,business,description,app_ids from digger_business where business=?`
  20. _mngAssetListSQL = `select id,name,type,src,description from digger_asset %s limit ?,?`
  21. _mngAssetTotalSQL = `select count(*) from digger_asset %s`
  22. _mngAssetAllSQL = `select id,name,type,src,description from digger_asset`
  23. _mngAssetInfoSQL = `select id,name,type,src,description from digger_asset where id=?`
  24. _mngAssetInfoByNameSQL = `select id,name,type,src,description from digger_asset where name=?`
  25. _mngAddAssetSQL = `insert into digger_asset (name,type,src,description) values (?,?,?,?)`
  26. _mngUpdateAssetSQL = `update digger_asset set name=?,type=?,src=?,description=? where id=?`
  27. _mngApplistSQL = `select id,business,appid,description,db_name,es_name,table_name,databus_name,table_prefix,table_format,index_prefix,
  28. index_version,index_format,index_type,index_id,data_index_suffix,index_mapping,data_fields,data_extra,review_num,review_time,
  29. sleep,size,sql_by_id,sql_by_mtime,sql_by_idmtime,databus_info,databus_index_id,query_max_indexes from digger_app where business=?`
  30. _mngAppInfoSQL = `select id,business,appid,description,db_name,es_name,table_name,databus_name,table_prefix,table_format,index_prefix,
  31. index_version,index_format,index_type,index_id,data_index_suffix,index_mapping,data_fields,data_extra,review_num,review_time,
  32. sleep,size,sql_by_id,sql_by_mtime,sql_by_idmtime,databus_info,databus_index_id,query_max_indexes from digger_app where id=?`
  33. _mngAppInfoByAppidSQL = `select id,business,appid,description,db_name,es_name,table_name,databus_name,table_prefix,table_format,index_prefix,
  34. index_version,index_format,index_type,index_id,data_index_suffix,index_mapping,data_fields,data_extra,review_num,review_time,
  35. sleep,size,sql_by_id,sql_by_mtime,sql_by_idmtime,databus_info,databus_index_id,query_max_indexes from digger_app where appid=?`
  36. _mngAddAppSQL = `insert into digger_app (business,appid,description) values (?,?,?)`
  37. _mngUpdateAppSQL = `update digger_app set business=?,appid=?,description=?,db_name=?,es_name=?,table_name=?,databus_name=?,table_prefix=?,table_format=?,index_prefix=?,
  38. index_version=?,index_format=?,index_type=?,index_id=?,data_index_suffix=?,index_mapping=?,data_fields=?,data_extra=?,review_num=?,review_time=?,
  39. sleep=?,size=?,sql_by_id=?,sql_by_mtime=?,sql_by_idmtime=?,databus_info=?,databus_index_id=?,query_max_indexes=? where id=?`
  40. _mngUpdateAppAssetTableSQL = `update digger_app set table_prefix=?,table_format=? where table_name=?`
  41. _mngUpdateAppAssetDatabusSQL = `update digger_app set databus_info=?,databus_index_id=? where databus_name=?`
  42. _mngCountSQL = `select time,count from digger_count where business=? and type=? and time >= ?`
  43. _mngPercentSQL = `select name,count from digger_count where business=? and type=? and time = ?`
  44. )
  45. // BusinessList .
  46. func (d *Dao) BusinessList(ctx context.Context, name string, offset, limit int) (list []*model.MngBusiness, err error) {
  47. sqlStr := fmt.Sprintf(_mngBusinessListSQL, name)
  48. rows, err := d.db.Query(ctx, sqlStr, offset, limit)
  49. if err != nil {
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. b := &model.MngBusiness{}
  55. if err = rows.Scan(&b.ID, &b.Name, &b.Desc, &b.AppsJSON); err != nil {
  56. return
  57. }
  58. b.Apps = make([]*model.MngBusinessApp, 0)
  59. if b.AppsJSON != "" {
  60. if err = json.Unmarshal([]byte(b.AppsJSON), &b.Apps); err != nil {
  61. return
  62. }
  63. }
  64. list = append(list, b)
  65. }
  66. err = rows.Err()
  67. return
  68. }
  69. // BusinessTotal .
  70. func (d *Dao) BusinessTotal(ctx context.Context, name string) (total int64, err error) {
  71. sqlStr := fmt.Sprintf(_mngBusinessListTotalSQL, name)
  72. err = d.db.QueryRow(ctx, sqlStr).Scan(&total)
  73. return
  74. }
  75. // BusinessAll .
  76. func (d *Dao) BusinessAll(ctx context.Context) (list []*model.MngBusiness, err error) {
  77. rows, err := d.db.Query(ctx, _mngBusinessAllSQL)
  78. if err != nil {
  79. return
  80. }
  81. defer rows.Close()
  82. for rows.Next() {
  83. b := &model.MngBusiness{}
  84. if err = rows.Scan(&b.ID, &b.Name, &b.Desc, &b.AppsJSON); err != nil {
  85. return
  86. }
  87. b.Apps = make([]*model.MngBusinessApp, 0)
  88. if b.AppsJSON != "" {
  89. if err = json.Unmarshal([]byte(b.AppsJSON), &b.Apps); err != nil {
  90. return
  91. }
  92. }
  93. list = append(list, b)
  94. }
  95. err = rows.Err()
  96. return
  97. }
  98. // AddBusiness .
  99. func (d *Dao) AddBusiness(ctx context.Context, b *model.MngBusiness) (id int64, err error) {
  100. res, err := d.db.Exec(ctx, _mngAddBusinessSQL, b.Name, b.Desc, b.AppsJSON)
  101. if err != nil {
  102. return
  103. }
  104. id, err = res.LastInsertId()
  105. return
  106. }
  107. // UpdateBusiness .
  108. func (d *Dao) UpdateBusiness(ctx context.Context, b *model.MngBusiness) (err error) {
  109. _, err = d.db.Exec(ctx, _mngUpdateBusinessSQL, b.Name, b.Desc, b.AppsJSON, b.ID)
  110. return
  111. }
  112. // BusinessInfo .
  113. func (d *Dao) BusinessInfo(ctx context.Context, id int64) (info *model.MngBusiness, err error) {
  114. info = new(model.MngBusiness)
  115. if err = d.db.QueryRow(ctx, _mngBusinessInfoSQL, id).Scan(&info.ID, &info.Name, &info.Desc, &info.AppsJSON); err != nil {
  116. if err == sqlx.ErrNoRows {
  117. info = nil
  118. err = nil
  119. }
  120. return
  121. }
  122. info.Apps = make([]*model.MngBusinessApp, 0)
  123. if info.AppsJSON != "" {
  124. err = json.Unmarshal([]byte(info.AppsJSON), &info.Apps)
  125. }
  126. return
  127. }
  128. // BusinessInfoByName .
  129. func (d *Dao) BusinessInfoByName(ctx context.Context, name string) (info *model.MngBusiness, err error) {
  130. info = new(model.MngBusiness)
  131. if err = d.db.QueryRow(ctx, _mngBusinessInfoByNameSQL, name).Scan(&info.ID, &info.Name, &info.Desc, &info.AppsJSON); err != nil {
  132. if err == sqlx.ErrNoRows {
  133. info = nil
  134. err = nil
  135. }
  136. return
  137. }
  138. info.Apps = make([]*model.MngBusinessApp, 0)
  139. if info.AppsJSON != "" {
  140. err = json.Unmarshal([]byte(info.AppsJSON), &info.Apps)
  141. }
  142. return
  143. }
  144. // AssetList .
  145. func (d *Dao) AssetList(ctx context.Context, typ int, name string, offset, limit int) (list []*model.MngAsset, err error) {
  146. where := " where 1 "
  147. if typ > 0 {
  148. where += fmt.Sprintf(" and type=%d ", typ)
  149. }
  150. if name != "" {
  151. where += fmt.Sprintf(" and name like '%%%s%%'", name)
  152. }
  153. sqlStr := fmt.Sprintf(_mngAssetListSQL, where)
  154. rows, err := d.db.Query(ctx, sqlStr, offset, limit)
  155. if err != nil {
  156. return
  157. }
  158. defer rows.Close()
  159. for rows.Next() {
  160. a := &model.MngAsset{}
  161. if err = rows.Scan(&a.ID, &a.Name, &a.Type, &a.Config, &a.Desc); err != nil {
  162. return
  163. }
  164. list = append(list, a)
  165. }
  166. err = rows.Err()
  167. return
  168. }
  169. // AssetTotal .
  170. func (d *Dao) AssetTotal(ctx context.Context, typ int, name string) (total int64, err error) {
  171. where := " where 1 "
  172. if typ > 0 {
  173. where += fmt.Sprintf(" and type=%d ", typ)
  174. }
  175. if name != "" {
  176. where += fmt.Sprintf(" and name like '%%%s%%'", name)
  177. }
  178. sqlStr := fmt.Sprintf(_mngAssetTotalSQL, where)
  179. err = d.db.QueryRow(ctx, sqlStr).Scan(&total)
  180. return
  181. }
  182. // AssetAll .
  183. func (d *Dao) AssetAll(ctx context.Context) (list []*model.MngAsset, err error) {
  184. rows, err := d.db.Query(ctx, _mngAssetAllSQL)
  185. if err != nil {
  186. return
  187. }
  188. defer rows.Close()
  189. for rows.Next() {
  190. a := &model.MngAsset{}
  191. if err = rows.Scan(&a.ID, &a.Name, &a.Type, &a.Config, &a.Desc); err != nil {
  192. return
  193. }
  194. list = append(list, a)
  195. }
  196. err = rows.Err()
  197. return
  198. }
  199. // AssetInfo .
  200. func (d *Dao) AssetInfo(ctx context.Context, id int64) (info *model.MngAsset, err error) {
  201. info = new(model.MngAsset)
  202. if err = d.db.QueryRow(ctx, _mngAssetInfoSQL, id).Scan(&info.ID, &info.Name, &info.Type, &info.Config, &info.Desc); err != nil {
  203. if err == sqlx.ErrNoRows {
  204. info = nil
  205. err = nil
  206. }
  207. return
  208. }
  209. return
  210. }
  211. // AssetInfoByName .
  212. func (d *Dao) AssetInfoByName(ctx context.Context, name string) (info *model.MngAsset, err error) {
  213. info = new(model.MngAsset)
  214. if err = d.db.QueryRow(ctx, _mngAssetInfoByNameSQL, name).Scan(&info.ID, &info.Name, &info.Type, &info.Config, &info.Desc); err != nil {
  215. if err == sqlx.ErrNoRows {
  216. info = nil
  217. err = nil
  218. }
  219. return
  220. }
  221. return
  222. }
  223. // AddAsset .
  224. func (d *Dao) AddAsset(ctx context.Context, b *model.MngAsset) (id int64, err error) {
  225. res, err := d.db.Exec(ctx, _mngAddAssetSQL, b.Name, b.Type, b.Config, b.Desc)
  226. if err != nil {
  227. return
  228. }
  229. id, err = res.LastInsertId()
  230. return
  231. }
  232. // UpdateAsset .
  233. func (d *Dao) UpdateAsset(ctx context.Context, b *model.MngAsset) (err error) {
  234. _, err = d.db.Exec(ctx, _mngUpdateAssetSQL, b.Name, b.Type, b.Config, b.Desc, b.ID)
  235. return
  236. }
  237. // AppList .
  238. func (d *Dao) AppList(ctx context.Context, business string) (list []*model.MngApp, err error) {
  239. rows, err := d.db.Query(ctx, _mngApplistSQL, business)
  240. if err != nil {
  241. return
  242. }
  243. defer rows.Close()
  244. for rows.Next() {
  245. a := &model.MngApp{}
  246. if err = rows.Scan(&a.ID, &a.Business, &a.AppID, &a.Desc, &a.DBName, &a.ESName, &a.TableName, &a.DatabusName, &a.TablePrefix, &a.TableFormat,
  247. &a.IndexPrefix, &a.IndexVersion, &a.IndexFormat, &a.IndexType, &a.IndexID, &a.DataIndexSuffix, &a.IndexMapping,
  248. &a.DataFields, &a.DataExtra, &a.ReviewNum, &a.ReviewTime, &a.Sleep, &a.Size, &a.SQLByID, &a.SQLByMtime,
  249. &a.SQLByIDMtime, &a.DatabusInfo, &a.DatabusIndexID, &a.QueryMaxIndexes); err != nil {
  250. return
  251. }
  252. list = append(list, a)
  253. }
  254. err = rows.Err()
  255. return
  256. }
  257. // AppInfo .
  258. func (d *Dao) AppInfo(ctx context.Context, id int64) (a *model.MngApp, err error) {
  259. a = new(model.MngApp)
  260. if err = d.db.QueryRow(ctx, _mngAppInfoSQL, id).Scan(&a.ID, &a.Business, &a.AppID, &a.Desc, &a.DBName, &a.ESName, &a.TableName, &a.DatabusName,
  261. &a.TablePrefix, &a.TableFormat, &a.IndexPrefix, &a.IndexVersion, &a.IndexFormat, &a.IndexType, &a.IndexID, &a.DataIndexSuffix, &a.IndexMapping,
  262. &a.DataFields, &a.DataExtra, &a.ReviewNum, &a.ReviewTime, &a.Sleep, &a.Size, &a.SQLByID, &a.SQLByMtime,
  263. &a.SQLByIDMtime, &a.DatabusInfo, &a.DatabusIndexID, &a.QueryMaxIndexes); err != nil {
  264. if err == sqlx.ErrNoRows {
  265. a = nil
  266. err = nil
  267. }
  268. return
  269. }
  270. return
  271. }
  272. // AppInfoByAppid .
  273. func (d *Dao) AppInfoByAppid(ctx context.Context, appid string) (a *model.MngApp, err error) {
  274. a = new(model.MngApp)
  275. if err = d.db.QueryRow(ctx, _mngAppInfoByAppidSQL, appid).Scan(&a.ID, &a.Business, &a.AppID, &a.Desc, &a.DBName, &a.ESName, &a.TableName, &a.DatabusName,
  276. &a.TablePrefix, &a.TableFormat, &a.IndexPrefix, &a.IndexVersion, &a.IndexFormat, &a.IndexType, &a.IndexID, &a.DataIndexSuffix, &a.IndexMapping,
  277. &a.DataFields, &a.DataExtra, &a.ReviewNum, &a.ReviewTime, &a.Sleep, &a.Size, &a.SQLByID, &a.SQLByMtime,
  278. &a.SQLByIDMtime, &a.DatabusInfo, &a.DatabusIndexID, &a.QueryMaxIndexes); err != nil {
  279. if err == sqlx.ErrNoRows {
  280. a = nil
  281. err = nil
  282. }
  283. return
  284. }
  285. return
  286. }
  287. // AddApp .
  288. func (d *Dao) AddApp(ctx context.Context, a *model.MngApp) (id int64, err error) {
  289. res, err := d.db.Exec(ctx, _mngAddAppSQL, a.Business, a.AppID, a.Desc)
  290. if err != nil {
  291. return
  292. }
  293. id, err = res.LastInsertId()
  294. return
  295. }
  296. // UpdateApp .
  297. func (d *Dao) UpdateApp(ctx context.Context, a *model.MngApp) (err error) {
  298. _, err = d.db.Exec(ctx, _mngUpdateAppSQL, a.Business, a.AppID, a.Desc, a.DBName, a.ESName, a.TableName, a.DatabusName, a.TablePrefix, a.TableFormat,
  299. a.IndexPrefix, a.IndexVersion, a.IndexFormat, a.IndexType, a.IndexID, a.DataIndexSuffix, a.IndexMapping,
  300. a.DataFields, a.DataExtra, a.ReviewNum, a.ReviewTime, a.Sleep, a.Size, a.SQLByID, a.SQLByMtime,
  301. a.SQLByIDMtime, a.DatabusInfo, a.DatabusIndexID, a.QueryMaxIndexes, a.ID)
  302. return
  303. }
  304. // UpdateAppAssetTable .
  305. func (d *Dao) UpdateAppAssetTable(ctx context.Context, name string, t *model.MngAssetTable) (err error) {
  306. _, err = d.db.Exec(ctx, _mngUpdateAppAssetTableSQL, t.TablePrefix, t.TableFormat, name)
  307. return
  308. }
  309. // UpdateAppAssetDatabus .
  310. func (d *Dao) UpdateAppAssetDatabus(ctx context.Context, name string, v *model.MngAssetDatabus) (err error) {
  311. _, err = d.db.Exec(ctx, _mngUpdateAppAssetDatabusSQL, v.DatabusInfo, v.DatabusIndexID, name)
  312. return
  313. }
  314. // MngCount .
  315. func (d *Dao) MngCount(ctx context.Context, c *model.MngCount) (list []*model.MngCountRes, err error) {
  316. list = []*model.MngCountRes{}
  317. sTime := time.Now().AddDate(0, 0, -365).Format("2006-01-02")
  318. rows, err := d.db.Query(ctx, _mngCountSQL, c.Business, c.Type, sTime)
  319. if err != nil {
  320. return
  321. }
  322. defer rows.Close()
  323. for rows.Next() {
  324. a := &model.MngCountRes{}
  325. if err = rows.Scan(&a.Time, &a.Count); err != nil {
  326. return
  327. }
  328. a.Time = a.Time[:10]
  329. list = append(list, a)
  330. }
  331. err = rows.Err()
  332. return
  333. }
  334. // MngPercent .
  335. func (d *Dao) MngPercent(ctx context.Context, c *model.MngCount) (list []*model.MngPercentRes, err error) {
  336. list = []*model.MngPercentRes{}
  337. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  338. rows, err := d.db.Query(ctx, _mngPercentSQL, c.Business, c.Type, yesterday)
  339. if err != nil {
  340. return
  341. }
  342. defer rows.Close()
  343. for rows.Next() {
  344. a := &model.MngPercentRes{}
  345. if err = rows.Scan(&a.Name, &a.Count); err != nil {
  346. return
  347. }
  348. list = append(list, a)
  349. }
  350. err = rows.Err()
  351. return
  352. }
  353. // Unames .
  354. func (d *Dao) Unames(c context.Context, uids []string) (res *model.UnamesData, err error) {
  355. params := url.Values{}
  356. params.Set("uids", strings.Join(uids, ","))
  357. if err = d.client.Get(c, d.managerUnames, "", params, &res); err != nil {
  358. return
  359. }
  360. if res.Code != 0 {
  361. return
  362. }
  363. return
  364. }