group_pasture.go 7.4 KB

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