package api import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "strings" "../../pkg/app" "../../pkg/e" "../../routers/restful" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/astaxie/beego/logs" "github.com/gin-gonic/gin" ) func GetRemind(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) parammaps := fsion.Get("parammaps") pastureid := parammaps.Get("pastureid").ValueInt64() // pastureid := 0 tx := restful.Engine.NewSession() defer tx.Close() dataList, err := tx.SQL("select * from remind ").Query().List() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } resp, err := http.Get(fmt.Sprintf("http://tmrwatch.cn/notice/msgtype?sys_name=crm&pasture_id=%d", pastureid)) if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } defer resp.Body.Close() buf := bytes.NewBuffer(make([]byte, 0, 512)) buf.ReadFrom(resp.Body) fmt.Println(string(buf.Bytes())) respMap := make(map[string]interface{}, 0) err = json.Unmarshal(buf.Bytes(), &respMap) if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } for _, data := range dataList { r := make(map[string]interface{}, 0) r["sys_name"] = "" r["service_id"] = "" r["pasture_id"] = 0 r["type_name"] = "" r["remind_type_id"] = "" r["remind_type"] = "" r["push_date"] = "" r["push_time"] = "" r["interval_time"] = "" r["push_limit"] = "" r["template_id"] = "" r["roles_id"] = "" r["users_id"] = "" r["date_type"] = "" r["cycle_type"] = "" data["data"] = r for _, resp := range respMap["data"].([]interface{}) { resp1 := resp.(map[string]interface{}) if data["id"].(int64) == int64(resp1["service_id"].(float64)) { if resp1["push_time"] == nil { resp1["push_time"] = "" } data["data"] = resp1 break } } } appG.Response(http.StatusOK, e.SUCCESS, dataList) } type NoticeMsgtype struct { SysName string `json:"sys_name"` PastureId int64 `json:"pasture_id"` ServiceId int64 `json:"service_id"` TypeName string `json:"type_name"` RemindType int64 `json:"remind_type"` PushDate int64 `json:"push_date"` PushTime string `json:"push_time"` IntervalTime int64 `json:"interval_time"` PushLimit int64 `json:"push_limit"` TemplateId string `json:"template_id"` UsersId string `json:"users_id"` DateType int64 `json:"date_type"` CycleType int64 `json:"cycle_type"` DelayTime int64 `json:"delay_time"` Status int64 `json:"status"` } func UpdateRemind(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) parammaps := gofasion.NewFasion(string(dataByte)) id := parammaps.Get("id").ValueInt64() noticeMsgtype := new(NoticeMsgtype) noticeMsgtype.SysName = parammaps.Get("sys_name").ValueStr() noticeMsgtype.ServiceId = id // noticeMsgtype.PastureId = parammaps.Get("pastureid").ValueInt64() noticeMsgtype.TypeName = parammaps.Get("type_name").ValueStr() noticeMsgtype.RemindType = parammaps.Get("remind_type").ValueInt64() noticeMsgtype.PushDate = parammaps.Get("push_date").ValueInt64() noticeMsgtype.PushTime = parammaps.Get("push_time").ValueStr() noticeMsgtype.IntervalTime = parammaps.Get("interval_time").ValueInt64() noticeMsgtype.PushLimit = parammaps.Get("push_limit").ValueInt64() noticeMsgtype.TemplateId = parammaps.Get("template_id").ValueStr() noticeMsgtype.DateType = parammaps.Get("date_type").ValueInt64() noticeMsgtype.CycleType = parammaps.Get("cycle_type").ValueInt64() noticeMsgtype.DelayTime = parammaps.Get("delay_time").ValueInt64() noticeMsgtype.Status = parammaps.Get("status").ValueInt64() //TODO noticeMsgtype.SysName = "crm" userList := parammaps.Get("user").Array() var userstr []string for _, user := range userList { userstr = append(userstr, user.ValueStr()) } tx := restful.Engine.NewSession() defer tx.Close() _, err := tx.SQL(` update remind set user = ? where id = ? `, strings.Join(userstr, ","), id).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } noticeMsgtypebyte, err := json.Marshal(noticeMsgtype) if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } req, err := http.NewRequest("POST", "http://tmrwatch.cn/notice/msgtype", bytes.NewBuffer(noticeMsgtypebyte)) if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { logs.Error(err) } defer resp.Body.Close() getresp, err := http.Get(fmt.Sprintf("http://tmrwatch.cn/notice/msgtype?sys_name=crm&pasture_id=%d&service_id=%d", noticeMsgtype.PastureId, id)) if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } defer getresp.Body.Close() buf := bytes.NewBuffer(make([]byte, 0, 512)) buf.ReadFrom(getresp.Body) respMap := make(map[string]interface{}, 0) err = json.Unmarshal(buf.Bytes(), &respMap) if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } for _, resp := range respMap["data"].([]interface{}) { resp1 := resp.(map[string]interface{}) _, err := tx.SQL(` update remind set service_id = ? where id = ? `, resp1["service_id"], id).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } appG.Response(http.StatusOK, e.SUCCESS, true) }