Commit 91b37254 by 孙龙

up

parent 3470068c
go run ./order/bom_create_order/main.go -configDir=./config/ -logDir=./log/
\ No newline at end of file
......@@ -8,6 +8,7 @@ jingDiao="https://oapi.dingtalk.com/robot/send?access_token=eaeae1253e9c125a053b
tuangou_webhook="https://oapi.dingtalk.com/robot/send?access_token=a13d6128de5f918fc0f0a4cef70cab8b1962fca709649f7856aea4b1ca4dddf5"
erpLabel_webhook="https://oapi.dingtalk.com/robot/send?access_token=3966b80b7e56e8ffdff5ed2aefa1ea465c0f944a0db6ec5519eeef8721d1eb43"
yaoHaoYou_hook="https://oapi.dingtalk.com/robot/send?access_token=087c11504fb541d004507f06285da88c2501fc1a4ecacd0216b9da964aa65518"
bomCreateBom="https://oapi.dingtalk.com/robot/send?access_token=0fc661fcbb51d312780c0245986363a38c788c50c2e652e7f250df4f393ae938"
[Api_domain]
......
......@@ -8,6 +8,9 @@ dns="lie_labels:lie_labels#zsyM@tcp(192.168.2.232:3306)/lie_labels?parseTime=tru
[Liexincms_databases]
dns="ichuntcms:ichuntcms#zsyM@tcp(192.168.2.232:3306)/ichuntcms?parseTime=true"
[Bom_databases]
dns="root:root@tcp(192.168.2.239:3306)/lie_bom?parseTime=true"
[rabbitmq_ichunt]
queue_name="send_buyer_mail"
routing_key="send_buyer_mail"
......@@ -40,4 +43,12 @@ queue_name="ichunt_addorder_yaohaoyou"
routing_key="ichunt_addorder_yaohaoyou"
exchange=""
type="direct"
dns="amqp://guest:guest@192.168.2.232:5672/"
[rabbitmq_bomcreateorder]
queue_name="api_create_order_bom"
routing_key="api_create_order_bom"
exchange=""
type="direct"
dns="amqp://guest:guest@192.168.2.232:5672/"
\ No newline at end of file
......@@ -9,6 +9,7 @@ var (
DB *sqlx.DB
LabelDB *sqlx.DB
CmsDB *sqlx.DB
BomDB *sqlx.DB
)
func Init(dns string) error {
......@@ -42,6 +43,21 @@ func InitLabelDB(dns string) error{
LabelDB.SetMaxIdleConns(10)
return nil
}
func InitBomDB(dns string) error{
var err error
BomDB, err = sqlx.Open("mysql", dns)
if err != nil {
return err
}
err = BomDB.Ping()
if err != nil {
return err
}
BomDB.SetMaxOpenConns(50)
BomDB.SetMaxIdleConns(10)
return nil
}
func InitCmslDB(dns string) error{
var err error
CmsDB, err = sqlx.Open("mysql", dns)
......@@ -66,7 +82,9 @@ func CreateDB(action string,dns string) error{
case "InitLabelDB":
err = InitLabelDB(dns)
case "InitCmsDB":
err = InitCmslDB(dns)
err = InitCmslDB(dns)
case "BomDB":
err = InitBomDB(dns)
}
return err
}
......
package bom
import (
"errors"
"fmt"
"github.com/ichunt2019/logger"
"github.com/syyongx/php2go"
"go-queue-server/dal/db"
"strconv"
"time"
)
//下单需要处理的json数据
type CreateOrderBomData struct {
Order_id int64 `json:"order_id"`
Matching_ids *[]string `json:"matching_ids"`
CreateBomOrderData []*map[string]string
}
//下单后的json
//create_bom_order 下单
type CreateBomOrderJson struct {
Job string `json:"create_bom_order"`
Data string `json :"data"`
}
type LieOrderInfo struct {
OrderId string `db:"order_id"`
OrderSn string `db:"order_sn"`
OrderAmount float64 ` db:"order_amount"`
CreateTime int64 `db:"create_time"`
}
func returnError(err string) error{
logger.Error(err)
return errors.New(err)
}
func BuildBomOrder(orderBomData *CreateOrderBomData) (err error){
order_id := orderBomData.Order_id
//查询订单相关信息
lieOrder := &LieOrderInfo{}
getOrderSql := "select order_id,order_sn,order_amount,create_time from lie_order where order_id = ? "
err = db.DB.Get(lieOrder,getOrderSql,order_id)
if err != nil{
return returnError(fmt.Sprintf("没有找到相关平台订单信息 订单id: : %s",order_id))
}
tx := db.BomDB.MustBegin()
var(
BomId int64
argss []interface{}
)
for _,v := range orderBomData.CreateBomOrderData{
bom_sn := (*v)["bom_sn"]
matching_id := (*v)["matching_id"]
//查询bom主表信息
bomSql := "select bom_id from lie_bom where bom_sn = ? "
err := tx.Get(&BomId,bomSql,bom_sn)
if err != nil{
tx.Rollback()
return returnError(fmt.Sprintf("没有找到相关的bom单 bom_sn : %s",bom_sn))
}
matchTabel := getBomMatchTable(BomId)
//查询匹配表信息
//selectBomMatchInfo := "select bom_item_id from "+matchTabel+" where where bom_id = ? and matching_id= ?"
//err = tx.Get(&BomItemId,selectBomMatchInfo,BomId,matching_id)
//if err != nil{
// tx.Rollback()
// return returnError(fmt.Sprintf("没有找到相关的bom单的匹配表数据bom_id %s bom_sn : %s,matching_id %s",bom_sn,matching_id))
//}
//更新lie_bom_item_matching_xxx 匹配表
matchsql := " update "+matchTabel+" set is_corder = 1 where bom_id = ? and matching_id= ? "
argss = make([]interface{},0)
argss = append(argss,BomId) //bomid
argss = append(argss,matching_id) //匹配表主键
_ =tx.MustExec(matchsql,argss...)
//更新bom主表信息
bomsql2 := "update lie_bom set is_corder = 1 where bom_id = ?"
_ =tx.MustExec(bomsql2,BomId)
//修改或者信息lie_bom_extend信息 首次下单时间
var fcorder_time int64
bom_extend_sql1 := "select fcorder_time as nums from lie_bom_extend where bom_id = ?"
err = tx.Get(&fcorder_time,bom_extend_sql1,BomId)
if err == nil{
//更新首次下单时间
if(fcorder_time == 0){
time_ := time.Now().Unix()
bom_extend_sql := "update lie_bom_extend set fcorder_time = ? where bom_id = ?"
_ =tx.MustExec(bom_extend_sql,time_,BomId)
}
}
//新增lie_bom_match_extend信息
bom_match_extend_sql := " insert into lie_bom_match_extend (bom_id,matching_id,order_sn,order_id,create_time) values(?,?,?,?,?) "
argss = nil
argss = make([]interface{},0)
argss = append(argss,BomId) //bomid
argss = append(argss,matching_id)//matching_id
argss = append(argss,lieOrder.OrderSn)//order_sn
argss = append(argss,lieOrder.OrderId)//order_sn
argss = append(argss,lieOrder.CreateTime)//create_time
bom_match_extend_sql_rest :=tx.MustExec(bom_match_extend_sql,argss...)
_,err = bom_match_extend_sql_rest.RowsAffected()
if(err != nil){
return returnError(fmt.Sprintf("新增lie_bom_match_extend信息失败 原因: %s",err))
}
}
//新增lie_bom_order bom单和平台订单关联表
lie_bom_order_sql := " insert into lie_bom_order (bom_id,order_id,order_sn,add_order_amount,create_time,update_time) values(?,?,?,?,?,?)"
argss = nil
argss = make([]interface{},0)
argss = append(argss,BomId) //bom_id
argss = append(argss,lieOrder.OrderId) //order_id
argss = append(argss,lieOrder.OrderSn) //order_sn
argss = append(argss,lieOrder.OrderAmount) //add_order_amount
argss = append(argss,time.Now().Unix()) //create_time
argss = append(argss,time.Now().Unix()) //update_time
lie_bom_order_sql_result :=tx.MustExec(lie_bom_order_sql,argss...)
_,err =lie_bom_order_sql_result.RowsAffected()
if(err != nil){
return returnError(fmt.Sprintf("新增lie_bom_order信息失败 原因: %s",err))
}
tx.Commit()
return nil
}
func getBomMatchTable(bom_id int64) string{
bom_idstr := strconv.FormatInt(bom_id,10)
table_ := php2go.Substr(php2go.Strrev(bom_idstr),0,1)
return "lie_bom_item_matching_"+table_
}
func getBomItemTable(bom_id int64) string{
bom_idstr := strconv.FormatInt(bom_id,10)
table_ := php2go.Substr(php2go.Strrev(bom_idstr),0,1)
return "lie_bom_item_"+table_
}
\ No newline at end of file
......@@ -14,6 +14,7 @@ require (
github.com/jmoiron/sqlx v1.2.0
github.com/prometheus/common v0.7.0 // indirect
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71
github.com/syyongx/php2go v0.9.4
github.com/tealeg/xlsx v1.0.5
github.com/tidwall/gjson v1.6.0
)
......@@ -90,6 +90,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/syyongx/php2go v0.9.4 h1:qUtETTHzqHzxZK8plkbkb0YawD8bpLpxNsbzHQmb22Y=
github.com/syyongx/php2go v0.9.4/go.mod h1:meN2eIhhUoxOd2nMxbpe8g6cFPXI5O9/UAAuz7oDdzw=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
......
package main
import (
"flag"
"fmt"
"github.com/ichunt2019/golang-rbmq-sl/utils/rabbitmq"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/order/bom_create_order/service"
"go-queue-server/util"
"go-queue-server/util/ding"
_ "sync"
_ "time"
)
type RecvPro struct {
}
//// 实现消费者 消费消息失败 自动进入延时尝试 尝试3次之后入库db
func (t *RecvPro) Consumer(dataByte []byte) error {
logger.Info(string(dataByte))
fmt.Println(string(dataByte))
service.CreateBomOrder(string(dataByte))
//services.YaohaoyouOrder(string(dataByte));
return nil
}
//消息已经消费3次 失败了 请进行处理
func (t *RecvPro) FailAction(err error,dataByte []byte) error {
logger.Error("任务处理失败了,我要进入db日志库了")
logger.Error("任务处理失败了,发送钉钉消息通知主人")
logger.Error(string(dataByte))
logger.Error("错误原因:%s",err)
ding.Send(
util.Configs.Ding_msg.BomCreateOrderhook,
"@所有人 创建bom订单告警:\n",
fmt.Sprintf("失败原因:%s \n %s",err,string(dataByte)) ,
make([]string,0),
false,
)
return nil
}
func initDb(action string,dns string) (err error) {
err = db.CreateDB(action,dns)
if err != nil {
fmt.Println(err)
panic(err)
return
}
return
}
var ConfigDir string
var LogDir string
// 解析命令行参数
func initArgs() {
// worker -config ./worker.json
// worker -h
flag.StringVar(&ConfigDir, "configDir", "", "配置文件")
flag.StringVar(&LogDir, "logDir", "", "日志目录")
flag.Parse()
}
func main() {
initArgs()
//初始化配置文件
util.Init(ConfigDir)
//
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir+"bomcorder/"
logConfig["log_chan_size"] = "1000"
logger.InitLogger("file",logConfig)
logger.Init()
//初始化db
initDb("Init",util.Configs.Liexin_databases.Dns)
initDb("BomDB",util.Configs.Bom_databases.Dns)
t := &RecvPro{}
rabbitmq.Recv(rabbitmq.QueueExchange{
util.Configs.Rabbitmq_bomcreateorder.QueueName,
util.Configs.Rabbitmq_bomcreateorder.QueueName,
"",
util.Configs.Rabbitmq_bomcreateorder.Type,
util.Configs.Rabbitmq_bomcreateorder.Dns,
},t,2)
}
\ No newline at end of file
package service
import (
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"go-queue-server/dal/order/bom"
"strings"
)
var createOrderMap []*map[string]string
func CreateBomOrder(bomjson string) error{
var (
//jsonData map[string]interface{}
err error
)
job := gjson.Get(bomjson, "job")
jobname := job.String()
data := gjson.Get(bomjson, "data")
if jobname == "create_bom_order"{
fmt.Println(data)
//jsonData = gjson.Parse(data.String()).Value().(map[string]interface{})
//处理下单逻辑
err = createOrder(data.String())
}
return err
}
/*
创建订单
*/
func createOrder(jsonData string) (err error){
orderBomData := &bom.CreateOrderBomData{}
createOrderMap := make([]*map[string]string,0)
err = json.Unmarshal([]byte(jsonData),orderBomData)
if err != nil{
return err
}
for _,v := range *orderBomData.Matching_ids{
//fmt.Println(v)
kv := strings.Split(v,"_")
if len(kv) != 2{
return errors.New(fmt.Sprintf("参数有误:%s",jsonData))
}
createOrderMap = append(createOrderMap,&map[string]string{"bom_sn":kv[0],"matching_id":kv[1]})
}
orderBomData.CreateBomOrderData = make([]*map[string]string,0)
orderBomData.CreateBomOrderData = createOrderMap
//处理数据层逻辑
_ = bom.BuildBomOrder(orderBomData)
return nil
}
\ No newline at end of file
......@@ -9,10 +9,12 @@ type Config struct{
Liexin_databases *LiexinMysqlConfig
LiexinLabel_databases *LiexinLabelMysqlConfig
Liexincms_databases *LiexinCmsMysqlConfig
Bom_databases *BomDatabasesMysqlConfig
Rabbitmq_ichunt *RabbitmqIchunt
Rabbitmq_erp_label *RabbitmqErpLabel
Rabbitmq_order_push_stock *RabbitmqOrderPushStock
Rabbitmq_yaohaoyou *RabbitmqYaoHaoYou
Rabbitmq_bomcreateorder *RabbitmqBomCreateOrder
Crm_domain *SendMail
Ding_msg *Ding
Api_domain *ApiDomain
......@@ -31,6 +33,10 @@ type LiexinCmsMysqlConfig struct{
Dns string `toml:"dns"`
}
type BomDatabasesMysqlConfig struct {
Dns string `toml:"dns"`
}
type RabbitmqIchunt struct {
QueueName string `toml:"queue_name"`
RoutingKey string `toml:"routing_key"`
......@@ -58,6 +64,12 @@ type RabbitmqYaoHaoYou struct {
Dns string `toml:"dns"`
}
type RabbitmqBomCreateOrder struct {
QueueName string `toml:"queue_name"`
Type string `toml:"type"`
Dns string `toml:"dns"`
}
type SendMail struct{
SendMailUrl string `toml:"send_mail"`
}
......@@ -68,6 +80,7 @@ type Ding struct {
TuangouWebhook string `toml:"tuangou_webhook"`
ErpLabelWebhook string `toml:"erpLabel_webhook"`
YaoHaoYouWebhook string `toml:"yaoHaoYou_hook"`
BomCreateOrderhook string `toml:"bomCreateBom_hook"`
}
type ApiDomain struct {
......
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