pasture.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-pasture/model"
  7. "net/http"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. "gorm.io/gorm"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. )
  12. func (s *StoreEntry) SearchBarnList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBarnResponse, error) {
  13. penList := make([]*model.Pen, 0)
  14. var count int64 = 0
  15. pref := s.DB.Model(new(model.Pen)).Where("is_delete = ?", pasturePb.IsShow_Ok)
  16. if req.Name != "" {
  17. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  18. }
  19. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  20. Find(&penList).Error; err != nil {
  21. return nil, xerr.WithStack(err)
  22. }
  23. configBarnTypeList := make([]*model.ConfigPenType, 0)
  24. if err := s.DB.Model(new(model.ConfigPenType)).Find(&configBarnTypeList).Error; err != nil {
  25. return nil, xerr.WithStack(err)
  26. }
  27. return &pasturePb.SearchBarnResponse{
  28. Code: http.StatusOK,
  29. Message: "ok",
  30. Data: &pasturePb.SearchBarnData{
  31. List: model.PenSlice(penList).ToPB(configBarnTypeList),
  32. Total: int32(count),
  33. PageSize: pagination.PageSize,
  34. Page: pagination.Page,
  35. },
  36. }, nil
  37. }
  38. func (s *StoreEntry) CreateOrUpdateBarn(ctx context.Context, req *pasturePb.SearchBarnList) error {
  39. if req.Id > 0 {
  40. barn := &model.Pen{Id: req.Id}
  41. if err := s.DB.Model(&model.Pen{}).First(barn).Error; err != nil {
  42. if !errors.Is(err, gorm.ErrRecordNotFound) {
  43. return xerr.WithStack(err)
  44. }
  45. }
  46. }
  47. if err := s.DB.Model(&model.Pen{}).Where(map[string]interface{}{
  48. "id": req.Id,
  49. }).Assign(map[string]interface{}{
  50. "name": req.Name,
  51. "remarks": req.Remarks,
  52. "pen_type": req.BarnTypeId,
  53. "lengths": req.Lengths,
  54. "widths": req.Widths,
  55. "doctrinal_capacity": req.DoctrinalCapacity,
  56. "bed_number": req.BedNumber,
  57. "neck_number": req.NeckNumber,
  58. "is_delete": pasturePb.IsShow_Ok,
  59. "is_show": pasturePb.IsShow_Ok,
  60. }).FirstOrCreate(&model.Pen{}).Error; err != nil {
  61. return xerr.WithStack(err)
  62. }
  63. return nil
  64. }
  65. func (s *StoreEntry) SearchBarnTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  66. barnTypeList := make([]*model.ConfigPenType, 0)
  67. var count int64 = 0
  68. pref := s.DB.Model(new(model.ConfigPenType))
  69. if req.Name != "" {
  70. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  71. }
  72. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  73. Find(&barnTypeList).Error; err != nil {
  74. return nil, xerr.WithStack(err)
  75. }
  76. return &pasturePb.SearchBaseConfigResponse{
  77. Code: http.StatusOK,
  78. Message: "ok",
  79. Data: &pasturePb.SearchBaseConfigData{
  80. List: model.ConfigBarnTypeSlice(barnTypeList).ToPB(),
  81. Total: int32(count),
  82. PageSize: pagination.PageSize,
  83. Page: pagination.Page,
  84. },
  85. }, nil
  86. }
  87. func (s *StoreEntry) CreateOrUpdateBarnType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  88. if req.Id > 0 {
  89. barn := &model.ConfigPenType{Id: int64(req.Id)}
  90. if err := s.DB.Model(&model.ConfigPenType{}).First(barn).Error; err != nil {
  91. if !errors.Is(err, gorm.ErrRecordNotFound) {
  92. return xerr.WithStack(err)
  93. }
  94. }
  95. }
  96. if err := s.DB.Model(&model.ConfigPenType{}).Where(map[string]interface{}{
  97. "id": req.Id,
  98. }).Assign(map[string]interface{}{
  99. "name": req.Name,
  100. "remarks": req.Remarks,
  101. "is_show": pasturePb.IsShow_Ok,
  102. }).FirstOrCreate(&model.ConfigPenType{}).Error; err != nil {
  103. return xerr.WithStack(err)
  104. }
  105. return nil
  106. }
  107. func (s *StoreEntry) SearchBreedStatusList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  108. breedStatusList := make([]*model.ConfigBreedStatus, 0)
  109. var count int64 = 0
  110. pref := s.DB.Model(new(model.ConfigBreedStatus))
  111. if req.Name != "" {
  112. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  113. }
  114. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  115. Find(&breedStatusList).Error; err != nil {
  116. return nil, xerr.WithStack(err)
  117. }
  118. return &pasturePb.SearchBaseConfigResponse{
  119. Code: http.StatusOK,
  120. Message: "ok",
  121. Data: &pasturePb.SearchBaseConfigData{
  122. List: model.ConfigBreedStatusSlice(breedStatusList).ToPB(),
  123. Total: int32(count),
  124. PageSize: pagination.PageSize,
  125. Page: pagination.Page,
  126. },
  127. }, nil
  128. }
  129. func (s *StoreEntry) CreateOrUpdateBreedStatus(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  130. if req.Id > 0 {
  131. barn := &model.ConfigBreedStatus{Id: int64(req.Id)}
  132. if err := s.DB.Model(&model.ConfigBreedStatus{}).First(barn).Error; err != nil {
  133. if !errors.Is(err, gorm.ErrRecordNotFound) {
  134. return xerr.WithStack(err)
  135. }
  136. }
  137. }
  138. if err := s.DB.Model(&model.ConfigBreedStatus{}).Where(map[string]interface{}{
  139. "id": req.Id,
  140. }).Assign(map[string]interface{}{
  141. "name": req.Name,
  142. "remarks": req.Remarks,
  143. "is_show": pasturePb.IsShow_Ok,
  144. }).FirstOrCreate(&model.ConfigBreedStatus{}).Error; err != nil {
  145. return xerr.WithStack(err)
  146. }
  147. return nil
  148. }
  149. func (s *StoreEntry) SearchCowKindList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  150. configCowKindList := make([]*model.ConfigCowKind, 0)
  151. var count int64 = 0
  152. pref := s.DB.Model(new(model.ConfigCowKind))
  153. if req.Name != "" {
  154. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  155. }
  156. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  157. Find(&configCowKindList).Error; err != nil {
  158. return nil, xerr.WithStack(err)
  159. }
  160. return &pasturePb.SearchBaseConfigResponse{
  161. Code: http.StatusOK,
  162. Message: "ok",
  163. Data: &pasturePb.SearchBaseConfigData{
  164. List: model.ConfigCowKindSlice(configCowKindList).ToPB(),
  165. Total: int32(count),
  166. PageSize: pagination.PageSize,
  167. Page: pagination.Page,
  168. },
  169. }, nil
  170. }
  171. func (s *StoreEntry) CreateOrUpdateCowKind(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  172. if req.Id > 0 {
  173. barn := &model.ConfigCowKind{Id: int64(req.Id)}
  174. if err := s.DB.Model(&model.ConfigCowKind{}).First(barn).Error; err != nil {
  175. if !errors.Is(err, gorm.ErrRecordNotFound) {
  176. return xerr.WithStack(err)
  177. }
  178. }
  179. }
  180. if err := s.DB.Model(&model.ConfigCowKind{}).Where(map[string]interface{}{
  181. "id": req.Id,
  182. }).Assign(map[string]interface{}{
  183. "name": req.Name,
  184. "remarks": req.Remarks,
  185. "is_show": pasturePb.IsShow_Ok,
  186. }).FirstOrCreate(&model.ConfigCowKind{}).Error; err != nil {
  187. return xerr.WithStack(err)
  188. }
  189. return nil
  190. }
  191. func (s *StoreEntry) SearchCowStatusList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  192. configCowStatusList := make([]*model.ConfigCowStatus, 0)
  193. var count int64 = 0
  194. pref := s.DB.Model(new(model.ConfigCowStatus))
  195. if req.Name != "" {
  196. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  197. }
  198. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  199. Find(&configCowStatusList).Error; err != nil {
  200. return nil, xerr.WithStack(err)
  201. }
  202. return &pasturePb.SearchBaseConfigResponse{
  203. Code: http.StatusOK,
  204. Message: "ok",
  205. Data: &pasturePb.SearchBaseConfigData{
  206. List: model.ConfigCowStatusSlice(configCowStatusList).ToPB(),
  207. Total: int32(count),
  208. PageSize: pagination.PageSize,
  209. Page: pagination.Page,
  210. },
  211. }, nil
  212. }
  213. func (s *StoreEntry) CreateOrUpdateCowStatus(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  214. if req.Id > 0 {
  215. barn := &model.ConfigCowStatus{Id: int64(req.Id)}
  216. if err := s.DB.Model(&model.ConfigCowStatus{}).First(barn).Error; err != nil {
  217. if !errors.Is(err, gorm.ErrRecordNotFound) {
  218. return xerr.WithStack(err)
  219. }
  220. }
  221. }
  222. if err := s.DB.Model(&model.ConfigCowStatus{}).Where(map[string]interface{}{
  223. "id": req.Id,
  224. }).Assign(map[string]interface{}{
  225. "name": req.Name,
  226. "remarks": req.Remarks,
  227. "is_show": pasturePb.IsShow_Ok,
  228. }).FirstOrCreate(&model.ConfigCowStatus{}).Error; err != nil {
  229. return xerr.WithStack(err)
  230. }
  231. return nil
  232. }
  233. func (s *StoreEntry) SearchCowTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  234. configCowTypeList := make([]*model.ConfigCowType, 0)
  235. var count int64 = 0
  236. pref := s.DB.Model(new(model.ConfigCowType))
  237. if req.Name != "" {
  238. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  239. }
  240. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  241. Find(&configCowTypeList).Error; err != nil {
  242. return nil, xerr.WithStack(err)
  243. }
  244. return &pasturePb.SearchBaseConfigResponse{
  245. Code: http.StatusOK,
  246. Message: "ok",
  247. Data: &pasturePb.SearchBaseConfigData{
  248. List: model.ConfigCowTypeSlice(configCowTypeList).ToPB(),
  249. Total: int32(count),
  250. PageSize: pagination.PageSize,
  251. Page: pagination.Page,
  252. },
  253. }, nil
  254. }
  255. func (s *StoreEntry) CreateOrUpdateCowType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  256. if req.Id > 0 {
  257. barn := &model.ConfigCowType{Id: int64(req.Id)}
  258. if err := s.DB.Model(&model.ConfigCowType{}).First(barn).Error; err != nil {
  259. if !errors.Is(err, gorm.ErrRecordNotFound) {
  260. return xerr.WithStack(err)
  261. }
  262. }
  263. }
  264. if err := s.DB.Model(&model.ConfigCowType{}).Where(map[string]interface{}{
  265. "id": req.Id,
  266. }).Assign(map[string]interface{}{
  267. "name": req.Name,
  268. "remarks": req.Remarks,
  269. "is_show": pasturePb.IsShow_Ok,
  270. }).FirstOrCreate(&model.ConfigCowType{}).Error; err != nil {
  271. return xerr.WithStack(err)
  272. }
  273. return nil
  274. }
  275. func (s *StoreEntry) SearchTransferPenReasonList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  276. configTransferPenReasonList := make([]*model.ConfigTransferPenReason, 0)
  277. var count int64 = 0
  278. pref := s.DB.Model(new(model.ConfigTransferPenReason))
  279. if req.Name != "" {
  280. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  281. }
  282. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  283. Find(&configTransferPenReasonList).Error; err != nil {
  284. return nil, xerr.WithStack(err)
  285. }
  286. return &pasturePb.SearchBaseConfigResponse{
  287. Code: http.StatusOK,
  288. Message: "ok",
  289. Data: &pasturePb.SearchBaseConfigData{
  290. List: model.ConfigTransferPenReasonSlice(configTransferPenReasonList).ToPB(),
  291. Total: int32(count),
  292. PageSize: pagination.PageSize,
  293. Page: pagination.Page,
  294. },
  295. }, nil
  296. }
  297. func (s *StoreEntry) CreateOrUpdateTransferPenReason(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  298. if req.Id > 0 {
  299. barn := &model.ConfigTransferPenReason{Id: int64(req.Id)}
  300. if err := s.DB.Model(&model.ConfigTransferPenReason{}).First(barn).Error; err != nil {
  301. if !errors.Is(err, gorm.ErrRecordNotFound) {
  302. return xerr.WithStack(err)
  303. }
  304. }
  305. }
  306. if err := s.DB.Model(&model.ConfigTransferPenReason{}).Where(map[string]interface{}{
  307. "id": req.Id,
  308. }).Assign(map[string]interface{}{
  309. "name": req.Name,
  310. "remarks": req.Remarks,
  311. "is_show": pasturePb.IsShow_Ok,
  312. }).FirstOrCreate(&model.ConfigTransferPenReason{}).Error; err != nil {
  313. return xerr.WithStack(err)
  314. }
  315. return nil
  316. }
  317. func (s *StoreEntry) SearchCowSourceList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  318. configCowSourceList := make([]*model.ConfigCowSource, 0)
  319. var count int64 = 0
  320. pref := s.DB.Model(new(model.ConfigCowSource))
  321. if req.Name != "" {
  322. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  323. }
  324. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  325. Find(&configCowSourceList).Error; err != nil {
  326. return nil, xerr.WithStack(err)
  327. }
  328. return &pasturePb.SearchBaseConfigResponse{
  329. Code: http.StatusOK,
  330. Message: "ok",
  331. Data: &pasturePb.SearchBaseConfigData{
  332. List: model.ConfigCowSourceSlice(configCowSourceList).ToPB(),
  333. Total: int32(count),
  334. PageSize: pagination.PageSize,
  335. Page: pagination.Page,
  336. },
  337. }, nil
  338. }
  339. func (s *StoreEntry) CreateOrUpdateCowSource(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  340. if req.Id > 0 {
  341. barn := &model.ConfigCowSource{Id: int64(req.Id)}
  342. if err := s.DB.Model(&model.ConfigCowSource{}).First(barn).Error; err != nil {
  343. if !errors.Is(err, gorm.ErrRecordNotFound) {
  344. return xerr.WithStack(err)
  345. }
  346. }
  347. }
  348. if err := s.DB.Model(&model.ConfigCowSource{}).Where(map[string]interface{}{
  349. "id": req.Id,
  350. }).Assign(map[string]interface{}{
  351. "name": req.Name,
  352. "remarks": req.Remarks,
  353. "is_show": pasturePb.IsShow_Ok,
  354. }).FirstOrCreate(&model.ConfigCowSource{}).Error; err != nil {
  355. return xerr.WithStack(err)
  356. }
  357. return nil
  358. }