|
|
@ -1,20 +1,11 @@ |
|
|
|
package couponusers |
|
|
|
|
|
|
|
import ( |
|
|
|
"CouponBackendGo/api/v1/couponusers" |
|
|
|
"CouponBackendGo/internal/dao" |
|
|
|
"CouponBackendGo/internal/service" |
|
|
|
"context" |
|
|
|
"database/sql" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
|
"github.com/gogf/gf/v2/os/gtime" |
|
|
|
"github.com/xuri/excelize/v2" |
|
|
|
"mime/multipart" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
|
|
|
|
type sCouponUsers struct{} |
|
|
@ -23,295 +14,6 @@ func init() { |
|
|
|
service.RegisterCouponUsers(&sCouponUsers{}) |
|
|
|
} |
|
|
|
|
|
|
|
// 根据优惠券id查看拥有优惠券的用户
|
|
|
|
func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, jwcode int, name string) (users []couponusers.GetCouponUsersRes, err error) { //待解释
|
|
|
|
//从redis中获取所有数据但暂时不返回, 用于条件查询
|
|
|
|
value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d CouponUsers", couponId)) |
|
|
|
//redis中有数据
|
|
|
|
if value.String() != "" { |
|
|
|
err = json.Unmarshal(value.Bytes(), &users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("redis中数据解析失败") |
|
|
|
} |
|
|
|
} else { //redis中没有数据
|
|
|
|
//没有从redis中获取成功,从数据库中查询,并存储到redis中
|
|
|
|
/*查询所有数据*/ |
|
|
|
//在coupon_users中查询出对应的jwcode
|
|
|
|
err = dao.CouponUsers.Ctx(ctx).Fields("jwcode"). |
|
|
|
Where("coupon_id = ", couponId).Scan(&users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据卡券id搜索jwcode失败") |
|
|
|
} |
|
|
|
//根据jwcode查询用户信息
|
|
|
|
for i, userInfo := range users { |
|
|
|
err = dao.MemberInfo.Ctx(ctx).Fields("jwcode", "name", "deptName", "shopName"). |
|
|
|
Where("jwcode = ?", userInfo.Jwcode).Scan(&users[i]) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据jwcode搜索用户信息失败") |
|
|
|
} |
|
|
|
} |
|
|
|
/*查询所有数据*/ |
|
|
|
|
|
|
|
//将数据存入redis
|
|
|
|
UsersMarshal, _ := json.Marshal(users) |
|
|
|
_, err = g.Redis().Set(ctx, fmt.Sprintf("%d CouponUsers", couponId), UsersMarshal) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("将数据存入redis失败") |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//条件查询
|
|
|
|
if jwcode != 0 && name == "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if user.Jwcode == jwcode { |
|
|
|
result = append(result, user) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} else if jwcode == 0 && name != "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if strings.Contains(user.Name, name) { |
|
|
|
result = append(result, user) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} else if jwcode != 0 && name != "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if user.Jwcode == jwcode && strings.Contains(user.Name, name) { |
|
|
|
result = append(result, user) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} |
|
|
|
|
|
|
|
return users, err |
|
|
|
} |
|
|
|
|
|
|
|
// 根据jwcode,优惠券id删除用户
|
|
|
|
func (s *sCouponUsers) DeleteCouponUserByJwcode(ctx context.Context, couponId, jwcode int) (result sql.Result, err error) { |
|
|
|
if jwcode == 0 { |
|
|
|
return nil, errors.New("jwcode不能为空") |
|
|
|
} |
|
|
|
if couponId == 0 { |
|
|
|
return nil, errors.New("couponId不能为空") |
|
|
|
} |
|
|
|
result, err = dao.CouponUsers.Ctx(ctx).Where("coupon_id = ?", couponId).Where("jwcode = ?", jwcode).Delete() |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("删除用户失败") |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} |
|
|
|
|
|
|
|
// 通过excel导入精网号
|
|
|
|
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("打开文件失败") |
|
|
|
} |
|
|
|
// 延时关闭文件
|
|
|
|
defer func(f *excelize.File) { |
|
|
|
err := f.Close() |
|
|
|
if err != nil { |
|
|
|
return // 返回错误
|
|
|
|
} |
|
|
|
}(f) |
|
|
|
|
|
|
|
//读取所有工作表名称
|
|
|
|
GetSheetMap := f.GetSheetMap() |
|
|
|
if len(GetSheetMap) == 0 { |
|
|
|
return nil, errors.New("没有工作表") |
|
|
|
} |
|
|
|
// 读取第一个工作表
|
|
|
|
sheetName := GetSheetMap[1] |
|
|
|
//rows, err := f.GetRows(f.GetSheetName(1))
|
|
|
|
rows, err := f.GetRows(sheetName) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
// 将每行的第一列转换为int,并添加到切片中
|
|
|
|
for i, row := range rows { |
|
|
|
//跳过第一行
|
|
|
|
if i == 0 { |
|
|
|
continue |
|
|
|
} |
|
|
|
// 假设jwcode在每行的第一列
|
|
|
|
/*参数校验*/ |
|
|
|
//参数校验,检查每行是否有足够数据
|
|
|
|
if len(row) == 0 { |
|
|
|
continue // 跳过空行
|
|
|
|
} |
|
|
|
//参数校验,检查jwcode是否为非空字符串
|
|
|
|
jwcodeStr := row[0] |
|
|
|
if jwcodeStr == "" { |
|
|
|
continue // 跳过空行
|
|
|
|
} |
|
|
|
//将字符串转换为整数
|
|
|
|
jwcode, err := strconv.Atoi(jwcodeStr) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("参数转换失败") |
|
|
|
} |
|
|
|
jwcodes = append(jwcodes, jwcode) |
|
|
|
/*参数校验*/ |
|
|
|
} |
|
|
|
return //返回jwcodes切片
|
|
|
|
} |
|
|
|
|
|
|
|
// 根据精网号发放用户优惠券
|
|
|
|
func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) { |
|
|
|
//去重
|
|
|
|
m := make(map[int]bool) |
|
|
|
var uniqueJwcodes []int //存放去重后的精网号
|
|
|
|
for _, jwcode := range jwcodes { |
|
|
|
if _, exist := m[jwcode]; !exist { |
|
|
|
m[jwcode] = true |
|
|
|
uniqueJwcodes = append(uniqueJwcodes, jwcode) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//插入数据
|
|
|
|
for _, jwcode := range uniqueJwcodes { |
|
|
|
//检查数据库中是否存在
|
|
|
|
count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() |
|
|
|
if err != nil { |
|
|
|
return num, errors.New("检索数据库中是否已存在数据失败") |
|
|
|
} |
|
|
|
//不存在,可以插入
|
|
|
|
if count == 0 { |
|
|
|
result, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{ |
|
|
|
"jwcode": jwcode, |
|
|
|
"coupon_id": couponId, |
|
|
|
"time": gtime.Now().Unix(), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return num, errors.New("插入数据库失败") |
|
|
|
} |
|
|
|
//获取受影响的行数
|
|
|
|
affected, err := result.RowsAffected() |
|
|
|
num += int(affected) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return num, err //返回受影响的行数,即新增的条数
|
|
|
|
} |
|
|
|
|
|
|
|
// 导入满足条件的用户jwcode到redis
|
|
|
|
func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err error) { |
|
|
|
var ctx g.Ctx |
|
|
|
//打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
|
|
|
|
f, err := excelize.OpenReader(file) |
|
|
|
if err != nil { |
|
|
|
return errors.New("打开文件失败") |
|
|
|
} |
|
|
|
// 延时关闭文件
|
|
|
|
defer func(f *excelize.File) { |
|
|
|
err := f.Close() |
|
|
|
if err != nil { |
|
|
|
// 处理关闭文件时的错误
|
|
|
|
return |
|
|
|
} |
|
|
|
}(f) |
|
|
|
|
|
|
|
//读取所有工作表名称
|
|
|
|
GetSheetMap := f.GetSheetMap() |
|
|
|
if len(GetSheetMap) == 0 { |
|
|
|
return errors.New("没有工作表") |
|
|
|
} |
|
|
|
// 读取第一个工作表
|
|
|
|
sheetName := GetSheetMap[1] |
|
|
|
//rows, err := f.GetRows(f.GetSheetName(1))
|
|
|
|
rows, err := f.GetRows(sheetName) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// 将每行的第一列转换为int,并添加到切片中
|
|
|
|
var jwcodes []int |
|
|
|
for i, row := range rows { |
|
|
|
//跳过第一行
|
|
|
|
if i == 0 { |
|
|
|
continue |
|
|
|
} |
|
|
|
// 假设jwcode在每行的第一列
|
|
|
|
/*参数校验*/ |
|
|
|
//参数校验,检查每行是否有足够数据
|
|
|
|
if len(row) == 0 { |
|
|
|
continue // 跳过空行
|
|
|
|
} |
|
|
|
//参数校验,检查jwcode是否为非空字符串
|
|
|
|
jwcodeStr := row[0] |
|
|
|
if jwcodeStr == "" { |
|
|
|
continue // 跳过空行
|
|
|
|
} |
|
|
|
//将字符串转换为整数
|
|
|
|
jwcode, err := strconv.Atoi(jwcodeStr) |
|
|
|
if err != nil { |
|
|
|
return errors.New("参数转换失败") |
|
|
|
} |
|
|
|
|
|
|
|
jwcodes = append(jwcodes, jwcode) |
|
|
|
/*参数校验*/ |
|
|
|
} |
|
|
|
|
|
|
|
//将jwcodes存入redis
|
|
|
|
MarshalJwcodes, _ := json.Marshal(jwcodes) |
|
|
|
_, err = g.Redis().Set(ctx, "EligibleJwcodes", MarshalJwcodes) |
|
|
|
if err != nil { |
|
|
|
return errors.New("存入redis失败") |
|
|
|
} |
|
|
|
return //返回nil表示成功
|
|
|
|
} |
|
|
|
|
|
|
|
// 给单个用户发放卡券
|
|
|
|
func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId int) (err error) { |
|
|
|
//查看库中是否已经存在
|
|
|
|
count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() |
|
|
|
if err != nil { |
|
|
|
return errors.New("检索数据库中是否已存在数据失败") |
|
|
|
} |
|
|
|
//已存在 不添加,直接返回
|
|
|
|
if count > 0 { |
|
|
|
return errors.New("该用户已领取该卡券") |
|
|
|
} |
|
|
|
|
|
|
|
//不存在 查看是否满足条件,满足就添加,不满足就返回
|
|
|
|
/*从redis中获取符合条件的精网号EligibleJwcodes*/ |
|
|
|
redisResult, err := g.Redis().Get(ctx, "EligibleJwcodes") |
|
|
|
if err != nil { |
|
|
|
return errors.New("从redis中获取数据失败") |
|
|
|
} |
|
|
|
var EligibleJwcodes []int |
|
|
|
err = json.Unmarshal(redisResult.Bytes(), &EligibleJwcodes) |
|
|
|
if err != nil { |
|
|
|
return errors.New("redis中数据解析失败") |
|
|
|
} |
|
|
|
/*从redis中获取符合条件的精网号EligibleJwcodes*/ |
|
|
|
|
|
|
|
//检查jwcode是否在EligibleJwcodes中
|
|
|
|
for _, EligibleJwcode := range EligibleJwcodes { |
|
|
|
if EligibleJwcode == jwcode { |
|
|
|
//存在,可以插入
|
|
|
|
_, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{ |
|
|
|
"jwcode": jwcode, |
|
|
|
"coupon_id": couponId, |
|
|
|
"time": gtime.Now().Unix(), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return errors.New("插入数据库失败") |
|
|
|
} |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return errors.New("该用户精网号不符合领取条件") //遍历完了所有满足条件的用户,发现不在其中,不符合条件
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 添加用户选择武器记录
|
|
|
|
func (s *sCouponUsers) AddRecord(ctx context.Context, jwcode int, id int, name string) (err error) { |
|
|
|
_, err = dao.CouponUsers.Ctx(ctx).Data(g.Map{ |
|
|
|