pasture_service.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. ForageSourceMap = map[operationPb.ForageSource_Kind]string{
  35. operationPb.ForageSource_SYSTEM_BUILT_IN: "系统内置",
  36. operationPb.ForageSource_USER_DEFINED: "用户自定义",
  37. }
  38. ForagePlanTypeMap = map[operationPb.ForagePlanType_Kind]string{
  39. operationPb.ForagePlanType_INVALID: "无",
  40. operationPb.ForagePlanType_FORKLIFT: "铲车",
  41. operationPb.ForagePlanType_CONCENTRATE: "精料",
  42. }
  43. JumpDelaTypeMap = map[operationPb.JumpDelaType_Kind]string{
  44. operationPb.JumpDelaType_INVALID: "禁用",
  45. operationPb.JumpDelaType_THREE: "3秒",
  46. operationPb.JumpDelaType_SIX: "6秒",
  47. operationPb.JumpDelaType_NINE: "9秒",
  48. }
  49. IsShowMap = map[operationPb.IsShow_Kind]string{
  50. operationPb.IsShow_OK: "是",
  51. operationPb.IsShow_NO: "否",
  52. }
  53. )
  54. // CreateGroupPasture 创建集团牧场
  55. func (s *StoreEntry) CreateGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  56. pastureList := model.NewGroupPasture(req)
  57. if err := s.DB.Create(pastureList).Error; err != nil {
  58. return xerr.WithStack(err)
  59. }
  60. return 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).Debug().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 = ?", 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. UniqueEncode: req.UniqueEncode,
  368. ForageSourceId: req.ForageSourceId,
  369. PlanTypeId: req.PlanTypeId,
  370. SmallMaterialScale: req.SmallMaterialScale,
  371. AllowError: int64(req.AllowError),
  372. PackageWeight: int64(req.PackageWeight),
  373. Price: int64(req.Price),
  374. JumpWeight: int64(req.JumpWeight),
  375. JumpDelay: req.JumpDelay,
  376. ConfirmStart: req.ConfirmStart,
  377. RelayLocations: int64(req.RelayLocations),
  378. Jmp: req.Jmp,
  379. Backup1: req.Backup1,
  380. Backup2: req.Backup2,
  381. Backup3: req.Backup3,
  382. }
  383. if err := s.DB.Model(new(model.Forage)).Omit("is_show", "is_delete").Where("id = ?", req.Id).
  384. Updates(updateData).Error; err != nil {
  385. return xerr.WithStack(err)
  386. }
  387. return nil
  388. }
  389. func (s *StoreEntry) SearchForageList(ctx context.Context, req *operationPb.SearchForageListRequest) (*operationPb.SearchForageListResponse, error) {
  390. forage := make([]*model.Forage, 0)
  391. var count int64 = 0
  392. pref := s.DB.Model(new(model.Forage)).Where("is_delete = ?", operationPb.IsShow_OK)
  393. if req.Name != "" {
  394. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  395. }
  396. if req.CategoryId != "" {
  397. pref.Where("category_id = ?", req.CategoryId)
  398. }
  399. if req.ForageSourceId > 0 {
  400. pref.Where("forage_source_id = ?", req.ForageSourceId)
  401. }
  402. if req.IsShow > 0 {
  403. pref.Where("is_show = ?", req.IsShow)
  404. }
  405. if req.AllowError > 0 {
  406. pref.Where("allow_error = ?", req.AllowError)
  407. }
  408. if req.JumpWeight > 0 {
  409. pref.Where("jump_weight = ?", req.JumpWeight)
  410. }
  411. if req.JumpDelay > 0 {
  412. pref.Where("jump_delay = ?", req.JumpDelay)
  413. }
  414. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  415. Find(&forage).Error; err != nil {
  416. return nil, xerr.WithStack(err)
  417. }
  418. return &operationPb.SearchForageListResponse{
  419. Code: http.StatusOK,
  420. Msg: "ok",
  421. Data: &operationPb.SearchForageList{
  422. Page: req.Pagination.Page,
  423. PageSize: req.Pagination.PageSize,
  424. Total: int32(count),
  425. List: model.ForageSlice(forage).ToPB(),
  426. },
  427. }, nil
  428. }
  429. func (s *StoreEntry) ForageEnumList(ctx context.Context) *operationPb.ForageEnumListResponse {
  430. res := &operationPb.ForageEnumListResponse{
  431. Code: http.StatusOK,
  432. Msg: "ok",
  433. Data: &operationPb.ForageEnumList{
  434. JumpDelaType: make([]*operationPb.JumpDelaTypeEnum, 0),
  435. ForageSource: make([]*operationPb.ForageSourceEnum, 0),
  436. ForagePlanType: make([]*operationPb.ForagePlanTypeEnum, 0),
  437. CattleParentCategory: make([]*operationPb.CattleParentCategoryEnum, 0),
  438. ForageParentCategory: make([]*operationPb.ForageParentCategoryEnum, 0),
  439. IsShow: make([]*operationPb.IsShowEnum, 0),
  440. FormulaType: make([]*operationPb.FormulaTypeEnum, 0),
  441. FormulaList: make([]*operationPb.FormulaOptionEnum, 0),
  442. ConfirmStart: make([]*operationPb.IsShowEnum, 0),
  443. FormulationEvaluation: make([]*operationPb.FormulaOptionEnum, 0),
  444. UseMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
  445. UseMaterialsList: make([]*operationPb.FormulaOptionEnum, 0),
  446. PriceMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
  447. },
  448. }
  449. // 跳转延迟
  450. res.Data.JumpDelaType = append(res.Data.JumpDelaType, &operationPb.JumpDelaTypeEnum{
  451. Value: operationPb.JumpDelaType_INVALID,
  452. Label: "禁用",
  453. }, &operationPb.JumpDelaTypeEnum{
  454. Value: operationPb.JumpDelaType_THREE,
  455. Label: "3秒",
  456. }, &operationPb.JumpDelaTypeEnum{
  457. Value: operationPb.JumpDelaType_SIX,
  458. Label: "6秒",
  459. })
  460. // 饲料来源
  461. res.Data.ForageSource = append(res.Data.ForageSource, &operationPb.ForageSourceEnum{
  462. Value: operationPb.ForageSource_SYSTEM_BUILT_IN,
  463. Label: "系统内置",
  464. }, &operationPb.ForageSourceEnum{
  465. Value: operationPb.ForageSource_USER_DEFINED,
  466. Label: "用户自定义",
  467. })
  468. // 计划类型
  469. res.Data.ForagePlanType = append(res.Data.ForagePlanType,
  470. &operationPb.ForagePlanTypeEnum{
  471. Value: operationPb.ForagePlanType_INVALID,
  472. Label: "无",
  473. },
  474. &operationPb.ForagePlanTypeEnum{
  475. Value: operationPb.ForagePlanType_FORKLIFT,
  476. Label: "铲车",
  477. }, &operationPb.ForagePlanTypeEnum{
  478. Value: operationPb.ForagePlanType_CONCENTRATE,
  479. Label: "精料",
  480. })
  481. // 畜牧类别
  482. res.Data.CattleParentCategory = append(res.Data.CattleParentCategory, &operationPb.CattleParentCategoryEnum{
  483. Value: operationPb.CattleCategoryParent_LACTATION_CAW,
  484. Label: "泌乳牛",
  485. }, &operationPb.CattleParentCategoryEnum{
  486. Value: operationPb.CattleCategoryParent_FATTEN_CAW,
  487. Label: "育肥牛",
  488. }, &operationPb.CattleParentCategoryEnum{
  489. Value: operationPb.CattleCategoryParent_RESERVE_CAW,
  490. Label: "后备牛",
  491. }, &operationPb.CattleParentCategoryEnum{
  492. Value: operationPb.CattleCategoryParent_DRY_CAW,
  493. Label: "干奶牛",
  494. }, &operationPb.CattleParentCategoryEnum{
  495. Value: operationPb.CattleCategoryParent_PERINATAL_CAW,
  496. Label: "围产牛",
  497. }, &operationPb.CattleParentCategoryEnum{
  498. Value: operationPb.CattleCategoryParent_OTHER_CAW,
  499. Label: "其他",
  500. })
  501. // 饲料类别
  502. res.Data.ForageParentCategory = append(res.Data.ForageParentCategory, &operationPb.ForageParentCategoryEnum{
  503. Value: operationPb.ForageCategoryParent_ROUGHAGE,
  504. Label: "粗料",
  505. }, &operationPb.ForageParentCategoryEnum{
  506. Value: operationPb.ForageCategoryParent_CONCENTRATE,
  507. Label: "精料",
  508. }, &operationPb.ForageParentCategoryEnum{
  509. Value: operationPb.ForageCategoryParent_HALF_ROUGHAGE_HALF_CONCENTRATE,
  510. Label: "粗料精料参半",
  511. }, &operationPb.ForageParentCategoryEnum{
  512. Value: operationPb.ForageCategoryParent_OTHER,
  513. Label: "其他",
  514. })
  515. res.Data.IsShow = append(res.Data.IsShow, &operationPb.IsShowEnum{
  516. Value: operationPb.IsShow_OK,
  517. Label: "是",
  518. }, &operationPb.IsShowEnum{
  519. Value: operationPb.IsShow_NO,
  520. Label: "否",
  521. })
  522. res.Data.FormulationEvaluation = append(res.Data.FormulationEvaluation, &operationPb.FormulaOptionEnum{
  523. Value: 0,
  524. Label: "按配方查询",
  525. }, &operationPb.FormulaOptionEnum{
  526. Value: 1,
  527. Label: "按栏舍查询",
  528. })
  529. res.Data.FormulaType = append(res.Data.FormulaType, &operationPb.FormulaTypeEnum{
  530. Value: operationPb.FormulaType_FEED_FORMULA,
  531. Label: "饲喂配方",
  532. }, &operationPb.FormulaTypeEnum{
  533. Value: operationPb.FormulaType_PREMIXED_FORMULA,
  534. Label: "预混配方",
  535. }, &operationPb.FormulaTypeEnum{
  536. Value: operationPb.FormulaType_SUPPLEMENTARY_FORMULA,
  537. Label: "补料配方",
  538. })
  539. res.Data.ConfirmStart = append(res.Data.ConfirmStart, &operationPb.IsShowEnum{
  540. Value: operationPb.IsShow_OK,
  541. Label: "启用",
  542. }, &operationPb.IsShowEnum{
  543. Value: operationPb.IsShow_NO,
  544. Label: "禁用",
  545. })
  546. res.Data.UseMaterialsList = append(res.Data.UseMaterialsList, &operationPb.FormulaOptionEnum{
  547. Value: 1,
  548. Label: "理论",
  549. }, &operationPb.FormulaOptionEnum{
  550. Value: 2,
  551. Label: "实际",
  552. })
  553. res.Data.UseMaterialsType = append(res.Data.UseMaterialsType, &operationPb.FormulaOptionEnum{
  554. Value: 1,
  555. Label: "畜牧类别",
  556. }, &operationPb.FormulaOptionEnum{
  557. Value: 2,
  558. Label: "栏舍名称",
  559. }, &operationPb.FormulaOptionEnum{
  560. Value: 3,
  561. Label: "日期",
  562. }, &operationPb.FormulaOptionEnum{
  563. Value: 4,
  564. Label: "TMR设备号",
  565. })
  566. res.Data.PriceMaterialsType = append(res.Data.UseMaterialsType, &operationPb.FormulaOptionEnum{
  567. Value: 5,
  568. Label: "TMR班次",
  569. }, &operationPb.FormulaOptionEnum{
  570. Value: 6,
  571. Label: "车次",
  572. })
  573. res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
  574. Value: 0,
  575. Label: "所有",
  576. })
  577. formulaList := make([]*model.FeedFormula, 0)
  578. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).Find(&formulaList).Error; err != nil {
  579. zaplog.Error("OptionFormula", zap.Any("Err", err))
  580. return res
  581. }
  582. for _, v := range formulaList {
  583. res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
  584. Value: int32(v.Id),
  585. Label: v.Name,
  586. })
  587. }
  588. return res
  589. }
  590. func (s *StoreEntry) DeleteForageList(ctx context.Context, ids []int64) error {
  591. if len(ids) == 0 {
  592. return xerr.Custom("参数错误")
  593. }
  594. if err := s.DB.Model(new(model.Forage)).Where("id IN ?", ids).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  595. return xerr.WithStack(err)
  596. }
  597. return nil
  598. }
  599. func (s *StoreEntry) IsShowForage(ctx context.Context, req *operationPb.IsShowForage) error {
  600. forage := &model.Forage{Id: int64(req.ForageId)}
  601. if err := s.DB.First(forage).Error; err != nil {
  602. if errors.Is(err, gorm.ErrRecordNotFound) {
  603. return xerr.Custom("该数据不存在")
  604. }
  605. return xerr.WithStack(err)
  606. }
  607. if err := s.DB.Model(new(model.Forage)).Where("id = ?", req.ForageId).Update("is_show", req.IsShow).Error; err != nil {
  608. return xerr.WithStack(err)
  609. }
  610. return nil
  611. }
  612. // ExcelImportForage 导入excel
  613. func (s *StoreEntry) ExcelImportForage(ctx context.Context, req io.Reader) error {
  614. xlsx, err := excelize.OpenReader(req)
  615. if err != nil {
  616. return xerr.WithStack(err)
  617. }
  618. defer xlsx.Close()
  619. rows, err := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
  620. if err != nil {
  621. return xerr.WithStack(err)
  622. }
  623. if len(rows) > 10000 {
  624. rows = rows[:10000]
  625. }
  626. forageList := make([]*model.Forage, 0)
  627. for i, row := range rows {
  628. if i == 0 {
  629. continue
  630. }
  631. var (
  632. forageSourceId int32
  633. categoryId, price, jumpWeight, relayLocations int
  634. allowError, packageWeight int
  635. name, uniqueEncode, backup1, backup2, backup3 string
  636. )
  637. for k, v := range row {
  638. if k == 0 {
  639. name = v
  640. }
  641. if k == 2 {
  642. uniqueEncode = v
  643. }
  644. if k == 3 {
  645. forageSourceId = operationPb.ForageSource_Kind_value[v]
  646. }
  647. if k == 5 {
  648. allowError, _ = strconv.Atoi(v)
  649. }
  650. if k == 6 {
  651. packageWeight, _ = strconv.Atoi(v)
  652. }
  653. if k == 7 {
  654. price, _ = strconv.Atoi(v)
  655. }
  656. if k == 8 {
  657. jumpWeight, _ = strconv.Atoi(v)
  658. }
  659. if k == 11 {
  660. relayLocations, _ = strconv.Atoi(v)
  661. }
  662. if k == 13 {
  663. backup1 = v
  664. }
  665. if k == 14 {
  666. backup2 = v
  667. }
  668. if k == 15 {
  669. backup3 = v
  670. }
  671. }
  672. forageItem := &model.Forage{
  673. Name: name,
  674. CategoryId: int64(categoryId),
  675. MaterialType: 0,
  676. UniqueEncode: uniqueEncode,
  677. ForageSourceId: operationPb.ForageSource_Kind(forageSourceId),
  678. PlanTypeId: 0,
  679. SmallMaterialScale: "",
  680. AllowError: int64(allowError),
  681. PackageWeight: int64(packageWeight),
  682. Price: int64(price),
  683. JumpWeight: int64(jumpWeight),
  684. JumpDelay: 0,
  685. ConfirmStart: 0,
  686. RelayLocations: int64(relayLocations),
  687. Jmp: 0,
  688. Backup1: backup1,
  689. Backup2: backup2,
  690. Backup3: backup3,
  691. IsShow: operationPb.IsShow_OK,
  692. IsDelete: operationPb.IsShow_OK,
  693. DataSource: operationPb.DataSource_EXCEL_IMPORT,
  694. }
  695. forageList = append(forageList, forageItem)
  696. }
  697. if len(forageList) > 0 {
  698. if err = s.DB.Create(forageList).Error; err != nil {
  699. return xerr.WithStack(err)
  700. }
  701. }
  702. return nil
  703. }
  704. // ExcelExportForage 流式导出excel
  705. func (s *StoreEntry) ExcelExportForage(ctx context.Context, req *operationPb.SearchForageListRequest) (*bytes.Buffer, error) {
  706. res, err := s.SearchForageList(ctx, req)
  707. if err != nil {
  708. return nil, xerr.WithStack(err)
  709. }
  710. if len(res.Data.List) <= 0 {
  711. return nil, xerr.Custom("数据为空")
  712. }
  713. file := excelize.NewFile()
  714. defer file.Close()
  715. streamWriter, err := file.NewStreamWriter("Sheet1")
  716. if err != nil {
  717. return nil, xerr.WithStack(err)
  718. }
  719. titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
  720. "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
  721. if err = streamWriter.SetRow("A1", titles); err != nil {
  722. return nil, xerr.WithStack(err)
  723. }
  724. for i, item := range res.Data.List {
  725. cell, err := excelize.CoordinatesToCellName(1, i+2)
  726. if err != nil {
  727. zaplog.Error("excelize.CoordinatesToCellName", zap.Any("Err", err))
  728. continue
  729. }
  730. row := make([]interface{}, 0)
  731. row = append(row, item.Name, item.CategoryName, item.UniqueEncode, item.ForageSourceId, item.PlanTypeId,
  732. item.AllowError, item.PackageWeight, float64(item.Price/100.00), item.JumpWeight, item.JumpDelay,
  733. item.ConfirmStart, item.RelayLocations, item.Jmp, item.Backup1, item.Backup2, item.Backup3)
  734. if err = streamWriter.SetRow(cell, row); err != nil {
  735. return nil, xerr.WithStack(err)
  736. }
  737. }
  738. if err = streamWriter.Flush(); err != nil {
  739. return nil, xerr.WithStack(err)
  740. }
  741. return file.WriteToBuffer()
  742. }
  743. // ExcelTemplateForage 导出模板
  744. func (s *StoreEntry) ExcelTemplateForage(ctx context.Context) (*bytes.Buffer, error) {
  745. file := excelize.NewFile()
  746. defer file.Close()
  747. streamWriter, err := file.NewStreamWriter("Sheet1")
  748. if err != nil {
  749. return nil, xerr.WithStack(err)
  750. }
  751. titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
  752. "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
  753. if err = streamWriter.SetRow("A1", titles); err != nil {
  754. return nil, xerr.WithStack(err)
  755. }
  756. if err = streamWriter.Flush(); err != nil {
  757. return nil, xerr.WithStack(err)
  758. }
  759. return file.WriteToBuffer()
  760. }