Пример #1
0
func UpdateBankPaper(rc *impl.RCM_Cmd) (interface{}, error) {
	var uid, bankId int64
	var paperIds, p2bIds, ext string
	err := rc.ValidF(`
		uid,R|I,R:0;
		bankId,R|I,R:0;
		paperIds,R|S,L:0;
		p2bIds,O|S,L:0;
		ext,O|S,L:0;
		`, &uid, &bankId, &paperIds, &p2bIds, &ext)
	if err != nil {
		log.E("UpdateBankPaper arg err:%v", err)
		return 0, err
	}
	extParse := []P2b_Paper{}
	json.Unmarshal([]byte(ext), &extParse)
	rs := []P2b_Paper{}
	paperIdsArr := []int64{}
	err = json.Unmarshal([]byte(paperIds), &paperIdsArr)
	if err != nil {
		return nil, err
	}

	if p2bIds == "" {
		p2bIds = "0"
	}
	sql_ := "delete from ebs_p2b where bank_id=? and tid not in (" + p2bIds + ")"
	var conn *sql.DB = dbMgr.DbConn()
	tx, err := conn.Begin()
	if err != nil {
		return nil, err
	}

	//同步练习
	for _, v := range extParse {
		fmt.Println(tx.Exec("insert into ebs_p2b (tid,bank_id,paper_id,status,add1) values (?,?,?,'N','P')", v.P2bId, bankId, v.PId))
	}
	//同步练习

	if ext != "" {
		sql_ += " and add1='P'"
	} else {
		sql_ += " and (add1!='P' or add1 is null)"
	}
	log.D("%s", sql_)
	if _, err := tx.Exec(sql_, bankId); err != nil {
		tx.Rollback()
		return nil, err
	}
	for _, v := range paperIdsArr {
		if res, err := tx.Exec("insert into ebs_p2b (bank_id,paper_id,status) values (?,?,'N')", bankId, v); err != nil {
			tx.Rollback()
			return nil, err
		} else {
			p2bId, _ := res.LastInsertId()
			rs = append(rs, P2b_Paper{p2bId, v})
		}
	}
	return rs, tx.Commit()
}
Пример #2
0
func (b BANK) R(columns []string) (BANK, error) {
	vals := []interface{}{}
	for _, v := range columns {
		(&b).GetFieldByColumn(v, &vals)
	}
	sql_ := fmt.Sprintf("select %s from %s where tid=?", strings.Join(columns, ","), b.TableName())
	err := dbMgr.DbConn().QueryRow(sql_, *b.Tid).Scan(vals...)
	return b, err
}
Пример #3
0
func getPaperFromAnswerSnapshot(aid int64) (p PAPER, e error) {

	conn := dbMgr.DbConn()
	var s sql.NullString
	if e = conn.QueryRow("select paper_snapshot from ebs_answer where tid=?", aid).Scan(&s); e != nil {
		return
	} else {
		json.Unmarshal([]byte(s.String), &p)
	}
	return
}
Пример #4
0
func (c *BANK) U(tx *sql.Tx) error {
	c_st := reflect.TypeOf(*c)
	c_st_val := reflect.ValueOf(*c)

	if c.Tid == nil {
		return fmt.Errorf("tid is nil")
	}
	tid := *c.Tid
	if tid <= 0 {
		return fmt.Errorf("tid is not valid")
	}
	setSql := []string{}
	setVal := []interface{}{}
	for i := 0; i < c_st.NumField(); i++ {
		k := c_st.Field(i).Name
		tag := c_st.Field(i).Tag
		column := tag.Get("column")
		if column == "TID" || column == "" {
			continue
		}
		if !c_st_val.FieldByName(k).IsNil() {
			a := c_st_val.FieldByName(k)
			setSql = append(setSql, fmt.Sprintf("%s=?", column))
			setVal = append(setVal, getVal(a.Interface()))
		}
	}
	sql_ := fmt.Sprintf("update %s set %s where tid=%d", c.TableName(), strings.Join(setSql, ","), tid)
	if tx == nil {
		var conn *sql.DB
		conn = dbMgr.DbConn()
		n := 0
		if conn.QueryRow(fmt.Sprintf("select tid from %s where tid=%d", c.TableName(), tid)).Scan(&n); n == 0 {
			return fmt.Errorf("未找到请求的数据")
		}
		if _, err := conn.Exec(sql_, setVal...); err != nil {
			return err
		}
	} else {
		n := 0
		if tx.QueryRow(fmt.Sprintf("select tid from %s where tid=%d", c.TableName(), tid)).Scan(&n); n == 0 {
			return fmt.Errorf("未找到请求的数据")
		}
		if _, err := tx.Exec(sql_, setVal...); err != nil {
			return err
		}
	}
	return nil
}
Пример #5
0
func init() {
	s, _ := os.Getwd()
	fmt.Println("pwd:", s)
	arr := strings.Split(s, "src")
	confPath := arr[0] + "conf/ebs.properties"
	fmt.Println("conf file path is:", confPath)
	fmt.Println(conf.Cfg.InitWithFilePath(confPath))
	DEM.G_Dn = "mysql"
	//setting database connection
	DEM.G_Dsn = conf.TestDbConn()
	//	DEM.G_Dsn = "cny:123@tcp(127.0.0.1:3306)/rcpv2_test?charset=utf8"
	fmt.Println("db init err:", dbMgr.Init("DEM", conf.TestDbConn()))
	//	fmt.Println("db init err:", dbMgr.Init("DEM", "cny:123@tcp(127.0.0.1:3306)/rcpv2_test?charset=utf8"))
	t_conn = dbMgr.DbConn()

}
Пример #6
0
//获取题库的题目数量和试卷数量
func GetBankQcAndPc(bankIds string) ([]I, error) {
	rs := []I{}
	sql_ := fmt.Sprintf(`select count(*),bank_id,'qc' from ebs_q2b where bank_id in (%s) group by bank_id
union select count(*),bank_id,'pc' from ebs_p2b where bank_id in (%s) group by bank_id;`, bankIds, bankIds)
	conn := dbMgr.DbConn()
	if rows, err := conn.Query(sql_); err != nil {
		return rs, err
	} else {
		for rows.Next() {
			var n, bankId sql.NullInt64
			var t sql.NullString
			rows.Scan(&n, &bankId, &t)
			rs = append(rs, I{bankId.Int64, n.Int64, t.String})
		}
	}
	return rs, nil
}
Пример #7
0
func GetAnswerRecord(hs *routing.HTTPSession) routing.HResult {
	var aId int64
	err := hs.ValidCheckVal(`
		aId,R|I,R:0;
		`, &aId)
	if err != nil {
		log.E("GetAnswerRecord arg error:%v", err)
		return hs.MsgResE(1, fmt.Sprintf("%s:%s", common_.ARG_ERROR, err.Error()))
	}
	conn := dbMgr.DbConn()
	var content sql.NullString
	if err := conn.QueryRow("select content from ebs_answer where tid=?", aId).Scan(&content); err != nil {
		if err != sql.ErrNoRows {
			return hs.MsgResE(2, err.Error())
		}
	}
	return hs.MsgRes(content.String)
}
Пример #8
0
func GetUsrRecord(uid int64, bankIds string) ([]map[string]interface{}, error) {
	rs := []map[string]interface{}{}
	sql_ := fmt.Sprintf(`select count(*) cnt,tid,qid,GROUP_CONCAT(paper_id),bank_id,type from ebs_answer where uid=? and bank_id in (%s) group by bank_id,type`, bankIds)
	conn := dbMgr.DbConn()
	if rows, err := conn.Query(sql_, uid); err != nil {
		return rs, err
	} else {
		for rows.Next() {
			var v1, v2, v3, v5 sql.NullInt64
			var v6, v4 sql.NullString
			o := make(map[string]interface{})
			rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6)
			o["cnt"], o["aid"], o["qId"], o["pId"], o["bankId"], o["type"] = v1.Int64, v2.Int64, v3.Int64, v4.String, v5.Int64, v6.String
			rs = append(rs, o)
		}
	}
	return rs, nil
}
Пример #9
0
func FuncListBankQuestion(uid, bankId int64) ([]QUESTION, error) {
	rs := []QUESTION{}
	usrFilter := ""
	args := []interface{}{bankId, bankId}
	if uid != 0 {
		usrFilter = " and b.uid=?"
		args = append(args, uid)
	}
	conn := dbMgr.DbConn()
	sql_ := `select q.TID, q.NAME, q.BANK_ID, q.PID, q.TAG, q.DESCRIPTION, q.DURATION, q.SCORE, q.Q_ANALYZE, q.TYPE,q.Q_OPTION, q.ANSWER, q.UID, q.UNAME,q2b.score,q2b.ext,q2b.tid,q2b.seq
	from ebs_question q join ebs_q2b q2b on q2b.bank_id=? and q2b.q_id=q.tid join ebs_bank b on b.tid=?` + usrFilter + ` order by q2b.seq asc`
	log.D("sql:%s", sql_)
	if rows, err := conn.Query(sql_, args...); err != nil {
		return rs, err
	} else {
		columns := []string{"TID", "NAME", "BANK_ID", "PID", "TAG", "DESCRIPTION", "DURATION", "SCORE", "Q_ANALYZE", "TYPE",
			"Q_OPTION", "ANSWER", "UID", "UNAME", "SCORE"}
		for rows.Next() {
			vals := []interface{}{}
			b := QUESTION{}
			for _, v := range columns {
				(&b).GetFieldByColumn(v, &vals)
			}
			var ext sql.NullString
			var q2bId int64
			var q2bSeq sql.NullInt64
			vals = append(vals, &ext, &q2bId, &q2bSeq)
			if err := rows.Scan(vals...); err != nil {
				log.E("scan err:%v", err)
			}
			b.Q2bId = q2bId
			b.Q2bSeq = q2bSeq.Int64
			if ext.String != "" {
				extParse := EXT{}
				json.Unmarshal([]byte(ext.String), &extParse)
				b.SCORE = &extParse.Score
				b.DURATION = &extParse.Duration
				b.Ext = extParse
			}
			rs = append(rs, b)
		}
	}
	return rs, nil
}
Пример #10
0
func GetUsrPaperRecord(uid int64, p2bIds string) ([]map[string]interface{}, error) {
	rs := []map[string]interface{}{}
	if _, err := common.IdsValid(p2bIds, ",", -1); err != nil {
		return rs, err
	}
	sql_ := fmt.Sprintf(`select a.tid,a.begin,a.end,a.duration,a.score,a.uid,a.uname,a.status,a.p2b_id,a.bank_id,p.tid,p.name,qCnt.cnt,p2b.ext from ebs_answer a join ebs_p2b p2b on p2b.tid=a.p2b_id join
	ebs_paper p on p.tid=a.paper_id left join (select paper_id,count(*) cnt from ebs_p2q group by paper_id) qCnt on qCnt.paper_id=p.tid where a.p2b_id in (%s) and a.uid=?`, p2bIds)
	conn := dbMgr.DbConn()
	if rows, err := conn.Query(sql_, uid); err != nil {
		return rs, err
	} else {
		for rows.Next() {
			var v1, v2, v3, v4, v6, v9, v10, v11, v13 sql.NullInt64
			var v5 sql.NullFloat64
			var v7, v8, v12, v14 sql.NullString
			rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9, &v10, &v11, &v12, &v13, &v14)
			o := map[string]interface{}{
				"aid":      v1.Int64,
				"begin":    v2.Int64,
				"end":      v3.Int64,
				"duration": v4.Int64,
				"score":    v5.Float64,
				"uid":      v6.Int64,
				"uname":    v7.String,
				"status":   v8.String,
				"p2bId":    v9.Int64,
				"bankId":   v10.Int64,
				"pId":      v11.Int64,
				"pName":    v12.String,
				"qCnt":     v13.Int64,
				"ext":      v14.String,
				"process":  0,
			}
			rs = append(rs, o)
		}
	}
	return rs, nil
}
Пример #11
0
func (c *PAPER) C(tx *sql.Tx) (int64, error) {
	columns := []string{}
	columns_val := []string{}
	vals := []interface{}{}
	c_st := reflect.TypeOf(*c)
	c_st_val := reflect.ValueOf(*c)
	for i := 0; i < c_st.NumField(); i++ {
		k := c_st.Field(i).Name
		tag := c_st.Field(i).Tag
		column := tag.Get("column")
		if column == "" || column == "TID" {
			continue
		}
		if !c_st_val.FieldByName(k).IsNil() {
			a := c_st_val.FieldByName(k)
			columns = append(columns, fmt.Sprintf("%s", column))
			columns_val = append(columns_val, "?")
			vals = append(vals, getVal(a.Interface()))
		}
	}
	sql_ := fmt.Sprintf("insert into %s (%s) values (%s)", c.TableName(), strings.Join(columns, ","), strings.Join(columns_val, ","))

	if tx == nil {
		tx_ := dbMgr.DbConn()
		if rs, err := tx_.Exec(sql_, vals...); err != nil {
			return 0, err
		} else {
			return rs.LastInsertId()
		}
	} else {
		tx_ := tx
		if rs, err := tx_.Exec(sql_, vals...); err != nil {
			return 0, err
		} else {
			return rs.LastInsertId()
		}
	}
}
Пример #12
0
//获取一个题库中的试卷成绩,group by uid
func GetBankPaperScore(bankId, paperId int64) (interface{}, error) {
	rs := make(map[string]float64)
	sql_ := "select uid,score from ebs_answer where banK_id=? and p2b_id=? and status=?"
	conn := dbMgr.DbConn()
	if rows, err := conn.Query(sql_, bankId, paperId, "REMARKED"); err != nil {
		log.E("%v", err)
		return rs, err
	} else {
		for rows.Next() {
			var v1 sql.NullString
			var v2 sql.NullFloat64
			rows.Scan(&v1, &v2)
			rs[v1.String] = v2.Float64
		}
	}
	var totalScore sql.NullFloat64
	if err := conn.QueryRow(`select sum(score) from ebs_p2q where paper_id in (select p2b.paper_id from ebs_p2b p2b where p2b.tid=?)`, paperId).Scan(&totalScore); err != nil {
		return rs, err
	} else {
		rs["-1"] = totalScore.Float64
	}
	return rs, nil
}
Пример #13
0
func SaveAnswer(hs *routing.HTTPSession) routing.HResult {
	var aId int64
	var a string
	err := hs.ValidCheckVal(`
		aId,R|I,R:0;
		a,R|S,L:0;
		`, &aId, &a)
	if err != nil {
		log.E("SaveAnswer arg error:%v", err)
		return hs.MsgResE(1, fmt.Sprintf("%s:%s", common_.ARG_ERROR, err.Error()))
	}
	aParse := []ANSWER2{}
	if err := json.Unmarshal([]byte(a), &aParse); err != nil {
		log.E("%v", err)
		return hs.MsgResE(1, fmt.Sprintf("%s:作答内容格式错误,%s", common_.ARG_ERROR, err.Error()))
	}
	sql_ := `update ebs_answer set content=? where tid=?`
	conn := dbMgr.DbConn()
	if _, err := conn.Exec(sql_, a, aId); err != nil {
		log.E("SaveAnswer db error:%v,sql:%s,args:%v", err, sql_, a)
		return hs.MsgResE(2, fmt.Sprintf("%s:%s", common_.DB_ERROR, err.Error()))
	}
	return hs.MsgRes("success")
}
Пример #14
0
func AttendBankInfo(bankIds string) (II, error) {
	rs := II{}
	if bankIds == "" {
		return rs, nil
	}
	sql1 := fmt.Sprintf(`select a.count_,qCount from (select count(*) count_,'qCount',bank_id from ebs_q2b where bank_id in (%s) group by bank_id union select count(*),'pCount',bank_id from ebs_p2b where bank_id in (%s) group by bank_id)
	a join ebs_bank b on b.tid=a.bank_id`, bankIds, bankIds)
	if rows, err := dbMgr.DbConn().Query(sql1); err != nil {
		return rs, err
	} else {
		for rows.Next() {
			var cnt sql.NullInt64
			var t sql.NullString
			rows.Scan(&cnt, &t)
			if t.String == "qCount" {
				rs.QC = cnt.Int64
			}
			if t.String == "pCount" {
				rs.PC = cnt.Int64
			}
		}
	}
	return rs, nil
}
Пример #15
0
//查看题库题目,不显示题目答案
func FuncListBankQuestion2(bankId int64, parse common.ListParam, aid int64) ([]QUESTION, common.Pagiation, error) {
	rs := []QUESTION{}
	args := []interface{}{bankId}
	args = append(args, aid)

	//	answerFilter := ""
	//	if aid!=0{
	//		answerFilter = " and a.answer_key=?"
	//	}
	statusFilter := ""
	//1答对,0答错,-1未答
	if parse.Status != "" {
		statusFilter = " and a.add1=?"
		s := parse.Status
		if parse.Status == "-1" {
			statusFilter = " and a.status=?"
			s = "N"
		}
		args = append(args, s)
	}

	typeFilter := ""
	if parse.Type != 0 {
		typeFilter = " and q.type=?"
		args = append(args, parse.Type)
	}
	if parse.Pa.Pn < 1 || parse.Pa.Ps < 1 {
		parse.Pa.Pn = 1
		parse.Pa.Ps = 10
	}
	start := (parse.Pa.Pn - 1) * parse.Pa.Ps
	end := parse.Pa.Ps

	conn := dbMgr.DbConn()
	tx, err := conn.Begin()
	if err != nil {
		return rs, parse.Pa, err
	}
	sql_ := fmt.Sprintf(`select SQL_CALC_FOUND_ROWS q.TID, q.NAME, q.BANK_ID, q.PID, q.TAG, q.DESCRIPTION, q.DURATION, q.SCORE, q.Q_ANALYZE, q.TYPE,q.Q_OPTION, q.UID, q.UNAME,q2b.score,q2b.ext,q2b.tid,q2b.seq,q.desc2,a.tid,a.status,a.content,a.paper_snapshot,a.answer_snapshot from
	ebs_answer a left join ebs_question q on q.tid=a.qid left join ebs_q2b q2b on q2b.tid=a.q2b_id and q2b.bank_id=?
	where a.answer_key=? %s order by q2b.seq asc limit %d,%d`, statusFilter+typeFilter, start, end)
	if aid == 0 {
		sql_ = fmt.Sprintf(`select SQL_CALC_FOUND_ROWS q.TID, q.NAME, q.BANK_ID, q.PID, q.TAG, q.DESCRIPTION, q.DURATION, q.SCORE, q.Q_ANALYZE, q.TYPE,q.Q_OPTION, q.UID, q.UNAME,q2b.score,q2b.ext,q2b.tid,q2b.seq,q.desc2,a.tid,a.status,a.content,a.paper_snapshot,a.answer_snapshot
	from ebs_question q join ebs_q2b q2b on q2b.bank_id=? and q2b.q_id=q.tid left join ebs_answer a on a.answer_key=? and a.q2b_id=q2b.tid where 1=1 %s order by q2b.seq asc limit %d,%d`, statusFilter+typeFilter, start, end)
	}
	log.D("%v", sql_)
	if rows, err := tx.Query(sql_, args...); err != nil {
		tx.Commit()
		return rs, parse.Pa, err
	} else {
		columns := []string{"TID", "NAME", "BANK_ID", "PID", "TAG", "DESCRIPTION", "DURATION", "SCORE", "Q_ANALYZE", "TYPE",
			"Q_OPTION", "UID", "UNAME", "SCORE"}
		for rows.Next() {
			vals := []interface{}{}
			b := QUESTION{}
			for _, v := range columns {
				(&b).GetFieldByColumn(v, &vals)
			}
			var ext, desc2, aStatus, aContent, paperSnapshot, answerSnapshot sql.NullString
			var q2bId sql.NullInt64
			var q2bSeq, aIdRes sql.NullInt64
			vals = append(vals, &ext, &q2bId, &q2bSeq, &desc2, &aIdRes, &aStatus, &aContent, &paperSnapshot, &answerSnapshot)
			if err := rows.Scan(vals...); err != nil {
				log.E("scan err:%v", err)
			}
			qParse := QUESTION{}
			json.Unmarshal([]byte(paperSnapshot.String), &qParse)
			if qParse.Tid != nil {
				log.D("qParse:%v", qParse.ToString())
				b = qParse
				bb, _ := json.Marshal(qParse.Ext)
				ext.String = string(bb)
			}

			b.Q2bId = q2bId.Int64
			b.Q2bSeq = q2bSeq.Int64
			if b.TYPE != nil {
				if *b.TYPE == 35 {
					b.DESCRIPTION = &desc2.String
				}
			}
			if aid != 0 {
				b.Extra = map[string]interface{}{"aid": aIdRes.Int64, "aStatus": aStatus.String, "aContent": aContent.String}
			}
			if ext.String != "" {
				extParse := EXT{}
				json.Unmarshal([]byte(ext.String), &extParse)
				b.SCORE = &extParse.Score
				b.DURATION = &extParse.Duration
				b.Ext = extParse
			}
			rs = append(rs, b)
		}
	}

	if err := tx.QueryRow(`select FOUND_ROWS()`).Scan(&parse.Pa.Total); err != nil {
		tx.Commit()
		return rs, parse.Pa, err
	}
	tx.Commit()
	return rs, parse.Pa, nil
}
Пример #16
0
func run(args []string) {
	defer StopSrv()
	cfile := "conf/ebs.properties"
	if len(args) > 1 {
		cfile = args[1]
	}
	fmt.Println("Using config file:", cfile)
	err := conf.Cfg.InitWithFilePath(cfile)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	uap_conf.Cfg = conf.Cfg

	//init js conf
	conf.JsConf()

	log.I("Config:\n%v", conf.Cfg.Show())
	//test connect
	if len(conf.DbConn()) < 1 {
		fmt.Println("DB_CONN is not exist in config")
		return
	}

	// uri := "mongodb://*****:*****@192.168.2.30:27017/fs"
	// dbName := "fs"
	//	err = trak.DbInit(conf.TRACE_DB_CONN(), conf.TRACE_DB())
	//	if err != nil {
	//		panic(fmt.Sprintf("connect mongodb err %v", err.Error()))
	//	}

	dbMgr.Init("mysql", conf.DbConn())
	uap.InitDb(dbMgr.DbConn)
	usr.CheckUcs(dbMgr.DbConn())

	if err := nets.RedirectLog(conf.LOG_ADDR(), conf.LOG_PORT(), conf.LOG_KEY(), true); err != nil {
		fmt.Println("remote log error:", err)
	}
	sinfo := `{"description": "ebs接口文档",
    "version": "1.0.0",
    "title": "ebs接口文档"
    }`
	infoParse := map[string]interface{}{}
	json.Unmarshal([]byte(sinfo), &infoParse)
	go_swagger.NewH().InitSwagger(go_swagger.Swagger{
		SwaggerVersion: "2.0",
		Info:           infoParse,
		Host:           "ebs2.dev.jxzy.com",
		BasePath:       "",
		Schemes:        []string{"http"},
	}).AddTag(go_swagger.Tag{Name: "answer", Description: "作答相关接口"})

	//
	//
	mux := http.NewServeMux()
	mux.Handle("/", NewSrvMux(mux, "", "www"))
	log.D("running server on %s", conf.ListenAddr())
	s := http.Server{Addr: conf.ListenAddr(), Handler: mux}
	err = s.ListenAndServe()
	if err != nil {
		fmt.Println(err)
	}
}
Пример #17
0
//客观题批改
func correct(aId int64) (string, error) {
	var hasSubjective bool = false
	var totalScore float64
	sql_ := "select content,type,qid,paper_id,begin,q2b_id from ebs_answer where tid=?"
	var conn *sql.DB = dbMgr.DbConn()
	var content, type_ sql.NullString
	var qid, pId, begin, q2bId sql.NullInt64
	if err := conn.QueryRow(sql_, aId).Scan(&content, &type_, &qid, &pId, &begin, &q2bId); err != nil {
		return "", err
	}

	var qAnswerMap = make(map[string]string)
	var qAnswerMap2 = make(map[string]string)
	var qAnswerMap3 = make(map[string]string)

	//如果是一道题目
	if type_.String == "10" {
		sql_ := `select q.tid,q.answer,q2b.seq,q2b.ext,q.type from ebs_question q join ebs_q2b q2b on q2b.q_id=q.tid where q2b.tid=?`
		var v1 sql.NullInt64
		var v2 sql.NullString
		var v3 sql.NullString
		var v4, v5 sql.NullString
		if err := conn.QueryRow(sql_, q2bId.Int64).Scan(&v1, &v2, &v3, &v4, &v5); err != nil {
			return "", err
		} else {
			if v5.String == "40" {
				//如果是主观题
				hasSubjective = true
			}
			s_qId := fmt.Sprintf("%d", v1.Int64)
			qAnswerMap[s_qId] = v2.String
			qAnswerMap2[s_qId] = v4.String
			qAnswerMap3[s_qId] = v5.String
		}
	}
	//如果是一张试卷
	if type_.String == "20" {
		//获取试卷中的所有客观题题目
		cnt := 0
		if conn.QueryRow(`select count(*) from ebs_question q join ebs_p2q p2q on (p2q.qid=q.tid or q.pid=p2q.qid) and p2q.paper_id=? where q.type=40`, pId.Int64).Scan(&cnt); cnt != 0 {
			hasSubjective = true
		}

		//从analyze_snapshot字段中查找客观题解析
		sql_analyze := `select analyze_snapshot from ebs_answer where tid=?`
		var s sql.NullString
		if err := conn.QueryRow(sql_analyze, aId).Scan(&s); err != nil {
			return "", err
		} else {
			m := []map[string]interface{}{}
			//如果analyze_snapshot不为空
			if s.String != "" {
				json.Unmarshal([]byte(s.String), &m)
				for _, v := range m {
					s_qId := fmt.Sprintf("%v", v["v1"])
					qAnswerMap[s_qId] = fmt.Sprintf("%v", v["v2"])
					qAnswerMap2[s_qId] = fmt.Sprintf("%v", v["v4"])
					qAnswerMap3[s_qId] = fmt.Sprintf("%v", v["v5"])
				}
			} else {
				//从题目中查找解析
				sql_ := `select q.tid,q.answer,p2q.seq,p2q.ext,q.type from ebs_question q join ebs_p2q p2q on (p2q.qid=q.tid or q.pid=p2q.qid) and p2q.paper_id=? where q.type>=10 and q.type<=35`
				if rows, err := conn.Query(sql_, pId.Int64); err != nil {
					return "", err
				} else {
					for rows.Next() {
						var v1 sql.NullInt64
						var v2 sql.NullString
						var v3 sql.NullString
						var v4, v5 sql.NullString
						rows.Scan(&v1, &v2, &v3, &v4, &v5)
						s_qId := fmt.Sprintf("%d", v1.Int64)
						qAnswerMap[s_qId] = v2.String
						qAnswerMap2[s_qId] = v4.String
						qAnswerMap3[s_qId] = v5.String
					}
				}
			}
		}

	}

	//如果是题库作答
	if type_.String == "30" {
		sql_ := "select count(*) from ebs_answer where answer_key=? and status='REMARKING'"
		n := 0
		if conn.QueryRow(sql_, aId).Scan(&n); n > 0 {
			hasSubjective = true
		}
		conn.QueryRow("select sum(score) from ebs_answer where status='REMARKED' and answer_key=?", aId).Scan(&totalScore)
	}
	parse := []ANSWER2{}
	if err := json.Unmarshal([]byte(content.String), &parse); err != nil {
		return "", err
	}
	for i, v := range parse {
		s_qId := fmt.Sprintf("%v", v.QId)
		qAnswer := qAnswerMap[s_qId]
		s_ext := qAnswerMap2[s_qId]
		s_qType := qAnswerMap3[s_qId]
		ext := EXT{}
		json.Unmarshal([]byte(s_ext), &ext)
		qAnswerParse := [][]Q_ANSWER_ITEM{}
		if err := json.Unmarshal([]byte(qAnswer), &qAnswerParse); err != nil {
			log.D("qId为%s的题目答案格式错误,%v,%s", s_qId, err, qAnswer)
			parse[i].Msg = fmt.Sprintf("qId为%s的题目答案格式错误,%v,%s", s_qId, err, qAnswer)
			continue
			//			return err
		}
		if len(qAnswerParse) == 0 {
			log.D("qId为%s的题目没有答案", s_qId)
			parse[i].Msg = fmt.Sprintf("qId为%s的题目没有答案", s_qId)
			continue
		}
		if err := match(&(parse[i]), qAnswerParse, ext, s_qType); err != nil {
			return "", err
		} else {
			totalScore += (parse[i]).Score
		}
	}
	b, _ := json.Marshal(parse)
	//	now := time.Now().Unix()
	//	duration := now - begin.Int64
	status := "REMARKED"
	if hasSubjective {
		status = "REMARKING"
	}
	var add1 int64
	if len(parse) != 0 {
		add1 = parse[0].Right
	}
	if _, err := conn.Exec("update ebs_answer set score=?,content=?,status=?,add1=? where tid=?", totalScore, string(b), status, add1, aId); err != nil {
		return "", err
	}
	return status, nil
}
Пример #18
0
func GetBankCountInfo(bankIds string, uid int64) ([]map[string]interface{}, error) {
	rs_ := []map[string]interface{}{}
	_, err := common.IdsValid(bankIds, ",", -1)
	if err != nil {
		return rs_, err
	}
	rs := make(map[int64]interface{})
	sql1 := fmt.Sprintf(`select a.count_,b.tid,b.name,qCount from (select count(*) count_,'qCount',bank_id from ebs_q2b where bank_id in (%s) group by bank_id union select count(*),'pCount',bank_id from ebs_p2b where bank_id in (%s) group by bank_id)
	a join ebs_bank b on b.tid=a.bank_id`, bankIds, bankIds)
	sql2 := fmt.Sprintf(`select p2q.cnt,p.tid,p.name,p.description,p2b.bank_id,p2b.ext,p.q_grp,group_concat(a.score),group_concat(a.status),group_concat(a.tid),p2b.tid from ebs_paper p
	left join (select count(*) cnt,p2q.paper_id from ebs_p2q p2q join ebs_p2b p2b on p2q.paper_id=p2b.paper_id and p2b.bank_id in (%s)  group by paper_id) p2q on p2q.paper_id=p.tid
	join ebs_p2b p2b on p2b.paper_id=p.tid
	left join ebs_answer a on a.p2b_id=p2b.tid and a.uid=? where p2b.bank_id in (%s) group by p.tid`, bankIds, bankIds)
	//	log.D("sql1:%s\nsql2:%s",sql1,sql2)
	conn := dbMgr.DbConn()
	if rows, err := conn.Query(sql1); err != nil {
		return rs_, err
	} else {
		for rows.Next() {
			var v1, v2 sql.NullInt64
			var v3, v4 sql.NullString
			rows.Scan(&v1, &v2, &v3, &v4)
			if v, ok := rs[v2.Int64]; ok {
				v.(map[string]interface{})[v4.String] = v1.Int64
			} else {
				o := make(map[string]interface{})
				o["paperInfo"] = []map[string]interface{}{}
				o["name"] = v3.String
				o["bankId"] = v2.Int64

				o[v4.String] = v1.Int64
				rs[v2.Int64] = o
			}
		}

	}
	if rows, err := conn.Query(sql2, uid); err != nil {
		return rs_, err
	} else {
		for rows.Next() {

			var v1, v2, v5, v11 sql.NullInt64
			var v3, v4, v6, v7 sql.NullString
			var v8, v9, v10 sql.NullString
			rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9, &v10, &v11)
			o := map[string]interface{}{
				"pId": v2.Int64, "qCount": v1.Int64, "name": v3.String, "desc": v4.String, "p2bId": v11.Int64,
				"score": v8.String, "status": v9.String, "aid": v10.String, "ext": func() EXT {
					ext := EXT{}
					json.Unmarshal([]byte(v6.String), &ext)
					if ext.Begin == 0 {
						ext.P = "1"
					} else {
						if ext.Duration != 0 {
							diff := time.Now().Unix() - ext.Begin/1000
							fmt.Println(diff, time.Now().Unix(), ext.Begin/1000)
							if diff < ext.Duration {
								ext.P = "1"
							} else {
								ext.P = "0"
							}
						}
					}
					return ext
				}(), "part": func() int {
					qGrp := []Q_GRP{}
					json.Unmarshal([]byte(v7.String), &qGrp)
					return len(qGrp)
				}(),
			}

			if v, ok := rs[v5.Int64]; ok {
				if _, ok2 := v.(map[string]interface{})["paperInfo"]; ok2 {
					v.(map[string]interface{})["paperInfo"] = append(v.(map[string]interface{})["paperInfo"].([]map[string]interface{}), o)
				}
			}
		}
	}
	for _, v := range rs {
		rs_ = append(rs_, v.(map[string]interface{}))
	}
	return rs_, nil
}
Пример #19
0
func FuncListBankPaper(uid, bankId int64) ([]map[string]interface{}, error) {
	rs := []map[string]interface{}{}
	sql_ := fmt.Sprintf(`select p.tid,p.name,p2b.tid p2bId,p.description,group_concat(a.tid),group_concat(a.status),p2b.ext,group_concat(a.score),p.q_grp,a.submit_time,avgA.aScore,avgA.aDuration,group_concat(a.duration),content,p2q.cnt,a.duration
	  from ebs_p2b p2b 
				join ebs_paper p on p2b.paper_id=p.tid and p2b.bank_id=? 
				join (select p2b_id,avg(score) aScore,avg(duration) aDuration from ebs_answer where status='REMARKED' group by p2b_id) avgA on avgA.p2b_id=p2b.tid
				join (select count(*) cnt,paper_id from ebs_p2q group by paper_id) p2q on p2q.paper_id=p.tid
				left join (select tid,paper_id,status,score,p2b_id,submit_time,duration,content from ebs_answer where uid=? and bank_id=? order by tid asc) a on a.p2b_id=p2b.tid group by p2b.tid order by p2b.time desc`)
	conn := dbMgr.DbConn()
	if rows, err := conn.Query(sql_, bankId, uid, bankId); err != nil {
		return rs, err
	} else {
		for rows.Next() {
			var tid, qCount, firstDuration sql.NullInt64
			var name, desc, a_status, aids, ext, scores, qGrp, submitTime, durations, content sql.NullString
			var p2bId sql.NullInt64
			var avgScore, avgDuration sql.NullFloat64
			rows.Scan(&tid, &name, &p2bId, &desc, &aids, &a_status, &ext, &scores, &qGrp, &submitTime, &avgScore, &avgDuration, &durations, &content, &qCount, &firstDuration)
			status := ""
			if a_status.String != "" {
				status = a_status.String
			}
			answer2 := []ANSWER2{}
			json.Unmarshal([]byte(content.String), &answer2)
			doneQuestion := 0
			for _, v := range answer2 {
				if v.Right >= 0 {
					doneQuestion++
				}
			}
			paperRecord := []map[string]interface{}{}
			if a_status.String != "" {
				arr := strings.Split(a_status.String, ",")
				arr2 := strings.Split(aids.String, ",")
				for i, v := range arr {
					paperRecord = append(paperRecord, map[string]interface{}{
						"status": v, "aid": arr2[i], "score": 0, "duration": 0,
					})
				}
				arr3 := strings.Split(scores.String, ",")
				if len(arr3) == len(arr) {
					for i, v := range arr3 {
						paperRecord[i]["score"] = v
					}
				} else {
					if len(arr3) > 0 {
						paperRecord[0]["score"] = arr3[0]
					}
				}
				arr4 := strings.Split(durations.String, ",")
				if len(arr) == len(arr4) {
					for i, v := range arr4 {
						paperRecord[i]["duration"] = v
					}
				} else {
					if len(arr4) > 0 {
						paperRecord[0]["duration"] = arr4[0]
					}
				}

			}
			n, _ := strconv.ParseInt(submitTime.String, 10, 64)
			s_submitTime := time.Unix(n, 0).Format("2006-01-02 15:04:05")
			if n == 0 {
				s_submitTime = "0000-00-00 00:00:00"
			}
			o := map[string]interface{}{
				"id": tid.Int64, "name": name.String, "p2bId": p2bId.Int64, "bankId": bankId, "desc": desc.String, "price": 0, "a_status": status,
				"qGrp": qGrp.String, "submitTime": s_submitTime, "paperRecord": paperRecord, "avgScore": avgScore.Float64, "avgDuration": avgDuration.Float64,
				"qCount": qCount.Int64, "doneQuestion": doneQuestion, "duration": firstDuration.Int64,
				"answerIds": aids.String, "ext": ext.String, "score": scores.String, "p": func() string {
					ext_ := EXT{}
					json.Unmarshal([]byte(ext.String), &ext_)
					if ext_.Begin == 0 {
						return "1"
					} else {
						if ext_.Duration != 0 {
							diff := time.Now().Unix() - ext_.Begin/1000
							fmt.Println(diff, time.Now().Unix(), ext_.Begin/1000)
							if diff < ext_.Duration {
								return "1"
							} else {
								return "0"
							}
						}
					}
					return "1"

				}(),
			}
			rs = append(rs, o)
		}
	}
	return rs, nil
}
Пример #20
0
func getPaper(id int64, f int64) (p PAPER, err_ error) {

	conn := dbMgr.DbConn()
	fmt.Println(conn.QueryRow("select tid,name,description,bank_id,tag,duration,score,q_grp,uid,uname from ebs_paper where tid=? and status='N'", id).Scan(
		&p.Tid, &p.Name, &p.DESCRIPTION, &p.BANK_ID, &p.TAG, &p.DURATION, &p.SCORE, &p.Q_GRP, &p.UID, &p.UNAME,
	))

	qgParse := []Q_GRP{}
	if p.Q_GRP != nil {
		fmt.Println(json.Unmarshal([]byte(*p.Q_GRP), &qgParse))
	}

	if len(qgParse) == 0 {
		return
	}
	questions := []map[string]interface{}{}
	child_questions := []map[string]interface{}{}
	sql_ := `select q.q_analyze,q.answer,q.tid,q.name,q.pid,q.tag,q.description,q.duration,q.score,q.type,q.q_option,p2q.seq,p2q.q_grp,p2q.tid,p2q.ext,q.desc2,q.uid from ebs_p2q p2q join ebs_question q on q.tid=p2q.qid where p2q.paper_id=? order by p2q.seq`
	//union select q.q_analyze,q.answer,q.tid,q.name,q.pid,q.tag,q.description,q.duration,q.score,q.type,q.q_option,0,0,0,0,q.desc2,q.uid from ebs_question q where q.pid<>0 and q.pid in (
	//select q.tid from ebs_p2q p2q join ebs_question q on q.tid=p2q.qid where p2q.paper_id=?)
	if rows, err := conn.Query(sql_, id); err != nil {
		log.E("GetPaperDetail Query err:%v,sql:%s", err, sql_)
		err_ = err
		return
	} else {
		for rows.Next() {
			var v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17 sql.NullString
			rows.Scan(&v12, &v13, &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9, &v10, &v11, &v14, &v15, &v16, &v17)
			if v3.String != "0" {
				o := map[string]interface{}{
					"id":          v1.String,
					"name":        v2.String,
					"pid":         v3.String,
					"tag":         v4.String,
					"description": v5.String,
					"desc":        v5.String,
					"duration":    v6.String,
					"score":       v7.String,
					"type":        v8.String,
					"option":      v9.String,
					"seq":         v10.String,
					"uid":         v17.String,
				}
				if v8.String == "35" {
					o["description"], o["desc"] = v16.String, v16.String
				}
				if f == 1 {
					o["answer"] = v13.String
					o["analyze"] = v12.String
					o["desc"] = v5.String
				}
				child_questions = append(child_questions, o)

			} else {

				o := map[string]interface{}{
					"id":          v1.String,
					"name":        v2.String,
					"pid":         v3.String,
					"tag":         v4.String,
					"description": v5.String,
					"desc":        v5.String,
					"duration":    v6.String,
					"score": func() float64 {
						ext_ := EXT{}
						json.Unmarshal([]byte(v15.String), &ext_)
						return ext_.Score
					}(),
					"type":   v8.String,
					"option": v9.String,
					"seq":    v10.String,
					"qGrp":   v11.String,
					"p2qId":  v14.String,
					"ext": func() EXT {
						ext_ := EXT{}
						json.Unmarshal([]byte(v15.String), &ext_)
						return ext_
					}(),
					"uid": v17.String,
				}
				if v8.String == "35" {
					o["description"], o["desc"] = v16.String, v16.String
				}
				if f == 1 {
					o["answer"] = v13.String
					o["analyze"] = v12.String
					o["desc"] = v5.String
				}
				questions = append(questions, o)
			}
		}

		for i_, v_ := range questions {
			children := []map[string]interface{}{}
			for _, v := range child_questions {
				if v["pid"] == v_["id"] {
					children = append(children, v)
				}
			}
			questions[i_]["children"] = children
		}
	}
	var totalScore float64
	for _, v_ := range questions {
		totalScore += v_["score"].(float64)
	}
	for i, v := range qgParse {
		children := []map[string]interface{}{}
		for _, v_ := range questions {
			if v_["qGrp"] == fmt.Sprintf("%d", v.Id) {
				children = append(children, v_)
			}
		}
		qgParse[i].Questions = children
	}
	p.SCORE = &totalScore
	p.Q_PARSE = qgParse
	return
}