|
@ -79,13 +79,13 @@ func (s *sCouponUsers) DeleteCouponUserByJwcode(ctx context.Context, couponId, j |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 通过excel导入精网号, 解析并发放卡券
|
|
|
// 通过excel导入精网号, 解析并发放卡券
|
|
|
func (s *sCouponUsers) InsertJwcodeByExcel(ctx context.Context, file multipart.File, couponId int) (num int, err error) { |
|
|
|
|
|
|
|
|
func (s *sCouponUsers) InsertJwcodeByExcel(ctx context.Context, file multipart.File, couponId int) (num, num1 int, err error) { |
|
|
// func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) {
|
|
|
// func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) {
|
|
|
|
|
|
|
|
|
//打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
|
|
|
//打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
|
|
|
f, err := excelize.OpenReader(file) |
|
|
f, err := excelize.OpenReader(file) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0, errors.New("打开文件失败") |
|
|
|
|
|
|
|
|
return 0, 0, errors.New("打开文件失败") |
|
|
} |
|
|
} |
|
|
// 延时关闭文件
|
|
|
// 延时关闭文件
|
|
|
defer func(f *excelize.File) { |
|
|
defer func(f *excelize.File) { |
|
@ -98,12 +98,12 @@ func (s *sCouponUsers) InsertJwcodeByExcel(ctx context.Context, file multipart.F |
|
|
//读取所有工作表名称
|
|
|
//读取所有工作表名称
|
|
|
GetSheetMap := f.GetSheetMap() |
|
|
GetSheetMap := f.GetSheetMap() |
|
|
if len(GetSheetMap) == 0 { |
|
|
if len(GetSheetMap) == 0 { |
|
|
return 0, errors.New("没有工作表") |
|
|
|
|
|
|
|
|
return 0, 0, errors.New("没有工作表") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
rows, err := f.GetRows("Sheet1") |
|
|
rows, err := f.GetRows("Sheet1") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0, err |
|
|
|
|
|
|
|
|
return 0, 0, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 将每行的第一列转换为int,并添加到切片中
|
|
|
// 将每行的第一列转换为int,并添加到切片中
|
|
@ -127,22 +127,22 @@ func (s *sCouponUsers) InsertJwcodeByExcel(ctx context.Context, file multipart.F |
|
|
//将字符串转换为整数
|
|
|
//将字符串转换为整数
|
|
|
jwcode, err := strconv.Atoi(jwcodeStr) |
|
|
jwcode, err := strconv.Atoi(jwcodeStr) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0, errors.New("参数转换失败") |
|
|
|
|
|
|
|
|
return 0, 0, errors.New("参数转换失败") |
|
|
} |
|
|
} |
|
|
jwcodes = append(jwcodes, jwcode) |
|
|
jwcodes = append(jwcodes, jwcode) |
|
|
/*参数校验*/ |
|
|
/*参数校验*/ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*给用户发放卡券*/ |
|
|
/*给用户发放卡券*/ |
|
|
num, err = s.InsertCouponUsersByJwcodes(ctx, jwcodes, couponId) |
|
|
|
|
|
|
|
|
num, num1, err = s.InsertCouponUsersByJwcodes(ctx, jwcodes, couponId) |
|
|
/*给用户发放卡券*/ |
|
|
/*给用户发放卡券*/ |
|
|
|
|
|
|
|
|
//return //返回jwcodes切片
|
|
|
//return //返回jwcodes切片
|
|
|
return num, err |
|
|
|
|
|
|
|
|
return num, num1, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 传来jwcodes字符串,解析并发放卡券
|
|
|
// 传来jwcodes字符串,解析并发放卡券
|
|
|
func (s *sCouponUsers) InsertCouponUsersByJwcodeStr(ctx context.Context, jwcodeStr string, couponId int) (num int, err error) { |
|
|
|
|
|
|
|
|
func (s *sCouponUsers) InsertCouponUsersByJwcodeStr(ctx context.Context, jwcodeStr string, couponId int) (num, num1 int, err error) { |
|
|
//将字符串转换为切片
|
|
|
//将字符串转换为切片
|
|
|
jwcodes := strings.Split(jwcodeStr, ",") |
|
|
jwcodes := strings.Split(jwcodeStr, ",") |
|
|
//转换成int
|
|
|
//转换成int
|
|
@ -154,20 +154,20 @@ func (s *sCouponUsers) InsertCouponUsersByJwcodeStr(ctx context.Context, jwcodeS |
|
|
} |
|
|
} |
|
|
jwcodeInt, err := strconv.Atoi(jwcode) |
|
|
jwcodeInt, err := strconv.Atoi(jwcode) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0, errors.New("参数转换失败") |
|
|
|
|
|
|
|
|
return 0, 0, errors.New("参数转换失败") |
|
|
} |
|
|
} |
|
|
jwcodesInt = append(jwcodesInt, jwcodeInt) |
|
|
jwcodesInt = append(jwcodesInt, jwcodeInt) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*给用户发放卡券*/ |
|
|
/*给用户发放卡券*/ |
|
|
num, err = s.InsertCouponUsersByJwcodes(ctx, jwcodesInt, couponId) |
|
|
|
|
|
|
|
|
num, num1, err = s.InsertCouponUsersByJwcodes(ctx, jwcodesInt, couponId) |
|
|
/*给用户发放卡券*/ |
|
|
/*给用户发放卡券*/ |
|
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
return num, num1, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 根据精网号发放用户优惠券,群发 //不被controller层调用了,但不能删除
|
|
|
// 根据精网号发放用户优惠券,群发 //不被controller层调用了,但不能删除
|
|
|
func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) { |
|
|
|
|
|
|
|
|
func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num, num1 int, err error) { |
|
|
//去重
|
|
|
//去重
|
|
|
m := make(map[int]bool) |
|
|
m := make(map[int]bool) |
|
|
var uniqueJwcodes []int //存放去重后的精网号
|
|
|
var uniqueJwcodes []int //存放去重后的精网号
|
|
@ -180,28 +180,42 @@ func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes [ |
|
|
|
|
|
|
|
|
//插入数据
|
|
|
//插入数据
|
|
|
for _, jwcode := range uniqueJwcodes { |
|
|
for _, jwcode := range uniqueJwcodes { |
|
|
//检查数据库中是否存在
|
|
|
|
|
|
|
|
|
//检查数据库表coupon_users中是否存在
|
|
|
count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() |
|
|
count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return num, errors.New("检索数据库中是否已存在数据失败") |
|
|
|
|
|
|
|
|
return num, num1, errors.New("检索数据库中是否已存在数据失败") |
|
|
} |
|
|
} |
|
|
//不存在,可以插入
|
|
|
|
|
|
|
|
|
//coupon_users中不存在,可以插入
|
|
|
if count == 0 { |
|
|
if count == 0 { |
|
|
result, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{ |
|
|
|
|
|
"jwcode": jwcode, |
|
|
|
|
|
"coupon_id": couponId, |
|
|
|
|
|
"time": gtime.Now().Unix(), |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
//判断在member_info表中是否存在
|
|
|
|
|
|
count1, err := dao.MemberInfo.Ctx(ctx).Where("jwcode = ?", jwcode).Count() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return num, errors.New("插入数据库失败") |
|
|
|
|
|
|
|
|
return num, num1, errors.New("检索member_info中是否已存在数据失败") |
|
|
|
|
|
} |
|
|
|
|
|
//在member_info表中存在
|
|
|
|
|
|
if count1 > 0 { |
|
|
|
|
|
result, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{ |
|
|
|
|
|
"jwcode": jwcode, |
|
|
|
|
|
"coupon_id": couponId, |
|
|
|
|
|
"time": gtime.Now().Unix(), |
|
|
|
|
|
}) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return num, num1, errors.New("插入数据库失败") |
|
|
|
|
|
} |
|
|
|
|
|
//获取受影响的行数
|
|
|
|
|
|
affected, err := result.RowsAffected() |
|
|
|
|
|
num += int(affected) |
|
|
|
|
|
} else { |
|
|
|
|
|
//在member_info表中不存在
|
|
|
|
|
|
num1++ |
|
|
} |
|
|
} |
|
|
//获取受影响的行数
|
|
|
|
|
|
affected, err := result.RowsAffected() |
|
|
|
|
|
num += int(affected) |
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
//在coupon_users中存在,即已经有卡券了
|
|
|
|
|
|
num1++ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return num, err //返回受影响的行数,即新增的条数
|
|
|
|
|
|
|
|
|
return num, num1, err //返回受影响的行数,即新增的条数
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 导入满足条件的用户jwcode到redis //不上传到redis了,上传到数据库表,到时候直接从表里查
|
|
|
// 导入满足条件的用户jwcode到redis //不上传到redis了,上传到数据库表,到时候直接从表里查
|
|
|