mysql.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "time"
  8. "go-common/app/admin/main/spy/conf"
  9. "go-common/app/admin/main/spy/model"
  10. xsql "go-common/library/database/sql"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _eventSQL = "SELECT id,name,nick_name,service_id,ctime,mtime FROM spy_event WHERE name=? AND deleted = 0 LIMIT 1;"
  15. _allGroupSQL = "SELECT id, name, ctime FROM spy_factor_group;"
  16. _updateFactorSQL = "UPDATE spy_factor SET factor_val = ? WHERE id = ?;"
  17. _factorAllSQL = "SELECT id,nick_name,service_id,event_id,group_id,risk_level,factor_val,ctime,mtime FROM spy_factor WHERE group_id = ? ORDER BY factor_val;"
  18. _getList = "SELECT id,mid,event_id,score,base_score,event_score,remark,reason,factor_val,ctime FROM spy_user_event_history_%02d WHERE %s ORDER BY id DESC LIMIT %d,%d;"
  19. _getListTc = "SELECT COUNT(1) FROM spy_user_event_history_%02d WHERE %s;"
  20. _addLogSQL = "INSERT INTO `spy_log`(`name`,`module`,`context`,`ref_id`,`ctime`)VALUES(?,?,?,?,?);"
  21. _getSettingListSQL = "SELECT id,property,name,val,ctime,mtime FROM spy_system_config"
  22. _updateSettingSQL = "UPDATE spy_system_config SET val=? WHERE property=?"
  23. _getUserInfoSQL = "SELECT id,mid,score,base_score,event_score,state,relive_times,mtime FROM spy_user_info_%02d WHERE mid=?"
  24. _addFactorSQL = "INSERT INTO `spy_factor`(`nick_name`,`service_id`,`event_id`,`group_id`,`risk_level`,`factor_val`,`category_id`,`ctime`)VALUES(?,?,?,?,?,?,?,?);"
  25. _addEventSQL = "INSERT INTO `spy_event`(`name`,`nick_name`,`service_id`,`status`,`ctime`,`mtime`)VALUES(?,?,?,?,?,?);"
  26. _addServiceSQL = "INSERT INTO `spy_service`(`name`,`nick_name`,`status`,`ctime`,`mtime`)VALUES(?,?,?,?,?);"
  27. _addGroupSQL = "INSERT INTO `spy_factor_group`(`name`,`ctime`)VALUES(?,?);"
  28. _getReportList = "SELECT id, name, date_version, val, ctime FROM spy_report limit ?,?;"
  29. _getReportCount = "SELECT COUNT(1) FROM spy_report;"
  30. _updateStatStateSQL = "UPDATE spy_statistics SET state=? WHERE id=?"
  31. _updateStatQuantitySQL = "UPDATE spy_statistics SET quantity=? WHERE id=?"
  32. _updateStatDeleteSQL = "UPDATE spy_statistics SET isdel=? WHERE id=?"
  33. _statByIDSQL = "SELECT id,target_mid,target_id,event_id,state,type,quantity,isdel,ctime,mtime FROM spy_statistics WHERE id = ?;"
  34. _logListSQL = "SELECT id,ref_id,name,module,context,ctime FROM spy_log WHERE ref_id = ? AND module = ?;"
  35. _statListByMidSQL = "SELECT id,target_mid,target_id,event_id,state,type,quantity,isdel,ctime,mtime FROM spy_statistics WHERE target_mid = ? AND isdel = 0 ORDER BY id desc limit ?,?;"
  36. _statListByIDSQL = "SELECT id,target_mid,target_id,event_id,state,type,quantity,isdel,ctime,mtime FROM spy_statistics WHERE target_id = ? AND type = ? AND isdel = 0 ORDER BY id desc limit ?,?;"
  37. _statCountByMidSQL = "SELECT COUNT(1) FROM spy_statistics WHERE target_mid = ? AND isdel = 0;"
  38. _statCountByIDSQL = "SELECT COUNT(1) FROM spy_statistics WHERE target_id = ? AND type = ? AND isdel = 0;"
  39. _allEventSQL = "SELECT id,name,nick_name,service_id,status,ctime,mtime FROM spy_event WHERE status<>0"
  40. _updateEventNameSQL = "UPDATE spy_event SET nick_name = ? WHERE id = ?;"
  41. )
  42. // Event get event from db.
  43. func (d *Dao) Event(ctx context.Context, eventName string) (event *model.Event, err error) {
  44. var (
  45. row *xsql.Row
  46. )
  47. event = &model.Event{}
  48. row = d.eventStmt.QueryRow(ctx, eventName)
  49. if err = row.Scan(&event.ID, &event.Name, &event.NickName, &event.ServiceID, &event.CTime, &event.MTime); err != nil {
  50. if err == sql.ErrNoRows {
  51. err = nil
  52. return
  53. }
  54. log.Error("row.Scan() error(%v)", err)
  55. }
  56. return
  57. }
  58. // Factors get all factor info by group id.
  59. func (d *Dao) Factors(c context.Context, gid int64) (res []*model.Factor, err error) {
  60. var rows *xsql.Rows
  61. if rows, err = d.factorAllStmt.Query(c, gid); err != nil {
  62. log.Error("d.allTypesStmt.Query(%d) error(%v)", gid, err)
  63. return
  64. }
  65. defer rows.Close()
  66. for rows.Next() {
  67. r := new(model.Factor)
  68. if err = rows.Scan(&r.ID, &r.NickName, &r.ServiceID, &r.EventID, &r.GroupID, &r.RiskLevel, &r.FactorVal, &r.CTime, &r.MTime); err != nil {
  69. log.Error("row.Scan() error(%v)", err)
  70. res = nil
  71. return
  72. }
  73. res = append(res, r)
  74. }
  75. err = rows.Err()
  76. return
  77. }
  78. // Groups get all group info.
  79. func (d *Dao) Groups(c context.Context) (res []*model.FactorGroup, err error) {
  80. var rows *xsql.Rows
  81. if rows, err = d.allGroupStmt.Query(c); err != nil {
  82. log.Error("d.allGroupStmt.Query error(%v)", err)
  83. return
  84. }
  85. defer rows.Close()
  86. for rows.Next() {
  87. r := new(model.FactorGroup)
  88. if err = rows.Scan(&r.ID, &r.Name, &r.CTime); err != nil {
  89. log.Error("row.Scan() error(%v)", err)
  90. res = nil
  91. return
  92. }
  93. res = append(res, r)
  94. }
  95. err = rows.Err()
  96. return
  97. }
  98. // UpdateFactor update factor.
  99. func (d *Dao) UpdateFactor(c context.Context, factorVal float32, id int64) (affected int64, err error) {
  100. var res sql.Result
  101. if res, err = d.db.Exec(c, _updateFactorSQL, factorVal, id); err != nil {
  102. log.Error("_updateFactorSQL: db.Exec(%v, %d) error(%v)", factorVal, id, err)
  103. return
  104. }
  105. return res.RowsAffected()
  106. }
  107. func hitHistory(id int64) int64 {
  108. return id % conf.Conf.Property.HistoryShard
  109. }
  110. // genListSQL get history sql
  111. func (d *Dao) genListSQL(SQLType string, h *model.HisParamReq) (SQL string, values []interface{}) {
  112. values = make([]interface{}, 0, 1)
  113. cond := " mid = ?"
  114. values = append(values, h.Mid)
  115. switch SQLType {
  116. case "list":
  117. SQL = fmt.Sprintf(_getList, hitHistory(h.Mid), cond, (h.Pn-1)*h.Ps, h.Ps)
  118. case "count":
  119. SQL = fmt.Sprintf(_getListTc, hitHistory(h.Mid), cond)
  120. }
  121. return
  122. }
  123. //HistoryPage user event history.
  124. func (d *Dao) HistoryPage(c context.Context, h *model.HisParamReq) (hs []*model.EventHistoryDto, err error) {
  125. SQL, values := d.genListSQL("list", h)
  126. rows, err := d.db.Query(c, SQL, values...)
  127. if err != nil {
  128. log.Error("dao.QuestionPage(%v,%v) error(%v)", SQL, values, err)
  129. return
  130. }
  131. defer rows.Close()
  132. hs = make([]*model.EventHistoryDto, 0, h.Ps)
  133. for rows.Next() {
  134. hdb := &model.EventHistory{}
  135. err = rows.Scan(&hdb.ID, &hdb.Mid, &hdb.EventID, &hdb.Score, &hdb.BaseScore, &hdb.EventScore,
  136. &hdb.Remark, &hdb.Reason, &hdb.FactorVal, &hdb.Ctime)
  137. eventMSG := &model.EventMessage{}
  138. if err = json.Unmarshal([]byte(hdb.Remark), eventMSG); err != nil {
  139. log.Error("json.Unmarshal(%s) error(%v)", hdb.Remark, err)
  140. } else {
  141. hdb.TargetID = eventMSG.TargetID
  142. hdb.TargetMid = eventMSG.TargetMid
  143. }
  144. if err != nil {
  145. log.Error("rows.Scan error(%v)", err)
  146. return
  147. }
  148. h := &model.EventHistoryDto{
  149. ID: hdb.ID,
  150. Score: hdb.Score,
  151. BaseScore: hdb.BaseScore,
  152. EventScore: hdb.EventScore,
  153. Reason: hdb.Reason,
  154. Ctime: hdb.Ctime.Unix(),
  155. TargetID: hdb.TargetID,
  156. TargetMid: hdb.TargetMid,
  157. }
  158. if eventMSG.Time != 0 {
  159. _, offset := time.Now().Zone()
  160. t := time.Unix(eventMSG.Time, 0).Add(-time.Duration(offset) * time.Second)
  161. h.SpyTime = t.Unix()
  162. }
  163. hs = append(hs, h)
  164. }
  165. return
  166. }
  167. // HistoryPageTotalC user ecent history page.
  168. func (d *Dao) HistoryPageTotalC(c context.Context, h *model.HisParamReq) (totalCount int, err error) {
  169. SQL, values := d.genListSQL("count", h)
  170. row := d.db.QueryRow(c, SQL, values...)
  171. if err = row.Scan(&totalCount); err != nil {
  172. if err == sql.ErrNoRows {
  173. row = nil
  174. err = nil
  175. } else {
  176. log.Error("row.Scan error(%v)", err)
  177. }
  178. }
  179. return
  180. }
  181. //AddLog add log.
  182. func (d *Dao) AddLog(c context.Context, l *model.Log) (affected int64, err error) {
  183. var res sql.Result
  184. if res, err = d.db.Exec(c, _addLogSQL, l.Name, l.Module, l.Context, l.RefID, l.Ctime); err != nil {
  185. fmt.Println("add log ", err)
  186. log.Error("add question: d.db.Exec(%v) error(%v)", l, err)
  187. return
  188. }
  189. return res.RowsAffected()
  190. }
  191. // SettingList get all setting list
  192. func (d *Dao) SettingList(c context.Context) (list []*model.Setting, err error) {
  193. var (
  194. rows *xsql.Rows
  195. )
  196. list = make([]*model.Setting, 0)
  197. if rows, err = d.db.Query(c, _getSettingListSQL); err != nil {
  198. log.Error("d.db.Query(%s) error(%v)", _getSettingListSQL, err)
  199. return
  200. }
  201. defer rows.Close()
  202. for rows.Next() {
  203. var setting = &model.Setting{}
  204. if err = rows.Scan(&setting.ID, &setting.Property, &setting.Name, &setting.Val, &setting.CTime, &setting.MTime); err != nil {
  205. log.Error("rows.Scan() error(%v)", err)
  206. return
  207. }
  208. list = append(list, setting)
  209. }
  210. return
  211. }
  212. // UpdateSetting update setting
  213. func (d *Dao) UpdateSetting(c context.Context, property string, val string) (affected int64, err error) {
  214. var res sql.Result
  215. if res, err = d.db.Exec(c, _updateSettingSQL, val, property); err != nil {
  216. log.Error("d.db.Exec(%s,%d,%s) error(%v)", _updateSettingSQL, val, property)
  217. return
  218. }
  219. if affected, err = res.RowsAffected(); err != nil {
  220. log.Error("res.RowsAffected() error(%v)", err)
  221. return
  222. }
  223. return
  224. }
  225. func hitInfo(id int64) int64 {
  226. return id % conf.Conf.Property.UserInfoShard
  227. }
  228. // Info get lastest user info by mid.
  229. func (d *Dao) Info(c context.Context, mid int64) (res *model.UserInfo, err error) {
  230. res = &model.UserInfo{}
  231. hitIndex := hitInfo(mid)
  232. row := d.getUserInfoStmt[hitIndex].QueryRow(c, mid)
  233. if err = row.Scan(&res.ID, &res.Mid, &res.Score, &res.BaseScore, &res.EventScore, &res.State, &res.ReliveTimes,
  234. &res.Mtime); err != nil {
  235. if err == sql.ErrNoRows {
  236. err = nil
  237. res = nil
  238. return
  239. }
  240. log.Error("Info:row.Scan() error(%v)", err)
  241. }
  242. return
  243. }
  244. //AddFactor add factor.
  245. func (d *Dao) AddFactor(c context.Context, f *model.Factor) (ret int64, err error) {
  246. var res sql.Result
  247. if res, err = d.db.Exec(c, _addFactorSQL, f.NickName, f.ServiceID, f.EventID, f.GroupID, f.RiskLevel, f.FactorVal, f.CategoryID, f.CTime); err != nil {
  248. log.Error("d.db AddFactor: d.db.Exec(%v) error(%v)", f, err)
  249. return
  250. }
  251. ret, err = res.RowsAffected()
  252. return
  253. }
  254. //AddEvent add event.
  255. func (d *Dao) AddEvent(c context.Context, f *model.Event) (ret int64, err error) {
  256. var res sql.Result
  257. if res, err = d.db.Exec(c, _addEventSQL, f.Name, f.NickName, f.ServiceID, f.Status, f.CTime, f.MTime); err != nil {
  258. log.Error("d.db AddEvent: d.db.Exec(%v) error(%v)", f, err)
  259. return
  260. }
  261. ret, err = res.RowsAffected()
  262. return
  263. }
  264. //AddService add service.
  265. func (d *Dao) AddService(c context.Context, f *model.Service) (ret int64, err error) {
  266. var res sql.Result
  267. if res, err = d.db.Exec(c, _addServiceSQL, f.Name, f.NickName, f.Status, f.CTime, f.MTime); err != nil {
  268. log.Error("d.db AddService: d.db.Exec(%v) error(%v)", f, err)
  269. return
  270. }
  271. ret, err = res.RowsAffected()
  272. return
  273. }
  274. //AddGroup add group.
  275. func (d *Dao) AddGroup(c context.Context, f *model.FactorGroup) (ret int64, err error) {
  276. var res sql.Result
  277. if res, err = d.db.Exec(c, _addGroupSQL, f.Name, f.CTime); err != nil {
  278. log.Error("d.db AddGroup: d.db.Exec(%v) error(%v)", f, err)
  279. return
  280. }
  281. ret, err = res.RowsAffected()
  282. return
  283. }
  284. // ReportList report list.
  285. func (d *Dao) ReportList(c context.Context, ps, pn int) (list []*model.ReportDto, err error) {
  286. var (
  287. rows *xsql.Rows
  288. )
  289. if ps == 0 || pn == 0 {
  290. ps = 8
  291. pn = 1
  292. }
  293. list = make([]*model.ReportDto, 0)
  294. if rows, err = d.db.Query(c, _getReportList, (pn-1)*ps, ps); err != nil {
  295. log.Error("d.db.Query(%s) error(%v)", _getReportList, err)
  296. return
  297. }
  298. defer rows.Close()
  299. for rows.Next() {
  300. var r = &model.Report{}
  301. if err = rows.Scan(&r.ID, &r.Name, &r.DateVersion, &r.Val, &r.Ctime); err != nil {
  302. log.Error("rows.Scan() error(%v)", err)
  303. return
  304. }
  305. list = append(list, &model.ReportDto{
  306. ID: r.ID,
  307. Name: r.Name,
  308. DateVersion: r.DateVersion,
  309. Val: r.Val,
  310. Ctime: r.Ctime.Unix(),
  311. })
  312. }
  313. return
  314. }
  315. // ReportCount get repoet total count.
  316. func (d *Dao) ReportCount(c context.Context) (totalCount int, err error) {
  317. var row = d.db.QueryRow(c, _getReportCount)
  318. if err = row.Scan(&totalCount); err != nil {
  319. if err == sql.ErrNoRows {
  320. row = nil
  321. err = nil
  322. } else {
  323. log.Error("row.Scan error(%v)", err)
  324. }
  325. }
  326. return
  327. }
  328. // UpdateStatState update stat state.
  329. func (d *Dao) UpdateStatState(c context.Context, state int8, id int64) (affected int64, err error) {
  330. var res sql.Result
  331. if res, err = d.db.Exec(c, _updateStatStateSQL, state, id); err != nil {
  332. log.Error("d.db.Exec(%s,%d,%s) error(%v)", _updateStatStateSQL, state, id)
  333. return
  334. }
  335. if affected, err = res.RowsAffected(); err != nil {
  336. log.Error("res.RowsAffected() error(%v)", err)
  337. return
  338. }
  339. return
  340. }
  341. // UpdateStatQuantity update stat quantity
  342. func (d *Dao) UpdateStatQuantity(c context.Context, count int64, id int64) (affected int64, err error) {
  343. var res sql.Result
  344. if res, err = d.db.Exec(c, _updateStatQuantitySQL, count, id); err != nil {
  345. log.Error("d.db.Exec(%s,%d,%s) error(%v)", _updateStatQuantitySQL, count, id)
  346. return
  347. }
  348. if affected, err = res.RowsAffected(); err != nil {
  349. log.Error("res.RowsAffected() error(%v)", err)
  350. return
  351. }
  352. return
  353. }
  354. // DeleteStat delete stat.
  355. func (d *Dao) DeleteStat(c context.Context, isdel int8, id int64) (affected int64, err error) {
  356. var res sql.Result
  357. if res, err = d.db.Exec(c, _updateStatDeleteSQL, isdel, id); err != nil {
  358. log.Error("d.db.Exec(%s,%d,%s) error(%v)", _updateStatDeleteSQL, isdel, id)
  359. return
  360. }
  361. if affected, err = res.RowsAffected(); err != nil {
  362. log.Error("res.RowsAffected() error(%v)", err)
  363. return
  364. }
  365. return
  366. }
  367. // Statistics get stat info by id from db.
  368. func (d *Dao) Statistics(c context.Context, id int64) (stat *model.Statistics, err error) {
  369. var (
  370. row *xsql.Row
  371. )
  372. stat = &model.Statistics{}
  373. row = d.db.QueryRow(c, _statByIDSQL, id)
  374. if err = row.Scan(&stat.ID, &stat.TargetMid, &stat.TargetID, &stat.EventID, &stat.State, &stat.Type, &stat.Quantity, &stat.Isdel, &stat.Ctime, &stat.Mtime); err != nil {
  375. if err == sql.ErrNoRows {
  376. err = nil
  377. stat = nil
  378. return
  379. }
  380. log.Error("row.Scan() error(%v)", err)
  381. }
  382. return
  383. }
  384. //LogList log list.
  385. func (d *Dao) LogList(c context.Context, refID int64, module int8) (list []*model.Log, err error) {
  386. var (
  387. rows *xsql.Rows
  388. )
  389. list = make([]*model.Log, 0)
  390. if rows, err = d.db.Query(c, _logListSQL, refID, module); err != nil {
  391. log.Error("d.db.Query(%s) error(%v)", _logListSQL, err)
  392. return
  393. }
  394. defer rows.Close()
  395. for rows.Next() {
  396. var r = &model.Log{}
  397. if err = rows.Scan(&r.ID, &r.RefID, &r.Name, &r.Module, &r.Context, &r.Ctime); err != nil {
  398. log.Error("rows.Scan() error(%v)", err)
  399. return
  400. }
  401. list = append(list, &model.Log{
  402. ID: r.ID,
  403. RefID: r.RefID,
  404. Name: r.Name,
  405. Module: r.Module,
  406. Context: r.Context,
  407. CtimeUnix: r.Ctime.Unix(),
  408. })
  409. }
  410. return
  411. }
  412. //StatListByMid stat list by mid.
  413. func (d *Dao) StatListByMid(c context.Context, mid int64, pn, ps int) (list []*model.Statistics, err error) {
  414. var (
  415. rows *xsql.Rows
  416. )
  417. if ps == 0 || pn == 0 {
  418. ps = 8
  419. pn = 1
  420. }
  421. list = make([]*model.Statistics, 0)
  422. if rows, err = d.db.Query(c, _statListByMidSQL, mid, (pn-1)*ps, ps); err != nil {
  423. log.Error("d.db.Query(%s) error(%v)", _statListByMidSQL, err)
  424. return
  425. }
  426. defer rows.Close()
  427. for rows.Next() {
  428. var stat = &model.Statistics{}
  429. if err = rows.Scan(&stat.ID, &stat.TargetMid, &stat.TargetID, &stat.EventID, &stat.State, &stat.Type, &stat.Quantity, &stat.Isdel, &stat.Ctime, &stat.Mtime); err != nil {
  430. log.Error("rows.Scan() error(%v)", err)
  431. return
  432. }
  433. list = append(list, &model.Statistics{
  434. ID: stat.ID,
  435. TargetMid: stat.TargetMid,
  436. TargetID: stat.TargetID,
  437. EventID: stat.EventID,
  438. Type: stat.Type,
  439. State: stat.State,
  440. Quantity: stat.Quantity,
  441. Isdel: stat.Isdel,
  442. Ctime: stat.Ctime,
  443. Mtime: stat.Mtime,
  444. CtimeUnix: stat.Ctime.Unix(),
  445. MtimeUnix: stat.Mtime.Unix(),
  446. })
  447. }
  448. return
  449. }
  450. //StatListByID stat list by id.
  451. func (d *Dao) StatListByID(c context.Context, id int64, t int8, pn, ps int) (list []*model.Statistics, err error) {
  452. var (
  453. rows *xsql.Rows
  454. )
  455. if ps == 0 || pn == 0 {
  456. ps = 8
  457. pn = 1
  458. }
  459. list = make([]*model.Statistics, 0)
  460. if rows, err = d.db.Query(c, _statListByIDSQL, id, t, (pn-1)*ps, ps); err != nil {
  461. log.Error("d.db.Query(%s) error(%v)", _statListByIDSQL, err)
  462. return
  463. }
  464. defer rows.Close()
  465. for rows.Next() {
  466. var stat = &model.Statistics{}
  467. if err = rows.Scan(&stat.ID, &stat.TargetMid, &stat.TargetID, &stat.EventID, &stat.State, &stat.Type, &stat.Quantity, &stat.Isdel, &stat.Ctime, &stat.Mtime); err != nil {
  468. log.Error("rows.Scan() error(%v)", err)
  469. return
  470. }
  471. list = append(list, &model.Statistics{
  472. ID: stat.ID,
  473. TargetMid: stat.TargetMid,
  474. TargetID: stat.TargetID,
  475. EventID: stat.EventID,
  476. Type: stat.Type,
  477. State: stat.State,
  478. Quantity: stat.Quantity,
  479. Isdel: stat.Isdel,
  480. Ctime: stat.Ctime,
  481. Mtime: stat.Mtime,
  482. CtimeUnix: stat.Ctime.Unix(),
  483. MtimeUnix: stat.Mtime.Unix(),
  484. })
  485. }
  486. return
  487. }
  488. // StatCountByMid count by mid.
  489. func (d *Dao) StatCountByMid(c context.Context, mid int64) (totalCount int64, err error) {
  490. row := d.db.QueryRow(c, _statCountByMidSQL, mid)
  491. if err = row.Scan(&totalCount); err != nil {
  492. if err == sql.ErrNoRows {
  493. row = nil
  494. err = nil
  495. } else {
  496. log.Error("row.Scan error(%v)", err)
  497. }
  498. }
  499. return
  500. }
  501. // StatCountByID count by id.
  502. func (d *Dao) StatCountByID(c context.Context, id int64, t int8) (totalCount int64, err error) {
  503. row := d.db.QueryRow(c, _statCountByIDSQL, id, t)
  504. if err = row.Scan(&totalCount); err != nil {
  505. if err == sql.ErrNoRows {
  506. row = nil
  507. err = nil
  508. } else {
  509. log.Error("row.Scan error(%v)", err)
  510. }
  511. }
  512. return
  513. }
  514. //AllEvent all event.
  515. func (d *Dao) AllEvent(c context.Context) (list []*model.Event, err error) {
  516. var (
  517. rows *xsql.Rows
  518. )
  519. list = make([]*model.Event, 0)
  520. if rows, err = d.db.Query(c, _allEventSQL); err != nil {
  521. log.Error("d.db.Query(%s) error(%v)", _allEventSQL, err)
  522. return
  523. }
  524. defer rows.Close()
  525. for rows.Next() {
  526. var event = &model.Event{}
  527. if err = rows.Scan(&event.ID, &event.Name, &event.NickName, &event.ServiceID, &event.Status, &event.CTime, &event.MTime); err != nil {
  528. log.Error("rows.Scan() error(%v)", err)
  529. return
  530. }
  531. list = append(list, &model.Event{
  532. ID: event.ID,
  533. Name: event.Name,
  534. NickName: event.NickName,
  535. ServiceID: event.ServiceID,
  536. Status: event.Status,
  537. CTime: event.CTime,
  538. MTime: event.MTime,
  539. })
  540. }
  541. return
  542. }
  543. // UpdateEventName update event name.
  544. func (d *Dao) UpdateEventName(c context.Context, e *model.Event) (affected int64, err error) {
  545. var res sql.Result
  546. if res, err = d.db.Exec(c, _updateEventNameSQL, e.NickName, e.ID); err != nil {
  547. log.Error("_updateEventNameSQL: db.Exec(%v, %d) error(%v)", e.NickName, e.ID, err)
  548. return
  549. }
  550. return res.RowsAffected()
  551. }