group_pasture.go 9.3 KB

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