pasture.go 13 KB

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