group_pasture.go 8.7 KB

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