task.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. }
  225. func Unlocking(start time.Time, tasks []models.TaxTask) {
  226. reqs := []string{}
  227. for _, v := range tasks {
  228. reqs = append(reqs, strings.Split(v.ReqNo, "&")...)
  229. }
  230. req := strings.Join(reqs, "*")
  231. UnTels := make(map[string]bool, 1000)
  232. if len(tasks) < 1 {
  233. return
  234. }
  235. //工商的任务不在这里解锁了(避免影响到这个手机号下的税局申报任务,因为如果不加锁的话120秒是可能不够工商申报的,并发的话税局任务会一直挂掉)
  236. //如果想要工商任务快一点(客户嫌慢的话),工商任务拿到的时候自己加锁,加锁时长要大于等待验证码的时间(确保收到验证码后解锁 解的是自己加的锁),然后收到验证码的时候解锁
  237. for _, task := range tasks {
  238. if task.TaskName == variable.TaxCjGsnb || task.TaskName == variable.TaxSbGsnb {
  239. return
  240. }
  241. }
  242. // 采集pdf除外 因为超了20分钟就不解锁了 pdf统一上锁3600
  243. if (tasks[0].Address == "jiangsu" || tasks[0].Address == "henan") && tasks[0].TaskName != variable.TaxCjInvoicePdf { //&& (tasks[0].LoginType == "政务网登录" || tasks[0].LoginType == "免验证码登录")
  244. checkPass := true
  245. info, _ := GetEacComInfo(tasks[0])
  246. if info.Dlfs == "新版登录" && strings.Contains(tasks[0].Result.BusinessLog, "验证码接收失败") {
  247. logger.Info("结束该手机号剩余任务:" + info.Tel)
  248. common.CancelLeftTasks(info.Tel)
  249. }
  250. for _, task := range tasks {
  251. if strings.Contains(task.Result.BusinessLog, "超限") { //strings.Contains(task.Result.BusinessLog, "锁定") ||
  252. checkPass = false
  253. }
  254. }
  255. if tasks[0].Address == "henan" && checkPass {
  256. if !time.Now().After(start.Add(12 * time.Minute)) { //不超过7分钟的解锁
  257. tel := strings.Split(tasks[0].Tel, `#`)[0]
  258. if time.Since(start).Seconds() < 45 {
  259. itv := common.FloatToIntStr((45 - time.Since(start).Seconds()) / 1)
  260. logger.Info("加锁:" + tel + " " + itv)
  261. common.AddTelLockerX2(tel, itv)
  262. return
  263. }
  264. logger.Info("解锁:" + tel)
  265. common.AddTelLockerX2Req(tel, "-1", req)
  266. } else if tasks[0].TaskName == variable.TaxCjInvoicePdf { //pdf的任务,如果没有任务在执行也解锁
  267. tel := strings.Split(tasks[0].Tel, `#`)[0]
  268. status, err := common.GetTelTaskStatus(tel)
  269. if err != nil {
  270. logger.Info("获取手机号任务状态失败")
  271. return
  272. }
  273. if !status {
  274. logger.Info("解锁:" + tel)
  275. common.AddTelLockerX2Req(tel, "-1", req)
  276. }
  277. return
  278. }
  279. }
  280. if tasks[0].Address == "jiangsu" && !time.Now().After(start.Add(18*time.Minute)) && checkPass {
  281. tel := strings.Split(tasks[0].Tel, `#`)[0]
  282. if time.Since(start).Seconds() < 45 {
  283. itv := common.FloatToIntStr((45 - time.Since(start).Seconds()) / 1)
  284. logger.Info("加锁:" + tel + " " + itv)
  285. common.AddTelLockerX2(tel, itv)
  286. return
  287. }
  288. logger.Info("解锁:" + tel)
  289. common.AddTelLockerX2Req(tel, "-1", req)
  290. }
  291. return
  292. }
  293. for _, task := range tasks {
  294. info, _ := GetEacComInfo(task)
  295. //address := info.Area
  296. // 新版登录 收不到验证码撤销掉剩余的任务,避免一直发导致超限
  297. if info.Dlfs == "新版登录" && strings.Contains(task.Result.BusinessLog, "验证码接收失败") {
  298. logger.Info("结束该手机号剩余任务:" + info.Tel)
  299. common.CancelLeftTasks(info.Tel)
  300. }
  301. //
  302. {
  303. //// 新版=统一解锁
  304. //if (info.Dlfs == "新版登录" || info.Dlfs == "代理登录") && info.Tel != "" && !(strings.Contains(task.Result.BusinessLog, "超限") || strings.Contains(task.Result.BusinessLog, "发送短信过于频繁")) {
  305. // if !UnTels[info.Tel] {
  306. // UnTels[info.Tel] = true
  307. // }
  308. //}
  309. //// zrridno统一解锁
  310. //if address == Hebei || address == Hainan || address == Yunnan || address == Shandong || address == Beijing || address == Xizang || address == "zhejiang" || address == "guangdong" || address == "jilin" {
  311. // if info.Dlfs == "新版登录" {
  312. // if !UnTels[info.Tel] {
  313. // UnTels[info.Tel] = true
  314. // }
  315. // } else {
  316. // if !UnTels[info.Zzridno] {
  317. // UnTels[info.Zzridno] = true
  318. // }
  319. // }
  320. //}
  321. //// zrrxm解锁
  322. //if address == "ningxia" {
  323. // if !UnTels[info.Zzrxm] {
  324. // UnTels[info.Zzrxm] = true
  325. // }
  326. //}
  327. //// 所有登陆方式都需要解锁的地区
  328. //if address == Shanxi || address == Jiangxi || address == Guangxi || address == Neimenggu || address == Xinjiang || address == Shenzhen || address == Henan || address == Anhui || address == Shaanxi || address == Hunan || address == Qingdao || address == Sichuan || address == Guizhou || address == Gansu || address == Dalian {
  329. // if !UnTels[info.Tel] {
  330. // UnTels[info.Tel] = true
  331. // }
  332. //}
  333. ////if (address == "tianjin" || address == "guangdong" || address == "zhejiang" || address == "hubei") && info.Dlfs == "新版登录" {
  334. //// if !UnTels[info.Zzridno+"_"+info.Area] {
  335. //// UnTels[info.Zzridno+"_"+info.Area] = true
  336. //// }
  337. ////}
  338. //// 针对发送超限
  339. //if strings.Contains(task.Result.BusinessLog, "超限") || strings.Contains(task.Result.BusinessLog, "发送短信过于频繁") {
  340. // common.AddTelLockerX2(info.Tel, "600")
  341. //}
  342. //// 针对新疆
  343. //if strings.Contains(task.Result.BusinessLog, "因上条短信验证码未正常使用,无法重新获取验证码,请您在5分钟后上条短信失效后") {
  344. // common.AddTelLockerX2(info.Tel, "300")
  345. //}
  346. }
  347. // 现在直接用手机号解锁,因为不管用什么加锁都会加到tel字段上
  348. if info.Tel != "" {
  349. UnTels[info.Tel] = true
  350. }
  351. // 频繁加锁
  352. if strings.Contains(task.Result.BusinessLog, "短信发送申请过于频繁") || strings.Contains(task.Result.BusinessLog, "超限") || strings.Contains(task.Result.BusinessLog, "发送短信过于频繁") || strings.Contains(task.Result.BusinessLog, "因上条短信验证码未正常使用,无法重新获取验证码,请您在5分钟后上条短信失效后") {
  353. UnTels[info.Tel] = false
  354. common.AddTelLockerX2(info.Tel, "300") // 直接覆盖一个5分钟的锁
  355. }
  356. }
  357. for k := range UnTels {
  358. haspdf := false
  359. for _, v := range tasks {
  360. if v.TaskName == variable.TaxCjInvoicePdf {
  361. haspdf = true
  362. break
  363. }
  364. }
  365. if time.Since(start).Seconds() < 1195 || haspdf {
  366. logger.Info("开始时间:", start.Format("2006-01-02 15:04:05"), ";结束时间:", time.Now().Format("2006-01-02 15:04:05"), ";任务时长:", time.Since(start).Seconds())
  367. logger.Info("解锁:" + k)
  368. common.AddTelLockerX2Req(k, "-1", req)
  369. }
  370. //if strings.Contains(k, "anhui") {
  371. // logger.Info("安徽暂时全解锁:" + k)
  372. // common.AddTelLockerX2Req(k, "-1", req)
  373. //}
  374. }
  375. }