package mobile

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

func GetCustomerUserList(c *gin.Context) {
	var req request.GetCustomerUserListReq
	_ = c.ShouldBindQuery(&req)
	err, list, total := service.GetCustomerUserList(req)
	reply := request.GetCustomerUserListReply{}
	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 ExportMsgExcel(c *gin.Context) {
	reqData := request.GetCustomerUserListReq{}
	_ = c.ShouldBindQuery(&reqData)
	reqData.MocId = 1

	fileName := "用户短链列表" + time.Now().Format("20060102")

	//region := []string{"广东", "福建", "江西", "安徽", "广西", "新疆", "天津", "山西", "贵州", "吉林", "北京", "上海", "黑龙江"}
	regionIn := "'广东','福建','江西','安徽','广西','新疆','天津','山西','贵州','吉林','北京','上海','黑龙江'"

	f := excelize.NewFile()

	codeList := service.GetVcodeList(reqData.MocId)

	//for _, v := range region {
	req := request.GetCustomerUserListReq{}
	req.RegionIn = regionIn
	//req.UserType = 2

	req.PageSize = 99999

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

	sheetName := "用户短链列表"
	// Create a new sheet.
	index := f.NewSheet(sheetName)

	f.SetCellValue(sheetName, "A1", "用户ID")
	f.SetCellValue(sheetName, "B1", "用户名称")
	f.SetCellValue(sheetName, "C1", "省")
	f.SetCellValue(sheetName, "D1", "市")
	f.SetCellValue(sheetName, "E1", "区")
	f.SetCellValue(sheetName, "F1", "工作人员")
	f.SetCellValue(sheetName, "G1", "工作人员电话")
	f.SetCellValue(sheetName, "H1", "是否识别")
	f.SetCellValue(sheetName, "I1", "短链")

	for k, v := range list {
		url := ""
		code, ok := codeList[v.Id]
		if ok == true {
			url = global.GVA_CONFIG.Common.Url + "/" + code
		} else {
			url = global.GVA_CONFIG.Common.Url + "/" + service.GetVcode(v.Id, req.MocId)
		}

		userType := "未识别"
		if v.UserType == 1 {
			userType = "已识别"
		}

		a := strconv.Itoa(k + 2)
		f.SetCellValue(sheetName, "A"+a, v.Id)
		f.SetCellValue(sheetName, "B"+a, v.UserName)
		f.SetCellValue(sheetName, "C"+a, v.Region)
		f.SetCellValue(sheetName, "D"+a, v.City)
		f.SetCellValue(sheetName, "E"+a, v.Zone)
		f.SetCellValue(sheetName, "F"+a, v.WorkerName)
		f.SetCellValue(sheetName, "G"+a, v.WorkerMobile)
		f.SetCellValue(sheetName, "H"+a, userType)
		f.SetCellValue(sheetName, "I"+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 msgExcel(req request.GetCustomerUserListReq) {

}