group_pasture.go 8.6 KB

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