feed_formula.go 9.8 KB

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