group_pasture.go 8.7 KB

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