|
@@ -1145,3 +1145,274 @@ func (c *CustomAnalysisController) ShareDetail() {
|
|
|
br.Success = true
|
|
|
br.Msg = "获取成功"
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+type CustomAnalysisCommonController struct {
|
|
|
+ controllers.BaseCommonController
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (c *CustomAnalysisCommonController) FixClassify() {
|
|
|
+ br := new(models.BaseResponse).Init()
|
|
|
+ defer func() {
|
|
|
+ if br.ErrMsg != "" {
|
|
|
+ br.IsSendEmail = false
|
|
|
+ }
|
|
|
+ if br.Ret == 200 {
|
|
|
+
|
|
|
+ }
|
|
|
+ c.Data["json"] = br
|
|
|
+ c.ServeJSON()
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ groupExcels := make(map[int][]*excelModel.ExcelInfo)
|
|
|
+ for _, v := range excels {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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,
|
|
|
+ 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 {
|
|
|
+
|
|
|
+ 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 {
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ br.Ret = 200
|
|
|
+ br.Success = true
|
|
|
+ br.Msg = "操作成功"
|
|
|
+}
|