package promotion import ( "strings" "time" "github.com/KenmyZhang/aliyun-communicate" "git.rosy.net.cn/baseapi/utils" "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/model/dao" "git.rosy.net.cn/jx-callback/globals" ) const ( DefGoodsOrderDays = 60 MaxBatchSize = 500 ) type tMobileInfo struct { Mobile string } func SendAdvertingByGoodsOrder(ctx *jxcontext.Context, 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 IF(t1.consignee_mobile2 <> '', t1.consignee_mobile2, t1.consignee_mobile) mobile FROM goods_order t1 LEFT JOIN black_client t2 ON IF(t1.consignee_mobile2 <> '', t1.consignee_mobile2, t1.consignee_mobile) = t2.mobile WHERE IF(t1.consignee_mobile2 <> '', t1.consignee_mobile2, t1.consignee_mobile) <> '' AND t2.id IS NULL AND LENGTH(IF(t1.consignee_mobile2 <> '', t1.consignee_mobile2, t1.consignee_mobile)) = 11 AND LEFT(IF(t1.consignee_mobile2 <> '', t1.consignee_mobile2, t1.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 "", 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.NewParallelTask("SendAdvertingByGoodsOrder", tasksch.NewParallelConfig().SetBatchSize(MaxBatchSize), ctx, func(t *tasksch.ParallelTask, 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) tasksch.HandleTask(task, nil, true).Run() if !isAsync { _, err = task.GetResult(0) } return task.ID, err }