group_pasture.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package model
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  7. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  8. "gitee.com/xuyiping_admin/pkg/tool"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. "io/ioutil"
  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. 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. b, err := ioutil.ReadAll(resp.Body)
  133. if err != nil {
  134. zaplog.Error("PastureClient", zap.Any("ioutil.ReadAll", err))
  135. return nil, xerr.WithStack(err)
  136. }
  137. if resp.StatusCode != http.StatusOK {
  138. if len(b) > 0 {
  139. return nil, xerr.Customf("err:%v,body:%s", err, string(b))
  140. } else {
  141. return nil, xerr.Customf("err:%v", err)
  142. }
  143. }
  144. return b, nil
  145. }
  146. func (p *PastureClient) DoGet(url string) ([]byte, error) {
  147. // 获取token
  148. if err := p.GetToken(); err != nil {
  149. zaplog.Error("PastureClient", zap.Any("DoGet GetToken Err", err), zap.Any("detail", p.Detail))
  150. return nil, xerr.WithStack(err)
  151. }
  152. req, err := http.NewRequest(http.MethodGet, url, nil)
  153. if err != nil {
  154. zaplog.Error("PastureClient", zap.Any("DoGet", err))
  155. return nil, err
  156. }
  157. req.Header.Add("Accept", "application/json")
  158. req.Header.Add("token", p.Token)
  159. req.Header.Add("Content-Type", "application/json")
  160. req.Header.Add("pasture_id", fmt.Sprintf("%d", p.Detail.PastureId))
  161. return p.doRequest(req)
  162. }
  163. func (p *PastureClient) DoPost(url string, body interface{}) ([]byte, error) {
  164. b, err := json.Marshal(body)
  165. if err != nil {
  166. zaplog.Error("PastureClient", zap.Any("DoPost-Marshal", err))
  167. return nil, xerr.WithStack(err)
  168. }
  169. // 获取token
  170. if !strings.Contains(url, PastureAccountDistributionURl) {
  171. if err = p.GetToken(); err != nil {
  172. zaplog.Error("PastureClient", zap.Any("DoPost GetToken Err", err), zap.Any("detail", p.Detail))
  173. return nil, xerr.WithStack(err)
  174. }
  175. }
  176. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  177. if err != nil {
  178. zaplog.Error("PastureClient", zap.Any("NewRequest", err))
  179. return nil, xerr.WithStack(err)
  180. }
  181. req.Header.Add("Accept", "application/json")
  182. req.Header.Add("token", p.Token)
  183. req.Header.Add("Content-Type", "application/json")
  184. req.Header.Add("pasture_id", fmt.Sprintf("%d", p.Detail.PastureId))
  185. return p.doRequest(req)
  186. }
  187. func (p *PastureClient) GetToken() error {
  188. url := fmt.Sprintf("%s/%s", p.Detail.Domain, "auth")
  189. body := &PastureTokenRequest{
  190. UserName: p.Detail.Account,
  191. Password: p.Detail.Password,
  192. }
  193. b, err := json.Marshal(body)
  194. if err != nil {
  195. return xerr.WithStack(err)
  196. }
  197. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  198. if err != nil {
  199. zaplog.Error("PastureClient", zap.Any("GetToken", err))
  200. return xerr.WithStack(err)
  201. }
  202. req.Header.Add("Accept", "application/json")
  203. req.Header.Add("Content-Type", "application/json")
  204. result, err := p.doRequest(req)
  205. if err != nil {
  206. zaplog.Error("PastureClient", zap.Any("GetToken-doRequest", err))
  207. return xerr.WithStack(err)
  208. }
  209. response := &PastureTokenResponse{}
  210. if err = json.Unmarshal(result, response); err != nil {
  211. zaplog.Error("PastureClient", zap.String("GetToken", string(result)))
  212. return xerr.WithStack(err)
  213. }
  214. if response.Code != http.StatusOK || response.Data == nil || response.Data.Token == "" {
  215. zaplog.Error("PastureClient", zap.Any("response", response))
  216. return xerr.Customf("获取牧场端token失败 Err:%s", err)
  217. }
  218. p.Token = response.Data.Token
  219. return nil
  220. }
  221. type GroupPastureSlice []*GroupPasture
  222. func (g GroupPastureSlice) ToPB() []*operationPb.AddPastureRequest {
  223. res := make([]*operationPb.AddPastureRequest, len(g))
  224. for i, v := range g {
  225. res[i] = &operationPb.AddPastureRequest{
  226. Id: int32(v.Id),
  227. Name: v.Name,
  228. Account: v.Account,
  229. ManagerUser: v.ManagerUser,
  230. ManagerPhone: v.ManagerPhone,
  231. Address: v.Address,
  232. Domain: v.Domain,
  233. IsShow: v.IsShow,
  234. CreatedAt: int32(v.CreatedAt),
  235. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
  236. }
  237. }
  238. return res
  239. }
  240. func (g *GroupPasture) ToPb() *operationPb.AddPastureRequest {
  241. return &operationPb.AddPastureRequest{
  242. Id: int32(g.Id),
  243. Name: g.Name,
  244. ManagerUser: g.ManagerUser,
  245. ManagerPhone: g.ManagerPhone,
  246. Address: g.Address,
  247. IsShow: g.IsShow,
  248. CreatedAt: int32(g.CreatedAt),
  249. }
  250. }