recall.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package controller
  2. import (
  3. v1 "demo/api/v1"
  4. "demo/internal/app"
  5. "demo/internal/model"
  6. "fmt"
  7. "github.com/gogf/gf/v2/net/ghttp"
  8. "github.com/pkg/errors"
  9. "github.com/siddontang/go/log"
  10. "github.com/xuri/excelize/v2"
  11. "strconv"
  12. "time"
  13. )
  14. // AddRecall
  15. // @Summary 脖环召回计划添加
  16. // @Description add by json account
  17. // @Tags 脖环召回计划
  18. // @Accept json
  19. // @Produce json
  20. // @Param account body v1.AddRecallReq true "Add account"
  21. // @Router /recall/add [post]
  22. func AddRecall(r *ghttp.Request) {
  23. var req *v1.AddRecallReq
  24. username := r.Context().Value("jwt_username")
  25. appG := app.Ghttp{C: r}
  26. err := appG.C.Parse(&req)
  27. if err != nil {
  28. err = errors.Wrap(err, "AddRecall-error")
  29. log.Error(err)
  30. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  31. return
  32. }
  33. file, _, err := r.FormFile("file")
  34. if err != nil {
  35. err = errors.Wrap(err, "AddRecall-error")
  36. log.Error(err)
  37. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  38. return
  39. }
  40. var REGISTNUM2 []string
  41. xlsx1, err := excelize.OpenReader(file)
  42. rows, _ := xlsx1.GetRows("sheet")
  43. for i, r := range rows {
  44. if i > 0 {
  45. REGISTNUM2 = append(REGISTNUM2, r[0])
  46. }
  47. }
  48. dataList, err := srv.GetFactoryidByREGISTNUM2(REGISTNUM2)
  49. if err != nil {
  50. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  51. return
  52. }
  53. for _, data := range dataList {
  54. exist := false
  55. for _, factoryID := range req.FactoryID {
  56. if factoryID == data.FactoryId {
  57. exist = true
  58. break
  59. }
  60. }
  61. if !exist {
  62. appG.Response(app.StatusInternalServerError, app.ERROR, "牛号与批次不匹配!!!")
  63. return
  64. }
  65. }
  66. neckringList, err := srv.GetNeckRingListingIDByFactoryId(req.FactoryID)
  67. if err != nil {
  68. err = errors.Wrap(err, "AddRecall-error")
  69. log.Error(err)
  70. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  71. return
  72. }
  73. var hasRecalled []string
  74. for _, REGISTNUM := range REGISTNUM2 {
  75. exist := false
  76. code, _ := strconv.ParseInt(REGISTNUM, 10, 64)
  77. for _, item := range neckringList {
  78. if item.Code == code {
  79. exist = true
  80. break
  81. }
  82. }
  83. if !exist {
  84. hasRecalled = append(hasRecalled, REGISTNUM)
  85. }
  86. }
  87. if len(hasRecalled) > 0 {
  88. dataList := make([]map[string]interface{}, 0)
  89. for _, REGISTNUM := range REGISTNUM2 {
  90. exist := false
  91. for _, code := range hasRecalled {
  92. if REGISTNUM == code {
  93. exist = true
  94. break
  95. }
  96. }
  97. data := make(map[string]interface{})
  98. data["a1"] = REGISTNUM
  99. data["a2"] = ""
  100. if exist {
  101. data["a2"] = fmt.Sprintf("%s已经被召回!!!", REGISTNUM)
  102. }
  103. dataList = append(dataList, data)
  104. }
  105. appG.Response(app.StatusOK, app.RECALL, dataList)
  106. return
  107. }
  108. err = srv.AddRecall(model.Factory{
  109. Pastureid: req.Pastureid,
  110. Count: req.Count,
  111. CreateName: username.(string),
  112. CreateTime: time.Now(),
  113. Genre: 2,
  114. }, req.FactoryID, REGISTNUM2)
  115. if err != nil {
  116. return
  117. }
  118. appG.Response(app.StatusOK, app.SUCCESS, true)
  119. }
  120. // GetRecall
  121. // @Summary 脖环召回查看
  122. // @Description add by json account
  123. // @Tags 脖环召回计划
  124. // @Accept json
  125. // @Produce json
  126. // @Param index path int true "int "
  127. // @Param pagesize path int true "int "
  128. // @Param batch path string true "string 批次"
  129. // @Param startcount path string true "string 数量"
  130. // @Param endcount path string true "string 数量"
  131. // @Param createname path string true "string 登记人"
  132. // @Param startdate path string true "string 登记时间"
  133. // @Param enddate path string true "string 登记时间"
  134. // @Param factoryid path int true "string 查看召回批次"
  135. // @Param replacement path int true "string 置换时查看传 1"
  136. // @Success 200 {object} v1.GetRecallRes
  137. // @Router /recall/list [get]
  138. func GetRecall(r *ghttp.Request) {
  139. var req *v1.GetRecallReq
  140. appG := app.Ghttp{C: r}
  141. err := appG.C.Parse(&req)
  142. if err != nil {
  143. err = errors.Wrap(err, "GetNeckRingListing-error")
  144. log.Error(err)
  145. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  146. return
  147. }
  148. respList, count, err := srv.GetRecall(req.Index, req.PageSize, req.Pastureid, req.Batch, req.StartCount, req.EndCount, req.CreateName, req.StartDate, req.EndDate, req.FactoryID, req.Replacement)
  149. if err != nil {
  150. return
  151. }
  152. resp := v1.GetRecallRes{}
  153. dataList := make([]*v1.Recall, 0)
  154. for _, item := range respList {
  155. var data v1.Recall
  156. data.Id = item.Id
  157. data.PastureId = item.Pastureid
  158. data.PastureName = item.PastureName
  159. data.Count = item.Count
  160. data.Batch = item.Batch
  161. data.Recall = item.Recall
  162. data.CreateName = item.CreateName
  163. if !item.CreateTime.IsZero() {
  164. data.CreateTime = item.CreateTime.Format("2006-01-02")
  165. }
  166. dataList = append(dataList, &data)
  167. }
  168. resp.List = dataList
  169. resp.Total = count
  170. appG.Response(app.StatusOK, app.SUCCESS, resp)
  171. }