pasture_service.go 28 KB

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