group_pasture.go 8.9 KB

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