kobe6258 5 ماه پیش
والد
کامیت
ab87086548
3فایلهای تغییر یافته به همراه60 افزوده شده و 49 حذف شده
  1. 17 39
      controllers/user.go
  2. 42 9
      models/user.go
  3. 1 1
      utils/excel_utils.go

+ 17 - 39
controllers/user.go

@@ -26,6 +26,16 @@ var (
 		"C": {"累计阅读次数", "ReadCount"},
 		"D": {"注册时间", "CreatedTime"},
 	}
+
+	userCols = map[string]utils.ExcelColMapping{
+		"A": {"姓名", "RealName"},
+		"B": {"手机号", "Mobile"},
+		"C": {"公司名称", "CompanyName"},
+		"D": {"注册时间", "CreatedTime"},
+		"E": {"是否关注公众号", "FollowingGzhStr"},
+		"F": {"最近一次阅读时间", "LastReadTime"},
+		"G": {"累计阅读次数", "ReadCount"},
+	}
 )
 
 // TemplateList
@@ -441,7 +451,7 @@ func (this *UserController) ExportTemplateUsers() {
 	year, month, day := time.Now().Date()
 	yearStr := strconv.Itoa(year)[2:]
 	fileName := fmt.Sprintf("临时用户表%s.%d.%d.xlsx", yearStr, month, day)
-	file, err := utils.ExportExcel("用户表", templateCols, models.TemplateUsersItem{}, list)
+	file, err := utils.ExportExcel("临时用户表", templateCols, list)
 	_ = this.downloadExcelFile(file, fileName)
 	br.Ret = 200
 	br.Success = true
@@ -488,29 +498,10 @@ func (this *UserController) ExportOfficialUsers() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
-	pageSize, _ := this.GetInt("PageSize")
-	currentIndex, _ := this.GetInt("CurrentIndex")
 	keyword := this.GetString("Keyword")
 	FollowingGzh := this.GetString("FollowingGzh")
 	RegisterBeginDate := this.GetString("RegisterBeginDate")
 	RegisterEndDate := this.GetString("RegisterEndDate")
-	if pageSize <= 0 {
-		pageSize = utils.PageSize20
-	} else if pageSize > utils.PageSize100 {
-		pageSize = utils.PageSize100
-	}
-	if currentIndex <= 0 {
-		currentIndex = 1
-	}
-	if pageSize <= 0 {
-		pageSize = utils.PageSize20
-	} else if pageSize > utils.PageSize100 {
-		pageSize = utils.PageSize100
-	}
-	if currentIndex <= 0 {
-		currentIndex = 1
-	}
-	startSize := utils.StartIndex(currentIndex, pageSize)
 
 	sortParamInt, _ := this.GetInt("SortParam", 0)
 	sortTypeInt, _ := this.GetInt("SortType", 0)
@@ -589,31 +580,18 @@ func (this *UserController) ExportOfficialUsers() {
 		condition += ` AND ( mobile LIKE ? or real_name like ?)`
 		pars = utils.GetLikeKeywordPars(pars, keyword, 2)
 	}
+	userList, err := models.GetPageOfficialUserByCondition(condition, pars, sortStr)
 
-	resp := new(response.UserListResp)
-	total, userList, err := models.GetPageOfficialUserList(condition, pars, sortStr, startSize, pageSize)
 	if err != nil {
 		br.Msg = "查询用户失败"
 		br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
 		return
 	}
-
-	//list := make([]*models.UserView, 0)
-	//var wg sync.WaitGroup
-	//wg.Add(len(userList))
-	//for _, v := range userList {
-	//	go func(v *models.User) {
-	//		defer wg.Done()
-	//		tempUser, _ := models.GetTemplateUser(v.TemplateUserId)
-	//		userView := v.FillUserInfo(tempUser)
-	//		list = append(list, &userView)
-	//	}(&v)
-	//}
-	//wg.Wait()
-	page := paging.GetPaging(currentIndex, pageSize, total)
-	resp.Paging = page
-	resp.List = userList
-	br.Data = resp
+	year, month, day := time.Now().Date()
+	yearStr := strconv.Itoa(year)[2:]
+	fileName := fmt.Sprintf("用户表%s.%d.%d.xlsx", yearStr, month, day)
+	file, err := utils.ExportExcel("用户列表", userCols, userList)
+	_ = this.downloadExcelFile(file, fileName)
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"

+ 42 - 9
models/user.go

@@ -32,15 +32,17 @@ func (u User) FillUserInfo(user *TemplateUser) UserView {
 }
 
 type UserView struct {
-	Id             int
-	TemplateUserId int
-	RealName       string `description:"姓名"`
-	Mobile         string `description:"手机号码"`
-	FollowingGzh   bool
-	LastReadTime   string
-	ReadCount      int
-	AccountStatus  AccountStatus `description:"账号状态"`
-	CreatedTime    string
+	Id              int
+	TemplateUserId  int
+	CompanyName     string
+	RealName        string `description:"姓名"`
+	Mobile          string `description:"手机号码"`
+	FollowingGzh    bool
+	FollowingGzhStr string
+	LastReadTime    string
+	ReadCount       int
+	AccountStatus   AccountStatus `description:"账号状态"`
+	CreatedTime     string
 }
 
 func GetPageOfficialUserList(condition string, pars []interface{}, sortStr string, startSize int, pageSize int) (total int, userList []*UserView, err error) {
@@ -69,5 +71,36 @@ func GetPageOfficialUserList(condition string, pars []interface{}, sortStr strin
 	if userList == nil {
 		userList = []*UserView{}
 	}
+	for _, item := range userList {
+		if item.FollowingGzh {
+			item.FollowingGzhStr = "是"
+		} else {
+			item.FollowingGzhStr = "否"
+		}
+	}
+	return
+}
+
+func GetPageOfficialUserByCondition(condition string, pars []interface{}, sortStr string) (userList []UserView, err error) {
+	o := orm.NewOrm()
+	totalSql := `SELECT distinct template_user_id FROM users`
+	var officialIds []int
+	_, err = o.Raw(totalSql).QueryRows(&officialIds)
+	if err != nil {
+		return
+	}
+	idCondition := " and tus.id in (" + utils.GetOrmReplaceHolder(len(officialIds)) + ")"
+	sql := `SELECT us.id as id,us.template_user_id as template_user_id, us.real_name as real_name, tus.mobile,tus.following_gzh,tus.created_time,tus.read_count,tus.last_read_time,tus.account_status  FROM template_users tus LEFT JOIN (select id, real_name,template_user_id from users) us on us.template_user_id=tus.id WHERE  1=1`
+	sql = sql + idCondition
+	if condition != "" {
+		sql += condition
+	}
+	if sortStr != `` {
+		sql += ` ORDER BY ` + sortStr
+	}
+	_, err = o.Raw(sql, officialIds, pars).QueryRows(&userList)
+	if userList == nil {
+		userList = []UserView{}
+	}
 	return
 }

+ 1 - 1
utils/excel_utils.go

@@ -11,7 +11,7 @@ type ExcelColMapping struct {
 	Field string
 }
 
-func ExportExcel[T any](sheetName string, cols map[string]ExcelColMapping, dataType T, dataList []T) (file *excelize.File, err error) {
+func ExportExcel[T any](sheetName string, cols map[string]ExcelColMapping, dataList []T) (file *excelize.File, err error) {
 	// 创建一个新的 Excel 文件
 	file = excelize.NewFile()
 	// 创建一个工作表