ftp.go 6.8 KB

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