scheduled.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "../../pkg/app"
  10. "../../pkg/e"
  11. "../../routers/restful"
  12. "github.com/Anderson-Lu/gofasion/gofasion"
  13. "github.com/astaxie/beego/logs"
  14. "github.com/gin-gonic/gin"
  15. )
  16. func GetRemind(c *gin.Context) {
  17. appG := app.Gin{C: c}
  18. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  19. fsion := gofasion.NewFasion(string(dataByte))
  20. parammaps := fsion.Get("parammaps")
  21. pastureid := parammaps.Get("pastureid").ValueInt64()
  22. // pastureid := 0
  23. tx := restful.Engine.NewSession()
  24. defer tx.Close()
  25. dataList, err := tx.SQL("select * from remind ").Query().List()
  26. if err != nil {
  27. logs.Error(err)
  28. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  29. return
  30. }
  31. resp, err := http.Get(fmt.Sprintf("http://tmrwatch.cn/notice/msgtype?sys_name=crm&pasture_id=%d", pastureid))
  32. if err != nil {
  33. logs.Error(err)
  34. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  35. return
  36. }
  37. defer resp.Body.Close()
  38. buf := bytes.NewBuffer(make([]byte, 0, 512))
  39. buf.ReadFrom(resp.Body)
  40. fmt.Println(string(buf.Bytes()))
  41. respMap := make(map[string]interface{}, 0)
  42. err = json.Unmarshal(buf.Bytes(), &respMap)
  43. if err != nil {
  44. logs.Error(err)
  45. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  46. return
  47. }
  48. for _, data := range dataList {
  49. r := make(map[string]interface{}, 0)
  50. r["sys_name"] = ""
  51. r["service_id"] = ""
  52. r["pasture_id"] = 0
  53. r["type_name"] = ""
  54. r["remind_type_id"] = ""
  55. r["remind_type"] = ""
  56. r["push_date"] = ""
  57. r["push_time"] = ""
  58. r["interval_time"] = ""
  59. r["push_limit"] = ""
  60. r["template_id"] = ""
  61. r["roles_id"] = ""
  62. r["users_id"] = ""
  63. r["date_type"] = ""
  64. r["cycle_type"] = ""
  65. data["data"] = r
  66. for _, resp := range respMap["data"].([]interface{}) {
  67. resp1 := resp.(map[string]interface{})
  68. if data["id"].(int64) == int64(resp1["service_id"].(float64)) {
  69. if resp1["push_time"] == nil {
  70. resp1["push_time"] = ""
  71. }
  72. data["data"] = resp1
  73. break
  74. }
  75. }
  76. }
  77. appG.Response(http.StatusOK, e.SUCCESS, dataList)
  78. }
  79. type NoticeMsgtype struct {
  80. SysName string `json:"sys_name"`
  81. PastureId int64 `json:"pasture_id"`
  82. ServiceId int64 `json:"service_id"`
  83. TypeName string `json:"type_name"`
  84. RemindType int64 `json:"remind_type"`
  85. PushDate int64 `json:"push_date"`
  86. PushTime string `json:"push_time"`
  87. IntervalTime int64 `json:"interval_time"`
  88. PushLimit int64 `json:"push_limit"`
  89. TemplateId string `json:"template_id"`
  90. UsersId string `json:"users_id"`
  91. DateType int64 `json:"date_type"`
  92. CycleType int64 `json:"cycle_type"`
  93. DelayTime int64 `json:"delay_time"`
  94. Status int64 `json:"status"`
  95. }
  96. func UpdateRemind(c *gin.Context) {
  97. appG := app.Gin{C: c}
  98. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  99. parammaps := gofasion.NewFasion(string(dataByte))
  100. id := parammaps.Get("id").ValueInt64()
  101. noticeMsgtype := new(NoticeMsgtype)
  102. noticeMsgtype.SysName = parammaps.Get("sys_name").ValueStr()
  103. noticeMsgtype.ServiceId = id
  104. // noticeMsgtype.PastureId = parammaps.Get("pastureid").ValueInt64()
  105. noticeMsgtype.TypeName = parammaps.Get("type_name").ValueStr()
  106. noticeMsgtype.RemindType = parammaps.Get("remind_type").ValueInt64()
  107. noticeMsgtype.PushDate = parammaps.Get("push_date").ValueInt64()
  108. noticeMsgtype.PushTime = parammaps.Get("push_time").ValueStr()
  109. noticeMsgtype.IntervalTime = parammaps.Get("interval_time").ValueInt64()
  110. noticeMsgtype.PushLimit = parammaps.Get("push_limit").ValueInt64()
  111. noticeMsgtype.TemplateId = parammaps.Get("template_id").ValueStr()
  112. noticeMsgtype.DateType = parammaps.Get("date_type").ValueInt64()
  113. noticeMsgtype.CycleType = parammaps.Get("cycle_type").ValueInt64()
  114. noticeMsgtype.DelayTime = parammaps.Get("delay_time").ValueInt64()
  115. noticeMsgtype.Status = parammaps.Get("status").ValueInt64()
  116. //TODO
  117. noticeMsgtype.SysName = "crm"
  118. userList := parammaps.Get("user").Array()
  119. var userstr []string
  120. for _, user := range userList {
  121. userstr = append(userstr, user.ValueStr())
  122. }
  123. tx := restful.Engine.NewSession()
  124. defer tx.Close()
  125. _, err := tx.SQL(` update remind set user = ? where id = ? `, strings.Join(userstr, ","), id).Execute()
  126. if err != nil {
  127. logs.Error(err)
  128. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  129. return
  130. }
  131. noticeMsgtypebyte, err := json.Marshal(noticeMsgtype)
  132. if err != nil {
  133. logs.Error(err)
  134. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  135. return
  136. }
  137. req, err := http.NewRequest("POST", "http://tmrwatch.cn/notice/msgtype", bytes.NewBuffer(noticeMsgtypebyte))
  138. if err != nil {
  139. logs.Error(err)
  140. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  141. return
  142. }
  143. req.Header.Set("Content-Type", "application/json")
  144. client := &http.Client{}
  145. resp, err := client.Do(req)
  146. if err != nil {
  147. logs.Error(err)
  148. }
  149. defer resp.Body.Close()
  150. getresp, err := http.Get(fmt.Sprintf("http://tmrwatch.cn/notice/msgtype?sys_name=crm&pasture_id=%d&service_id=%d", noticeMsgtype.PastureId, id))
  151. if err != nil {
  152. logs.Error(err)
  153. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  154. return
  155. }
  156. defer getresp.Body.Close()
  157. buf := bytes.NewBuffer(make([]byte, 0, 512))
  158. buf.ReadFrom(getresp.Body)
  159. respMap := make(map[string]interface{}, 0)
  160. err = json.Unmarshal(buf.Bytes(), &respMap)
  161. if err != nil {
  162. logs.Error(err)
  163. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  164. return
  165. }
  166. for _, resp := range respMap["data"].([]interface{}) {
  167. resp1 := resp.(map[string]interface{})
  168. _, err := tx.SQL(` update remind set service_id = ? where id = ? `, resp1["service_id"], id).Execute()
  169. if err != nil {
  170. logs.Error(err)
  171. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  172. return
  173. }
  174. }
  175. appG.Response(http.StatusOK, e.SUCCESS, true)
  176. }