1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165 |
- 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"
- excel3 "eta/eta_api/services/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, sysUser.AdminId)
- 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)
- condition += " AND sys_user_id = ? "
- pars = append(pars, sysUser.AdminId)
- 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)
- // 写入ES
- go func() {
- excel3.EsAddOrEditExcel(excelInfo.ExcelInfoId)
- }()
- //新增操作日志
- //{
- // 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)
- // 写入ES
- go func() {
- excel3.EsAddOrEditExcel(excelInfo.ExcelInfoId)
- }()
- //新增操作日志
- //{
- // 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 = "获取成功"
- }
|