- SendAdvertingByGoodsOrder
This commit is contained in:
87
business/jxstore/promotion/promotion.go
Normal file
87
business/jxstore/promotion/promotion.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package promotion
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/KenmyZhang/aliyun-communicate"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
)
|
||||
|
||||
const (
|
||||
DefGoodsOrderDays = 60
|
||||
MaxBatchSize = 500
|
||||
)
|
||||
|
||||
type tMobileInfo struct {
|
||||
Mobile string
|
||||
}
|
||||
|
||||
func SendAdvertingByGoodsOrder(advertising string, days int, isAsync bool, userName string) (hint string, err error) {
|
||||
globals.SugarLogger.Debugf("SendAdvertingByGoodsOrder advertising:%s, days:%d, isAsync:%t, userName:%s", advertising, days, isAsync, userName)
|
||||
|
||||
if days == 0 {
|
||||
days = DefGoodsOrderDays
|
||||
}
|
||||
sqlParams := []interface{}{}
|
||||
sql1 := `
|
||||
SELECT consignee_mobile mobile
|
||||
FROM goods_order t1
|
||||
LEFT JOIN black_client t2 ON t1.consignee_mobile = t2.mobile
|
||||
WHERE consignee_mobile IS NOT NULL AND consignee_mobile <> '' AND t2.id IS NULL AND LENGTH(consignee_mobile) = 11 AND LEFT(consignee_mobile, 1) = '1'
|
||||
`
|
||||
sql2 := `
|
||||
SELECT buyer_mobile mobile
|
||||
FROM jxorder t1
|
||||
LEFT JOIN black_client t2 ON t1.buyer_mobile = t2.mobile
|
||||
WHERE buyer_mobile IS NOT NULL AND buyer_mobile <> '' AND t2.id IS NULL AND LENGTH(buyer_mobile) = 11 AND LEFT(buyer_mobile, 1) = '1'
|
||||
`
|
||||
if days > 0 {
|
||||
sql1 += " AND order_created_at > ?"
|
||||
sql2 += " AND order_start_time > ?"
|
||||
daysTime := time.Now().Add(-time.Duration(days) * 24 * time.Hour)
|
||||
sqlParams = append(sqlParams, daysTime, daysTime)
|
||||
}
|
||||
|
||||
var mobiles []*tMobileInfo
|
||||
db := dao.GetDB()
|
||||
if err = dao.GetRows(db, &mobiles, sql1+" UNION DISTINCT "+sql2, sqlParams...); err != nil {
|
||||
return hint, err
|
||||
}
|
||||
index := 0
|
||||
mobileNumbers := make([]string, len(mobiles))
|
||||
for _, v := range mobiles {
|
||||
if v.Mobile[:1] == "1" && strings.Index(v.Mobile, ",") == -1 {
|
||||
mobileNumbers[index] = v.Mobile
|
||||
index++
|
||||
}
|
||||
}
|
||||
mobileNumbers = mobileNumbers[:index]
|
||||
mobileNumbers = append(mobileNumbers, "18180948107")
|
||||
|
||||
smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/")
|
||||
task := tasksch.RunManagedTask("SendAdvertingByGoodsOrder", true, nil, 0, MaxBatchSize, userName, func(batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
numbers := make([]string, len(batchItemList))
|
||||
for k, v := range batchItemList {
|
||||
numbers[k] = v.(string)
|
||||
}
|
||||
numberStr := strings.Join(numbers, ",")
|
||||
_, err = smsClient.Execute(globals.AliKey, globals.AliSecret, numberStr, "京西菜市", "SMS_109345355", string(utils.MustMarshal(map[string]interface{}{
|
||||
"code": "code",
|
||||
})))
|
||||
if err != nil {
|
||||
globals.SugarLogger.Infof("SendAdvertingByGoodsOrder numbers:%s failed with error:%v", numberStr, err)
|
||||
}
|
||||
return nil, err
|
||||
}, mobileNumbers)
|
||||
|
||||
hint = task.ID
|
||||
if !isAsync {
|
||||
_, err = task.GetResult(0)
|
||||
}
|
||||
return hint, err
|
||||
}
|
||||
@@ -84,3 +84,19 @@ func (c *PromotionController) CreatePromotionByExcel() {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// @Title 创建促销
|
||||
// @Description 创建促销
|
||||
// @Param token header string true "认证token"
|
||||
// @Param advertising formData string true "广告语"
|
||||
// @Param days formData int false "多少天以内订单数据中的用户(-1:全部,0:60天,缺省0)"
|
||||
// @Param isAsync formData bool false "是否异步,缺省否"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /SendAdvertingByGoodsOrder [post]
|
||||
func (c *PromotionController) SendAdvertingByGoodsOrder() {
|
||||
c.callSendAdvertingByGoodsOrder(func(params *tPromotionSendAdvertingByGoodsOrderParams) (retVal interface{}, errCode string, err error) {
|
||||
retVal, err = promotion.SendAdvertingByGoodsOrder(params.Advertising, params.Days, params.IsAsync, GetUserNameFromToken(params.Token))
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -215,6 +215,14 @@ func init() {
|
||||
MethodParams: param.Make(),
|
||||
Params: nil})
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:PromotionController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:PromotionController"],
|
||||
beego.ControllerComments{
|
||||
Method: "SendAdvertingByGoodsOrder",
|
||||
Router: `/SendAdvertingByGoodsOrder`,
|
||||
AllowHTTPMethods: []string{"post"},
|
||||
MethodParams: param.Make(),
|
||||
Params: nil})
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SkuController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SkuController"],
|
||||
beego.ControllerComments{
|
||||
Method: "AddCategory",
|
||||
|
||||
Reference in New Issue
Block a user