pasture_service.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. package backend
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "kpt-tmr-group/model"
  9. "kpt-tmr-group/pkg/logger/zaplog"
  10. "kpt-tmr-group/pkg/tool"
  11. "kpt-tmr-group/pkg/xerr"
  12. operationPb "kpt-tmr-group/proto/go/backend/operation"
  13. "net/http"
  14. "strconv"
  15. "sync"
  16. "go.uber.org/zap"
  17. "github.com/xuri/excelize/v2"
  18. "gorm.io/gorm"
  19. )
  20. // CreateGroupPasture 创建集团牧场
  21. func (s *StoreEntry) CreateGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  22. // 账号同步牧场端
  23. groupPasture := model.NewGroupPasture(req)
  24. if err := s.DB.Create(groupPasture).Error; err != nil {
  25. return xerr.WithStack(err)
  26. }
  27. if err := s.PastureAccountDistribution(ctx, groupPasture); err != nil {
  28. s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_delete", operationPb.IsShow_NO)
  29. return xerr.WithStack(err)
  30. } else {
  31. s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_distribution", operationPb.IsShow_OK)
  32. }
  33. return nil
  34. }
  35. // PastureAccountDistribution 账号同步牧场端
  36. func (s *StoreEntry) PastureAccountDistribution(ctx context.Context, groupPasture *model.GroupPasture) error {
  37. if groupPasture.Domain == "" {
  38. return nil
  39. }
  40. body := &model.AccountDistribution{
  41. Account: groupPasture.Account,
  42. UserName: groupPasture.ManagerUser,
  43. Password: groupPasture.Password,
  44. Phone: groupPasture.ManagerPhone,
  45. PastureId: int32(groupPasture.Id),
  46. }
  47. res := &model.PastureCommonResponse{}
  48. if err := s.PastureHttpClient(ctx, model.PastureAccountDistributionURl, groupPasture.Id, body, res); err != nil {
  49. zaplog.Error("AccountDistribution", zap.Any("url", model.PastureAccountDistributionURl), zap.Any("err", err), zap.Any("body", body), zap.Any("res", res))
  50. return xerr.WithStack(err)
  51. }
  52. if res.Code != http.StatusOK {
  53. if err := s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_distribution", operationPb.IsShow_OK).Error; err != nil {
  54. zaplog.Error("AccountDistribution-Update", zap.Any("url", model.PastureAccountDistributionURl), zap.Any("err", err), zap.Any("body", body), zap.Any("res", res))
  55. return xerr.Customf("%s", res.Msg)
  56. }
  57. }
  58. return nil
  59. }
  60. func (s *StoreEntry) FindGroupPastureListByIds(ctx context.Context, pastureIds []int32) ([]*model.GroupPasture, error) {
  61. groupPastureList := make([]*model.GroupPasture, 0)
  62. if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
  63. Where("id IN ?", pastureIds).
  64. Find(&groupPastureList).Error; err != nil {
  65. return nil, xerr.WithStack(err)
  66. }
  67. return groupPastureList, nil
  68. }
  69. func (s *StoreEntry) GetGroupPastureListById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
  70. groupPasture := &model.GroupPasture{
  71. Id: int64(pastureId),
  72. }
  73. if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
  74. First(&groupPasture).Error; err != nil {
  75. return nil, xerr.WithStack(err)
  76. }
  77. return groupPasture, nil
  78. }
  79. // EditGroupPasture 创建集团牧场
  80. func (s *StoreEntry) EditGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  81. groupPasture := &model.GroupPasture{Id: int64(req.Id)}
  82. if err := s.DB.First(groupPasture).Error; err != nil {
  83. if errors.Is(err, gorm.ErrRecordNotFound) {
  84. return xerr.Custom("该数据不存在!")
  85. }
  86. return xerr.WithStack(err)
  87. }
  88. updateData := &model.GroupPasture{
  89. Name: req.Name,
  90. Account: req.Account,
  91. ManagerUser: req.ManagerUser,
  92. ManagerPhone: req.ManagerPhone,
  93. Address: req.Address,
  94. Domain: req.Domain,
  95. }
  96. if err := s.DB.Model(new(model.GroupPasture)).Omit("is_show", "password").
  97. Where("id = ?", req.Id).
  98. Updates(updateData).Error; err != nil {
  99. return xerr.WithStack(err)
  100. }
  101. return nil
  102. }
  103. // SearchGroupPastureList 查询牧场列表
  104. func (s *StoreEntry) SearchGroupPastureList(ctx context.Context, req *operationPb.SearchPastureRequest) (*operationPb.SearchPastureResponse, error) {
  105. groupPasture := make([]*model.GroupPasture, 0)
  106. var count int64 = 0
  107. pref := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK)
  108. if req.Name != "" {
  109. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  110. }
  111. if req.ManagerPhone != "" {
  112. pref.Where("manager_phone like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerPhone, "%"))
  113. }
  114. if req.ManagerUser != "" {
  115. pref.Where("manager_user like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerUser, "%"))
  116. }
  117. if req.StartTime > 0 && req.EndTime > 0 && req.EndTime >= req.StartTime {
  118. pref.Where("created_at BETWEEN ? AND ? ", req.StartTime, req.EndTime)
  119. }
  120. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  121. Find(&groupPasture).Error; err != nil {
  122. return nil, xerr.WithStack(err)
  123. }
  124. return &operationPb.SearchPastureResponse{
  125. Code: http.StatusOK,
  126. Msg: "ok",
  127. Data: &operationPb.SearchPastureData{
  128. Page: req.Pagination.Page,
  129. Total: int32(count),
  130. PageSize: req.Pagination.PageSize,
  131. List: model.GroupPastureSlice(groupPasture).ToPB(),
  132. },
  133. }, nil
  134. }
  135. func (s *StoreEntry) DeleteGroupPasture(ctx context.Context, pastureId int64) error {
  136. groupPasture := &model.GroupPasture{
  137. Id: pastureId,
  138. }
  139. if err := s.DB.First(groupPasture).Error; err != nil {
  140. if errors.Is(err, gorm.ErrRecordNotFound) {
  141. return xerr.Custom("该数据不存在")
  142. }
  143. return xerr.WithStack(err)
  144. }
  145. if err := s.DB.Model(groupPasture).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  146. return xerr.WithStack(err)
  147. }
  148. return nil
  149. }
  150. func (s *StoreEntry) ResetPasswordGroupPasture(ctx context.Context, pastureId int64) error {
  151. groupPasture := &model.GroupPasture{
  152. Id: pastureId,
  153. }
  154. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(groupPasture).Error; err != nil {
  155. if errors.Is(err, gorm.ErrRecordNotFound) {
  156. return xerr.Custom("该数据不存在")
  157. }
  158. return xerr.WithStack(err)
  159. }
  160. if err := s.DB.Model(groupPasture).Update("password", tool.Md5String(model.InitManagerPassword)).Error; err != nil {
  161. return xerr.WithStack(err)
  162. }
  163. return nil
  164. }
  165. func (s *StoreEntry) IsShowGroupPasture(ctx context.Context, req *operationPb.IsShowGroupPasture) error {
  166. groupPasture := &model.GroupPasture{
  167. Id: int64(req.PastureId),
  168. }
  169. if err := s.DB.First(groupPasture).Error; err != nil {
  170. if errors.Is(err, gorm.ErrRecordNotFound) {
  171. return xerr.Custom("该数据不存在")
  172. }
  173. return xerr.WithStack(err)
  174. }
  175. if err := s.DB.Model(groupPasture).Where("is_delete = ?", operationPb.IsShow_OK).Update("is_show", req.IsShow).Error; err != nil {
  176. return xerr.WithStack(err)
  177. }
  178. return nil
  179. }
  180. // AddCattleCategory 添加畜牧分类
  181. func (s *StoreEntry) AddCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  182. cattleCategory := model.NewCattleCategory(req)
  183. if err := s.DB.Create(cattleCategory).Error; err != nil {
  184. return xerr.WithStack(err)
  185. }
  186. defer func() {
  187. body := &model.CategoryRequest{
  188. ParentId: int32(req.ParentId),
  189. ParentName: req.ParentName,
  190. Name: req.Name,
  191. Id: int32(req.Id),
  192. Number: req.Number,
  193. IsShow: int32(req.IsShow),
  194. GroupId: int32(cattleCategory.Id),
  195. }
  196. s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body)
  197. }()
  198. return nil
  199. }
  200. // EditCattleCategory 编辑畜牧分类
  201. func (s *StoreEntry) EditCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  202. cattleCategory := &model.CattleCategory{Id: int64(req.Id)}
  203. if err := s.DB.First(cattleCategory).Error; err != nil {
  204. if errors.Is(err, gorm.ErrRecordNotFound) {
  205. return xerr.Custom("该数据不存在")
  206. }
  207. return xerr.WithStack(err)
  208. }
  209. updateData := &model.CattleCategory{
  210. ParentName: req.ParentName,
  211. Name: req.Name,
  212. Number: req.Number,
  213. ParentId: req.ParentId,
  214. }
  215. if err := s.DB.Model(new(model.CattleCategory)).Omit("is_show", "is_delete").
  216. Where("id = ?", req.Id).
  217. Updates(updateData).Error; err != nil {
  218. return xerr.WithStack(err)
  219. }
  220. defer func() {
  221. body := &model.CategoryRequest{
  222. ParentId: int32(req.ParentId),
  223. ParentName: req.ParentName,
  224. Name: req.Name,
  225. Id: int32(req.Id),
  226. Number: req.Number,
  227. IsShow: int32(req.IsShow),
  228. GroupId: int32(req.Id),
  229. }
  230. s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body)
  231. }()
  232. return nil
  233. }
  234. // IsShowCattleCategory 是否启用
  235. func (s *StoreEntry) IsShowCattleCategory(ctx context.Context, req *operationPb.IsShowCattleCategory) error {
  236. cattleCategory := &model.CattleCategory{Id: int64(req.CattleCategoryId)}
  237. if err := s.DB.First(cattleCategory).Error; err != nil {
  238. if errors.Is(err, gorm.ErrRecordNotFound) {
  239. return xerr.Custom("该数据不存在")
  240. }
  241. return xerr.WithStack(err)
  242. }
  243. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", req.CattleCategoryId).Update("is_show", req.IsShow).Error; err != nil {
  244. return xerr.WithStack(err)
  245. }
  246. defer func() {
  247. body := &model.CategoryRequest{
  248. ParentId: int32(cattleCategory.ParentId),
  249. ParentName: cattleCategory.ParentName,
  250. Name: cattleCategory.Name,
  251. Id: int32(req.CattleCategoryId),
  252. Number: cattleCategory.Number,
  253. IsShow: int32(req.IsShow),
  254. GroupId: int32(cattleCategory.Id),
  255. }
  256. s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body)
  257. }()
  258. return nil
  259. }
  260. // DeleteCattleCategory 是否删除
  261. func (s *StoreEntry) DeleteCattleCategory(ctx context.Context, cattleCategoryId int64) error {
  262. cattleCategory := &model.CattleCategory{Id: cattleCategoryId}
  263. if err := s.DB.First(cattleCategory).Error; err != nil {
  264. if errors.Is(err, gorm.ErrRecordNotFound) {
  265. return xerr.Custom("该数据不存在")
  266. }
  267. return xerr.WithStack(err)
  268. }
  269. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", cattleCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  270. return xerr.WithStack(err)
  271. }
  272. return nil
  273. }
  274. // SearchCattleCategoryList 牧畜分类类别列表
  275. func (s *StoreEntry) SearchCattleCategoryList(ctx context.Context, req *operationPb.SearchCattleCategoryRequest) (*operationPb.SearchCattleCategoryResponse, error) {
  276. cattleCategory := make([]*model.CattleCategory, 0)
  277. var count int64 = 0
  278. pref := s.DB.Model(new(model.CattleCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
  279. if req.Name != "" {
  280. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  281. }
  282. if req.ParentId > 0 {
  283. pref.Where("parent_id = ?", req.ParentId)
  284. }
  285. if req.IsShow > 0 {
  286. pref.Where("is_show = ?", req.IsShow)
  287. }
  288. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  289. Find(&cattleCategory).Debug().Error; err != nil {
  290. return nil, xerr.WithStack(err)
  291. }
  292. return &operationPb.SearchCattleCategoryResponse{
  293. Code: http.StatusOK,
  294. Msg: "ok",
  295. Data: &operationPb.SearchCattleCategoryData{
  296. Page: req.Pagination.Page,
  297. PageSize: req.Pagination.PageSize,
  298. Total: int32(count),
  299. List: model.CattleCategorySlice(cattleCategory).ToPB(),
  300. },
  301. }, nil
  302. }
  303. // AddForageCategory 添加饲料分类
  304. func (s *StoreEntry) AddForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
  305. forageCategory := model.NewForageCategory(req)
  306. if err := s.DB.Create(forageCategory).Error; err != nil {
  307. return xerr.WithStack(err)
  308. }
  309. defer func() {
  310. body := &model.CategoryRequest{
  311. ParentId: int32(req.ParentId),
  312. ParentName: req.ParentName,
  313. Name: req.Name,
  314. Id: int32(req.Id),
  315. Number: req.Number,
  316. IsShow: int32(req.IsShow),
  317. GroupId: int32(forageCategory.Id),
  318. }
  319. s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body)
  320. }()
  321. return nil
  322. }
  323. // EditForageCategory 编辑饲料分类
  324. func (s *StoreEntry) EditForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
  325. forageCategory := &model.ForageCategory{Id: int64(req.Id)}
  326. if err := s.DB.First(forageCategory).Error; err != nil {
  327. if errors.Is(err, gorm.ErrRecordNotFound) {
  328. return xerr.Custom("该数据不存在")
  329. }
  330. return xerr.WithStack(err)
  331. }
  332. updateData := &model.ForageCategory{
  333. ParentName: req.ParentName,
  334. Name: req.Name,
  335. Number: req.Number,
  336. ParentId: req.ParentId,
  337. }
  338. if err := s.DB.Model(new(model.ForageCategory)).Omit("is_show", "is_delete").
  339. Where("id = ?", req.Id).
  340. Updates(updateData).Error; err != nil {
  341. return xerr.WithStack(err)
  342. }
  343. defer func() {
  344. body := &model.CategoryRequest{
  345. ParentId: int32(req.ParentId),
  346. ParentName: req.ParentName,
  347. Name: req.Name,
  348. Id: int32(req.Id),
  349. Number: req.Number,
  350. IsShow: int32(req.IsShow),
  351. GroupId: int32(req.Id),
  352. }
  353. s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body)
  354. }()
  355. return nil
  356. }
  357. // IsShowForageCategory 是否启用
  358. func (s *StoreEntry) IsShowForageCategory(ctx context.Context, req *operationPb.IsShowForageCategory) error {
  359. forageCategory := &model.ForageCategory{Id: int64(req.ForageCategoryId)}
  360. if err := s.DB.First(forageCategory).Error; err != nil {
  361. if errors.Is(err, gorm.ErrRecordNotFound) {
  362. return xerr.Custom("该数据不存在")
  363. }
  364. return xerr.WithStack(err)
  365. }
  366. if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", req.ForageCategoryId).Update("is_show", req.IsShow).Error; err != nil {
  367. return xerr.WithStack(err)
  368. }
  369. defer func() {
  370. body := &model.CategoryRequest{
  371. ParentId: int32(forageCategory.ParentId),
  372. ParentName: forageCategory.ParentName,
  373. Name: forageCategory.Name,
  374. Id: req.ForageCategoryId,
  375. Number: forageCategory.Number,
  376. IsShow: int32(req.IsShow),
  377. GroupId: int32(forageCategory.Id),
  378. }
  379. s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body)
  380. }()
  381. return nil
  382. }
  383. // DeleteForageCategory 是否删除
  384. func (s *StoreEntry) DeleteForageCategory(ctx context.Context, forageCategoryId int64) error {
  385. forageCategory := &model.ForageCategory{Id: forageCategoryId}
  386. if err := s.DB.First(forageCategory).Error; err != nil {
  387. if errors.Is(err, gorm.ErrRecordNotFound) {
  388. return xerr.Custom("该数据不存在")
  389. }
  390. return xerr.WithStack(err)
  391. }
  392. if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", forageCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  393. return xerr.WithStack(err)
  394. }
  395. return nil
  396. }
  397. // SearchForageCategoryList 饲料分类类别列表
  398. func (s *StoreEntry) SearchForageCategoryList(ctx context.Context, req *operationPb.SearchForageCategoryRequest) (*operationPb.SearchForageCategoryResponse, error) {
  399. forageCategory := make([]*model.ForageCategory, 0)
  400. var count int64 = 0
  401. pref := s.DB.Model(new(model.ForageCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
  402. if req.Name != "" {
  403. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  404. }
  405. if req.ParentId > 0 {
  406. pref.Where("parent_id = ?", req.ParentId)
  407. }
  408. if req.Number != "" {
  409. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  410. }
  411. if req.IsShow > 0 {
  412. pref.Where("is_show = ?", req.IsShow)
  413. }
  414. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  415. Find(&forageCategory).Debug().Error; err != nil {
  416. return nil, xerr.WithStack(err)
  417. }
  418. return &operationPb.SearchForageCategoryResponse{
  419. Code: http.StatusOK,
  420. Msg: "ok",
  421. Data: &operationPb.SearchForageCategoryData{
  422. Page: req.Pagination.Page,
  423. PageSize: req.Pagination.PageSize,
  424. Total: int32(count),
  425. List: model.ForageCategorySlice(forageCategory).ToPB(),
  426. },
  427. }, nil
  428. }
  429. // CategoryDistribution 饲料分类和畜牧分类下发
  430. func (s *StoreEntry) CategoryDistribution(ctx context.Context, url string, req *model.CategoryRequest) error {
  431. groupList, err := s.SearchGroupPastureList(ctx, &operationPb.SearchPastureRequest{})
  432. if err != nil {
  433. return xerr.WithStack(err)
  434. }
  435. wg := sync.WaitGroup{}
  436. wg.Add(len(groupList.Data.List))
  437. for _, v := range groupList.Data.List {
  438. go func(data *operationPb.AddPastureRequest) {
  439. res := &model.PastureCommonResponse{}
  440. req.Id = data.Id
  441. if err = s.PastureHttpClient(ctx, url, int64(data.Id), req, res); err != nil {
  442. zaplog.Error("CategoryDistribution",
  443. zap.Any("url", url),
  444. zap.Any("err", err),
  445. zap.Any("body", req),
  446. zap.Any("res", res),
  447. )
  448. }
  449. if res.Code != http.StatusOK {
  450. zaplog.Error("CategoryDistribution-http",
  451. zap.Any("url", url),
  452. zap.Any("body", req),
  453. zap.Any("res", res),
  454. )
  455. }
  456. wg.Done()
  457. }(v)
  458. }
  459. wg.Wait()
  460. return nil
  461. }
  462. // CreateForage 创建饲料
  463. func (s *StoreEntry) CreateForage(ctx context.Context, req *operationPb.AddForageRequest) error {
  464. forage := model.NewForage(req)
  465. if err := s.DB.Create(forage).Error; err != nil {
  466. return xerr.WithStack(err)
  467. }
  468. return nil
  469. }
  470. // EditForage 编辑饲料
  471. func (s *StoreEntry) EditForage(ctx context.Context, req *operationPb.AddForageRequest) error {
  472. forage := model.Forage{Id: int64(req.Id)}
  473. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(&forage).Error; err != nil {
  474. if errors.Is(err, gorm.ErrRecordNotFound) {
  475. return xerr.Custom("该数据不存在")
  476. }
  477. return xerr.WithStack(err)
  478. }
  479. updateData := &model.Forage{
  480. Name: req.Name,
  481. CategoryId: int64(req.CategoryId),
  482. CategoryName: req.CategoryName,
  483. MaterialType: req.MaterialType,
  484. UniqueEncode: req.UniqueEncode,
  485. ForageSourceId: req.ForageSourceId,
  486. PlanTypeId: req.PlanTypeId,
  487. SmallMaterialScale: req.SmallMaterialScale,
  488. AllowError: int64(req.AllowError),
  489. PackageWeight: int64(req.PackageWeight),
  490. Price: int64(req.Price),
  491. JumpWeight: int64(req.JumpWeight),
  492. JumpDelay: req.JumpDelay,
  493. ConfirmStart: req.ConfirmStart,
  494. RelayLocations: int64(req.RelayLocations),
  495. Jmp: req.Jmp,
  496. Backup1: req.Backup1,
  497. Backup2: req.Backup2,
  498. Backup3: req.Backup3,
  499. }
  500. if err := s.DB.Model(new(model.Forage)).Omit("is_show", "is_delete").Where("id = ?", req.Id).
  501. Updates(updateData).Error; err != nil {
  502. return xerr.WithStack(err)
  503. }
  504. return nil
  505. }
  506. func (s *StoreEntry) SearchForageList(ctx context.Context, req *operationPb.SearchForageListRequest) (*operationPb.SearchForageListResponse, error) {
  507. forage := make([]*model.Forage, 0)
  508. var count int64 = 0
  509. pref := s.DB.Model(new(model.Forage)).Where("is_delete = ?", operationPb.IsShow_OK)
  510. if req.Name != "" {
  511. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  512. }
  513. if req.CategoryId > 0 {
  514. pref.Where("category_id = ?", req.CategoryId)
  515. }
  516. if req.ForageSourceId > 0 {
  517. pref.Where("forage_source_id = ?", req.ForageSourceId)
  518. }
  519. if req.IsShow > 0 {
  520. pref.Where("is_show = ?", req.IsShow)
  521. }
  522. if req.AllowError > 0 {
  523. pref.Where("allow_error = ?", req.AllowError)
  524. }
  525. if req.JumpWeight > 0 {
  526. pref.Where("jump_weight = ?", req.JumpWeight)
  527. }
  528. if req.JumpDelay > 0 {
  529. pref.Where("jump_delay = ?", req.JumpDelay)
  530. }
  531. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  532. Find(&forage).Error; err != nil {
  533. return nil, xerr.WithStack(err)
  534. }
  535. return &operationPb.SearchForageListResponse{
  536. Code: http.StatusOK,
  537. Msg: "ok",
  538. Data: &operationPb.SearchForageList{
  539. Page: req.Pagination.Page,
  540. PageSize: req.Pagination.PageSize,
  541. Total: int32(count),
  542. List: model.ForageSlice(forage).ToPB(),
  543. },
  544. }, nil
  545. }
  546. func (s *StoreEntry) ForageEnumList(ctx context.Context) *operationPb.ForageEnumListResponse {
  547. res := &operationPb.ForageEnumListResponse{
  548. Code: http.StatusOK,
  549. Msg: "ok",
  550. Data: &operationPb.ForageEnumList{
  551. JumpDelaType: make([]*operationPb.JumpDelaTypeEnum, 0),
  552. ForageSource: make([]*operationPb.ForageSourceEnum, 0),
  553. ForagePlanType: make([]*operationPb.ForagePlanTypeEnum, 0),
  554. CattleParentCategory: make([]*operationPb.CattleParentCategoryEnum, 0),
  555. ForageParentCategory: make([]*operationPb.ForageParentCategoryEnum, 0),
  556. IsShow: make([]*operationPb.IsShowEnum, 0),
  557. FormulaType: make([]*operationPb.FormulaTypeEnum, 0),
  558. FormulaList: make([]*operationPb.FormulaOptionEnum, 0),
  559. ConfirmStart: make([]*operationPb.IsShowEnum, 0),
  560. FormulationEvaluation: make([]*operationPb.FormulaOptionEnum, 0),
  561. UseMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
  562. UseMaterialsList: make([]*operationPb.FormulaOptionEnum, 0),
  563. PriceMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
  564. },
  565. }
  566. // 跳转延迟
  567. res.Data.JumpDelaType = append(res.Data.JumpDelaType, &operationPb.JumpDelaTypeEnum{
  568. Value: operationPb.JumpDelaType_INVALID,
  569. Label: "禁用",
  570. }, &operationPb.JumpDelaTypeEnum{
  571. Value: operationPb.JumpDelaType_THREE,
  572. Label: "3秒",
  573. }, &operationPb.JumpDelaTypeEnum{
  574. Value: operationPb.JumpDelaType_SIX,
  575. Label: "6秒",
  576. })
  577. // 饲料来源
  578. res.Data.ForageSource = append(res.Data.ForageSource, &operationPb.ForageSourceEnum{
  579. Value: operationPb.ForageSource_SYSTEM_BUILT_IN,
  580. Label: "系统内置",
  581. }, &operationPb.ForageSourceEnum{
  582. Value: operationPb.ForageSource_USER_DEFINED,
  583. Label: "用户自定义",
  584. })
  585. // 计划类型
  586. res.Data.ForagePlanType = append(res.Data.ForagePlanType,
  587. &operationPb.ForagePlanTypeEnum{
  588. Value: operationPb.ForagePlanType_INVALID,
  589. Label: "无",
  590. },
  591. &operationPb.ForagePlanTypeEnum{
  592. Value: operationPb.ForagePlanType_FORKLIFT,
  593. Label: "铲车",
  594. }, &operationPb.ForagePlanTypeEnum{
  595. Value: operationPb.ForagePlanType_CONCENTRATE,
  596. Label: "精料",
  597. })
  598. // 畜牧类别
  599. res.Data.CattleParentCategory = append(res.Data.CattleParentCategory, &operationPb.CattleParentCategoryEnum{
  600. Value: operationPb.CattleCategoryParent_LACTATION_CAW,
  601. Label: "泌乳牛",
  602. }, &operationPb.CattleParentCategoryEnum{
  603. Value: operationPb.CattleCategoryParent_FATTEN_CAW,
  604. Label: "育肥牛",
  605. }, &operationPb.CattleParentCategoryEnum{
  606. Value: operationPb.CattleCategoryParent_RESERVE_CAW,
  607. Label: "后备牛",
  608. }, &operationPb.CattleParentCategoryEnum{
  609. Value: operationPb.CattleCategoryParent_DRY_CAW,
  610. Label: "干奶牛",
  611. }, &operationPb.CattleParentCategoryEnum{
  612. Value: operationPb.CattleCategoryParent_PERINATAL_CAW,
  613. Label: "围产牛",
  614. }, &operationPb.CattleParentCategoryEnum{
  615. Value: operationPb.CattleCategoryParent_OTHER_CAW,
  616. Label: "其他",
  617. })
  618. // 饲料类别
  619. res.Data.ForageParentCategory = append(res.Data.ForageParentCategory, &operationPb.ForageParentCategoryEnum{
  620. Value: operationPb.ForageCategoryParent_ROUGHAGE,
  621. Label: "粗料",
  622. }, &operationPb.ForageParentCategoryEnum{
  623. Value: operationPb.ForageCategoryParent_CONCENTRATE,
  624. Label: "精料",
  625. }, &operationPb.ForageParentCategoryEnum{
  626. Value: operationPb.ForageCategoryParent_HALF_ROUGHAGE_HALF_CONCENTRATE,
  627. Label: "粗料精料参半",
  628. }, &operationPb.ForageParentCategoryEnum{
  629. Value: operationPb.ForageCategoryParent_OTHER,
  630. Label: "其他",
  631. })
  632. res.Data.IsShow = append(res.Data.IsShow, &operationPb.IsShowEnum{
  633. Value: operationPb.IsShow_OK,
  634. Label: "是",
  635. }, &operationPb.IsShowEnum{
  636. Value: operationPb.IsShow_NO,
  637. Label: "否",
  638. })
  639. res.Data.FormulationEvaluation = append(res.Data.FormulationEvaluation, &operationPb.FormulaOptionEnum{
  640. Value: 0,
  641. Label: "按配方查询",
  642. }, &operationPb.FormulaOptionEnum{
  643. Value: 1,
  644. Label: "按栏舍查询",
  645. })
  646. res.Data.FormulaType = append(res.Data.FormulaType, &operationPb.FormulaTypeEnum{
  647. Value: operationPb.FormulaType_FEED_FORMULA,
  648. Label: "饲喂配方",
  649. }, &operationPb.FormulaTypeEnum{
  650. Value: operationPb.FormulaType_PREMIXED_FORMULA,
  651. Label: "预混配方",
  652. }, &operationPb.FormulaTypeEnum{
  653. Value: operationPb.FormulaType_SUPPLEMENTARY_FORMULA,
  654. Label: "补料配方",
  655. })
  656. res.Data.ConfirmStart = append(res.Data.ConfirmStart, &operationPb.IsShowEnum{
  657. Value: operationPb.IsShow_OK,
  658. Label: "启用",
  659. }, &operationPb.IsShowEnum{
  660. Value: operationPb.IsShow_NO,
  661. Label: "禁用",
  662. })
  663. res.Data.UseMaterialsList = append(res.Data.UseMaterialsList, &operationPb.FormulaOptionEnum{
  664. Value: 1,
  665. Label: "理论",
  666. }, &operationPb.FormulaOptionEnum{
  667. Value: 2,
  668. Label: "实际",
  669. })
  670. res.Data.PriceMaterialsType = append(res.Data.PriceMaterialsType, &operationPb.FormulaOptionEnum{
  671. Value: 1,
  672. Label: "畜牧类别",
  673. }, &operationPb.FormulaOptionEnum{
  674. Value: 2,
  675. Label: "栏舍名称",
  676. }, &operationPb.FormulaOptionEnum{
  677. Value: 3,
  678. Label: "日期",
  679. }, &operationPb.FormulaOptionEnum{
  680. Value: 4,
  681. Label: "TMR设备号",
  682. })
  683. res.Data.UseMaterialsType = append(res.Data.PriceMaterialsType, &operationPb.FormulaOptionEnum{
  684. Value: 5,
  685. Label: "TMR班次",
  686. }, &operationPb.FormulaOptionEnum{
  687. Value: 6,
  688. Label: "车次",
  689. })
  690. res.Data.JumpType = append(res.Data.JumpType, &operationPb.FormulaOptionEnum{
  691. Value: 0,
  692. Label: "手动跳转",
  693. }, &operationPb.FormulaOptionEnum{
  694. Value: 1,
  695. Label: "自动跳转",
  696. })
  697. res.Data.StatisticsType = append(res.Data.StatisticsType, &operationPb.FormulaOptionEnum{
  698. Value: 7,
  699. Label: "无分类",
  700. }, &operationPb.FormulaOptionEnum{
  701. Value: 0,
  702. Label: "驾驶员",
  703. }, &operationPb.FormulaOptionEnum{
  704. Value: 1,
  705. Label: "配方名称",
  706. }, &operationPb.FormulaOptionEnum{
  707. Value: 2,
  708. Label: "栏舍名称",
  709. }, &operationPb.FormulaOptionEnum{
  710. Value: 3,
  711. Label: "牧畜类别",
  712. }, &operationPb.FormulaOptionEnum{
  713. Value: 4,
  714. Label: "车次",
  715. }, &operationPb.FormulaOptionEnum{
  716. Value: 5,
  717. Label: "TMR名称",
  718. }, &operationPb.FormulaOptionEnum{
  719. Value: 6,
  720. Label: "饲料",
  721. })
  722. res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
  723. Value: 0,
  724. Label: "所有",
  725. })
  726. formulaList := make([]*model.FeedFormula, 0)
  727. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).Find(&formulaList).Error; err != nil {
  728. zaplog.Error("OptionFormula", zap.Any("Err", err))
  729. return res
  730. }
  731. for _, v := range formulaList {
  732. res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
  733. Value: int32(v.Id),
  734. Label: v.Name,
  735. })
  736. }
  737. return res
  738. }
  739. func (s *StoreEntry) DeleteForageList(ctx context.Context, ids []int64) error {
  740. if len(ids) == 0 {
  741. return xerr.Custom("参数错误")
  742. }
  743. if err := s.DB.Model(new(model.Forage)).Where("id IN ?", ids).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  744. return xerr.WithStack(err)
  745. }
  746. return nil
  747. }
  748. func (s *StoreEntry) IsShowForage(ctx context.Context, req *operationPb.IsShowForage) error {
  749. forage := &model.Forage{Id: int64(req.ForageId)}
  750. if err := s.DB.First(forage).Error; err != nil {
  751. if errors.Is(err, gorm.ErrRecordNotFound) {
  752. return xerr.Custom("该数据不存在")
  753. }
  754. return xerr.WithStack(err)
  755. }
  756. if err := s.DB.Model(new(model.Forage)).Where("id = ?", req.ForageId).Update("is_show", req.IsShow).Error; err != nil {
  757. return xerr.WithStack(err)
  758. }
  759. return nil
  760. }
  761. // ExcelImportForage 导入excel
  762. func (s *StoreEntry) ExcelImportForage(ctx context.Context, req io.Reader) error {
  763. xlsx, err := excelize.OpenReader(req)
  764. if err != nil {
  765. return xerr.WithStack(err)
  766. }
  767. defer xlsx.Close()
  768. rows, err := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
  769. if err != nil {
  770. return xerr.WithStack(err)
  771. }
  772. if len(rows) > 10000 {
  773. rows = rows[:10000]
  774. }
  775. forageList := make([]*model.Forage, 0)
  776. for i, row := range rows {
  777. if i == 0 {
  778. continue
  779. }
  780. var (
  781. forageSourceId int32
  782. categoryId, price, jumpWeight, relayLocations int
  783. allowError, packageWeight int
  784. name, uniqueEncode, backup1, backup2, backup3 string
  785. )
  786. for k, v := range row {
  787. if k == 0 {
  788. name = v
  789. }
  790. if k == 2 {
  791. uniqueEncode = v
  792. }
  793. if k == 3 {
  794. forageSourceId = operationPb.ForageSource_Kind_value[v]
  795. }
  796. if k == 5 {
  797. allowError, _ = strconv.Atoi(v)
  798. }
  799. if k == 6 {
  800. packageWeight, _ = strconv.Atoi(v)
  801. }
  802. if k == 7 {
  803. price, _ = strconv.Atoi(v)
  804. }
  805. if k == 8 {
  806. jumpWeight, _ = strconv.Atoi(v)
  807. }
  808. if k == 11 {
  809. relayLocations, _ = strconv.Atoi(v)
  810. }
  811. if k == 13 {
  812. backup1 = v
  813. }
  814. if k == 14 {
  815. backup2 = v
  816. }
  817. if k == 15 {
  818. backup3 = v
  819. }
  820. }
  821. forageItem := &model.Forage{
  822. Name: name,
  823. CategoryId: int64(categoryId),
  824. MaterialType: 0,
  825. UniqueEncode: uniqueEncode,
  826. ForageSourceId: operationPb.ForageSource_Kind(forageSourceId),
  827. PlanTypeId: 0,
  828. SmallMaterialScale: "",
  829. AllowError: int64(allowError),
  830. PackageWeight: int64(packageWeight),
  831. Price: int64(price),
  832. JumpWeight: int64(jumpWeight),
  833. JumpDelay: 0,
  834. ConfirmStart: 0,
  835. RelayLocations: int64(relayLocations),
  836. Jmp: 0,
  837. Backup1: backup1,
  838. Backup2: backup2,
  839. Backup3: backup3,
  840. IsShow: operationPb.IsShow_OK,
  841. IsDelete: operationPb.IsShow_OK,
  842. DataSource: operationPb.DataSource_EXCEL_IMPORT,
  843. }
  844. forageList = append(forageList, forageItem)
  845. }
  846. if len(forageList) > 0 {
  847. if err = s.DB.Create(forageList).Error; err != nil {
  848. return xerr.WithStack(err)
  849. }
  850. }
  851. return nil
  852. }
  853. // ExcelExportForage 流式导出excel
  854. func (s *StoreEntry) ExcelExportForage(ctx context.Context, req *operationPb.SearchForageListRequest) (*bytes.Buffer, error) {
  855. res, err := s.SearchForageList(ctx, req)
  856. if err != nil {
  857. return nil, xerr.WithStack(err)
  858. }
  859. if len(res.Data.List) <= 0 {
  860. return nil, xerr.Custom("数据为空")
  861. }
  862. file := excelize.NewFile()
  863. defer file.Close()
  864. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  865. if err != nil {
  866. return nil, xerr.WithStack(err)
  867. }
  868. titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
  869. "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
  870. if err = streamWriter.SetRow("A1", titles); err != nil {
  871. return nil, xerr.WithStack(err)
  872. }
  873. for i, item := range res.Data.List {
  874. cell, err := excelize.CoordinatesToCellName(1, i+2)
  875. if err != nil {
  876. zaplog.Error("excelize.CoordinatesToCellName", zap.Any("Err", err))
  877. continue
  878. }
  879. row := make([]interface{}, 0)
  880. row = append(row, item.Name, item.CategoryName, item.UniqueEncode, item.ForageSourceId, item.PlanTypeId,
  881. item.AllowError, item.PackageWeight, float64(item.Price/100.00), item.JumpWeight, item.JumpDelay,
  882. item.ConfirmStart, item.RelayLocations, item.Jmp, item.Backup1, item.Backup2, item.Backup3)
  883. if err = streamWriter.SetRow(cell, row); err != nil {
  884. return nil, xerr.WithStack(err)
  885. }
  886. }
  887. if err = streamWriter.Flush(); err != nil {
  888. return nil, xerr.WithStack(err)
  889. }
  890. return file.WriteToBuffer()
  891. }
  892. // ExcelTemplateForage 导出模板
  893. func (s *StoreEntry) ExcelTemplateForage(ctx context.Context) (*bytes.Buffer, error) {
  894. file := excelize.NewFile()
  895. defer file.Close()
  896. streamWriter, err := file.NewStreamWriter("Sheet1")
  897. if err != nil {
  898. return nil, xerr.WithStack(err)
  899. }
  900. titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
  901. "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
  902. if err = streamWriter.SetRow("A1", titles); err != nil {
  903. return nil, xerr.WithStack(err)
  904. }
  905. if err = streamWriter.Flush(); err != nil {
  906. return nil, xerr.WithStack(err)
  907. }
  908. return file.WriteToBuffer()
  909. }