management.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package controller
  2. import (
  3. "strings"
  4. "time"
  5. v1 "demo/api/v1"
  6. "demo/internal/app"
  7. "demo/internal/model"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/pkg/errors"
  10. "github.com/siddontang/go/log"
  11. "github.com/xuri/excelize/v2"
  12. )
  13. // AddManagement
  14. // @Summary 脖环管理导入
  15. // @Description add by json account
  16. // @Tags 脖环管理
  17. // @Accept json
  18. // @Produce json
  19. // @Router /management/add [post]
  20. func AddManagement(r *ghttp.Request) {
  21. appG := app.Ghttp{C: r}
  22. pastureidStr := r.FormValue("pastureid")
  23. file, _, err := r.FormFile("file")
  24. if err != nil {
  25. err = errors.Wrap(err, "AddManagement-error")
  26. log.Error(err)
  27. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  28. return
  29. }
  30. xlsx1, err := excelize.OpenReader(file)
  31. rows, _ := xlsx1.GetRows("sheet")
  32. managementBhList := make([]*model.ManagementBh, 0)
  33. timeLayout := "2006-01-02"
  34. var genreStr string
  35. var genre int64
  36. for i, r := range rows {
  37. var exist bool
  38. if i > 0 {
  39. var arg model.ManagementBh
  40. if genreStr == "" {
  41. genreStr = r[2]
  42. } else if genreStr != r[2] {
  43. continue
  44. }
  45. arg.Code = r[0]
  46. if r[1] == "" {
  47. continue
  48. }
  49. date, _ := time.Parse(timeLayout, r[1])
  50. arg.Date = date
  51. for _, management := range managementBhList {
  52. if management.Code == arg.Code {
  53. exist = true
  54. continue
  55. }
  56. }
  57. if !exist {
  58. managementBhList = append(managementBhList, &arg)
  59. }
  60. }
  61. }
  62. //0 售卖,1 返厂 2 置换
  63. if strings.Index(genreStr, "售卖") >= 0 {
  64. genre = 0
  65. } else if strings.Index(genreStr, "返厂") >= 0 {
  66. genre = 1
  67. } else if strings.Index(genreStr, "置换") >= 0 {
  68. genre = 2
  69. }
  70. //pastureid, _ := strconv.ParseInt(pastureidStr, 10, 64)
  71. err = srv.AddManagement(model.Management{
  72. Pastureid: pastureidStr,
  73. ImportTime: time.Now(),
  74. Genre: genre,
  75. Count: int64(len(managementBhList)),
  76. }, managementBhList)
  77. if err != nil {
  78. err = errors.Wrap(err, "AddManagement-error")
  79. log.Error(err)
  80. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  81. return
  82. }
  83. appG.Response(app.StatusOK, app.SUCCESS, true)
  84. }
  85. // GetManagement
  86. // @Summary 脖环管理查看
  87. // @Description add by json account
  88. // @Tags 脖环管理
  89. // @Accept json
  90. // @Produce json
  91. // @Param index path int true "int "
  92. // @Param pagesize path int true "int "
  93. // @Param code path string true "string 脖环编码"
  94. // @Param status path string true "string 脖环状态"
  95. // @Param pastureid path string true "string 牧场id"
  96. // @Param genre path int64 true "int64 0 售卖,1 返厂 2 置换 -1 全部"
  97. // @Param startdate path string true "string 导入开始时间"
  98. // @Param enddate path string true "string 导入结束时间"
  99. // @Success 200 {object} v1.GetManagementRes
  100. // @Router /management/list [get]
  101. func GetManagement(r *ghttp.Request) {
  102. var req *v1.GetManagementReq
  103. appG := app.Ghttp{C: r}
  104. err := appG.C.Parse(&req)
  105. if err != nil {
  106. err = errors.Wrap(err, "GetNeckRingListing-error")
  107. log.Error(err)
  108. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  109. return
  110. }
  111. managementList, count, err := srv.GetManagement(req.Index, req.PageSize, req.PastureId, req.Genre, req.StartDate, req.EndDate)
  112. if err != nil {
  113. err = errors.Wrap(err, "GetNeckRingListing-error")
  114. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  115. return
  116. }
  117. resp := v1.GetManagementRes{}
  118. dataList := make([]*v1.Management, 0)
  119. for _, item := range managementList {
  120. var data v1.Management
  121. data.Id = item.Id
  122. data.Pastureid = item.Pastureid
  123. data.Count = item.Count
  124. data.Genre = item.Genre
  125. data.Recall = item.Recall
  126. if !item.ImportTime.IsZero() {
  127. data.ImportTime = item.ImportTime.Format("2006-01-02 15:04:05")
  128. }
  129. data.PastureName = item.PastureName
  130. dataList = append(dataList, &data)
  131. }
  132. resp.Total = count
  133. resp.List = dataList
  134. appG.Response(app.StatusOK, app.SUCCESS, resp)
  135. }
  136. // GetManagementBh
  137. // @Summary 脖环管理明细查看
  138. // @Description add by json account
  139. // @Tags 脖环管理
  140. // @Accept json
  141. // @Produce json
  142. // @Param index path int true "int "
  143. // @Param pagesize path int true "int "
  144. // @Param managementid path int true "int "
  145. // @Success 200 {object} v1.GetManagementBhRes
  146. // @Router /managementbh/list [get]
  147. func GetManagementBh(r *ghttp.Request) {
  148. var req *v1.GetManagementBhReq
  149. appG := app.Ghttp{C: r}
  150. err := appG.C.Parse(&req)
  151. if err != nil {
  152. err = errors.Wrap(err, "GetNeckRingListing-error")
  153. log.Error(err)
  154. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  155. return
  156. }
  157. managementList, count, err := srv.GetManagementBh(req.Index, req.PageSize, req.ManagementId)
  158. if err != nil {
  159. err = errors.Wrap(err, "GetNeckRingListing-error")
  160. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  161. return
  162. }
  163. resp := v1.GetManagementBhRes{}
  164. dataList := make([]*v1.ManagementBh, 0)
  165. for _, item := range managementList {
  166. var data v1.ManagementBh
  167. data.ManagementID = item.ManagementID
  168. data.Code = item.Code
  169. if !item.Date.IsZero() {
  170. data.Date = item.Date.Format("2006-01-02 15:04:05")
  171. }
  172. dataList = append(dataList, &data)
  173. }
  174. resp.Total = count
  175. resp.List = dataList
  176. appG.Response(app.StatusOK, app.SUCCESS, resp)
  177. }