2021-11-04 19:39:59 +08:00
|
|
|
package wt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"goweb-gin-demo/global"
|
|
|
|
"goweb-gin-demo/model/common/request"
|
|
|
|
"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-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 分页获取周报
|
|
|
|
func (wtReportsService *WtReportsService) GetWtReportsInfoList(info wtReq.WtReportsSearch) (err error, list interface{}, total int64) {
|
|
|
|
limit := info.PageSize
|
|
|
|
offset := info.PageSize * (info.Page - 1)
|
2021-11-06 12:59:51 +08:00
|
|
|
|
|
|
|
reportTable := global.GLOBAL_DB.Table("wt_reports")
|
|
|
|
|
|
|
|
err = reportTable.Count(&total).Error
|
2021-11-04 19:39:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-06 12:59:51 +08:00
|
|
|
var reportsSearchBOList []wtRes.WtReportsSearchBO
|
|
|
|
|
|
|
|
if info.Page == 0 {
|
|
|
|
limit = int(total)
|
|
|
|
}
|
|
|
|
|
|
|
|
//首选获取周报的ids
|
|
|
|
var reportIds []uint
|
|
|
|
reportTable.Select("`id`", offset, limit).Offset(offset).Limit(limit).Scan(&reportIds)
|
|
|
|
|
|
|
|
querySql := "SELECT id, user_name, send_to, header, contents, pictures, attachments, created_at, updated_at, cmc.comment_count "+
|
|
|
|
"FROM wt_reports "+
|
|
|
|
"left join (SELECT report_id, count(report_id) as comment_count FROM wt_comments WHERE report_id in ? GROUP BY report_id) as cmc "+
|
|
|
|
"on cmc.report_id = wt_reports.id "+
|
|
|
|
"WHERE 1=1 "
|
|
|
|
|
|
|
|
// 条件高级查询
|
|
|
|
if info.UserId > 0 {
|
|
|
|
querySql += " and id = " + strconv.Itoa(int(info.UserId))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(info.Content) != 0 {
|
|
|
|
querySql += " and contents LIKE '%" + info.Content + "%'"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(info.StartTime) != 0 && len(info.EndTime) != 0 {
|
|
|
|
querySql += " and created_at >= '" + info.StartTime + "' and created_at <= '"+ info.EndTime + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
querySql += " LIMIT ? OFFSET ? "
|
|
|
|
err = global.GLOBAL_DB.Raw( querySql, reportIds, limit, offset).Scan(&reportsSearchBOList).Error
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
for _, searchBO := range wtReportsList {
|
|
|
|
reportVO := reportToSearchResult(searchBO)
|
|
|
|
reportsSearchResults = append(reportsSearchResults, reportVO)
|
|
|
|
}
|
|
|
|
return reportsSearchResults
|
|
|
|
}
|
|
|
|
|
|
|
|
//单个转换
|
|
|
|
func reportToSearchResult(searchBO wtRes.WtReportsSearchBO) wtRes.WtReportsSearchResult {
|
|
|
|
searchResult := wtRes.WtReportsSearchResult{}
|
|
|
|
searchResult.GLOBAL_MODEL = searchBO.GLOBAL_MODEL
|
|
|
|
|
|
|
|
searchResult.UserName = searchBO.UserName
|
|
|
|
searchResult.Header = searchBO.Header
|
|
|
|
searchResult.CommentCount = searchBO.CommentCount
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|