problem_info.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package autocode
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  5. autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  7. "gorm.io/gorm"
  8. )
  9. type ProblemInfoService struct {
  10. }
  11. // CreateProblemInfo 创建ProblemInfo记录
  12. // Author [piexlmax](https://github.com/piexlmax)
  13. func (problemInfoService *ProblemInfoService) CreateProblemInfo(problemInfo autocode.ProblemInfo) (err error) {
  14. err = global.GVA_DB.Create(&problemInfo).Error
  15. return err
  16. }
  17. // DeleteProblemInfo 删除ProblemInfo记录
  18. // Author [piexlmax](https://github.com/piexlmax)
  19. func (problemInfoService *ProblemInfoService) DeleteProblemInfo(problemInfo autocode.ProblemInfo) (err error) {
  20. err = global.GVA_DB.Delete(&problemInfo).Error
  21. return err
  22. }
  23. // DeleteProblemInfoByIds 批量删除ProblemInfo记录
  24. // Author [piexlmax](https://github.com/piexlmax)
  25. func (problemInfoService *ProblemInfoService) DeleteProblemInfoByIds(ids request.IdsReq) (err error) {
  26. err = global.GVA_DB.Delete(&[]autocode.ProblemInfo{}, "id in ?", ids.Ids).Error
  27. return err
  28. }
  29. // UpdateProblemInfo 更新ProblemInfo记录
  30. // Author [piexlmax](https://github.com/piexlmax)
  31. func (problemInfoService *ProblemInfoService) UpdateProblemInfo(problemInfo autocode.ProblemInfo) (err error) {
  32. err = global.GVA_DB.Where("id=?", problemInfo.ID).Save(problemInfo).Error
  33. return err
  34. }
  35. func (problemInfoService *ProblemInfoService) UpdateProblemInfoCount(id uint) (err error) {
  36. err = global.GVA_DB.Model(&autocode.ProblemInfo{}).Where("id=?", id).Update("count", gorm.Expr("count+1")).Error
  37. return err
  38. }
  39. // GetProblemInfo 根据id获取ProblemInfo记录
  40. // Author [piexlmax](https://github.com/piexlmax)
  41. func (problemInfoService *ProblemInfoService) GetProblemInfo(id uint) (err error, problemInfo autocode.ProblemInfo) {
  42. err = global.GVA_DB.Where("id = ?", id).First(&problemInfo).Error
  43. return
  44. }
  45. func (problemInfoService *ProblemInfoService) GetProblemInfoCount() (err error, countInfo []autocode.ProblemCount) {
  46. global.GVA_DB.Raw("SELECT statistics.matterid as matterid,pt.problem as problem,statistics.mattercount as mattercount,statistics.num as num,statistics.mattercount / num as proportion FROM ( SELECT substring_index( substring_index( a.matter, '|', b.help_topic_id + 1 ), '|',- 1 ) AS matterid, count( 1 ) AS mattercount," +
  47. " ( SELECT count( 1 ) FROM problem_info a JOIN mysql.help_topic b ON b.help_topic_id < ( length( a.matter ) - length( REPLACE ( a.matter, '|', '' ) ) + 1 ) WHERE ( a.matter != '' AND a.matter IS NOT NULL )) num FROM problem_info a JOIN mysql.help_topic b ON b.help_topic_id < ( length( a.matter ) - length( REPLACE ( a.matter, '|', '' ) ) + 1 ) " +
  48. " WHERE ( a.matter != '' AND a.matter IS NOT NULL ) GROUP BY matterid ) statistics INNER JOIN problem_type pt ON pt.id = statistics.matterid").Scan(&countInfo)
  49. return err, countInfo
  50. }
  51. func (problemInfoService *ProblemInfoService) GetProblemInfoSum() (err error, countInfo []autocode.ProblemSum) {
  52. global.GVA_DB.Raw("SELECT statistics1.unit_id unit_id,statistics1.unit_name unit_name,statistics1.mattercount totalnum,statistics2.mattercount handnum,statistics1.num,statistics2.mattercount/statistics1.mattercount handproportion FROM (" +
  53. "SELECT a.unit_id,a.unit_name,count(substring_index(substring_index(a.matter,'|',b.help_topic_id+1),'|',-1)) AS mattercount,(" +
  54. "SELECT count(1) FROM problem_info a JOIN mysql.help_topic b ON b.help_topic_id< (length(a.matter)-length(" +
  55. "REPLACE (a.matter,'|',''))+1) WHERE (a.matter !='' AND a.matter IS NOT NULL)) num FROM problem_info a JOIN mysql.help_topic b ON b.help_topic_id< (length(a.matter)-length(" +
  56. "REPLACE (a.matter,'|',''))+1) WHERE (a.matter !='' AND a.matter IS NOT NULL) GROUP BY unit_id) statistics1 INNER JOIN (" +
  57. "SELECT a.unit_id,count(substring_index(substring_index(a.matter,'|',b.help_topic_id+1),'|',-1)) AS mattercount FROM problem_info a JOIN mysql.help_topic b ON b.help_topic_id< (length(a.matter)-length(" +
  58. "REPLACE (a.matter,'|',''))+1) WHERE (a.matter !='' AND a.matter IS NOT NULL) AND a.`status` !='Untreated' GROUP BY unit_id) statistics2 ON statistics1.unit_id=statistics2.unit_id").Scan(&countInfo)
  59. return err, countInfo
  60. }
  61. func (problemInfoService *ProblemInfoService) GetPLaceRate() (err error, countInfo []autocode.PlaceRate) {
  62. global.GVA_DB.Raw("SELECT statistics.site_type site_type,dict.label label,statistics.mattercount mattercount,statistics.num num,statistics.mattercount/num proportion FROM (" +
  63. "SELECT a.site_type,count(substring_index(substring_index(a.matter,'|',b.help_topic_id+1),'|',-1)) AS mattercount,(" +
  64. "SELECT count(1) FROM problem_info a JOIN mysql.help_topic b ON b.help_topic_id< (length(a.matter)-length(" +
  65. "REPLACE (a.matter,'|',''))+1) WHERE (a.matter !='' AND a.matter IS NOT NULL)) num FROM problem_info a JOIN mysql.help_topic b ON b.help_topic_id< (length(a.matter)-length(" +
  66. " REPLACE (a.matter,'|',''))+1) WHERE (a.matter !='' AND a.matter IS NOT NULL) GROUP BY site_type) statistics INNER JOIN sys_dictionary_details dict ON dict.`value`=statistics.site_type AND dict.sys_dictionary_id=8 AND dict.deleted_at IS NULL;").Scan(&countInfo)
  67. return err, countInfo
  68. }
  69. // GetProblemInfoInfoList 分页获取ProblemInfo记录
  70. // Author [piexlmax](https://github.com/piexlmax)
  71. func (problemInfoService *ProblemInfoService) GetProblemInfoInfoList(info autoCodeReq.ProblemInfoSearch) (err error, infoList []autocode.ProblemInfo, total int64) {
  72. limit := info.PageSize
  73. offset := info.PageSize * (info.Page - 1)
  74. // 创建db
  75. db := global.GVA_DB.Model(&autocode.ProblemInfo{})
  76. if info.SiteType != "" {
  77. db.Where("site_type=?", info.SiteType)
  78. }
  79. if info.UnitId != nil && *info.UnitId != 0 {
  80. db.Where("unit_id=?", info.UnitId)
  81. }
  82. if info.Status != "" {
  83. db.Where("status=?", info.Status)
  84. }
  85. if info.Oper != 0 {
  86. db.Where("oper=?", info.Oper)
  87. }
  88. if !info.CreatedAtStart.IsZero() && !info.CreatedAtEnd.IsZero() {
  89. db.Where("created_at between ? and ?", info.CreatedAtStart, info.CreatedAtEnd)
  90. }
  91. var problemInfos []autocode.ProblemInfo
  92. // 如果有条件搜索 下方会自动创建搜索语句
  93. err = db.Count(&total).Error
  94. err = db.Limit(limit).Offset(offset).Order("created_at desc").Find(&problemInfos).Error
  95. return err, problemInfos, total
  96. }