group_pasture.go 8.8 KB

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