pasture_service.go 28 KB

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