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 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 "

	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.Contacts != "" {
		conditions += " AND contacts like '%" + req.Contacts + "%'"
	}

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

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

	if req.ReferenceMobile != "" {
		conditions += " AND 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(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
}

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
}
