group_pasture.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package model
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "kpt-tmr-group/pkg/logger/zaplog"
  7. "kpt-tmr-group/pkg/tool"
  8. "kpt-tmr-group/pkg/xerr"
  9. operationPb "kpt-tmr-group/proto/go/backend/operation"
  10. "net/http"
  11. "strings"
  12. "time"
  13. "go.uber.org/zap"
  14. )
  15. type GroupPasture struct {
  16. Id int64 `json:"id,omitempty"`
  17. Name string `json:"name,omitempty"`
  18. PastureId int64 `json:"pasture_id"`
  19. Account string `json:"account,omitempty"`
  20. Password string `json:"password"`
  21. ManagerUser string `json:"manager_user"`
  22. ManagerPhone string `json:"manager_phone"`
  23. Domain string `json:"domain"`
  24. Extranet string `json:"extranet"`
  25. Intranet string `json:"intranet"`
  26. IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"`
  27. IsDelete operationPb.IsShow_Kind `json:"is_delete,omitempty"`
  28. IsDistribution operationPb.IsShow_Kind `json:"is_distribution"`
  29. Address string `json:"address"`
  30. CreatedAt int64 `json:"created_at,omitempty"`
  31. UpdatedAt int64 `json:"updated_at,omitempty"`
  32. }
  33. func (g *GroupPasture) TableName() string {
  34. return "group_pasture"
  35. }
  36. // GetPastureId 获取牧场id
  37. func (g *GroupPasture) GetPastureId() int64 {
  38. if g.PastureId > 0 {
  39. return g.PastureId
  40. }
  41. return g.Id
  42. }
  43. const (
  44. InitManagerPassword = "123456"
  45. UrlDataByName = "authdata/GetDataByName"
  46. UrlReportForm = "authdata/GetReportform"
  47. UrlSummary = "authdata/summary"
  48. UrlProcess = "authdata/processAnalysist"
  49. )
  50. const (
  51. FeedFormulaDistributeUrl = "pasture/feed_formula/distribute"
  52. FeedFormulaIsModifyUrl = "pasture/feed_formula/is_modify"
  53. DashboardAccuracyUrl = "pasture/dashboard/accuracy_data"
  54. DashboardExecTimeUrl = "pasture/dashboard/process_analysis"
  55. DashboardSprinkleFeedTimeUrl = "pasture/dashboard/sprinkle_statistics"
  56. PastureAccountDistributionURl = "pasture/account/distribute"
  57. ForageCategoryDistributionURl = "pasture/forage_category/distribute"
  58. CattleCategoryDistributionURl = "pasture/cattle_category/distribute"
  59. CattleCategoryDeleteURl = "pasture/cattle_category/delete"
  60. ForageCategoryDeleteURl = "pasture/cattle_category/delete"
  61. )
  62. var (
  63. UrlChart = map[string]string{
  64. "mr": "authdata/chart/feedEffMR", // 饲喂效率-效率统计 mr 泌乳牛干物质采食量
  65. "sl": "authdata/chart/feedEffSL", // 饲喂效率-效率统计 sl 牛栏剩料率
  66. "hl": "authdata/chart/feedEffHL", // 饲喂效率-效率统计 hl 混料时间统计
  67. "zh": "authdata/chart/feedEffZH", // 饲喂效率-效率统计 zh 转化率
  68. "cbft": "authdata/chart/feedEffCBFT", // 饲喂效率-效率统计 cbft 成本分析
  69. "jh": "authdata/chart/accuracyAllJH", // 准确性分析-汇总统计-计划统计
  70. "ft": "authdata/chart/accuracyAllFT", // 准确性分析-汇总统计-配方准确率
  71. "nq": "authdata/chart/accuracyAllNQ", // 准确性分析-汇总统计-牛群准确率
  72. "cc": "authdata/chart/accuracyAllCC", // 准确性分析-汇总统计-车辆准确率(重量)
  73. "allhl": "authdata/chart/accuracyAllHL", // 准确性分析-汇总统计-混料统计
  74. "qx": "authdata/chart/accuracyAllQX", // 准确性分析-汇总统计-混料计划取消次数
  75. "ls": "authdata/chart/accuracyAllLS", // 准确性分析-汇总统计-栏舍撒料时间统计
  76. }
  77. )
  78. type PastureTokenRequest struct {
  79. UserName string `json:"username"`
  80. Password string `json:"password"`
  81. }
  82. type PastureTokenResponse struct {
  83. Code int32 `json:"code"`
  84. Msg string `json:"msg"`
  85. Data *PastureTokenData `json:"data"`
  86. }
  87. type PastureTokenData struct {
  88. Token string `json:"token"`
  89. }
  90. func NewGroupPasture(req *operationPb.AddPastureRequest) *GroupPasture {
  91. groupPasture := &GroupPasture{
  92. Name: req.Name,
  93. PastureId: int64(req.PastureId),
  94. Account: req.Account,
  95. Password: tool.Md5String(InitManagerPassword),
  96. ManagerUser: req.ManagerUser,
  97. ManagerPhone: req.ManagerPhone,
  98. IsShow: operationPb.IsShow_OK,
  99. IsDelete: operationPb.IsShow_OK,
  100. Address: req.Address,
  101. Domain: req.Domain,
  102. IsDistribution: operationPb.IsShow_NO,
  103. }
  104. return groupPasture
  105. }
  106. type PastureClient struct {
  107. Token string `json:"token"`
  108. Detail *GroupPasture `json:"detail"`
  109. authClient *http.Client
  110. }
  111. func NewPastureClient(g *GroupPasture) *PastureClient {
  112. return &PastureClient{
  113. Detail: g,
  114. authClient: &http.Client{
  115. Timeout: time.Duration(5) * time.Second,
  116. },
  117. }
  118. }
  119. func (p *PastureClient) doRequest(req *http.Request) ([]byte, error) {
  120. resp, err := p.authClient.Do(req)
  121. if err != nil {
  122. zaplog.Error("PastureClient", zap.Any("authClient.Do", err))
  123. return nil, xerr.WithStack(err)
  124. }
  125. b, err := ioutil.ReadAll(resp.Body)
  126. if err != nil {
  127. zaplog.Error("PastureClient", zap.Any("ioutil.ReadAll", err))
  128. return nil, xerr.WithStack(err)
  129. }
  130. if resp.StatusCode != http.StatusOK {
  131. if len(b) > 0 {
  132. return nil, xerr.Customf("err:%v,body:%s", err, string(b))
  133. } else {
  134. return nil, xerr.Customf("err:%v", err)
  135. }
  136. }
  137. return b, nil
  138. }
  139. func (p *PastureClient) DoGet(url string) ([]byte, error) {
  140. // 获取token
  141. if err := p.GetToken(); err != nil {
  142. zaplog.Error("PastureClient", zap.Any("DoGet GetToken Err", err), zap.Any("detail", p.Detail))
  143. return nil, xerr.WithStack(err)
  144. }
  145. req, err := http.NewRequest(http.MethodGet, url, nil)
  146. if err != nil {
  147. zaplog.Error("PastureClient", zap.Any("DoGet", err))
  148. return nil, err
  149. }
  150. req.Header.Add("Accept", "application/json")
  151. req.Header.Add("token", p.Token)
  152. req.Header.Add("Content-Type", "application/json")
  153. return p.doRequest(req)
  154. }
  155. func (p *PastureClient) DoPost(url string, body interface{}) ([]byte, error) {
  156. b, err := json.Marshal(body)
  157. if err != nil {
  158. zaplog.Error("PastureClient", zap.Any("DoPost-Marshal", err))
  159. return nil, xerr.WithStack(err)
  160. }
  161. // 获取token
  162. if !strings.Contains(url, PastureAccountDistributionURl) {
  163. if err = p.GetToken(); err != nil {
  164. zaplog.Error("PastureClient", zap.Any("DoPost GetToken Err", err), zap.Any("detail", p.Detail))
  165. return nil, xerr.WithStack(err)
  166. }
  167. }
  168. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  169. if err != nil {
  170. zaplog.Error("PastureClient", zap.Any("NewRequest", err))
  171. return nil, xerr.WithStack(err)
  172. }
  173. req.Header.Add("Accept", "application/json")
  174. req.Header.Add("token", p.Token)
  175. req.Header.Add("Content-Type", "application/json")
  176. return p.doRequest(req)
  177. }
  178. func (p *PastureClient) GetToken() error {
  179. /*url := fmt.Sprintf("%s/%s", p.Detail.Domain, "auth")
  180. body := &PastureTokenRequest{
  181. UserName: p.Detail.Account,
  182. Password: p.Detail.Password,
  183. }
  184. b, err := json.Marshal(body)
  185. if err != nil {
  186. return xerr.WithStack(err)
  187. }
  188. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  189. if err != nil {
  190. zaplog.Error("PastureClient", zap.Any("GetToken", err))
  191. return xerr.WithStack(err)
  192. }
  193. req.Header.Add("Accept", "application/json")
  194. req.Header.Add("Content-Type", "application/json")
  195. result, err := p.doRequest(req)
  196. if err != nil {
  197. zaplog.Error("PastureClient", zap.Any("GetToken-doRequest", err))
  198. return xerr.WithStack(err)
  199. }
  200. response := &PastureTokenResponse{}
  201. if err = json.Unmarshal(result, response); err != nil {
  202. zaplog.Error("PastureClient", zap.String("GetToken", string(result)))
  203. return xerr.WithStack(err)
  204. }
  205. if response.Code != http.StatusOK || response.Data == nil || response.Data.Token == "" {
  206. zaplog.Error("PastureClient", zap.Any("response", response))
  207. return xerr.Customf("获取牧场端token失败 Err:%s", err)
  208. }
  209. p.Token = response.Data.Token*/
  210. p.Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiJlMTBhZGMzOTQ5YmE1OWFiYmU1NmUwNTdmMjBmODgzZSIsImV4cCI6MTY4OTI2ODIxOCwiaXNzIjoiaHR0cHM6Ly9naXRodWIuY29tL2twdHl1bi9nby1hZG1pbi8ifQ.Q2ztGs66HbIz4IpTy9pH7bZVsJEzVQBwpjJSLBzKn4c"
  211. return nil
  212. }
  213. type GroupPastureSlice []*GroupPasture
  214. func (g GroupPastureSlice) ToPB() []*operationPb.AddPastureRequest {
  215. res := make([]*operationPb.AddPastureRequest, len(g))
  216. for i, v := range g {
  217. res[i] = &operationPb.AddPastureRequest{
  218. Id: int32(v.Id),
  219. Name: v.Name,
  220. Account: v.Account,
  221. ManagerUser: v.ManagerUser,
  222. ManagerPhone: v.ManagerPhone,
  223. Address: v.Address,
  224. Domain: v.Domain,
  225. IsShow: v.IsShow,
  226. CreatedAt: int32(v.CreatedAt),
  227. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
  228. }
  229. }
  230. return res
  231. }
  232. func (g *GroupPasture) ToPb() *operationPb.AddPastureRequest {
  233. return &operationPb.AddPastureRequest{
  234. Id: int32(g.Id),
  235. Name: g.Name,
  236. ManagerUser: g.ManagerUser,
  237. ManagerPhone: g.ManagerPhone,
  238. Address: g.Address,
  239. IsShow: g.IsShow,
  240. CreatedAt: int32(g.CreatedAt),
  241. }
  242. }