goweb-gin-demo/server/service/wt/wt_report.go

222 lines
7.1 KiB
Go
Raw Normal View History

2021-11-04 19:39:59 +08:00
package wt
import (
"encoding/json"
"goweb-gin-demo/global"
"goweb-gin-demo/model/common/request"
2021-11-17 19:40:32 +08:00
systemModel "goweb-gin-demo/model/system"
2021-11-04 19:39:59 +08:00
"goweb-gin-demo/model/wt"
wtReq "goweb-gin-demo/model/wt/request"
2021-11-04 21:45:07 +08:00
wtRes "goweb-gin-demo/model/wt/response"
2021-11-17 19:40:32 +08:00
"goweb-gin-demo/service/system"
2021-11-06 12:59:51 +08:00
"strconv"
2021-11-04 19:39:59 +08:00
)
type WtReportsService struct {
}
// CreateWtReports 创建周报
func (wtReportsService *WtReportsService) CreateWtReports(reportsVO wtReq.WtReportsVO) (err error) {
wtReports := voToRrports(reportsVO)
err = global.GLOBAL_DB.Create(&wtReports).Error
return err
}
// DeleteWtReportsByIds 批量删除周报
func (wtReportsService *WtReportsService) DeleteWtReportsByIds(ids request.IdsReq) (err error) {
err = global.GLOBAL_DB.Delete(&[]wt.WtReports{}, "id in ?", ids.Ids).Error
return err
}
// UpdateWtReports 更新周报
func (wtReportsService *WtReportsService) UpdateWtReports(reportsVO wtReq.WtReportsVO) (err error) {
wtReports := voToRrports(reportsVO)
err = global.GLOBAL_DB.Updates(&wtReports).Error
return err
}
// GetWtReports 根据id获取周报
2021-11-04 21:45:07 +08:00
func (wtReportsService *WtReportsService) GetWtReports(id uint) (err error, reportsVO wtRes.WtReportsResult) {
2021-11-04 19:39:59 +08:00
report := wt.WtReports{}
err = global.GLOBAL_DB.Where("id = ?", id).First(&report).Error
reportVO := reportToVO(report)
return err, reportVO
}
// GetWtReportsInfoList 分页获取周报
2021-11-08 15:12:15 +08:00
func (wtReportsService *WtReportsService) GetWtReportsInfoList(info wtReq.WtReportsSearch) (err error, list []wtRes.WtReportsSearchResult, total int64) {
2021-11-04 19:39:59 +08:00
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
2021-11-06 12:59:51 +08:00
reportTable := global.GLOBAL_DB.Table("wt_reports")
var reportsSearchBOList []wtRes.WtReportsSearchBO
querySql := "SELECT id, user_name, user_id, send_to, header, contents, pictures, attachments, created_at, updated_at, cmc.comment_count " +
2021-11-08 15:12:15 +08:00
"FROM wt_reports " +
"left join (SELECT report_id, count(report_id) as comment_count FROM wt_comments GROUP BY report_id) as cmc " +
2021-11-08 15:12:15 +08:00
"on cmc.report_id = wt_reports.id " +
2021-11-15 20:43:04 +08:00
"WHERE "
condition := " 1=1 "
2021-11-06 12:59:51 +08:00
if info.CurrUserId > 0 {
2021-11-15 20:43:04 +08:00
condition += " and send_to LIKE '%\"id\":" + strconv.Itoa(int(info.CurrUserId)) + "%'"
}
2021-11-06 12:59:51 +08:00
// 条件高级查询
if info.UserId > 0 {
2021-11-15 20:43:04 +08:00
condition += " and user_id = " + strconv.Itoa(int(info.UserId))
}else {
condition += " OR user_id = " + strconv.Itoa(int(info.CurrUserId)) + " "
2021-11-06 12:59:51 +08:00
}
if len(info.Content) != 0 {
2021-11-15 20:43:04 +08:00
condition += " and contents LIKE '%" + info.Content + "%'"
2021-11-06 12:59:51 +08:00
}
if len(info.StartTime) != 0 && len(info.EndTime) != 0 {
2021-11-15 20:43:04 +08:00
condition += " and created_at >= '" + info.StartTime + "' and created_at <= '" + info.EndTime + "'"
}
querySql += condition + " ORDER BY created_at DESC LIMIT ? OFFSET ? "
err = reportTable.Where(condition).Count(&total).Error
if err != nil {
return
}
if info.Page == 0 {
limit = int(total)
2021-11-06 12:59:51 +08:00
}
err = global.GLOBAL_DB.Raw(querySql, limit, offset).Scan(&reportsSearchBOList).Error
2021-11-06 12:59:51 +08:00
reportsSearchResultList := reportsToSearchResult(reportsSearchBOList)
return err, reportsSearchResultList, total
2021-11-04 19:39:59 +08:00
}
//数据转换一下, 需要把json数据转换为字符串
func voToRrports(reportsVO wtReq.WtReportsVO) wt.WtReports {
sendToJson, _ := json.Marshal(reportsVO.SendTo)
contentJson, _ := json.Marshal(reportsVO.Contents)
picturesJson, _ := json.Marshal(reportsVO.Pictures)
attachmentsJson, _ := json.Marshal(reportsVO.Attachments)
wtReports := wt.WtReports{
GLOBAL_MODEL: global.GLOBAL_MODEL{ID: reportsVO.ID},
UserName: reportsVO.UserName,
2021-11-08 15:12:15 +08:00
UserId: reportsVO.UserId,
2021-11-04 19:39:59 +08:00
SendTo: string(sendToJson),
Header: reportsVO.Header,
Contents: string(contentJson),
Pictures: string(picturesJson),
Attachments: string(attachmentsJson),
}
return wtReports
}
2021-11-06 12:59:51 +08:00
// 批量转换 数据转换, 把字符串转换为json
func reportsToSearchResult(wtReportsList []wtRes.WtReportsSearchBO) []wtRes.WtReportsSearchResult {
var reportsSearchResults []wtRes.WtReportsSearchResult
2021-11-17 19:40:32 +08:00
var sysServiceGroup system.SystemServiceGroup
_, userMap := sysServiceGroup.GetAllUserInfoMap()
2021-11-06 12:59:51 +08:00
for _, searchBO := range wtReportsList {
2021-11-17 19:40:32 +08:00
userInfo := userMap[searchBO.UserName]
reportVO := reportToSearchResult(searchBO, userInfo)
2021-11-06 12:59:51 +08:00
reportsSearchResults = append(reportsSearchResults, reportVO)
}
return reportsSearchResults
}
//单个转换
2021-11-17 19:40:32 +08:00
func reportToSearchResult(searchBO wtRes.WtReportsSearchBO, user systemModel.SysUser) wtRes.WtReportsSearchResult {
2021-11-06 12:59:51 +08:00
searchResult := wtRes.WtReportsSearchResult{}
searchResult.GLOBAL_MODEL = searchBO.GLOBAL_MODEL
searchResult.UserName = searchBO.UserName
2021-11-08 15:12:15 +08:00
searchResult.UserId = searchBO.UserId
2021-11-06 12:59:51 +08:00
searchResult.Header = searchBO.Header
searchResult.CommentCount = searchBO.CommentCount
2021-11-17 19:40:32 +08:00
if len(user.NickName) == 0 {
searchResult.NickName = searchBO.UserName
} else {
searchResult.NickName = user.NickName
}
2021-11-06 12:59:51 +08:00
json.Unmarshal([]byte(searchBO.SendTo), &searchResult.SendTo)
json.Unmarshal([]byte(searchBO.Contents), &searchResult.Contents)
json.Unmarshal([]byte(searchBO.Pictures), &searchResult.Pictures)
json.Unmarshal([]byte(searchBO.Attachments), &searchResult.Attachments)
return searchResult
}
2021-11-04 19:39:59 +08:00
// 批量转换 数据转换, 把字符串转换为json
2021-11-04 21:45:07 +08:00
func reportsToVOs(wtReportsList []wt.WtReports) []wtRes.WtReportsResult {
var reportsVOList []wtRes.WtReportsResult
2021-11-04 19:39:59 +08:00
for _, report := range wtReportsList {
reportVO := reportToVO(report)
reportsVOList = append(reportsVOList, reportVO)
}
return reportsVOList
}
//单个转换
2021-11-04 21:45:07 +08:00
func reportToVO(report wt.WtReports) wtRes.WtReportsResult {
reportVO := wtRes.WtReportsResult{}
reportVO.GLOBAL_MODEL = report.GLOBAL_MODEL
2021-11-04 19:39:59 +08:00
reportVO.UserName = report.UserName
2021-11-08 15:12:15 +08:00
reportVO.UserId = report.UserId
2021-11-04 19:39:59 +08:00
reportVO.Header = report.Header
json.Unmarshal([]byte(report.SendTo), &reportVO.SendTo)
json.Unmarshal([]byte(report.Contents), &reportVO.Contents)
json.Unmarshal([]byte(report.Pictures), &reportVO.Pictures)
json.Unmarshal([]byte(report.Attachments), &reportVO.Attachments)
return reportVO
}
2021-11-08 15:12:15 +08:00
2021-11-08 18:06:18 +08:00
func (wtReportsService *WtReportsService) getWtReportListForStat(statData wt.StatDataSearch) (err error, userIds []int) {
2021-11-08 15:12:15 +08:00
var commitUserIds []int
sql := "created_at >= '" + statData.StartTime + "' and created_at <= '" + statData.EndTime + "' and user_id in ? "
err = global.GLOBAL_DB.Model(&wt.WtReports{}).Select("user_id").Where(sql, statData.UserIds).Scan(&commitUserIds).Error
return err, commitUserIds
}
2021-11-08 18:06:18 +08:00
func (wtReportsService *WtReportsService) getWtReportListForExcel(statData wt.StatDataSearch) (err error, result []wtRes.WtReportsResult) {
var wtReportList []wt.WtReports
condition := " 1=1 "
if len(statData.StartTime) != 0 {
condition += "and created_at >= '" + statData.StartTime + "'"
}
if len(statData.EndTime) != 0 {
condition += "and created_at <= '" + statData.EndTime + "'"
}
if len(statData.UserIds) != 0 {
userIdStr := "( "
for i, id := range statData.UserIds {
if i < (len(statData.UserIds) - 1) {
userIdStr += strconv.Itoa(id) + ", "
} else {
userIdStr += strconv.Itoa(id)
}
}
userIdStr += " )"
condition += " and user_id in " + userIdStr
}
err = global.GLOBAL_DB.Model(&wt.WtReports{}).Where(condition).Scan(&wtReportList).Error
reportsResults := reportsToVOs(wtReportList)
return err, reportsResults
}