survey.go 5.09 KB
package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"strconv"
)

func CreateSurveyUser(req model.SurveyUser) (error, int) {
	err := global.GVA_DB.Table("survey_user").Create(&req).Error
	return err, req.Id
}

func UpdateSurveyUser(req model.SurveyUser) (error, int) {
	err := global.GVA_DB.Table("survey_user").Where("id=?", req.Id).Updates(&req).Error
	return err, req.Id
}

func CreateSurveyUserData(req model.SurveyUserData) error {
	err := global.GVA_DB.Table("survey_user_data").Create(&req).Error
	return err
}

func CreateSurveyLog(req model.SurveyLog) error {
	err := global.GVA_DB.Table("survey_log").Create(&req).Error
	return err
}

func GetSurveyUserList(req request.GetSurveyUserListReq) (error, []request.GetSurveyUserList, int64) {
	pagesize := 10
	page := 1
	if req.PageSize != 0 {
		pagesize = req.PageSize
	}
	if req.Page != 0 {
		page = req.Page
	}
	currentpage := pagesize * (page - 1)

	list := make([]request.GetSurveyUserList, 0)

	table := " survey_user as sur " +
		" left join customer_user as cu on sur.user_id=cu.id " +
		" left join vcode as vc on sur.user_id=vc.user_id and sur.moc_id = vc.moc_id "

	field := " sur.id, sur.user_id, sur.moc_id, sur.code_mobile, sur.code, sur.contacts, sur.contacts_mobile" +
		", sur.reference, sur.reference_mobile, sur.create_time" +
		", sur.country, sur.area, sur.region, sur.city" +
		", cu.worker_name, cu.worker_mobile " +
		", vc.code as vcode "

	conditions := ""

	orderby := " sur.create_time desc "

	if req.MocId != 0 {
		conditions += " AND sur.moc_id = " + strconv.Itoa(req.MocId)
	}

	if req.Status != 0 {
		conditions += " AND sur.status = " + strconv.Itoa(req.Status)
	}

	if req.Region != "" {
		conditions += " AND sur.region like '%" + req.Region + "%'"
	}

	if req.City != "" {
		conditions += " AND sur.city like '%" + req.City + "%'"
	}

	if req.Contacts != "" {
		conditions += " AND sur.contacts like '%" + req.Contacts + "%'"
	}

	if req.ContactsMobile != "" {
		conditions += " AND sur.contacts_mobile like '%" + req.ContactsMobile + "%'"
	}

	if req.Reference != "" {
		conditions += " AND sur.reference like '%" + req.Reference + "%'"
	}

	if req.ReferenceMobile != "" {
		conditions += " AND sur.reference_mobile like '%" + req.ReferenceMobile + "%'"
	}

	if req.StartCreateTime != "" {
		conditions += " AND sur.create_time >'" + req.StartCreateTime + "'"
	}

	if req.EndCreateTime != "" {
		conditions += " AND sur.create_time <='" + req.EndCreateTime + "'"
	}

	//@@总条数,总页数
	var totalItem int64 = 0

	sqlStr := "SELECT count(sur.id) as totalItem FROM " + table + " where 1=1 " + conditions
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数

	sqlStr2 := "SELECT " + field +
		" FROM " + table +
		" WHERE 1>0 " + conditions +
		" ORDER BY " + orderby +
		" LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize)

	global.GVA_DB.Raw(sqlStr2).Scan(&list)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil, 0
	}
	return nil, list, totalItem
}

func GetSurveyUserDataList(id int) []model.SurveyUserData {

	list := make([]model.SurveyUserData, 0)

	table := " survey_user_data "

	field := " survey_user_id, `option`, option_value, option_num, price "

	conditions := " AND survey_user_id = " + strconv.Itoa(id)

	sqlStr2 := "SELECT " + field +
		" FROM " + table +
		" WHERE 1>0 " + conditions

	global.GVA_DB.Raw(sqlStr2).Scan(&list)
	if global.GVA_DB.Error != nil {
		return []model.SurveyUserData{}
	}
	return list
}

func GetSurveyLogList(req request.GetSurveyLogListReq) (error, []request.GetSurveyLogList, int64) {
	pagesize := 10
	page := 1
	if req.PageSize != 0 {
		pagesize = req.PageSize
	}
	if req.Page != 0 {
		page = req.Page
	}
	currentpage := pagesize * (page - 1)

	list := make([]request.GetSurveyLogList, 0)

	table := " survey_log as sl " +
		" left join customer_user as cu on sur.user_id=cu.id "

	field := " sur.id, sur.user_id, sur.moc_id, sur.contacts, sur.contacts_mobile" +
		", sur.reference, sur.reference_mobile, sur.create_time" +
		", sur.country, sur.area, sur.region, sur.city" +
		", cu.worker_name, cu.worker_mobile "

	conditions := ""

	orderby := " sur.create_time desc "

	if req.MocId != 0 {
		conditions += " AND sur.moc_id = " + strconv.Itoa(currentpage)
	}

	if req.Region != "" {
		conditions += " AND region like '%" + req.Region + "%'"
	}

	if req.City != "" {
		conditions += " AND city like '%" + req.City + "%'"
	}

	if req.StartCreateTime != "" {
		conditions += " AND sur.create_time >'" + req.StartCreateTime + "'"
	}

	if req.EndCreateTime != "" {
		conditions += " AND sur.create_time <='" + req.EndCreateTime + "'"
	}

	//@@总条数,总页数
	var totalItem int64 = 0

	sqlStr := "SELECT count(vo.id) as totalItem FROM " + table + " where 1=1 " + conditions
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数

	sqlStr2 := "SELECT " + field +
		" FROM " + table +
		" WHERE 1>0 " + conditions +
		" ORDER BY " + orderby +
		" LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize)

	global.GVA_DB.Raw(sqlStr2).Scan(&list)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil, 0
	}
	return nil, list, totalItem
}