package v1

import (
	"fmt"
	"gin-vue-admin/global"
	"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 GetMobileUserList(c *gin.Context) {
	var req request.GetMobileUserListReq
	_ = c.ShouldBindJSON(&req)
	req.IsManager = "0"
	req.UserType = global.GVA_CONFIG.System.Usertype
	if err, list, total := service.GetMobileUserList(req); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     req.Page,
			PageSize: req.PageSize,
		}, "获取成功", c)
	}
}

func GetMobileUserInfo(c *gin.Context) {
	var req request.GetMobileUserInfoReq
	_ = c.ShouldBindJSON(&req)
	if err, list := service.GetMobileUserInfo(req.Id); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(list, c)
	}
}

func ExportMobileUser(c *gin.Context) {
	var req request.GetMobileUserListReq
	_ = c.ShouldBindQuery(&req)
	req.Page = 1
	req.PageSize = 9999
	req.IsManager = "0"
	req.UserType = global.GVA_CONFIG.System.Usertype
	_, reply, _ := service.GetMobileUserList(req)

	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", "详细地址")
	f.SetCellValue(fileName, "L1", "最新开通vip时间")
	f.SetCellValue(fileName, "M1", "vip到期时间")
	f.SetCellValue(fileName, "N1", "vip开通方式")

	for k, v := range reply {

		level := ""
		if v.VipLevel == "1" {
			level = "体验版"
		} else if v.VipLevel == "2" {
			level = "赠送版"
		} else if v.VipLevel == "3" {
			level = "付费版"
		}

		a := strconv.Itoa(k + 2)
		f.SetCellValue(fileName, "A"+a, v.Mobile)
		f.SetCellValue(fileName, "B"+a, utils.String2Time(v.CreatedAt))
		f.SetCellValue(fileName, "C"+a, v.Name)
		f.SetCellValue(fileName, "D"+a, v.Phone)
		f.SetCellValue(fileName, "E"+a, v.WorkingYears)
		f.SetCellValue(fileName, "F"+a, v.WorkingPlace)
		f.SetCellValue(fileName, "G"+a, v.Position)
		f.SetCellValue(fileName, "H"+a, v.ProvinceName)
		f.SetCellValue(fileName, "I"+a, v.CityName)
		f.SetCellValue(fileName, "J"+a, v.CountyName)
		f.SetCellValue(fileName, "K"+a, v.HospitalAddress)
		f.SetCellValue(fileName, "L"+a, utils.String2Time(v.UpdateTime))
		f.SetCellValue(fileName, "M"+a, utils.String2Time(v.MaturityTime))
		f.SetCellValue(fileName, "N"+a, level)
	}

	// 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
}
