package notice

import (
	"applet/app/db"
	"applet/app/e"
	"applet/app/md"
	"applet/app/utils"
	"applet/app/utils/cache"
	"code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
	"code.fnuoos.com/EggPlanet/egg_models.git/src/model"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gin-gonic/gin"
	"time"
)

func AliyunSmsFilePhone(c *gin.Context) {
	file, _, err := c.Request.FormFile("file")
	if err != nil {
		e.OutErr(c, 400, e.NewErr(400, "上传文件错误"))
		return
	}
	// 打开上传的文件
	xlsx, err := excelize.OpenReader(file)
	if err != nil {
		e.OutErr(c, 400, e.NewErr(400, "打开文件失败"))
		return
	}

	rows := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
	phone := make([]string, 0)
	for i, row := range rows {
		// 去掉第一行是excel表头部分
		if i == 0 {
			continue
		}
		phone = append(phone, row[0])
	}
	res := map[string]interface{}{
		"phone": phone,
	}
	e.OutSuc(c, res, nil)
	return
}
func AliyunSmsList(c *gin.Context) {
	var req *md.NoticeAliyunSmsListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	var resp md.NoticeAliyunSmsListResp
	noticeList := make([]md.NoticeAliyunSmsList, 0)
	NewJpushNoticeDb := implement.NewAliyunSmsNoticeDb(db.Db)
	notice, total, _ := NewJpushNoticeDb.FindAliyunSmsNoticeAndTotal(req.Page, req.Limit, "")
	resp.Total = total
	resp.SelectData = []map[string]string{
		{"name": "X小时未签到提醒", "value": "sign"},
		{"name": "注销提醒", "value": "user_delete"},
		{"name": "提现到账提醒", "value": "withdraw"},
		{"name": "X小时未登录提醒", "value": "login"},
		{"name": "升级提醒", "value": "update"},
	}
	if notice != nil {
		for _, v := range *notice {
			tmp := md.NoticeAliyunSmsList{
				Id:        utils.IntToStr(v.Id),
				Title:     v.Title,
				Day:       utils.IntToStr(v.Day),
				NoticeDay: utils.IntToStr(v.NoticeDay),
				Content:   v.Content,
				Type:      v.Type,
				IsShow:    utils.IntToStr(v.IsShow),
			}
			noticeList = append(noticeList, tmp)
		}
	}
	resp.List = noticeList
	e.OutSuc(c, resp, nil)
	return
}
func AliyunSmsSave(c *gin.Context) {
	var req *md.NoticeAliyunSmsSaveReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	var data = new(model.JpushNotice)
	if utils.StrToInt(req.Id) > 0 {
		NewJpushNoticeDb := implement.NewJpushNoticeDb(db.Db)
		notice, _ := NewJpushNoticeDb.GetJpushNotice(req.Id)
		if notice == nil {
			e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
			return
		}
		data = notice
	} else {
		data.CreateAt = time.Now()
		db.Db.Insert(data)
	}
	data.UpdateAt = time.Now()
	data.Content = req.Content
	data.Type = req.Type
	data.Title = req.Title
	data.Day = utils.StrToInt(req.Day)
	data.NoticeDay = utils.StrToInt(req.NoticeDay)
	db.Db.Where("id=?", data.Id).Update(data)
	e.OutSuc(c, "success", nil)
	return
}
func AliyunSmsDel(c *gin.Context) {
	var req *md.NoticeDelReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	db.Db.In("id", req.Id).Delete(&model.AliyunSmsNotice{})
	e.OutSuc(c, "success", nil)
	return
}
func AliyunSmsSaleBase(c *gin.Context) {

	redisConn := cache.GetPool().Get()
	sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
	aliyunSmsNotice := sysCfgDb.SysCfgGetWithDb("aliyun_sms_sale_code")
	res := map[string]string{
		"aliyun_sms_sale_code": aliyunSmsNotice,
	}
	e.OutSuc(c, res, nil)
	return
}
func AliyunSmsSaleSave(c *gin.Context) {
	var req map[string]string
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	redisConn := cache.GetPool().Get()
	sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
	sysCfgDb.SysCfgUpdate("aliyun_sms_sale_code", req["aliyun_sms_sale_code"])
	e.OutSuc(c, "success", nil)
	return
}
func AliyunSmsPushList(c *gin.Context) {
	var req *md.NoticeAliyunSmsListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	var resp md.NoticeAliyunSmsPushListResp
	noticeList := make([]md.NoticeAliyunSmsPushList, 0)
	resp.TargetData = []map[string]string{
		{"name": "全部会员", "value": "0"},
		{"name": "指定会员", "value": "1"},
		{"name": "指定等级", "value": "2"},
		{"name": "指定标签", "value": "3"},
		{"name": "站外手机", "value": "4"},
	}
	NewAliyunSmsRecordDb := implement.NewAliyunSmsRecordDb(db.Db)
	notice, total, _ := NewAliyunSmsRecordDb.FindAliyunSmsRecordAndTotal(req.Page, req.Limit, "")
	resp.Total = total
	if notice != nil {
		for _, v := range *notice {
			tmp := md.NoticeAliyunSmsPushList{
				Id:       utils.IntToStr(v.Id),
				Title:    v.Title,
				Content:  v.Content,
				Target:   v.Target,
				State:    utils.IntToStr(v.State),
				CreateAt: v.CreateAt.Format("2006-01-02 15:04:05"),
			}
			noticeList = append(noticeList, tmp)
		}
	}
	resp.List = noticeList
	e.OutSuc(c, resp, nil)
	return
}
func AliyunSmsPushSave(c *gin.Context) {
	var req *md.NoticeAliyunSmsPushSaveReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	if utils.StrToInt(req.Id) > 0 {
	} else {
		data := &model.AliyunSmsRecord{
			Platform: "all",
			Target:   req.Target,
			Phone:    utils.SerializeStr(req.PhoneList),
			TagId:    utils.SerializeStr(req.TagList),
			Level:    utils.SerializeStr(req.Level),
			Title:    req.Title,
			Content:  req.Content,
			CreateAt: time.Now(),
			UpdateAt: time.Now(),
		}
		db.Db.Insert(data)
	}
	e.OutSuc(c, "success", nil)
	return
}