package data import ( "encoding/json" "errors" "eta/eta_api/models/data_manage" "eta/eta_api/models/system" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/http" "strings" "time" ) // AddMysteelChemicalClassify 添加钢联化工分类 func AddMysteelChemicalClassify(classifyName string, parentId, level, sysUserId int, sysUserName string) (classifyInfo *data_manage.BaseFromMysteelChemicalClassify, err error, errMsg string) { count, err := data_manage.GetBaseFromMysteelChemicalClassifyCount(classifyName, parentId) if err != nil { errMsg = `判断名称是否已存在失败` return } if count > 0 { errMsg = `分类名称:`+classifyName+` 已存在,请重新输入` return } //获取该层级下最大的排序数 maxSort, err := data_manage.GetBaseFromMysteelChemicalClassifyMaxSort(parentId) classifyInfo = &data_manage.BaseFromMysteelChemicalClassify{ //BaseFromMysteelChemicalClassifyId: 0, ClassifyName: classifyName, ParentId: parentId, SysUserId: sysUserId, SysUserRealName: sysUserName, Sort: maxSort + 1, Level: level + 1, ModifyTime: time.Now(), CreateTime: time.Now(), } classifyId, err := data_manage.AddBaseFromMysteelChemicalClassify(classifyInfo) if err != nil { errMsg = "保存分类失败" } classifyInfo.BaseFromMysteelChemicalClassifyId = int(classifyId) return } // EditMysteelChemicalClassify 编辑钢联化工分类 func EditMysteelChemicalClassify(classifyId int, classifyName string, sysUser *system.Admin) (err error, errMsg string) { item, err := data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId) if err != nil { errMsg = `保存失败` return } // 操作权限校验 button := GetMysteelChemicalOpButton(sysUser, item.SysUserId) if !button.OpButton { errMsg = "无权限操作" return } // 名字相同,那么就直接返回 if item.ClassifyName == classifyName { return } // 判断名称是否已存在 count, err := data_manage.GetBaseFromMysteelChemicalClassifyCount(classifyName, item.ParentId) if err != nil { errMsg = "判断名称是否已存在失败" return } if count > 0 { errMsg = "分类名称已存在,请重新输入" err = errors.New(errMsg) return } // 修改数据 err = data_manage.EditBaseFromMysteelChemicalClassify(classifyId, classifyName) if err != nil { errMsg = "保存失败" } return } // MoveMysteelChemicalClassify 移动钢联化工分类 func MoveMysteelChemicalClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int, sysUser *system.Admin) (err error, errMsg string) { //判断分类是否存在 classifyInfo, err := data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取分类信息失败,Err:" + err.Error()) return } // 操作权限校验 button := GetMysteelChemicalOpButton(sysUser, classifyInfo.SysUserId) if !button.MoveButton { errMsg = "无权限操作" return } updateCol := make([]string, 0) //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级 if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 { parentClassifyInfo, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyById(parentClassifyId) if tmpErr != nil { errMsg = "移动失败" err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error()) return } classifyInfo.ParentId = parentClassifyInfo.BaseFromMysteelChemicalClassifyId classifyInfo.Level = parentClassifyInfo.Level + 1 classifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "ParentId", "Level", "ModifyTime") } //如果有传入 上一个兄弟节点分类id if prevClassifyId > 0 { prevClassify, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyById(prevClassifyId) if tmpErr != nil { errMsg = "移动失败" err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + tmpErr.Error()) return } //如果是移动在两个兄弟节点之间 if nextClassifyId > 0 { //下一个兄弟节点 nextClassify, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyById(nextClassifyId) if tmpErr != nil { errMsg = "移动失败" err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + tmpErr.Error()) return } //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1 if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == classifyInfo.Sort { //变更兄弟节点的排序 updateSortStr := `sort + 2` _ = data_manage.UpdateBaseFromMysteelChemicalClassifySortByClassifyId(parentClassifyId, prevClassify.BaseFromMysteelChemicalClassifyId, prevClassify.Sort, updateSortStr) } else { //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了 if nextClassify.Sort-prevClassify.Sort == 1 { //变更兄弟节点的排序 updateSortStr := `sort + 1` _ = data_manage.UpdateBaseFromMysteelChemicalClassifySortByClassifyId(parentClassifyId, 0, prevClassify.Sort, updateSortStr) } } } classifyInfo.Sort = prevClassify.Sort + 1 classifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else { firstClassify, tmpErr := data_manage.GetFirstBaseFromMysteelChemicalClassify() if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() { errMsg = "移动失败" err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error()) return } //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序 if firstClassify != nil && firstClassify.Sort == 0 { updateSortStr := ` sort + 1 ` _ = data_manage.UpdateBaseFromMysteelChemicalClassifySortByClassifyId(parentClassifyId, firstClassify.BaseFromMysteelChemicalClassifyId-1, 0, updateSortStr) } classifyInfo.Sort = 0 //那就是排在第一位 classifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } //更新 if len(updateCol) > 0 { err = classifyInfo.Update(updateCol) if err != nil { errMsg = "移动失败" err = errors.New("修改失败,Err:" + err.Error()) return } } return } // DelMysteelChemicalClassify 删除钢联化工分类 func DelMysteelChemicalClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) { //判断分类是否存在 classifyInfo, err := data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId) if err != nil { errMsg = "删除失败" err = errors.New("获取分类信息失败,Err:" + err.Error()) return } // 操作权限校验 button := GetMysteelChemicalOpButton(sysUser, classifyInfo.SysUserId) if !button.DeleteButton { errMsg = "无权限操作" return } // 需要删除的分类id集合 classifyIdList := make([]int, 0) classifyIdList = append(classifyIdList, classifyId) mysteelChemicalIndexIdList := make([]int, 0) indexCodeList := make([]string, 0) // 查找所有的子集id if classifyInfo.ParentId <= 0 { childrenClassifyInfoList, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyByParentId(classifyId) if tmpErr != nil { errMsg = "删除失败" err = errors.New("获取分类信息失败,Err:" + tmpErr.Error()) return } for _, v := range childrenClassifyInfoList { classifyIdList = append(classifyIdList, v.BaseFromMysteelChemicalClassifyId) } } // 获取分类下的所有指标 baseFromMysteelChemicalIndexList, err := data_manage.GetBaseFromMysteelChemicalIndexListByClassifyIdList(classifyIdList) if err != nil { errMsg = "删除失败" err = errors.New("获取分类下的指标信息失败,Err:" + err.Error()) return } for _, v := range baseFromMysteelChemicalIndexList { mysteelChemicalIndexIdList = append(mysteelChemicalIndexIdList, v.BaseFromMysteelChemicalIndexId) indexCodeList = append(indexCodeList, v.IndexCode) } // 获取已经加入到EDB指标库的钢联化工指标 edbInfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexCodeList) if err != nil { errMsg = "删除失败" err = errors.New("获取分类下的指标信息失败,Err:" + err.Error()) return } if len(edbInfoList) > 0 { errMsg = "该分类下存在指标加入到ETA图库,无法删除" return } // 删除对应的分类和指标 err = data_manage.DeleteMysteelChemicalByClassifyId(classifyIdList, mysteelChemicalIndexIdList) if err != nil { return } // 删除指标服务的excel表 go func(list []*data_manage.BaseFromMysteelChemicalIndex) { for _, v := range list { CurlDelMysteelChemicalData(v.IndexCode, v.UpdateWeek) } }(baseFromMysteelChemicalIndexList) return } // AddMysteelChemicalIndex 添加钢联化工指标 func AddMysteelChemicalIndex(classifyId int, indexCode, updateWeek, updateTimeStr string, sysUserId int, sysUserName string) (baseFromMysteelChemicalIndex *data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) { baseFromMysteelChemicalIndex, err = data_manage.GetBaseFromMysteelChemicalIndexByCode(indexCode) if err != nil && err.Error() != utils.ErrNoRow() { errMsg = `获取数据失败` return } if baseFromMysteelChemicalIndex != nil { errMsg = `指标ID:`+indexCode+` 已存在,请重新输入` return } // 更新时间切割 var updateTime1, updateTime2 string if updateTimeStr != `` { updateTimeList := strings.Split(updateTimeStr, ",") updateTime1 = updateTimeList[0] if len(updateTimeList) > 1 { updateTime2 = updateTimeList[1] } } edbTerminal := getEdbTerminal(utils.DATA_SOURCE_MYSTEEL_CHEMICAL) if edbTerminal == nil { errMsg = `钢联终端信息未配置` err = errors.New(errMsg) return } baseFromMysteelChemicalIndex = &data_manage.BaseFromMysteelChemicalIndex{ //BaseFromMysteelChemicalIndexId: 0, BaseFromMysteelChemicalClassifyId: classifyId, IndexCode: indexCode, IndexName: "", Unit: "", Source: "", Frequency: "", StartDate: "", EndDate: "", Describe: "", UpdateWeek: updateWeek, UpdateTime: updateTime1, UpdateTime2: updateTime2, SysUserId: sysUserId, SysUserRealName: sysUserName, ModifyTime: time.Now(), CreateTime: time.Now(), TerminalCode: edbTerminal.TerminalCode, // 终端编码 } _, err = data_manage.AddBaseFromMysteelChemicalIndex(baseFromMysteelChemicalIndex) if err != nil { errMsg = "保存分类失败" } return } // EditMysteelChemicalIndex 编辑钢联化工指标 func EditMysteelChemicalIndex(indexId, classifyId int, updateWeek, updateTimeStr string, sysUser *system.Admin) (baseFromMysteelChemicalIndex *data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) { baseFromMysteelChemicalIndex, err = data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId) if err != nil { errMsg = `获取数据失败` if err.Error() == utils.ErrNoRow() { errMsg = `该指标未入库` err = nil } return } // 操作权限校验 button := GetMysteelChemicalOpButton(sysUser, baseFromMysteelChemicalIndex.SysUserId) if !button.OpButton { errMsg = "无权限操作" return } oldUpdateWeek := baseFromMysteelChemicalIndex.UpdateWeek // 更新时间切割 var updateTime1, updateTime2 string if updateTimeStr != `` { updateTimeList := strings.Split(updateTimeStr, ",") updateTime1 = updateTimeList[0] if len(updateTimeList) > 1 { updateTime2 = updateTimeList[1] } } baseFromMysteelChemicalIndex.BaseFromMysteelChemicalClassifyId = classifyId baseFromMysteelChemicalIndex.UpdateWeek = updateWeek baseFromMysteelChemicalIndex.UpdateTime = updateTime1 baseFromMysteelChemicalIndex.UpdateTime2 = updateTime2 baseFromMysteelChemicalIndex.ModifyTime = time.Now() err = baseFromMysteelChemicalIndex.Update([]string{"BaseFromMysteelChemicalClassifyId", "UpdateWeek", "UpdateTime", "UpdateTime2", "ModifyTime"}) if err != nil { errMsg = "保存分类失败" } go func(oldUpdateWeek string, indexInfo *data_manage.BaseFromMysteelChemicalIndex) { // 先删除excel表 CurlDelMysteelChemicalData(indexInfo.IndexCode, oldUpdateWeek) }(oldUpdateWeek, baseFromMysteelChemicalIndex) return } // DelMysteelChemical 删除钢联化工指标 func DelMysteelChemical(indexId int, sysUser *system.Admin) (err error, errMsg string) { baseFromMysteelChemicalIndex, err := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId) if err != nil { errMsg = `获取数据失败` if err.Error() == utils.ErrNoRow() { errMsg = `该指标未入库` err = nil } return } // 操作权限校验 button := GetMysteelChemicalOpButton(sysUser, baseFromMysteelChemicalIndex.SysUserId) if !button.DeleteButton { errMsg = "无权限操作" return } // 获取已经加入到EDB指标库的钢联化工指标 edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, baseFromMysteelChemicalIndex.IndexCode) if err != nil && err.Error() != utils.ErrNoRow() { errMsg = "删除失败" err = errors.New("获取分类下的指标信息失败,Err:" + err.Error()) return } if edbInfo != nil { errMsg = "当前指标已被引用,不可删除" err = nil return } // 删除对应的分类和指标 err = data_manage.DeleteMysteelChemical(indexId) if err != nil { return } // 删除指标服务的excel表 go CurlDelMysteelChemicalData(baseFromMysteelChemicalIndex.IndexCode, baseFromMysteelChemicalIndex.UpdateWeek) return } // MoveMysteelChemical 移动钢联化工指标 func MoveMysteelChemical(indexId, classifyId, prevIndexId, nextIndexId int, sysUser *system.Admin) (err error, errMsg string) { //分类信息 baseFromMysteelChemicalIndex, err := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId) if err != nil { errMsg = `获取数据失败` if err.Error() == utils.ErrNoRow() { errMsg = `该指标未入库` err = nil } return } // 操作权限校验 button := GetMysteelChemicalOpButton(sysUser, baseFromMysteelChemicalIndex.SysUserId) if !button.MoveButton { errMsg = "无权限操作" return } //判断分类是否存在 _, err = data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取分类信息失败,Err:" + err.Error()) if err.Error() == utils.ErrNoRow() { errMsg = "找不到该分类" err = nil } return } //如果改变了分类,那么移动该图表数据 if baseFromMysteelChemicalIndex.BaseFromMysteelChemicalClassifyId != classifyId { tmpErr := data_manage.MoveBaseFromMysteelChemicalIndex(indexId, classifyId) if err != nil { errMsg = "移动失败" err = errors.New("移动失败,Err:" + tmpErr.Error()) return } } //移动排序 updateCol := make([]string, 0) //如果有传入 上一个兄弟节点分类id if prevIndexId > 0 { prevIndex, tmpErr := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(prevIndexId) if err != nil { errMsg = "移动失败" err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error()) return } //如果是移动在两个兄弟节点之间 if nextIndexId > 0 { //下一个兄弟节点 nextIndex, tmpErr := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(nextIndexId) if err != nil { errMsg = "移动失败" err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error()) return } //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1 if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == baseFromMysteelChemicalIndex.Sort { //变更兄弟节点的排序 updateSortStr := `sort + 2` _ = data_manage.UpdateBaseFromMysteelChemicalIndexByClassifyId(prevIndex.BaseFromMysteelChemicalClassifyId, prevIndex.Sort, prevIndex.BaseFromMysteelChemicalIndexId, updateSortStr) } else { //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了 if nextIndex.Sort-prevIndex.Sort == 1 { //变更兄弟节点的排序 updateSortStr := `sort + 1` _ = data_manage.UpdateBaseFromMysteelChemicalIndexByClassifyId(prevIndex.BaseFromMysteelChemicalClassifyId, prevIndex.Sort, prevIndex.BaseFromMysteelChemicalIndexId, updateSortStr) } } } baseFromMysteelChemicalIndex.Sort = prevIndex.Sort + 1 baseFromMysteelChemicalIndex.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else { firstClassify, tmpErr := data_manage.GetFirstBaseFromMysteelChemicalIndexByClassifyId(classifyId) if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() { errMsg = "移动失败" err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error()) return } //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序 if firstClassify != nil && firstClassify.Sort == 0 { updateSortStr := ` sort + 1 ` _ = data_manage.UpdateBaseFromMysteelChemicalIndexByClassifyId(firstClassify.BaseFromMysteelChemicalClassifyId, 0, firstClassify.BaseFromMysteelChemicalIndexId-1, updateSortStr) } baseFromMysteelChemicalIndex.Sort = 0 //那就是排在第一位 baseFromMysteelChemicalIndex.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } //更新 if len(updateCol) > 0 { err = baseFromMysteelChemicalIndex.Update(updateCol) if err != nil { errMsg = "移动失败" err = errors.New("修改失败,Err:" + err.Error()) return } } if err != nil { errMsg = "移动失败" err = errors.New("修改失败,Err:" + err.Error()) return } return } // GetMysteelChemicalOpButton 获取钢联化工的操作权限 func GetMysteelChemicalOpButton(sysUser *system.Admin, belongUserId int) (button data_manage.BaseFromMysteelChemicalClassifyItemsButton) { //2、用户对于自己添加的分类,有权限编辑、移动和删除该分类; //3、ficc管理员和超管对所有分类有编辑、移动和删除权限; if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.MysteelChemicalPermission == 1 { button.AddButton = true button.OpButton = true button.DeleteButton = true button.MoveButton = true } return } // CurlDelMysteelChemicalData 请求钢联服务器删除指标 func CurlDelMysteelChemicalData(indexCode, updateWeek string) { postUrl := utils.MySteelPostUrl + `/api/index/delete` //type IndexAddReq struct { // IndexCode string `json:"IndexCode" binding:"required"` //指标编码 // UpdateWeek string `json:"UpdateWeek"` //更新周期 // RunMode string `description:"运行环境:debug:测试(默认),release:生产" json:"UpdateWeek"` //更新周期 //} paramsMap := make(map[string]interface{}) updateWeek = WeekMap[updateWeek] paramsMap["IndexCode"] = indexCode paramsMap["UpdateWeek"] = updateWeek paramsMap["RunMode"] = utils.RunMode paramJson, err := json.Marshal(paramsMap) if err != nil { fmt.Println("param json.Marshal Err:" + err.Error()) err = errors.New("param json.Marshal Err:" + err.Error()) return } utils.FileLog.Info(fmt.Sprintf("CurlDelMysteelChemicalData parms:%s", string(paramJson))) result, err := http.Post(postUrl, string(paramJson), "application/json") if err != nil { fmt.Println("post err:" + err.Error()) err = errors.New("post Err:" + err.Error()) return } utils.FileLog.Info("CurlDelMysteelChemicalData Result:%s", string(result)) } // WeekMap 中英文日期转换 var WeekMap = map[string]string{ "": "", "周一": "monday", "周二": "tuesday", "周三": "wednesday", "周四": "thursday", "周五": "friday", "周六": "saturday", "周日": "sunday", } // CurlRefreshMysteelChemicalData 请求钢联服务器添加指标 func CurlRefreshMysteelChemicalData(mergeFilePath string) { postUrl := utils.MySteelPostUrl + `/api/index/refresh` //type IndexAddReq struct { // IndexCode string `json:"IndexCode" binding:"required"` //指标编码 // UpdateWeek string `json:"UpdateWeek"` //更新周期 // RunMode string `description:"运行环境:debug:测试(默认),release:生产" json:"UpdateWeek"` //更新周期 //} paramsMap := make(map[string]interface{}) paramsMap["MergeFilePath"] = mergeFilePath paramJson, err := json.Marshal(paramsMap) if err != nil { fmt.Println("param json.Marshal Err:" + err.Error()) err = errors.New("param json.Marshal Err:" + err.Error()) return } utils.FileLog.Info(fmt.Sprintf("CurlRefreshMysteelChemicalData parms:%s", string(paramJson))) result, err := http.Post(postUrl, string(paramJson), "application/json") if err != nil { fmt.Println("post err:" + err.Error()) err = errors.New("post Err:" + err.Error()) return } utils.FileLog.Info("CurlRefreshMysteelChemicalData Result:%s", string(result)) } // getEdbTerminal 根据指标来源获取可用指标终端信息 func getEdbTerminal(source int) (edbTerminal *data_manage.EdbTerminal) { terminalNumList, err := data_manage.GetMysteelChemicalGroupTerminalNum() if err != nil { return } terminalNumMap := make(map[string]int) for _, v := range terminalNumList { terminalNumMap[v.TerminalCode] = v.Num } terminalList, err := data_manage.GetEdbTerminalListBySource(source) if err != nil { return } num := 0 for _, v := range terminalList { tmpNum := terminalNumMap[v.TerminalCode] if edbTerminal == nil { edbTerminal = v num = tmpNum } else if tmpNum < num { edbTerminal = v num = tmpNum } } return }