db_sync.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/ep/saga/model"
  6. pkgerr "github.com/pkg/errors"
  7. )
  8. /*-------------------------------------- commit ----------------------------------------*/
  9. // HasCommit ...
  10. func (d *Dao) HasCommit(projID int, commitID string) (total int, err error) {
  11. err = pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ? AND commit_id = ?", projID, commitID).Count(&total).Error)
  12. return
  13. }
  14. // DelCommit ...
  15. func (d *Dao) DelCommit(projID int, commitID string) (err error) {
  16. return pkgerr.WithStack(d.db.Where("project_id = ? AND commit_id = ?", projID, commitID).Delete(&model.StatisticsCommits{}).Error)
  17. }
  18. // UpdateCommit ...
  19. func (d *Dao) UpdateCommit(projID int, commitID string, commit *model.StatisticsCommits) (err error) {
  20. return pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ? AND commit_id = ?", projID, commitID).Update(commit).Error)
  21. }
  22. // CreateCommit ...
  23. func (d *Dao) CreateCommit(req *model.StatisticsCommits) (err error) {
  24. return pkgerr.WithStack(d.db.Create(req).Error)
  25. }
  26. // QueryCommitByID query commit info by ID
  27. func (d *Dao) QueryCommitByID(projID int, commitID string) (commit *model.StatisticsCommits, err error) {
  28. commit = &model.StatisticsCommits{}
  29. err = pkgerr.WithStack(d.db.Where("project_id = ? AND commit_id = ?", projID, commitID).First(commit).Error)
  30. return
  31. }
  32. // CountCommitByTime ...
  33. func (d *Dao) CountCommitByTime(projID int, since, until string) (total int, err error) {
  34. err = pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until).Count(&total).Error)
  35. return
  36. }
  37. // QueryProjectCommits ...
  38. func (d *Dao) QueryProjectCommits(c context.Context, projectID int) (commits []*model.StatisticsCommits, err error) {
  39. err = pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ?", projectID).Find(&commits).Error)
  40. return
  41. }
  42. /*-------------------------------------- issue ----------------------------------------*/
  43. // HasIssue ...
  44. func (d *Dao) HasIssue(projID, issueID int) (total int, err error) {
  45. err = pkgerr.WithStack(d.db.Model(&model.StatisticsIssues{}).Where("project_id = ? AND issue_id = ?", projID, issueID).Count(&total).Error)
  46. return
  47. }
  48. // UpdateIssue ...
  49. func (d *Dao) UpdateIssue(projID, issueID int, issue *model.StatisticsIssues) (err error) {
  50. return pkgerr.WithStack(d.db.Model(&model.StatisticsIssues{}).Where("project_id = ? AND issue_id = ?", projID, issueID).Update(issue).Error)
  51. }
  52. // CreateIssue ...
  53. func (d *Dao) CreateIssue(req *model.StatisticsIssues) (err error) {
  54. return pkgerr.WithStack(d.db.Create(req).Error)
  55. }
  56. /*-------------------------------------- runner ----------------------------------------*/
  57. // HasRunner ...
  58. func (d *Dao) HasRunner(projID, runnerID int) (total int, err error) {
  59. err = pkgerr.WithStack(d.db.Model(&model.StatisticsRunners{}).Where("project_id = ? AND runner_id = ?", projID, runnerID).Count(&total).Error)
  60. return
  61. }
  62. // UpdateRunner ...
  63. func (d *Dao) UpdateRunner(projID, runnerID int, runner *model.StatisticsRunners) (err error) {
  64. return pkgerr.WithStack(d.db.Model(&model.StatisticsRunners{}).Where("project_id = ? AND runner_id = ?", projID, runnerID).Update(runner).Error)
  65. }
  66. // CreateRunner ...
  67. func (d *Dao) CreateRunner(req *model.StatisticsRunners) (err error) {
  68. return pkgerr.WithStack(d.db.Create(req).Error)
  69. }
  70. /*-------------------------------------- job ----------------------------------------*/
  71. // HasJob ...
  72. func (d *Dao) HasJob(projID, jobID int) (total int, err error) {
  73. err = pkgerr.WithStack(d.db.Model(&model.StatisticsJobs{}).Where("project_id = ? AND job_id=?", projID, jobID).Count(&total).Error)
  74. return
  75. }
  76. // UpdateJob ...
  77. func (d *Dao) UpdateJob(projID, jobID int, runner *model.StatisticsJobs) (err error) {
  78. return pkgerr.WithStack(d.db.Model(&model.StatisticsJobs{}).Where("project_id = ? AND job_id = ?", projID, jobID).Update(runner).Error)
  79. }
  80. // CreateJob ...
  81. func (d *Dao) CreateJob(req *model.StatisticsJobs) (err error) {
  82. return pkgerr.WithStack(d.db.Create(req).Error)
  83. }
  84. // QueryJobsByTime ...
  85. func (d *Dao) QueryJobsByTime(projID int, req *model.ProjectJobRequest, since, until string) (total int, jobs []*model.StatisticsJobs, err error) {
  86. gDB := d.db.Model(&model.StatisticsJobs{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until)
  87. if req.Branch != "" {
  88. gDB = gDB.Where("ref = ?", req.Branch)
  89. }
  90. if req.User != "" {
  91. gDB = gDB.Where("user_name = ?", req.User)
  92. }
  93. if req.Machine != "" {
  94. gDB = gDB.Where("runner_description = ?", req.Machine)
  95. }
  96. if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
  97. return
  98. }
  99. if err = pkgerr.WithStack(gDB.Find(&jobs).Error); err != nil {
  100. return
  101. }
  102. return
  103. }
  104. /*-------------------------------------- note ----------------------------------------*/
  105. // HasNote ...
  106. func (d *Dao) HasNote(c context.Context, projectID, noteID int) (total int, err error) {
  107. err = pkgerr.WithStack(d.db.Model(&model.StatisticsNotes{}).Where("project_id = ? AND note_id = ?", projectID, noteID).Count(&total).Error)
  108. return
  109. }
  110. // UpdateNote ...
  111. func (d *Dao) UpdateNote(c context.Context, projectID, noteID int, note *model.StatisticsNotes) (err error) {
  112. return pkgerr.WithStack(d.db.Model(&model.StatisticsNotes{}).Where("project_id = ? AND note_id = ?", projectID, noteID).Update(note).Error)
  113. }
  114. // CreateNote ...
  115. func (d *Dao) CreateNote(c context.Context, note *model.StatisticsNotes) (err error) {
  116. return pkgerr.WithStack(d.db.Create(note).Error)
  117. }
  118. // NoteByMRIID ...
  119. func (d *Dao) NoteByMRIID(c context.Context, projectID, mrIID int) (notes []*model.StatisticsNotes, err error) {
  120. err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? ", projectID, mrIID).Order("note_id desc").Find(&notes).Error)
  121. return
  122. }
  123. // NoteByID ...
  124. func (d *Dao) NoteByID(c context.Context, projectID, mrIID, noteID int) (note *model.StatisticsNotes, err error) {
  125. note = &model.StatisticsNotes{}
  126. err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? AND note_id = ?", projectID, mrIID, noteID).First(note).Error)
  127. return
  128. }
  129. /*-------------------------------------- mr ----------------------------------------*/
  130. // CountMRByTime ...
  131. func (d *Dao) CountMRByTime(projID int, since, until string) (total int, err error) {
  132. err = pkgerr.WithStack(d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until).Count(&total).Error)
  133. return
  134. }
  135. // MRByProjectID query mr by projectID
  136. func (d *Dao) MRByProjectID(c context.Context, projectID int, since *time.Time, until *time.Time) (mrs []*model.StatisticsMrs, err error) {
  137. gDB := d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? ", projectID)
  138. if since != nil || until != nil {
  139. gDB = gDB.Where("updated_at >= ? AND updated_at <= ?", since, until)
  140. }
  141. err = pkgerr.WithStack(gDB.Find(&mrs).Error)
  142. return
  143. }
  144. // HasMR ...
  145. func (d *Dao) HasMR(c context.Context, projectID, mrIID int) (total int, err error) {
  146. err = pkgerr.WithStack(d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? AND mr_iid = ?", projectID, mrIID).Count(&total).Error)
  147. return
  148. }
  149. // UpdateMR update
  150. func (d *Dao) UpdateMR(c context.Context, projectID, mrIID int, mr *model.StatisticsMrs) (err error) {
  151. return pkgerr.WithStack(d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? AND mr_iid = ?", projectID, mrIID).Update(mr).Error)
  152. }
  153. // CreateMR ...
  154. func (d *Dao) CreateMR(c context.Context, req *model.StatisticsMrs) (err error) {
  155. return pkgerr.WithStack(d.db.Create(req).Error)
  156. }
  157. /*-------------------------------------- aggMR ----------------------------------------*/
  158. // HasAggregateReviewer ...
  159. func (d *Dao) HasAggregateReviewer(c context.Context, projectID, mrIID, reviewerID, reviewID int) (total int, err error) {
  160. err = pkgerr.WithStack(d.db.Model(&model.AggregateMrReviewer{}).Where("project_id = ? and mr_iid = ? and reviewer_id=? and review_id = ? ", projectID, mrIID, reviewerID, reviewID).Count(&total).Error)
  161. return
  162. }
  163. // UpdateAggregateReviewer ...
  164. func (d *Dao) UpdateAggregateReviewer(c context.Context, projectID, mrIID, reviewerID, reviewID int, aggregateReviewer *model.AggregateMrReviewer) (err error) {
  165. return pkgerr.WithStack(d.db.Model(&model.AggregateMrReviewer{}).Where("project_id = ? and mr_iid = ? and reviewer_id=? and review_id = ? ", projectID, mrIID, reviewerID, reviewID).Update(aggregateReviewer).Error)
  166. }
  167. // CreateAggregateReviewer ...
  168. func (d *Dao) CreateAggregateReviewer(c context.Context, req *model.AggregateMrReviewer) (err error) {
  169. return pkgerr.WithStack(d.db.Create(req).Error)
  170. }
  171. /*-------------------------------------- pipeline ----------------------------------------*/
  172. // HasPipeline ...
  173. func (d *Dao) HasPipeline(projID int, pipelineID int) (total int, err error) {
  174. err = pkgerr.WithStack(d.db.Model(&model.StatisticsPipeline{}).Where("project_id = ? AND pipeline_id = ?", projID, pipelineID).Count(&total).Error)
  175. return
  176. }
  177. // UpdatePipeline ...
  178. func (d *Dao) UpdatePipeline(projID int, pipelineID int, pipeline *model.StatisticsPipeline) (err error) {
  179. return pkgerr.WithStack(d.db.Model(&model.StatisticsPipeline{}).Where("project_id = ? AND pipeline_id = ?", projID, pipelineID).Update(pipeline).Error)
  180. }
  181. // CreatePipeline ...
  182. func (d *Dao) CreatePipeline(req *model.StatisticsPipeline) (err error) {
  183. return pkgerr.WithStack(d.db.Create(req).Error)
  184. }
  185. // QueryPipelinesByTime ...
  186. func (d *Dao) QueryPipelinesByTime(projID int, req *model.PipelineDataReq, since, until string) (total, statNum int, pipelines []*model.StatisticsPipeline, err error) {
  187. gDB := d.db.Model(&model.StatisticsPipeline{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until)
  188. if req.Branch != "" {
  189. gDB = gDB.Where("ref = ?", req.Branch)
  190. }
  191. if req.User != "" {
  192. gDB = gDB.Where("user = ?", req.User)
  193. }
  194. if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
  195. return
  196. }
  197. if req.State != "" {
  198. gDB = gDB.Where("status = ?", req.State)
  199. }
  200. if err = pkgerr.WithStack(gDB.Count(&statNum).Error); err != nil {
  201. return
  202. }
  203. if err = pkgerr.WithStack(gDB.Find(&pipelines).Error); err != nil {
  204. return
  205. }
  206. return
  207. }
  208. /*-------------------------------------- member ----------------------------------------*/
  209. // HasMember ...
  210. func (d *Dao) HasMember(c context.Context, projectID, memberID int) (total int, err error) {
  211. err = pkgerr.WithStack(d.db.Model(&model.StatisticsMembers{}).Where("project_id = ? AND member_id = ?", projectID, memberID).Count(&total).Error)
  212. return
  213. }
  214. // UpdateMember ...
  215. func (d *Dao) UpdateMember(c context.Context, projectID, memberID int, member *model.StatisticsMembers) (err error) {
  216. err = pkgerr.WithStack(d.db.Model(&model.StatisticsMembers{}).Where("project_id = ? AND member_id = ?", projectID, memberID).Update(member).Error)
  217. return
  218. }
  219. // QueryMemberByID ...
  220. func (d *Dao) QueryMemberByID(c context.Context, projectID, memberID int) (member *model.StatisticsMembers, err error) {
  221. err = pkgerr.WithStack(d.db.Model(&model.StatisticsMembers{}).Where("project_id = ? AND member_id = ?", projectID, memberID).First(&member).Error)
  222. return
  223. }
  224. // CreateMember ...
  225. func (d *Dao) CreateMember(c context.Context, member *model.StatisticsMembers) (err error) {
  226. return pkgerr.WithStack(d.db.Create(member).Error)
  227. }
  228. /*-------------------------------------- emoji ----------------------------------------*/
  229. // HasMRAwardEmoji ...
  230. func (d *Dao) HasMRAwardEmoji(c context.Context, projectID, mrIID, awardEmojiID int) (total int, err error) {
  231. err = pkgerr.WithStack(d.db.Model(&model.StatisticsMRAwardEmojis{}).Where("project_id = ? and mr_iid = ? and award_emoji_id=? ", projectID, mrIID, awardEmojiID).Count(&total).Error)
  232. return
  233. }
  234. // UpdateMRAwardEmoji update
  235. func (d *Dao) UpdateMRAwardEmoji(c context.Context, projectID, mrIID, awardEmojiID int, awardEmoji *model.StatisticsMRAwardEmojis) (err error) {
  236. return pkgerr.WithStack(d.db.Model(&model.StatisticsMRAwardEmojis{}).Where("project_id = ? and mr_iid = ? and award_emoji_id=? ", projectID, mrIID, awardEmojiID).Update(awardEmoji).Error)
  237. }
  238. // CreateMRAwardEmoji ...
  239. func (d *Dao) CreateMRAwardEmoji(c context.Context, awardEmoji *model.StatisticsMRAwardEmojis) (err error) {
  240. return pkgerr.WithStack(d.db.Create(awardEmoji).Error)
  241. }
  242. // AwardEmojiByMRIID ...
  243. func (d *Dao) AwardEmojiByMRIID(c context.Context, projectID, mrIID int) (AwardEmojis []*model.StatisticsMRAwardEmojis, err error) {
  244. err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? ", projectID, mrIID).Find(&AwardEmojis).Error)
  245. return
  246. }
  247. /*-------------------------------------- discussion ----------------------------------------*/
  248. // HasDiscussion ...
  249. func (d *Dao) HasDiscussion(c context.Context, projectID, mrIID int, discussionID string) (total int, err error) {
  250. err = pkgerr.WithStack(d.db.Model(&model.StatisticsDiscussions{}).Where("project_id = ? and mr_iid = ? and discussion_id=? ", projectID, mrIID, discussionID).Count(&total).Error)
  251. return
  252. }
  253. // UpdateDiscussion ...
  254. func (d *Dao) UpdateDiscussion(c context.Context, projectID, mrIID int, discussionID string, discussion *model.StatisticsDiscussions) (err error) {
  255. err = pkgerr.WithStack(d.db.Model(&model.StatisticsDiscussions{}).Where("project_id = ? and mr_iid = ? and discussion_id=? ", projectID, mrIID, discussionID).Update(discussion).Error)
  256. return
  257. }
  258. // CreateDiscussion ...
  259. func (d *Dao) CreateDiscussion(c context.Context, discussion *model.StatisticsDiscussions) (err error) {
  260. return pkgerr.WithStack(d.db.Create(discussion).Error)
  261. }
  262. // DiscussionsByMRIID ...
  263. func (d *Dao) DiscussionsByMRIID(c context.Context, projectID, mrIID int) (discussions []*model.StatisticsDiscussions, err error) {
  264. err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? ", projectID, mrIID).Find(&discussions).Error)
  265. return
  266. }
  267. /*-------------------------------------- Branch ----------------------------------------*/
  268. // DeleteProjectBranch ...
  269. func (d *Dao) DeleteProjectBranch(c context.Context, projectID int) error {
  270. return pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ?", projectID).Update("is_deleted", model.BranchDeleted).Error)
  271. }
  272. // HasBranch is exist project branch info in database.
  273. func (d *Dao) HasBranch(c context.Context, projectID int, branchName string) (total int, err error) {
  274. err = pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).Count(&total).Error)
  275. return
  276. }
  277. // UpdateBranch update
  278. func (d *Dao) UpdateBranch(c context.Context, projectID int, branchName string, branch *model.StatisticsBranches) error {
  279. return pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).Save(branch).Error)
  280. }
  281. // CreateBranch query all the records in contact_infos
  282. func (d *Dao) CreateBranch(c context.Context, branch *model.StatisticsBranches) error {
  283. return pkgerr.WithStack(d.db.Create(branch).Error)
  284. }
  285. // QueryProjectBranch ...
  286. func (d *Dao) QueryProjectBranch(c context.Context, projectID int) (b []*model.StatisticsBranches, err error) {
  287. err = pkgerr.WithStack(d.db.Where("project_id = ? and is_deleted != ?", projectID, model.BranchDeleted).Find(&b).Error)
  288. return
  289. }
  290. // QueryFirstBranch ...
  291. func (d *Dao) QueryFirstBranch(c context.Context, projectID int, branchName string) (branch *model.StatisticsBranches, err error) {
  292. branch = &model.StatisticsBranches{}
  293. err = pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).First(branch).Error)
  294. return
  295. }
  296. // DeleteAggregateBranch ...
  297. func (d *Dao) DeleteAggregateBranch(c context.Context, projectID int) error {
  298. return pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ?", projectID).Update("is_deleted", model.BranchDeleted).Error)
  299. }
  300. // HasAggregateBranch ...
  301. func (d *Dao) HasAggregateBranch(c context.Context, projectID int, branchName string) (total int, err error) {
  302. err = pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).Count(&total).Error)
  303. return
  304. }
  305. // UpdateAggregateBranch ...
  306. func (d *Dao) UpdateAggregateBranch(c context.Context, projectID int, branchName string, aggregateBranch *model.AggregateBranches) error {
  307. return pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ? AND branch_name = ?", projectID, branchName).Save(aggregateBranch).Error)
  308. }
  309. // CreateAggregateBranch ...
  310. func (d *Dao) CreateAggregateBranch(c context.Context, aggregateBranch *model.AggregateBranches) error {
  311. return pkgerr.WithStack(d.db.Create(aggregateBranch).Error)
  312. }
  313. // QueryFirstAggregateBranch ...
  314. func (d *Dao) QueryFirstAggregateBranch(c context.Context, projectID int, branchName string) (branch *model.AggregateBranches, err error) {
  315. branch = &model.AggregateBranches{}
  316. err = pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).First(branch).Error)
  317. return
  318. }