package controllersimport ( "github.com/revel/revel" "fmt" "time" "strconv" "strings" "database/sql" "text/template" _"github.com/go-sql-driver/mysql")type Spark struct { *revel.Controller}var ( starlist = map[string]string{ "xuezhiqian":"薛之谦", "wanghan":"汪涵", "yangdi":"杨迪", "guoxuefu":"郭雪芙", "zhangyu":"张宇", "liuwei":"刘维", "tianyuan":"田源", "qianfeng":"钱枫", "shenmengchen":"沈梦辰", "zhaosilu":"赵思璐", } db *sql.DB)type dbObj struct{ db *sql.DB}func (c Spark) getcase() *dbObj{ if db != nil{ return &dbObj{db:db} }else{ var conf map[string]string conf = c.getconf() fmt.Println(conf) server := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s",conf["user"],conf["pwd"],conf["host"],conf["port"],conf["db"],conf["charset"]) db, _ = sql.Open("mysql", server) //第一个参数为驱动名 db.SetMaxOpenConns( 100 ) db.SetMaxIdleConns( 10 ) return &dbObj{db} }}func (c Spark) getdb() { if db == nil { var conf map[string]string conf = c.getconf() fmt.Println(conf) server := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s",conf["user"],conf["pwd"],conf["host"],conf["port"],conf["db"],conf["charset"]) db, _ = sql.Open("mysql", server) //第一个参数为驱动名 db.SetMaxOpenConns( 100 ) db.SetMaxIdleConns( 10 ) }}func (c Spark) getconf() map[string]string{ var conf map[string]string conf = make(map[string]string,10) conf["host"],_ = revel.Config.String("host") conf["db"],_ = revel.Config.String("db") conf["pwd"],_ = revel.Config.String("pwd") conf["user"],_ = revel.Config.String("user") conf["port"],_ = revel.Config.String("port") conf["charset"],_ = revel.Config.String("charset") return conf}func (c Spark) Add (starname string,url string , username string,cb string) revel.Result { starname = template.HTMLEscapeString(strings.TrimSpace(starname)) username = template.HTMLEscapeString(strings.TrimSpace(username)) url = template.HTMLEscapeString(strings.TrimSpace(url)) if starname == "" { return c.display(1,"请填写好你支持的明星,再提交!","",cb) } if url == "" { return c.display(2,"请填写好你剪辑好的视频,再提交!","",cb) } newlist := make(map[string]string,len(starlist)) for key,name := range starlist { newlist[name] = key } _, ok := newlist[starname] if(!ok){ return c.display(3,"填写的明星不存在","",cb) } t := time.Now().Unix() tt := strconv.FormatInt(t,10) //c.getdb() c.getcase() //stmt,err := db.Prepare("insert into star (starname,username,videourl,addtime,status) values (?,?,?,?,?)") //checkErr(err) //res,err := stmt.Exec(starname,username,url,tt,0) //checkErr(err) //id,err := res.LastInsertId() //fmt.Println(id) fmt.Println(db) _,err := db.Exec("insert into star (starname,username,videourl,addtime,status) values (?,?,?,?,?)",starname,username,url,tt,0) _,err = db.Exec("update info set num=num+1 where starname=?",starname) //checkErr(err) //id,err = res.LastInsertId() if err == nil { return c.display(0,"ok","提交成功,请耐心等待工作人员审核",cb) } else { return c.display(4,"插入数据库失败","",cb) }}func (c Spark) Getstarlist(cb string) revel.Result{ c.getcase() rows,err := db.Query("select id,starname, num ,imgurl,videourl ,headimg from info order by num desc") checkErr(err) defer rows.Close() list := make( []map[string]interface{},0,1 ) for rows.Next() { row := make(map[string]interface{}) var id int var starname string var num int var imgurl string var videourl string var headimg string err = rows.Scan(&id,&starname,&num,&imgurl,&videourl,&headimg) row["id"] = id row["starname"] = starname row["num"] = num row["imgurl"] = imgurl row["videourl"] = videourl row["headimg"] = headimg checkErr(err) list = append(list,row) } return c.display(0,"ok",list,cb)}func (c Spark) Getone(name string,cb string) revel.Result{ //c.getdb() c.getcase() stmt,err := db.Prepare("select id,starname, num ,imgurl,videourl ,headimg from info where starname=?") checkErr(err) var id int var starname string var num int var imgurl string var videourl string var headimg string error := stmt.QueryRow(name).Scan(&id,&starname,&num,&imgurl,&videourl,&headimg) checkErr(error) row := make(map[string]interface{}) row["id"] = id row["starname"] = starname row["num"] = num row["imgurl"] = imgurl row["videourl"] = videourl row["headimg"] = headimg return c.display(0,"ok",row,cb)}func checkErr(err error) { if err != nil { panic(err) }}func (c Spark) display (errno int,msg string,data interface{},cb string)revel.Result{ var result = make(map[string]interface{},2) result["errno"] = errno result["msg"] = msg result["data"] = data if cb != "" { return c.RenderJSONP( cb,result ) } else { return c.RenderJSON( result ) }}