123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package services
- import (
- "encoding/json"
- "eta/eta_data_init/models"
- "eta/eta_data_init/utils"
- "fmt"
- "github.com/xuri/excelize/v2"
- "os"
- "path/filepath"
- "strings"
- )
- // InitMysteelChemicalIndex
- // 初始化钢联化工指标数据(添加到数据源)
- func InitMysteelChemicalIndex(filePath string, isApi bool) {
- var err error
- defer func() {
- if err != nil {
- fmt.Println("InitBaseIndexData Err:" + err.Error())
- }
- }()
- //读取excel
- path, err := filepath.Abs(os.Args[0])
- if err != nil {
- fmt.Println(err)
- }
- dir := filepath.Dir(path)
- fmt.Println("dir:" + dir)
- dataPath := dir + filePath
- //dataPath = "./docs/gl_api_test.xlsx"
- fmt.Println("dataPath:" + dataPath)
- f, err := excelize.OpenFile(dataPath)
- if err != nil {
- fmt.Println(err)
- return
- }
- defer func() {
- // Close the spreadsheet.
- if err := f.Close(); err != nil {
- fmt.Println(err)
- }
- }()
- rows, err := f.GetRows("Sheet1")
- if err != nil {
- fmt.Println(err)
- return
- }
- classifyMethod := "mysteel_chemical/mysteel_classify/get_or_add"
- indexMethod := "mysteel_chemical/add/mysteel/index"
- apiHandleMethod := "mysteel_chemical/handle/api/mysteel/index"
- fmt.Println("rows len:", len(rows))
- for rk, row := range rows {
- if rk > 0 {
- var classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source, terminalCode string
- for ck, colCell := range row {
- switch ck {
- case 0:
- classifyFirst = colCell
- case 1:
- classifySecond = colCell
- case 2:
- classifyThree = colCell
- case 3:
- indexCode = colCell
- case 4:
- indexName = colCell
- case 5:
- frequency = colCell
- case 6:
- unit = colCell
- case 7:
- source = colCell
- case 8:
- terminalCode = colCell //终端号
- }
- }
- if classifySecond != "" &&
- classifyThree != "" &&
- indexCode != "" &&
- indexName != "" &&
- terminalCode != `` {
- //classifyFirstMap := make(map[string]interface{})
- //classifyFirstMap["ClassifyName"] = classifyFirst
- //classifyFirstMap["ParentId"] = 0
- //classifyFirstMap["Level"] = 0
- //result, err := PostEdbLib(classifyFirstMap, method)
- //if err != nil {
- // utils.FileLog.Info("初始化分类1失败:" + err.Error())
- // return
- //}
- //
- //resp := new(models.ClassifyResp)
- //err = json.Unmarshal(result, &resp)
- //if err != nil {
- // utils.FileLog.Info("初始化分类1失败:" + err.Error())
- // return
- //}
- //
- //if resp.Ret != 200 {
- // utils.FileLog.Info("初始化分类1失败:" + resp.Msg + ";" + resp.ErrMsg)
- // return
- //}
- // 如果频度只有年月日这种,并没有度,那么需要补充
- if !strings.Contains(frequency, "度") {
- frequency = frequency + "度"
- }
- classifySecondMap := make(map[string]interface{})
- classifySecondMap["ClassifyName"] = classifySecond
- classifySecondMap["ParentId"] = 0
- classifySecondMap["Level"] = 0
- classifySecondMap["SysUserId"] = utils.OpUserId
- classifySecondMap["SysUserRealName"] = utils.OpUserRealName
- result, err := PostEdbLib(classifySecondMap, classifyMethod)
- if err != nil {
- utils.FileLog.Info("初始化分类2失败:" + err.Error())
- return
- }
- resp := new(models.MysteelClassifyResp)
- err = json.Unmarshal(result, &resp)
- if err != nil {
- utils.FileLog.Info("初始化分类2失败:" + err.Error())
- return
- }
- if resp.Ret != 200 {
- utils.FileLog.Info("初始化分类2失败:" + resp.Msg + ";" + resp.ErrMsg)
- return
- }
- classifyThreeMap := make(map[string]interface{})
- classifyThreeMap["ClassifyName"] = classifyThree
- classifyThreeMap["ParentId"] = resp.Data.BaseFromMysteelChemicalClassifyId
- classifyThreeMap["Level"] = 1
- classifyThreeMap["SysUserId"] = utils.OpUserId
- classifyThreeMap["SysUserRealName"] = utils.OpUserRealName
- result, err = PostEdbLib(classifyThreeMap, classifyMethod)
- if err != nil {
- utils.FileLog.Info("初始化分类3失败:" + err.Error())
- return
- }
- resp = new(models.MysteelClassifyResp)
- err = json.Unmarshal(result, &resp)
- if err != nil {
- utils.FileLog.Info("初始化分类3失败:" + err.Error())
- return
- }
- if resp.Ret != 200 {
- utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
- return
- }
- indexMap := make(map[string]interface{})
- indexMap["EdbCode"] = indexCode
- indexMap["TerminalCode"] = terminalCode
- indexMap["BaseFromMysteelChemicalClassifyId"] = resp.Data.BaseFromMysteelChemicalClassifyId
- indexMap["SysUserId"] = utils.OpUserId
- indexMap["SysUserRealName"] = utils.OpUserRealName
- result, err = PostEdbLib(indexMap, indexMethod)
- if err != nil {
- utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
- return
- }
- indexResp := new(models.EdbInfoResp)
- err = json.Unmarshal(result, &indexResp)
- if err != nil {
- utils.FileLog.Info("初始化分类2失败:" + err.Error())
- return
- }
- if indexResp.Ret != 200 {
- if strings.Contains(indexResp.Msg, "新增指标失败") {
- continue
- } else {
- fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
- utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
- return
- }
- }
- fmt.Println("add index success:" + indexCode)
- // 请求API初始化数据
- if !isApi {
- continue
- }
- fmt.Println("请求API刷新数据")
- handleReq := make(map[string][]map[string]interface{})
- handleMap := make(map[string]interface{})
- handleMap["IndexCode"] = indexCode
- handleReq["List"] = append(handleReq["List"], handleMap)
- res, e := PostHandleMysteelIndexEdbLib(handleReq, apiHandleMethod)
- if e != nil {
- tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib写入指标失败, code: %s, err: %v, result: %s", indexCode, e, string(res))
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- return
- }
- handleResp := new(models.EdbInfoResp)
- if e = json.Unmarshal(res, &handleResp); e != nil {
- tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib解析resp失败, code: %s, err: %v, result: %s", indexCode, e, string(res))
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- return
- }
- if handleResp.Ret != 200 {
- tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib写入指标失败, code: %s, Msg: %s, ErrMsg: %s", indexCode, handleResp.Msg, handleResp.ErrMsg)
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- return
- }
- fmt.Println("handle index success:" + indexCode)
- } else {
- fmt.Println("data is empty")
- fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
- }
- }
- }
- }
|