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, cu.region as c_region, cu.city as c_city, cu.zone as c_zone " +
		", 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 + " 00:00:00'"
	}

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

	//@@总条数，总页数
	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
}

// 留资数量
func GetSurveyUserCount(req request.GetSurveyUserListReq) int64 {
	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 "

	conditions := ""

	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 + " 00:00:00'"
	}

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

	if req.IsReference == 1 {
		conditions += " AND sur.contacts = cu.worker_mobile "
	} else if req.IsReference == 2 {
		conditions += " AND sur.contacts != cu.worker_mobile "
	}

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

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

	if global.GVA_DB.Error != nil {
		return 0
	}
	return totalItem
}

// 点击量
// SELECT  count(sl.page) as cid, sl.moc_id  FROM  (select moc_id, user_id, page, ip from survey_log where create_time > '2023-09-11 00:00:00' and create_time <= '2023-09-11 23:59:59'  AND page = 10 and moc_id != 0 group by ip)sl left join customer_user as cu on sl.user_id = cu.id group by sl.moc_id
func GetSurveyLogCount(req request.GetSurveyLogListReq) map[int]int {
	conditions := ""

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

	if req.SlPage != 0 {
		conditions += " AND page = " + strconv.Itoa(req.SlPage)
	}

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

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

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

	if req.EndCreateTime != "" {
		conditions += " AND create_time <='" + req.EndCreateTime + " 23:59:59'"
	}

	conditions += "  and moc_id != 0 "

	table := " (select moc_id, user_id, page, ip from survey_log WHERE 1>0 " + conditions + " group by ip)sl "
	//table := " survey_log as sl left join customer_user as cu on sl.user_id=cu.id "

	field := " count(sl.page) as cid, sl.moc_id "

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

	sqlStr2 := "SELECT " + field +
		" FROM " + table

	global.GVA_DB.Raw(sqlStr2).Scan(&list)
	if global.GVA_DB.Error != nil {
		return nil
	}
	reply := make(map[int]int, 0)
	for _, v := range list {
		reply[v.MocId] = v.Cid
	}
	return reply
}

// 点击列表
// SELECT  user_id, user_name, region, city, worker_name, worker_mobile, user_source, ip  FROM  (select moc_id, user_id, page, ip from survey_log where create_time > '2023-09-11 00:00:00' and create_time <= '2023-09-11 23:59:59' AND moc_id = 1 AND page = 10 group by ip)sl left join customer_user as cu on sl.user_id = cu.id
func GetSurveyLogPageList(req request.GetSurveyLogListReq) []request.GetSurveyLogPageList {
	conditions := ""

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

	if req.SlPage != 0 {
		conditions += " AND page = " + strconv.Itoa(req.SlPage)
	}

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

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

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

	if req.EndCreateTime != "" {
		conditions += " AND create_time <='" + req.EndCreateTime + " 23:59:59'"
	}

	conditions += "  and moc_id != 0 "

	table := " (select moc_id, user_id, page, ip, create_time from survey_log WHERE 1>0 " + conditions + " group by ip order by create_time asc)sl "
	table += " left join customer_user as cu on sl.user_id = cu.id "

	field := " cu.id, cu.user_name, cu.region, cu.city, cu.worker_name, cu.worker_mobile, cu.user_source, cu.user_type" +
		", sl.ip, sl.create_time "

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

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

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