pasture_service.go 26 KB

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