feed_formula.go 9.8 KB

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