group_pasture.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. 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. return p.doRequest(req)
  161. }
  162. func (p *PastureClient) DoPost(url string, body interface{}) ([]byte, error) {
  163. b, err := json.Marshal(body)
  164. if err != nil {
  165. zaplog.Error("PastureClient", zap.Any("DoPost-Marshal", err))
  166. return nil, xerr.WithStack(err)
  167. }
  168. // 获取token
  169. if !strings.Contains(url, PastureAccountDistributionURl) {
  170. if err = p.GetToken(); err != nil {
  171. zaplog.Error("PastureClient", zap.Any("DoPost GetToken Err", err), zap.Any("detail", p.Detail))
  172. return nil, xerr.WithStack(err)
  173. }
  174. }
  175. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  176. if err != nil {
  177. zaplog.Error("PastureClient", zap.Any("NewRequest", err))
  178. return nil, xerr.WithStack(err)
  179. }
  180. req.Header.Add("Accept", "application/json")
  181. req.Header.Add("token", p.Token)
  182. req.Header.Add("Content-Type", "application/json")
  183. return p.doRequest(req)
  184. }
  185. func (p *PastureClient) GetToken() error {
  186. url := fmt.Sprintf("%s/%s", p.Detail.Domain, "auth")
  187. body := &PastureTokenRequest{
  188. UserName: p.Detail.Account,
  189. Password: p.Detail.Password,
  190. }
  191. b, err := json.Marshal(body)
  192. if err != nil {
  193. return xerr.WithStack(err)
  194. }
  195. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  196. if err != nil {
  197. zaplog.Error("PastureClient", zap.Any("GetToken", err))
  198. return xerr.WithStack(err)
  199. }
  200. req.Header.Add("Accept", "application/json")
  201. req.Header.Add("Content-Type", "application/json")
  202. result, err := p.doRequest(req)
  203. if err != nil {
  204. zaplog.Error("PastureClient", zap.Any("GetToken-doRequest", err))
  205. return xerr.WithStack(err)
  206. }
  207. response := &PastureTokenResponse{}
  208. if err = json.Unmarshal(result, response); err != nil {
  209. zaplog.Error("PastureClient", zap.String("GetToken", string(result)))
  210. return xerr.WithStack(err)
  211. }
  212. if response.Code != http.StatusOK || response.Data == nil || response.Data.Token == "" {
  213. zaplog.Error("PastureClient", zap.Any("response", response))
  214. return xerr.Customf("获取牧场端token失败 Err:%s", err)
  215. }
  216. p.Token = response.Data.Token
  217. return nil
  218. }
  219. type GroupPastureSlice []*GroupPasture
  220. func (g GroupPastureSlice) ToPB() []*operationPb.AddPastureRequest {
  221. res := make([]*operationPb.AddPastureRequest, len(g))
  222. for i, v := range g {
  223. res[i] = &operationPb.AddPastureRequest{
  224. Id: int32(v.Id),
  225. Name: v.Name,
  226. Account: v.Account,
  227. ManagerUser: v.ManagerUser,
  228. ManagerPhone: v.ManagerPhone,
  229. Address: v.Address,
  230. Domain: v.Domain,
  231. IsShow: v.IsShow,
  232. CreatedAt: int32(v.CreatedAt),
  233. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
  234. }
  235. }
  236. return res
  237. }
  238. func (g *GroupPasture) ToPb() *operationPb.AddPastureRequest {
  239. return &operationPb.AddPastureRequest{
  240. Id: int32(g.Id),
  241. Name: g.Name,
  242. ManagerUser: g.ManagerUser,
  243. ManagerPhone: g.ManagerPhone,
  244. Address: g.Address,
  245. IsShow: g.IsShow,
  246. CreatedAt: int32(g.CreatedAt),
  247. }
  248. }