178 lines
5.7 KiB
Go
178 lines
5.7 KiB
Go
package cms
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/weixinmsg"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
)
|
|
|
|
func SendStoreMessage(ctx *jxcontext.Context, title, content string, storeIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
|
db := dao.GetDB()
|
|
dao.Begin(db)
|
|
defer dao.Rollback(db)
|
|
msg := &model.Message{
|
|
Title: title,
|
|
Content: content,
|
|
Type: model.MessageTypeStore,
|
|
}
|
|
dao.WrapAddIDCULDEntity(msg, ctx.GetUserName())
|
|
if err = dao.CreateEntity(db, msg); err != nil {
|
|
return "", err
|
|
}
|
|
msgStatusList := make([]*model.MessageStatus, len(storeIDs))
|
|
for k, storeID := range storeIDs {
|
|
msgStatus := &model.MessageStatus{
|
|
MessageID: msg.ID,
|
|
StoreID: storeID,
|
|
Status: model.MessageStatusNew,
|
|
}
|
|
dao.WrapAddIDCULDEntity(msgStatus, ctx.GetUserName())
|
|
if err = dao.CreateEntity(db, msgStatus); err != nil {
|
|
return "", err
|
|
}
|
|
msgStatusList[k] = msgStatus
|
|
}
|
|
dao.Commit(db)
|
|
|
|
rootTask := tasksch.NewParallelTask("SendStoreMessage", nil, ctx.GetUserName(), func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
db := dao.GetDB()
|
|
msgStatus := batchItemList[0].(*model.MessageStatus)
|
|
if err = weixinmsg.NotifyStoreMessage(msgStatus.StoreID, msgStatus.MessageID, msgStatus.ID, msg.Title, msg.Content); err == nil {
|
|
msgStatus.Status = model.MessageStatusSendAllSuccess
|
|
} else {
|
|
msgStatus.Status = model.MessageStatusSendAllFailed
|
|
}
|
|
dao.WrapUpdateULEntity(msgStatus, ctx.GetUserName())
|
|
globals.SugarLogger.Debug(utils.Format4Output(msgStatus, false))
|
|
_, err = dao.UpdateEntity(db, msgStatus)
|
|
return nil, err
|
|
}, msgStatusList)
|
|
tasksch.ManageTask(rootTask).Run()
|
|
|
|
if !isAsync {
|
|
_, err = rootTask.GetResult(0)
|
|
} else {
|
|
hint = rootTask.ID
|
|
}
|
|
return "", err
|
|
}
|
|
|
|
func ReadStoreMessage(ctx *jxcontext.Context, msgID, msgStatusID int) (redirectURL string, err error) {
|
|
msgStatus := &model.MessageStatus{}
|
|
msgStatus.ID = msgStatusID
|
|
db := dao.GetDB()
|
|
if err = dao.GetEntity(db, msgStatus); err != nil {
|
|
return redirectURL, err
|
|
}
|
|
msgStatus.ReadCount++
|
|
dao.WrapUpdateULEntity(msgStatus, ctx.GetUserName())
|
|
_, err = dao.UpdateEntity(db, msgStatus)
|
|
return redirectURL, err
|
|
}
|
|
|
|
func GetStoreMessages(ctx *jxcontext.Context, msgIDs, storeIDs, types []int, fromTime, toTime time.Time, keyword string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
|
|
sql := `
|
|
SELECT SQL_CALC_FOUND_ROWS t1.*
|
|
FROM message t1
|
|
WHERE 1 = 1
|
|
`
|
|
sqlParams := []interface{}{}
|
|
if len(storeIDs) > 0 {
|
|
sql += " AND (SELECT COUNT(*) FROM message_status t2 WHERE t2.message_id = t1.id AND t2.store_id IN (" + dao.GenQuestionMarks(len(storeIDs)) + ") ) > 0"
|
|
sqlParams = append(sqlParams, storeIDs)
|
|
}
|
|
if len(msgIDs) > 0 {
|
|
sql += " AND t1.id IN (" + dao.GenQuestionMarks(len(msgIDs)) + ")"
|
|
sqlParams = append(sqlParams, msgIDs)
|
|
}
|
|
if len(types) > 0 {
|
|
sql += " AND t1.type IN (" + dao.GenQuestionMarks(len(types)) + ")"
|
|
sqlParams = append(sqlParams, types)
|
|
}
|
|
if fromTime != utils.DefaultTimeValue {
|
|
sql += " AND t1.created_at >= ?"
|
|
sqlParams = append(sqlParams, fromTime)
|
|
}
|
|
if toTime != utils.DefaultTimeValue {
|
|
sql += " AND t1.created_at <= ?"
|
|
sqlParams = append(sqlParams, toTime)
|
|
}
|
|
if keyword != "" {
|
|
keywordLike := "%" + keyword + "%"
|
|
sql += " AND (t1.title LIKE ? OR t1.content LIKE ?)"
|
|
sqlParams = append(sqlParams, keywordLike, keywordLike)
|
|
}
|
|
sql += " LIMIT ? OFFSET ?"
|
|
pageSize = jxutils.FormalizePageSize(pageSize)
|
|
sqlParams = append(sqlParams, pageSize, offset)
|
|
db := dao.GetDB()
|
|
dao.Begin(db)
|
|
defer dao.Commit(db)
|
|
var msgList []*model.Message
|
|
if err = dao.GetRows(db, &msgList, sql, sqlParams...); err == nil {
|
|
pagedInfo = &model.PagedInfo{
|
|
TotalCount: dao.GetLastTotalRowCount(db),
|
|
Data: msgList,
|
|
}
|
|
}
|
|
return pagedInfo, err
|
|
}
|
|
|
|
func GetStoreMessageStatuses(ctx *jxcontext.Context, msgIDs, storeIDs []int, fromReadCount, toReadCount int, fromTime, toTime time.Time, keyword string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
|
|
sql := `
|
|
SELECT SQL_CALC_FOUND_ROWS t1.*
|
|
FROM message_status t1
|
|
WHERE 1 = 1
|
|
`
|
|
sqlParams := []interface{}{}
|
|
if len(storeIDs) > 0 {
|
|
sql += " AND t1.store_id IN (" + dao.GenQuestionMarks(len(storeIDs)) + ")"
|
|
sqlParams = append(sqlParams, storeIDs)
|
|
}
|
|
if len(msgIDs) > 0 {
|
|
sql += " AND t1.message_id IN (" + dao.GenQuestionMarks(len(msgIDs)) + ")"
|
|
sqlParams = append(sqlParams, msgIDs)
|
|
}
|
|
if fromTime != utils.DefaultTimeValue {
|
|
sql += " AND t1.created_at >= ?"
|
|
sqlParams = append(sqlParams, fromTime)
|
|
}
|
|
if toTime != utils.DefaultTimeValue {
|
|
sql += " AND t1.created_at <= ?"
|
|
sqlParams = append(sqlParams, toTime)
|
|
}
|
|
if fromReadCount >= 0 {
|
|
sql += " AND t1.read_count >= ?"
|
|
sqlParams = append(sqlParams, fromReadCount)
|
|
}
|
|
if toReadCount >= 0 {
|
|
sql += " AND t1.read_count <= ?"
|
|
sqlParams = append(sqlParams, toReadCount)
|
|
}
|
|
if keyword != "" {
|
|
keywordLike := "%" + keyword + "%"
|
|
sql += " AND (t1.last_operator LIKE ?)"
|
|
sqlParams = append(sqlParams, keywordLike)
|
|
}
|
|
sql += " LIMIT ? OFFSET ?"
|
|
sqlParams = append(sqlParams, jxutils.FormalizePageSize(pageSize), offset)
|
|
db := dao.GetDB()
|
|
dao.Begin(db)
|
|
defer dao.Commit(db)
|
|
var msgStatusList []*model.MessageStatus
|
|
if err = dao.GetRows(db, &msgStatusList, sql, sqlParams...); err == nil {
|
|
pagedInfo = &model.PagedInfo{
|
|
TotalCount: dao.GetLastTotalRowCount(db),
|
|
Data: msgStatusList,
|
|
}
|
|
}
|
|
return pagedInfo, err
|
|
}
|