pasture_service.go 31 KB

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