pasture_sync_service.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "kpt-tmr-group/model"
  6. "math"
  7. "net/http"
  8. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. "gorm.io/gorm"
  11. )
  12. const (
  13. FeedCategory = "feed"
  14. FeedCategoryDelete = "feed_delete"
  15. CowCategory = "cow"
  16. CowCategoryDelete = "cow_delete"
  17. )
  18. func (s *StoreEntry) CategorySyncData(ctx context.Context, req *operationPb.CategorySyncRequest) error {
  19. pastureDetail, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  20. if err != nil {
  21. return xerr.WithStack(err)
  22. }
  23. switch req.KeyWord {
  24. case FeedCategory:
  25. history := &model.ForageCategory{}
  26. if err = s.DB.Model(new(model.ForageCategory)).
  27. Where("pasture_id = ?", req.PastureId).
  28. Where("data_id = ?", req.Id).
  29. Where("data_source = ? ", operationPb.DataSource_FROM_PASTURE).
  30. First(history).Error; err != nil {
  31. if !errors.Is(err, gorm.ErrRecordNotFound) {
  32. return xerr.WithStack(err)
  33. }
  34. }
  35. if history.IsShow == operationPb.IsShow_OK && history.Id > 0 {
  36. if err = s.DB.Model(new(model.ForageCategory)).Where("id = ?", history.Id).Updates(map[string]interface{}{
  37. "number": req.Number,
  38. "is_show": req.IsShow,
  39. "parent_id": req.ParentId,
  40. "parent_name": req.ParentName,
  41. "name": req.Name,
  42. }).Error; err != nil {
  43. return xerr.WithStack(err)
  44. }
  45. }
  46. newFeedData := model.NewPastureForageCategory(req, pastureDetail)
  47. if err = s.DB.Model(new(model.ForageCategory)).Create(newFeedData).Error; err != nil {
  48. return xerr.WithStack(err)
  49. }
  50. case CowCategory:
  51. history := &model.CattleCategory{}
  52. if err = s.DB.Model(new(model.CattleCategory)).
  53. Where("pasture_id = ?", req.PastureId).
  54. Where("data_id = ?", req.Id).
  55. Where("data_source = ? ", operationPb.DataSource_FROM_PASTURE).
  56. First(history).Error; err != nil {
  57. if !errors.Is(err, gorm.ErrRecordNotFound) {
  58. return xerr.WithStack(err)
  59. }
  60. }
  61. if history.IsShow == operationPb.IsShow_OK && history.Id > 0 {
  62. if err = s.DB.Model(new(model.CattleCategory)).Where("id = ?", history.Id).Updates(map[string]interface{}{
  63. "number": req.Number,
  64. "is_show": req.IsShow,
  65. "parent_id": req.ParentId,
  66. "parent_name": req.ParentName,
  67. "name": req.Name,
  68. }).Error; err != nil {
  69. return xerr.WithStack(err)
  70. }
  71. }
  72. newCattleData := model.NewPastureCattleCategory(req, pastureDetail)
  73. if err = s.DB.Model(new(model.CattleCategory)).Create(newCattleData).Error; err != nil {
  74. return xerr.WithStack(err)
  75. }
  76. }
  77. return nil
  78. }
  79. func (s *StoreEntry) CategoryDeleteData(ctx context.Context, req *operationPb.CategoryDeleteRequest) error {
  80. var modelValue interface{}
  81. switch req.KeyWord {
  82. case FeedCategoryDelete:
  83. modelValue = new(model.ForageCategory)
  84. case CowCategoryDelete:
  85. modelValue = new(model.CattleCategory)
  86. }
  87. if modelValue == nil {
  88. return nil
  89. }
  90. if err := s.DB.Model(modelValue).Where("data_id = ?", req.DataId).
  91. Where("pasture_id = ?", req.PastureId).Where("data_source = ?", operationPb.DataSource_FROM_PASTURE).
  92. Updates(map[string]interface{}{
  93. "is_delete": operationPb.IsShow_OK,
  94. }).Error; err != nil {
  95. return xerr.WithStack(err)
  96. }
  97. return nil
  98. }
  99. func (s *StoreEntry) FeedFormulaSyncData(ctx context.Context, req *operationPb.FeedFormulaSyncRequest) error {
  100. groupPastureList, err := s.DataSyncGroupPastureList(ctx)
  101. if err != nil {
  102. return xerr.WithStack(err)
  103. }
  104. var (
  105. ok bool
  106. groupPasture *model.GroupPasture
  107. )
  108. for _, pastureDetail := range groupPastureList {
  109. if pastureDetail.Id != int64(req.PastureId) {
  110. continue
  111. }
  112. groupPasture = pastureDetail
  113. ok = true
  114. }
  115. if ok {
  116. total := 0
  117. page := 1
  118. pageSize := 100
  119. for i := 0; i <= total; i++ {
  120. body := &model.FeedFormulaListRequest{
  121. PastureId: int32(groupPasture.PastureId),
  122. Page: int32(page),
  123. PageSize: int32(pageSize),
  124. }
  125. response := &model.FeedFormulaListResponse{}
  126. if err = s.PastureHttpClient(ctx, model.FeedFormulaAsyncUrl, int64(req.PastureId), body, response); err != nil {
  127. return xerr.WithStack(err)
  128. }
  129. if response.Code != http.StatusOK {
  130. return xerr.Customf("%s", response.Msg)
  131. }
  132. if response.Data.Total > 0 && response.Data.List != nil {
  133. total = int(math.Ceil(float64(response.Data.Total) / float64(pageSize)))
  134. }
  135. if err = s.FeedFormulaInsert(ctx, groupPasture, response.Data.List); err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. if total <= (pageSize * page) {
  139. break
  140. }
  141. page++
  142. }
  143. }
  144. return nil
  145. }
  146. func (s *StoreEntry) FeedFormulaInsert(ctx context.Context, groupPasture *model.GroupPasture, list []*model.FeedTemplate) error {
  147. res := make([]*model.FeedFormula, 0)
  148. for _, data := range list {
  149. res = append(res, &model.FeedFormula{
  150. Name: data.TName,
  151. Colour: data.TColor,
  152. EncodeNumber: data.TCode,
  153. CattleCategoryId: operationPb.CattleCategoryParent_Kind(data.CCid),
  154. CattleCategoryName: data.CCName,
  155. FormulaTypeId: operationPb.FormulaType_Kind(data.FTTypeId),
  156. FormulaTypeName: data.FTType,
  157. DataSourceId: operationPb.DataSource_FROM_PASTURE,
  158. DataSourceName: "牧场同步",
  159. Remarks: data.Remark,
  160. Version: data.Version,
  161. PastureId: groupPasture.PastureId,
  162. PastureName: groupPasture.Name,
  163. PastureDataId: data.Id,
  164. IsShow: operationPb.IsShow_Kind(data.Enable),
  165. IsModify: operationPb.IsShow_Kind(data.IsModify),
  166. IsDelete: operationPb.IsShow_OK,
  167. })
  168. }
  169. if err := s.DB.Model(new(model.FeedFormula)).Create(res).Error; err != nil {
  170. return xerr.WithStack(err)
  171. }
  172. return nil
  173. }
  174. func (s *StoreEntry) FeedSyncData(ctx context.Context, req *operationPb.FeedFormulaSyncRequest) error {
  175. groupPastureList, err := s.DataSyncGroupPastureList(ctx)
  176. if err != nil {
  177. return xerr.WithStack(err)
  178. }
  179. var (
  180. ok bool
  181. groupPasture *model.GroupPasture
  182. )
  183. for _, pastureDetail := range groupPastureList {
  184. if pastureDetail.Id != int64(req.PastureId) {
  185. continue
  186. }
  187. groupPasture = pastureDetail
  188. ok = true
  189. }
  190. if ok {
  191. if req.Page <= 0 {
  192. req.Page = 1
  193. }
  194. if req.PageSize <= 0 {
  195. req.PageSize = 1000
  196. }
  197. body := &model.FeedFormulaListRequest{
  198. PastureId: int32(groupPasture.PastureId),
  199. Page: req.Page,
  200. PageSize: req.PageSize,
  201. }
  202. response := &model.FeedListResponse{}
  203. if err = s.PastureHttpClient(ctx, model.FeedAsyncUrl, int64(req.PastureId), body, response); err != nil {
  204. return xerr.WithStack(err)
  205. }
  206. if response.Code != http.StatusOK {
  207. return xerr.Customf("%s", response.Msg)
  208. }
  209. if err = s.FeedInsert(ctx, groupPasture, response.Data.List); err != nil {
  210. return xerr.WithStack(err)
  211. }
  212. }
  213. return nil
  214. }
  215. // FeedInsert 饲料数据同步
  216. func (s *StoreEntry) FeedInsert(ctx context.Context, groupPasture *model.GroupPasture, list []*model.Feed) error {
  217. res := make([]*model.Forage, 0)
  218. for _, data := range list {
  219. if data.Id <= 0 {
  220. continue
  221. }
  222. res = append(res, model.NewForageByPastureFeed(groupPasture, data))
  223. }
  224. if err := s.DB.Model(new(model.Forage)).Create(res).Error; err != nil {
  225. return xerr.WithStack(err)
  226. }
  227. return nil
  228. }
  229. // FeedFormulaDetailListSyncData 饲料配方详情同步
  230. func (s *StoreEntry) FeedFormulaDetailListSyncData(ctx context.Context, req *operationPb.FeedFormulaSyncRequest) error {
  231. groupPastureList, err := s.DataSyncGroupPastureList(ctx)
  232. if err != nil {
  233. return xerr.WithStack(err)
  234. }
  235. var (
  236. ok bool
  237. groupPasture *model.GroupPasture
  238. )
  239. for _, pastureDetail := range groupPastureList {
  240. if pastureDetail.Id != int64(req.PastureId) {
  241. continue
  242. }
  243. groupPasture = pastureDetail
  244. ok = true
  245. }
  246. if ok {
  247. total := 0
  248. page := 1
  249. pageSize := 100
  250. for i := 0; i <= total; i++ {
  251. body := &model.FeedFormulaListRequest{
  252. PastureId: int32(groupPasture.PastureId),
  253. Page: int32(page),
  254. PageSize: int32(pageSize),
  255. }
  256. response := &model.FeedFormulaDetailListResponse{}
  257. if err = s.PastureHttpClient(ctx, model.FeedFormulaDetailListAsyncUrl, int64(req.PastureId), body, response); err != nil {
  258. return xerr.WithStack(err)
  259. }
  260. if response.Code != http.StatusOK {
  261. return xerr.Customf("%s", response.Msg)
  262. }
  263. if response.Data.Total > 0 && response.Data.List != nil {
  264. total = int(math.Ceil(float64(response.Data.Total) / float64(pageSize)))
  265. }
  266. if total <= (page * pageSize) {
  267. break
  268. }
  269. page++
  270. if err = s.FeedFormulaDetailInsert(ctx, groupPasture, response.Data.List); err != nil {
  271. return xerr.WithStack(err)
  272. }
  273. }
  274. }
  275. return nil
  276. }
  277. func (s *StoreEntry) FeedFormulaDetailInsert(ctx context.Context, groupPasture *model.GroupPasture, list []*model.FeedTemplateDetail) error {
  278. res := make([]*model.FeedFormulaDetail, 0)
  279. for _, data := range list {
  280. if data.Id <= 0 {
  281. continue
  282. }
  283. isLockCowCountRatio := 0
  284. if data.IsLockCount == 0 {
  285. isLockCowCountRatio = 2
  286. }
  287. if data.IsLockCount == 1 {
  288. isLockCowCountRatio = 1
  289. }
  290. res = append(res, &model.FeedFormulaDetail{
  291. PastureId: groupPasture.PastureId,
  292. PastureName: groupPasture.Name,
  293. PastureDataId: data.Id,
  294. ForageId: data.FId,
  295. FeedFormulaId: data.FtId,
  296. Weight: int32(data.FWeight * 100),
  297. ForageGroupName: data.FeedGroup,
  298. ForageName: data.FName,
  299. StirDelay: data.AutoSecond,
  300. AllowError: data.Deviation,
  301. Prefit: data.PreFtId,
  302. IsShow: operationPb.IsShow_OK,
  303. IsLockCowCountRatio: operationPb.IsShow_Kind(isLockCowCountRatio),
  304. Sort: data.Sort,
  305. })
  306. }
  307. if err := s.DB.Model(new(model.FeedFormulaDetail)).Create(res).Error; err != nil {
  308. return xerr.WithStack(err)
  309. }
  310. return nil
  311. }