12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- 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, 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)
- //新增操作日志
- //{
- // 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 = "获取成功"
- }
|