task.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package task
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "git.listensoft.net/tool/jspkit/common"
  8. "git.listensoft.net/tool/jspkit/common/lxhttp"
  9. "git.listensoft.net/tool/jspkit/common/models"
  10. "git.listensoft.net/tool/jspkit/common/variable"
  11. "git.listensoft.net/tool/jspkit/logger"
  12. "git.listensoft.net/tool/jspkit/taxerr"
  13. "github.com/spf13/viper"
  14. "github.com/tidwall/gjson"
  15. "os"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. const taxMaxTime = 20 * time.Minute //超时默认20分钟
  22. // GetContent 根据地区定制Content 设置超时时间
  23. func GetContent(childCtx context.Context, address string, pdfTel string, ticker *time.Ticker) (childCtx1 context.Context) {
  24. if address == "shandong" || address == "neimenggu" || address == "jiangsu" { // 针对20分钟不够用的地区
  25. childCtx1, _ = context.WithTimeout(childCtx, 30*time.Minute)
  26. } else if address == "sichuan" || address == "chongqing" || address == "shanxi" || (address == "anhui") {
  27. childCtx1, _ = context.WithTimeout(childCtx, 40*time.Minute)
  28. } else if address == "fujian" || address == "beijing" || address == "qinghai" || address == "xiamen" {
  29. childCtx1, _ = context.WithTimeout(childCtx, 40*time.Minute)
  30. } else {
  31. childCtx1, _ = context.WithTimeout(childCtx, taxMaxTime)
  32. }
  33. if pdfTel != "" {
  34. //childCtx1, _ = context.WithTimeout(childCtx, 120*time.Minute) //四川的pdf采集 一个小时不够
  35. ticker = time.NewTicker(4 * time.Minute) // 每四分钟加五分钟的锁
  36. go func() {
  37. for t := range ticker.C { // 阻塞等待接收ticker发出的时间信号
  38. common.AddTelLockerX2(pdfTel, "300")
  39. logger.Info("Tick at:", t)
  40. }
  41. }()
  42. }
  43. return
  44. }
  45. // GetTaxTask 获取任务
  46. func GetTaxTask(robotName string) (list []models.TaxTask, err error) {
  47. client := lxhttp.NewHttpClient()
  48. defer client.CloseIdleConnections()
  49. param := map[string]string{
  50. "robotName": robotName,
  51. }
  52. bys, err2 := lxhttp.POSTJson(client, variable.TaxTaskURL+"/api/v1/getRobotQueue", param, map[string]string{})
  53. if err2 != nil {
  54. return
  55. }
  56. data := gjson.GetBytes(bys, "data").String()
  57. list = []models.TaxTask{}
  58. err = json.Unmarshal([]byte(data), &list)
  59. return
  60. }
  61. // EndQueue 释放机器人
  62. func EndQueue(robotName string) {
  63. client := lxhttp.NewHttpClient()
  64. defer client.CloseIdleConnections()
  65. param := map[string]string{
  66. "robotName": robotName,
  67. }
  68. bys, err2 := lxhttp.POSTJson(client, variable.TaxTaskURL+"/api/v1/endQueue", param, map[string]string{}) // https://task.listensoft.net
  69. if err2 != nil {
  70. return
  71. }
  72. logger.Info("endQueue - ", string(bys))
  73. }
  74. // GetRobotName 获取当前机器人名称
  75. func GetRobotName() string {
  76. //环境变量中读取名称
  77. envRobotName := os.Getenv("robotName")
  78. logger.Info("envRobotName:", envRobotName)
  79. if envRobotName != "" {
  80. return envRobotName
  81. }
  82. //config中读取文件
  83. confRobotName := viper.GetString("robotName")
  84. logger.Info("confRobotName:", confRobotName)
  85. if confRobotName != "" { //如果是测试环境 固定一个 robotName 方便调试 一直获取一个任务
  86. return confRobotName
  87. }
  88. //读取不到默认文件夹名称
  89. dir, _ := os.Getwd()
  90. _, file := filepath.Split(dir)
  91. logger.Info("fileDIrRobotName:", file)
  92. return file
  93. }
  94. // 结束任务
  95. func EndTask(ctx context.Context, tasks []models.TaxTask) {
  96. clientURL := variable.TaxTaskURL + "/api/v1/endTaskTax"
  97. for k, v := range tasks {
  98. symbolToRemove := ","
  99. // 使用Trim函数删除字符串两端的指定符号
  100. tasks[k].Result.BusinessImg = strings.Trim(v.Result.BusinessImg, symbolToRemove)
  101. }
  102. for _, task := range tasks {
  103. // 检查 如果截图lv《申报清册_有遗漏》 状态不是有遗漏 这边强制改成有遗漏 遇到别的情况再额外处理
  104. if task.TaskName == variable.TaxJcShenBao &&
  105. (task.Result.BusinessStatus != variable.TaxSuccessdOmit && task.Result.BusinessStatus != variable.TaxFail) &&
  106. strings.Contains(task.Result.BusinessImg, "申报清册_有遗漏") {
  107. task.Result.BusinessStatus = variable.TaxSuccessdOmit
  108. task.Result.BusinessLog = "申报清册有遗漏,请看截图" + task.Result.BusinessLog
  109. }
  110. if task.ReqNo == "" {
  111. logger.Info("跳过用于解锁的空任务:", task)
  112. continue
  113. }
  114. task.Result.ReqNo = task.ReqNo //reqNo 返回
  115. if task.Result.Status == 0 { //如果没有状态 申报检查会带状态过来
  116. task.Result.Status = variable.TaxSuccess //默认任务成功
  117. }
  118. if !task.Result.BusinessStatus.Check() { //没有状态的话
  119. task.Result.BusinessStatus = variable.TaxFail //失败
  120. task.Result.BusinessLog = common.HandleError(ctx, taxerr.NewWebStuckTitle(false)).Error() //提示不支持的任务
  121. }
  122. //if task.Result.BusinessStatus.Success() { //如果是成功的话
  123. // task.Result.BusinessLog = "成功" //提示不支持的任务
  124. //}
  125. //if task.TaskName == variable.TaxCjInInvoice && strings.Contains(task.Result.BusinessLog, "请勾选认证后再次发起采集") {
  126. // task.Result.BusinessStatus = variable.TaxSuccessNeedPay
  127. //}
  128. if strings.Contains(string(task.TaskName), `kk`) && task.Result.BusinessStatus != variable.TaxTiJiaoJinSan {
  129. if task.Result.BusinessStatus != variable.TaxSuccessPaid && task.Result.BusinessStatus != variable.TaxSuccessdPaid {
  130. task.Result.BusinessStatus = variable.TaxFail
  131. } else {
  132. if task.TaskName != variable.TaxKkShebao { //社保的扣款任务要保留原状态作为 系统扣款 和 非系统扣款
  133. task.Result.BusinessStatus = variable.TaxSuccess
  134. }
  135. }
  136. }
  137. bys, _ := json.Marshal(task.Result)
  138. logger.Info("结束任务:", string(bys))
  139. for i := 0; i < 6; i++ {
  140. logger.Info(time.Now(), fmt.Sprintf("-----------第%s次尝试endTaskTax", strconv.Itoa(i)))
  141. res, err := lxhttp.HttpClientPost(lxhttp.NewHttpClient(), clientURL, "application/json", bytes.NewReader(bys))
  142. if err != nil || strings.Contains(string(res), "Sorry") {
  143. logger.Error("任务结束请求错误:" + err.Error())
  144. time.Sleep(time.Second * 10)
  145. continue
  146. } else {
  147. logger.Info(time.Now(), fmt.Sprintf("-----------第%s次endTaskTax成功", strconv.Itoa(i)))
  148. logger.Info("任务结束发送到服务器返回结果:", string(res))
  149. break
  150. }
  151. }
  152. }
  153. }
  154. // =================================== 历史数据采集相关 ======================================
  155. // 校验凭证出现的所有科目是否都在中间课余表中
  156. func CheckSubjectCodes(midSubjects []models.HisSubject, Vouchers []models.HisVoucher) bool {
  157. var Map1 map[string]bool // 凭证的所有科目
  158. Map1 = make(map[string]bool, len(midSubjects))
  159. var Map2 map[string]bool // 中间课余表的所有科目
  160. Map2 = make(map[string]bool, len(midSubjects))
  161. for _, v1 := range Vouchers {
  162. for _, v2 := range v1.Items {
  163. if !Map1[v2.SubjectCode] {
  164. Map1[v2.SubjectCode] = true
  165. }
  166. }
  167. }
  168. for _, v := range midSubjects {
  169. if !Map2[v.SubjectCode] {
  170. Map2[v.SubjectCode] = true
  171. }
  172. }
  173. for v := range Map1 {
  174. if !Map2[v] {
  175. for _, v1 := range Vouchers {
  176. for _, v2 := range v1.Items {
  177. if v == v2.SubjectCode {
  178. logger.Info(v1.VoucherNo, v1.Period)
  179. break
  180. }
  181. }
  182. }
  183. logger.Info(v)
  184. return false
  185. }
  186. }
  187. return true
  188. }
  189. // 返回错误信息
  190. func CheckSubjectCodesError(midSubjects []models.HisSubject, Vouchers []models.HisVoucher) (bool, string) {
  191. var Map1 map[string]bool // 凭证的所有科目
  192. Map1 = make(map[string]bool, len(midSubjects))
  193. var Map2 map[string]bool // 中间课余表的所有科目
  194. Map2 = make(map[string]bool, len(midSubjects))
  195. for _, v1 := range Vouchers {
  196. for _, v2 := range v1.Items {
  197. if !Map1[v2.SubjectCode] {
  198. Map1[v2.SubjectCode] = true
  199. }
  200. }
  201. }
  202. for _, v := range midSubjects {
  203. if !Map2[v.SubjectCode] {
  204. Map2[v.SubjectCode] = true
  205. }
  206. }
  207. for v := range Map1 {
  208. if !Map2[v] {
  209. msg := ""
  210. for _, v1 := range Vouchers {
  211. for _, v2 := range v1.Items {
  212. if v == v2.SubjectCode {
  213. msg = fmt.Sprintf("%s期%s凭证出现缺失%s科目", v1.Period, v1.VoucherNo, v)
  214. logger.Info(v1.VoucherNo, v1.Period)
  215. break
  216. }
  217. }
  218. }
  219. logger.Info(v)
  220. return false, msg
  221. }
  222. }
  223. return true, ""
  224. }