post_data.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package module
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "tmr-watch/conf/setting"
  13. "tmr-watch/http/handle/restful"
  14. "tmr-watch/models"
  15. "tmr-watch/pkg/logging"
  16. "github.com/Anderson-Lu/gofasion/gofasion"
  17. "github.com/gin-gonic/gin"
  18. "github.com/xormplus/xorm"
  19. )
  20. const (
  21. ClassDataSyncURl = "group/class/sync"
  22. ClassDataDeleteURl = "group/class/sync"
  23. )
  24. func PostDataByName(c *gin.Context, fashion *gofasion.Fasion) (interface{}, error) {
  25. sqlnamestr := fashion.Get("name").ValueStr()
  26. s_params := make([]interface{}, 0)
  27. tx := restful.Engine.NewSession()
  28. defer tx.Close()
  29. err := tx.Begin()
  30. defer func() {
  31. switch {
  32. case err != nil:
  33. if tx != nil {
  34. tx.Rollback()
  35. }
  36. default:
  37. if tx != nil {
  38. err = tx.Commit()
  39. }
  40. }
  41. if tx != nil {
  42. tx.Close()
  43. }
  44. }()
  45. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  46. if sql != "" {
  47. if fashion.HasKey("parammaps") {
  48. parammaps := fashion.Get("parammaps")
  49. logging.Info("PostDataByName ", sqlnamestr, parammaps.ValueStr())
  50. paramslist := strings.Split(p, ",")
  51. if len(paramslist) > 0 && p != "" {
  52. for _, value := range paramslist {
  53. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  54. if tempv, exists := c.Get("jwt_username"); exists {
  55. s_params = append(s_params, tempv.(string))
  56. } else {
  57. s_params = append(s_params, "")
  58. }
  59. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  60. ids, err := setting.SnowIds.NextId()
  61. if err != nil {
  62. ids = time.Now().UnixNano()
  63. logging.Info("create SnowIds err", err)
  64. }
  65. s_params = append(s_params, ids)
  66. } else {
  67. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  68. }
  69. }
  70. }
  71. } else if fashion.HasKey("params") {
  72. params := fashion.Get("params").Array()
  73. logging.Info("PostDataByName ", sqlnamestr, params)
  74. for _, v_params := range params {
  75. s_params = append(s_params, v_params.ValueStr())
  76. }
  77. }
  78. queryData, err := ExecDataBySqlT(sql, s_params, tx)
  79. if err != nil {
  80. logging.Error("PostDataByName err: ", err)
  81. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  82. if (sqlnamestr == "copybarfeedremain" || sqlnamestr == "copyFtdry" || sqlnamestr == "copyPennsieve") &&
  83. strings.Contains(err.Error(), "Duplicate entry") {
  84. return nil, errors.New("同一栏舍,同一班次不可重复录入")
  85. } else if (sqlnamestr == "copybarmilk" || sqlnamestr == "copyBodyscore" || sqlnamestr == "copyDungsieve" || sqlnamestr == "copyDungscore") &&
  86. strings.Contains(err.Error(), "Duplicate entry") {
  87. return nil, errors.New("同一栏舍不可重复录入")
  88. } else {
  89. return nil, err
  90. }
  91. } else {
  92. return queryData, nil
  93. }
  94. }
  95. return nil, nil
  96. }
  97. func ParamsMarshal(keyWord string, fashion *gofasion.Fasion) (*models.ClassRequest, error) {
  98. params := fashion.Get("parammaps")
  99. classRequest := &models.ClassRequest{}
  100. if keyWord != "cow" {
  101. if err := json.Unmarshal([]byte(params.Json()), classRequest); err != nil {
  102. logging.Error("GroupCategorySync", "Unmarshal", err)
  103. return nil, err
  104. }
  105. } else {
  106. classCowRequest := &models.ClassCowRequest{}
  107. if err := json.Unmarshal([]byte(params.Json()), classCowRequest); err != nil {
  108. logging.Error("GroupCategorySync", "Unmarshal", err)
  109. return nil, err
  110. }
  111. classRequest.Enable = classCowRequest.Enable
  112. classRequest.PastureId = classCowRequest.PastureId
  113. classRequest.BigFeedClassId = classCowRequest.PastureId
  114. classRequest.BigFeedClassName = classCowRequest.ParentName
  115. classRequest.FcCode = classCowRequest.ClassCode
  116. classRequest.FcName = classCowRequest.ClassName
  117. }
  118. return classRequest, nil
  119. }
  120. func GroupCategorySync(keyWord string, fasion *gofasion.Fasion) error {
  121. classRequest, err := ParamsMarshal(keyWord, fasion)
  122. if err != nil {
  123. return err
  124. }
  125. parentId, _ := strconv.Atoi(classRequest.BigFeedClassId)
  126. pastureId, _ := strconv.Atoi(classRequest.PastureId)
  127. body := &models.CategoryRequest{
  128. KeyWord: keyWord,
  129. PastureId: int32(pastureId),
  130. ParentId: int32(parentId),
  131. ParentName: classRequest.BigFeedClassName,
  132. Name: classRequest.FcName,
  133. Number: classRequest.FcCode,
  134. IsShow: classRequest.Enable,
  135. }
  136. URL := fmt.Sprintf("%s/%s", GetGroupDomain(int64(pastureId)), ClassDataSyncURl)
  137. result, err := DoPost(URL, body)
  138. if err != nil {
  139. return err
  140. }
  141. response := &models.GroupCommonResponse{}
  142. if err = json.Unmarshal(result, response); err != nil {
  143. return err
  144. }
  145. if response.Code != http.StatusOK {
  146. return fmt.Errorf("msg: %s", response.Msg)
  147. }
  148. return nil
  149. }
  150. func GroupCategoryDelete(keyWord string, fashion *gofasion.Fasion) error {
  151. params := fashion.Get("parammaps")
  152. classRequest := &models.ClassDeleteRequest{}
  153. if err := json.Unmarshal([]byte(params.Json()), classRequest); err != nil {
  154. logging.Error("GroupCategorySync", "Unmarshal", err)
  155. return err
  156. }
  157. pastureId, _ := strconv.Atoi(classRequest.PastureId)
  158. dataId, _ := strconv.Atoi(classRequest.Id)
  159. body := &models.GroupCategoryDeleteRequest{
  160. KeyWord: keyWord,
  161. PastureId: int32(pastureId),
  162. DataId: int32(dataId),
  163. }
  164. URL := fmt.Sprintf("%s/%s", GetGroupDomain(int64(pastureId)), ClassDataDeleteURl)
  165. result, err := DoPost(URL, body)
  166. if err != nil {
  167. return err
  168. }
  169. response := &models.GroupCommonResponse{}
  170. if err = json.Unmarshal(result, response); err != nil {
  171. return err
  172. }
  173. if response.Code != http.StatusOK {
  174. return fmt.Errorf("msg: %s", response.Msg)
  175. }
  176. return nil
  177. }
  178. func GetGroupDomain(pastureId int64) string {
  179. res := &models.SysOpt{}
  180. if _, err := restful.Engine.Table(new(models.SysOpt).TableName()).
  181. Where("pastureid = ?", pastureId).And("inforname = ?", "domain").Get(res); err != nil {
  182. logging.Error("GetGroupDomain", err)
  183. return ""
  184. }
  185. return res.InforValue
  186. }
  187. func ExecDataBySqlT(sqlstr string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  188. queryData, err := restful.ExecQueryT(sqlstr, params, tx)
  189. return queryData, err
  190. }
  191. type GroupClient struct {
  192. Detail *models.Pasture
  193. authClient *http.Client
  194. }
  195. func NewGroupClient(detail *models.Pasture) *GroupClient {
  196. return &GroupClient{
  197. Detail: detail,
  198. authClient: &http.Client{
  199. Timeout: time.Duration(5) * time.Second,
  200. },
  201. }
  202. }
  203. func doRequest(req *http.Request) ([]byte, error) {
  204. groupClient := NewGroupClient(nil)
  205. resp, err := groupClient.authClient.Do(req)
  206. if err != nil {
  207. return nil, err
  208. }
  209. b, err := ioutil.ReadAll(resp.Body)
  210. if err != nil {
  211. return nil, err
  212. }
  213. if resp.StatusCode != http.StatusOK {
  214. if len(b) > 0 {
  215. return nil, fmt.Errorf("err:%v,body:%s", err, string(b))
  216. } else {
  217. return nil, fmt.Errorf("err:%v", err)
  218. }
  219. }
  220. return b, nil
  221. }
  222. func DoGet(url string) ([]byte, error) {
  223. req, err := http.NewRequest(http.MethodGet, url, nil)
  224. if err != nil {
  225. return nil, err
  226. }
  227. req.Header.Add("Accept", "application/json")
  228. req.Header.Add("Content-Type", "application/json")
  229. return doRequest(req)
  230. }
  231. func DoPost(url string, body interface{}) ([]byte, error) {
  232. b, err := json.Marshal(body)
  233. if err != nil {
  234. return nil, err
  235. }
  236. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  237. if err != nil {
  238. return nil, err
  239. }
  240. req.Header.Add("Accept", "application/json")
  241. req.Header.Add("Content-Type", "application/json")
  242. return doRequest(req)
  243. }