Commit 70bf6c37 by 朱继来

添加bom询价无结果队列

parent 4b619e12
package main
import (
"crm-server/internal/dao"
"encoding/json"
"flag"
"fmt"
"crm-server/configs"
"github.com/ichunt2019/logger"
"github.com/streadway/amqp"
"crm-server/internal/service"
"strconv"
"time"
)
var LogDir string
// 解析命令行参数
func initArgs() {
// worker -config ./worker.json
// worker -h
flag.StringVar(&LogDir, "logDir", "", "日志目录")
flag.Parse()
}
type BomNoResult struct {
UserId int `json:"user_id"`
BomUrl string `json:"bom_url"`
}
// 钉钉告警
func sendMsg(msg_text string) {
// 默认参数
var mobile []string = make([]string, 0)
var isAtAll bool = false
service.DingSend(configs.Ding_crm_msg, "队列任务", msg_text, mobile, isAtAll)
}
// 用户账号
type Account struct {
Mobile string
Email string
}
func main() {
initArgs()
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir+"/bom_no_result"
logConfig["log_chan_size"] = "10"
logger.InitLogger("file",logConfig)
logger.Init()
conn, err := amqp.Dial("amqp://"+configs.RABBITMQDSN+"/")
if err != nil {
logger.Info("Failed to connect to RabbitMQ ", err.Error())
sendMsg("Bom推送到CRM,连接MQ失败,原因:"+err.Error())
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
logger.Info("Failed to open a channel ", err.Error())
sendMsg("Bom推送到CRM,打开channel失败,原因:"+err.Error())
}
defer ch.Close()
q, err := ch.QueueDeclare(
"crm_bom_no_result", // name
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
logger.Info("Failed to declare a queue ", err.Error())
sendMsg("Bom推送到CRM,声明queue失败,原因:"+err.Error())
}
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
logger.Info("Failed to register a consumer ", err.Error())
sendMsg("Bom推送到CRM,注册消费者失败,原因:"+err.Error())
}
forever := make(chan bool)
var bom_on_result BomNoResult
go func() {
for d := range msgs {
fmt.Println(string(d.Body))
sendMsg("Bom询价无结果参数:"+string(d.Body))
json.Unmarshal(d.Body, &bom_on_result)
consume(bom_on_result)
}
}()
<-forever
}
// 处理
func consume(bom BomNoResult) {
// 获取crm的用户ID
var crmUserId int
err := dao.GetDb().Get(&crmUserId, "select user_id from lie_user where outter_uid = ?", bom.UserId)
if err != nil {
logger.Info("Failed to connect crm db ", err.Error())
sendMsg("Bom推送到CRM,连接CRM数据库失败,原因:"+err.Error())
}
if crmUserId == 0 { // 不存在推送告警
sendMsg("Bom推送到CRM,用户ID("+strconv.Itoa(bom.UserId)+")CRM不存在")
return
}
// 查询feedback bom询价无结果类型type=10
var fid int
dao.GetDb().Get(&fid, "select id from lie_feedback where user_id = ? and outter_uid = ? and type = 10", crmUserId, bom.UserId)
if fid == 0 { // 不存在,则添加到feedback表
create_time := time.Now().Unix()
dao.GetDb().Exec("insert into lie_feedback (user_id, outter_uid, type, bom_url, create_time) values (?, ?, ?, ?, ?)", crmUserId, bom.UserId, 10, bom.BomUrl, create_time)
} else { // 更新
dao.GetDb().Exec("update lie_feedback set bom_url = ? where user_id = ? and type = 10", bom.BomUrl, crmUserId)
}
// 若存在客服,推送钉钉消息
var sale_id int
dao.GetDb().Get(&sale_id, "select sale_id from lie_salesman where user_id = ?", crmUserId)
if sale_id != 0 {
var sale_uid int64 // 客服前台ID
dao.GetCmsDb().Get(&sale_uid, "select user_id from lie_intracode where admin_id = ?", 1357)
if sale_uid == 0 {
sendMsg("Bom推送到CRM,内部用户ID("+strconv.Itoa(sale_id)+")未绑定前台账号")
return
}
var account Account
var client_account string
dao.GetLiexinDb().Get(&account,"select mobile, email from lie_user_main where user_id = ?", bom.UserId)
if account.Mobile != "" {
client_account = account.Mobile
} else {
client_account = account.Email
}
service.SendBomMsg(sale_uid, client_account)
}
}
......@@ -6,6 +6,7 @@ require (
github.com/bilibili/kratos v0.0.0-20191025092737-e14170de04ba
github.com/gogo/protobuf v1.3.0
github.com/golang/protobuf v1.3.2
github.com/ichunt2019/logger v1.0.5
github.com/jmoiron/sqlx v1.2.0
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/pkg/errors v0.8.1
......
package service
import (
"encoding/json"
_ "fmt"
"github.com/ichunt2019/logger"
"io/ioutil"
"net/http"
"strings"
)
type Msg struct {
Msgtype string `json:"msgtype"`
Text Contents `json:"text"`
At Ats `json:"at"`
}
type Contents struct {
Content string `json:"content"`
}
type Ats struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}
// json 返回值
type JosnResp struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
}
func DingSend(ding_url string, ding_tag string, textMsg string, mobiles []string, isAtAll bool) (jsonStr string) {
var msg Msg
msg = Msg{Msgtype:"text"}
if ding_tag != "" {
msg.Text.Content = ding_tag + ":" + textMsg // 固定标签 + 文本
} else {
msg.Text.Content = textMsg // 文本
}
msg.At.AtMobiles = mobiles
msg.At.IsAtAll = isAtAll;
content, _ := json.Marshal(msg)
// content := `{
// "msgtype": "text",
// "text": {
// "content": "`+ msg + `"
// }
// }`
client := &http.Client{}
req, _ := http.NewRequest("POST", ding_url, strings.NewReader(string(content)))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req);
defer resp.Body.Close()
if err != nil {
logger.Info(err.Error())
}
body, _ := ioutil.ReadAll(resp.Body) // 获取接口返回数据
res := JosnResp{}
if err1 := json.Unmarshal([]byte(body), &res); err1 != nil { // 将接口数据写入res
logger.Info(err1.Error())
}
result, _ := json.Marshal(res)
return string(result)
}
\ No newline at end of file
......@@ -46,3 +46,30 @@ func SendMessage(mobile int64 , content string){
}
}
/**
bom询价无结果 钉钉推送
user_id 内部用户ID
client_account 客户账号
*/
func SendBomMsg(user_id int64 , client_account string){
if user_id != 0 {
timeNow := time.Now().Unix()
requestContent,_ := json.Marshal(map[string]string{"account":client_account})
requestTel,_ := json.Marshal([]int64{user_id})
resp,err := http.PostForm(APIDOMAIN,url.Values{
"data" : {string(requestContent)},
"touser" : {string(requestTel)},
"keyword" : {"bom_no_result"},
"k1" : {strconv.FormatInt(int64(timeNow),10)},
"k2" : {common.Md5(common.Md5(strconv.FormatInt(int64(timeNow),10))+APIMD5STR)},
"is_ignore" : {},
})
if err != nil {
fmt.Print(err)
}
defer resp.Body.Close()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment