group_pasture.go 8.4 KB

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