group_pasture.go 8.1 KB

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