作者 陈志颖

test:排行榜管理

@@ -67,10 +67,10 @@ func (listIntervalService *ListIntervalService) CreateListInterval(createListInt @@ -67,10 +67,10 @@ func (listIntervalService *ListIntervalService) CreateListInterval(createListInt
67 startTime := time.Date(createListIntervalCommand.ListIntervalStartTime.Local().Year(), createListIntervalCommand.ListIntervalStartTime.Local().Month(), createListIntervalCommand.ListIntervalStartTime.Local().Day(), 0, 0, 0, 0, time.Local) 67 startTime := time.Date(createListIntervalCommand.ListIntervalStartTime.Local().Year(), createListIntervalCommand.ListIntervalStartTime.Local().Month(), createListIntervalCommand.ListIntervalStartTime.Local().Day(), 0, 0, 0, 0, time.Local)
68 endTime := time.Date(createListIntervalCommand.ListIntervalEndTime.Local().Year(), createListIntervalCommand.ListIntervalEndTime.Local().Month(), createListIntervalCommand.ListIntervalEndTime.Local().Day(), 23, 59, 59, 0, time.Local) 68 endTime := time.Date(createListIntervalCommand.ListIntervalEndTime.Local().Year(), createListIntervalCommand.ListIntervalEndTime.Local().Month(), createListIntervalCommand.ListIntervalEndTime.Local().Day(), 23, 59, 59, 0, time.Local)
69 69
  70 + // 判断年榜时间是否重叠
70 if len(listIntervals) > 0 { 71 if len(listIntervals) > 0 {
71 - ok := listIntervalDao.RankPeriodCheckTime(startTime, endTime, 0, listIntervals[0].CompanyId)  
72 - fmt.Print(ok, "\n")  
73 - if ok { 72 + ok := listIntervalDao.RankPeriodCheckTime(startTime.Local().Unix(), endTime.Local().Unix(), 0, listIntervals[0].CompanyId)
  73 + if !ok {
74 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "年榜时间不能重叠") 74 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "年榜时间不能重叠")
75 } 75 }
76 } 76 }
@@ -89,33 +89,6 @@ func (listIntervalService *ListIntervalService) CreateListInterval(createListInt @@ -89,33 +89,6 @@ func (listIntervalService *ListIntervalService) CreateListInterval(createListInt
89 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 89 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
90 } 90 }
91 return listInterval, nil 91 return listInterval, nil
92 -  
93 - // 判断年榜时间是否和上个年榜重叠  
94 - //if len(listIntervals) > 0 {  
95 - //if startTime.Before(listIntervals[0].IntervalEndTime.Local()) || startTime.Equal(listIntervals[0].IntervalEndTime.Local()){  
96 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "年榜开始时间必须大于上一个年榜结束时间")  
97 - //} else {  
98 - // // 创建排行榜时间命令  
99 - // newListInterval := &domain.ListInterval {  
100 - // CompanyId: createListIntervalCommand.CompanyId,  
101 - // IntervalStartTime: startTime,  
102 - // IntervalEndTime: endTime,  
103 - // }  
104 - //  
105 - // // 保存排行榜时间  
106 - // listInterval, err := listIntervalRepository.Save(newListInterval)  
107 - // if err != nil {  
108 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
109 - // }  
110 - // if err := transactionContext.CommitTransaction(); err != nil {  
111 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
112 - // }  
113 - //  
114 - // return listInterval, nil  
115 - //}  
116 - //} else {  
117 -  
118 - //}  
119 } 92 }
120 93
121 // 返回排行榜时间列表 94 // 返回排行榜时间列表
@@ -200,6 +173,7 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt @@ -200,6 +173,7 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt
200 if err := updateListIntervalCommand.ValidateCommand(); err != nil { 173 if err := updateListIntervalCommand.ValidateCommand(); err != nil {
201 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 174 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
202 } 175 }
  176 +
203 transactionContext, err := factory.CreateTransactionContext(nil) 177 transactionContext, err := factory.CreateTransactionContext(nil)
204 if err != nil { 178 if err != nil {
205 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 179 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -240,18 +214,6 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt @@ -240,18 +214,6 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt
240 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateListIntervalCommand.ListIntervalId))) 214 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateListIntervalCommand.ListIntervalId)))
241 } 215 }
242 216
243 - //// 获取所有年榜  
244 - //listListIntervalQuery := &query.ListListIntervalQuery {  
245 - // CompanyId: listIntervalFound.CompanyId,  
246 - //}  
247 - ////获取所有年榜  
248 - //_, listIntervals, err := listIntervalRepository.Find(tool_funs.SimpleStructToMap(listListIntervalQuery))  
249 - //if err != nil {  
250 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
251 - //}  
252 - //  
253 - //var currentListIntervalIndex int  
254 -  
255 // 当前更新的开始结束时间 217 // 当前更新的开始结束时间
256 startTime := time.Date(updateListIntervalCommand.ListIntervalStartTime.Local().Year(), updateListIntervalCommand.ListIntervalStartTime.Local().Month(), updateListIntervalCommand.ListIntervalStartTime.Local().Day(), 0, 0, 0, 0, time.Local) 218 startTime := time.Date(updateListIntervalCommand.ListIntervalStartTime.Local().Year(), updateListIntervalCommand.ListIntervalStartTime.Local().Month(), updateListIntervalCommand.ListIntervalStartTime.Local().Day(), 0, 0, 0, 0, time.Local)
257 endTime := time.Date(updateListIntervalCommand.ListIntervalEndTime.Local().Year(), updateListIntervalCommand.ListIntervalEndTime.Local().Month(), updateListIntervalCommand.ListIntervalEndTime.Local().Day(), 23, 59, 59, 0, time.Local) 219 endTime := time.Date(updateListIntervalCommand.ListIntervalEndTime.Local().Year(), updateListIntervalCommand.ListIntervalEndTime.Local().Month(), updateListIntervalCommand.ListIntervalEndTime.Local().Day(), 23, 59, 59, 0, time.Local)
@@ -259,11 +221,13 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt @@ -259,11 +221,13 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt
259 updateListIntervalCommand.ListIntervalStartTime = startTime 221 updateListIntervalCommand.ListIntervalStartTime = startTime
260 updateListIntervalCommand.ListIntervalEndTime = endTime 222 updateListIntervalCommand.ListIntervalEndTime = endTime
261 223
262 - ok := listIntervalDao.RankPeriodCheckTime(startTime, endTime, listIntervalFound.ListIntervalId, listIntervalFound.CompanyId)  
263 - if ok { 224 + // 判断年榜时间是否重叠
  225 + ok := listIntervalDao.RankPeriodCheckTime(startTime.Unix(), endTime.Unix(), listIntervalFound.ListIntervalId, listIntervalFound.CompanyId)
  226 + if !ok {
264 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "年榜时间不能重叠") 227 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "年榜时间不能重叠")
265 } 228 }
266 229
  230 + // 更新年榜
267 if err := listIntervalFound.Update(tool_funs.SimpleStructToMap(updateListIntervalCommand)); err != nil { 231 if err := listIntervalFound.Update(tool_funs.SimpleStructToMap(updateListIntervalCommand)); err != nil {
268 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 232 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
269 } 233 }
@@ -275,69 +239,6 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt @@ -275,69 +239,6 @@ func (listIntervalService *ListIntervalService) UpdateListInterval(updateListInt
275 } 239 }
276 return listIntervalUpdated, nil 240 return listIntervalUpdated, nil
277 } 241 }
278 -  
279 - // 判断年榜时间是否和上个年榜重叠  
280 - //if len(listIntervals) > 0 {  
281 - // // 找到当前排行榜所处位置  
282 - // for i, listInterval := range listIntervals {  
283 - // if listInterval.ListIntervalId == listIntervalFound.ListIntervalId {  
284 - // currentListIntervalIndex = i  
285 - // }  
286 - // }  
287 - //  
288 - // if currentListIntervalIndex == 0 && len(listIntervals) > 1 { // 当前排行榜在首位,且不止一个活动  
289 - // // 上一个活动开始结束时间  
290 - // //lastIntervalStartTime := listIntervals[currentListIntervalIndex + 1].IntervalStartTime.Local()  
291 - // lastIntervalEndTime := listIntervals[currentListIntervalIndex + 1].IntervalEndTime.Local()  
292 - //  
293 - // if startTime.Before(lastIntervalEndTime) || startTime.Equal(lastIntervalEndTime) { // 开始时间重叠判断  
294 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "年榜时间不能重叠")  
295 - // }  
296 - // } else if currentListIntervalIndex != 0 { // 当前排行榜不在首位  
297 - // fmt.Print(currentListIntervalIndex, "\n")  
298 - //  
299 - // // 上一个年榜开始结束时间  
300 - // lastIntervalStartTime := listIntervals[currentListIntervalIndex + 1].IntervalStartTime.Local()  
301 - // lastIntervalEndTime := listIntervals[currentListIntervalIndex + 1].IntervalEndTime.Local()  
302 - // fmt.Print(lastIntervalEndTime,"\n")  
303 - //  
304 - // // 下一个年榜开始结束时间  
305 - // nextIntervalStartTime := listIntervals[currentListIntervalIndex - 1].IntervalStartTime.Local()  
306 - // fmt.Print(nextIntervalStartTime, "\n")  
307 - // nextIntervalEndTime := listIntervals[currentListIntervalIndex - 1].IntervalEndTime.Local()  
308 - //  
309 - // fmt.Print(startTime, "\n")  
310 - // fmt.Print(endTime, "\n")  
311 - //  
312 - // if startTime.After(lastIntervalStartTime) && startTime.Before(lastIntervalEndTime) || endTime.After(nextIntervalStartTime) && endTime.Before(nextIntervalEndTime) {  
313 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "年榜时间不能重叠")  
314 - // }  
315 - // }  
316 - //  
317 - // if err := listIntervalFound.Update(tool_funs.SimpleStructToMap(updateListIntervalCommand)); err != nil {  
318 - // return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
319 - // }  
320 - // if listIntervalUpdated, err := listIntervalRepository.Save(listIntervalFound); err != nil {  
321 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
322 - // } else {  
323 - // if err := transactionContext.CommitTransaction(); err != nil {  
324 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
325 - // }  
326 - // return listIntervalUpdated, nil  
327 - // }  
328 - //} else { // 正常更新  
329 - // if err := listIntervalFound.Update(tool_funs.SimpleStructToMap(updateListIntervalCommand)); err != nil {  
330 - // return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
331 - // }  
332 - // if listIntervalUpdated, err := listIntervalRepository.Save(listIntervalFound); err != nil {  
333 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
334 - // } else {  
335 - // if err := transactionContext.CommitTransaction(); err != nil {  
336 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
337 - // }  
338 - // return listIntervalUpdated, nil  
339 - // }  
340 - //}  
341 } 242 }
342 243
343 // 移除排行榜时间 244 // 移除排行榜时间
@@ -4,7 +4,6 @@ import ( @@ -4,7 +4,6 @@ import (
4 "fmt" 4 "fmt"
5 "github.com/go-pg/pg" 5 "github.com/go-pg/pg"
6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
7 - "time"  
8 ) 7 )
9 8
10 type ListIntervalDao struct { 9 type ListIntervalDao struct {
@@ -12,33 +11,37 @@ type ListIntervalDao struct { @@ -12,33 +11,37 @@ type ListIntervalDao struct {
12 } 11 }
13 12
14 // 榜单时间管理 13 // 榜单时间管理
15 -func (dao *ListIntervalDao) RankPeriodCheckTime(startTime time.Time, endTime time.Time, idNot int, companyId int) bool { 14 +func (dao *ListIntervalDao) RankPeriodCheckTime(startTime int64, endTime int64, idNot int, companyId int) bool {
16 fmt.Print(startTime, "\n") 15 fmt.Print(startTime, "\n")
17 fmt.Print(endTime, "\n") 16 fmt.Print(endTime, "\n")
  17 + var count int
18 tx := dao.transactionContext.PgTx 18 tx := dao.transactionContext.PgTx
19 - _, err := tx.Query(  
20 - pg.Scan(), 19 + _, err := tx.QueryOne(
  20 + pg.Scan(&count),
21 `SELECT count(*) FROM list_intervals 21 `SELECT count(*) FROM list_intervals
22 WHERE id <> ? 22 WHERE id <> ?
23 AND company_id = ? 23 AND company_id = ?
24 AND 24 AND
25 ( 25 (
26 - ( to_char(list_interval_start_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS') BETWEEN ? AND ?) 26 + ( extract(epoch from list_interval_start_time) BETWEEN ? AND ?)
27 OR 27 OR
28 - ( to_char(list_interval_end_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS') BETWEEN ? AND ?) 28 + ( extract(epoch from list_interval_end_time) BETWEEN ? AND ?)
29 OR 29 OR
30 - (? BETWEEN to_char(list_interval_start_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS') AND to_char(list_interval_end_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS')) 30 + (? BETWEEN extract(epoch from list_interval_start_time) AND extract(epoch from list_interval_end_time))
31 OR 31 OR
32 - (? BETWEEN to_char(list_interval_start_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS') AND to_char(list_interval_end_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS')) 32 + (? BETWEEN extract(epoch from list_interval_start_time) AND extract(epoch from list_interval_end_time))
33 ) 33 )
34 LIMIT 1 `, 34 LIMIT 1 `,
35 idNot, companyId, startTime, endTime, startTime, endTime, startTime, endTime) 35 idNot, companyId, startTime, endTime, startTime, endTime, startTime, endTime)
36 if err != nil { 36 if err != nil {
37 fmt.Errorf(err.Error()) 37 fmt.Errorf(err.Error())
38 - fmt.Print(err.Error(), "\n")  
39 - return true 38 + return false
40 } 39 }
  40 + if count > 0 {
  41 + fmt.Print(count, "\n")
41 return false 42 return false
  43 + }
  44 + return true
42 } 45 }
43 46
44 func NewListIntervalDao(transactionContext *pgTransaction.TransactionContext) (*ListIntervalDao, error) { 47 func NewListIntervalDao(transactionContext *pgTransaction.TransactionContext) (*ListIntervalDao, error) {