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.
357 lines
10 KiB
357 lines
10 KiB
package couponusers
|
|
|
|
import (
|
|
"CouponBackendGo/api/v1/couponusers"
|
|
"CouponBackendGo/internal/dao"
|
|
"CouponBackendGo/internal/service"
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"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{}
|
|
|
|
func init() {
|
|
service.RegisterCouponUsers(&sCouponUsers{})
|
|
}
|
|
|
|
// 根据优惠券id查看拥有优惠券的用户
|
|
func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, jwcode int, name string, pageNo, pageSize int) (users []couponusers.GetCouponUsersRes, err error) {
|
|
//没有条件查询时,分页
|
|
if jwcode == 0 && name == "" {
|
|
/*查询所有数据*/
|
|
//在coupon_users中查询出对应的jwcode
|
|
err = dao.CouponUsers.Ctx(ctx).Fields("jwcode").
|
|
Where("coupon_id = ", couponId).Page(pageNo, pageSize).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搜索用户信息失败")
|
|
}
|
|
}
|
|
/*查询所有数据*/
|
|
|
|
return users, err
|
|
} else { //有条件查询时,不在分页,全查后筛选
|
|
|
|
/*查询所有数据*/
|
|
//在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搜索用户信息失败")
|
|
}
|
|
}
|
|
/*查询所有数据*/
|
|
|
|
//条件查询
|
|
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("没有工作表")
|
|
}
|
|
|
|
rows, err := f.GetRows("Sheet1")
|
|
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 //不上传到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("没有工作表")
|
|
}
|
|
|
|
// 读取第一个工作表
|
|
rows, err := f.GetRows("Sheet1")
|
|
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("参数转换失败")
|
|
}
|
|
|
|
// 判断jwcode是否在表coupon_qualified_users中
|
|
count, err := dao.CouponQualifiedUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Count()
|
|
if err != nil {
|
|
return errors.New("检索数据库中是否已存在数据失败")
|
|
}
|
|
if count == 0 { //不存在,插入
|
|
_, err = dao.CouponQualifiedUsers.Ctx(ctx).Insert(g.Map{
|
|
"jwcode": jwcode,
|
|
})
|
|
}
|
|
|
|
//jwcodes = append(jwcodes, jwcode)
|
|
/*参数校验*/
|
|
}
|
|
|
|
return //返回nil表示成功
|
|
}
|
|
|
|
// 判断某用户能否抽到卡券
|
|
func (s *sCouponUsers) IsEligibleUser(ctx context.Context, jwcode int, couponIds []int) (img string, err error) {
|
|
//从数据库中获取符合条件的精网号EligibleJwcodes
|
|
var EligibleJwcodes []couponusers.InsertCouponUserRes
|
|
err = dao.CouponQualifiedUsers.Ctx(ctx).Fields("jwcode").Scan(&EligibleJwcodes)
|
|
if err != nil {
|
|
return "", errors.New("从数据库中获取符合条件的精网号失败")
|
|
}
|
|
if len(EligibleJwcodes) == 0 {
|
|
return "", errors.New("核验数据库(满足条件)中数据为空")
|
|
}
|
|
|
|
//检查jwcode是否在EligibleJwcodes中
|
|
for _, EligibleJwcode := range EligibleJwcodes {
|
|
if EligibleJwcode.Jwcode == jwcode {
|
|
//存在,有资格,判断是否抽取过
|
|
for _, couponId := range couponIds {
|
|
count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count()
|
|
if err != nil {
|
|
return "", errors.New("检索数据库中是否已存在数据失败")
|
|
}
|
|
//有记录,抽取过
|
|
if count > 0 {
|
|
err = dao.CouponUsers.Ctx(ctx).Fields("img_url").Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Scan(&img)
|
|
return img, errors.New("该用户已领取过该卡券")
|
|
}
|
|
}
|
|
//所有的都没有记录,没有抽取过
|
|
return "", nil
|
|
}
|
|
}
|
|
|
|
return "", errors.New("该用户不满足领取条件")
|
|
}
|
|
|
|
// 给单个用户发放卡券
|
|
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("该用户已领取该卡券")
|
|
}
|
|
|
|
//不存在 查看是否满足条件,满足就添加,不满足就返回
|
|
|
|
//从数据库中获取符合条件的精网号EligibleJwcodes
|
|
var EligibleJwcodes []couponusers.InsertCouponUserRes
|
|
err = dao.CouponQualifiedUsers.Ctx(ctx).Fields("jwcode").Scan(&EligibleJwcodes)
|
|
if err != nil {
|
|
return errors.New("从数据库中获取符合条件的精网号失败")
|
|
}
|
|
if len(EligibleJwcodes) == 0 {
|
|
return errors.New("核验数据库(满足条件)中数据为空")
|
|
}
|
|
|
|
//检查jwcode是否在EligibleJwcodes中
|
|
for _, EligibleJwcode := range EligibleJwcodes {
|
|
if EligibleJwcode.Jwcode == 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) ExportCouponUsers() {
|
|
|
|
}
|