| | |
| | | { |
| | | "web_port": "10210", |
| | | "sql_addr": "10.6.201.7", |
| | | "sql_addr": "192.168.20.249", |
| | | "sql_db_name": "LZGS", |
| | | "sql_username": "webapi", |
| | | "sql_password": "api2023", |
| | | "sql_username": "sa", |
| | | "sql_password": "basic@2023", |
| | | "nsq_server": "fai365.com:4150", |
| | | "nsq_webapi": "http://121.31.232.83:9080/api/nsq/pub", |
| | | "order_topic": "aps.wangpengfei.erp.seorder", |
| | | "inventory_topic": "aps.wangpengfei.erp.inventory", |
| | | "query_topic": "aps.wangpengfei.erp.k3resource", |
| | | "reply_topic": "aps.wangpengfei.erp.k3repley", |
| | | "nsq_webapi": "http://121.31.232.83:9080/api/nsq/pub?topic=your_topic", |
| | | "order_topic": "aps.factory.erp.seorder", |
| | | "inventory_topic": "aps.factory.erp.inventory", |
| | | "query_topic": "aps.factory.erp.k3resource", |
| | | "reply_topic": "aps.factory.erp.k3reply", |
| | | "cst_webapi": "http://192.168.20.249/cst/local_post.ashx", |
| | | "cst_query_topic": "aps.factory.erp.cstApply", |
| | | "cst_reply_topic": "aps.factory.erp.cstReply", |
| | | "interval": 60, |
| | | "debug": false |
| | | } |
| | |
| | | NsqWebApi string `json:"nsq_webapi"` // nsq HTTP接口地址 |
| | | OrderTopic string `json:"order_topic"` // 订单上报的topic |
| | | InventoryTopic string `json:"inventory_topic"` // 库存上报的topic |
| | | QueryTopic string `json:"query_topic"` // 金蝶查询接口的topic |
| | | ReplyTopic string `json:"reply_topic"` // 金蝶响应查询接口的topic |
| | | SqlQueryTopic string `json:"query_topic"` // 金蝶查询接口的topic |
| | | SqlReplyTopic string `json:"reply_topic"` // 金蝶响应查询接口的topic |
| | | CSTWebApi string `json:"cst_webapi"` // 生产任务单提交地址 |
| | | CSTQueryTopic string `json:"cst_query_topic"` // 提交生产任务单主题 |
| | | CSTReplyTopic string `json:"cst_reply_topic"` // 响应生产任务单主题 |
| | | SyncInterval int `json:"interval"` // 同步的时间间隔, 单位/秒 |
| | | Debug bool `json:"debug"` // 本地调试, 取本地数据 |
| | | } |
| | |
| | | Options.NsqWebApi = "http://121.31.232.83:9080/api/nsq/pub?topic=your_topic" |
| | | Options.OrderTopic = "aps.factory.erp.seorder" |
| | | Options.InventoryTopic = "aps.factory.erp.inventory" |
| | | Options.QueryTopic = "aps.factory.erp.k3resource" |
| | | Options.ReplyTopic = "aps.factory.erp.k3reply" |
| | | Options.SqlQueryTopic = "aps.factory.erp.k3resource" |
| | | Options.SqlReplyTopic = "aps.factory.erp.k3reply" |
| | | Options.CSTQueryTopic = "aps.factory.erp.cstApply" |
| | | Options.CSTReplyTopic = "aps.factory.erp.cstReply" |
| | | Options.SyncInterval = 60 |
| | | Options.Debug = false |
| | | } |
| | |
| | | |
| | | report.StartReport() |
| | | |
| | | // 开启订阅 |
| | | go nsqclient.InitNsqConsumer(config.Options.QueryTopic, "sensor01", kingdee.QueryMsgHandle) |
| | | // 开启订阅SQL查询 |
| | | go nsqclient.InitNsqConsumer(config.Options.SqlQueryTopic, "sensor01", kingdee.SqlQueryHandle) |
| | | go nsqclient.InitNsqConsumer(config.Options.CSTQueryTopic, "sensor01", kingdee.CSTQueryHandle) |
| | | |
| | | go webserver.Serve(serverPort.Text) |
| | | }) |
New file |
| | |
| | | package kingdee |
| | | |
| | | import ( |
| | | "bytes" |
| | | "encoding/json" |
| | | "fmt" |
| | | "io/ioutil" |
| | | "kingdee-dbapi/config" |
| | | "kingdee-dbapi/logger" |
| | | "kingdee-dbapi/nsqclient" |
| | | "mime/multipart" |
| | | "net/http" |
| | | "strconv" |
| | | "time" |
| | | ) |
| | | |
| | | var JoHeadTemplate = ` |
| | | { |
| | | "fdate": "%s", //日期 当天日期 |
| | | "ftype": 1054, //固定值 |
| | | "forderinterid": 0, //固定值 |
| | | "fpporderinterid": 0, //固定值 |
| | | "fparentinterid": 0, //固定值 |
| | | "fsupplyid": 0, //固定值 |
| | | "fplanconfirmed": 0, //计划确认 |
| | | "fplanmode": 14036, //计划模式 |
| | | "fworktypeid": 55, //生产类型 |
| | | "fmrp": 1052, //单据来源 |
| | | "fbomcategory": 36820, //BOM 类别 |
| | | "fcardclosed": 1059, //流转卡关联关闭 |
| | | "fprintcount": 0, //打印次数 |
| | | "ffinclosed": 0, //财务结算标志 |
| | | "ffinclosedate": "", //财务结算日期 |
| | | "ffincloseer": 0, //财务结算人 |
| | | "fmtono": " ", //计划跟踪号: |
| | | "fauxqtyforitem": 0, //因料报废数量 |
| | | "funitid": 0, //单位 接口自动获取, 设置成0就可以 |
| | | "fcostobjid": 0, //成本对象 , 接口自动获取, 设置成0就可以 |
| | | "fbominterid": 0, //BOM编号 接口自动获取, 设置成0就可以 |
| | | "fbillerid": 16394, //与制单人一致 |
| | | "fplancategory": 2, //计划类别 |
| | | "fcheckdate": "%s", //制单日期 |
| | | "fstartdate": "%s", //开工日期 |
| | | "ffinishdate": "%s", //完工日期 |
| | | "fplancommitdate": "%s", //计划开工日期 |
| | | "fplanfinishdate": "%s", //计划完工日期 |
| | | "fitemid": "%s->", //物料编码 * |
| | | "fworkshop": "%s->", //生产车间编号 |
| | | "fauxqty": %d, //计划生产数量 |
| | | "fauxinhighlimitqty": %d, //完工入库上限 |
| | | "fauxinlowlimitqty": %d, //完工入库下限 |
| | | "finhighlimit": 0, //完工入库超收比例 |
| | | "finlowlimit": 0 //完工入库欠收比例 |
| | | } |
| | | ` |
| | | |
| | | type CSTNsqQuery struct { |
| | | FBillNo string `json:"FBillNo"` // 订单编号 |
| | | FNumber string `json:"FNumber"` // 物料代码 |
| | | FSource string `json:"FSource"` // 生产车间代码 |
| | | FStartDate string `json:"FStartDate"` // 开工日期 |
| | | FFinishDate string `json:"FFinishDate"` // 完工日期 |
| | | UseAmount float64 `json:"UseAmount"` // 使用数量 |
| | | } |
| | | |
| | | type CSTNsqReply struct { |
| | | FBillNo string `json:"fBillNo"` // 订单编号 |
| | | Code int `json:"code"` // CST接口返回的code |
| | | Message string `json:"message"` // 成功则返回单号, 失败返回 |
| | | } |
| | | |
| | | type CSTServiceResponse struct { |
| | | ErrCode int `json:"errcode"` |
| | | ErrMsg string `json:"errmsg"` |
| | | Data struct { |
| | | FBillNo string `json:"fbillno"` |
| | | FBillId string `json:"fbillid"` |
| | | FTranType string `json:"ftrantype"` |
| | | FClassTypeId int `json:"fclasstypeid"` |
| | | } `json:"data"` |
| | | } |
| | | |
| | | func CSTQueryHandle(msg []byte) error { |
| | | var query []CSTNsqQuery |
| | | var reply []CSTNsqReply |
| | | |
| | | if err := json.Unmarshal(msg, &query); err != nil { |
| | | logger.Warn("解析请求失败, %s", err.Error()) |
| | | return err |
| | | } |
| | | |
| | | for _, q := range query { |
| | | ret := Commit2CSTService(q) |
| | | reply = append(reply, ret) |
| | | } |
| | | |
| | | replyData, _ := json.Marshal(reply) |
| | | ok := nsqclient.Produce(config.Options.CSTReplyTopic, replyData) |
| | | logger.Warn("应答查询请求结果:%t, key:%s", ok, replyData) |
| | | |
| | | return nil |
| | | } |
| | | |
| | | func Commit2CSTService(order CSTNsqQuery) (result CSTNsqReply) { |
| | | fmt.Println(order) |
| | | result.Code = -1 |
| | | |
| | | today := time.Now().Format("2006-01-02") |
| | | joHead := fmt.Sprintf(JoHeadTemplate, |
| | | today, // 日期 |
| | | today, // 制单日期 |
| | | order.FStartDate, // 开工日期 |
| | | order.FFinishDate, // 完工日期 |
| | | order.FStartDate, // 计划开工日期 |
| | | order.FFinishDate, // 计划完工日期 |
| | | order.FNumber, // 物料编码 |
| | | order.FSource, // 生产车间编码 |
| | | int(order.UseAmount), // 生产数量 |
| | | int(order.UseAmount), // 完工入库上限 |
| | | int(order.UseAmount), // 完工入库下限 |
| | | ) |
| | | fmt.Println(joHead) |
| | | params := map[string]string{ |
| | | "action": "生产任务单.新增", |
| | | "fuserid": "16394", |
| | | "jo_head": joHead, |
| | | "ja_body": "[{}]", |
| | | } |
| | | |
| | | request, err := newMultipartRequest(config.Options.CSTWebApi, params) |
| | | if err != nil { |
| | | result.Message = err.Error() |
| | | return |
| | | } |
| | | |
| | | client := &http.Client{} |
| | | resp, err := client.Do(request) |
| | | if err != nil { |
| | | result.Message = err.Error() |
| | | return |
| | | } |
| | | |
| | | defer resp.Body.Close() |
| | | |
| | | body, err := ioutil.ReadAll(resp.Body) |
| | | if err != nil { |
| | | result.Message = err.Error() |
| | | return |
| | | } |
| | | |
| | | var rspMsg CSTServiceResponse |
| | | err = json.Unmarshal(body, &rspMsg) |
| | | if err != nil { |
| | | result.Message = err.Error() |
| | | return |
| | | } |
| | | |
| | | if rspMsg.ErrCode == 0 { |
| | | result.Code = 0 |
| | | result.Message = rspMsg.Data.FBillNo |
| | | } else { |
| | | result.Code = rspMsg.ErrCode |
| | | result.Message = rspMsg.ErrMsg |
| | | } |
| | | |
| | | return |
| | | } |
| | | |
| | | func newMultipartRequest(url string, params map[string]string) (*http.Request, error) { |
| | | body := &bytes.Buffer{} |
| | | writer := multipart.NewWriter(body) |
| | | // 设置Boundary, 接口验证了六个- |
| | | err := writer.SetBoundary("------basicHttpClient" + strconv.Itoa(int(time.Now().Unix()))) |
| | | if err != nil { |
| | | fmt.Println(err.Error()) |
| | | } |
| | | for key, val := range params { |
| | | _ = writer.WriteField(key, val) |
| | | } |
| | | |
| | | writer.Close() |
| | | |
| | | req, err := http.NewRequest("POST", url, body) |
| | | req.Header.Set("Content-Type", writer.FormDataContentType()) |
| | | req.Header.Set("User-Agent", "basicHttpClient/0.0.1") |
| | | req.Header.Set("Connection", "keep-alive") |
| | | req.Header.Set("Accept", "*/*") |
| | | |
| | | return req, err |
| | | } |
| | |
| | | "kingdee-dbapi/nsqclient" |
| | | ) |
| | | |
| | | type QueryMsg struct { |
| | | type SqlQueryMsg struct { |
| | | Key string // 请求 |
| | | Command string |
| | | Success bool |
| | |
| | | Result []byte |
| | | } |
| | | |
| | | func QueryMsgHandle(msg []byte) error { |
| | | var query QueryMsg |
| | | func SqlQueryHandle(msg []byte) error { |
| | | var query SqlQueryMsg |
| | | |
| | | if err := json.Unmarshal(msg, &query); err != nil { |
| | | logger.Warn("解析请求失败, %s", err.Error()) |
| | |
| | | } |
| | | |
| | | replyData, _ := json.Marshal(query) |
| | | ok := nsqclient.Produce(config.Options.ReplyTopic, replyData) |
| | | ok := nsqclient.Produce(config.Options.SqlReplyTopic, replyData) |
| | | logger.Warn("应答查询请求结果:%t, key:%s", ok, query.Key) |
| | | |
| | | return nil |
| | |
| | | |
| | | // 测试查询请求 |
| | | //sql := []byte("select * from t_icitem where FItemID=3316") |
| | | //ok := nsqclient.Produce(config.Options.QueryTopic, sql) |
| | | //ok := nsqclient.Produce(config.Options.SqlQueryTopic, sql) |
| | | //logger.Debug("测试请求接口, %v", ok) |
| | | |
| | | time.Sleep(time.Duration(config.Options.SyncInterval) * time.Second) |
| | |
| | | package webserver |
| | | |
| | | import ( |
| | | "fmt" |
| | | "kingdee-dbapi/kingdee" |
| | | |
| | | "github.com/gin-gonic/gin" |
| | |
| | | "total": len(rspData), |
| | | }) |
| | | } |
| | | |
| | | func CSTCommit(c *gin.Context) { |
| | | var req kingdee.CSTNsqQuery |
| | | if err := c.ShouldBind(&req); err != nil { |
| | | c.JSON(401, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | fmt.Println(req) |
| | | rsp := kingdee.Commit2CSTService(req) |
| | | |
| | | c.JSON(200, gin.H{ |
| | | "success": true, |
| | | "message": "", |
| | | "data": rsp, |
| | | }) |
| | | } |
| | |
| | | r := gin.Default() |
| | | r.GET("/order", OrderList) |
| | | r.GET("/inventory", InventoryList) |
| | | r.POST("/cst", CSTCommit) |
| | | |
| | | r.Run(":" + port) |
| | | } |