- 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
|
||||
|
||||
@@ -337,6 +337,30 @@ func NotifyStoreOpRequestStatus(isAccepted bool, storeID, nameID int, spuName st
|
||||
return SendMsgToStore(storeID, templateID, fileURL, data)
|
||||
}
|
||||
|
||||
func NotifyStoreMessage(storeID, msgID, msgStatusID int, title, content string) (err error) {
|
||||
globals.SugarLogger.Debugf("NotifyStoreMessage storeID:%d, msgID:%d, title:%s, content:%s", storeID, msgID, title, content)
|
||||
templateID := "gIG2olBZtQbjXmp6doNB_dESu60By5xuXYOGxksLv3Y"
|
||||
fileURL := fmt.Sprintf("%s%d", WX_TO_STORE_SKU_PAGE_URL, storeID)
|
||||
data := map[string]interface{}{
|
||||
"first": map[string]interface{}{
|
||||
"value": title,
|
||||
"color": "#333333",
|
||||
},
|
||||
"keyword1": map[string]interface{}{
|
||||
"value": content,
|
||||
"color": "#2E408E",
|
||||
},
|
||||
"keyword2": map[string]interface{}{
|
||||
"value": utils.Time2Str(time.Now()),
|
||||
"color": "#2E408E",
|
||||
},
|
||||
"remark": map[string]interface{}{
|
||||
"value": "点击查看详情",
|
||||
},
|
||||
}
|
||||
return SendMsgToStore(storeID, templateID, fileURL, data)
|
||||
}
|
||||
|
||||
func FormatDeliveryTime(order *model.GoodsOrder) string {
|
||||
var tmpTime time.Time
|
||||
if order.ExpectedDeliveredTime == utils.DefaultTimeValue {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DefPageSize = 50
|
||||
UnlimitedPageSize = 999999999
|
||||
UnlimitedPageSize = math.MaxInt32
|
||||
)
|
||||
|
||||
type GoodsOrderExt struct {
|
||||
|
||||
@@ -137,6 +137,17 @@ func CreateEntity(db *DaoDB, item interface{}) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertMulti执行成功后ID不会改写成正确的(象Insert一样)
|
||||
func CreateMultiEntities(db *DaoDB, item interface{}) (err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
}
|
||||
if _, err = db.db.InsertMulti(20, item); err != nil && !IsDuplicateError(err) {
|
||||
globals.SugarLogger.Errorf("CreateEntity %s failed with error:%v", reflect.TypeOf(item).Name(), err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func CreateOrUpdate(db *DaoDB, item interface{}, colConflitAndArgs ...string) (err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
|
||||
@@ -9,19 +9,33 @@ const (
|
||||
MessageStatusSendAllSuccess = 1
|
||||
MessageStatusSendSuccess = 2
|
||||
MessageStatusSendAllFailed = 3
|
||||
MessageStatusRead = 4
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ModelIDCULD
|
||||
Type int8
|
||||
Title string
|
||||
Content string
|
||||
Type int8 `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func (*Message) TableIndex() [][]string {
|
||||
return [][]string{
|
||||
[]string{"CreatedAt"},
|
||||
[]string{"DeletedAt"},
|
||||
}
|
||||
}
|
||||
|
||||
type MessageStatus struct {
|
||||
ModelIDCULD
|
||||
MessageID int
|
||||
StoreID int
|
||||
Status int8
|
||||
MessageID int `orm:"column(message_id)" json:"messageID"`
|
||||
StoreID int `orm:"column(store_id)" json:"storeID"`
|
||||
Status int8 `json:"status"`
|
||||
ReadCount int `json:"readCount"`
|
||||
}
|
||||
|
||||
func (*MessageStatus) TableIndex() [][]string {
|
||||
return [][]string{
|
||||
[]string{"MessageID", "Status"},
|
||||
[]string{"StoreID", "MessageID", "Status"},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user