group_pasture.go 8.0 KB

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