ftp.go 6.8 KB

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