problem_info.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package autocode
  2. import (
  3. global "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. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  8. "github.com/flipped-aurora/gin-vue-admin/server/service"
  9. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  10. "github.com/gin-gonic/gin"
  11. "github.com/silenceper/wechat/v2/officialaccount/message"
  12. "go.uber.org/zap"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. type ProblemInfoApi struct {
  18. }
  19. var problemInfoService = service.ServiceGroupApp.AutoCodeServiceGroup.ProblemInfoService
  20. // CreateProblemInfo 创建ProblemInfo
  21. // @Tags ProblemInfo
  22. // @Summary 创建ProblemInfo
  23. // @Security ApiKeyAuth
  24. // @accept application/json
  25. // @Produce application/json
  26. // @Param data body autocode.ProblemInfo true "创建ProblemInfo"
  27. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  28. // @Router /problemInfo/createProblemInfo [post]
  29. func (problemInfoApi *ProblemInfoApi) CreateProblemInfo(c *gin.Context) {
  30. userId := utils.GetUserID(c)
  31. var problemInfo autocode.ProblemInfo
  32. _ = c.ShouldBindJSON(&problemInfo)
  33. problemInfo.Status = "Untreated"
  34. if err, place := service.ServiceGroupApp.AutoCodeServiceGroup.PlaceService.GetPlace(problemInfo.SiteId); err != nil {
  35. response.FailWithMessage("创建失败,选择站点错误", c)
  36. } else {
  37. problemInfo.Oper = userId
  38. problemInfo.SiteType = place.Type
  39. problemInfo.Position = place.Position
  40. problemInfo.SiteName = place.Name
  41. problemInfo.Region = place.Region
  42. }
  43. if problemInfo.UnitId != nil && *problemInfo.UnitId != 0 {
  44. probleTypeIds := strings.Split(problemInfo.Matter, "|")
  45. if len(probleTypeIds) > 0 {
  46. _, proTypes := problemTypeService.GetProblemTypeIds(probleTypeIds)
  47. var integral int
  48. for _, proType := range proTypes {
  49. inter := *proType.Integral
  50. integral = integral + inter
  51. }
  52. problemInfo.Integral = &integral
  53. _, unit := unitService.GetUnit(uint(*problemInfo.UnitId))
  54. oldIntegral := *unit.UnitIntegral
  55. nowIntegral := oldIntegral - integral
  56. unit.UnitIntegral = &nowIntegral
  57. unitService.UpdateUnitA(unit)
  58. }
  59. }
  60. if err := problemInfoService.CreateProblemInfo(problemInfo); err != nil {
  61. global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
  62. response.FailWithMessage("发布失败", c)
  63. } else {
  64. response.OkWithMessage("发布成功", c)
  65. }
  66. }
  67. func (problemInfoApi *ProblemInfoApi) ExportExcel(c *gin.Context) {
  68. var problemInfo autocodeReq.ProblemInfoSearch
  69. _ = c.ShouldBindJSON(&problemInfo)
  70. filePath := global.GVA_CONFIG.Excel.Dir + strconv.FormatInt(time.Now().Unix(), 10) + ".xlsx"
  71. if err, list, _ := problemInfoService.GetProblemInfoInfoList(problemInfo); err != nil {
  72. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  73. response.FailWithMessage("获取失败", c)
  74. } else {
  75. _, siteType := service.ServiceGroupApp.SystemServiceGroup.DictionaryService.GetSysDictionary("site_type", 0)
  76. _, proList := problemTypeService.GetProblemTypeInfoListAll()
  77. err := service.ServiceGroupApp.ExampleServiceGroup.ExcelService.ProblemInfoList2Excel(list, filePath, siteType, proList)
  78. if err != nil {
  79. global.GVA_LOG.Error("转换Excel失败!", zap.Any("err", err))
  80. response.FailWithMessage("转换Excel失败", c)
  81. return
  82. }
  83. c.Writer.Header().Add("success", "true")
  84. c.File(filePath)
  85. }
  86. }
  87. func (problemInfoApi *ProblemInfoApi) SendUser(c *gin.Context) {
  88. var sendUser autocodeReq.SendUser
  89. _ = c.ShouldBindJSON(&sendUser)
  90. _, problem := problemInfoService.GetProblemInfo(sendUser.ProblemID)
  91. _, oper := service.ServiceGroupApp.SystemServiceGroup.FindUserById(int(problem.Oper))
  92. appid := global.GVA_CONFIG.Wxxcx.Appid
  93. for _, id := range sendUser.ID {
  94. userId, _ := strconv.Atoi(id)
  95. _, user := service.ServiceGroupApp.SystemServiceGroup.FindUserById(userId)
  96. if user.WechatId == "" {
  97. continue
  98. }
  99. msg := &message.TemplateMessage{
  100. ToUser: user.WechatId,
  101. TemplateID: global.GVA_CONFIG.Wxxcx.Msgid,
  102. Data: map[string]*message.TemplateDataItem{
  103. "first": {Value: "有新的站点问题"},
  104. "keyword1": {Value: oper.Username},
  105. "keyword2": {Value: problem.SiteName},
  106. "keyword3": {Value: problem.Department},
  107. "keyword4": {Value: problem.CreatedAt.Format("2006-01-02 15:04:05")},
  108. "remark": {Value: "点击查看问题"},
  109. },
  110. MiniProgram: struct {
  111. AppID string `json:"appid"`
  112. PagePath string `json:"pagepath"`
  113. }{AppID: appid, PagePath: "/pages/public-details?id=" + strconv.Itoa(int(sendUser.ProblemID))},
  114. }
  115. if _, err := global.GVA_WECHAT.GetTemplate().Send(msg); err != nil {
  116. global.GVA_LOG.Error("微信模板通知失败!", zap.Any("err", err))
  117. }
  118. }
  119. response.OkWithMessage("发送成功", c)
  120. }
  121. // DeleteProblemInfo 删除ProblemInfo
  122. // @Tags ProblemInfo
  123. // @Summary 删除ProblemInfo
  124. // @Security ApiKeyAuth
  125. // @accept application/json
  126. // @Produce application/json
  127. // @Param data body autocode.ProblemInfo true "删除ProblemInfo"
  128. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  129. // @Router /problemInfo/deleteProblemInfo [delete]
  130. func (problemInfoApi *ProblemInfoApi) DeleteProblemInfo(c *gin.Context) {
  131. var problemInfo autocode.ProblemInfo
  132. _ = c.ShouldBindJSON(&problemInfo)
  133. if err := problemInfoService.DeleteProblemInfo(problemInfo); err != nil {
  134. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  135. response.FailWithMessage("删除失败", c)
  136. } else {
  137. response.OkWithMessage("删除成功", c)
  138. }
  139. }
  140. // DeleteProblemInfoByIds 批量删除ProblemInfo
  141. // @Tags ProblemInfo
  142. // @Summary 批量删除ProblemInfo
  143. // @Security ApiKeyAuth
  144. // @accept application/json
  145. // @Produce application/json
  146. // @Param data body request.IdsReq true "批量删除ProblemInfo"
  147. // @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
  148. // @Router /problemInfo/deleteProblemInfoByIds [delete]
  149. func (problemInfoApi *ProblemInfoApi) DeleteProblemInfoByIds(c *gin.Context) {
  150. var IDS request.IdsReq
  151. _ = c.ShouldBindJSON(&IDS)
  152. if err := problemInfoService.DeleteProblemInfoByIds(IDS); err != nil {
  153. global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
  154. response.FailWithMessage("批量删除失败", c)
  155. } else {
  156. response.OkWithMessage("批量删除成功", c)
  157. }
  158. }
  159. // UpdateProblemInfo 更新ProblemInfo
  160. // @Tags ProblemInfo
  161. // @Summary 更新ProblemInfo
  162. // @Security ApiKeyAuth
  163. // @accept application/json
  164. // @Produce application/json
  165. // @Param data body autocode.ProblemInfo true "更新ProblemInfo"
  166. // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
  167. // @Router /problemInfo/updateProblemInfo [put]
  168. func (problemInfoApi *ProblemInfoApi) UpdateProblemInfo(c *gin.Context) {
  169. var problemInfo autocode.ProblemInfo
  170. _ = c.ShouldBindJSON(&problemInfo)
  171. _, problemInfoNow := problemInfoService.GetProblemInfo(problemInfo.ID)
  172. problemInfoNow.Handler = problemInfo.Handler
  173. problemInfoNow.HandImgs = problemInfo.HandImgs
  174. problemInfoNow.HandText = problemInfo.HandText
  175. problemInfoNow.Status = "Processed"
  176. if err := problemInfoService.UpdateProblemInfo(problemInfoNow); err != nil {
  177. global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
  178. response.FailWithMessage("更新失败", c)
  179. } else {
  180. response.OkWithMessage("更新成功", c)
  181. }
  182. }
  183. // FindProblemInfo 用id查询ProblemInfo
  184. // @Tags ProblemInfo
  185. // @Summary 用id查询ProblemInfo
  186. // @Security ApiKeyAuth
  187. // @accept application/json
  188. // @Produce application/json
  189. // @Param data query autocode.ProblemInfo true "用id查询ProblemInfo"
  190. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
  191. // @Router /problemInfo/findProblemInfo [get]
  192. func (problemInfoApi *ProblemInfoApi) FindProblemInfo(c *gin.Context) {
  193. var problemInfo autocode.ProblemInfo
  194. _ = c.ShouldBindQuery(&problemInfo)
  195. if err, reproblemInfo := problemInfoService.GetProblemInfo(problemInfo.ID); err != nil {
  196. global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
  197. response.FailWithMessage("查询失败", c)
  198. } else {
  199. //param := reqMode.SysDictionarySearch{, request.PageInfo{PageSize: 9999, Page: 0}}
  200. _, reproblemInfo.MatterList, _ = service.ServiceGroupApp.AutoCodeServiceGroup.GetProblemTypeInfoListBySiteType(reproblemInfo.SiteType)
  201. response.OkWithData(gin.H{"reproblemInfo": reproblemInfo}, c)
  202. }
  203. }
  204. func (problemInfoApi *ProblemInfoApi) FindProblemPInfo(c *gin.Context) {
  205. var problemInfo autocode.ProblemInfo
  206. _ = c.ShouldBindQuery(&problemInfo)
  207. if err, reproblemInfo := problemInfoService.GetProblemInfo(problemInfo.ID); err != nil {
  208. global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
  209. response.FailWithMessage("查询失败", c)
  210. } else {
  211. userId := utils.GetUserID(c)
  212. //记录查看记录
  213. if err, readCount := service.ServiceGroupApp.AutoCodeServiceGroup.ReadCountService.GetReadCountByUserId(int(userId), int(problemInfo.ID)); err != nil {
  214. //新增
  215. service.ServiceGroupApp.AutoCodeServiceGroup.ReadCountService.CreateReadCount(autocode.ReadCount{ProblemId: problemInfo.ID, UserId: userId, UserName: utils.GetUserInfo(c).Username})
  216. } else {
  217. service.ServiceGroupApp.AutoCodeServiceGroup.ReadCountService.UpdateReadCountBy(readCount.ID)
  218. }
  219. //累加总阅读量
  220. problemInfoService.UpdateProblemInfoCount(problemInfo.ID)
  221. //param := reqMode.SysDictionarySearch{, request.PageInfo{PageSize: 9999, Page: 0}}
  222. _, reproblemInfo.MatterList, _ = service.ServiceGroupApp.AutoCodeServiceGroup.GetProblemTypeInfoListBySiteType(reproblemInfo.SiteType)
  223. response.OkWithData(gin.H{"reproblemInfo": reproblemInfo}, c)
  224. }
  225. }
  226. // GetProblemInfoList 分页获取ProblemInfo列表
  227. // @Tags ProblemInfo
  228. // @Summary 分页获取ProblemInfo列表
  229. // @Security ApiKeyAuth
  230. // @accept application/json
  231. // @Produce application/json
  232. // @Param data query autocodeReq.ProblemInfoSearch true "分页获取ProblemInfo列表"
  233. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  234. // @Router /problemInfo/getProblemInfoList [get]
  235. func (problemInfoApi *ProblemInfoApi) GetProblemInfoList(c *gin.Context) {
  236. var pageInfo autocodeReq.ProblemInfoSearch
  237. _ = c.ShouldBindQuery(&pageInfo)
  238. if err, list, total := problemInfoService.GetProblemInfoInfoList(pageInfo); err != nil {
  239. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  240. response.FailWithMessage("获取失败", c)
  241. } else {
  242. response.OkWithDetailed(response.PageResult{
  243. List: list,
  244. Total: total,
  245. Page: pageInfo.Page,
  246. PageSize: pageInfo.PageSize,
  247. }, "获取成功", c)
  248. }
  249. }