|
|
@ -109,11 +109,13 @@ func (s *sCouponUsers) DeleteCouponUserByJwcode(ctx context.Context, couponId, j |
|
|
|
} |
|
|
|
|
|
|
|
// 通过excel导入精网号
|
|
|
|
func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) { |
|
|
|
func (s *sCouponUsers) InsertJwcodeByExcel(ctx context.Context, file multipart.File, couponId int) (num int, err error) { |
|
|
|
// func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) {
|
|
|
|
|
|
|
|
//打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
|
|
|
|
f, err := excelize.OpenReader(file) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("打开文件失败") |
|
|
|
return 0, errors.New("打开文件失败") |
|
|
|
} |
|
|
|
// 延时关闭文件
|
|
|
|
defer func(f *excelize.File) { |
|
|
@ -126,15 +128,16 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, |
|
|
|
//读取所有工作表名称
|
|
|
|
GetSheetMap := f.GetSheetMap() |
|
|
|
if len(GetSheetMap) == 0 { |
|
|
|
return nil, errors.New("没有工作表") |
|
|
|
return 0, errors.New("没有工作表") |
|
|
|
} |
|
|
|
|
|
|
|
rows, err := f.GetRows("Sheet1") |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return 0, err |
|
|
|
} |
|
|
|
|
|
|
|
// 将每行的第一列转换为int,并添加到切片中
|
|
|
|
var jwcodes []int |
|
|
|
for i, row := range rows { |
|
|
|
//跳过第一行
|
|
|
|
if i == 0 { |
|
|
@ -154,15 +157,46 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, |
|
|
|
//将字符串转换为整数
|
|
|
|
jwcode, err := strconv.Atoi(jwcodeStr) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("参数转换失败") |
|
|
|
return 0, errors.New("参数转换失败") |
|
|
|
} |
|
|
|
jwcodes = append(jwcodes, jwcode) |
|
|
|
/*参数校验*/ |
|
|
|
} |
|
|
|
return //返回jwcodes切片
|
|
|
|
|
|
|
|
/*给用户发放卡券*/ |
|
|
|
num, err = s.InsertCouponUsersByJwcodes(ctx, jwcodes, couponId) |
|
|
|
/*给用户发放卡券*/ |
|
|
|
|
|
|
|
//return //返回jwcodes切片
|
|
|
|
return num, err |
|
|
|
} |
|
|
|
|
|
|
|
// 传来jwcodes字符串,解析并发放卡券
|
|
|
|
func (s *sCouponUsers) InsertCouponUsersByJwcodeStr(ctx context.Context, jwcodeStr string, couponId int) (num int, err error) { |
|
|
|
//将字符串转换为切片
|
|
|
|
jwcodes := strings.Split(jwcodeStr, ",") |
|
|
|
//转换成int
|
|
|
|
var jwcodesInt []int |
|
|
|
for _, jwcode := range jwcodes { |
|
|
|
//参数校验,检查jwcode是否为非空字符串
|
|
|
|
if jwcode == "" { |
|
|
|
continue |
|
|
|
} |
|
|
|
jwcodeInt, err := strconv.Atoi(jwcode) |
|
|
|
if err != nil { |
|
|
|
return 0, errors.New("参数转换失败") |
|
|
|
} |
|
|
|
jwcodesInt = append(jwcodesInt, jwcodeInt) |
|
|
|
} |
|
|
|
|
|
|
|
/*给用户发放卡券*/ |
|
|
|
num, err = s.InsertCouponUsersByJwcodes(ctx, jwcodesInt, couponId) |
|
|
|
/*给用户发放卡券*/ |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// 根据精网号发放用户优惠券
|
|
|
|
// 根据精网号发放用户优惠券,群发 //不被controller层调用了,但不能删除
|
|
|
|
func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) { |
|
|
|
//去重
|
|
|
|
m := make(map[int]bool) |
|
|
|