invoiceManager.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. }
  98. g.Log().Errorf(ctx, "RunJob-开蓝票任务异常 %v", err)
  99. } else {
  100. g.Log().Infof(ctx, "RunJob-开蓝票任务完成 共%d个 完成%d个", total, okNum)
  101. }
  102. // TODO 蓝票自助开票(管理后台扫码开票)
  103. total, okNum, err = im.selfMakeInvoice(ctx)
  104. if err != nil {
  105. if gerror.Is(err, consts.LoginOutErr) {
  106. g.Log().Infof(ctx, "RunJob-任务中止-身份过期,需要重新登录")
  107. return
  108. }
  109. g.Log().Errorf(ctx, "RunJob-开自助蓝票任务异常 %v", err)
  110. } else {
  111. g.Log().Infof(ctx, "RunJob-开自助蓝票任务完成 共%d个 完成%d个", total, okNum)
  112. }
  113. //TODO 红票任务
  114. }
  115. // simpleMakeInvoice 简单开票
  116. func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int, err error) {
  117. var (
  118. res gdb.Result
  119. )
  120. //查询需要开票的数据
  121. 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")
  122. if err != nil {
  123. return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
  124. }
  125. g.Log().Infof(ctx, "simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
  126. total, okNum = res.Len(), 0
  127. for _, m := range res.List() {
  128. select {
  129. case im.runPool <- true:
  130. case <-time.After(time.Minute * 5):
  131. err = gerror.New("simpleMakeInvoice-开票等待超时")
  132. return
  133. }
  134. var (
  135. orderCode = gconv.String(m["order_code"])
  136. iType = gconv.String(m["invoice_type"])
  137. remark = gconv.String(m["remark"])
  138. prices float64
  139. )
  140. //公对公转账 账单金额可以修改 开发票应取实付金额 pay_money
  141. //微信支付宝支付 pay_money为订单金额减去微信or支付包红包
  142. if gconv.String(m["pay_way"]) == "transferAccounts" {
  143. prices = gconv.Float64(m["pay_money"]) / float64(100)
  144. } else {
  145. prices = gconv.Float64(m["order_money"]) / float64(100)
  146. }
  147. c := MakeInvoiceData{
  148. Type: "2",
  149. Id: orderCode,
  150. Notes: remark,
  151. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  152. InvoiceArr: []MakeInvoiceItems{{
  153. Xmmc: g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
  154. WhStatus: 1, //开票项是否维护
  155. Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
  156. Sl: "1", //数量
  157. }},
  158. }
  159. if iType == "单位" {
  160. c.Gmfmc = gconv.String(m["company_name"])
  161. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  162. } else {
  163. c.Gmfmc = iType
  164. }
  165. err = im.Auth.MakeSingleInvoice(c)
  166. if err != nil {
  167. im.ReleasePool()
  168. if gerror.Is(err, consts.LoginOutErr) {
  169. g.Log().Infof(ctx, "simpleMakeInvoice-身份过期,需要重新登录")
  170. return
  171. }
  172. g.Log().Errorf(ctx, "simpleMakeInvoice-开票接口调用异常 %v", err)
  173. continue
  174. }
  175. okNum++
  176. }
  177. return
  178. }
  179. // selfMakeInvoice 自助开票
  180. func (im *InvoiceManager) selfMakeInvoice(ctx context.Context) (total, okNum int, err error) {
  181. var (
  182. res gdb.Result
  183. )
  184. 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")
  185. if err != nil {
  186. return -1, -1, gerror.Wrap(err, "selfMakeInvoice-查询待开票异常")
  187. }
  188. total, okNum = res.Len(), 0
  189. for _, m := range res.List() {
  190. select {
  191. case im.runPool <- true:
  192. case <-time.After(time.Minute * 5):
  193. err = gerror.New("selfMakeInvoice-开票等待超时")
  194. return
  195. }
  196. var (
  197. orderCode = gconv.String(m["only_Identifying"])
  198. iType = gconv.String(m["invoice_type"])
  199. prices = gconv.String(m["invoice_money"])
  200. remark = gconv.String(m["remark"])
  201. )
  202. c := MakeInvoiceData{
  203. Type: "2",
  204. Id: orderCode,
  205. Notes: remark,
  206. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  207. InvoiceArr: []MakeInvoiceItems{{
  208. Xmmc: g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
  209. WhStatus: 1, //开票项是否维护
  210. Je: prices, //金额
  211. Sl: "1", //数量
  212. }},
  213. }
  214. if iType == "单位" {
  215. c.Gmfmc = gconv.String(m["company_name"])
  216. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  217. } else {
  218. c.Gmfmc = iType
  219. }
  220. err = im.Auth.MakeSingleInvoice(c)
  221. if err != nil {
  222. im.ReleasePool()
  223. if gerror.Is(err, consts.LoginOutErr) {
  224. g.Log().Infof(ctx, "selfMakeInvoice-身份过期,需要重新登录")
  225. return
  226. }
  227. g.Log().Errorf(ctx, "selfMakeInvoice-开票接口调用异常 %v", err)
  228. continue
  229. }
  230. okNum++
  231. }
  232. return
  233. }