ftp.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package service
  2. import (
  3. "fmt"
  4. "io"
  5. "mtp20_assisted/core"
  6. "mtp20_assisted/global"
  7. "mtp20_assisted/model"
  8. "mtp20_assisted/utils"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "encoding/csv"
  14. "encoding/hex"
  15. "go.uber.org/zap"
  16. )
  17. func InitTimer() (err error) {
  18. // 获取触发时间点
  19. timeStr := global.M2A_CONFIG.FTP.TimeAt
  20. timeArr := strings.Split(timeStr, ":")
  21. hour, _ := strconv.Atoi(timeArr[0])
  22. min, _ := strconv.Atoi(timeArr[1])
  23. sec, _ := strconv.Atoi(timeArr[2])
  24. loc, err := time.LoadLocation("Local")
  25. targetTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), hour, min, sec, 0, loc)
  26. // 启动协程处理任务
  27. go func() {
  28. for range time.Tick(24 * time.Hour) {
  29. // 计算时间差,等待执行时间
  30. dur := time.Until(targetTime)
  31. if dur < 0 {
  32. targetTime = targetTime.Add(24 * time.Hour)
  33. dur = time.Until(targetTime)
  34. }
  35. time.Sleep(dur)
  36. // 执行函数
  37. ImportWMSReckon()
  38. }
  39. }()
  40. return
  41. }
  42. // ImportWMSReckon 导入WMS结算单
  43. func ImportWMSReckon() {
  44. // 连接FTP
  45. var err error
  46. if global.M2A_FTP, err = core.FTP(); err != nil {
  47. global.M2A_LOG.Error("连接FTP失败, err:", zap.Error(err))
  48. return
  49. }
  50. defer global.M2A_FTP.Quit()
  51. // 暂存ftp文件的临时目录
  52. os.RemoveAll("./.ftp_tmp")
  53. os.Mkdir("./.ftp_tmp", os.ModePerm)
  54. // 列出FTP服务器上的文件列表
  55. entries, err := global.M2A_FTP.List(global.M2A_CONFIG.FTP.Folder)
  56. if err != nil {
  57. global.M2A_LOG.Error("FTP获取文件列表失败, err:", zap.Error(err))
  58. return
  59. }
  60. for _, entry := range entries {
  61. // 下载FTP服务器上的文件
  62. srcFile, err := global.M2A_FTP.Retr(global.M2A_CONFIG.FTP.Folder + "/" + entry.Name)
  63. if err != nil {
  64. global.M2A_LOG.Error("获取FTP源文件失败,", zap.String("file", entry.Name), zap.String("err", err.Error()))
  65. continue
  66. }
  67. dstFile, err := os.Create("./.ftp_tmp/" + entry.Name)
  68. if err != nil {
  69. global.M2A_LOG.Error("创建FTP临时文件失败,", zap.String("file", entry.Name), zap.String("err", err.Error()))
  70. continue
  71. }
  72. _, err = io.Copy(dstFile, srcFile)
  73. if err != nil {
  74. global.M2A_LOG.Error("下载FTP文件失败,", zap.String("file", entry.Name), zap.String("err", err.Error()))
  75. continue
  76. }
  77. srcFile.Close()
  78. // 创建CSV Reader
  79. dstFile.Close()
  80. dstFile, _ = os.Open("./.ftp_tmp/" + entry.Name)
  81. // defer dstFile.Close()
  82. reader := csv.NewReader(dstFile)
  83. // 设置分隔符为逗号
  84. reader.Comma = ','
  85. // 读取所有行
  86. rows, e := reader.ReadAll()
  87. if e != nil {
  88. err = e
  89. global.M2A_LOG.Error("读取CSV文件失败,", zap.String("file", entry.Name), zap.String("err", err.Error()))
  90. continue
  91. }
  92. for i, row := range rows {
  93. if i == 0 { // 第一行是列头
  94. continue
  95. }
  96. // 检验记录
  97. // 会员统一信用代码
  98. userKey := row[0]
  99. key, _ := hex.DecodeString(utils.AESSecretKey)
  100. var userKeyEncrypted []byte
  101. if userKeyEncrypted, err = utils.AESEncrypt([]byte(userKey), key); err != nil || userKeyEncrypted == nil {
  102. global.M2A_LOG.Error("加密会员统一信用代码失败,", zap.String("UserKey", userKey), zap.String("err", err.Error()))
  103. continue
  104. }
  105. userinfo := model.Userinfo{
  106. CARDNUM: hex.EncodeToString(userKeyEncrypted),
  107. }
  108. has := false
  109. if has, err = global.M2A_DB.Get(&userinfo); err != nil || !has {
  110. global.M2A_LOG.Error("用户唯一标识在数据库中无对应记录或读取数据库失败,", zap.String("UserKey", string(userKeyEncrypted)), zap.String("err", err.Error()))
  111. continue
  112. }
  113. // WMS结算单流水号
  114. wmsOrderID := row[1]
  115. gzbscreckonorder := model.Gzbscreckonorder{
  116. WMSORDERID: wmsOrderID,
  117. }
  118. has = false
  119. if has, _ = global.M2A_DB.Get(&gzbscreckonorder); has {
  120. global.M2A_LOG.Error("WMS结算单流水号已在数据库中存在,", zap.String("WMSOrderID", wmsOrderID))
  121. continue
  122. }
  123. // “会员统一信用代码”+ “月份(yyyyMM)“
  124. reckonMonth := row[3]
  125. gzbscreckonorder = model.Gzbscreckonorder{
  126. USERKEY: userKey,
  127. RECKONMONTH: reckonMonth,
  128. }
  129. has = false
  130. if has, _ = global.M2A_DB.Get(&gzbscreckonorder); has {
  131. global.M2A_LOG.Error("会员统一信用代码及月份已在数据库中存在,", zap.String("UserKey", userKey), zap.String("ReckonMonth", reckonMonth))
  132. continue
  133. }
  134. // 总费用
  135. totalFeeStr := row[9]
  136. if totalFeeStr == "" {
  137. global.M2A_LOG.Error("总费用为空,", zap.String("UserKey", userKey))
  138. continue
  139. }
  140. totalFee, err := strconv.ParseFloat(totalFeeStr, 64)
  141. if err != nil || totalFee == 0.0 {
  142. global.M2A_LOG.Error("总费用值错误,", zap.String("UserKey", userKey), zap.String("totalFee", totalFeeStr))
  143. continue
  144. }
  145. // 获取资金账户ID
  146. taaccounts := make([]model.Taaccount, 0)
  147. if err := global.M2A_DB.Where("USERID = ?", userinfo.USERID).Find(&taaccounts); err != nil || len(taaccounts) == 0 {
  148. global.M2A_LOG.Error("获取资金账户失败,", zap.String("UserKey", userKey), zap.String("err", err.Error()))
  149. continue
  150. }
  151. // 写入数据库
  152. sql := fmt.Sprintf(`
  153. insert into GZ_BSCRECKONORDER
  154. (
  155. OrderID,
  156. UserKey,
  157. WMSOrderID,
  158. ContractNo,
  159. ReckonMonth,
  160. ServiceFee,
  161. StorageFee,
  162. Premium,
  163. PowerFee,
  164. CustomsFee,
  165. TotalFee,
  166. Applicant,
  167. ApplicantTime,
  168. ImportFile,
  169. UserID,
  170. AccountID,
  171. PayStatus
  172. ) values
  173. (
  174. SEQ_GZ_BSCRECKONORDER.nextval,
  175. '%v',
  176. '%v',
  177. '%v',
  178. '%v',
  179. '%v',
  180. '%v',
  181. '%v',
  182. '%v',
  183. '%v',
  184. %v,
  185. '%v',
  186. '%v',
  187. '%v',
  188. %v,
  189. %v,
  190. 2
  191. )
  192. `, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11],
  193. entry.Name, userinfo.USERID, taaccounts[0].ACCOUNTID)
  194. if _, err := global.M2A_DB.Exec(sql); err != nil {
  195. global.M2A_LOG.Error("FTP数据写入数据库失败,", zap.String("UserKey", row[0]), zap.String("err", err.Error()))
  196. continue
  197. }
  198. // 移入FTP备份文件夹
  199. dstFile.Close()
  200. dstFile, _ = os.Open("./.ftp_tmp/" + entry.Name)
  201. // defer dstFile.Close()
  202. // 注意上传之前需要把之前的关闭,不然会报类似 移入FTP备份文件夹失败, {"file": "xxxxxxxxxxxx_202304.csv", "err": "229 Entering Extended Passive Mode (|||23113|)."}
  203. if err = global.M2A_FTP.Stor(global.M2A_CONFIG.FTP.FolderBakup+"/"+entry.Name, dstFile); err != nil {
  204. global.M2A_LOG.Error("移入FTP备份文件夹失败,", zap.String("file", entry.Name), zap.String("err", err.Error()))
  205. }
  206. if err = global.M2A_FTP.Delete(global.M2A_CONFIG.FTP.Folder + "/" + entry.Name); err != nil {
  207. global.M2A_LOG.Error("移入FTP备份文件夹失败,", zap.String("file", entry.Name), zap.String("err", err.Error()))
  208. }
  209. dstFile.Close()
  210. }
  211. }
  212. }