- normal store weixin msg.
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
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) (err error) {
|
||||
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)
|
||||
@@ -15,19 +22,156 @@ func SendStoreMessage(ctx *jxcontext.Context, title, content string, storeIDs []
|
||||
Content: content,
|
||||
Type: model.MessageTypeStore,
|
||||
}
|
||||
dao.WrapAddIDCULDEntity(msg, ctx.GetUserName())
|
||||
if err = dao.CreateEntity(db, msg); err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
for _, storeID := range storeIDs {
|
||||
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
|
||||
return "", err
|
||||
}
|
||||
msgStatusList[k] = msgStatus
|
||||
}
|
||||
dao.Commit(db)
|
||||
return err
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
14
business/jxstore/cms/message_test.go
Normal file
14
business/jxstore/cms/message_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package cms
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
)
|
||||
|
||||
func TestSendStoreMessage(t *testing.T) {
|
||||
err := SendStoreMessage(jxcontext.AdminCtx, "title", "content", []int{1}, false, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -324,7 +324,7 @@ func CreateStore(ctx *jxcontext.Context, storeExt *StoreExt, userName string) (i
|
||||
globals.SugarLogger.Debugf("CreateStore storeExt:%s", utils.Format4Output(storeExt, false))
|
||||
store := &storeExt.Store
|
||||
if store.ID != 0 && !jxutils.IsLegalStoreID(store.ID) {
|
||||
return 0, fmt.Errorf("ID:%d不是合法的京西门店编号")
|
||||
return 0, fmt.Errorf("ID:%d不是合法的京西门店编号", store.ID)
|
||||
}
|
||||
|
||||
existingID := store.ID
|
||||
|
||||
Reference in New Issue
Block a user