You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package task
import (
"encoding/base64"
"fmt"
mysql2 "git.oa00.com/go/mysql"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/gin-gonic/gin"
"gopkg.in/gomail.v2"
"path/filepath"
"recook/configs"
"recook/internal/static_path"
"recook/internal/v2/lib/back"
"recook/internal/v2/model/recook/goods"
"recook/tools"
"strconv"
"time"
)
type PushNewGoods struct {
}
//邮件提醒
type newResponse struct {
GysName string `json:"gys_name"`
GoodsName string `json:"goods_name"`
Coke string `json:"coke"`
SkuID uint `json:"sku_id"`
}
// SendEmail @Title 发送邮件
func (p *PushNewGoods) SendEmail(email string, one []newResponse) error {
m := gomail.NewMessage()
m.SetHeader("From", configs.Config_Gys_Email_Setting_Mail)
m.SetHeader("To", email)
//m.SetHeader("To", "792209833@qq.com")
m.SetHeader("Subject", "未设置售价商品通知")
if (len(one)) <= 10 {
str := "<br/><br/>\n您好\n<br/><br/>"
if len(one) > 0 {
for _, v := range one {
//fmt.Printf("%v:%v,%v,%v,%v\n", i, v.GysName, v.GoodsName, v.Coke, v.SkuID)
str += v.GysName + "," + v.GoodsName + "," + v.Coke + "," + fmt.Sprintf("%v", v.SkuID) + "<br/><br/>"
}
}
m.SetBody("text/html", str)
} else {
str := "具体数据请看表格"
m.SetBody("text/html", str)
// 把资料整合成Excel
f := excelize.NewFile()
// Create a new sheet.
//index2 := f.NewSheet("Sheet2")
f.SetColWidth("Sheet1", "A", "A", 35)
f.SetColWidth("Sheet1", "B", "B", 50)
f.SetColWidth("Sheet1", "C", "C", 20)
f.SetCellValue("Sheet1", "A1", "供应商")
f.SetCellValue("Sheet1", "B1", "商品名称")
f.SetCellValue("Sheet1", "C1", "条形码")
f.SetCellValue("Sheet1", "D1", "skuId")
for i, v := range one {
fmt.Printf("%v:%v,%v,%v,%v\n", i, v.GysName, v.GoodsName, v.Coke, v.SkuID)
f.SetCellValue("Sheet1", "A"+strconv.Itoa(i+2), v.GysName)
f.SetCellValue("Sheet1", "B"+strconv.Itoa(i+2), v.GoodsName)
f.SetCellValue("Sheet1", "C"+strconv.Itoa(i+2), v.Coke)
f.SetCellValue("Sheet1", "D"+strconv.Itoa(i+2), v.SkuID)
}
//f.SetActiveSheet(index2)
name := time.Now().Format("20060102-150405") + tools.GenerateGoodsHashSign() + ".xlsx"
f.Path = filepath.Join(static_path.Dir.Root, static_path.Dir.Temp, name)
if err := f.SaveAs(f.Path); err != nil {
fmt.Println(err)
}
fmt.Println(f.Path)
m.Attach(f.Path, gomail.Rename("=?utf-8?B?"+base64.StdEncoding.EncodeToString([]byte("未设置售价商品通知.xlsx"))+"?="))
}
d := gomail.NewDialer(configs.Config_Gys_Email_Setting_Smtp, configs.Config_Gys_Email_Setting_Port, configs.Config_Gys_Email_Setting_User, configs.Config_Gys_Email_Setting_Pass)
if err := d.DialAndSend(m); err != nil {
return err
}
return nil
}
// SendNewGoods @Title 查询上架价格为0的商品 发送邮件
func (p *PushNewGoods) SendNewGoods(c *gin.Context) {
var goodsSkuModels []goods.RecookGoodsSkuModel
var goodsList []newResponse
mysql2.Db.Joins("Goods").Preload("Goods.Vendor").Where("discount_price = 0 and publish_status = ?", goods.RecookGoodsInfoPublishStatusTrue).Find(&goodsSkuModels)
for _, recookGoodsSkuModel := range goodsSkuModels {
goodsList = append(goodsList, newResponse{
GysName: recookGoodsSkuModel.Goods.Vendor.EnterpriseName,
GoodsName: recookGoodsSkuModel.Goods.GoodsName,
Coke: recookGoodsSkuModel.Code,
SkuID: recookGoodsSkuModel.Id,
})
}
err := p.SendEmail(configs.ConfigJTEmail, goodsList)
if err != nil {
return
}
back.Suc(c, "邮件已发送", "")
return
}