|
|
@ -3,16 +3,19 @@ package couponusers |
|
|
|
import ( |
|
|
|
"CouponBackendGo/api/v1/couponusers" |
|
|
|
"CouponBackendGo/internal/dao" |
|
|
|
"CouponBackendGo/internal/model/dto" |
|
|
|
"CouponBackendGo/internal/service" |
|
|
|
"context" |
|
|
|
"database/sql" |
|
|
|
"errors" |
|
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
|
"github.com/gogf/gf/v2/net/ghttp" |
|
|
|
"github.com/gogf/gf/v2/os/gtime" |
|
|
|
"github.com/xuri/excelize/v2" |
|
|
|
"mime/multipart" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
type sCouponUsers struct{} |
|
|
@ -23,74 +26,41 @@ func init() { |
|
|
|
|
|
|
|
// 根据优惠券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搜索用户信息失败") |
|
|
|
} |
|
|
|
} |
|
|
|
/*查询所有数据*/ |
|
|
|
db := dao.CouponUsers.Ctx(ctx) |
|
|
|
|
|
|
|
return users, err |
|
|
|
} else { //有条件查询时,不在分页,全查后筛选
|
|
|
|
//基于量表JOIN查询
|
|
|
|
db = db.As("cu").InnerJoin("member_info mi", "cu.jwcode = mi.jwcode"). |
|
|
|
Where("cu.coupon_id = ?", couponId) |
|
|
|
|
|
|
|
/*查询所有数据*/ |
|
|
|
//在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 |
|
|
|
} |
|
|
|
//如果jwcode条件存在
|
|
|
|
if jwcode != 0 { |
|
|
|
db = db.Where("cu.jwcode = ?", jwcode) |
|
|
|
} |
|
|
|
|
|
|
|
return users, err |
|
|
|
//如果name条件存在
|
|
|
|
if name != "" { |
|
|
|
db = db.Where("mi.name like ?", "%"+name+"%") |
|
|
|
} |
|
|
|
|
|
|
|
//查询总数
|
|
|
|
total, err := db.Count() |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("查询总数失败") |
|
|
|
} |
|
|
|
|
|
|
|
//查询数据(分页)
|
|
|
|
err = db.Fields("mi.jwcode, mi.name, mi.deptName, mi.shopName"). |
|
|
|
Page(pageNo, pageSize).Scan(&users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("查询数据失败") |
|
|
|
} |
|
|
|
|
|
|
|
//设置总数
|
|
|
|
for i := range users { |
|
|
|
users[i].Total = total |
|
|
|
} |
|
|
|
|
|
|
|
return users, err |
|
|
|
} |
|
|
|
|
|
|
|
// 根据jwcode,优惠券id删除用户
|
|
|
@ -108,7 +78,7 @@ func (s *sCouponUsers) DeleteCouponUserByJwcode(ctx context.Context, couponId, j |
|
|
|
return result, err |
|
|
|
} |
|
|
|
|
|
|
|
// 通过excel导入精网号
|
|
|
|
// 通过excel导入精网号, 解析并发放卡券
|
|
|
|
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) {
|
|
|
|
|
|
|
@ -384,9 +354,57 @@ func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId i |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/*未编写*/ |
|
|
|
// 导出拥有卡券的用户列表
|
|
|
|
func (s *sCouponUsers) ExportCouponUsers() { |
|
|
|
func (s *sCouponUsers) ExportCouponUsers(r *ghttp.Request, couponId, jwcode int, name string, pageNo, pageSize int) { |
|
|
|
//调用查询
|
|
|
|
users, err := s.GetCouponUsersByCondition(r.Context(), couponId, jwcode, name, pageNo, pageSize) |
|
|
|
if err != nil { |
|
|
|
r.Response.WriteJsonExit(dto.Error("查询失败:" + err.Error())) |
|
|
|
return |
|
|
|
} |
|
|
|
if len(users) == 0 { |
|
|
|
r.Response.WriteJsonExit(dto.Error("查询结果为空")) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//创建 Excel文件
|
|
|
|
excelFile := excelize.NewFile() //默认会创建一个Sheet1
|
|
|
|
sheetName := "Sheet1" |
|
|
|
//设置表头
|
|
|
|
headers := []string{"序号", "精网号", "姓名", "部门", "门店"} |
|
|
|
for i, header := range headers { |
|
|
|
col := string('A' + i) //将索引转换为 Excel列名
|
|
|
|
excelFile.SetCellValue(sheetName, col+"1", header) |
|
|
|
} |
|
|
|
|
|
|
|
/*写入数据*/ |
|
|
|
rowIndex := 2 //从第二行开始写入数据
|
|
|
|
for _, user := range users { |
|
|
|
excelFile.SetCellValue(sheetName, "A"+strconv.Itoa(rowIndex), rowIndex-1) //序号
|
|
|
|
excelFile.SetCellValue(sheetName, "B"+strconv.Itoa(rowIndex), user.Jwcode) //精网号
|
|
|
|
excelFile.SetCellValue(sheetName, "C"+strconv.Itoa(rowIndex), user.Name) //姓名
|
|
|
|
excelFile.SetCellValue(sheetName, "D"+strconv.Itoa(rowIndex), user.DeptName) //部门
|
|
|
|
excelFile.SetCellValue(sheetName, "E"+strconv.Itoa(rowIndex), user.ShopName) //门店
|
|
|
|
rowIndex++ |
|
|
|
} |
|
|
|
/*写入数据*/ |
|
|
|
|
|
|
|
//设置文件名
|
|
|
|
fileName := "Users_" + time.Now().Format("20060102150405") + ".xlsx" |
|
|
|
|
|
|
|
//保存到缓冲区并返回
|
|
|
|
buffer, err := excelFile.WriteToBuffer() |
|
|
|
if err != nil { |
|
|
|
r.Response.WriteJsonExit(dto.Error("生成Excel文件失败:" + err.Error())) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// 设置响应头,指定内容类型为Excel文件
|
|
|
|
r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") |
|
|
|
// 设置响应头,指定文件名为fileName
|
|
|
|
r.Response.Header().Set("Content-Disposition", "attachment; filename="+fileName) |
|
|
|
// 将buffer中的内容写入响应
|
|
|
|
r.Response.Write(buffer.Bytes()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|