invoiceManager.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package service
  2. import (
  3. "ElectronicInvoice/internal/consts"
  4. "context"
  5. "github.com/gogf/gf/v2/database/gdb"
  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. "github.com/gogf/gf/v2/util/gconv"
  10. "strconv"
  11. "time"
  12. )
  13. var (
  14. JyInvoiceManager *InvoiceManager
  15. )
  16. type InvoiceManager struct {
  17. Auth *TripartiteAuth
  18. jobRunning bool //开票是否运行中
  19. StopRunning bool //财务占用账号,需要暂停任务
  20. Login bool //登录状态
  21. runPool chan bool //任务(每次只能进行一个开票任务)
  22. phoneCode chan string //短信验证码池
  23. ScanLogin chan bool //扫码登录
  24. }
  25. func init() {
  26. JyInvoiceManager = createInvoiceManager()
  27. job, err := gcron.Add(context.Background(), g.Cfg().MustGet(context.Background(), "invoiceJob.cron").String(), JyInvoiceManager.RunJob, "invoiceJob")
  28. if err != nil {
  29. panic(err)
  30. }
  31. job.Start()
  32. JyInvoiceManager.Auth.CallBackInit()
  33. //go JyInvoiceManager.RunOneJob(context.Background())//流程
  34. //go JyInvoiceManager.Demo(context.Background()) //开票
  35. //go JyInvoiceManager.RedDemo(context.Background())//红冲
  36. }
  37. func createInvoiceManager() *InvoiceManager {
  38. return &InvoiceManager{
  39. Auth: createTripartite(),
  40. Login: true, //默认已经登录
  41. runPool: make(chan bool, 1), //开票只能单线程跑
  42. phoneCode: make(chan string, 1), //手机验证码
  43. ScanLogin: make(chan bool, 1), //扫码登录通知
  44. }
  45. }
  46. func (im *InvoiceManager) ReleasePool() {
  47. <-im.runPool
  48. }
  49. func (im *InvoiceManager) MobileVerificationCode(code string) error {
  50. if code == "" {
  51. return gerror.New("验证码为空")
  52. }
  53. select {
  54. case <-time.After(time.Minute):
  55. return gerror.New("验证码接收超时")
  56. case im.phoneCode <- code:
  57. return nil
  58. }
  59. }
  60. func (im *InvoiceManager) MobileVerificationClear() error {
  61. select {
  62. case <-time.After(time.Minute):
  63. return gerror.New("清除验证码超时")
  64. case <-im.phoneCode:
  65. return nil
  66. }
  67. }
  68. // RunJob 开票定时任务
  69. func (im *InvoiceManager) RunJob(ctx context.Context) {
  70. if im.jobRunning || im.StopRunning {
  71. g.Log().Infof(ctx, "RunJob-程序本次任务中断 jobRunning:%v StopRunning:%v", im.jobRunning, im.StopRunning)
  72. return
  73. }
  74. im.jobRunning = true
  75. defer func() {
  76. im.jobRunning = false
  77. g.Log().Infof(ctx, "RunJob-开票任务完成")
  78. }()
  79. if g.Cfg().MustGet(ctx, "invoiceJob.stop", false).Bool() {
  80. g.Log().Infof(ctx, "RunJob-开票程序任务已暂停,开启请删除 config.json > invoiceJob.stop")
  81. return
  82. }
  83. if !im.Login {
  84. if err := im.Auth.Login(); err != nil {
  85. g.Log().Errorf(ctx, "模拟登录异常 %v", err)
  86. return
  87. } else {
  88. g.Log().Infof(ctx, "登录成功")
  89. }
  90. }
  91. //TODO 普通蓝票任务
  92. total, okNum, err := im.simpleMakeInvoice(ctx)
  93. if err != nil {
  94. if gerror.Is(err, consts.LoginOutErr) {
  95. g.Log().Infof(ctx, "RunJob-任务中止-开蓝票任务身份过期,需要重新登录")
  96. return
  97. } else if gerror.Is(err, consts.WaitTimeOut) {
  98. g.Log().Errorf(ctx, "RunJob-任务中止-开蓝票任务等待超时异常 %v", err)
  99. return
  100. }
  101. g.Log().Errorf(ctx, "RunJob-开蓝票任务异常 %v", err)
  102. } else {
  103. g.Log().Infof(ctx, "RunJob-开蓝票任务完成 共%d个 完成%d个", total, okNum)
  104. }
  105. // TODO 蓝票自助开票(管理后台扫码开票)
  106. total, okNum, err = im.selfMakeInvoice(ctx)
  107. if err != nil {
  108. if gerror.Is(err, consts.LoginOutErr) {
  109. g.Log().Infof(ctx, "RunJob-任务中止-开自助蓝票任务身份过期,需要重新登录")
  110. return
  111. } else if gerror.Is(err, consts.WaitTimeOut) {
  112. g.Log().Errorf(ctx, "RunJob-任务中止-开自助蓝票任务等待超时异常 %v", err)
  113. return
  114. }
  115. g.Log().Errorf(ctx, "RunJob-开自助蓝票任务异常 %v", err)
  116. } else {
  117. g.Log().Infof(ctx, "RunJob-开自助蓝票任务完成 共%d个 完成%d个", total, okNum)
  118. }
  119. //TODO 红票任务
  120. }
  121. // simpleMakeInvoice 简单开票
  122. func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int, err error) {
  123. var (
  124. res gdb.Result
  125. )
  126. //查询需要开票的数据
  127. res, err = g.DB().Query(ctx, "SELECT a.*,b.pay_way,b.order_money,b.pay_money FROM invoice a INNER JOIN dataexport_order b ON a.order_code=b.order_code WHERE a.invoice_status=0 AND a.invoice_changed=0 AND a.invoice_variety='普通发票(电子发票)' AND a.invoice_order_code is NULL")
  128. if err != nil {
  129. return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
  130. }
  131. g.Log().Infof(ctx, "simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
  132. total, okNum = res.Len(), 0
  133. for _, m := range res.List() {
  134. select {
  135. case im.runPool <- true:
  136. case <-time.After(time.Minute * 5):
  137. err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
  138. return
  139. }
  140. var (
  141. orderCode = gconv.String(m["order_code"])
  142. iType = gconv.String(m["invoice_type"])
  143. remark = gconv.String(m["remark"])
  144. prices float64
  145. )
  146. //公对公转账 账单金额可以修改 开发票应取实付金额 pay_money
  147. //微信支付宝支付 pay_money为订单金额减去微信or支付包红包
  148. if gconv.String(m["pay_way"]) == "transferAccounts" {
  149. prices = gconv.Float64(m["pay_money"]) / float64(100)
  150. } else {
  151. prices = gconv.Float64(m["order_money"]) / float64(100)
  152. }
  153. c := MakeInvoiceData{
  154. Type: "2",
  155. Id: orderCode,
  156. Notes: remark,
  157. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  158. InvoiceArr: []MakeInvoiceItems{{
  159. Xmmc: g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
  160. WhStatus: 1, //开票项是否维护
  161. Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
  162. Sl: "1", //数量
  163. }},
  164. }
  165. if iType == "单位" {
  166. c.Gmfmc = gconv.String(m["company_name"])
  167. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  168. } else {
  169. c.Gmfmc = iType
  170. }
  171. err = im.Auth.MakeSingleInvoice(c)
  172. if err != nil {
  173. im.ReleasePool()
  174. if gerror.Is(err, consts.LoginOutErr) {
  175. g.Log().Infof(ctx, "simpleMakeInvoice-身份过期,需要重新登录")
  176. return
  177. }
  178. g.Log().Errorf(ctx, "simpleMakeInvoice-开票接口调用异常 %v", err)
  179. continue
  180. }
  181. okNum++
  182. }
  183. return
  184. }
  185. // selfMakeInvoice 自助开票
  186. func (im *InvoiceManager) selfMakeInvoice(ctx context.Context) (total, okNum int, err error) {
  187. var (
  188. res gdb.Result
  189. )
  190. res, err = g.DB().Query(ctx, "SELECT a.invoice_money,a.only_Identifying,a.invoice_type,a.company_name,a.taxpayer_identnum,a.remark FROM invoice a WHERE a.invoice_status=0 AND a.invoice_changed=0 AND a.invoice_variety='电子普通发票' AND a.invoice_order_code is not NULL GROUP BY invoice_order_code")
  191. if err != nil {
  192. return -1, -1, gerror.Wrap(err, "selfMakeInvoice-查询待开票异常")
  193. }
  194. total, okNum = res.Len(), 0
  195. for _, m := range res.List() {
  196. select {
  197. case im.runPool <- true:
  198. case <-time.After(time.Minute * 5):
  199. err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
  200. return
  201. }
  202. var (
  203. orderCode = gconv.String(m["only_Identifying"])
  204. iType = gconv.String(m["invoice_type"])
  205. prices = gconv.String(m["invoice_money"])
  206. remark = gconv.String(m["remark"])
  207. )
  208. c := MakeInvoiceData{
  209. Type: "2",
  210. Id: orderCode,
  211. Notes: remark,
  212. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  213. InvoiceArr: []MakeInvoiceItems{{
  214. Xmmc: g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
  215. WhStatus: 1, //开票项是否维护
  216. Je: prices, //金额
  217. Sl: "1", //数量
  218. }},
  219. }
  220. if iType == "单位" {
  221. c.Gmfmc = gconv.String(m["company_name"])
  222. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  223. } else {
  224. c.Gmfmc = iType
  225. }
  226. err = im.Auth.MakeSingleInvoice(c)
  227. if err != nil {
  228. im.ReleasePool()
  229. if gerror.Is(err, consts.LoginOutErr) {
  230. g.Log().Infof(ctx, "selfMakeInvoice-身份过期,需要重新登录")
  231. return
  232. }
  233. g.Log().Errorf(ctx, "selfMakeInvoice-开票接口调用异常 %v", err)
  234. continue
  235. }
  236. okNum++
  237. }
  238. return
  239. }