WH01243 %!s(int64=5) %!d(string=hai) anos
pai
achega
ee7119e670

+ 1 - 1
invoice/src/api/InvoiceController.go

@@ -57,7 +57,7 @@ func InvoiceDownload(context *gin.Context) {
 	fmt.Println(dat["model"])
 	fmt.Println(dat["model"].(string))
 	Modelint, _ := strconv.Atoi(dat["model"].(string))
-	dataMap,code,msg:=InvoiceService.InvoiceDownload(strings.Replace(fmt.Sprintln(dat["swno"]), "\n", "", -1),strings.Replace(fmt.Sprintln(dat["saleTax"]), "\n", "", -1),Modelint)
+	dataMap,code,msg:=InvoiceService.InvoiceDownload(strings.Replace(fmt.Sprintln(dat["swno"]), "\n", "", -1),strings.Replace(fmt.Sprintln(dat["saleTax"]), "\n", "", -1),Modelint,0,0,"0")
 	result.Data=dataMap
 	result.Code=code
 	result.Msg=msg

+ 2 - 1
invoice/src/config.json

@@ -13,5 +13,6 @@
     "OperationCode": "10",
     "Verified": "0",
     "Kpr": "梁艺晓",
-    "JyUrl": "http://192.168.20.191:86/jypay/invoice/callback"
+    "JyUrl": "http://192.168.20.191:86/jypay/invoice/callback",
+    "Frequency": 3
 }

+ 3 - 0
invoice/src/entity/Invoice.go

@@ -1,6 +1,8 @@
 package entity
 
 type Invoice struct {
+
+	Changed 		  string    `json:"changed"`           //类型0开票1换开
 	Swno              string    `json:"swno"`              //流水号
 	SaleTax           string    `json:"saleTax"`           //销方税号
 	Store             string    `json:"store"`             //门店
@@ -118,3 +120,4 @@ var OperationCode string
 var Verified string
 var Kpr string
 var  JyUrl string
+var  Frequency int

+ 8 - 3
invoice/src/main.go

@@ -9,7 +9,9 @@ import (
 	"log"
 	"service"
 	"spirit/redis"
+	"spirit/utils"
 	"spirit/web/core"
+	"strconv"
 	"util"
 )
 
@@ -49,12 +51,15 @@ func Init() {
 	entity.Verified = fmt.Sprint(Config["Verified"])
 	entity.Kpr=fmt.Sprint(Config["Kpr"])
 	entity.JyUrl=fmt.Sprint(Config["JyUrl"])
+	entity.Frequency=utils.SafeConvert2Int(Config["Frequency"])
+
 	keys := redis.GetKeys(core.GetConfiguration().Redis.Modules, "fp_*")
 	invoiceService := service.InvoiceService{}
 	for numb, _ := range keys {
 		valueInface := redis.Get(core.GetConfiguration().Redis.Modules, string(keys[numb].([]byte)))
-		class_detail_map := make(map[string]interface{})
-		class_detail_map = valueInface.(map[string]interface{})
-		invoiceService.Timer(class_detail_map["swno"].(string), class_detail_map["saleTax"].(string),class_detail_map["model"].(int))
+		_rdsVal := valueInface.(map[string]interface{})
+		model, _ := strconv.Atoi(fmt.Sprint(_rdsVal["model"]))
+		isfeedback, _ :=strconv.Atoi(fmt.Sprint(_rdsVal["isfeedback"]))
+		invoiceService.Timer(_rdsVal["swno"].(string), _rdsVal["saleTax"].(string),model,isfeedback,_rdsVal["changed"].(string))
 	}
 }

+ 159 - 71
invoice/src/service/InvoiceService.go

@@ -11,6 +11,7 @@ import (
 	"net/http"
 	"spirit/redis"
 	"spirit/web/core"
+	"strconv"
 	"strings"
 	"time"
 	"util"
@@ -25,17 +26,17 @@ var (
 // 发票开具
 func (u *InvoiceService) InvoiceAdd(solgan *entity.Invoice) (string, int, interface{}) {
 	rand.Seed(time.Now().Unix())
-	solgan.InvoMemo=solgan.Swno
-	solgan.Swno=strings.Replace(fmt.Sprintln(solgan.Swno+fmt.Sprintln(rand.Intn(1000000))), "\n", "", -1)
+	solgan.InvoMemo = solgan.Swno
+	solgan.Swno = strings.Replace(fmt.Sprintln(solgan.Swno+fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))), "\n", "", -1)
 	solgan.SaleTax = entity.SaleTax
-	solgan.InvType =entity.InvType
-	solgan.BillType =entity.BillType
-	solgan.Kpy=entity.Kpr
+	solgan.InvType = entity.InvType
+	solgan.BillType = entity.BillType
+	solgan.Kpy = entity.Kpr
 	solgan.SpecialRedFlag = entity.SpecialRedFlag
 	solgan.OperationCode = entity.OperationCode
 	solgan.Verified = entity.Verified
-	now  := time.Now()
-	solgan.BillDate=now.Format("2006-01-02 15:04:05")
+	now := time.Now()
+	solgan.BillDate = now.Format("2006-01-02 15:04:05")
 	url := entity.Url + "?build_invoice"
 	bytesData, _ := json.Marshal(solgan)
 	reader := bytes.NewReader(bytesData)
@@ -50,34 +51,33 @@ func (u *InvoiceService) InvoiceAdd(solgan *entity.Invoice) (string, int, interf
 	if err := json.Unmarshal([]byte(respBytes), &data); err == nil {
 		fmt.Println("==============json str 转map=======================", data)
 	}
-	util.Loger.Println("开具发票:数据",  string(bytesData))
+	util.Loger.Println("开具发票:数据", string(bytesData))
 	util.Loger.Println("开具发票:结果", data)
 	//开票正常时
 	if (data["returnCode"] == entity.LineUpCode || data["returnCode"] == entity.SealCode || data["returnCode"] == entity.SuccessCode) {
-		pdfData, code, msg := u.InvoiceDownload(solgan.Swno, solgan.SaleTax, 0)
+		pdfData, code, msg := u.InvoiceDownload(solgan.Swno, solgan.SaleTax, 0, 0, 0,solgan.Changed)
 		if (fmt.Sprint(code) == entity.SuccessCode) {
 			data1 := make(map[string]interface{})
 			data1["fpdm"] = pdfData["fpdm"]
 			data1["fphm"] = pdfData["fphm"]
 			data1["swno"] = solgan.Swno
-			data1["path"]=pdfData["path"]
+			data1["path"] = pdfData["path"]
 			return msg, 0, data1
 		} else {
 			data1 := make(map[string]interface{})
 			data1["fpdm"] = pdfData["fpdm"]
 			data1["fphm"] = pdfData["fphm"]
 			data1["swno"] = solgan.Swno
-			data1["path"]=pdfData["path"]
+			data1["path"] = pdfData["path"]
 			return msg, code, data1
 		}
-	}else{
+	} else {
 		return fmt.Sprintln(data["returnMsg"]), 1, data
 	}
-	}
-
+}
 
 // 发票下载
-func (u *InvoiceService) InvoiceDownload(swno string, saleTax string,model int) (map[string]interface{}, int, string) {
+func (u *InvoiceService) InvoiceDownload(swno string, saleTax string, model int, isLoop int, isfeedback int,changed string) (map[string]interface{}, int, string) {
 	url := entity.Url + "?get_invoice"
 	song := make(map[string]interface{})
 	song["swno"] = swno
@@ -95,28 +95,40 @@ func (u *InvoiceService) InvoiceDownload(swno string, saleTax string,model int)
 	if err := json.Unmarshal([]byte(respBytes), &dat); err == nil {
 		fmt.Println("==============json str 转map=======================")
 	}
-	util.Loger.Println("发票下载:", "流水号:",swno, "发票开具方税号:",saleTax,"结果:", dat)
+	util.Loger.Println("发票下载:", "流水号:", swno, "发票开具方税号:", saleTax, "结果:", dat)
 	if (dat.ReturnMsg.MsgCode == entity.SuccessCode) {
-		var  swno1 string
-		if(model==1){
-			swno1=swno[4 : len(swno)]
-		}else{
-			swno1=swno
+		var swno1 string
+		if (model == 1) {
+			swno1 = swno[4:len(swno)]
+		} else {
+			swno1 = swno
 		}
 		pdfData := make(map[string]interface{})
-		path := util.ImgHandle(dat.FpMsgs[0].PdfContent, swno,saleTax,swno1)
+		path := util.ImgHandle(dat.FpMsgs[0].PdfContent, swno, saleTax, swno1)
 		pdfData["path"] = path
 		pdfData["fpdm"] = dat.FpMsgs[0].Fpdm
 		pdfData["fphm"] = dat.FpMsgs[0].Fphm
 		pdfData["swno"] = swno
 		return pdfData, 0, dat.ReturnMsg.Msg
 	} else if (dat.ReturnMsg.MsgCode == entity.LineUpCode || dat.ReturnMsg.MsgCode == entity.SealCode) {
-		swnoMap := make(map[string]interface{})
+		/*swnoMap := make(map[string]interface{}, 0)
 		swnoMap["swno"] = swno
 		swnoMap["saleTax"] = saleTax
 		swnoMap["model"] = model
-		redis.Pop("fp_"+swno, fmt.Sprint(swnoMap))
-		u.Timer(swno, saleTax,model)
+		swnoMap["isfeedback"] = isfeedback
+		swnoMap["isLoop"] = isLoop
+		valueInface := redis.Get(core.GetConfiguration().Redis.Modules, "fp_"+swno)
+		if (valueInface == nil) {
+			swnoMap["Frequency"] = 0
+		} else {
+			_rdsVal := valueInface.(map[string]interface{})
+			model, _ := strconv.Atoi(fmt.Sprint(_rdsVal["Frequency"]))
+			swnoMap["Frequency"] = model + 1
+		}
+		redis.PutKV("fp_"+swno, swnoMap)*/
+		if isLoop == 0 {
+			u.Timer(swno, saleTax, model, isfeedback,changed)
+		}
 		return nil, 2, dat.ReturnMsg.Msg
 	} else {
 		return nil, 1, dat.ReturnMsg.Msg
@@ -125,24 +137,25 @@ func (u *InvoiceService) InvoiceDownload(swno string, saleTax string,model int)
 
 // 发票是否存在
 //noinspection ALL
-func (u *InvoiceService) InvoiceSee(swno string, saleTax string,model string) (int, interface{}, string) {
-	var  swno1 string
-	if(model=="1"){
-		swno1="RED_"+swno
-	}else{
-		swno1=swno
+func (u *InvoiceService) InvoiceSee(swno string, saleTax string, model string,) (int, interface{}, string) {
+	var swno1 string
+	if (model == "1") {
+		swno1 = "RED_" + swno
+	} else {
+		swno1 = swno
 	}
-	fool := util.Exists("./static/res/" + saleTax + "/"+swno+"/"+swno1 + ".pdf")
+	fool := util.Exists("./static/res/" + saleTax + "/" + swno + "/" + swno1 + ".pdf")
 	msg := "发票地址"
 
 	if fool {
-		pdfData:=make(map[string]interface{})
-		pdfData["path"] = entity.PdfUrl+"/static/res/" + saleTax + "/" + swno +"/"+swno1+ ".pdf"
-		pdfData["fpdm"]=""
-		pdfData["fphm"]=""
+		pdfData := make(map[string]interface{})
+		pdfData["path"] = entity.PdfUrl + "/static/res/" + saleTax + "/" + swno + "/" + swno1 + ".pdf"
+		pdfData["fpdm"] = ""
+		pdfData["fphm"] = ""
 		return 0, pdfData, msg
 	} else {
-		path, numb, _ := u.InvoiceDownload(swno1, saleTax,0)
+		modelInt, _ := strconv.Atoi(model)
+		path, numb, _ := u.InvoiceDownload(swno1, saleTax, modelInt, 0, 0,"0")
 		return numb, path, msg
 	}
 }
@@ -167,14 +180,25 @@ func (u *InvoiceService) InvoiceRedSubmit(fpdm string, fphm string) (string, int
 	if err := json.Unmarshal([]byte(respBytes), &data); err == nil {
 		fmt.Println("==============json str 转map=======================")
 	}
-	util.Loger.Println("冲红:", "发票单号:",fpdm,"发票号码:", fphm,"结果:", data)
-	if (data["returnCode"] == entity.SuccessCode) {
-		u.InvoiceDownload(data["redSwno"].(string),data["saleTax"].(string),1)
-		return fmt.Sprintln(data["returnMsg"]), 0, data
-	}  else if (data["returnCode"] == entity.LineUpCode){
-		u.InvoiceDownload(data["redSwno"].(string),data["saleTax"].(string),1)
-		return fmt.Sprintln(data["returnMsg"]), 2, data
-	}else{
+	util.Loger.Println("冲红:", "发票单号:", fpdm, "发票号码:", fphm, "结果:", data)
+	if (data["returnCode"] == entity.LineUpCode || data["returnCode"] == entity.SealCode || data["returnCode"] == entity.SuccessCode) {
+		pdfData, code, msg := u.InvoiceDownload(data["redSwno"].(string), data["saleTax"].(string), int(1), int(0), int(0),"0")
+		if (fmt.Sprint(code) == entity.SuccessCode) {
+			data1 := make(map[string]interface{})
+			data1["fpdm"] = pdfData["fpdm"]
+			data1["fphm"] = pdfData["fphm"]
+			data1["swno"] = data["redSwno"]
+			data1["path"] = pdfData["path"]
+			return msg, 0, data1
+		} else {
+			data1 := make(map[string]interface{})
+			data1["fpdm"] = pdfData["fpdm"]
+			data1["fphm"] = pdfData["fphm"]
+			data1["swno"] = data["redSwno"]
+			data1["path"] = pdfData["path"]
+			return msg, code, data1
+		}
+	} else {
 		return fmt.Sprintln(data["returnMsg"]), 1, data
 	}
 }
@@ -182,40 +206,104 @@ func (u *InvoiceService) InvoiceRedSubmit(fpdm string, fphm string) (string, int
 // 换票
 //noinspection ALL
 func (u *InvoiceService) InvoiceReplace(solgan *entity.Invoice) (string, int, interface{}) {
-	msg, code, dataMap := invoiceService.InvoiceRedSubmit(solgan.Yfpdm, solgan.Yfphm)
-	util.Loger.Println("换票:",  "发票单号:",solgan.Yfpdm,"发票号码:", solgan.Yfphm)
-	if (code == 1) {
-		return msg, code, dataMap
+	msg, code1, dataMap := invoiceService.InvoiceRedSubmit(solgan.Yfpdm, solgan.Yfphm)
+	util.Loger.Println("换票:", "发票单号:", solgan.Yfpdm, "发票号码:", solgan.Yfphm)
+	if (code1 == 1) {
+		return msg, code1, dataMap
 	}
-	solgan.Yfphm=""
-	solgan.Yfpdm=""
+	solgan.Yfphm = ""
+	solgan.Yfpdm = ""
 	msg, code, datamap := u.InvoiceAdd(solgan)
-	return msg, code, datamap
+	_rdsVal := datamap.(map[string]interface{})
+	_dataMap := dataMap.(map[string]interface{})
+	_rdsVal["chcode"]=code1
+	_rdsVal["chfpdm"] = _dataMap["fpdm"]
+	_rdsVal["chfphm"] = _dataMap["fphm"]
+	_rdsVal["chswno"] = _dataMap["swno"]
+	_rdsVal["chpath"] = _dataMap["path"]
+	return msg, code, _rdsVal
 }
 
-func (u *InvoiceService) Timer(swno string, saleTax string,model int ) {
+func (u *InvoiceService) Timer(swno string, saleTax string, model int, isfeedback int,changed string) {
+
+	swnoMap := make(map[string]interface{}, 0)
+	swnoMap["swno"] = swno
+	swnoMap["saleTax"] = saleTax
+	swnoMap["model"] = model
+	swnoMap["isfeedback"] = isfeedback
+	swnoMap["isLoop"] = 1
+	swnoMap["changed"]=changed
+	valueInface := redis.Get(core.GetConfiguration().Redis.Modules, "fp_"+swno)
+	if (valueInface == nil) {
+		swnoMap["Frequency"] = 0
+		redis.PutKV("fp_"+swno, swnoMap)
+	}
 	c := cron.New()
 	spec := entity.TimingCron
 	c.AddFunc(spec, func() { // AddFunc 是添加任务的地方,此函数接收两个参数,第一个为表示定时任务的字符串,第二个为真正的真正的任务。
-		util.Loger.Println("定时任务开始:", "流水号:",swno, "发票开具方税号:",saleTax,)
-		pdfData,numb, _ := u.InvoiceDownload(swno, saleTax,model)
+		util.Loger.Println("定时任务开始:", "流水号:", swno, "发票开具方税号:", saleTax, )
+		valueInface := redis.Get(core.GetConfiguration().Redis.Modules, "fp_"+swno)
+		swnoMap := make(map[string]interface{}, 0)
+		if (valueInface == nil) {
+			swnoMap["swno"] = swno
+			swnoMap["saleTax"] =saleTax
+			swnoMap["model"] =model
+			swnoMap["isfeedback"] = isfeedback
+			swnoMap["Frequency"] = 0
+			swnoMap["changed"]=changed
+			redis.PutKV("fp_"+swno, swnoMap)
+		} else {
+			_rdsVal := valueInface.(map[string]interface{})
+			swnoMap["swno"] = _rdsVal["swno"]
+			swnoMap["saleTax"] = _rdsVal["saleTax"]
+			swnoMap["model"] = _rdsVal["model"]
+			swnoMap["isfeedback"] = _rdsVal["isfeedback"]
+			swnoMap["isLoop"] = _rdsVal["isLoop"]
+			swnoMap["changed"]=changed
+			numb, _ := strconv.Atoi(fmt.Sprint(_rdsVal["Frequency"]))
+			fmt.Sprint("numb","执行次数")
+			if numb < entity.Frequency {
+				util.Loger.Println("第:", numb, "次执行", "流水号:", swno, "发票开具方税号:", saleTax)
+				numb++
+				swnoMap["Frequency"] = numb
+				redis.PutKV("fp_"+swno, swnoMap)
+			} else {
+				redis.Del(core.GetConfiguration().Redis.Modules, "fp_"+swno)
+				c.Stop()
+			}
+		}
+
+		pdfData, numb, _ := u.InvoiceDownload(swno, saleTax, model, 1, 0,changed)
+		fmt.Println(pdfData)
 		if (numb == 0) {
-			util.Loger.Println("定时开票成功:", "流水号:",swno, "发票开具方税号:",saleTax,)
-			redis.Del(core.GetConfiguration().Redis.Modules, "fp_"+swno)
-			//修改发票中状态
-			url:=entity.JyUrl+"?order_code="+swno[0 : len(swno)-6]+"&code=1&pfdm="+pdfData["fpdm"].(string)+"&fphm="+pdfData["fphm"].(string)+"&path="+pdfData["path"].(string)+"&swno="+swno
-			util.Loger.Println("更改发票状态:", "流水号:",swno, "发票开具方税号:",saleTax,)
-			req, _ := http.NewRequest("GET", url, nil)
-			res, _ := http.DefaultClient.Do(req)
-			defer res.Body.Close()
-			body, _ := ioutil.ReadAll(res.Body)
-			dat := make(map[string]interface{})
-			err := json.Unmarshal([]byte(body), &dat)
-			fmt.Println(err)
-			util.Loger.Println("更改发票结果:","流水号:",swno, "发票开具方税号:",saleTax,dat)
-			util.Loger.Println("定时任务结束:", "流水号:",swno, "发票开具方税号:",saleTax)
-			c.Stop()
-		}else if (numb == 1){
+			util.Loger.Println("定时开票成功:", "流水号:", swno, "发票开具方税号:", saleTax)
+			if (isfeedback == 0) {
+				//修改发票中状态
+				if (model == 1) {
+					swno = swno[4:len(swno)]
+				}
+					url:=entity.JyUrl+"?order_code="+swno[0 : len(swno)-6]+"&code=1&pfdm="+pdfData["fpdm"].(string)+"&fphm="+pdfData["fphm"].(string)+"&path="+pdfData["path"].(string)+"&swno="+swno+"&changed="+changed
+					util.Loger.Println("更改发票状态:", "流水号:",swno, "发票开具方税号:",saleTax,url)
+					req, _ := http.NewRequest("GET", url, nil)
+					res, _ := http.DefaultClient.Do(req)
+					defer res.Body.Close()
+					body, _ := ioutil.ReadAll(res.Body)
+					dat := make(map[string]interface{})
+					err := json.Unmarshal([]byte(body), &dat)
+					fmt.Println(err)
+					util.Loger.Println("更改发票结果:","流水号:",swno, "发票开具方税号:",saleTax,dat)
+					util.Loger.Println("定时任务结束:", "流水号:",swno, "发票开具方税号:",saleTax)
+					if(dat["status"]=="success"){
+						redis.Del(core.GetConfiguration().Redis.Modules, "fp_"+swno)
+						c.Stop()
+					}
+				/*redis.Del(core.GetConfiguration().Redis.Modules, "fp_"+swno)
+				c.Stop()*/
+			} else {
+				redis.Del(core.GetConfiguration().Redis.Modules, "fp_"+swno)
+				c.Stop()
+			}
+		} else if (numb == 1) {
 			c.Stop()
 		}
 	})

+ 4 - 4
invoice/src/util/push.go

@@ -25,15 +25,15 @@ func Exists(path string) (bool) {
 }
 
 //base64转pdf存本地
-func ImgHandle(imgBase64 string, swno string,saleTax string) string {
+func ImgHandle(imgBase64 string, swno string,saleTax string,swno1 string) string {
 	imgs, err := base64.StdEncoding.DecodeString(imgBase64)
 	if err != nil {
 		fmt.Println(err)
 	}
 	imgname := swno  + ".pdf"
-	exPath := "./static/res/" + saleTax + "/"
-	exPath1 := "/static/res/" + saleTax + "/"
-	os.MkdirAll("./static/res/"+saleTax, os.ModePerm)
+	exPath := "./static/res/" + saleTax + "/"+swno1+"/"
+	exPath1 := "/static/res/" + saleTax + "/"+swno1+"/"
+	os.MkdirAll("./static/res/"+saleTax+"/"+swno1, os.ModePerm)
 	file, err := os.OpenFile(exPath+imgname, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
 	if err != nil {
 		fmt.Println(err)