group_pasture.go 9.3 KB

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