示例#1
0
//根据电话获取parent表的id
func getParentIdByPhone(phone string) (int64, error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select pid from parent where telephone=?"

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, phone)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	var id int64

	if rows.Next() {

		err = commonlib.PutRecord(rows, &id)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return 0, err
		}
	}

	return id, nil
}
示例#2
0
func getNewestFreeScheduleIdByChildId(childId string) (scheduleId, classId string, err error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select schedule_detail_id,wyclass_id from schedule_detail_child where child_id=? and is_free=1 order by id desc "
	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, childId)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return "", "", err
	}

	if rows.Next() {
		err = commonlib.PutRecord(rows, &scheduleId, &classId)
		if err != nil {
			lessgo.Log.Error(err.Error())
			return "", "", err
		}
	}

	return scheduleId, classId, nil
}
示例#3
0
//根据id查找视频
func FindAudioById(id int) (*Audio, error) {

	baseSql := "select aid,cid,start_time,end_time,filename,localphone,remotephone,seconds,`inout` from audio where aid=? "

	lessgo.Log.Debug(baseSql)

	db := lessgo.GetMySQL()
	defer db.Close()

	rows, err := db.Query(baseSql, id)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return nil, err
	}

	audio := new(Audio)

	if rows.Next() {
		err := commonlib.PutRecord(rows, &audio.Aid, &audio.Cid, &audio.StartTime, &audio.EndTime, &audio.Filename, &audio.LocalPhone, &audio.RemotePhone, &audio.Seconds, &audio.Inout)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return nil, err
		}
	}

	return audio, nil
}
示例#4
0
//获取指定合同的有效上课次数,这里的有效指的是正常上课和旷课
func getVaildNumOfContract(id string) (int, error) {

	sql := `select count(1) from sign_in where contract_id=? and (type=1 or type=3) `

	lessgo.Log.Debug(sql)

	db := lessgo.GetMySQL()
	defer db.Close()

	rows, err := db.Query(sql, id)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	num := 0

	if rows.Next() {
		err = commonlib.PutRecord(rows, &num)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return 0, err
		}
	}

	return num, nil
}
示例#5
0
//修改表单数据读取
func LoadCourseware(id string) (loadFormObjects []lessgo.LoadFormObject, err error) {
	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select id,name,intro from courseware where id=? "
	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, id)

	var name, intro string

	if rows.Next() {
		err := commonlib.PutRecord(rows, &id, &name, &intro)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			return nil, err
		}
	}

	h1 := lessgo.LoadFormObject{"id", id}
	h2 := lessgo.LoadFormObject{"name", name}
	h3 := lessgo.LoadFormObject{"intro", intro}

	loadFormObjects = append(loadFormObjects, h1)
	loadFormObjects = append(loadFormObjects, h2)
	loadFormObjects = append(loadFormObjects, h3)

	return loadFormObjects, nil
}
示例#6
0
func FindChildById(id string) (Child, error) {
	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select cid,name,card_id,pid,sex,birthday,hobby,center_id,avatar from child where cid=?"

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, id)

	if err != nil {
		lessgo.Log.Warn(err.Error())
		return Child{}, err
	}

	child := Child{}

	if rows.Next() {
		err = commonlib.PutRecord(rows, &child.Cid, &child.Name, &child.CardId, &child.Pid, &child.Sex, &child.Birthday, &child.Hobby, &child.CenterId, &child.Avatar)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			return Child{}, err
		}
	}

	return child, nil
}
示例#7
0
//根据id获取员工信息
func FindEmployeeById(id int) (Employee, error) {

	var employee Employee

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select user_id,username,really_name,department_id,center_id from employee where user_id=?"

	rows, err := db.Query(sql, id)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return employee, err
	}

	if rows.Next() {
		err := commonlib.PutRecord(rows, &employee.UserId, &employee.UserName, &employee.ReallyName, &employee.DepartmentId, &employee.CenterId)
		if err != nil {
			lessgo.Log.Error(err.Error())
			return employee, err
		}
	}

	return employee, nil
}
示例#8
0
func checkScheduleExist(centerId, roomId, timeId, dayDate string) (flag bool, err error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select id from class_schedule_detail where center_id=? and room_id=? and time_id=? and day_date=?"
	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, centerId, roomId, timeId, dayDate)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return false, err
	}

	scheduleDetailId := 0

	if rows.Next() {
		err = commonlib.PutRecord(rows, &scheduleDetailId)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return false, err
		}
	}

	if scheduleDetailId == 0 {
		return false, nil
	}

	return true, nil
}
示例#9
0
func FindClassById(id string) (WyClass, error) {
	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select wc.class_id,wc.assistant_id,wc.name,wc.course_id,wc.center_id,wc.child_num,wc.end_time,wc.deadline,wc.max_child_num,wc.teacher_id,c.is_probation from wyclass wc left join course c on wc.course_id=c.cid where wc.class_id=?"

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, id)

	if err != nil {
		lessgo.Log.Warn(err.Error())
		return WyClass{}, err
	}

	class := WyClass{}

	if rows.Next() {
		err = commonlib.PutRecord(rows, &class.ClassId, &class.AssistantId, &class.Name, &class.CourseId, &class.CenterId, &class.ChildNum, &class.EndTime, &class.DeadLine, &class.MaxChildNum, &class.TeacherId, &class.IsProbation)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			return WyClass{}, err
		}
	}

	return class, nil
}
示例#10
0
func GetNewsById(id string) (*News, string, error) {
	sql := "select title, news_content, preview_image, preview_text, is_carousel, carousel_image, upload_time from news where id=?;"
	lessgo.Log.Debug(sql, id)
	rows, err := DBSelect(sql, id)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return nil, "查询用户信息出错", err
	}

	news := new(News)
	news.Id, _ = strconv.Atoi(id)
	if rows.Next() {
		err = commonlib.PutRecord(rows, &news.Title, &news.NewsContent, &news.PreviewImage,
			&news.PreviewText, &news.IsCarousel, &news.CarouselImage, &news.UploadTime)
		if err != nil {
			lessgo.Log.Error(err.Error())
			return nil, "封装用户信息出错", err
		}
		news.UploadTime = TimeFormat(news.UploadTime)
	}
	lessgo.Log.Debug(news)

	return news, "处理成功", nil
}
示例#11
0
/*
select id,child_id,apply_time,contract_no,parent_id,price,employee_id,center_id,course_id,left_lesson_num,type,status,expire_date from contract where id=?
*/
func getContractIdByChildIdAndScheduleId(childId, scheduleId string) (contractId, isFree string, err error) {

	sql := `select contract_id,is_free from schedule_detail_child where child_id=? and schedule_detail_id=?`
	lessgo.Log.Debug(sql)

	db := lessgo.GetMySQL()
	defer db.Close()

	rows, err := db.Query(sql, childId, scheduleId)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return "0", "", err
	}

	if rows.Next() {
		err = commonlib.PutRecord(rows, &contractId, &isFree)
	}

	if err != nil {
		lessgo.Log.Error(err.Error())
		return "0", "", err
	}

	return contractId, isFree, nil
}
示例#12
0
//todo 目前只返回第一个孩子的id,逻辑有待优化
func getChildByParentId(pid string) (int64, error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select cid from child where pid=?"

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, pid)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	var id int64

	if rows.Next() {

		err = commonlib.PutRecord(rows, &id)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return 0, err
		}
	}

	return id, nil
}
示例#13
0
//todo 目前只返回第一个孩子的id,逻辑有待优化
func getChildByConsumerId(consumerId string) (int64, error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select ch.cid from child ch left join consumer_new cons on cons.parent_id=ch.pid where cons.id=?"

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, consumerId)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	var id int64

	if rows.Next() {

		err = commonlib.PutRecord(rows, &id)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return 0, err
		}
	}

	return id, nil
}
示例#14
0
func checkScheduleTempChildExist(childId, scheduleTempId string) (flag bool, err error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := " select count(1) from schedule_template_child where child_id=? and  schedule_template_id=? "

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, childId, scheduleTempId)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return false, err
	}

	num := 0

	if rows.Next() {

		err = commonlib.PutRecord(rows, &num)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return false, err
		}
	}

	if num > 0 {
		return true, nil
	}

	return false, nil
}
示例#15
0
func FindLessonById(id string) (Lesson, error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select lid,course_id,caption,teacher_id,keystone,lesson_time,order_no,is_last from lesson where lid=? "

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, id)

	if err != nil {
		lessgo.Log.Warn(err.Error())
		return Lesson{}, err
	}

	lesson := Lesson{}

	if rows.Next() {
		err = commonlib.PutRecord(rows, &lesson.Lid, &lesson.CourseId, &lesson.Caption, &lesson.TeacherId, &lesson.KeyStone, &lesson.LessonTime, &lesson.OrderNo, &lesson.IsLast)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			return Lesson{}, err
		}
	}

	return lesson, nil
}
示例#16
0
func FindTimeSectionById(id string) (TimeSection, error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select id,center_id,start_time,end_time,lesson_no from time_section where id=? "

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, id)

	if err != nil {
		lessgo.Log.Warn(err.Error())
		return TimeSection{}, err
	}

	timeSection := TimeSection{}

	if rows.Next() {
		err = commonlib.PutRecord(rows, &timeSection.Id, &timeSection.CenterId, &timeSection.StartTime, &timeSection.EndTime, &timeSection.LessonNo)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			return TimeSection{}, err
		}
	}

	return timeSection, nil
}
示例#17
0
func getFurtherScheduleIds(timeId, roomId, week, startTime, courseId string) ([]string, error) {

	sql := `
			select id from class_schedule_detail where time_id=? and room_id=? and week=? and start_time>=? and course_id=?`

	lessgo.Log.Debug(sql)

	db := lessgo.GetMySQL()
	defer db.Close()

	rows, err := db.Query(sql, timeId, roomId, week, startTime, courseId)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return nil, err
	}

	ids := []string{}

	for rows.Next() {
		id := ""
		err := commonlib.PutRecord(rows, &id)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return nil, err
		}

		ids = append(ids, id)
	}

	return ids, nil
}
示例#18
0
//根据角色ID获取员工列表
func EmployeeListByRoleIdAction(w http.ResponseWriter, r *http.Request) {
	m := make(map[string]interface{})

	err := r.ParseForm()

	if err != nil {
		m["success"] = false
		m["code"] = 100
		m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
		commonlib.OutputJson(w, m, " ")
		return
	}

	id := r.FormValue("id")

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select e.user_id,e.really_name from (select distinct(user_id) uid from employee_role where role_id =?) a left join employee e  on a.uid = e.user_id"

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, id)

	if err != nil {
		m["success"] = false
		m["code"] = 100
		m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
		commonlib.OutputJson(w, m, " ")
		return
	}

	employees := []lessgo.Employee{}

	for rows.Next() {
		employee := lessgo.Employee{}

		err := commonlib.PutRecord(rows, &employee.UserId, &employee.ReallyName)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			m["success"] = false
			m["code"] = 100
			m["msg"] = "系统发生错误,请联系IT部门"
			commonlib.OutputJson(w, m, " ")
			return
		}

		employees = append(employees, employee)
	}

	m["success"] = true
	m["code"] = 200
	m["datas"] = employees

	commonlib.OutputJson(w, m, " ")
}
示例#19
0
文件: server.go 项目: huangjq/lessgo
//根据id查找对象
func FindById(entity Entity, id string) (*Model, error) {

	dataSql := "select " + entity.Pk + ","

	for index, field := range entity.Fields {
		dataSql += field.Name
		if index < len(entity.Fields)-1 {
			dataSql += ","
		}
	}

	dataSql += " from " + entity.Id + " where " + entity.Pk + "=?"

	Log.Debug(dataSql)

	db := DBPool{}.getMySQL()
	defer db.Close()

	rows, err := db.Query(dataSql, id)

	if err != nil {
		Log.Warn(err.Error())
		return nil, err
	}

	model := new(Model)
	model.Entity = entity
	model.Id = 0
	model.Props = []*Prop{}

	if rows.Next() {

		fillObjects := []interface{}{}

		fillObjects = append(fillObjects, &model.Id)

		for _, field := range entity.Fields {
			prop := new(Prop)
			prop.Name = field.Name
			prop.Value = ""
			fillObjects = append(fillObjects, &prop.Value)
			model.Props = append(model.Props, prop)
		}

		err = commonlib.PutRecord(rows, fillObjects...)

		if err != nil {
			return nil, err
		}
	}

	return model, nil
}
示例#20
0
func CheckPwd(username, password string) (bool, lessgo.Employee, string) {

	var employee lessgo.Employee
	var dbPwd string

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select user_id,username,password,really_name,department_id from employee where username=?"
	rows, err := db.Query(sql, username)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return false, employee, " 数据库异常!"
	}
	if rows.Next() {
		err := rows.Scan(&employee.UserId, &employee.UserName,
			&dbPwd, &employee.ReallyName, &employee.DepartmentId)
		if err != nil {
			lessgo.Log.Error(err.Error())
			return false, employee, " 数据库异常!"
		}
	}

	if password != dbPwd {
		lessgo.Log.Warn("密码错误:", username, " : ", password)
		return false, employee, "密码错误"
	}

	sql = "select r.role_id,r.level,r.code from employee_role er left join role r on r.role_id=er.role_id where er.user_id=?"
	rows, err = db.Query(sql, employee.UserId)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return false, employee, " 数据库异常!"
	}
	var roleId, roleCode, roleLevel string
	for rows.Next() {

		err := commonlib.PutRecord(rows, &roleId, &roleLevel, &roleCode)
		if err != nil {
			lessgo.Log.Error(err.Error())
			return false, employee, " 数据库异常!"
		}
		employee.RoleId += roleId + ","
		employee.RoleCode += roleCode + ","
		employee.RoleLevel += roleLevel + ","
	}
	lessgo.Log.Info(employee)
	return true, employee, ""
}
示例#21
0
func ClassByCenterIdAction(w http.ResponseWriter, r *http.Request) {

	m := make(map[string]interface{})

	id := r.FormValue("id")

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select class_id,name from wyclass where center_id=? "

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, id)

	if err != nil {
		m["success"] = false
		m["code"] = 100
		m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
		commonlib.OutputJson(w, m, " ")
		return
	}

	classes := []WyClass{}

	for rows.Next() {
		wyClass := WyClass{}

		err := commonlib.PutRecord(rows, &wyClass.ClassId, &wyClass.Name)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			m["success"] = false
			m["code"] = 100
			m["msg"] = "系统发生错误,请联系IT部门"
			commonlib.OutputJson(w, m, " ")
			return
		}

		classes = append(classes, wyClass)
	}

	m["success"] = true
	m["code"] = 200
	m["datas"] = classes

	commonlib.OutputJson(w, m, " ")
}
示例#22
0
func GetFillObjectPage(db *sql.DB, sql string, currPageNo, pageSize, totalNum int, params []interface{}) (*commonlib.TraditionPage, error) {

	rows, err := db.Query(sql, params...)

	if err != nil {
		Log.Error(err.Error())
		return nil, err
	}

	objects := []interface{}{}

	columns, err := rows.Columns()

	if err != nil {
		Log.Error(err.Error())
		return nil, err
	}

	for rows.Next() {

		model := new(Model)

		fillObjects := []interface{}{}

		fillObjects = append(fillObjects, &model.Id)

		for index, column := range columns {
			if index > 0 { //第一个列必须是id
				prop := new(Prop)
				prop.Name = column
				prop.Value = ""
				fillObjects = append(fillObjects, &prop.Value)
				model.Props = append(model.Props, prop)
			}
		}

		err = commonlib.PutRecord(rows, fillObjects...)

		if err != nil {
			Log.Error(err.Error())
			return nil, err
		}

		objects = append(objects, model)
	}

	return commonlib.BulidTraditionPage(currPageNo, pageSize, totalNum, objects), nil
}
示例#23
0
//如果scheduleId=“”,则视为无班签到的判断
func checkSignInExist(childId, scheduleId string) (bool, error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	dataSql := ""

	if scheduleId == "" {
		dataSql = "select count(1) from sign_in where child_id=? and schedule_detail_id is null "
	} else {
		dataSql = "select count(1) from sign_in where child_id=? and schedule_detail_id=? "
	}

	lessgo.Log.Debug(dataSql)

	var rows *sql.Rows
	var err error

	if scheduleId == "" {
		rows, err = db.Query(dataSql, childId)
	} else {
		rows, err = db.Query(dataSql, childId, scheduleId)
	}

	if err != nil {
		lessgo.Log.Error(err.Error())
		return false, err
	}

	num := 0

	if rows.Next() {

		err = commonlib.PutRecord(rows, &num)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return false, err
		}
	}

	if num > 0 {
		return true, nil
	}

	return false, nil
}
示例#24
0
//根据时间key值获取时间区间,false表示没有找到相应的时间区间
func FindRangeTimeDim(yearKey, monthKey, weekKey string) (startTime, endTime string, flag bool) {

	db := GetMySQL()

	defer db.Close()

	sql := ""

	param := ""

	if weekKey != "" {
		sql = "select min(day_date),max(day_date) from time_dim where week_key=?"
		param = weekKey
	} else if monthKey != "" {
		sql = "select min(day_date),max(day_date) from time_dim where month_key=?"
		param = monthKey
	} else {
		sql = "select min(day_date),max(day_date) from time_dim where current_year=?"
		param = yearKey
	}

	Log.Debug(sql)

	rows, err := db.Query(sql, param)

	if err != nil {
		Log.Error(err.Error())
		return "", "", false
	}

	if rows.Next() {
		err := commonlib.PutRecord(rows, &startTime, &endTime)
		if err != nil {
			Log.Warn(err.Error())
			return "", "", false
		}

		if startTime != "" && endTime != "" {
			return startTime, endTime, true
		}

	}

	return "", "", false
}
示例#25
0
func GetGalleryById(id string) (*Gallery, string, error) {
	sql := "select gal.id, gal.title, gal.comment, (select GROUP_CONCAT(category_id) from category_gallery where gallery_id=gal.id) as image_category, gal.image_name, gal.image_suffix from gallery gal where gal.id=?;"
	rows, err := DBSelect(sql, id)
	lessgo.Log.Debug(sql, id)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return nil, "查询信息出错", err
	}
	gallery := new(Gallery)
	if rows.Next() {
		err = commonlib.PutRecord(rows, &gallery.Id, &gallery.Title, &gallery.Comment, &gallery.ImageCategory, &gallery.ImageName, &gallery.ImageSuffix)
		if err != nil {
			lessgo.Log.Error("封装对象数据时发生错误")
			return nil, "封装信息出错", err
		}
	}

	return gallery, "处理成功", nil
}
示例#26
0
func GetSubcategories() (options []*SelectOption, err error) {
	sql := "select id, name from category where parent_code is not null and parent_code <> '';"
	rows, err := DBSelect(sql)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return nil, err
	}

	for rows.Next() {
		option := new(SelectOption)

		err = commonlib.PutRecord(rows, &option.Value, &option.Description)
		if err != nil {
			lessgo.Log.Error("封装对象数据时发生错误")
			return nil, err
		}
		options = append(options, option)
	}

	return options, nil
}
示例#27
0
func checkCourseNameExist(centerId, courseName string) (bool, error) {

	db := lessgo.GetMySQL()
	defer db.Close()

	dataSql := ""

	dataSql = "select count(1) from course where center_id=? and name=?"

	lessgo.Log.Debug(dataSql)

	var rows *sql.Rows
	var err error

	rows, err = db.Query(dataSql, centerId, courseName)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return false, err
	}

	num := 0

	if rows.Next() {

		err = commonlib.PutRecord(rows, &num)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return false, err
		}
	}

	if num > 0 {
		return true, nil
	}

	return false, nil
}
示例#28
0
func checkContractNoExist(contractNo string) (bool, error) {

	if contractNo == "" {
		return false, nil
	}

	db := lessgo.GetMySQL()
	defer db.Close()

	sql := "select count(1) from contract where contract_no=? "

	lessgo.Log.Debug(sql)

	rows, err := db.Query(sql, contractNo)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return false, err
	}

	num := 0

	if rows.Next() {

		err = commonlib.PutRecord(rows, &num)

		if err != nil {
			lessgo.Log.Error(err.Error())
			return false, err
		}
	}

	if num > 0 {
		return true, nil
	}

	return false, nil
}
示例#29
0
func GetDataMap(rows *sql.Rows) (map[string]string, error) {

	type TmpString struct {
		Value string
	}

	dataMap := make(map[string]string)

	columns, err := rows.Columns()

	if err != nil {
		Log.Error(err.Error())
		return nil, err
	}

	objects := []*TmpString{}
	tmpString := []interface{}{}

	for i := 0; i < len(columns); i++ {
		var tmp = new(TmpString)
		objects = append(objects, tmp)
		tmpString = append(tmpString, &tmp.Value)
	}

	err = commonlib.PutRecord(rows, tmpString...)

	if err != nil {
		Log.Error(err.Error())
		return nil, err
	}

	for index, column := range columns {
		dataMap[column] = objects[index].Value
	}

	return dataMap, nil
}
示例#30
0
//顾问分页数据服务
func VideoListAction(w http.ResponseWriter, r *http.Request) {

	m := make(map[string]interface{})

	employee := lessgo.GetCurrentEmployee(r)

	if employee.UserId == "" {
		lessgo.Log.Warn("用户未登陆")
		m["success"] = false
		m["code"] = 100
		m["msg"] = "用户未登陆"
		commonlib.OutputJson(w, m, " ")
		return
	}

	err := r.ParseForm()

	if err != nil {
		m["success"] = false
		m["code"] = 100
		m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
		commonlib.OutputJson(w, m, " ")
		return
	}

	pageNoString := r.FormValue("page")
	pageNo := 1
	if pageNoString != "" {
		pageNo, err = strconv.Atoi(pageNoString)
		if err != nil {
			pageNo = 1
			lessgo.Log.Warn("错误的pageNo:", pageNo)
		}
	}

	pageSizeString := r.FormValue("rows")
	pageSize := 10
	if pageSizeString != "" {
		pageSize, err = strconv.Atoi(pageSizeString)
		if err != nil {
			lessgo.Log.Warn("错误的pageSize:", pageSize)
		}
	}

	dataType := ""

	roleIds := strings.Split(employee.RoleId, ",")

	for _, roleId := range roleIds {
		if roleId == "1" || roleId == "3" || roleId == "6" || roleId == "10" {
			dataType = "all"
			break
		} else if roleId == "2" {
			dataType = "center"
			break
		} else {
			dataType = "self"
		}
	}

	//fmt.Println("dataType:" ,dataType, ",roleIds:", roleIds)
	//r.FormValue("cid-eq")
	//fmt.Println(r.Form)

	sql := ""

	params := []interface{}{}

	if dataType == "all" {
		sql += `select v.vid,v.cid,ce.name as centername,r.name rname,c.course_id,co.name,c.teacher_id,e.really_name,v.start_time,v.end_time from
				video v left join class_schedule_detail c on v.schedule_detail_id=c.id left join room r on r.rid=v.rid left join employee e
					on c.teacher_id=e.user_id left join course co on c.course_id=co.cid left join center ce on v.cid=ce.cid
						where data_rel_status=2`
	} else if dataType == "center" {
		sql += `select v.vid,v.cid,ce.name as centername,r.name rname,c.course_id,co.name,c.teacher_id,e.really_name,v.start_time,v.end_time from
				video v left join class_schedule_detail c on v.schedule_detail_id=c.id left join room r on r.rid=v.rid left join employee e
					on c.teacher_id=e.user_id left join course co on c.course_id=co.cid left join center ce on v.cid=ce.cid
						where v.cid=? and data_rel_status=2`
		userId, _ := strconv.Atoi(employee.UserId)
		_employee, err := FindEmployeeById(userId)
		if err != nil {
			m["success"] = false
			m["code"] = 100
			m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
			commonlib.OutputJson(w, m, " ")
			return
		}

		params = append(params, _employee.CenterId)

	} else if dataType == "self" {
		sql += `select v.vid,v.cid,ce.name as centername,r.name rname,c.course_id,co.name,c.teacher_id,e.really_name,v.start_time,v.end_time from
			video v left join class_schedule_detail c on v.schedule_detail_id=c.id left join room r on r.rid=v.rid left join employee e
				on c.teacher_id=e.user_id left join course co on c.course_id=co.cid left join center ce on v.cid=ce.cid
					where c.teacher_id=? and data_rel_status=2`

		params = append(params, employee.UserId)
	}

	center_id := r.FormValue("cid-eq")
	if center_id != "" {
		sql += " and v.cid=?"
		params = append(params, center_id)
		lessgo.Log.Debug(sql)
		lessgo.Log.Debug(params)
	}

	course_id := r.FormValue("courseId-eq")
	if course_id != "" {
		sql += " and c.course_id=?"
		params = append(params, course_id)
		lessgo.Log.Debug(params...)
	}

	name := r.FormValue("name-like")
	if name != "" {
		sql += " and e.really_name like ?"
		params = append(params, "%"+name+"%")
	}

	countSql := "select count(1) from (" + sql + ") num"

	lessgo.Log.Debug(countSql)

	db := lessgo.GetMySQL()
	defer db.Close()

	rows, err := db.Query(countSql, params...)
	if err != nil {
		lessgo.Log.Warn(err.Error())
		m["success"] = false
		m["code"] = 100
		m["msg"] = "系统发生错误,请联系IT部门"
		commonlib.OutputJson(w, m, " ")
		return
	}

	totalNum := 0

	if rows.Next() {
		err := rows.Scan(&totalNum)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			m["success"] = false
			m["code"] = 100
			m["msg"] = "系统发生错误,请联系IT部门"
			commonlib.OutputJson(w, m, " ")
			return
		}
	}

	totalPage := int(math.Ceil(float64(totalNum) / float64(pageSize)))

	currPageNo := pageNo

	if currPageNo > totalPage {
		currPageNo = totalPage
	}

	params = append(params, (currPageNo-1)*pageSize)
	params = append(params, pageSize)

	lessgo.Log.Debug(sql + " limit ?,?")
	lessgo.Log.Debug(params...)
	lessgo.Log.Debug(params[0], ":", params[1])
	rows, err = db.Query(sql+" limit ?,?", params...)

	if err != nil {
		lessgo.Log.Warn(err.Error())
		m["success"] = false
		m["code"] = 100
		m["msg"] = "系统发生错误,请联系IT部门"
		commonlib.OutputJson(w, m, " ")
		return
	}
	colums, _ := rows.Columns()
	column_len := len(colums) - 1
	objects := []interface{}{}

	for rows.Next() {

		model := new(lessgo.Model)

		fillObjects := []interface{}{}

		fillObjects = append(fillObjects, &model.Id)

		for i := 0; i < column_len; i++ {
			prop := new(lessgo.Prop)
			prop.Name = fmt.Sprint(i)
			prop.Value = ""
			fillObjects = append(fillObjects, &prop.Value)
			model.Props = append(model.Props, prop)
		}

		err = commonlib.PutRecord(rows, fillObjects...)

		if err != nil {
			lessgo.Log.Warn(err.Error())
			m["success"] = false
			m["code"] = 100
			m["msg"] = "系统发生错误,请联系IT部门"
			commonlib.OutputJson(w, m, " ")
			return
		}

		objects = append(objects, model)
	}

	pageData := commonlib.BulidTraditionPage(currPageNo, pageSize, totalNum, objects)

	m["PageData"] = pageData
	m["DataLength"] = len(pageData.Datas) - 1
	if len(pageData.Datas) > 0 {
		m["FieldLength"] = len(pageData.Datas[0].(*lessgo.Model).Props) - 1
	}

	commonlib.RenderTemplate(w, r, "entity_page.json", m, template.FuncMap{"getPropValue": lessgo.GetPropValue, "compareInt": lessgo.CompareInt, "dealJsonString": lessgo.DealJsonString}, "../lessgo/template/entity_page.json")

}