statistic_service.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. package backend
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "kpt-tmr-group/model"
  8. "kpt-tmr-group/pkg/logger/zaplog"
  9. "kpt-tmr-group/pkg/xerr"
  10. operationPb "kpt-tmr-group/proto/go/backend/operation"
  11. "net/http"
  12. "sort"
  13. "strconv"
  14. "strings"
  15. "github.com/xuri/excelize/v2"
  16. "go.uber.org/zap"
  17. )
  18. type PastureClientHandler func(ctx context.Context, pastureId int64, body interface{}) error
  19. const FeedFormulaDistributeUrl = "pasture/feed_formula/distribute"
  20. // type eventHandler func(ev map[string]interface{}, openID string, appID string, enterpriseID int, cts int64, conn redis.Conn) error
  21. // PastureDetailById 获取指定牧场详情
  22. func (s *StoreEntry) PastureDetailById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
  23. result := &model.GroupPasture{Id: pastureId}
  24. if err := s.DB.Where("is_delete = ? and is_show = ?", operationPb.IsShow_OK, operationPb.IsShow_OK).First(result).Error; err != nil {
  25. return nil, xerr.WithStack(err)
  26. }
  27. return result, nil
  28. }
  29. func (s *StoreEntry) PastureHttpClient(ctx context.Context, apiUrl string, pastureId int64, body, response interface{}) error {
  30. pastureDetail, err := s.PastureDetailById(ctx, pastureId)
  31. if err != nil {
  32. zaplog.Error("SearchFormulaEstimateList", zap.Any("Err", err), zap.Int64("pastureId", pastureId))
  33. return xerr.Customf("该牧场数据错误,Err:%s", err)
  34. }
  35. pastureClient := model.NewPastureClient(pastureDetail)
  36. url := fmt.Sprintf("%s/%s", pastureDetail.Domain, apiUrl)
  37. result, err := pastureClient.DoPost(url, body)
  38. if err != nil {
  39. return xerr.WithStack(err)
  40. }
  41. zaplog.Info("PastureHttpClient", zap.String("url", url), zap.Any("request", body), zap.String("response", string(result)))
  42. if err = json.Unmarshal(result, response); err != nil {
  43. return xerr.WithStack(err)
  44. }
  45. return nil
  46. }
  47. // SearchFormulaEstimateList 配方评估
  48. func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*model.PastureCommonResponse, error) {
  49. body := &model.PastureCommonRequest{
  50. Name: req.ApiName,
  51. Page: req.Pagination.Page,
  52. Offset: req.Pagination.PageOffset,
  53. PageCount: req.Pagination.PageSize,
  54. ReturnType: "Map",
  55. ParamMaps: &model.FormulaEstimateParams{
  56. PastureId: fmt.Sprintf("%d", req.PastureId),
  57. StartTime: req.StartTime,
  58. StopTime: req.EndTime,
  59. Search: fmt.Sprintf("%d", req.SearchType),
  60. TempletId: fmt.Sprintf("%d", req.TemplateId),
  61. Barid: fmt.Sprintf("%d", req.BarnId),
  62. },
  63. }
  64. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  65. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  66. return nil, xerr.WithStack(err)
  67. }
  68. return response, nil
  69. }
  70. // SearchInventoryStatistics 库存管理-库存统计
  71. func (s *StoreEntry) SearchInventoryStatistics(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*model.PastureCommonResponse, error) {
  72. body := &model.PastureCommonRequest{
  73. Name: req.ApiName,
  74. Page: req.Pagination.Page,
  75. Offset: req.Pagination.PageOffset,
  76. PageCount: req.Pagination.PageSize,
  77. ReturnType: "Map",
  78. ParamMaps: &model.InventoryStatisticsParams{
  79. PastureId: fmt.Sprintf("%d", req.PastureId),
  80. StartTime: req.StartTime,
  81. StopTime: req.EndTime,
  82. FeedName: req.FeedName,
  83. },
  84. }
  85. response := &model.PastureCommonResponse{
  86. Data: &model.PastureCommonData{
  87. List: make([]*model.InventoryStatisticsList, 0),
  88. },
  89. }
  90. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  91. return nil, xerr.WithStack(err)
  92. }
  93. return response, nil
  94. }
  95. // InventoryStatisticsExcelExport 库存管理-库存统计-报表导出
  96. func (s *StoreEntry) InventoryStatisticsExcelExport(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*bytes.Buffer, error) {
  97. result, err := s.SearchInventoryStatistics(ctx, req)
  98. if err != nil {
  99. return nil, xerr.WithStack(err)
  100. }
  101. b, _ := json.Marshal(result.Data.List)
  102. inventoryStatisticsList := make([]*model.InventoryStatisticsList, 0)
  103. if err = json.Unmarshal(b, &inventoryStatisticsList); err != nil {
  104. return nil, xerr.Customf("牧场端返回数据错误")
  105. }
  106. file := excelize.NewFile()
  107. defer file.Close()
  108. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  109. if err != nil {
  110. return nil, xerr.WithStack(err)
  111. }
  112. // 表头
  113. titles := map[string][]interface{}{
  114. "A1": {"饲料名称", "期初", nil, "用料", nil, nil, nil, "期末"},
  115. "A2": {nil, "期初库存(kg)", "期初金额(元)", "入库重量(kg)", "系统出库重量(kg)", "人工用料重量(kg)", "损耗重量", "期末库存(kg)", "期末金额(kg)"},
  116. }
  117. for cell, values := range titles {
  118. if err = streamWriter.SetRow(cell, values); err != nil {
  119. return nil, xerr.WithStack(err)
  120. }
  121. }
  122. for i, item := range inventoryStatisticsList {
  123. cell, err := excelize.CoordinatesToCellName(1, i+3)
  124. if err != nil {
  125. zaplog.Error("InventoryStatisticsExcelExport CoordinatesToCellName", zap.Any("Err", err))
  126. continue
  127. }
  128. row := make([]interface{}, 0)
  129. row = append(row, item.FeedName, item.StartSum, item.StartPrice, item.LaidSum, item.UseSumXT, item.UseSumRG, item.UseSumXH, item.StopSum, item.StopPrice)
  130. if err = streamWriter.SetRow(cell, row); err != nil {
  131. return nil, xerr.WithStack(err)
  132. }
  133. }
  134. hvCell := map[string]string{
  135. "A1": "A2",
  136. "B1": "C1",
  137. "D1": "G1",
  138. "H1": "I1",
  139. }
  140. // 合并单元格
  141. for h, v := range hvCell {
  142. if err = streamWriter.MergeCell(h, v); err != nil {
  143. return nil, xerr.WithStack(err)
  144. }
  145. }
  146. // 修改样式
  147. /*style1, err := file.NewStyle(&excelize.Style{
  148. Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1},
  149. Alignment: &excelize.Alignment{Horizontal: "center"},
  150. })
  151. if err != nil {
  152. return nil, xerr.WithStack(err)
  153. }
  154. style1, err := file.NewStyle(&excelize.Style{
  155. //Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1},
  156. Alignment: &excelize.Alignment{Horizontal: "center"},
  157. })
  158. if err != nil {
  159. return nil, xerr.WithStack(err)
  160. }
  161. if err = file.SetCellStyle(model.DefaultSheetName, "A1", "A1", style1); err != nil {
  162. return nil, xerr.WithStack(err)
  163. }*/
  164. if err = streamWriter.Flush(); err != nil {
  165. return nil, xerr.WithStack(err)
  166. }
  167. return file.WriteToBuffer()
  168. }
  169. // SearchUserMaterialsStatistics 库存管理-用料分析
  170. func (s *StoreEntry) SearchUserMaterialsStatistics(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*model.PastureCommonResponse, error) {
  171. body := &model.PastureCommonRequest{
  172. Name: req.ApiName,
  173. Page: req.Pagination.Page,
  174. Offset: req.Pagination.PageOffset,
  175. PageCount: req.Pagination.PageSize,
  176. ReturnType: "Map",
  177. Checked: req.ErrorCheck,
  178. ParamMaps: &model.UserMaterialsStatisticsParams{
  179. PastureId: fmt.Sprintf("%d", req.PastureId),
  180. StartTime: req.StartTime,
  181. StopTime: req.EndTime,
  182. FeedName: req.FeedName,
  183. Typea: fmt.Sprintf("%d", req.TypeCheck),
  184. },
  185. }
  186. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{
  187. List: &model.UserMaterialsList{},
  188. }}
  189. if err := s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil {
  190. return nil, xerr.WithStack(err)
  191. }
  192. return response, nil
  193. }
  194. func (s *StoreEntry) UserMaterialsStatisticsExcelExport(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*bytes.Buffer, error) {
  195. result, err := s.SearchUserMaterialsStatistics(ctx, req)
  196. if err != nil {
  197. return nil, xerr.WithStack(err)
  198. }
  199. b, _ := json.Marshal(result.Data.List)
  200. userMaterialsList := &model.UserMaterialsList{}
  201. if err = json.Unmarshal(b, userMaterialsList); err != nil {
  202. return nil, xerr.Customf("牧场端返回数据错误")
  203. }
  204. file := excelize.NewFile()
  205. defer file.Close()
  206. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  207. if err != nil {
  208. return nil, xerr.WithStack(err)
  209. }
  210. // 表数据
  211. excelValuesList := map[int][]interface{}{}
  212. cProp := make([]string, 0)
  213. for _, data2 := range userMaterialsList.Data2 {
  214. excelValuesList[1] = append(excelValuesList[1], data2.Label)
  215. for _, c := range data2.Children {
  216. excelValuesList[2] = append(excelValuesList[2], c.Label)
  217. cProp = append(cProp, c.Prop)
  218. }
  219. }
  220. for cell, values := range excelValuesList {
  221. if err = streamWriter.SetRow(fmt.Sprintf("A%d", cell), values); err != nil {
  222. return nil, xerr.WithStack(err)
  223. }
  224. delete(excelValuesList, cell)
  225. }
  226. for i, data1 := range userMaterialsList.Data1 {
  227. data1Map, ok := data1.(map[string]interface{})
  228. if ok {
  229. for _, prop := range cProp {
  230. newValue := ""
  231. if value, yes := data1Map[prop]; yes {
  232. if value != nil {
  233. switch v := value.(type) {
  234. case string:
  235. newValue = v
  236. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  237. newValue = fmt.Sprintf("%d", v)
  238. default:
  239. newValue = fmt.Sprintf("%v", v)
  240. }
  241. }
  242. excelValuesList[i+3] = append(excelValuesList[i+3], newValue)
  243. }
  244. }
  245. }
  246. }
  247. excelValuesKeys := make([]int, 0)
  248. for k, _ := range excelValuesList {
  249. excelValuesKeys = append(excelValuesKeys, k)
  250. }
  251. sort.Ints(excelValuesKeys)
  252. for _, v := range excelValuesKeys {
  253. if err = streamWriter.SetRow(fmt.Sprintf("A%d", v), excelValuesList[v]); err != nil {
  254. return nil, xerr.WithStack(err)
  255. }
  256. }
  257. if err = streamWriter.Flush(); err != nil {
  258. return nil, xerr.WithStack(err)
  259. }
  260. return file.WriteToBuffer()
  261. }
  262. // SearchPriceStatistics 库存管理-价格分析
  263. func (s *StoreEntry) SearchPriceStatistics(ctx context.Context, req *operationPb.SearchPriceStatisticsRequest) (*model.PastureCommonResponse, error) {
  264. body := &model.PastureCommonRequest{
  265. Name: req.ApiName,
  266. Page: req.Pagination.Page,
  267. Offset: req.Pagination.PageOffset,
  268. PageCount: req.Pagination.PageSize,
  269. ReturnType: "Map",
  270. ParamMaps: &model.PriceStatisticsParams{
  271. PastureId: fmt.Sprintf("%d", req.PastureId),
  272. StartTime: req.StartTime,
  273. StopTime: req.EndTime,
  274. FeedName: req.FeedName,
  275. },
  276. }
  277. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  278. if err := s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil {
  279. return nil, xerr.WithStack(err)
  280. }
  281. return response, nil
  282. }
  283. // SearchFeedStatistics 饲喂效率-效率统计
  284. func (s *StoreEntry) SearchFeedStatistics(ctx context.Context, req *operationPb.SearchFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  285. body := &model.PastureCommonRequest{
  286. Name: req.ApiName,
  287. Page: req.Pagination.Page,
  288. Offset: req.Pagination.PageOffset,
  289. PageCount: req.Pagination.PageSize,
  290. ReturnType: "Map",
  291. ParamMaps: &model.FeedStatisticsParams{
  292. PastureId: fmt.Sprintf("%d", req.PastureId),
  293. StartTime: req.StartTime,
  294. StopTime: req.StartTime,
  295. Date: req.StartTime,
  296. FeedTName: req.FormulaTemplate,
  297. BarName: req.BarnName,
  298. CowClass: req.CattleCategoryName,
  299. Times: fmt.Sprintf("%d", req.ClassNumber),
  300. },
  301. }
  302. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  303. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  304. return nil, xerr.WithStack(err)
  305. }
  306. return response, nil
  307. }
  308. // FeedChartStatistics 饲喂效率图表分析
  309. func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) {
  310. body := &model.FeedChartParams{
  311. ParamMaps: &model.ParamMaps{
  312. PastureId: fmt.Sprintf("%d", req.PastureId),
  313. StartTime: req.StartTime,
  314. StopTime: req.StartTime,
  315. Status: req.Status,
  316. },
  317. }
  318. url, ok := model.UrlChart[req.ApiType]
  319. if !ok {
  320. return nil, xerr.Customf("错误的接口类型:%s", req.ApiType)
  321. }
  322. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  323. if err := s.PastureHttpClient(ctx, url, int64(req.PastureId), body, response); err != nil {
  324. return nil, xerr.WithStack(err)
  325. }
  326. return response, nil
  327. }
  328. // CowsAnalysis 饲喂效率-牛群评估
  329. func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) {
  330. body := &model.PastureCommonRequest{
  331. Name: req.ApiName,
  332. Page: req.Pagination.Page,
  333. Offset: req.Pagination.PageOffset,
  334. PageCount: req.Pagination.PageSize,
  335. ReturnType: "Map",
  336. ParamMaps: &model.MixFeedStatisticsParams{
  337. PastureId: fmt.Sprintf("%d", req.PastureId),
  338. StartTime: req.StartTime,
  339. StopTime: req.StartTime,
  340. },
  341. }
  342. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  343. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  344. return nil, xerr.WithStack(err)
  345. }
  346. return response, nil
  347. }
  348. // SearchAccuracyAggStatistics 准确性分析-汇总分析
  349. func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) {
  350. body := &model.FeedChartParams{
  351. ParamMaps: &model.AccuracyAggParams{
  352. PastureId: fmt.Sprintf("%d", req.PastureId),
  353. StartTime: req.StartTime,
  354. StopTime: req.EndTime,
  355. FName: req.Fname,
  356. Sort: req.Sort,
  357. Status: req.Status,
  358. Times: req.Times,
  359. Genre: req.Genre,
  360. IsDate: req.Isdate,
  361. Hlwc1: req.Hlwc1,
  362. Hlwc2: req.Hlwc2,
  363. Hlzq1: req.Hlzq1,
  364. Hlzq2: req.Hlzq2,
  365. Hlzql1: req.Hlzql1,
  366. Hlzql2: req.Hlzql2,
  367. Slwc1: req.Slwc1,
  368. Slwc2: req.Slwc2,
  369. Slzq1: req.Slzq1,
  370. Slzq2: req.Slzq2,
  371. Slzql1: req.Slzql1,
  372. Slzql2: req.Slzql2,
  373. Error: req.IsError,
  374. },
  375. }
  376. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  377. if err := s.PastureHttpClient(ctx, model.UrlSummary, int64(req.PastureId), body, response); err != nil {
  378. return nil, xerr.WithStack(err)
  379. }
  380. return response, nil
  381. }
  382. // SearchMixFeedStatistics 准确性分析-混料统计
  383. func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  384. times := ""
  385. if req.ClassNumber > 0 {
  386. times = fmt.Sprintf("%d", req.ClassNumber)
  387. }
  388. body := &model.PastureCommonRequest{
  389. Name: req.ApiName,
  390. Page: req.Pagination.Page,
  391. Offset: req.Pagination.PageOffset,
  392. PageCount: req.Pagination.PageSize,
  393. ReturnType: "Map",
  394. ParamMaps: &model.MixFeedStatisticsParams{
  395. PastureId: fmt.Sprintf("%d", req.PastureId),
  396. StartTime: req.StartTime,
  397. StopTime: req.StartTime,
  398. TmrTName: req.EquipmentName,
  399. ProjName: req.TrainNumber,
  400. Times: times,
  401. ButtonType: req.ButtonType,
  402. TempletName: req.FormulationName,
  403. Isuse: req.IsUse,
  404. Hlwc1: req.Hlwc1,
  405. Hlwc2: req.Hlwc2,
  406. Hlzq1: req.Hlzq1,
  407. Hlzq2: req.Hlzq2,
  408. Hlzql1: req.Hlzql1,
  409. Hlzql2: req.Hlzql2,
  410. Error: req.IsError,
  411. },
  412. }
  413. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  414. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  415. return nil, xerr.WithStack(err)
  416. }
  417. return response, nil
  418. }
  419. // SearchSprinkleStatistics 准确性分析-撒料统计
  420. func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) {
  421. times := ""
  422. if req.ClassNumber > 0 {
  423. times = fmt.Sprintf("%d", req.ClassNumber)
  424. }
  425. body := &model.PastureCommonRequest{
  426. Name: req.ApiName,
  427. Page: req.Pagination.Page,
  428. Offset: req.Pagination.PageOffset,
  429. PageCount: req.Pagination.PageSize,
  430. ReturnType: "Map",
  431. ParamMaps: &model.SprinkleStatisticsParams{
  432. PastureId: fmt.Sprintf("%d", req.PastureId),
  433. StartTime: req.StartTime,
  434. StopTime: req.StartTime,
  435. TmrTName: req.EquipmentName,
  436. ProjName: req.TrainNumber,
  437. Times: times,
  438. ButtonType: req.ButtonType,
  439. TempletName: req.FormulationName,
  440. Isuse: req.IsUse,
  441. Fname: req.BarnName,
  442. Slwc1: req.Slwc1,
  443. Slwc2: req.Slwc2,
  444. Slzq2: req.Slzq2,
  445. Slzq1: req.Slzq1,
  446. Slzql1: req.Slzql1,
  447. Slzql2: req.Slzql2,
  448. Error: req.IsError,
  449. },
  450. }
  451. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  452. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  453. return nil, xerr.WithStack(err)
  454. }
  455. return response, nil
  456. }
  457. // SearchProcessAnalysis 过程分析
  458. func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) {
  459. body := &model.PastureCommonRequest{
  460. Name: req.ApiName,
  461. Page: req.Pagination.Page,
  462. Offset: req.Pagination.PageOffset,
  463. PageCount: req.Pagination.PageSize,
  464. ReturnType: "Map",
  465. ParamMaps: &model.ProcessAnalysisParams{
  466. PastureId: fmt.Sprintf("%d", req.PastureId),
  467. StartTime: req.StartTime,
  468. StopTime: req.StartTime,
  469. TmrTName: req.TmrName,
  470. IsCompleted: "",
  471. LpPlanType: fmt.Sprintf("%d", req.PlanType),
  472. FClassId: req.MixFeedType,
  473. Hlzq1: req.Hlzq1,
  474. Hlzq2: req.Hlzq2,
  475. Hlwc1: req.Hlwc1,
  476. Hlwc2: req.Hlwc2,
  477. Slwc1: req.Slwc1,
  478. Slwc2: req.Slwc2,
  479. Slzq2: req.Slzq2,
  480. Slzq1: req.Slzq1,
  481. Error: req.ErrorRange,
  482. },
  483. }
  484. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  485. if err := s.PastureHttpClient(ctx, model.UrlProcess, int64(req.PastureId), body, response); err != nil {
  486. return nil, xerr.WithStack(err)
  487. }
  488. return response, nil
  489. }
  490. // GetDataByName 共同接口
  491. func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetDataByNameRequest) (*model.PastureCommonResponse, error) {
  492. body := &model.PastureCommonRequest{
  493. Name: req.ApiName,
  494. ParamMaps: &model.GetDataByNameParams{
  495. PastureId: fmt.Sprintf("%d", req.PastureId),
  496. StartTime: req.StartTime,
  497. StopTime: req.StartTime,
  498. },
  499. }
  500. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  501. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  502. return nil, xerr.WithStack(err)
  503. }
  504. return response, nil
  505. }
  506. // GetTrainNumber 获取班次
  507. func (s *StoreEntry) GetTrainNumber(ctx context.Context, req *operationPb.TrainNumberRequest) (*operationPb.TrainNumberResponse, error) {
  508. body := &model.PastureCommonRequest{
  509. Name: req.ApiName,
  510. Page: req.Pagination.Page,
  511. Offset: req.Pagination.PageOffset,
  512. PageCount: req.Pagination.PageSize,
  513. ReturnType: "Map",
  514. ParamMaps: &model.TrainNumberParams{
  515. PastureId: fmt.Sprintf("%d", req.PastureId),
  516. InfoRName: req.InfoName,
  517. },
  518. }
  519. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  520. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  521. return nil, xerr.WithStack(err)
  522. }
  523. result := &operationPb.TrainNumberResponse{
  524. Code: http.StatusOK,
  525. Msg: "ok",
  526. Data: &operationPb.TrainNumberData{List: make([]*operationPb.FormulaOptionEnum, 0)},
  527. }
  528. if response.Data.List == nil {
  529. return result, nil
  530. }
  531. b, _ := json.Marshal(response.Data.List)
  532. trainNumberList := make([]*model.TrainNumberList, 0)
  533. if err := json.Unmarshal(b, &trainNumberList); err != nil {
  534. return nil, xerr.WithStack(err)
  535. }
  536. formulaOption := make([]*operationPb.FormulaOptionEnum, 0)
  537. if len(trainNumberList) > 0 {
  538. infoValue := trainNumberList[0].InfoValue
  539. switch infoValue {
  540. case "1":
  541. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  542. Value: 1,
  543. Label: "第一班",
  544. })
  545. case "2":
  546. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  547. Value: 1,
  548. Label: "第一班",
  549. }, &operationPb.FormulaOptionEnum{
  550. Value: 2,
  551. Label: "第二班",
  552. })
  553. case "3":
  554. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  555. Value: 1,
  556. Label: "第一班",
  557. }, &operationPb.FormulaOptionEnum{
  558. Value: 2,
  559. Label: "第二班",
  560. }, &operationPb.FormulaOptionEnum{
  561. Value: 3,
  562. Label: "第三班",
  563. })
  564. }
  565. }
  566. result.Data.List = formulaOption
  567. return result, nil
  568. }
  569. func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) {
  570. dataList := &model.CommonValueRatio{
  571. MaxValue: "",
  572. MiddleValue: "",
  573. MinValue: "",
  574. PastureName: make([]string, 0),
  575. DateDay: make([]string, 0),
  576. DataList: make([][]string, 0),
  577. PastureIds: make([]int32, 0),
  578. }
  579. res := &model.SearchAnalysisAccuracyResponse{
  580. Code: http.StatusOK,
  581. Msg: "ok",
  582. Data: &model.AnalysisAccuracyData{
  583. Chart: &model.Chart{
  584. MixedFodderAccurateRatio: dataList,
  585. MixedFodderCorrectRatio: dataList,
  586. SprinkleFodderAccurateRatio: dataList,
  587. SprinkleFodderCorrectRatio: dataList,
  588. },
  589. Table: &model.Table{
  590. TitleList: make([]*model.TableList, 0),
  591. DataList: make([]*interface{}, 0),
  592. },
  593. },
  594. }
  595. res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
  596. Name: "title",
  597. Value: "牧场",
  598. })
  599. analysisAccuracy := make([]*model.OptionsAnalysisAccuracy, 0)
  600. pref := s.DB.Model(new(model.AnalysisAccuracy))
  601. if req.EndDate != "" && req.StartDate != "" {
  602. pref.Where("date_day BETWEEN ? AND ?", req.StartDate, req.EndDate)
  603. }
  604. if req.CattleParentCategoryId > 0 {
  605. pref.Where("cattle_parent_category_id = ?", req.CattleParentCategoryId)
  606. }
  607. if req.FeedFormulaId > 0 {
  608. pref.Where("feed_formula_id = ?", req.FeedFormulaId)
  609. }
  610. if len(req.PastureIds) > 0 {
  611. pref.Where("pasture_id IN ?", req.PastureIds)
  612. }
  613. if err := pref.Select("pasture_id,pasture_name,date_day,sum(iweight) as all_iweight,sum(lweight) as all_lweight,sum(oweight) as all_oweight,sum(actual_weight_minus) as all_actual_weight_minus,sum(allow_ratio) as all_allow_ratio,sum(alweight) as all_alweight").
  614. Group("pasture_id,pasture_name,date_day").Order("pasture_name,date_day").Find(&analysisAccuracy).Debug().Error; err != nil {
  615. return nil, xerr.WithStack(err)
  616. }
  617. mixedFodderAccurateRatio := make([]float64, 0)
  618. mapPastureName := make(map[string]bool, 0)
  619. mapDateDay := make(map[string]bool, 0)
  620. mapRatio := make(map[string]bool, 0)
  621. tableList := make([]*model.TableList, 0)
  622. for k, v := range analysisAccuracy {
  623. if _, ok := mapPastureName[v.PastureName]; !ok {
  624. res.Data.Chart.MixedFodderAccurateRatio.PastureName = append(res.Data.Chart.MixedFodderAccurateRatio.PastureName, v.PastureName)
  625. res.Data.Chart.MixedFodderAccurateRatio.PastureIds = append(res.Data.Chart.MixedFodderAccurateRatio.PastureIds, int32(v.PastureId))
  626. mapPastureName[v.PastureName] = true
  627. }
  628. dataDayFormat := strings.TrimRight(v.DateDay, "T00:00:00+08:00")
  629. if _, ok := mapDateDay[v.DateDay]; !ok {
  630. res.Data.Chart.MixedFodderAccurateRatio.DateDay = append(res.Data.Chart.MixedFodderAccurateRatio.DateDay, dataDayFormat)
  631. mapDateDay[v.DateDay] = true
  632. }
  633. valueRatio1 := float64(v.AllIweight/v.AllLweight) / 100.0
  634. if _, ok := mapRatio[v.DateDay]; !ok {
  635. valueRatio := make([]string, 0)
  636. for _, a := range analysisAccuracy {
  637. if a.DateDay == v.DateDay {
  638. valueRatio = append(valueRatio, strconv.FormatFloat(float64(a.AllIweight/a.AllLweight)/100.0, 'f', 2, 64))
  639. }
  640. }
  641. res.Data.Chart.MixedFodderAccurateRatio.DataList = append(res.Data.Chart.MixedFodderAccurateRatio.DataList, valueRatio)
  642. mapRatio[v.DateDay] = true
  643. tableList = append(tableList, &model.TableList{
  644. Name: fmt.Sprintf("date%d", k),
  645. Value: dataDayFormat,
  646. })
  647. }
  648. mixedFodderAccurateRatio = append(mixedFodderAccurateRatio, valueRatio1)
  649. }
  650. mixedFodderAccurateRatioMaxValue, mixedFodderAccurateRatioMiddleValue, mixedFodderAccurateRatioMinValue := calculateRatio(mixedFodderAccurateRatio)
  651. res.Data.Chart.MixedFodderAccurateRatio.MaxValue = strconv.FormatFloat(mixedFodderAccurateRatioMaxValue, 'f', 2, 64)
  652. res.Data.Chart.MixedFodderAccurateRatio.MiddleValue = strconv.FormatFloat(mixedFodderAccurateRatioMiddleValue, 'f', 2, 64)
  653. res.Data.Chart.MixedFodderAccurateRatio.MinValue = strconv.FormatFloat(mixedFodderAccurateRatioMinValue, 'f', 2, 64)
  654. res.Data.Table.TitleList = append(res.Data.Table.TitleList, tableList...)
  655. return res, nil
  656. }
  657. func calculateRatio(res []float64) (float64, float64, float64) {
  658. var maxValue, middleValue, minValue, allValue float64 = 0, 0, 0, 0
  659. for _, v := range res {
  660. if v > maxValue {
  661. maxValue = v
  662. }
  663. if v <= maxValue {
  664. minValue = v
  665. }
  666. allValue += v
  667. }
  668. middleValue = allValue / float64(len(res))
  669. return maxValue, middleValue, minValue
  670. }