123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- package excel
- import (
- "encoding/json"
- "eta/eta_mobile/controllers"
- "eta/eta_mobile/models"
- excelModel "eta/eta_mobile/models/data_manage/excel"
- "eta/eta_mobile/models/data_manage/excel/request"
- "eta/eta_mobile/models/data_manage/excel/response"
- "eta/eta_mobile/services"
- "eta/eta_mobile/services/data/data_manage_permission"
- "eta/eta_mobile/services/data/excel"
- "eta/eta_mobile/utils"
- "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
- }
- 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 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
- }
|