|
@@ -0,0 +1,176 @@
|
|
|
|
+package service
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "bytes"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "entity"
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/robfig/cron"
|
|
|
|
+ "io/ioutil"
|
|
|
|
+ "net/http"
|
|
|
|
+ "spirit/redis"
|
|
|
|
+ "spirit/web/core"
|
|
|
|
+ "util"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type InvoiceService struct{}
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ invoiceService = InvoiceService{}
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// 发票开具
|
|
|
|
+func (u *InvoiceService) InvoiceAdd(solgan *entity.Invoice) (string, int, interface{}) {
|
|
|
|
+ url := entity.Url + "?build_invoice"
|
|
|
|
+ bytesData, _ := json.Marshal(solgan)
|
|
|
|
+ fmt.Println("json", string(bytesData))
|
|
|
|
+ reader := bytes.NewReader(bytesData)
|
|
|
|
+ request, err := http.NewRequest("POST", url, reader)
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ request.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
|
|
|
+ client := http.Client{}
|
|
|
|
+ resp, err := client.Do(request)
|
|
|
|
+ respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
+ data := make(map[string]interface{})
|
|
|
|
+ if err := json.Unmarshal([]byte(respBytes), &data); err == nil {
|
|
|
|
+ fmt.Println("==============json str 转map=======================", data)
|
|
|
|
+ }
|
|
|
|
+ util.Loger.Println("开具发票:数据", solgan)
|
|
|
|
+ util.Loger.Println("开具发票:结果", data)
|
|
|
|
+ if (data["returnCode"] == entity.SuccessCode) {
|
|
|
|
+ pdfData,_,_:=u.InvoiceDownload(solgan.Swno, solgan.SaleTax,0)
|
|
|
|
+ data["fpdm"]=pdfData["fpdm"]
|
|
|
|
+ data["fphm"]=pdfData["fphm"]
|
|
|
|
+ return fmt.Sprintln(data["returnMsg"]), 0, data
|
|
|
|
+ } else if (data["returnCode"] == entity.LineUpCode || data["returnCode"] == entity.SealCode) {
|
|
|
|
+ u.InvoiceDownload(solgan.Swno, solgan.SaleTax,0)
|
|
|
|
+ fmt.Println("完成")
|
|
|
|
+ return fmt.Sprintln(data["returnMsg"]), 2, data
|
|
|
|
+ } else {
|
|
|
|
+ return fmt.Sprintln(data["returnMsg"]), 1, data
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 发票下载
|
|
|
|
+func (u *InvoiceService) InvoiceDownload(swno string, saleTax string,model int) (map[string]interface{}, int, string) {
|
|
|
|
+ url := entity.Url + "?get_invoice"
|
|
|
|
+ song := make(map[string]interface{})
|
|
|
|
+ song["swno"] = swno
|
|
|
|
+ song["saleTax"] = saleTax
|
|
|
|
+ bytesData, _ := json.Marshal(song)
|
|
|
|
+ fmt.Println("json", string(bytesData))
|
|
|
|
+ reader := bytes.NewReader(bytesData)
|
|
|
|
+ request, err := http.NewRequest("POST", url, reader)
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ request.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
|
|
|
+ client := http.Client{}
|
|
|
|
+ resp, err := client.Do(request)
|
|
|
|
+ respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
+ var dat *entity.GetInvoice
|
|
|
|
+ if err := json.Unmarshal([]byte(respBytes), &dat); err == nil {
|
|
|
|
+ fmt.Println("==============json str 转map=======================")
|
|
|
|
+ }
|
|
|
|
+ util.Loger.Println("发票下载:", "流水号:",swno, "发票开具方税号:",saleTax,"结果:", dat)
|
|
|
|
+ if (dat.ReturnMsg.MsgCode == entity.SuccessCode) {
|
|
|
|
+ if(model==1){
|
|
|
|
+ swno=swno[4 : len(swno)]
|
|
|
|
+ }
|
|
|
|
+ pdfData := make(map[string]interface{})
|
|
|
|
+ path := util.ImgHandle(dat.FpMsgs[0].PdfContent, swno)
|
|
|
|
+ pdfData["path"] = path
|
|
|
|
+ pdfData["fpdm"] = dat.FpMsgs[0].Fpdm
|
|
|
|
+ pdfData["fphm"] = dat.FpMsgs[0].Fphm
|
|
|
|
+ /*if(email!=""){
|
|
|
|
+ //util.SendPdf(email,pathArr,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["swno"] = swno
|
|
|
|
+ swnoMap["saleTax"] = saleTax
|
|
|
|
+ swnoMap["model"] = model
|
|
|
|
+ redis.Pop("fp_"+swno, fmt.Sprint(swnoMap))
|
|
|
|
+ u.Timer(swno, saleTax,model)
|
|
|
|
+ return nil, 2, dat.ReturnMsg.Msg
|
|
|
|
+ } else {
|
|
|
|
+ return nil, 1, dat.ReturnMsg.Msg
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 发票是否存在
|
|
|
|
+//noinspection ALL
|
|
|
|
+func (u *InvoiceService) InvoiceSee(swno string, saleTax string) (int, interface{}, string) {
|
|
|
|
+ fool := util.Exists("./static/res/" + swno + "/")
|
|
|
|
+ msg := "发票地址"
|
|
|
|
+ if fool {
|
|
|
|
+ pdfData:=make(map[string]interface{})
|
|
|
|
+ pdfData["path"] = entity.PdfUrl+"/static/res/" + swno + "/" + swno + ".pdf"
|
|
|
|
+ return 0, pdfData, msg
|
|
|
|
+ } else {
|
|
|
|
+ path, numb, _ := u.InvoiceDownload(swno, saleTax,0)
|
|
|
|
+ return numb, path, msg
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 退票
|
|
|
|
+//noinspection ALL
|
|
|
|
+func (u *InvoiceService) InvoiceRedSubmit(fpdm string, fphm string) (string, int, interface{}) {
|
|
|
|
+ url := entity.Url + "?redSubmitEInvoiceInfo"
|
|
|
|
+ song := make(map[string]interface{})
|
|
|
|
+ song["fpdm"] = fpdm
|
|
|
|
+ song["fphm"] = fphm
|
|
|
|
+ bytesData, _ := json.Marshal(song)
|
|
|
|
+ fmt.Println("json", string(bytesData))
|
|
|
|
+ reader := bytes.NewReader(bytesData)
|
|
|
|
+ request, err := http.NewRequest("POST", url, reader)
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ request.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
|
|
|
+ client := http.Client{}
|
|
|
|
+ resp, err := client.Do(request)
|
|
|
|
+ respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
+ data := make(map[string]interface{})
|
|
|
|
+ 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{
|
|
|
|
+ return fmt.Sprintln(data["returnMsg"]), 1, data
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 换票
|
|
|
|
+//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, code, datamap := u.InvoiceAdd(solgan)
|
|
|
|
+ return msg, code, datamap
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (u *InvoiceService) Timer(swno string, saleTax string,model int ) {
|
|
|
|
+ c := cron.New()
|
|
|
|
+ spec := entity.TimingCron
|
|
|
|
+ c.AddFunc(spec, func() { // AddFunc 是添加任务的地方,此函数接收两个参数,第一个为表示定时任务的字符串,第二个为真正的真正的任务。
|
|
|
|
+ util.Loger.Println("定时任务开始:", "流水号:",swno, "发票开具方税号:",saleTax,)
|
|
|
|
+ _, numb, _ := u.InvoiceDownload(swno, saleTax,model)
|
|
|
|
+ if (numb == 0) {
|
|
|
|
+ util.Loger.Println("定时开票成功:", "流水号:",swno, "发票开具方税号:",saleTax,)
|
|
|
|
+ redis.Del(core.GetConfiguration().Redis.Modules, "fp_"+swno)
|
|
|
|
+ util.Loger.Println("定时任务结束:", "流水号:",swno, "发票开具方税号:",saleTax,)
|
|
|
|
+ c.Stop()
|
|
|
|
+ }else if (numb == 1){
|
|
|
|
+ c.Stop()
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ c.Start()
|
|
|
|
+}
|