|
@@ -6,6 +6,7 @@ import (
|
|
|
"github.com/tealeg/xlsx"
|
|
|
"hongze/hongze_data_crawler/models"
|
|
|
"hongze/hongze_data_crawler/utils"
|
|
|
+ "os"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -18,8 +19,51 @@ func FileCoalFirm() (err error) {
|
|
|
go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromDalian ErrMsg:"+err.Error(), utils.EmailSendToUsers)
|
|
|
}
|
|
|
}()
|
|
|
- path := "static/file/firm.xlsx"
|
|
|
- xlFile, err := xlsx.OpenFile(path)
|
|
|
+ var xlFile *xlsx.File
|
|
|
+ syear := time.Now().Year()
|
|
|
+ var smonth string
|
|
|
+ dateMonth := int(time.Now().Month())
|
|
|
+ smonth = strconv.Itoa(dateMonth)
|
|
|
+ //path := "static/file/firm.xlsx"
|
|
|
+ path := "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月).xlsx"
|
|
|
+ path = fmt.Sprintf(path,syear,smonth)
|
|
|
+ exist,err := PathExists(path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if exist == false {
|
|
|
+ path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月中旬).xlsx"
|
|
|
+ path = fmt.Sprintf(path,syear,smonth)
|
|
|
+ exist,err = PathExists(path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }else if exist == false {
|
|
|
+ path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月上旬).xlsx"
|
|
|
+ path = fmt.Sprintf(path,syear,smonth)
|
|
|
+ exist,err = PathExists(path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }else if exist == false {
|
|
|
+ //本月三个文件都没有,去取上一个月的
|
|
|
+ dateMonth = dateMonth - 1
|
|
|
+ smonth = strconv.Itoa(dateMonth)
|
|
|
+ path = "/home/code/python/coal_mail/emailFile/全国分企业煤炭产量旬度数据(%s年%s月).xlsx"
|
|
|
+ path = fmt.Sprintf(path,syear,smonth)
|
|
|
+ exist,err = PathExists(path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ path = fmt.Sprintf(path,syear,smonth)
|
|
|
+ xlFile, err = xlsx.OpenFile(path)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
return
|
|
@@ -244,3 +288,15 @@ func trimProvinceName(name string) string {
|
|
|
name = strings.Replace(name, "+", "", -1)
|
|
|
return name
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+func PathExists(path string)(bool, error) {
|
|
|
+ _, err := os.Stat(path)
|
|
|
+ if err == nil { //文件或者目录存在
|
|
|
+ return true, nil
|
|
|
+ }
|
|
|
+ if os.IsNotExist(err) {
|
|
|
+ return false, nil
|
|
|
+ }
|
|
|
+ return false, err
|
|
|
+}
|