group_pasture.go 8.9 KB

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