Commit de3b8abf by Joneq

初步完成

parent 28bc9099
# Demo # Demo
## 项目简介 ## 项目简介
1. 1.20200818项目准备完成
...@@ -3,11 +3,18 @@ module photo_taking ...@@ -3,11 +3,18 @@ module photo_taking
go 1.13 go 1.13
require ( require (
github.com/basgys/goxml2json v1.1.0
github.com/beevik/etree v1.1.0
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/go-kratos/kratos v0.5.1-0.20200526160825-521d240568d0 github.com/go-kratos/kratos v0.5.1-0.20200526160825-521d240568d0
github.com/gogo/protobuf v1.3.1 github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.3.5 github.com/golang/protobuf v1.3.5
github.com/google/go-cmp v0.5.0 // indirect
github.com/google/wire v0.4.0 github.com/google/wire v0.4.0
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e github.com/mushishixian/gosoap v1.2.1-0.20200424081802-b11347c911bc
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/common v0.9.1
google.golang.org/genproto v0.0.0-20200402124713-8ff61da6d932 google.golang.org/genproto v0.0.0-20200402124713-8ff61da6d932
google.golang.org/grpc v1.28.1 google.golang.org/grpc v1.28.1
) )
...@@ -10,3 +10,39 @@ type Article struct { ...@@ -10,3 +10,39 @@ type Article struct {
Content string Content string
Author string Author string
} }
type ErpOrder struct {
Id int64 `json:"id"`
Ordersn string `json:"ordersn"`
Issuebillid string `json:"issuebillid"`
Issueentryid string `json:"issueentryid"`
Materialno string `json:"materialno"`
MaterialName string `json:"materialname"`
Brand string `json:"brand"`
Qty int64 `json:"qty"`
Unit string `json:"unit"`
Baseqty int64 `json:"baseqty"`
Lot string `json:"lot"`
Factorylot string `json:"factorylot"`
Warehouse string `json:"warehouse"`
Location string `json:"location"`
Salegroup string `json:"salegroup"`
Saleperson string `json:"saleperson"`
}
type OrderImage struct {
Issuebillid string `json:"issuebillid"`
Issueentryid string `json:"issueentryid"`
ImageSrc string `json:"image_src"`
}
type ErpRequestParam struct {
Number string `json:"number"`
}
type OrderAllInfo struct {
OrderInfo ErpOrder `json:"order_info"`
OrderImageInfo []OrderImage `json:"order_image_info"`
}
\ No newline at end of file
package http package http
import ( import (
"net/http" "encoding/json"
pb "photo_taking/api"
"photo_taking/internal/model"
"github.com/go-kratos/kratos/pkg/conf/paladin" "github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/log" "github.com/go-kratos/kratos/pkg/log"
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster" bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
"github.com/mushishixian/gosoap"
"net/http"
pb "photo_taking/api"
"photo_taking/internal/dao"
"photo_taking/internal/model"
"photo_taking/internal/service"
) )
var svc pb.DemoServer var svc pb.DemoServer
...@@ -37,6 +40,13 @@ func initRouter(e *bm.Engine) { ...@@ -37,6 +40,13 @@ func initRouter(e *bm.Engine) {
g := e.Group("/photo_taking") g := e.Group("/photo_taking")
{ {
g.GET("/start", howToStart) g.GET("/start", howToStart)
//获取订单信息
g.GET("/get_order_info/:order_id", getOrderInfo)
//增加订单图片
g.GET("/add_order_image/:issueentryid/:issueentryid/:image_src", addOrderImage)
//提交订单
g.GET("/submit_order/:ordersn",submitOrder)
} }
} }
...@@ -54,3 +64,157 @@ func howToStart(c *bm.Context) { ...@@ -54,3 +64,157 @@ func howToStart(c *bm.Context) {
} }
c.JSON(k, nil) c.JSON(k, nil)
} }
//获取订单信息
func getOrderInfo(c *bm.Context) {
orderId,_ := c.Params.Get("order_id")
//查询数据库里面是否有该数据
orderAllInfo := service.SelectOrderInfoFromOrderId(c,orderId)
//如果没查到就去erp添加
if len(orderAllInfo) == 0 {
var erpRequestParam model.ErpRequestParam
erpRequestParam.Number = orderId;
if err := service.LoginErp(c); err != nil {
service.ErrorReturn(c,err.Error())
}
var (
soap *gosoap.Client
params gosoap.ArrayParams
res *gosoap.Response
orderRequest struct{Number string `json:"number"`}
)
orderRequest.Number = orderId
jsonByte, _ := json.Marshal(orderRequest)
soap, err := gosoap.SoapClient(service.ErpConfig.ErpOrderRequestApi)
if err != nil {
service.ErrorReturn(c,err.Error())
}
params = gosoap.ArrayParams{
{"json", string(jsonByte)},
}
res, err = soap.Call(service.ErpConfig.ErpOrderRequestMethod, params)
if err != nil {
service.ErrorReturn(c,err.Error())
}
err = service.CheckOrderReturnData(res)
if err != nil {
service.ErrorReturn(c,err.Error())
}
returnContent := service.ErpReturnOrderData.Xml.P.A
db,close,err := dao.NewDB()
if err != nil {
service.ErrorReturn(c,err.Error())
}
for _,currentContent := range returnContent{
_,err = db.Exec(c,"INSERT INTO `lie_erp_order` (ordersn,issuebillid,issueentryid,materialno,materialname,brand,qty,unit,baseqty,lot,factorylot,warehouse,location,salegroup,saleperson)VALUES" +
"(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",orderId,currentContent["issueBillID"],currentContent["issueEntryID"],
currentContent["materialNo"],currentContent["materialName"],currentContent["brand"],currentContent["qty"],
currentContent["unit"],currentContent["baseQty"],currentContent["lot"],currentContent["factoryLot"],
currentContent["warehouse"],currentContent["location"],currentContent["saleGroup"],currentContent["salePerson"])
}
close()
//添加完之后再查询下
orderAllInfo = service.SelectOrderInfoFromOrderId(c,orderId)
}
c.JSON(orderAllInfo,nil)
}
//增加订单图片
func addOrderImage(c *bm.Context) {
orderId,_ := c.Params.Get("issuebillid")
entryid,_ := c.Params.Get("issueentryid")
image_src,_ := c.Params.Get("image_src")
db,close,err := dao.NewDB()
if err != nil {
service.ErrorReturn(c,"数据库连接失败")
}
_,err = db.Exec(c,"INSERT INTO `lie_order_goods_img` (`issuebillid`, `issueentryid`, `image_src`)VALUES(?,?,?)",orderId,entryid,image_src)
close()
c.JSON("添加成功",err)
}
//提交订单
func submitOrder(c *bm.Context) {
ordersn,_ := c.Params.Get("ordersn")
//查询数据库里面是否有该数据
orderAllInfo := service.SelectOrderInfoFromOrderId(c,ordersn)
if len(orderAllInfo) <= 0 {
service.ErrorReturn(c,"该订单暂无需要提交的信息")
}
var ErpSubmitImage struct{
OrderSn string `json:"order_sn"`
ImageAll []model.OrderImage `json:"image_all"`
}
ErpSubmitImage.OrderSn = ordersn
for _,currentData := range orderAllInfo{
if len(currentData.OrderImageInfo) <= 0 {
service.ErrorReturn(c,"该订单有图片未提交")
}else{
ErpSubmitImage.ImageAll = append(ErpSubmitImage.ImageAll,currentData.OrderImageInfo...)
}
}
if err := service.LoginErp(c); err != nil {
service.ErrorReturn(c,err.Error())
}
var (
soap *gosoap.Client
params gosoap.ArrayParams
res *gosoap.Response
)
jsonByte, _ := json.Marshal(ErpSubmitImage)
soap, err := gosoap.SoapClient(service.ErpConfig.ErpOrderRequestApi)
if err != nil {
service.ErrorReturn(c,err.Error())
}
params = gosoap.ArrayParams{
{"json", string(jsonByte)},
}
res, err = soap.Call(service.ErpConfig.ErpOrderSubmitMethod, params)
if err != nil {
service.ErrorReturn(c,string(res.Body))
}
err = service.CheckSubmitOrderData(res)
if err != nil {
service.ErrorReturn(c,err.Error())
}
c.JSON("提交成功",nil)
}
\ No newline at end of file
package service
import (
"photo_taking/internal/dao"
"photo_taking/internal/model"
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
"github.com/go-kratos/kratos/pkg/log"
)
//通过订单ID获取订单的信息
func SelectOrderInfoFromOrderId(c *bm.Context,orderId string)([]model.OrderAllInfo) {
var orderAllInfo []model.OrderAllInfo
//查询订单信息是否存在
db,close,err := dao.NewDB()
if err != nil {
log.Error(err.Error())
}
rows,err := db.Query(c,"select id,ordersn,issuebillid,issueentryid,materialno,materialname,brand,qty,unit,baseqty,lot,factorylot,warehouse,location,salegroup,saleperson from lie_erp_order where ordersn = ?",orderId)
defer rows.Close()
if err != nil {
log.Error("查询订单数据失败(%v)", err)
}
for rows.Next() {
var currentOrderAllInfo model.OrderAllInfo
var orderInfo model.ErpOrder
if err = rows.Scan(&orderInfo.Id,&orderInfo.Ordersn,&orderInfo.Issuebillid,&orderInfo.Issueentryid,&orderInfo.Materialno,&orderInfo.MaterialName,&orderInfo.Brand,
&orderInfo.Qty,&orderInfo.Unit,&orderInfo.Baseqty,&orderInfo.Lot,&orderInfo.Factorylot,&orderInfo.Warehouse,&orderInfo.Location,&orderInfo.Salegroup,&orderInfo.Saleperson); err != nil {
log.Error("scan demo log error(%v)", err)
return orderAllInfo
}
//如果订单号是空也直接返回
if orderInfo.Issuebillid == "" {
return orderAllInfo
}
currentOrderAllInfo.OrderInfo = orderInfo
currentOrderAllInfo.OrderImageInfo = GetOrderEntryImage(c,orderInfo.Issuebillid,orderInfo.Issueentryid)
orderAllInfo = append(orderAllInfo, currentOrderAllInfo)
}
close()
return orderAllInfo
}
//获取订单分录的图片
func GetOrderEntryImage(c *bm.Context,Issuebillid,Issueentryid string)([]model.OrderImage) {
db,close,err := dao.NewDB()
if err != nil {
log.Error(err.Error())
}
var orderAllImage []model.OrderImage
imagerows,err := db.Query(c,"select issuebillid,issueentryid,image_src from lie_order_goods_img where issuebillid = ? and issueentryid = ?",Issuebillid,Issueentryid)
defer imagerows.Close()
for imagerows.Next() {
var orderCurrentImage model.OrderImage
if err = imagerows.Scan(&orderCurrentImage.Issuebillid,&orderCurrentImage.Issueentryid,&orderCurrentImage.ImageSrc); err != nil {
log.Error("scan demo log error(%v)", err)
continue
}
orderAllImage = append(orderAllImage,orderCurrentImage)
}
close()
return orderAllImage
}
\ No newline at end of file
package service
import (
"encoding/json"
"errors"
"github.com/beevik/etree"
"github.com/go-kratos/kratos/pkg/conf/paladin"
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
"github.com/mushishixian/gosoap"
"github.com/prometheus/common/log"
"strings"
xj "github.com/basgys/goxml2json"
)
var ErpConfig struct{
ErpLoginStatusApi string
ErpOrderRequestApi string
ErpOrderRequestMethod string
ErpOrderSubmitMethod string
ErpUserName string
ErpPassword string
ErpSlnName string
ErpDcName string
ErpLanguage string
ErpDbType string
ErpAuthPattern string
}
type Pkey struct {
A []map[string]string `json:"a"`
}
type Xmlkey struct {
P Pkey `json:"p"`
}
type ReturnData struct {
Xml Xmlkey `json:"xml"`
}
var ErpReturnOrderData ReturnData
func LoginErp(c *bm.Context)(err error) {
paladin.Get("erp.toml").UnmarshalTOML(&ErpConfig);
soap, err := gosoap.SoapClient(ErpConfig.ErpLoginStatusApi)
if err != nil {
log.Error("SoapClient error: %s", err)
return
}
// Use gosoap.ArrayParams to support fixed position params
params := gosoap.ArrayParams{
{"userName", ErpConfig.ErpUserName},
{"password", ErpConfig.ErpPassword},
{"slnName", ErpConfig.ErpSlnName},
{"dcName", ErpConfig.ErpDcName},
{"language", ErpConfig.ErpLanguage},
{"dbType", ErpConfig.ErpDbType},
{"authPattern", ErpConfig.ErpAuthPattern},
}
res, err := soap.Call("login", params)
if err != nil {
log.Error("Call error: %s", err)
return
}
doc := etree.NewDocument()
if err := doc.ReadFromBytes(res.Body); err != nil {
return err
}
root := doc.SelectElement("multiRef")
sessionId := root.SelectElement("sessionId").Text()
if sessionId != "" {
return
}
return errors.New(string(res.Body))
}
//获取订单的返回数据
func CheckOrderReturnData(res *gosoap.Response) (err error) {
doc := etree.NewDocument()
if err = doc.ReadFromBytes(res.Body); err != nil {
return
}
//没有这个xml节点代表金蝶报错了
root := doc.SelectElement("ns1:getOrderOutDataResponse")
if root == nil {
return errors.New(string(res.Body))
}
result := root.SelectElement("getOrderOutDataReturn")
if result != nil {
xml := strings.NewReader(`<?xml version="1.0" encoding="UTF-8"?>`+result.Text())
turnJson, err := xj.Convert(xml)
if err != nil {
return errors.New("转换Json失败")
}
err = json.Unmarshal([]byte(turnJson.String()),&ErpReturnOrderData)
if err != nil {
return errors.New("没查询到erp订单数据")
}
return nil
}
return errors.New("金蝶返回响应格式无法识别")
}
//获取提交订单的返回数据
func CheckSubmitOrderData(res *gosoap.Response) (err error) {
doc := etree.NewDocument()
if err = doc.ReadFromBytes(res.Body); err != nil {
return
}
//没有这个xml节点代表金蝶报错了
root := doc.SelectElement("ns1:erpAuditIssueBillResponse")
if root == nil {
return errors.New(string(res.Body))
}
result := root.SelectElement("erpAuditIssueBillReturn")
if result != nil {
var returnData map[string]string
err = json.Unmarshal([]byte(result.Text()),&returnData)
if err != nil {
return errors.New("没查询到erp订单数据")
}
for k,v := range returnData{
if k != "0000" {
return errors.New(v)
}
}
return nil
}
return errors.New("金蝶返回响应格式无法识别")
}
\ No newline at end of file
package service
import(
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
"github.com/go-kratos/kratos/pkg/log"
"errors"
)
func ErrorReturn(c *bm.Context,errString string) {
err := errors.New(errString)
log.Error("error:(%v)", err)
c.JSON(err.Error(),err)
panic("error Exit")
}
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