package mobile

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"github.com/xuri/excelize/v2"
	"go.uber.org/zap"
	"strconv"
	"time"
)

func GetSurveyUserList(c *gin.Context) {
	var req request.GetSurveyUserListReq
	_ = c.ShouldBindJSON(&req)

	err, list, total := service.GetSurveyUserList(req)

	reply := request.GetSurveyUserListReply{}
	reply.List = list
	reply.Total = total

	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithDetailed(reply, "获取成功", c)
	}
	return
}

func GetSurveyLogList(c *gin.Context) {
	var req request.GetSurveyLogListReq
	_ = c.ShouldBindJSON(&req)

	err, list, total := service.GetSurveyLogList(req)

	reply := request.GetSurveyLogListReply{}
	reply.List = list
	reply.Total = total

	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithDetailed(reply, "获取成功", c)
	}
	return
}

func CreateSurveyUser(c *gin.Context) {
	var req request.CreateSurveyUserReq
	_ = c.ShouldBindJSON(&req)

	SurveyUser := model.SurveyUser{
		Id:              req.Id,
		Contacts:        req.Contacts,
		ContactsMobile:  req.ContactsMobile,
		Reference:       req.Reference,
		ReferenceMobile: req.ReferenceMobile,
		Status:          1,
	}

	service.UpdateSurveyUser(SurveyUser)

	//for _, v := range req.Data {
	//	v.SurveyUserId = surveyUserId
	//	service.CreateSurveyUserData(v)
	//}

	response.OkWithMessage("修改成功", c)
	return
}

func CreateSurveyUserData(c *gin.Context) {
	var req request.CreateSurveyUserReq
	_ = c.ShouldBindJSON(&req)
	for _, v := range req.Data {
		v.SurveyUserId = req.Id
		service.CreateSurveyUserData(v)
	}
	response.OkWithMessage("修改成功", c)
	return
}

func CreateSurveyLog(c *gin.Context) {
	var req model.SurveyLog
	_ = c.ShouldBindJSON(&req)

	ipData, _ := service.GetIpaddr(c.ClientIP())
	//ipData, _ := service.GetIpaddr("125.34.219.138")
	//fmt.Println(ipData)

	req.Country = ipData.Country
	req.Area = ipData.Area
	req.Region = ipData.Region
	req.City = ipData.City

	req.CreateTime = utils.NowTime()
	req.Ip = c.ClientIP()
	if err := service.CreateSurveyLog(req); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
	return
}

func ExportSurveyUserExcel(c *gin.Context) {
	var req request.GetSurveyUserListReq
	_ = c.ShouldBindQuery(&req)

	mocData := service.GetMoc(1)

	req.PageSize = 99999
	req.Status = 1

	_, list, _ := service.GetSurveyUserList(req)

	fileName := "留资信息列表" + time.Now().Format("20060102")
	f := excelize.NewFile()
	// Create a new sheet.
	index := f.NewSheet(fileName)

	f.MergeCell(fileName, "A1", "H1")
	f.SetCellValue(fileName, "A1", "留资信息")
	f.MergeCell(fileName, "I1", "S1")
	f.SetCellValue(fileName, "I1", "电销沟通结果")
	f.MergeCell(fileName, "T1", "X1")
	f.SetCellValue(fileName, "T1", "渠道信息")

	f.SetCellValue(fileName, "A2", "联系人")
	f.SetCellValue(fileName, "B2", "联系电话")
	f.SetCellValue(fileName, "C2", "省")
	f.SetCellValue(fileName, "D2", "市")
	f.SetCellValue(fileName, "E2", "区")
	f.SetCellValue(fileName, "F2", "留资时间")
	f.SetCellValue(fileName, "G2", "留资选配产品")
	f.SetCellValue(fileName, "H2", "备注")
	f.SetCellValue(fileName, "I2", "电话沟通")
	f.SetCellValue(fileName, "J2", "沟通日期")
	f.SetCellValue(fileName, "K2", "医院名称")
	f.SetCellValue(fileName, "L2", "医院状态")
	f.SetCellValue(fileName, "M2", "开业时间")
	f.SetCellValue(fileName, "N2", "所属省份")
	f.SetCellValue(fileName, "O2", "签单状态")
	f.SetCellValue(fileName, "P2", "转介绍渠道状态")
	f.SetCellValue(fileName, "Q2", "签约金额")
	f.SetCellValue(fileName, "R2", "签约选配产品")
	f.SetCellValue(fileName, "S2", "电销人员")
	f.SetCellValue(fileName, "T2", "转介绍人")
	f.SetCellValue(fileName, "U2", "转介绍人电话")
	f.SetCellValue(fileName, "V2", "渠道类型")
	f.SetCellValue(fileName, "W2", "渠道")
	f.SetCellValue(fileName, "X2", "渠道链接")

	for k, v := range list {

		surveyData := service.GetSurveyUserDataList(v.Id)

		optionData := ""
		for _, v2 := range surveyData {
			optionData += v2.OptionValue + " * " + strconv.Itoa(v2.OptionNum) + "; "
		}

		region := v.CRegion
		if region == "" {
			region = v.Region
		}

		city := v.CCity
		if city == "" {
			city = v.City
		}

		url := global.GVA_CONFIG.Common.Url + "?" + v.Vcode

		a := strconv.Itoa(k + 3)
		f.SetCellValue(fileName, "A"+a, v.Contacts)
		f.SetCellValue(fileName, "B"+a, v.ContactsMobile)
		f.SetCellValue(fileName, "C"+a, region)
		f.SetCellValue(fileName, "D"+a, city)
		f.SetCellValue(fileName, "E"+a, v.CZone)
		f.SetCellValue(fileName, "F"+a, utils.StringToTime(v.CreateTime))
		f.SetCellValue(fileName, "G"+a, optionData)
		f.SetCellValue(fileName, "H"+a, "")
		f.SetCellValue(fileName, "I"+a, "")
		f.SetCellValue(fileName, "J"+a, "")
		f.SetCellValue(fileName, "K"+a, "")
		f.SetCellValue(fileName, "L"+a, "")
		f.SetCellValue(fileName, "M"+a, "")
		f.SetCellValue(fileName, "N"+a, "")
		f.SetCellValue(fileName, "O"+a, "")
		f.SetCellValue(fileName, "P"+a, "")
		f.SetCellValue(fileName, "Q"+a, "")
		f.SetCellValue(fileName, "R"+a, "")
		f.SetCellValue(fileName, "S"+a, "")
		f.SetCellValue(fileName, "T"+a, v.Reference)
		f.SetCellValue(fileName, "U"+a, v.ReferenceMobile)
		f.SetCellValue(fileName, "V"+a, mocData[v.MocId])
		f.SetCellValue(fileName, "W"+a, "短信")
		f.SetCellValue(fileName, "X"+a, url)
	}

	// Set active sheet of the workbook.
	f.SetActiveSheet(index)
	// Save xlsx file by the given path.
	if err := f.SaveAs("./" + fileName + ".csv"); err != nil {
		fmt.Println(err)
		return
	}

	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "./"+fileName+".csv")) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
	c.Writer.Header().Add("Content-Type", "application/octet-stream")
	c.File("./" + fileName + ".csv")
	return
}

func ExportMocDataExcel(c *gin.Context) {
	var req request.GetSurveyUserListReq
	_ = c.ShouldBindQuery(&req)

	mocData := service.GetMoc(1)

	fileName := "数据统计" + time.Now().Format("20060102")
	f := excelize.NewFile()
	// Create a new sheet.
	index := f.NewSheet(fileName)

	f.SetCellValue(fileName, "A1", "日期")
	f.SetCellValue(fileName, "B1", "渠道类型")
	f.SetCellValue(fileName, "C1", "渠道")
	f.SetCellValue(fileName, "D1", "推广链接")
	f.SetCellValue(fileName, "E1", "推广费用")
	f.SetCellValue(fileName, "F1", "曝光量")
	f.SetCellValue(fileName, "G1", "点击量")
	f.SetCellValue(fileName, "H1", "点击率")
	f.SetCellValue(fileName, "I1", "留资数量")
	f.SetCellValue(fileName, "J1", "留资率")
	f.SetCellValue(fileName, "K1", "获客单价")

	for k, v := range mocData {
		req.MocId = k
		req.Status = 1
		surveyCount := service.GetSurveyUserCount(req) //留资数量

		logCountReq := request.GetSurveyLogListReq{
			SlPage:          10,
			MocId:           k,
			StartCreateTime: req.StartCreateTime,
			EndCreateTime:   req.EndCreateTime,
		}
		logCount := service.GetSurveyLogCount(logCountReq) //点击量

		a := strconv.Itoa(k + 1)
		f.SetCellValue(fileName, "A"+a, req.StartCreateTime)
		f.SetCellValue(fileName, "B"+a, v)
		f.SetCellValue(fileName, "C"+a, "短信")
		f.SetCellValue(fileName, "D"+a, "")
		f.SetCellValue(fileName, "E"+a, "")
		f.SetCellValue(fileName, "F"+a, "")
		f.SetCellValue(fileName, "G"+a, logCount[k])
		f.SetCellValue(fileName, "H"+a, "")
		f.SetCellValue(fileName, "I"+a, surveyCount)
		f.SetCellValue(fileName, "J"+a, "")
		f.SetCellValue(fileName, "K"+a, "")
	}
	// Set active sheet of the workbook.
	f.SetActiveSheet(index)
	// Save xlsx file by the given path.
	if err := f.SaveAs("./" + fileName + ".csv"); err != nil {
		fmt.Println(err)
		return
	}

	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "./"+fileName+".csv")) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
	c.Writer.Header().Add("Content-Type", "application/octet-stream")
	c.File("./" + fileName + ".csv")
	return
}

func ExportSurveyLogPageList(c *gin.Context) {
	var req request.GetSurveyLogListReq
	_ = c.ShouldBindQuery(&req)

	list := service.GetSurveyLogPageList(req)

	fileName := "点击明细" + time.Now().Format("20060102")
	f := excelize.NewFile()
	// Create a new sheet.
	index := f.NewSheet(fileName)

	f.SetCellValue(fileName, "A1", "用户ID")
	f.SetCellValue(fileName, "B1", "用户名")
	f.SetCellValue(fileName, "C1", "省")
	f.SetCellValue(fileName, "D1", "市")
	f.SetCellValue(fileName, "E1", "工作人员")
	f.SetCellValue(fileName, "F1", "工作人员电话")
	f.SetCellValue(fileName, "G1", "数据来源")
	f.SetCellValue(fileName, "H1", "访问IP")
	f.SetCellValue(fileName, "I1", "访问时间")
	f.SetCellValue(fileName, "J1", "是否识别")

	for k, v := range list {
		userType := "未识别"
		if v.UserType == 1 {
			userType = "已识别"
		}
		a := strconv.Itoa(k + 2)
		f.SetCellValue(fileName, "A"+a, v.Id)
		f.SetCellValue(fileName, "B"+a, v.UserName)
		f.SetCellValue(fileName, "C"+a, v.Region)
		f.SetCellValue(fileName, "D"+a, v.City)
		f.SetCellValue(fileName, "E"+a, v.WorkerName)
		f.SetCellValue(fileName, "F"+a, v.WorkerMobile)
		f.SetCellValue(fileName, "G"+a, v.UserSource)
		f.SetCellValue(fileName, "H"+a, v.Ip)
		f.SetCellValue(fileName, "I"+a, utils.StringToTime(v.CreateTime))
		f.SetCellValue(fileName, "J"+a, userType)
	}
	// Set active sheet of the workbook.
	f.SetActiveSheet(index)
	// Save xlsx file by the given path.
	if err := f.SaveAs("./" + fileName + ".csv"); err != nil {
		fmt.Println(err)
		return
	}

	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "./"+fileName+".csv")) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
	c.Writer.Header().Add("Content-Type", "application/octet-stream")
	c.File("./" + fileName + ".csv")
	return
}
