123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422 |
- package excel
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- excelPermissionModel "eta/eta_api/models/data_manage/data_manage_permission"
- excelModel "eta/eta_api/models/data_manage/excel"
- "eta/eta_api/models/data_manage/excel/request"
- "eta/eta_api/models/data_manage/excel/response"
- "eta/eta_api/models/system"
- "eta/eta_api/services"
- "eta/eta_api/services/data/data_manage_permission"
- "eta/eta_api/services/data/excel"
- excel2 "eta/eta_api/services/data/excel"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- // CustomAnalysisController 自定义分析
- type CustomAnalysisController struct {
- controllers.BaseAuthController
- }
- // ExcelByName
- // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
- // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
- // @Param ExcelName query string true "搜索关键词"
- // @Success 200 {object} response.ExcelListResp
- // @router /excel_by_name [get]
- func (c *CustomAnalysisController) ExcelByName() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- excelName := c.GetString("ExcelName")
- if excelName == `` {
- br.Msg = "请选择表格"
- br.ErrMsg = "ExcelName未传"
- br.IsSendEmail = false
- return
- }
- resp := response.FindExcelInfoResp{}
- excelName = utils.TrimLRStr(excelName)
- // 获取数据详情
- excelDetail, err := excelModel.GetNoContentExcelInfoByName(excelName, utils.CUSTOM_ANALYSIS_TABLE)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- br.Msg = "获取表格事变"
- br.ErrMsg = err.Error()
- return
- }
- // 数据权限
- haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelDetail.ExcelInfoId, excelDetail.ExcelClassifyId, excelDetail.IsJoinPermission, c.SysUser.AdminId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
- return
- }
- resp.IsFind = true
- resp.ExcelInfo = response.FindExcelInfo{
- ExcelInfoId: excelDetail.ExcelInfoId,
- Source: excelDetail.Source,
- ExcelType: excelDetail.ExcelType,
- ExcelName: excelDetail.ExcelName,
- UniqueCode: excelDetail.UniqueCode,
- ExcelClassifyId: excelDetail.ExcelClassifyId,
- SysUserId: excelDetail.SysUserId,
- SysUserRealName: excelDetail.SysUserRealName,
- ExcelImage: excelDetail.ExcelImage,
- FileUrl: excelDetail.FileUrl,
- Sort: excelDetail.Sort,
- ModifyTime: excelDetail.ModifyTime,
- CreateTime: excelDetail.CreateTime,
- Button: excel.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, haveOperaAuth),
- HaveOperaAuth: true,
- }
- if excelDetail != nil {
- sheetList, err := excelModel.GetAllSheetItemList(excelDetail.ExcelInfoId)
- if err != nil {
- br.Msg = "获取sheet失败"
- br.ErrMsg = "获取sheet失败,err:" + err.Error()
- return
- }
- resp.SheetList = sheetList
- }
- //resp := response.ExcelListResp{
- // Paging: page,
- // List: list,
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Add
- // @Title 新增表格接口
- // @Description 新增表格接口
- // @Param request body request.AddExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /add [post]
- func (c *CustomAnalysisController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req request.AddExcelInfoReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.ExcelName = strings.Trim(req.ExcelName, " ")
- if req.ExcelName == "" {
- br.Msg = "请填写表格名称!"
- br.IsSendEmail = false
- return
- }
- // 获取是否存在该表格名称
- {
- var condition string
- var pars []interface{}
- condition += " AND source=? "
- pars = append(pars, utils.CUSTOM_ANALYSIS_TABLE)
- condition += " AND excel_name=? "
- pars = append(pars, req.ExcelName)
- count, err := excelModel.GetExcelInfoCountByCondition(condition, pars)
- if err != nil {
- br.Msg = "判断表格名称是否存在失败"
- br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
- return
- }
- if count > 0 {
- br.Msg = "表格名称已存在,请重新填写表格名称"
- br.IsSendEmail = false
- return
- }
- }
- if req.ExcelClassifyId <= 0 {
- br.Msg = "分类参数错误!"
- br.IsSendEmail = false
- return
- }
- excelInfo, err, errMsg, isSendEmail := excel.AddCustomAnalysisTable(utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, sysUser)
- if err != nil {
- br.Msg = "保存失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "保存失败,Err:" + err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
- //if req.Source == 1 {
- // go UpdateExcelInfoFileUrl(excelInfo)
- //}
- //
- resp := new(response.AddExcelInfoResp)
- resp.ExcelInfoId = excelInfo.ExcelInfoId
- resp.UniqueCode = excelInfo.UniqueCode
- // 生成excel文件
- go excel.UpdateExcelInfoFileUrl(excelInfo)
- //新增操作日志
- //{
- // excelLog := &data_manage.ExcelInfoLog{
- // //ExcelInfoLogId: 0,
- // ExcelInfoId: excelInfo.ExcelInfoId,
- // ExcelName: req.ExcelName,
- // ExcelClassifyId: req.ExcelClassifyId,
- // SysUserId: sysUser.AdminId,
- // SysUserRealName: sysUser.RealName,
- // UniqueCode: excelInfo.UniqueCode,
- // CreateTime: time.Now(),
- // Content: string(c.Ctx.Input.RequestBody),
- // Status: "新增表格",
- // Method: c.Ctx.Input.URI(),
- // }
- // go data_manage.AddExcelInfoLog(excelLog)
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = false //数据量太大了,不写入日志吧
- }
- // Save
- // @Title 保存表格接口
- // @Description 保存表格接口
- // @Param request body request.AddExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /save [post]
- func (c *CustomAnalysisController) Save() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req request.SaveExcelInfoReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.ExcelName = strings.Trim(req.ExcelName, " ")
- if req.ExcelName == "" {
- br.Msg = "请填写表格名称!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请选择excel!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelClassifyId <= 0 {
- br.Msg = "分类参数错误!"
- br.IsSendEmail = false
- return
- }
- excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "找不到该EXCEL!"
- br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
- return
- }
- if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
- br.Msg = "EXCEL异常!"
- br.IsSendEmail = false
- return
- }
- err, errMsg, isSendEmail := excel.SaveCustomAnalysisTable(excelInfo, utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, req.OpSheetList)
- if err != nil {
- br.Msg = "保存失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "保存失败,Err:" + err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
- //if req.Source == 1 {
- // go UpdateExcelInfoFileUrl(excelInfo)
- //}
- //
- resp := new(response.AddExcelInfoResp)
- resp.ExcelInfoId = excelInfo.ExcelInfoId
- resp.UniqueCode = excelInfo.UniqueCode
- // 生成excel文件
- go excel.UpdateExcelInfoFileUrl(excelInfo)
- //新增操作日志
- //{
- // excelLog := &data_manage.ExcelInfoLog{
- // //ExcelInfoLogId: 0,
- // ExcelInfoId: excelInfo.ExcelInfoId,
- // ExcelName: req.ExcelName,
- // ExcelClassifyId: req.ExcelClassifyId,
- // SysUserId: sysUser.AdminId,
- // SysUserRealName: sysUser.RealName,
- // UniqueCode: excelInfo.UniqueCode,
- // CreateTime: time.Now(),
- // Content: string(c.Ctx.Input.RequestBody),
- // Status: "新增表格",
- // Method: c.Ctx.Input.URI(),
- // }
- // go data_manage.AddExcelInfoLog(excelLog)
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = false //数据量太大了,不写入日志吧
- }
- // BaseExcelDetail
- // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
- // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
- // @Param UniqueCode query string true "excel唯一编码"
- // @Success 200 {object} response.ExcelListResp
- // @router /excel/base [get]
- func (c *CustomAnalysisController) BaseExcelDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- uniqueCode := c.GetString("UniqueCode")
- if uniqueCode == `` {
- br.Msg = "请选择表格"
- br.ErrMsg = "UniqueCode未传"
- br.IsSendEmail = false
- return
- }
- resp := response.FindExcelInfoResp{}
- // 获取数据详情
- excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- br.Msg = "获取表格事变"
- br.ErrMsg = err.Error()
- return
- }
- // 编辑状态
- markStatus, err := services.UpdateExcelEditMark(excelDetail.ExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
- if err != nil {
- br.Msg = "查询标记状态失败"
- br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
- return
- }
- // 数据权限
- //haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelDetail.ExcelInfoId, excelDetail.ExcelClassifyId, excelDetail.IsJoinPermission, c.SysUser.AdminId)
- //if err != nil {
- // br.Msg = "获取ETA表格失败"
- // br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
- // return
- //}
- // 自定义分析权限与其他表格略有差别...
- permissions, e := excelPermissionModel.GetExcelPermissionByExcelIdAndUserId(excelDetail.ExcelInfoId, sysUser.AdminId)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取表格权限失败, %v", e)
- return
- }
- var permissionType []int
- for _, v := range permissions {
- permissionType = append(permissionType, v.PermissionType)
- }
- opButton := excel.GetCustomAnalysisOpButton(sysUser, excelDetail.SysUserId, permissionType)
- resp.IsFind = true
- resp.ExcelInfo = response.FindExcelInfo{
- ExcelInfoId: excelDetail.ExcelInfoId,
- Source: excelDetail.Source,
- ExcelType: excelDetail.ExcelType,
- ExcelName: excelDetail.ExcelName,
- UniqueCode: excelDetail.UniqueCode,
- ExcelClassifyId: excelDetail.ExcelClassifyId,
- SysUserId: excelDetail.SysUserId,
- SysUserRealName: excelDetail.SysUserRealName,
- ExcelImage: excelDetail.ExcelImage,
- FileUrl: excelDetail.FileUrl,
- Sort: excelDetail.Sort,
- ModifyTime: excelDetail.ModifyTime,
- CreateTime: excelDetail.CreateTime,
- Button: opButton,
- HaveOperaAuth: true,
- }
- if markStatus.Status == 0 {
- resp.ExcelInfo.CanEdit = true
- } else {
- resp.ExcelInfo.Editor = markStatus.Editor
- }
- if excelDetail != nil {
- sheetList, err := excelModel.GetAllSheetItemList(excelDetail.ExcelInfoId)
- if err != nil {
- br.Msg = "获取sheet失败"
- br.ErrMsg = "获取sheet失败,err:" + err.Error()
- return
- }
- if len(sheetList) > 0 {
- sheetIdList := make([]int, 0)
- for _, v := range sheetList {
- sheetIdList = append(sheetIdList, v.ExcelSheetId)
- }
- // 获取所有sheet的第一页的数据
- sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, 1)
- if err != nil {
- br.Msg = "获取sheet中的数据失败"
- br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
- return
- }
- sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
- for _, v := range sheetDataList {
- sheetDataMap[v.ExcelSheetId] = v
- }
- for k, v := range sheetList {
- sheetData, ok := sheetDataMap[v.ExcelSheetId]
- if !ok {
- continue
- }
- v.Data = sheetData
- sheetList[k] = v
- }
- }
- // TODO 合并单元格信息、计算公式
- resp.SheetList = sheetList
- }
- //resp := response.ExcelListResp{
- // Paging: page,
- // List: list,
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // ExcelDataList
- // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
- // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
- // @Param UniqueCode query string true "excel唯一编码"
- // @Param Page query int true "页码"
- // @Success 200 {object} response.ExcelListResp
- // @router /excel/data [get]
- func (c *CustomAnalysisController) ExcelDataList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- uniqueCode := c.GetString("UniqueCode")
- if uniqueCode == `` {
- br.Msg = "请选择表格"
- br.ErrMsg = "UniqueCode未传"
- br.IsSendEmail = false
- return
- }
- page, _ := c.GetInt("Page")
- if page <= 0 {
- br.Msg = "页码异常"
- br.ErrMsg = "页码异常"
- br.IsSendEmail = false
- return
- }
- sheetList := make([]*excelModel.SheetItem, 0)
- // 获取数据详情
- excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = sheetList
- return
- }
- br.Msg = "获取表格事变"
- br.ErrMsg = err.Error()
- return
- }
- if excelDetail.Source != utils.CUSTOM_ANALYSIS_TABLE {
- br.Msg = "excel异常"
- br.ErrMsg = "excel异常"
- br.IsSendEmail = false
- return
- }
- if excelDetail != nil {
- sheetList, err = excelModel.GetAllNoConfigSheetItemList(excelDetail.ExcelInfoId)
- if err != nil {
- br.Msg = "获取sheet失败"
- br.ErrMsg = "获取sheet失败,err:" + err.Error()
- return
- }
- if len(sheetList) > 0 {
- sheetIdList := make([]int, 0)
- for _, v := range sheetList {
- sheetIdList = append(sheetIdList, v.ExcelSheetId)
- }
- // 获取所有sheet的第一页的数据
- sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, page)
- if err != nil {
- br.Msg = "获取sheet中的数据失败"
- br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
- return
- }
- sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
- for _, v := range sheetDataList {
- sheetDataMap[v.ExcelSheetId] = v
- }
- for k, v := range sheetList {
- sheetData, ok := sheetDataMap[v.ExcelSheetId]
- if !ok {
- continue
- }
- v.Data = sheetData
- sheetList[k] = v
- }
- }
- // TODO 合并单元格信息、计算公式
- }
- //resp := response.ExcelListResp{
- // Paging: page,
- // List: list,
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = sheetList
- }
- // ClassifyList
- // @Title 分类列表
- // @Description 分类列表
- // @Param IsShare query bool false "是否共享表格: true-是; false-否(默认)"
- // @Success 200 {object} response.ExcelClassifyListResp
- // @router /excel_classify/list [get]
- func (c *CustomAnalysisController) ClassifyList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- source := utils.CUSTOM_ANALYSIS_TABLE
- isShare, _ := c.GetBool("IsShare")
- resp := new(response.ExcelClassifyListResp)
- // 查询我分享的/分享给我的表格
- excels, e := excelPermissionModel.GetAdminAuthExcelInfoPermission(source, sysUser.AdminId, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取我分享的/分享给我的表格失败, %v", e)
- return
- }
- hasShareMap := make(map[int]bool) // 是否已分享标记
- for _, v := range excels {
- if v.CreateUserId == sysUser.AdminId {
- hasShareMap[int(v.ExcelInfoId)] = true
- }
- }
- // 共享
- if isShare {
- shareExcel := new(excelModel.ExcelClassifyItems)
- shareExcel.ExcelClassifyId = excelModel.CustomAnalysisMenuShareId
- shareExcel.ExcelClassifyName = "我共享的"
- shareExcel.Level = 1
- shareExcel.Sort = 1
- shareExcel.UniqueCode = fmt.Sprintf("SHARE%d", shareExcel.ExcelClassifyId)
- shareExcel.HaveOperaAuth = true
- shareExcel.Children = make([]*excelModel.ExcelClassifyItems, 0)
- sharedExcel := new(excelModel.ExcelClassifyItems)
- sharedExcel.ExcelClassifyId = excelModel.CustomAnalysisMenuSharedId
- sharedExcel.ExcelClassifyName = "收到共享"
- sharedExcel.Level = 1
- sharedExcel.Sort = 2
- sharedExcel.UniqueCode = fmt.Sprintf("SHARED%d", sharedExcel.ExcelClassifyId)
- sharedExcel.HaveOperaAuth = true
- sharedExcel.Children = make([]*excelModel.ExcelClassifyItems, 0)
- // 收到共享细分目录
- sharedUserClassify := make(map[int]*excelModel.ExcelClassifyItems)
- adminIdName := make(map[int]string)
- {
- adminOb := new(system.Admin)
- adminList, e := adminOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取用户列表失败, %v", e)
- return
- }
- for _, v := range adminList {
- adminIdName[v.AdminId] = v.RealName
- }
- }
- sortUser := make([]int, 0) // 分享人目录排序
- shareExists := make(map[int]bool)
- for _, v := range excels {
- item := new(excelModel.ExcelClassifyItems)
- item.ExcelInfoId = int(v.ExcelInfoId)
- item.ExcelClassifyName = v.ExcelName
- item.UniqueCode = v.UniqueCode
- item.HaveOperaAuth = true
- item.HasShare = hasShareMap[int(v.ExcelInfoId)]
- // 我共享的
- if v.CreateUserId == sysUser.AdminId && !shareExists[int(v.ExcelInfoId)] {
- shareExists[int(v.ExcelInfoId)] = true
- item.Level = 2
- item.ParentId = shareExcel.ExcelClassifyId
- item.ShowShareBtn = true
- shareExcel.Children = append(shareExcel.Children, item)
- continue
- }
- // 收到共享
- if int(v.SysUserId) == sysUser.AdminId {
- if adminIdName[v.CreateUserId] == "" {
- // 分享人用户不存在, 那么不显示该分类
- continue
- }
- if !utils.InArrayByInt(sortUser, v.CreateUserId) {
- sortUser = append(sortUser, v.CreateUserId)
- }
- if sharedUserClassify[v.CreateUserId] == nil {
- us := new(excelModel.ExcelClassifyItems)
- us.ExcelClassifyId = v.CreateUserId // 用户ID作为分类ID
- us.ExcelClassifyName = fmt.Sprintf("%s的表格", adminIdName[v.CreateUserId])
- us.Level = 2
- us.UniqueCode = fmt.Sprintf("%d-%d", sharedExcel.ExcelClassifyId, v.CreateUserId)
- us.HaveOperaAuth = true
- us.ShowShareBtn = false
- us.Children = make([]*excelModel.ExcelClassifyItems, 0)
- sharedUserClassify[v.CreateUserId] = us
- }
- item.Level = 3
- item.ParentId = sharedUserClassify[v.CreateUserId].ExcelClassifyId
- sharedUserClassify[v.CreateUserId].Children = append(sharedUserClassify[v.CreateUserId].Children, item)
- }
- }
- // 按照分享人第一次分享时间降序=_=!, 查询的时候是升序, 这里反过来
- for i, j := 0, len(sortUser)-1; i < j; i, j = i+1, j-1 {
- sortUser[i], sortUser[j] = sortUser[j], sortUser[i]
- }
- for _, v := range sortUser {
- if sharedUserClassify[v] != nil {
- sharedExcel.Children = append(sharedExcel.Children, sharedUserClassify[v])
- }
- }
- resp.AllNodes = make([]*excelModel.ExcelClassifyItems, 0)
- resp.AllNodes = append(resp.AllNodes, shareExcel, sharedExcel)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- // 我的表格
- classifyList, err := excelModel.GetAdminExcelClassifyBySource(source, sysUser.AdminId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- classifyMap := make(map[int]*excelModel.ExcelClassifyItems)
- for _, v := range classifyList {
- v.HaveOperaAuth = true
- classifyMap[v.ExcelClassifyId] = v
- }
- allExcelInfo, err := excelModel.GetNoContentExcelInfoAll(source, sysUser.AdminId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
- return
- }
- excelInfoMap := make(map[int][]*excelModel.ExcelClassifyItems)
- for _, v := range allExcelInfo {
- v.HaveOperaAuth = true
- v.ShowShareBtn = true
- v.HasShare = hasShareMap[v.ExcelInfoId]
- excelInfoMap[v.ExcelClassifyId] = append(excelInfoMap[v.ExcelClassifyId], v)
- }
- classifyListMap := make(map[int][]*excelModel.ExcelClassifyItems)
- for _, v := range classifyList {
- if v.ParentId > 0 {
- classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v)
- }
- if existItems, ok := excelInfoMap[v.ExcelClassifyId]; ok {
- v.Children = existItems
- }
- }
- for key, classify := range classifyList {
- subList, ok := classifyListMap[classify.ExcelClassifyId]
- if ok {
- classifyList[key].Children = append(classifyList[key].Children, subList...)
- sort.Slice(classifyList[key].Children, func(i, j int) bool {
- return excelModel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j])
- })
- }
- }
- nodeAll := make([]*excelModel.ExcelClassifyItems, 0)
- for _, v := range classifyList {
- if v.ParentId == 0 {
- sort.Slice(v.Children, func(i, j int) bool {
- return excelModel.ExcelClassifyItemBySort(v.Children[i], v.Children[j])
- })
- nodeAll = append(nodeAll, v)
- }
- }
- resp.AllNodes = nodeAll
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // List
- // @Title 自定义分析列表
- // @Description ETA表格列表接口
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param ExcelClassifyId query int true "分类id"
- // @Param Keyword query string true "搜索关键词"
- // @Param IsShare query bool false "是否共享表格: true-是; false-否(默认)"
- // @Success 200 {object} response.ExcelListResp
- // @router /excel/list [get]
- func (c *CustomAnalysisController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- excelClassifyId, _ := c.GetInt("ExcelClassifyId")
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- keyword := c.GetString("Keyword")
- keyword = strings.TrimSpace(keyword)
- adminId := sysUser.AdminId
- isShare, _ := c.GetBool("IsShare")
- source := utils.CUSTOM_ANALYSIS_TABLE
- var condition string
- var pars []interface{}
- condition += " AND source = ?"
- pars = append(pars, source)
- // 共享的
- //hasShareMap := make(map[int]bool)
- if isShare {
- if keyword != "" {
- keyword = fmt.Sprint("%", keyword, "%")
- }
- // 查询我分享的/分享给我的表格
- excels, e := excelPermissionModel.GetAdminAuthExcelInfoPermission(source, adminId, keyword)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取我分享的/分享给我的表格失败, %v", e)
- return
- }
- var excelIds, shareIds, sharedIds, userIds []int
- for _, v := range excels {
- id := int(v.ExcelInfoId)
- // 无分类ID
- if excelClassifyId <= 0 && !utils.InArrayByInt(excelIds, id) {
- excelIds = append(excelIds, id)
- continue
- }
- // 用户分类
- if excelClassifyId == v.CreateUserId {
- userIds = append(userIds, id)
- continue
- }
- // 我共享的
- if excelClassifyId == excelModel.CustomAnalysisMenuShareId && v.CreateUserId == sysUser.AdminId {
- shareIds = append(shareIds, id)
- //hasShareMap[id] = true
- continue
- }
- // 收到共享
- if excelClassifyId == excelModel.CustomAnalysisMenuSharedId && int(v.SysUserId) == sysUser.AdminId {
- sharedIds = append(sharedIds, id)
- continue
- }
- }
- if excelClassifyId > 0 {
- switch excelClassifyId {
- case excelModel.CustomAnalysisMenuShareId: // 我共享的
- excelIds = shareIds
- case excelModel.CustomAnalysisMenuSharedId: // 收到共享
- excelIds = sharedIds
- default: // 用户目录
- excelIds = userIds
- }
- }
- if len(excelIds) == 0 {
- page := paging.GetPaging(currentIndex, pageSize, 0)
- resp := response.ExcelListResp{
- Paging: page,
- List: make([]*excelModel.MyExcelInfoList, 0),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- condition += fmt.Sprintf(` AND excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
- pars = append(pars, excelIds)
- } else {
- condition += ` AND sys_user_id = ?`
- pars = append(pars, adminId)
- }
- // 筛选分类
- if !isShare && excelClassifyId > 0 {
- _, err := excelModel.GetExcelClassifyById(excelClassifyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取表格信息失败"
- br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
- return
- }
- childClassify, e, _ := excel2.GetChildClassifyByClassifyId(excelClassifyId, source)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
- return
- }
- if len(childClassify) == 0 {
- condition += " AND excel_classify_id = ? "
- pars = append(pars, excelClassifyId)
- } else {
- classifyIds := []int{excelClassifyId}
- for _, v := range childClassify {
- classifyIds = append(classifyIds, v.ExcelClassifyId)
- }
- condition += fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
- pars = append(pars, classifyIds)
- }
- }
- if keyword != "" {
- condition += ` AND (excel_name LIKE ?)`
- pars = utils.GetLikeKeywordPars(pars, keyword, 1)
- }
- // 获取表格信息
- var total int
- page := paging.GetPaging(currentIndex, pageSize, total)
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- list, err := excelModel.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Success = true
- br.Msg = "获取表格信息失败"
- br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
- return
- }
- for _, v := range list {
- v.HaveOperaAuth = true
- //v.Button.CancelShareButton = hasShareMap[v.ExcelInfoId]
- }
- // 总数据量
- dataCount, err := excelModel.GetExcelListCountByCondition(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取表格列表信息失败"
- br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
- return
- }
- page = paging.GetPaging(currentIndex, pageSize, dataCount)
- resp := response.ExcelListResp{
- Paging: page,
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Share
- // @Title 分享表格
- // @Description 分享表格
- // @Param request body request.ShareExcelInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /excel/share [post]
- func (c *CustomAnalysisController) Share() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg != "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- var req request.ShareExcelInfoReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请选择表格"
- return
- }
- excelInfo, e := excelModel.GetExcelInfoById(req.ExcelInfoId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "表格不存在, 请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取表格失败, %v", e)
- return
- }
- if excelInfo.SysUserId != sysUser.AdminId {
- br.Msg = "无权操作"
- br.ErrMsg = fmt.Sprintf("无权设置表格权限, ExcelInfoId: %d, SysAdminId: %d", req.ExcelInfoId, sysUser.AdminId)
- return
- }
- source := utils.CUSTOM_ANALYSIS_TABLE
- newPermissions := make([]*excelPermissionModel.ExcelInfoPermission, 0)
- for _, v := range req.ViewUserIds {
- newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
- ExcelInfoId: int32(req.ExcelInfoId),
- Source: int32(source),
- SysUserId: int32(v),
- CreateTime: time.Now().Local(),
- ModifyTime: time.Now().Local(),
- PermissionType: 1,
- })
- }
- for _, v := range req.EditUserIds {
- newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
- ExcelInfoId: int32(req.ExcelInfoId),
- Source: int32(source),
- SysUserId: int32(v),
- CreateTime: time.Now().Local(),
- ModifyTime: time.Now().Local(),
- PermissionType: 2,
- })
- }
- if e := excelPermissionModel.ClearAndSetExcelInfoPermission(source, req.ExcelInfoId, newPermissions); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("设置表格权限失败, %v", e)
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // ShareDetail
- // @Title 分享表格-详情
- // @Description 分享表格-详情
- // @Param ExcelInfoId query int true "表格ID"
- // @Success 200 {object} response.ShareExcelInfoDetail
- // @router /excel/share_detail [get]
- func (c *CustomAnalysisController) ShareDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg != "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- excelInfoId, _ := c.GetInt("ExcelInfoId")
- if excelInfoId <= 0 {
- br.Msg = "请选择表格"
- return
- }
- permissions, e := excelPermissionModel.GetExcelPermissionBySourceAndId(excelInfoId, utils.CUSTOM_ANALYSIS_TABLE)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取表格权限失败, %v", e)
- return
- }
- resp := new(response.ShareExcelInfoDetail)
- resp.ExcelInfoId = excelInfoId
- for _, v := range permissions {
- if v.PermissionType == 1 {
- resp.ViewUserIds = append(resp.ViewUserIds, int(v.SysUserId))
- }
- if v.PermissionType == 2 {
- resp.EditUserIds = append(resp.EditUserIds, int(v.SysUserId))
- }
- }
- br.Data = resp
- br.Ret = 200
- 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 = "操作成功"
- }
|