invoiceManager.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package service
  2. import (
  3. "ElectronicInvoice/util"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/os/gcron"
  9. "time"
  10. )
  11. var (
  12. JyInvoiceManager *InvoiceManager
  13. )
  14. type InvoiceManager struct {
  15. Auth *TripartiteAuth
  16. jobRunning bool //开票是否运行中
  17. StopRunning bool //财务占用账号,需要暂停任务
  18. Login bool //登录状态
  19. RunPool chan bool //任务(每次只能进行一个开票任务)
  20. phoneCode chan string //短信验证码池
  21. ScanLogin chan bool //扫码登录
  22. }
  23. func init() {
  24. JyInvoiceManager = createInvoiceManager()
  25. //开票任务
  26. job, err := gcron.Add(context.Background(), g.Cfg().MustGet(context.Background(), "invoiceJob.cron").String(), JyInvoiceManager.RunJob, "invoiceJob")
  27. if err != nil {
  28. panic(err)
  29. }
  30. job.Start()
  31. //超时开票提醒
  32. jobNotice, errNotice := gcron.Add(context.Background(), g.Cfg().MustGet(context.Background(), "invoiceJob.notice.unFinish.cron").String(), JyInvoiceManager.NoticeJob, "invoiceNoticeJob")
  33. if errNotice != nil {
  34. panic(errNotice)
  35. }
  36. jobNotice.Start()
  37. //初始化三方接口
  38. JyInvoiceManager.Auth.CallBackInit()
  39. //go JyInvoiceManager.RunOneJob(context.Background())//流程
  40. //go JyInvoiceManager.Demo(context.Background()) //开票
  41. //go JyInvoiceManager.Auth.Login() //登录
  42. //go JyInvoiceManager.RedDemo(context.Background())//红冲
  43. //err = JyInvoiceManager.Auth.AddTaxCode([]Billitem{{
  44. // Name: "技术服务费",
  45. // Ssflbm: "3040205000000000000",
  46. // Thirdid: "6f2e6a7a-18a7-4f21-b52e-a47437f817cc",
  47. //}})
  48. //if err != nil {
  49. // panic(err)
  50. //}
  51. //list, err := JyInvoiceManager.Auth.GetSuccessTaxCodeList()
  52. //g.Dump(list, err)
  53. }
  54. func createInvoiceManager() *InvoiceManager {
  55. return &InvoiceManager{
  56. Auth: createTripartite(),
  57. Login: true, //默认已经登录
  58. RunPool: make(chan bool, 1), //开票只能单线程跑
  59. phoneCode: make(chan string, 1), //手机验证码
  60. ScanLogin: make(chan bool, 1), //扫码登录通知
  61. }
  62. }
  63. func (im *InvoiceManager) MobileVerificationCode(code string) error {
  64. if code == "" {
  65. return gerror.New("验证码为空")
  66. }
  67. select {
  68. case <-time.After(time.Minute):
  69. return gerror.New("验证码接收超时")
  70. case im.phoneCode <- code:
  71. return nil
  72. }
  73. }
  74. func (im *InvoiceManager) MobileVerificationClear() error {
  75. select {
  76. case <-time.After(time.Minute):
  77. return gerror.New("清除验证码超时")
  78. case <-im.phoneCode:
  79. return nil
  80. }
  81. }
  82. // RunJob 开票定时任务
  83. func (im *InvoiceManager) RunJob(ctx context.Context) {
  84. if im.jobRunning || im.StopRunning {
  85. g.Log().Infof(ctx, "RunJob-程序本次任务中断 jobRunning:%v StopRunning:%v", im.jobRunning, im.StopRunning)
  86. return
  87. }
  88. im.jobRunning = true
  89. defer func() {
  90. im.jobRunning = false
  91. g.Log().Infof(ctx, "RunJob-开票任务完成")
  92. }()
  93. if g.Cfg().MustGet(ctx, "invoiceJob.stop", false).Bool() {
  94. g.Log().Infof(ctx, "RunJob-开票程序任务已暂停,开启请删除 config.json > invoiceJob.stop")
  95. return
  96. }
  97. if !im.Login {
  98. if err := im.Auth.Login(); err != nil {
  99. g.Log().Errorf(ctx, "模拟登录异常 %v", err)
  100. return
  101. } else {
  102. g.Log().Infof(ctx, "登录成功")
  103. }
  104. }
  105. //TODO 普通蓝票任务(线上开票、管理后台一个订单开多张票)
  106. total, okNum, end, err := im.simpleMakeInvoice(ctx)
  107. if err != nil {
  108. g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice任务异常 %v", err)
  109. if end {
  110. return
  111. }
  112. }
  113. g.Log().Infof(ctx, "RunJob-simpleMakeInvoice任务完成 共%d个 完成%d个", total, okNum)
  114. // TODO 多订单合并开票(管理后台-多订单合并开票)
  115. total, okNum, end, err = im.multipleOrdersMakeInvoice(ctx)
  116. if err != nil {
  117. g.Log().Errorf(ctx, "RunJob-multipleOrdersMakeInvoice任务异常 %v", err)
  118. if end {
  119. return
  120. }
  121. }
  122. g.Log().Infof(ctx, "RunJob-multipleOrdersMakeInvoice任务完成 共%d个 完成%d个", total, okNum)
  123. // TODO 红冲(线上开票、管理后台一个订单开多张票)
  124. total, okNum, end, err = im.makeRedInvoice(ctx)
  125. if err != nil {
  126. g.Log().Errorf(ctx, "RunJob-makeRedInvoice任务异常 %v", err)
  127. if end {
  128. return
  129. }
  130. }
  131. g.Log().Infof(ctx, "RunJob-makeRedInvoice任务完成 共%d个 完成%d个", total, okNum)
  132. // TODO 红冲(线上开票、管理后台一个订单开多张票)
  133. total, okNum, end, err = im.multipleOrdersMakeRedInvoice(ctx)
  134. if err != nil {
  135. g.Log().Errorf(ctx, "RunJob-multipleOrdersMakeRedInvoice任务异常 %v", err)
  136. if end {
  137. return
  138. }
  139. }
  140. g.Log().Infof(ctx, "RunJob-multipleOrdersMakeRedInvoice任务完成 共%d个 完成%d个", total, okNum)
  141. }
  142. // NoticeJob 为开票成功提示
  143. func (im *InvoiceManager) NoticeJob(ctx context.Context) {
  144. var (
  145. unMakeRed = 0
  146. unMakeBlue = 0
  147. limitHours = g.Cfg().MustGet(ctx, "invoiceJob.notice.unFinish.hours", 24).Int64()
  148. )
  149. if im.StopRunning {
  150. return
  151. }
  152. createTimeLimit := time.Now().Unix() - limitHours*60*60
  153. // 蓝票未完成发票数量查询
  154. unMakeBlue, _ = g.DB().GetCount(ctx, `SELECT count(*) FROM(
  155. SELECT a.id as onlyId FROM invoice a WHERE a.invoice_variety like '%电子%' AND ((a.invoice_status=0 AND a.invoice_changed=0) or (a.invoice_status=2 AND a.invoice_changed=1 )) AND (a.invoice_order_code is NULL OR a.invoice_order_code not like '%,%') AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<?
  156. UNION
  157. SELECT a.only_Identifying as onlyId FROM invoice a WHERE a.invoice_variety like '%电子%' AND ((a.invoice_status=0 AND a.invoice_changed=0) or (a.invoice_status=2 AND a.invoice_changed=1)) AND a.invoice_order_code like '%,%' AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<? GROUP BY invoice_order_code
  158. ) as data`, createTimeLimit, createTimeLimit)
  159. // 红票未完成发票数量查询
  160. unMakeRed, _ = g.DB().GetCount(ctx, `SELECT count(*) FROM(
  161. SELECT a.invoice_number as onlyId FROM invoice a WHERE a.invoice_variety like '%电子%' AND a.invoice_status=0 AND a.invoice_changed=1 AND (a.invoice_order_code is NULL OR a.invoice_order_code not like '%,%') AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<?
  162. UNION
  163. SELECT a.invoice_number as onlyId FROM invoice a WHERE a.invoice_variety like '%电子%' AND a.invoice_status=0 AND a.invoice_changed=1 AND a.invoice_order_code like '%,%' AND a.create_time > UNIX_TIMESTAMP('2024-04-23 00:00:00') AND a.create_time<? GROUP BY invoice_order_code
  164. ) as data`, createTimeLimit, createTimeLimit)
  165. if unMakeRed > 0 || unMakeBlue > 0 {
  166. noticeMsg := fmt.Sprintf("开票超%d小时提醒\n", limitHours)
  167. if unMakeBlue > 0 {
  168. noticeMsg += fmt.Sprintf("蓝票:%d例\n", unMakeBlue)
  169. }
  170. if unMakeRed > 0 {
  171. noticeMsg += fmt.Sprintf("红票:%d例\n", unMakeRed)
  172. }
  173. if err := util.SendSimpleMsg2ChatBot(noticeMsg, g.Cfg().MustGet(ctx, "invoiceJob.notice.unFinish.notice").Strings()...); err != nil {
  174. g.Log().Errorf(ctx, "发送未开票消息异常 %v", err)
  175. }
  176. }
  177. }