|
@@ -1145,3 +1145,274 @@ func (c *CustomAnalysisController) ShareDetail() {
|
|
|
br.Success = true
|
|
|
br.Msg = "获取成功"
|
|
|
}
|
|
|
+
|
|
|
+// CustomAnalysisCommonController 修复数据用的
|
|
|
+type CustomAnalysisCommonController struct {
|
|
|
+ controllers.BaseCommonController
|
|
|
+}
|
|
|
+
|
|
|
+//var OnceCustomAnalysisFixed = false
|
|
|
+
|
|
|
+// FixClassify
|
|
|
+// @Title 修复表格分类(一次性-后续版本可删掉)
|
|
|
+// @Description 修复表格分类
|
|
|
+// @Param ExcelInfoId query int true "表格ID"
|
|
|
+// @Success 200 string "操作成功"
|
|
|
+// @router /excel/fix_classify [get]
|
|
|
+func (c *CustomAnalysisCommonController) FixClassify() {
|
|
|
+ br := new(models.BaseResponse).Init()
|
|
|
+ defer func() {
|
|
|
+ if br.ErrMsg != "" {
|
|
|
+ br.IsSendEmail = false
|
|
|
+ }
|
|
|
+ if br.Ret == 200 {
|
|
|
+ //OnceCustomAnalysisFixed = true
|
|
|
+ }
|
|
|
+ c.Data["json"] = br
|
|
|
+ c.ServeJSON()
|
|
|
+ }()
|
|
|
+ //if OnceCustomAnalysisFixed {
|
|
|
+ // br.Ret = 200
|
|
|
+ // br.Success = true
|
|
|
+ // br.Msg = "请勿重复修复"
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ source := utils.CUSTOM_ANALYSIS_TABLE
|
|
|
+
|
|
|
+ // 获取所有分类
|
|
|
+ classifies, e := excelModel.GetExcelClassifyModelBySource(source)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ classifyMap := make(map[int]*excelModel.ExcelClassify)
|
|
|
+ for _, v := range classifies {
|
|
|
+ classifyMap[v.ExcelClassifyId] = v
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询所有excel
|
|
|
+ excels, e := excelModel.GetAllExcelInfoBySource(source)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("获取excel列表失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(excels) == 0 {
|
|
|
+ br.Ret = 200
|
|
|
+ br.Success = true
|
|
|
+ br.Msg = "无分类需要修复"
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 先按照用户ID分组
|
|
|
+ groupExcels := make(map[int][]*excelModel.ExcelInfo)
|
|
|
+ for _, v := range excels {
|
|
|
+ //if v.SysUserId != 198 {
|
|
|
+ // continue
|
|
|
+ //}
|
|
|
+ if groupExcels[v.SysUserId] == nil {
|
|
|
+ groupExcels[v.SysUserId] = make([]*excelModel.ExcelInfo, 0)
|
|
|
+ }
|
|
|
+ groupExcels[v.SysUserId] = append(groupExcels[v.SysUserId], v)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归函数
|
|
|
+ findAndReplace := func(replaceId map[int]*excelModel.ExcelClassify, userId int, excelItem *excelModel.ExcelInfo, classifyItem *excelModel.ExcelClassify, firstStep bool) (hasParent, updateChild bool, currentClassify *excelModel.ExcelClassify, err error) {
|
|
|
+ // 分类创建人与表格不一致
|
|
|
+ currentClassify = classifyItem
|
|
|
+ if classifyItem.SysUserId != userId {
|
|
|
+ // 是否已存在替换分类
|
|
|
+ _, ok := replaceId[classifyItem.ExcelClassifyId]
|
|
|
+ if !ok {
|
|
|
+ classifyNew := &excelModel.ExcelClassify{
|
|
|
+ ExcelClassifyName: fmt.Sprintf("%d%s", userId, classifyItem.ExcelClassifyName),
|
|
|
+ ParentId: classifyItem.ParentId, // 根据updateChild递归外更新
|
|
|
+ Source: source,
|
|
|
+ SysUserId: excelItem.SysUserId,
|
|
|
+ SysUserRealName: excelItem.SysUserRealName,
|
|
|
+ Level: classifyItem.Level,
|
|
|
+ UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
|
|
|
+ Sort: classifyItem.Sort,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ ModifyTime: time.Now(),
|
|
|
+ }
|
|
|
+ _, e = excelModel.AddExcelClassify(classifyNew)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("AddExcelClassify, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ replaceId[classifyItem.ExcelClassifyId] = classifyNew
|
|
|
+ excelItem.ExcelClassifyId = classifyNew.ExcelClassifyId
|
|
|
+ currentClassify = classifyNew
|
|
|
+ classifyMap[classifyNew.ExcelClassifyId] = classifyNew
|
|
|
+ } else {
|
|
|
+ excelItem.ExcelClassifyId = replaceId[classifyItem.ExcelClassifyId].ExcelClassifyId
|
|
|
+ currentClassify = replaceId[classifyItem.ExcelClassifyId]
|
|
|
+ }
|
|
|
+ // 仅第一次递归时修改
|
|
|
+ if firstStep {
|
|
|
+ //fmt.Printf("修改ExcelClassifyId: %d\n", excelItem.ExcelClassifyId)
|
|
|
+ excelItem.ModifyTime = time.Now().Local()
|
|
|
+ if e = excelItem.Update([]string{"ExcelClassifyId", "ModifyTime"}); e != nil {
|
|
|
+ err = fmt.Errorf("UpdateExcelInfo, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查上级
|
|
|
+ if classifyItem.ParentId <= 0 {
|
|
|
+ //fmt.Printf("终止递归, ClassifyId: %d", classifyItem.ExcelClassifyId)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ hasParent = true
|
|
|
+ parent := classifyMap[classifyItem.ParentId]
|
|
|
+ if parent == nil {
|
|
|
+ err = fmt.Errorf("未找到上级分类, ClassifyId: %d, ParentId: %d", classifyItem.ExcelClassifyId, classifyItem.ParentId)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if parent.SysUserId != userId {
|
|
|
+ updateChild = true
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for uid, gv := range groupExcels {
|
|
|
+
|
|
|
+ replaceId := make(map[int]*excelModel.ExcelClassify)
|
|
|
+ for _, v := range gv {
|
|
|
+ classifyItem, e := excelModel.GetExcelClassifyById(v.ExcelClassifyId)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("获取表格分类失败, ExcelClassifyId: %d, %v", v.ExcelClassifyId, e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 首次递归
|
|
|
+ hasParent, updateChild, currentClassify, e := findAndReplace(replaceId, uid, v, classifyItem, true)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("findAndReplace1, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !hasParent {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 二次递归
|
|
|
+ //fmt.Printf("二次递归: %d\n", currentClassify.ParentId)
|
|
|
+ parentClassify, e := excelModel.GetExcelClassifyById(currentClassify.ParentId)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("2-parent-GetExcelClassifyById, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ hasParent2, updateChild2, currentClassify2, e := findAndReplace(replaceId, uid, v, parentClassify, false)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("findAndReplace2, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 更新上次递归的parent_id
|
|
|
+ if updateChild {
|
|
|
+ currentClassify.ParentId = currentClassify2.ExcelClassifyId
|
|
|
+ currentClassify.ModifyTime = time.Now().Local()
|
|
|
+ if e = currentClassify.Update([]string{"ParentId", "ModifyTime"}); e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("2-更新父级分类ID失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !hasParent2 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 三次递归
|
|
|
+ //fmt.Printf("三次递归: %d\n", currentClassify2.ParentId)
|
|
|
+ parentClassify2, e := excelModel.GetExcelClassifyById(currentClassify2.ParentId)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("3-parent-GetExcelClassifyById, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ hasParent3, updateChild3, currentClassify3, e := findAndReplace(replaceId, uid, v, parentClassify2, false)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("findAndReplace3, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if updateChild2 {
|
|
|
+ currentClassify2.ParentId = currentClassify3.ExcelClassifyId
|
|
|
+ currentClassify2.ModifyTime = time.Now().Local()
|
|
|
+ if e = currentClassify2.Update([]string{"ParentId", "ModifyTime"}); e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("3-更新父级分类ID失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !hasParent3 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 四次递归
|
|
|
+ //fmt.Printf("四次递归: %d\n", currentClassify3.ParentId)
|
|
|
+ parentClassify3, e := excelModel.GetExcelClassifyById(currentClassify3.ParentId)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("4-parent-GetExcelClassifyById, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ hasParent4, updateChild4, currentClassify4, e := findAndReplace(replaceId, uid, v, parentClassify3, false)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("findAndReplace4, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if updateChild3 {
|
|
|
+ currentClassify3.ParentId = currentClassify4.ExcelClassifyId
|
|
|
+ currentClassify3.ModifyTime = time.Now().Local()
|
|
|
+ if e = currentClassify3.Update([]string{"ParentId", "ModifyTime"}); e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("4-更新父级分类ID失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !hasParent4 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 五次递归-理论上没了(自定义分析最多三级目录+最底层的表格)
|
|
|
+ //fmt.Printf("五次递归: %d\n", currentClassify4.ParentId)
|
|
|
+ parentClassify4, e := excelModel.GetExcelClassifyById(currentClassify4.ParentId)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("5-parent-GetExcelClassifyById, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ hasParent5, _, currentClassify5, e := findAndReplace(replaceId, uid, v, parentClassify4, false)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("findAndReplace5, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if updateChild4 {
|
|
|
+ currentClassify4.ParentId = currentClassify5.ExcelClassifyId
|
|
|
+ currentClassify4.ModifyTime = time.Now().Local()
|
|
|
+ if e = currentClassify4.Update([]string{"ParentId", "ModifyTime"}); e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = fmt.Sprintf("5-更新父级分类ID失败, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !hasParent5 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //fmt.Println("replaceId: ", replaceId)
|
|
|
+ }
|
|
|
+
|
|
|
+ br.Ret = 200
|
|
|
+ br.Success = true
|
|
|
+ br.Msg = "操作成功"
|
|
|
+}
|