group_pasture.go 8.2 KB

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