Initial commit
This commit is contained in:
219
FeeAuditUtils.go
Normal file
219
FeeAuditUtils.go
Normal file
@@ -0,0 +1,219 @@
|
||||
package mohwhelper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gitlab-ce.niaulang.com/niau-lang/golanghelper"
|
||||
)
|
||||
|
||||
type FeeAuditUtils struct {
|
||||
UnitNo string
|
||||
RequestAt string
|
||||
SourceSystem string
|
||||
Hash string
|
||||
Url string
|
||||
Cert []byte
|
||||
}
|
||||
|
||||
func NewFeeAuditUtils(unitNo, sourceSystem, hash, url, certPath string) (feeAuditUtils FeeAuditUtils, err error) {
|
||||
stringutils := golanghelper.StringUtils{}
|
||||
if stringutils.IsEmpty(unitNo) || stringutils.IsEmpty(sourceSystem) || stringutils.IsEmpty(hash) ||
|
||||
stringutils.IsEmpty(url) || stringutils.IsEmpty(certPath) {
|
||||
err = errors.New("Invalid params")
|
||||
} else {
|
||||
feeAuditUtils.Cert, err = os.ReadFile(certPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
feeAuditUtils.UnitNo = unitNo
|
||||
feeAuditUtils.SourceSystem = sourceSystem
|
||||
feeAuditUtils.Hash = hash
|
||||
feeAuditUtils.Url = url
|
||||
feeAuditUtils.RequestAt = time.Now().Format("20060102150405")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getChecksum 用 apData 來取得校驗碼
|
||||
func (f *FeeAuditUtils) getChecksum(apData string) string {
|
||||
apDataBytes := []byte(apData)
|
||||
timeBytes := []byte(f.RequestAt)
|
||||
hashBytes := []byte(f.Hash)
|
||||
|
||||
timeByteLen := float64(len(timeBytes))
|
||||
hashByteLen := float64(len(hashBytes))
|
||||
|
||||
var sum int
|
||||
var m1 int
|
||||
var m2 int
|
||||
|
||||
for i := 0; i < len(apDataBytes); i++ {
|
||||
m1 = int(math.Mod(float64(i), hashByteLen))
|
||||
m2 = int(math.Mod(float64(i), timeByteLen))
|
||||
sum += int(apDataBytes[i])*int(hashBytes[m1]) + int(timeBytes[m2])*int(hashBytes[m1])
|
||||
|
||||
if sum>>16 > 0 {
|
||||
sum = (sum >> 16) + (sum & 0xFFFF)
|
||||
}
|
||||
}
|
||||
|
||||
hex := fmt.Sprintf("%X", sum)
|
||||
|
||||
var stringutils golanghelper.StringUtils
|
||||
return stringutils.LeftPad(hex, 4, "0")
|
||||
}
|
||||
|
||||
// httpsPost 用來上傳 WriteoffResponse
|
||||
func (f *FeeAuditUtils) httpsPost(funcName string, writeoffRequest WriteoffRequest) (writeoffResponse WriteoffResponse, err error) {
|
||||
data, err := json.Marshal(&writeoffRequest)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
caCertPool := x509.NewCertPool()
|
||||
caCertPool.AppendCertsFromPEM(f.Cert)
|
||||
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: caCertPool,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
request, err := http.NewRequest("POST", f.Url+"/"+funcName, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
responseData, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(responseData, &writeoffResponse)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CreateRequest 用來産生 WirteoffRequest
|
||||
func (f *FeeAuditUtils) NewRequest(data []byte) WriteoffRequest {
|
||||
apData := base64.StdEncoding.EncodeToString(data)
|
||||
|
||||
checksum := f.getChecksum(apData)
|
||||
|
||||
return WriteoffRequest{
|
||||
UnitNo: f.UnitNo,
|
||||
RequestAt: f.RequestAt,
|
||||
SourceSystem: f.SourceSystem,
|
||||
ApData: apData,
|
||||
Checksum: checksum,
|
||||
}
|
||||
}
|
||||
|
||||
// SendFeeApply 傳送服務記錄申報
|
||||
func (f *FeeAuditUtils) SendFeeApply(writeoffRequest WriteoffRequest) (feeApplyResult FeeApplyResult, err error) {
|
||||
//uploadData, err := json.Marshal(&feeApply)
|
||||
//if err != nil {
|
||||
// return
|
||||
//}
|
||||
|
||||
writeoffResponse, err := f.httpsPost("FeeApply", writeoffRequest)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resultData, err := base64.StdEncoding.DecodeString(writeoffResponse.Result)
|
||||
|
||||
err = json.Unmarshal(resultData, &feeApplyResult)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SendObjDel 傳送服務紀錄刪除
|
||||
func (f *FeeAuditUtils) SendObjDel(writeoffRequest WriteoffRequest) (err error) {
|
||||
//uploadData, err := json.Marshal(&objDel)
|
||||
//if err != nil {
|
||||
// return
|
||||
//}
|
||||
|
||||
_, err = f.httpsPost("ObjDel", writeoffRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SendAppCompletionNotice 傳送申報確認通知
|
||||
func (f *FeeAuditUtils) SendAppCompletionNotice(writeoffRequest WriteoffRequest) (err error) {
|
||||
//uploadData, err := json.Marshal(&appCompletionNotice)
|
||||
//if err != nil {
|
||||
// return
|
||||
//}
|
||||
|
||||
_, err = f.httpsPost("appCompletionNotice", writeoffRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SendAppResultQuery 傳送「服務單位各分案審核狀態查詢」或「分案審核明細查詢」
|
||||
func (f *FeeAuditUtils) SendAppResultQuery(writeoffRequest WriteoffRequest) (appResultQueryResult AppResultQueryResult, err error) {
|
||||
//uploadData, err := json.Marshal(&appResultQuery)
|
||||
//if err != nil {
|
||||
// return
|
||||
//}
|
||||
|
||||
writeoffResponse, err := f.httpsPost("appResultQuery", writeoffRequest)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resultData, err := base64.StdEncoding.DecodeString(writeoffResponse.Result)
|
||||
|
||||
err = json.Unmarshal(resultData, &appResultQueryResult)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SendAppCancel 傳送撤回服務記錄
|
||||
func (f *FeeAuditUtils) SendAppCancel(writeoffRequest WriteoffRequest) (err error) {
|
||||
//uploadData, err := json.Marshal(&appCancel)
|
||||
//if err != nil {
|
||||
// return
|
||||
//}
|
||||
|
||||
_, err = f.httpsPost("appCancel", writeoffRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SendCancelResultResponse 傳送取消交易單處理結果回報
|
||||
func (f *FeeAuditUtils) SendCancelResultResponse(writeoffRequest WriteoffRequest) (err error) {
|
||||
//uploadData, err := json.Marshal(&cancelResultResponse)
|
||||
//if err != nil {
|
||||
// return
|
||||
//}
|
||||
|
||||
_, err = f.httpsPost("CancelResultResponse", writeoffRequest)
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user