survey.go 8.16 KB
Newer Older
haoyanbin's avatar
1  
haoyanbin committed
1 2 3
package mobile

import (
haoyanbin's avatar
haoyanbin committed
4
	"fmt"
haoyanbin's avatar
1  
haoyanbin committed
5 6 7 8 9 10 11
	"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"
haoyanbin's avatar
haoyanbin committed
12
	"github.com/xuri/excelize/v2"
haoyanbin's avatar
1  
haoyanbin committed
13
	"go.uber.org/zap"
haoyanbin's avatar
haoyanbin committed
14 15
	"strconv"
	"time"
haoyanbin's avatar
1  
haoyanbin committed
16 17 18 19 20 21
)

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

haoyanbin's avatar
haoyanbin committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	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)
	}
haoyanbin's avatar
1  
haoyanbin committed
53 54 55 56 57 58 59 60
	return
}

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

	SurveyUser := model.SurveyUser{
haoyanbin's avatar
1  
haoyanbin committed
61
		Id:              req.Id,
haoyanbin's avatar
haoyanbin committed
62 63 64 65
		Contacts:        req.Contacts,
		ContactsMobile:  req.ContactsMobile,
		Reference:       req.Reference,
		ReferenceMobile: req.ReferenceMobile,
haoyanbin's avatar
1  
haoyanbin committed
66
		Status:          1,
haoyanbin's avatar
1  
haoyanbin committed
67 68
	}

haoyanbin's avatar
1  
haoyanbin committed
69 70 71 72 73 74
	service.UpdateSurveyUser(SurveyUser)

	//for _, v := range req.Data {
	//	v.SurveyUserId = surveyUserId
	//	service.CreateSurveyUserData(v)
	//}
haoyanbin's avatar
1  
haoyanbin committed
75

haoyanbin's avatar
1  
haoyanbin committed
76 77 78 79 80 81 82
	response.OkWithMessage("修改成功", c)
	return
}

func CreateSurveyUserData(c *gin.Context) {
	var req request.CreateSurveyUserReq
	_ = c.ShouldBindJSON(&req)
haoyanbin's avatar
1  
haoyanbin committed
83
	for _, v := range req.Data {
haoyanbin's avatar
1  
haoyanbin committed
84
		v.SurveyUserId = req.Id
haoyanbin's avatar
1  
haoyanbin committed
85 86
		service.CreateSurveyUserData(v)
	}
haoyanbin's avatar
1  
haoyanbin committed
87
	response.OkWithMessage("修改成功", c)
haoyanbin's avatar
1  
haoyanbin committed
88 89 90 91 92 93
	return
}

func CreateSurveyLog(c *gin.Context) {
	var req model.SurveyLog
	_ = c.ShouldBindJSON(&req)
haoyanbin's avatar
1  
haoyanbin committed
94

haoyanbin's avatar
1  
haoyanbin committed
95 96
	ipData, _ := service.GetIpaddr(c.ClientIP())
	//ipData, _ := service.GetIpaddr("125.34.219.138")
haoyanbin's avatar
1  
haoyanbin committed
97 98 99 100 101 102
	//fmt.Println(ipData)

	req.Country = ipData.Country
	req.Area = ipData.Area
	req.Region = ipData.Region
	req.City = ipData.City
haoyanbin's avatar
1  
haoyanbin committed
103

haoyanbin's avatar
1  
haoyanbin committed
104
	req.CreateTime = utils.NowTime()
haoyanbin's avatar
1  
haoyanbin committed
105
	req.Ip = c.ClientIP()
haoyanbin's avatar
1  
haoyanbin committed
106 107 108 109 110 111 112 113
	if err := service.CreateSurveyLog(req); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
	return
}
haoyanbin's avatar
haoyanbin committed
114 115 116 117 118

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

haoyanbin's avatar
1  
haoyanbin committed
119 120
	mocData := service.GetMoc(1)

haoyanbin's avatar
haoyanbin committed
121
	req.PageSize = 99999
haoyanbin's avatar
haoyanbin committed
122
	req.Status = 1
haoyanbin's avatar
haoyanbin committed
123 124 125

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

haoyanbin's avatar
haoyanbin committed
126
	fileName := "留资信息列表" + time.Now().Format("20060102")
haoyanbin's avatar
haoyanbin committed
127 128 129 130
	f := excelize.NewFile()
	// Create a new sheet.
	index := f.NewSheet(fileName)

haoyanbin's avatar
1  
haoyanbin committed
131
	f.MergeCell(fileName, "A1", "H1")
haoyanbin's avatar
haoyanbin committed
132
	f.SetCellValue(fileName, "A1", "留资信息")
haoyanbin's avatar
1  
haoyanbin committed
133 134 135 136
	f.MergeCell(fileName, "I1", "S1")
	f.SetCellValue(fileName, "I1", "电销沟通结果")
	f.MergeCell(fileName, "T1", "X1")
	f.SetCellValue(fileName, "T1", "渠道信息")
haoyanbin's avatar
haoyanbin committed
137 138 139

	f.SetCellValue(fileName, "A2", "联系人")
	f.SetCellValue(fileName, "B2", "联系电话")
haoyanbin's avatar
1  
haoyanbin committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	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", "渠道链接")
haoyanbin's avatar
haoyanbin committed
162 163 164

	for k, v := range list {

haoyanbin's avatar
haoyanbin committed
165 166 167 168 169 170 171
		surveyData := service.GetSurveyUserDataList(v.Id)

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

haoyanbin's avatar
1  
haoyanbin committed
172 173 174 175 176 177 178 179 180 181
		region := v.CRegion
		if region == "" {
			region = v.Region
		}

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

haoyanbin's avatar
haoyanbin committed
182 183 184 185 186
		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)
haoyanbin's avatar
1  
haoyanbin committed
187 188 189 190 191
		f.SetCellValue(fileName, "C"+a, region)
		f.SetCellValue(fileName, "D"+a, city)
		f.SetCellValue(fileName, "E"+a, v.CZone)
		f.SetCellValue(fileName, "F"+a, v.CreateTime)
		f.SetCellValue(fileName, "G"+a, optionData)
haoyanbin's avatar
haoyanbin committed
192 193 194 195 196 197 198 199 200
		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, "")
haoyanbin's avatar
1  
haoyanbin committed
201 202 203 204 205 206 207 208
		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)
haoyanbin's avatar
haoyanbin committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	}

	// 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
}
haoyanbin's avatar
1  
haoyanbin committed
224 225 226 227 228

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

haoyanbin's avatar
1  
haoyanbin committed
229 230
	mocData := service.GetMoc(1)

haoyanbin's avatar
1  
haoyanbin committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	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", "获客单价")

haoyanbin's avatar
1  
haoyanbin committed
248
	for k, v := range mocData {
haoyanbin's avatar
1  
haoyanbin committed
249 250 251
		req.MocId = k
		req.Status = 1
		surveyCount := service.GetSurveyUserCount(req) //留资数量
haoyanbin's avatar
1  
haoyanbin committed
252

haoyanbin's avatar
1  
haoyanbin committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
		logCountReq := request.GetSurveyLogListReq{
			SlPage:  10,
			MocId:   k,
			Groupby: " user_id ",
		}
		logCount := service.GetSurveyLogCount(logCountReq) //点击量

		a := strconv.Itoa(k + 1)
		f.SetCellValue(fileName, "A"+a, time.Now().Format("2006/01/02"))
		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, "")
	}
haoyanbin's avatar
1  
haoyanbin committed
273 274 275 276 277 278 279 280 281 282 283 284 285
	// 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
}