feed_formula.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package group
  2. import (
  3. "net/http"
  4. "tmr-watch/middleware"
  5. "tmr-watch/models"
  6. "tmr-watch/pkg/app"
  7. "tmr-watch/pkg/e"
  8. "tmr-watch/service/group"
  9. "gitee.com/xuyiping_admin/pkg/valid"
  10. "gitee.com/xuyiping_admin/pkg/apierr"
  11. "github.com/gin-gonic/gin"
  12. )
  13. const (
  14. FeedCategoryDeleteKey = "feed_delete"
  15. CowCategoryDeleteKey = "cow_delete"
  16. )
  17. func Handle404(c *gin.Context) {
  18. c.String(http.StatusNotFound, "404 NotFound")
  19. }
  20. // DistributeFeedFormula 饲料配方下发
  21. func DistributeFeedFormula(c *gin.Context) {
  22. var req models.PastureBodyRequest
  23. if err := c.BindJSON(&req); err != nil {
  24. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  25. return
  26. }
  27. if err := valid.ValidateStruct(&req,
  28. valid.Field(&req.FeedFormula, valid.Required),
  29. valid.Field(&req.FeedFormulaDetail, valid.Required),
  30. ); err != nil {
  31. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  32. return
  33. }
  34. if err := group.DistributeFeedFormulaService(&req); err != nil {
  35. apierr.ClassifiedAbort(c, err)
  36. return
  37. }
  38. c.JSON(http.StatusOK, map[string]bool{
  39. "success": true,
  40. })
  41. }
  42. // CancelDistributeFeedFormula 饲料取消配方下发
  43. func CancelDistributeFeedFormula(c *gin.Context) {
  44. appG := app.Gin{C: c}
  45. var req models.CancelDistributeFeedFormulaRequest
  46. if err := c.BindJSON(&req); err != nil {
  47. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  48. return
  49. }
  50. if len(req.PastureDataId) <= 0 {
  51. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  52. return
  53. }
  54. if err := group.CancelDistributeFeedFormulaService(&req); err != nil {
  55. appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
  56. return
  57. }
  58. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  59. "success": true,
  60. })
  61. }
  62. // EditRecodeFeedFormula 饲料配方修改记录
  63. func EditRecodeFeedFormula(c *gin.Context) {
  64. appG := app.Gin{C: c}
  65. var req models.CancelDistributeFeedFormulaRequest
  66. if err := c.BindJSON(&req); err != nil {
  67. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  68. return
  69. }
  70. if len(req.PastureDataId) <= 0 {
  71. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  72. return
  73. }
  74. if err := group.CancelDistributeFeedFormulaService(&req); err != nil {
  75. appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
  76. return
  77. }
  78. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  79. "success": true,
  80. })
  81. }
  82. func FeedFormulaIsModify(c *gin.Context) {
  83. appG := app.Gin{C: c}
  84. var req models.PastureFeedFormulaIsModifyRequest
  85. if err := c.BindJSON(&req); err != nil {
  86. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  87. return
  88. }
  89. if req.PastureId <= 0 {
  90. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  91. return
  92. }
  93. if err := group.FeedFormulaIsModifyService(&req); err != nil {
  94. appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
  95. return
  96. }
  97. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  98. "success": true,
  99. })
  100. }
  101. // FeedFormulaAsyncList 获取配方列表
  102. func FeedFormulaAsyncList(c *gin.Context) {
  103. appG := app.Gin{C: c}
  104. var req models.FeedListRequest
  105. if err := c.BindJSON(&req); err != nil {
  106. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  107. return
  108. }
  109. if req.PastureId <= 0 {
  110. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  111. return
  112. }
  113. if req.Page <= 0 {
  114. req.Page = 1
  115. }
  116. if req.PageSize <= 0 {
  117. req.PageSize = 50
  118. }
  119. data, err := group.FeedFormulaList(&req)
  120. if err != nil {
  121. appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
  122. return
  123. }
  124. appG.Response(http.StatusOK, e.SUCCESS, data)
  125. }
  126. // FeedAsyncList 获取饲料
  127. func FeedAsyncList(c *gin.Context) {
  128. appG := app.Gin{C: c}
  129. var req models.FeedListRequest
  130. if err := c.BindJSON(&req); err != nil {
  131. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  132. return
  133. }
  134. if req.PastureId <= 0 {
  135. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  136. return
  137. }
  138. if req.Page <= 0 {
  139. req.Page = 1
  140. }
  141. if req.PageSize <= 0 {
  142. req.PageSize = 50
  143. }
  144. data, err := group.FeedList(&req)
  145. if err != nil {
  146. appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
  147. return
  148. }
  149. appG.Response(http.StatusOK, e.SUCCESS, data)
  150. }
  151. // FeedFormulaDetailList 饲料配方详情
  152. func FeedFormulaDetailList(c *gin.Context) {
  153. appG := app.Gin{C: c}
  154. var req models.FeedListRequest
  155. if err := c.BindJSON(&req); err != nil {
  156. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  157. return
  158. }
  159. if req.PastureId <= 0 {
  160. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  161. return
  162. }
  163. if req.Page <= 0 {
  164. req.Page = 1
  165. }
  166. if req.PageSize <= 0 {
  167. req.PageSize = 50
  168. }
  169. data, err := group.FeedTemplateDetailList(&req)
  170. if err != nil {
  171. appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
  172. return
  173. }
  174. appG.Response(http.StatusOK, e.SUCCESS, data)
  175. }
  176. // AnalysisAccuracy 准确性分析
  177. func AnalysisAccuracy(c *gin.Context) {
  178. appG := app.Gin{C: c}
  179. var req models.AnalysisAccuracyRequest
  180. if err := c.BindJSON(&req); err != nil {
  181. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  182. return
  183. }
  184. if res, err := group.AnalysisAccuracyService(&req); err != nil {
  185. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  186. "error": err,
  187. })
  188. } else {
  189. appG.Response(http.StatusOK, e.SUCCESS, res)
  190. }
  191. }
  192. // ProcessAnalysis 过程分析
  193. func ProcessAnalysis(c *gin.Context) {
  194. appG := app.Gin{C: c}
  195. var req models.AnalysisAccuracyRequest
  196. if err := c.BindJSON(&req); err != nil {
  197. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  198. return
  199. }
  200. if res, err := group.ProcessAnalysisService(&req); err != nil {
  201. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  202. "error": err,
  203. })
  204. } else {
  205. appG.Response(http.StatusOK, e.SUCCESS, res)
  206. }
  207. }
  208. // SprinkleStatistics 撒料统计
  209. func SprinkleStatistics(c *gin.Context) {
  210. appG := app.Gin{C: c}
  211. var req models.SprinkleStatisticsRequest
  212. if err := c.BindJSON(&req); err != nil {
  213. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  214. return
  215. }
  216. if res, err := group.SprinkleStatisticsService(&req); err != nil {
  217. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  218. "error": err,
  219. })
  220. return
  221. } else {
  222. appG.Response(http.StatusOK, e.SUCCESS, res)
  223. }
  224. }
  225. // DistributeAccount 账号下发
  226. func DistributeAccount(c *gin.Context) {
  227. appG := app.Gin{C: c}
  228. var req models.AccountDistributionRequest
  229. if err := c.BindJSON(&req); err != nil {
  230. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  231. return
  232. }
  233. if err := group.AccountDistributionService(&req); err != nil {
  234. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  235. "error": err,
  236. })
  237. } else {
  238. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  239. "success": true,
  240. })
  241. }
  242. }
  243. // ForageCategoryDistribute 饲料分类下发
  244. func ForageCategoryDistribute(c *gin.Context) {
  245. appG := app.Gin{C: c}
  246. var req models.CategoryRequest
  247. if err := c.BindJSON(&req); err != nil {
  248. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  249. return
  250. }
  251. if err := group.ForageCategoryDistributeService(&req); err != nil {
  252. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  253. "error": err,
  254. })
  255. } else {
  256. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  257. "success": true,
  258. })
  259. }
  260. }
  261. func ForageCategoryDelete(c *gin.Context) {
  262. appG := app.Gin{C: c}
  263. var req models.CategoryDeleteRequest
  264. if err := c.BindJSON(&req); err != nil {
  265. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  266. return
  267. }
  268. if err := group.CategoryDeleteService(FeedCategoryDeleteKey, &req); err != nil {
  269. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  270. "error": err,
  271. })
  272. } else {
  273. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  274. "success": true,
  275. })
  276. }
  277. }
  278. func CowCategoryDistribute(c *gin.Context) {
  279. appG := app.Gin{C: c}
  280. var req models.CategoryRequest
  281. if err := c.BindJSON(&req); err != nil {
  282. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  283. return
  284. }
  285. if err := group.CattleCategoryDistributeService(&req); err != nil {
  286. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  287. "error": err,
  288. })
  289. } else {
  290. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  291. "success": true,
  292. })
  293. }
  294. }
  295. func CowCategoryDelete(c *gin.Context) {
  296. appG := app.Gin{C: c}
  297. var req models.CategoryDeleteRequest
  298. if err := c.BindJSON(&req); err != nil {
  299. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  300. return
  301. }
  302. if err := group.CategoryDeleteService(CowCategoryDeleteKey, &req); err != nil {
  303. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  304. "error": err,
  305. })
  306. } else {
  307. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
  308. "success": true,
  309. })
  310. }
  311. }
  312. // FeedUsage 配方使用概况
  313. func FeedUsage(c *gin.Context) {
  314. appG := app.Gin{C: c}
  315. var req models.FeedFormulaUsageRequest
  316. if err := c.BindJSON(&req); err != nil {
  317. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  318. return
  319. }
  320. if res, err := group.FeedUsageService(&req); err != nil {
  321. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  322. "error": err,
  323. })
  324. return
  325. } else {
  326. appG.Response(http.StatusOK, e.SUCCESS, res)
  327. }
  328. }
  329. // FeedFormulaVersion 集团端配方信息更新同步
  330. func FeedFormulaVersion(c *gin.Context) {
  331. appG := app.Gin{C: c}
  332. var req models.FeedFormulaUpdateVersionRequest
  333. if err := c.BindJSON(&req); err != nil {
  334. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  335. return
  336. }
  337. if err := group.FeedFormulaVersionService(int64(c.GetInt(middleware.PastureId)), &req); err != nil {
  338. appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
  339. "error": err,
  340. })
  341. return
  342. }
  343. appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{"success": true})
  344. }