123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- package services
- import (
- "eta/eta_crawler/models"
- "eta/eta_crawler/services/alarm_msg"
- "eta/eta_crawler/utils"
- "fmt"
- "github.com/mozillazg/go-pinyin"
- "github.com/tealeg/xlsx"
- "strconv"
- "strings"
- "time"
- )
- func FileCoalCoastal() {
- var err error
- defer func() {
- if err != nil {
- fmt.Println("RefreshDataFromCoalCoastal Err:" + err.Error())
- msg := "失败提醒" + "RefreshDataFromCoalCoastal ErrMsg:" + err.Error()
- go alarm_msg.SendAlarmMsg(msg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalCoastal ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- //path := "static/file/沿海八省动力煤终端用户供耗存数据更新03.07.xlsx"
- //var month, day string
- //path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新%s%s.xlsx"
- path := "/home/code/python/coal_mail/emailFile/沿海八省动力煤终端用户供耗存数据更新.xlsx"
- //for i := 0; i < 7; i++ {
- // dateDay := time.Now().AddDate(0, 0, -i).Day()
- // if dateDay < 10 {
- // day = strconv.Itoa(dateDay)
- // day = "0" + day
- // } else {
- // day = strconv.Itoa(dateDay)
- // }
- // dateMonth := int(time.Now().AddDate(0, 0, -i).Month())
- // if dateMonth < 10 {
- // month = strconv.Itoa(dateMonth)
- // month = "0" + month
- // }else {
- // month = strconv.Itoa(dateMonth)
- // }
- // npath := fmt.Sprintf(path, month, day)
- // fmt.Println(npath)
- // exist, err := PathExists(npath)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- // if exist {
- // path = npath
- // break
- // }
- //}
- var xlFile *xlsx.File
- exist, err := PathExists(path)
- if err != nil {
- fmt.Println(err)
- return
- }
- if exist {
- xlFile, err = xlsx.OpenFile(path)
- if err != nil {
- fmt.Println("OpenFile err:", err)
- return
- }
- } else {
- return
- }
- var mappingItems []*models.BaseFromCoalmineMapping
- var indexItems []*models.BaseFromCoalmineCoastalIndex
- var codeMap = make(map[string]string)
- var indexMap = make(map[string]string)
- var nameMap = make(map[int]string)
- var groupMap = make(map[int]string)
- var dataTime string
- var sheetName, unit string
- codeList, err := models.GetBaseFromCoalmineMapping()
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Info("获取煤炭指标失败:", err)
- return
- }
- if len(codeList) > 0 {
- for _, v := range codeList {
- codeMap[v.IndexName] = v.IndexCode
- }
- }
- indexCompanyList, err := models.GetBaseFromCoalmineCoastalIndex()
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Info("获取煤炭公司指标失败:", err)
- return
- }
- if len(indexCompanyList) > 0 {
- for _, v := range indexCompanyList {
- indexMap[v.IndexName+v.DataTime] = v.DealValue
- }
- }
- for _, sheet := range xlFile.Sheets {
- sheetName = sheet.Name
- //遍历行读取
- maxCol := sheet.MaxCol
- for i := 0; i < maxCol; i++ {
- if i == 0 {
- row := sheet.Row(i)
- cells := row.Cells
- for k, cell := range cells {
- if k > 0 && k < 9 {
- text := cell.String()
- groupMap[k] = text
- var item models.BaseFromCoalmineMapping
- //合计命名
- if k == 9 {
- item.IndexName = sheetName
- }
- item.IndexName = text + sheetName
- nameMap[k] = item.IndexName
- //code
- exists := ContainsSpecialName(text)
- var code string
- if exists {
- abbr := trimProvinceName(text)
- //取处理后公司名首字母缩写
- a := pinyin.NewArgs()
- rows := pinyin.Pinyin(sheetName, a)
- for i := 0; i < len(rows); i++ {
- if len(rows[i]) != 0 {
- str := rows[i][0]
- pi := str[0:1]
- code += pi
- }
- }
- item.IndexCode = abbr + code + "coastal"
- } else {
- a := pinyin.NewArgs()
- rows := pinyin.Pinyin(text+sheetName, a)
- for i := 0; i < len(rows); i++ {
- if len(rows[i]) != 0 {
- str := rows[i][0]
- pi := str[0:1]
- code += pi
- }
- }
- item.IndexCode = code + "coastal"
- }
- item.CreateTime = time.Now()
- mappingItems = append(mappingItems, &item)
- }
- if k == 10 {
- unit = strings.Replace(cell.String(), "单位:", "", -1)
- }
- }
- }
- if i > 0 {
- row := sheet.Row(i)
- cells := row.Cells
- for k, cell := range cells {
- if k < 9 {
- var item models.BaseFromCoalmineCoastalIndex
- if k == 0 {
- text := cell.String()
- dataTime = ExcelDateToDate(text).Format(utils.FormatDate)
- }
- if k > 0 {
- text := cell.String()
- item.IndexName = nameMap[k]
- item.IndexCode = codeMap[nameMap[k]]
- item.DealValue = text
- item.GroupName = groupMap[k]
- item.Source = "沿海八省"
- item.DataTime = dataTime
- item.Frequency = "日度"
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- }
- item.Unit = unit
- indexItems = append(indexItems, &item)
- }
- }
- }
- }
- }
- //添加数据到数据库
- for _, v := range mappingItems {
- if codeMap[v.IndexName] == "" {
- codeMap[v.IndexName] = v.IndexCode
- newId, err := models.AddBaseFromCoalmineMapping(v)
- if err != nil {
- for i := 0; i < 10; i++ {
- v.IndexCode = v.IndexCode + strconv.Itoa(i)
- codeMap[v.IndexName] = v.IndexCode
- newId, err := models.AddBaseFromCoalmineMapping(v)
- if err != nil {
- fmt.Println("再次添加公司指标名称错误", err)
- continue
- } else {
- fmt.Println("新增公司成功", newId)
- break
- }
- }
- } else {
- fmt.Println("新增公司成功", newId)
- }
- }
- }
- fmt.Println("指标操作完成")
- //给indexItem中的code赋值并插入index表
- for _, v := range indexItems {
- v.IndexCode = codeMap[v.IndexName]
- if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
- newId, err := models.AddBaseFromCoalCoastalIndex(v)
- if err != nil {
- fmt.Println("添加数据错误", err)
- } else {
- fmt.Println("新增成功", newId)
- }
- } else {
- if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
- err = models.UpdateBaseFromCoalCoastalIndex(v)
- if err != nil {
- fmt.Println("修改数据错误错误", err)
- return
- }
- }
- }
- }
- return
- }
- func ttrimProvinceName(name string) string {
- name = strings.Replace(name, "陕西省", "sn", -1)
- name = strings.Replace(name, "陕西", "sn", -1)
- name = strings.Replace(name, "海南市", "hi", -1)
- name = strings.Replace(name, "海南", "hi", -1)
- name = strings.Replace(name, "河南省", "ha", -1)
- name = strings.Replace(name, "河南", "ha", -1)
- name = strings.Replace(name, "河北省", "he", -1)
- name = strings.Replace(name, "河北", "he", -1)
- name = strings.Replace(name, "澳门", "mo", -1)
- name = strings.Replace(name, "内蒙古自治区", "nm", -1)
- name = strings.Replace(name, "内蒙古", "nm", -1)
- name = strings.Replace(name, "黑龙江", "hl", -1)
- name = strings.Replace(name, "(", "", -1)
- name = strings.Replace(name, ")", "", -1)
- name = strings.Replace(name, "+", "", -1)
- return name
- }
- func ExcelDateToDate(excelDate string) time.Time {
- excelTime := time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
- var days, _ = strconv.Atoi(excelDate)
- return excelTime.Add(time.Second * time.Duration(days*86400))
- }
|