123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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"
- "time"
- )
- func FileCoalInland() () {
- var err error
- defer func() {
- if err != nil {
- fmt.Println("RefreshDataFromCoalInland Err:" + err.Error())
- msg := "失败提醒" + "RefreshDataFromCoalInland ErrMsg:" + err.Error()
- go alarm_msg.SendAlarmMsg(msg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalInland ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- //path := "/Users/xi/Desktop/内陆17省动力煤终端用户供耗存%s.xlsx"
- //path := "/home/code/python/coal_mail/emailFile/内陆17省动力煤终端用户供耗存%s.xlsx"
- //for i := 0;i < 7;i++{
- // npath := fmt.Sprintf(path,time.Now().AddDate(0,0,-i).Format(utils.FormatDateUnSpace))
- // fmt.Println(npath)
- // exist,err := PathExists(npath)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- // if exist{
- // path = npath
- // break
- // }
- // if i < 3 && !exist{
- // npath := "/home/code/python/coal_mail/emailFile/内陆17省动力煤终端用户供耗存.xlsx"
- // //npath := "/Users/xi/Desktop/内陆17省动力煤终端用户供耗存.xlsx"
- // fmt.Println(npath)
- // exist,err := PathExists(npath)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- // if exist{
- // path = npath
- // break
- // }
- // }
- //}
- //exist,err := PathExists(path)
- //if err != nil {
- // fmt.Println(err)
- // return
- //}
- //if !exist{
- // path = "/home/code/python/coal_mail/emailFile/内陆17省动力煤终端用户供耗存.xlsx"
- //}
- path := "/home/code/python/coal_mail/emailFile/内陆17省动力煤终端用户供耗存.xlsx"
- exist, err := PathExists(path)
- if err != nil {
- fmt.Println(err)
- return
- }
- var xlFile *xlsx.File
- 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.BaseFromCoalmineInlandIndex
- 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 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.GetBaseFromCoalmineInlandIndex()
- 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
- fmt.Println("sheetName:", sheetName)
- var unit string
- if sheetName == "可用天数" {
- unit = "天"
- } else {
- unit = "万吨"
- }
- //遍历行读取
- 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 < 18 {
- 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 + "inland"
- } 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 + "inland"
- }
- item.CreateTime = time.Now()
- mappingItems = append(mappingItems, &item)
- }
- }
- }
- if i > 0 {
- row := sheet.Row(i)
- cells := row.Cells
- for k, cell := range cells {
- if k < 18 {
- var item models.BaseFromCoalmineInlandIndex
- if k == 0 {
- text := cell.String()
- t, _ := time.ParseInLocation("01-02-06", text, time.Local)
- dataTime = t.Format(utils.FormatDate)
- if len(text) < 6 {
- 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.Unit = unit
- item.Frequency = "日度"
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- }
- 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.AddBaseFromCoalInlandIndex(v)
- if err != nil {
- fmt.Println("添加数据错误", err)
- } else {
- fmt.Println("新增成功", newId)
- }
- } else {
- if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
- err = models.UpdateBaseFromCoalInlandIndex(v)
- if err != nil {
- fmt.Println("修改数据错误错误", err)
- return
- }
- }
- }
- }
- return
- }
|