You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_invitation.go 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "sort"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/360EntSecGroup-Skylar/excelize/v2"
  13. )
  14. func QueryInvitationCurrentMonth(ctx *context.Context) {
  15. // userName := ctx.Query("userName")
  16. // currentTimeNow := time.Now()
  17. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  18. // pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location())
  19. //queryUserDataPage(ctx, "public.user_business_analysis_current_month", new(models.UserBusinessAnalysisCurrentMonth))
  20. //_, count := models.QueryUserStaticDataByTableName(1, 1, "public.user_business_analysis_current_month", new(models.UserBusinessAnalysisCurrentMonth), userName, 1)
  21. queryDataFromStaticTable(ctx, "public.user_business_analysis_current_month", new(models.UserBusinessAnalysisCurrentMonth))
  22. }
  23. func getInvitationExcelHeader(ctx *context.Context) map[string]string {
  24. excelHeader := make([]string, 0)
  25. excelHeader = append(excelHeader, ctx.Tr("user.static.id"))
  26. excelHeader = append(excelHeader, ctx.Tr("user.static.name"))
  27. excelHeader = append(excelHeader, ctx.Tr("user.static.invitationNum"))
  28. excelHeader = append(excelHeader, ctx.Tr("user.static.phone"))
  29. excelHeader = append(excelHeader, ctx.Tr("user.static.registdate"))
  30. excelHeaderMap := make(map[string]string, 0)
  31. var i byte
  32. i = 0
  33. for _, value := range excelHeader {
  34. excelColumn := getColumn(i) + fmt.Sprint(1)
  35. excelHeaderMap[excelColumn] = value
  36. i++
  37. }
  38. return excelHeaderMap
  39. }
  40. func getInvitationDetailExcelHeader(ctx *context.Context) map[string]string {
  41. excelHeader := make([]string, 0)
  42. excelHeader = append(excelHeader, ctx.Tr("user.static.id"))
  43. excelHeader = append(excelHeader, ctx.Tr("user.static.name"))
  44. excelHeader = append(excelHeader, ctx.Tr("user.static.srcUserId"))
  45. excelHeader = append(excelHeader, ctx.Tr("user.static.phone"))
  46. excelHeader = append(excelHeader, ctx.Tr("user.static.registdate"))
  47. excelHeaderMap := make(map[string]string, 0)
  48. var i byte
  49. i = 0
  50. for _, value := range excelHeader {
  51. excelColumn := getColumn(i) + fmt.Sprint(1)
  52. excelHeaderMap[excelColumn] = value
  53. i++
  54. }
  55. return excelHeaderMap
  56. }
  57. func writeInvitationExcel(row int, xlsx *excelize.File, sheetName string, userRecord *models.UserBusinessAnalysisAll) {
  58. rows := fmt.Sprint(row)
  59. var tmp byte
  60. tmp = 0
  61. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.ID)
  62. tmp = tmp + 1
  63. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.Name)
  64. tmp = tmp + 1
  65. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.InvitationUserNum)
  66. tmp = tmp + 1
  67. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.Phone)
  68. tmp = tmp + 1
  69. formatTime := userRecord.RegistDate.Format("2006-01-02 15:04:05")
  70. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime[0:len(formatTime)-3])
  71. }
  72. func writeInvitationDetailExcel(row int, xlsx *excelize.File, sheetName string, userRecord *models.Invitation) {
  73. rows := fmt.Sprint(row)
  74. var tmp byte
  75. tmp = 0
  76. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.UserID)
  77. tmp = tmp + 1
  78. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.Name)
  79. tmp = tmp + 1
  80. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.SrcUserID)
  81. tmp = tmp + 1
  82. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.Phone)
  83. tmp = tmp + 1
  84. formatTime := userRecord.CreatedUnix.Format("2006-01-02 15:04:05")
  85. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime[0:len(formatTime)-3])
  86. }
  87. func DownloadInvitationDetail(ctx *context.Context) {
  88. xlsx := excelize.NewFile()
  89. sheetName := ctx.Tr("user.static.invitationdetailsheetname")
  90. index := xlsx.NewSheet(sheetName)
  91. xlsx.DeleteSheet("Sheet1")
  92. excelHeader := getInvitationDetailExcelHeader(ctx)
  93. for k, v := range excelHeader {
  94. //设置单元格的值
  95. xlsx.SetCellValue(sheetName, k, v)
  96. }
  97. userNameMap := models.GetAllUserName()
  98. _, count := models.QueryInvitaionPage(1, 1)
  99. var indexTotal int64
  100. indexTotal = 0
  101. row := 1
  102. for {
  103. re, _ := models.QueryInvitaionPage(int(indexTotal), PAGE_SIZE)
  104. log.Info("return count=" + fmt.Sprint(count))
  105. for _, userRecord := range re {
  106. row++
  107. userRecord.Name = userNameMap[userRecord.UserID]
  108. writeInvitationDetailExcel(row, xlsx, sheetName, userRecord)
  109. }
  110. indexTotal += PAGE_SIZE
  111. if indexTotal >= count {
  112. break
  113. }
  114. }
  115. //设置默认打开的表单
  116. xlsx.SetActiveSheet(index)
  117. filename := sheetName + ".xlsx"
  118. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  119. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  120. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  121. log.Info("writer exel error." + err.Error())
  122. }
  123. }
  124. func queryDataFromStaticTable(ctx *context.Context, tableName string, queryObj interface{}) {
  125. page, pageSize := getPageInfo(ctx)
  126. userName := ctx.Query("userName")
  127. IsReturnFile := ctx.QueryBool("IsReturnFile")
  128. if IsReturnFile {
  129. //writer exec file.
  130. xlsx := excelize.NewFile()
  131. sheetName := ctx.Tr("user.static.invitationsheetname")
  132. index := xlsx.NewSheet(sheetName)
  133. xlsx.DeleteSheet("Sheet1")
  134. excelHeader := getInvitationExcelHeader(ctx)
  135. for k, v := range excelHeader {
  136. //设置单元格的值
  137. xlsx.SetCellValue(sheetName, k, v)
  138. }
  139. _, count := models.QueryUserInvitationDataByTableName(1, 1, tableName, queryObj, "", 1)
  140. var indexTotal int64
  141. indexTotal = 0
  142. row := 1
  143. for {
  144. re, _ := models.QueryUserInvitationDataByTableName(int(indexTotal), PAGE_SIZE, tableName, queryObj, "", 1)
  145. log.Info("return count=" + fmt.Sprint(count))
  146. for _, userRecord := range re {
  147. row++
  148. writeInvitationExcel(row, xlsx, sheetName, userRecord)
  149. }
  150. indexTotal += PAGE_SIZE
  151. if indexTotal >= count {
  152. break
  153. }
  154. }
  155. //设置默认打开的表单
  156. xlsx.SetActiveSheet(index)
  157. filename := sheetName + "_" + ctx.Tr("user.static."+tableName) + ".xlsx"
  158. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  159. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  160. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  161. log.Info("writer exel error." + err.Error())
  162. }
  163. } else {
  164. resultRecord, count := models.QueryUserInvitationDataByTableName((page-1)*pageSize, pageSize, tableName, queryObj, userName, 1)
  165. result := make([]models.Invitation, 0)
  166. for _, record := range resultRecord {
  167. invi := models.Invitation{
  168. SrcUserID: record.ID,
  169. Name: record.Name,
  170. InvitationUserNum: record.InvitationUserNum,
  171. Phone: record.Phone,
  172. CreatedUnix: record.RegistDate,
  173. }
  174. result = append(result, invi)
  175. }
  176. mapInterface := make(map[string]interface{})
  177. mapInterface["data"] = result
  178. mapInterface["count"] = count
  179. ctx.JSON(http.StatusOK, mapInterface)
  180. }
  181. }
  182. func QueryInvitationCurrentWeek(ctx *context.Context) {
  183. // currentTimeNow := time.Now()
  184. // offset := int(time.Monday - currentTimeNow.Weekday())
  185. // if offset > 0 {
  186. // offset = -6
  187. // }
  188. // pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  189. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  190. // queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix())
  191. queryDataFromStaticTable(ctx, "public.user_business_analysis_current_week", new(models.UserBusinessAnalysisCurrentWeek))
  192. }
  193. func QueryInvitationLastWeek(ctx *context.Context) {
  194. // currentTimeNow := time.Now()
  195. // offset := int(time.Monday - currentTimeNow.Weekday())
  196. // if offset > 0 {
  197. // offset = -6
  198. // }
  199. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  200. // pageStartTime := pageEndTime.AddDate(0, 0, -7)
  201. // queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix())
  202. queryDataFromStaticTable(ctx, "public.user_business_analysis_last_week", new(models.UserBusinessAnalysisLastWeek))
  203. }
  204. func QueryInvitationCurrentYear(ctx *context.Context) {
  205. // currentTimeNow := time.Now()
  206. // pageStartTime := time.Date(currentTimeNow.Year(), 1, 1, 0, 0, 0, 0, currentTimeNow.Location())
  207. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  208. // queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix())
  209. queryDataFromStaticTable(ctx, "public.user_business_analysis_current_year", new(models.UserBusinessAnalysisCurrentYear))
  210. }
  211. func QueryInvitationLast30Day(ctx *context.Context) {
  212. // currentTimeNow := time.Now()
  213. // pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -30)
  214. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  215. // queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix())
  216. queryDataFromStaticTable(ctx, "public.user_business_analysis_last30_day", new(models.UserBusinessAnalysisLast30Day))
  217. }
  218. func QueryInvitationLastMonth(ctx *context.Context) {
  219. // currentTimeNow := time.Now()
  220. // thisMonth := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location())
  221. // pageStartTime := thisMonth.AddDate(0, -1, 0)
  222. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1)
  223. // queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix())
  224. queryDataFromStaticTable(ctx, "public.user_business_analysis_last_month", new(models.UserBusinessAnalysisLastMonth))
  225. }
  226. func QueryInvitationYesterday(ctx *context.Context) {
  227. // currentTimeNow := time.Now().AddDate(0, 0, -1)
  228. // pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local)
  229. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location())
  230. // queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix())
  231. queryDataFromStaticTable(ctx, "public.user_business_analysis_yesterday", new(models.UserBusinessAnalysisYesterday))
  232. }
  233. func QueryInvitationAll(ctx *context.Context) {
  234. // currentTimeNow := time.Now()
  235. // pageStartTime := time.Date(2022, 8, 5, 0, 0, 0, 0, currentTimeNow.Location())
  236. // pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location())
  237. // queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix())
  238. queryDataFromStaticTable(ctx, "public.user_business_analysis_all", new(models.UserBusinessAnalysisAll))
  239. }
  240. func QueryUserDefineInvitationPage(ctx *context.Context) {
  241. startDate := ctx.Query("startDate")
  242. endDate := ctx.Query("endDate")
  243. startTime, _ := time.ParseInLocation("2006-01-02", startDate, time.Local)
  244. //startTime = startTime.UTC()
  245. endTime, _ := time.ParseInLocation("2006-01-02", endDate, time.Local)
  246. queryData(ctx, startTime, endTime)
  247. }
  248. func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) {
  249. page, pageSize := getPageInfo(ctx)
  250. IsReturnFile := ctx.QueryBool("IsReturnFile")
  251. dbResult := models.QueryInvitaionByTime(startTime.Unix(), endTime.Unix())
  252. invitaionNumMap := make(map[int64]int, 0)
  253. allUserIds := make([]int64, 0)
  254. for _, record := range dbResult {
  255. if _, ok := invitaionNumMap[record.SrcUserID]; !ok {
  256. invitaionNumMap[record.SrcUserID] = 1
  257. } else {
  258. invitaionNumMap[record.SrcUserID] = invitaionNumMap[record.SrcUserID] + 1
  259. }
  260. }
  261. invitaionNumList := make([]models.Invitation, 0)
  262. for key, value := range invitaionNumMap {
  263. invi := models.Invitation{
  264. SrcUserID: key,
  265. InvitationUserNum: value,
  266. }
  267. invitaionNumList = append(invitaionNumList, invi)
  268. allUserIds = append(allUserIds, key)
  269. }
  270. sort.Slice(invitaionNumList, func(i, j int) bool {
  271. return invitaionNumList[i].InvitationUserNum > invitaionNumList[j].InvitationUserNum
  272. })
  273. if IsReturnFile {
  274. xlsx := excelize.NewFile()
  275. sheetName := ctx.Tr("user.static.invitationsheetname")
  276. index := xlsx.NewSheet(sheetName)
  277. xlsx.DeleteSheet("Sheet1")
  278. excelHeader := getInvitationExcelHeader(ctx)
  279. for k, v := range excelHeader {
  280. //设置单元格的值
  281. xlsx.SetCellValue(sheetName, k, v)
  282. }
  283. end := 100
  284. userMap := make(map[int64]*models.User, 0)
  285. for i := 0; i < len(allUserIds); i += 100 {
  286. if end >= len(allUserIds) {
  287. end = len(allUserIds) - 1
  288. }
  289. if i == end {
  290. break
  291. }
  292. userList, err := models.GetUsersByIDs(allUserIds[i:end])
  293. if err == nil {
  294. for _, tmp := range userList {
  295. userMap[tmp.ID] = tmp
  296. }
  297. }
  298. end = end + 100
  299. }
  300. row := 1
  301. for _, userRecord := range invitaionNumList {
  302. row++
  303. rows := fmt.Sprint(row)
  304. var tmp byte
  305. tmp = 0
  306. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.SrcUserID)
  307. tmp = tmp + 1
  308. name := ""
  309. if userMap[userRecord.SrcUserID] != nil {
  310. name = userMap[userRecord.SrcUserID].Name
  311. }
  312. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, name)
  313. tmp = tmp + 1
  314. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.InvitationUserNum)
  315. tmp = tmp + 1
  316. Phone := ""
  317. if userMap[userRecord.SrcUserID] != nil {
  318. Phone = userMap[userRecord.SrcUserID].PhoneNumber
  319. }
  320. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, Phone)
  321. tmp = tmp + 1
  322. formatTime := ""
  323. if userMap[userRecord.SrcUserID] != nil {
  324. formatTime = userMap[userRecord.SrcUserID].CreatedUnix.Format("2006-01-02 15:04:05")
  325. }
  326. xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime[0:len(formatTime)-3])
  327. }
  328. //设置默认打开的表单
  329. xlsx.SetActiveSheet(index)
  330. filename := sheetName + "_" + getTimeFileName(startTime) + "_" + getTimeFileName(endTime) + ".xlsx"
  331. //filename := sheetName + "_" + ctx.Tr("user.static."+tableName) + ".xlsx"
  332. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename))
  333. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  334. if _, err := xlsx.WriteTo(ctx.Resp); err != nil {
  335. log.Info("writer exel error." + err.Error())
  336. }
  337. } else {
  338. result := make([]*models.Invitation, 0)
  339. userIds := make([]int64, 0)
  340. end := len(invitaionNumList) - 1
  341. for start := (page - 1) * pageSize; start <= end; start++ {
  342. invi := invitaionNumList[start]
  343. //todo name phone,createunix
  344. result = append(result, &invi)
  345. userIds = append(userIds, invi.SrcUserID)
  346. if len(result) == pageSize {
  347. break
  348. }
  349. }
  350. userList, err := models.GetUsersByIDs(userIds)
  351. if err == nil {
  352. for _, invi := range result {
  353. tmpUser := userList[0]
  354. for _, tmp := range userList {
  355. if tmp.ID == invi.SrcUserID {
  356. tmpUser = tmp
  357. break
  358. }
  359. }
  360. if invi.SrcUserID == tmpUser.ID {
  361. invi.Name = tmpUser.Name
  362. invi.Phone = tmpUser.PhoneNumber
  363. invi.CreatedUnix = tmpUser.CreatedUnix
  364. }
  365. }
  366. } else {
  367. log.Info("query user error." + err.Error())
  368. }
  369. mapInterface := make(map[string]interface{})
  370. mapInterface["data"] = result
  371. mapInterface["count"] = len(invitaionNumList)
  372. ctx.JSON(http.StatusOK, mapInterface)
  373. }
  374. }
  375. func getPageInfo(ctx *context.Context) (int, int) {
  376. page := ctx.QueryInt("page")
  377. if page <= 0 {
  378. page = 1
  379. }
  380. pageSize := ctx.QueryInt("pageSize")
  381. if pageSize <= 0 {
  382. pageSize = setting.UI.IssuePagingNum
  383. }
  384. return page, pageSize
  385. }