custom_analysis.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. excelPermissionModel "eta/eta_api/models/data_manage/data_manage_permission"
  7. excelModel "eta/eta_api/models/data_manage/excel"
  8. "eta/eta_api/models/data_manage/excel/request"
  9. "eta/eta_api/models/data_manage/excel/response"
  10. "eta/eta_api/models/system"
  11. "eta/eta_api/services"
  12. "eta/eta_api/services/data/data_manage_permission"
  13. "eta/eta_api/services/data/excel"
  14. excel2 "eta/eta_api/services/data/excel"
  15. excel3 "eta/eta_api/services/excel"
  16. "eta/eta_api/utils"
  17. "fmt"
  18. "github.com/rdlucklib/rdluck_tools/paging"
  19. "sort"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. // CustomAnalysisController 自定义分析
  25. type CustomAnalysisController struct {
  26. controllers.BaseAuthController
  27. }
  28. // ExcelByName
  29. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  30. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  31. // @Param ExcelName query string true "搜索关键词"
  32. // @Success 200 {object} response.ExcelListResp
  33. // @router /excel_by_name [get]
  34. func (c *CustomAnalysisController) ExcelByName() {
  35. br := new(models.BaseResponse).Init()
  36. defer func() {
  37. c.Data["json"] = br
  38. c.ServeJSON()
  39. }()
  40. sysUser := c.SysUser
  41. if sysUser == nil {
  42. br.Msg = "请登录"
  43. br.ErrMsg = "请登录,SysUser Is Empty"
  44. br.Ret = 408
  45. return
  46. }
  47. excelName := c.GetString("ExcelName")
  48. if excelName == `` {
  49. br.Msg = "请选择表格"
  50. br.ErrMsg = "ExcelName未传"
  51. br.IsSendEmail = false
  52. return
  53. }
  54. resp := response.FindExcelInfoResp{}
  55. excelName = utils.TrimLRStr(excelName)
  56. // 获取数据详情
  57. excelDetail, err := excelModel.GetNoContentExcelInfoByName(excelName, utils.CUSTOM_ANALYSIS_TABLE, sysUser.AdminId)
  58. if err != nil {
  59. if err.Error() == utils.ErrNoRow() {
  60. br.Ret = 200
  61. br.Success = true
  62. br.Msg = "获取成功"
  63. br.Data = resp
  64. return
  65. }
  66. br.Msg = "获取表格事变"
  67. br.ErrMsg = err.Error()
  68. return
  69. }
  70. // 数据权限
  71. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelDetail.ExcelInfoId, excelDetail.ExcelClassifyId, excelDetail.IsJoinPermission, c.SysUser.AdminId)
  72. if err != nil {
  73. br.Msg = "获取ETA表格失败"
  74. br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  75. return
  76. }
  77. resp.IsFind = true
  78. resp.ExcelInfo = response.FindExcelInfo{
  79. ExcelInfoId: excelDetail.ExcelInfoId,
  80. Source: excelDetail.Source,
  81. ExcelType: excelDetail.ExcelType,
  82. ExcelName: excelDetail.ExcelName,
  83. UniqueCode: excelDetail.UniqueCode,
  84. ExcelClassifyId: excelDetail.ExcelClassifyId,
  85. SysUserId: excelDetail.SysUserId,
  86. SysUserRealName: excelDetail.SysUserRealName,
  87. ExcelImage: excelDetail.ExcelImage,
  88. FileUrl: excelDetail.FileUrl,
  89. Sort: excelDetail.Sort,
  90. ModifyTime: excelDetail.ModifyTime,
  91. CreateTime: excelDetail.CreateTime,
  92. Button: excel.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, haveOperaAuth),
  93. HaveOperaAuth: true,
  94. }
  95. if excelDetail != nil {
  96. sheetList, err := excelModel.GetAllSheetItemList(excelDetail.ExcelInfoId)
  97. if err != nil {
  98. br.Msg = "获取sheet失败"
  99. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  100. return
  101. }
  102. resp.SheetList = sheetList
  103. }
  104. //resp := response.ExcelListResp{
  105. // Paging: page,
  106. // List: list,
  107. //}
  108. br.Ret = 200
  109. br.Success = true
  110. br.Msg = "获取成功"
  111. br.Data = resp
  112. }
  113. // Add
  114. // @Title 新增表格接口
  115. // @Description 新增表格接口
  116. // @Param request body request.AddExcelInfoReq true "type json string"
  117. // @Success 200 {object} response.AddExcelInfoResp
  118. // @router /add [post]
  119. func (c *CustomAnalysisController) Add() {
  120. br := new(models.BaseResponse).Init()
  121. defer func() {
  122. c.Data["json"] = br
  123. c.ServeJSON()
  124. }()
  125. sysUser := c.SysUser
  126. if sysUser == nil {
  127. br.Msg = "请登录"
  128. br.ErrMsg = "请登录,SysUser Is Empty"
  129. br.Ret = 408
  130. return
  131. }
  132. deleteCache := true
  133. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  134. defer func() {
  135. if deleteCache {
  136. _ = utils.Rc.Delete(cacheKey)
  137. }
  138. }()
  139. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  140. deleteCache = false
  141. br.Msg = "系统处理中,请稍后重试!"
  142. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  143. return
  144. }
  145. var req request.AddExcelInfoReq
  146. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  147. if err != nil {
  148. br.Msg = "参数解析异常!"
  149. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  150. return
  151. }
  152. req.ExcelName = strings.Trim(req.ExcelName, " ")
  153. if req.ExcelName == "" {
  154. br.Msg = "请填写表格名称!"
  155. br.IsSendEmail = false
  156. return
  157. }
  158. // 获取是否存在该表格名称
  159. {
  160. var condition string
  161. var pars []interface{}
  162. condition += " AND source=? "
  163. pars = append(pars, utils.CUSTOM_ANALYSIS_TABLE)
  164. condition += " AND excel_name=? "
  165. pars = append(pars, req.ExcelName)
  166. condition += " AND sys_user_id = ? "
  167. pars = append(pars, sysUser.AdminId)
  168. count, err := excelModel.GetExcelInfoCountByCondition(condition, pars)
  169. if err != nil {
  170. br.Msg = "判断表格名称是否存在失败"
  171. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  172. return
  173. }
  174. if count > 0 {
  175. br.Msg = "表格名称已存在,请重新填写表格名称"
  176. br.IsSendEmail = false
  177. return
  178. }
  179. }
  180. if req.ExcelClassifyId <= 0 {
  181. br.Msg = "分类参数错误!"
  182. br.IsSendEmail = false
  183. return
  184. }
  185. excelInfo, err, errMsg, isSendEmail := excel.AddCustomAnalysisTable(utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, sysUser)
  186. if err != nil {
  187. br.Msg = "保存失败"
  188. if errMsg != `` {
  189. br.Msg = errMsg
  190. }
  191. br.ErrMsg = "保存失败,Err:" + err.Error()
  192. br.IsSendEmail = isSendEmail
  193. return
  194. }
  195. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  196. //if req.Source == 1 {
  197. // go UpdateExcelInfoFileUrl(excelInfo)
  198. //}
  199. //
  200. resp := new(response.AddExcelInfoResp)
  201. resp.ExcelInfoId = excelInfo.ExcelInfoId
  202. resp.UniqueCode = excelInfo.UniqueCode
  203. // 生成excel文件
  204. go excel.UpdateExcelInfoFileUrl(excelInfo)
  205. // 写入ES
  206. go func() {
  207. excel3.EsAddOrEditExcel(excelInfo.ExcelInfoId)
  208. }()
  209. //新增操作日志
  210. //{
  211. // excelLog := &data_manage.ExcelInfoLog{
  212. // //ExcelInfoLogId: 0,
  213. // ExcelInfoId: excelInfo.ExcelInfoId,
  214. // ExcelName: req.ExcelName,
  215. // ExcelClassifyId: req.ExcelClassifyId,
  216. // SysUserId: sysUser.AdminId,
  217. // SysUserRealName: sysUser.RealName,
  218. // UniqueCode: excelInfo.UniqueCode,
  219. // CreateTime: time.Now(),
  220. // Content: string(c.Ctx.Input.RequestBody),
  221. // Status: "新增表格",
  222. // Method: c.Ctx.Input.URI(),
  223. // }
  224. // go data_manage.AddExcelInfoLog(excelLog)
  225. //}
  226. br.Ret = 200
  227. br.Success = true
  228. br.Msg = "保存成功"
  229. br.Data = resp
  230. br.IsAddLog = false //数据量太大了,不写入日志吧
  231. }
  232. // Save
  233. // @Title 保存表格接口
  234. // @Description 保存表格接口
  235. // @Param request body request.AddExcelInfoReq true "type json string"
  236. // @Success 200 {object} response.AddExcelInfoResp
  237. // @router /save [post]
  238. func (c *CustomAnalysisController) Save() {
  239. br := new(models.BaseResponse).Init()
  240. defer func() {
  241. c.Data["json"] = br
  242. c.ServeJSON()
  243. }()
  244. sysUser := c.SysUser
  245. if sysUser == nil {
  246. br.Msg = "请登录"
  247. br.ErrMsg = "请登录,SysUser Is Empty"
  248. br.Ret = 408
  249. return
  250. }
  251. deleteCache := true
  252. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  253. defer func() {
  254. if deleteCache {
  255. _ = utils.Rc.Delete(cacheKey)
  256. }
  257. }()
  258. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  259. deleteCache = false
  260. br.Msg = "系统处理中,请稍后重试!"
  261. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  262. return
  263. }
  264. var req request.SaveExcelInfoReq
  265. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  266. if err != nil {
  267. br.Msg = "参数解析异常!"
  268. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  269. return
  270. }
  271. req.ExcelName = strings.Trim(req.ExcelName, " ")
  272. if req.ExcelName == "" {
  273. br.Msg = "请填写表格名称!"
  274. br.IsSendEmail = false
  275. return
  276. }
  277. if req.ExcelInfoId <= 0 {
  278. br.Msg = "请选择excel!"
  279. br.IsSendEmail = false
  280. return
  281. }
  282. if req.ExcelClassifyId <= 0 {
  283. br.Msg = "分类参数错误!"
  284. br.IsSendEmail = false
  285. return
  286. }
  287. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  288. if err != nil {
  289. br.Msg = "找不到该EXCEL!"
  290. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  291. return
  292. }
  293. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  294. br.Msg = "EXCEL异常!"
  295. br.IsSendEmail = false
  296. return
  297. }
  298. err, errMsg, isSendEmail := excel.SaveCustomAnalysisTable(excelInfo, utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, req.OpSheetList)
  299. if err != nil {
  300. br.Msg = "保存失败"
  301. if errMsg != `` {
  302. br.Msg = errMsg
  303. }
  304. br.ErrMsg = "保存失败,Err:" + err.Error()
  305. br.IsSendEmail = isSendEmail
  306. return
  307. }
  308. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  309. //if req.Source == 1 {
  310. // go UpdateExcelInfoFileUrl(excelInfo)
  311. //}
  312. //
  313. resp := new(response.AddExcelInfoResp)
  314. resp.ExcelInfoId = excelInfo.ExcelInfoId
  315. resp.UniqueCode = excelInfo.UniqueCode
  316. // 生成excel文件
  317. go excel.UpdateExcelInfoFileUrl(excelInfo)
  318. // 写入ES
  319. go func() {
  320. excel3.EsAddOrEditExcel(excelInfo.ExcelInfoId)
  321. }()
  322. //新增操作日志
  323. //{
  324. // excelLog := &data_manage.ExcelInfoLog{
  325. // //ExcelInfoLogId: 0,
  326. // ExcelInfoId: excelInfo.ExcelInfoId,
  327. // ExcelName: req.ExcelName,
  328. // ExcelClassifyId: req.ExcelClassifyId,
  329. // SysUserId: sysUser.AdminId,
  330. // SysUserRealName: sysUser.RealName,
  331. // UniqueCode: excelInfo.UniqueCode,
  332. // CreateTime: time.Now(),
  333. // Content: string(c.Ctx.Input.RequestBody),
  334. // Status: "新增表格",
  335. // Method: c.Ctx.Input.URI(),
  336. // }
  337. // go data_manage.AddExcelInfoLog(excelLog)
  338. //}
  339. br.Ret = 200
  340. br.Success = true
  341. br.Msg = "保存成功"
  342. br.Data = resp
  343. br.IsAddLog = false //数据量太大了,不写入日志吧
  344. }
  345. // BaseExcelDetail
  346. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  347. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  348. // @Param UniqueCode query string true "excel唯一编码"
  349. // @Success 200 {object} response.ExcelListResp
  350. // @router /excel/base [get]
  351. func (c *CustomAnalysisController) BaseExcelDetail() {
  352. br := new(models.BaseResponse).Init()
  353. defer func() {
  354. c.Data["json"] = br
  355. c.ServeJSON()
  356. }()
  357. sysUser := c.SysUser
  358. if sysUser == nil {
  359. br.Msg = "请登录"
  360. br.ErrMsg = "请登录,SysUser Is Empty"
  361. br.Ret = 408
  362. return
  363. }
  364. uniqueCode := c.GetString("UniqueCode")
  365. if uniqueCode == `` {
  366. br.Msg = "请选择表格"
  367. br.ErrMsg = "UniqueCode未传"
  368. br.IsSendEmail = false
  369. return
  370. }
  371. resp := response.FindExcelInfoResp{}
  372. // 获取数据详情
  373. excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
  374. if err != nil {
  375. if err.Error() == utils.ErrNoRow() {
  376. br.Ret = 200
  377. br.Success = true
  378. br.Msg = "获取成功"
  379. br.Data = resp
  380. return
  381. }
  382. br.Msg = "获取表格事变"
  383. br.ErrMsg = err.Error()
  384. return
  385. }
  386. // 编辑状态
  387. markStatus, err := services.UpdateExcelEditMark(excelDetail.ExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
  388. if err != nil {
  389. br.Msg = "查询标记状态失败"
  390. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  391. return
  392. }
  393. // 数据权限
  394. //haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelDetail.ExcelInfoId, excelDetail.ExcelClassifyId, excelDetail.IsJoinPermission, c.SysUser.AdminId)
  395. //if err != nil {
  396. // br.Msg = "获取ETA表格失败"
  397. // br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  398. // return
  399. //}
  400. // 自定义分析权限与其他表格略有差别...
  401. permissions, e := excelPermissionModel.GetExcelPermissionByExcelIdAndUserId(excelDetail.ExcelInfoId, sysUser.AdminId)
  402. if e != nil {
  403. br.Msg = "获取失败"
  404. br.ErrMsg = fmt.Sprintf("获取表格权限失败, %v", e)
  405. return
  406. }
  407. var permissionType []int
  408. for _, v := range permissions {
  409. permissionType = append(permissionType, v.PermissionType)
  410. }
  411. opButton := excel.GetCustomAnalysisOpButton(sysUser, excelDetail.SysUserId, permissionType)
  412. resp.IsFind = true
  413. resp.ExcelInfo = response.FindExcelInfo{
  414. ExcelInfoId: excelDetail.ExcelInfoId,
  415. Source: excelDetail.Source,
  416. ExcelType: excelDetail.ExcelType,
  417. ExcelName: excelDetail.ExcelName,
  418. UniqueCode: excelDetail.UniqueCode,
  419. ExcelClassifyId: excelDetail.ExcelClassifyId,
  420. SysUserId: excelDetail.SysUserId,
  421. SysUserRealName: excelDetail.SysUserRealName,
  422. ExcelImage: excelDetail.ExcelImage,
  423. FileUrl: excelDetail.FileUrl,
  424. Sort: excelDetail.Sort,
  425. ModifyTime: excelDetail.ModifyTime,
  426. CreateTime: excelDetail.CreateTime,
  427. Button: opButton,
  428. HaveOperaAuth: true,
  429. }
  430. if markStatus.Status == 0 {
  431. resp.ExcelInfo.CanEdit = true
  432. } else {
  433. resp.ExcelInfo.Editor = markStatus.Editor
  434. }
  435. if excelDetail != nil {
  436. sheetList, err := excelModel.GetAllSheetItemList(excelDetail.ExcelInfoId)
  437. if err != nil {
  438. br.Msg = "获取sheet失败"
  439. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  440. return
  441. }
  442. if len(sheetList) > 0 {
  443. sheetIdList := make([]int, 0)
  444. for _, v := range sheetList {
  445. sheetIdList = append(sheetIdList, v.ExcelSheetId)
  446. }
  447. // 获取所有sheet的第一页的数据
  448. sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, 1)
  449. if err != nil {
  450. br.Msg = "获取sheet中的数据失败"
  451. br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
  452. return
  453. }
  454. sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
  455. for _, v := range sheetDataList {
  456. sheetDataMap[v.ExcelSheetId] = v
  457. }
  458. for k, v := range sheetList {
  459. sheetData, ok := sheetDataMap[v.ExcelSheetId]
  460. if !ok {
  461. continue
  462. }
  463. v.Data = sheetData
  464. sheetList[k] = v
  465. }
  466. }
  467. // TODO 合并单元格信息、计算公式
  468. resp.SheetList = sheetList
  469. }
  470. //resp := response.ExcelListResp{
  471. // Paging: page,
  472. // List: list,
  473. //}
  474. br.Ret = 200
  475. br.Success = true
  476. br.Msg = "获取成功"
  477. br.Data = resp
  478. }
  479. // ExcelDataList
  480. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  481. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  482. // @Param UniqueCode query string true "excel唯一编码"
  483. // @Param Page query int true "页码"
  484. // @Success 200 {object} response.ExcelListResp
  485. // @router /excel/data [get]
  486. func (c *CustomAnalysisController) ExcelDataList() {
  487. br := new(models.BaseResponse).Init()
  488. defer func() {
  489. c.Data["json"] = br
  490. c.ServeJSON()
  491. }()
  492. sysUser := c.SysUser
  493. if sysUser == nil {
  494. br.Msg = "请登录"
  495. br.ErrMsg = "请登录,SysUser Is Empty"
  496. br.Ret = 408
  497. return
  498. }
  499. uniqueCode := c.GetString("UniqueCode")
  500. if uniqueCode == `` {
  501. br.Msg = "请选择表格"
  502. br.ErrMsg = "UniqueCode未传"
  503. br.IsSendEmail = false
  504. return
  505. }
  506. page, _ := c.GetInt("Page")
  507. if page <= 0 {
  508. br.Msg = "页码异常"
  509. br.ErrMsg = "页码异常"
  510. br.IsSendEmail = false
  511. return
  512. }
  513. sheetList := make([]*excelModel.SheetItem, 0)
  514. // 获取数据详情
  515. excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
  516. if err != nil {
  517. if err.Error() == utils.ErrNoRow() {
  518. br.Ret = 200
  519. br.Success = true
  520. br.Msg = "获取成功"
  521. br.Data = sheetList
  522. return
  523. }
  524. br.Msg = "获取表格事变"
  525. br.ErrMsg = err.Error()
  526. return
  527. }
  528. if excelDetail.Source != utils.CUSTOM_ANALYSIS_TABLE {
  529. br.Msg = "excel异常"
  530. br.ErrMsg = "excel异常"
  531. br.IsSendEmail = false
  532. return
  533. }
  534. if excelDetail != nil {
  535. sheetList, err = excelModel.GetAllNoConfigSheetItemList(excelDetail.ExcelInfoId)
  536. if err != nil {
  537. br.Msg = "获取sheet失败"
  538. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  539. return
  540. }
  541. if len(sheetList) > 0 {
  542. sheetIdList := make([]int, 0)
  543. for _, v := range sheetList {
  544. sheetIdList = append(sheetIdList, v.ExcelSheetId)
  545. }
  546. // 获取所有sheet的第一页的数据
  547. sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, page)
  548. if err != nil {
  549. br.Msg = "获取sheet中的数据失败"
  550. br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
  551. return
  552. }
  553. sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
  554. for _, v := range sheetDataList {
  555. sheetDataMap[v.ExcelSheetId] = v
  556. }
  557. for k, v := range sheetList {
  558. sheetData, ok := sheetDataMap[v.ExcelSheetId]
  559. if !ok {
  560. continue
  561. }
  562. v.Data = sheetData
  563. sheetList[k] = v
  564. }
  565. }
  566. // TODO 合并单元格信息、计算公式
  567. }
  568. //resp := response.ExcelListResp{
  569. // Paging: page,
  570. // List: list,
  571. //}
  572. br.Ret = 200
  573. br.Success = true
  574. br.Msg = "获取成功"
  575. br.Data = sheetList
  576. }
  577. // ClassifyList
  578. // @Title 分类列表
  579. // @Description 分类列表
  580. // @Param IsShare query bool false "是否共享表格: true-是; false-否(默认)"
  581. // @Success 200 {object} response.ExcelClassifyListResp
  582. // @router /excel_classify/list [get]
  583. func (c *CustomAnalysisController) ClassifyList() {
  584. br := new(models.BaseResponse).Init()
  585. defer func() {
  586. if br.ErrMsg == "" {
  587. br.IsSendEmail = false
  588. }
  589. c.Data["json"] = br
  590. c.ServeJSON()
  591. }()
  592. sysUser := c.SysUser
  593. if sysUser == nil {
  594. br.Msg = "请登录"
  595. br.ErrMsg = "请登录,SysUser Is Empty"
  596. br.Ret = 408
  597. return
  598. }
  599. source := utils.CUSTOM_ANALYSIS_TABLE
  600. isShare, _ := c.GetBool("IsShare")
  601. resp := new(response.ExcelClassifyListResp)
  602. // 查询我分享的/分享给我的表格
  603. excels, e := excelPermissionModel.GetAdminAuthExcelInfoPermission(source, sysUser.AdminId, "")
  604. if e != nil {
  605. br.Msg = "获取失败"
  606. br.ErrMsg = fmt.Sprintf("获取我分享的/分享给我的表格失败, %v", e)
  607. return
  608. }
  609. hasShareMap := make(map[int]bool) // 是否已分享标记
  610. for _, v := range excels {
  611. if v.CreateUserId == sysUser.AdminId {
  612. hasShareMap[int(v.ExcelInfoId)] = true
  613. }
  614. }
  615. // 共享
  616. if isShare {
  617. shareExcel := new(excelModel.ExcelClassifyItems)
  618. shareExcel.ExcelClassifyId = excelModel.CustomAnalysisMenuShareId
  619. shareExcel.ExcelClassifyName = "我共享的"
  620. shareExcel.Level = 1
  621. shareExcel.Sort = 1
  622. shareExcel.UniqueCode = fmt.Sprintf("SHARE%d", shareExcel.ExcelClassifyId)
  623. shareExcel.HaveOperaAuth = true
  624. shareExcel.Children = make([]*excelModel.ExcelClassifyItems, 0)
  625. sharedExcel := new(excelModel.ExcelClassifyItems)
  626. sharedExcel.ExcelClassifyId = excelModel.CustomAnalysisMenuSharedId
  627. sharedExcel.ExcelClassifyName = "收到共享"
  628. sharedExcel.Level = 1
  629. sharedExcel.Sort = 2
  630. sharedExcel.UniqueCode = fmt.Sprintf("SHARED%d", sharedExcel.ExcelClassifyId)
  631. sharedExcel.HaveOperaAuth = true
  632. sharedExcel.Children = make([]*excelModel.ExcelClassifyItems, 0)
  633. // 收到共享细分目录
  634. sharedUserClassify := make(map[int]*excelModel.ExcelClassifyItems)
  635. adminIdName := make(map[int]string)
  636. {
  637. adminOb := new(system.Admin)
  638. adminList, e := adminOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  639. if e != nil {
  640. br.Msg = "获取失败"
  641. br.ErrMsg = fmt.Sprintf("获取用户列表失败, %v", e)
  642. return
  643. }
  644. for _, v := range adminList {
  645. adminIdName[v.AdminId] = v.RealName
  646. }
  647. }
  648. sortUser := make([]int, 0) // 分享人目录排序
  649. shareExists := make(map[int]bool)
  650. for _, v := range excels {
  651. item := new(excelModel.ExcelClassifyItems)
  652. item.ExcelInfoId = int(v.ExcelInfoId)
  653. item.ExcelClassifyName = v.ExcelName
  654. item.UniqueCode = v.UniqueCode
  655. item.HaveOperaAuth = true
  656. item.HasShare = hasShareMap[int(v.ExcelInfoId)]
  657. // 我共享的
  658. if v.CreateUserId == sysUser.AdminId && !shareExists[int(v.ExcelInfoId)] {
  659. shareExists[int(v.ExcelInfoId)] = true
  660. item.Level = 2
  661. item.ParentId = shareExcel.ExcelClassifyId
  662. item.ShowShareBtn = true
  663. shareExcel.Children = append(shareExcel.Children, item)
  664. continue
  665. }
  666. // 收到共享
  667. if int(v.SysUserId) == sysUser.AdminId {
  668. if adminIdName[v.CreateUserId] == "" {
  669. // 分享人用户不存在, 那么不显示该分类
  670. continue
  671. }
  672. if !utils.InArrayByInt(sortUser, v.CreateUserId) {
  673. sortUser = append(sortUser, v.CreateUserId)
  674. }
  675. if sharedUserClassify[v.CreateUserId] == nil {
  676. us := new(excelModel.ExcelClassifyItems)
  677. us.ExcelClassifyId = v.CreateUserId // 用户ID作为分类ID
  678. us.ExcelClassifyName = fmt.Sprintf("%s的表格", adminIdName[v.CreateUserId])
  679. us.Level = 2
  680. us.UniqueCode = fmt.Sprintf("%d-%d", sharedExcel.ExcelClassifyId, v.CreateUserId)
  681. us.HaveOperaAuth = true
  682. us.ShowShareBtn = false
  683. us.Children = make([]*excelModel.ExcelClassifyItems, 0)
  684. sharedUserClassify[v.CreateUserId] = us
  685. }
  686. item.Level = 3
  687. item.ParentId = sharedUserClassify[v.CreateUserId].ExcelClassifyId
  688. sharedUserClassify[v.CreateUserId].Children = append(sharedUserClassify[v.CreateUserId].Children, item)
  689. }
  690. }
  691. // 按照分享人第一次分享时间降序=_=!, 查询的时候是升序, 这里反过来
  692. for i, j := 0, len(sortUser)-1; i < j; i, j = i+1, j-1 {
  693. sortUser[i], sortUser[j] = sortUser[j], sortUser[i]
  694. }
  695. for _, v := range sortUser {
  696. if sharedUserClassify[v] != nil {
  697. sharedExcel.Children = append(sharedExcel.Children, sharedUserClassify[v])
  698. }
  699. }
  700. resp.AllNodes = make([]*excelModel.ExcelClassifyItems, 0)
  701. resp.AllNodes = append(resp.AllNodes, shareExcel, sharedExcel)
  702. br.Ret = 200
  703. br.Success = true
  704. br.Msg = "获取成功"
  705. br.Data = resp
  706. return
  707. }
  708. // 我的表格
  709. classifyList, err := excelModel.GetAdminExcelClassifyBySource(source, sysUser.AdminId)
  710. if err != nil {
  711. br.Msg = "获取失败"
  712. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  713. return
  714. }
  715. classifyMap := make(map[int]*excelModel.ExcelClassifyItems)
  716. for _, v := range classifyList {
  717. v.HaveOperaAuth = true
  718. classifyMap[v.ExcelClassifyId] = v
  719. }
  720. allExcelInfo, err := excelModel.GetNoContentExcelInfoAll(source, sysUser.AdminId)
  721. if err != nil && err.Error() != utils.ErrNoRow() {
  722. br.Msg = "获取失败"
  723. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  724. return
  725. }
  726. excelInfoMap := make(map[int][]*excelModel.ExcelClassifyItems)
  727. for _, v := range allExcelInfo {
  728. v.HaveOperaAuth = true
  729. v.ShowShareBtn = true
  730. v.HasShare = hasShareMap[v.ExcelInfoId]
  731. excelInfoMap[v.ExcelClassifyId] = append(excelInfoMap[v.ExcelClassifyId], v)
  732. }
  733. classifyListMap := make(map[int][]*excelModel.ExcelClassifyItems)
  734. for _, v := range classifyList {
  735. if v.ParentId > 0 {
  736. classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v)
  737. }
  738. if existItems, ok := excelInfoMap[v.ExcelClassifyId]; ok {
  739. v.Children = existItems
  740. }
  741. }
  742. for key, classify := range classifyList {
  743. subList, ok := classifyListMap[classify.ExcelClassifyId]
  744. if ok {
  745. classifyList[key].Children = append(classifyList[key].Children, subList...)
  746. sort.Slice(classifyList[key].Children, func(i, j int) bool {
  747. return excelModel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j])
  748. })
  749. }
  750. }
  751. nodeAll := make([]*excelModel.ExcelClassifyItems, 0)
  752. for _, v := range classifyList {
  753. if v.ParentId == 0 {
  754. sort.Slice(v.Children, func(i, j int) bool {
  755. return excelModel.ExcelClassifyItemBySort(v.Children[i], v.Children[j])
  756. })
  757. nodeAll = append(nodeAll, v)
  758. }
  759. }
  760. resp.AllNodes = nodeAll
  761. br.Ret = 200
  762. br.Success = true
  763. br.Msg = "获取成功"
  764. br.Data = resp
  765. }
  766. // List
  767. // @Title 自定义分析列表
  768. // @Description ETA表格列表接口
  769. // @Param PageSize query int true "每页数据条数"
  770. // @Param CurrentIndex query int true "当前页页码,从1开始"
  771. // @Param ExcelClassifyId query int true "分类id"
  772. // @Param Keyword query string true "搜索关键词"
  773. // @Param IsShare query bool false "是否共享表格: true-是; false-否(默认)"
  774. // @Success 200 {object} response.ExcelListResp
  775. // @router /excel/list [get]
  776. func (c *CustomAnalysisController) List() {
  777. br := new(models.BaseResponse).Init()
  778. defer func() {
  779. if br.ErrMsg == "" {
  780. br.IsSendEmail = false
  781. }
  782. c.Data["json"] = br
  783. c.ServeJSON()
  784. }()
  785. sysUser := c.SysUser
  786. if sysUser == nil {
  787. br.Msg = "请登录"
  788. br.ErrMsg = "请登录,SysUser Is Empty"
  789. br.Ret = 408
  790. return
  791. }
  792. excelClassifyId, _ := c.GetInt("ExcelClassifyId")
  793. pageSize, _ := c.GetInt("PageSize")
  794. currentIndex, _ := c.GetInt("CurrentIndex")
  795. keyword := c.GetString("Keyword")
  796. keyword = strings.TrimSpace(keyword)
  797. adminId := sysUser.AdminId
  798. isShare, _ := c.GetBool("IsShare")
  799. source := utils.CUSTOM_ANALYSIS_TABLE
  800. var condition string
  801. var pars []interface{}
  802. condition += " AND source = ?"
  803. pars = append(pars, source)
  804. // 共享的
  805. //hasShareMap := make(map[int]bool)
  806. if isShare {
  807. if keyword != "" {
  808. keyword = fmt.Sprint("%", keyword, "%")
  809. }
  810. // 查询我分享的/分享给我的表格
  811. excels, e := excelPermissionModel.GetAdminAuthExcelInfoPermission(source, adminId, keyword)
  812. if e != nil {
  813. br.Msg = "获取失败"
  814. br.ErrMsg = fmt.Sprintf("获取我分享的/分享给我的表格失败, %v", e)
  815. return
  816. }
  817. var excelIds, shareIds, sharedIds, userIds []int
  818. for _, v := range excels {
  819. id := int(v.ExcelInfoId)
  820. // 无分类ID
  821. if excelClassifyId <= 0 && !utils.InArrayByInt(excelIds, id) {
  822. excelIds = append(excelIds, id)
  823. continue
  824. }
  825. // 用户分类
  826. if excelClassifyId == v.CreateUserId {
  827. userIds = append(userIds, id)
  828. continue
  829. }
  830. // 我共享的
  831. if excelClassifyId == excelModel.CustomAnalysisMenuShareId && v.CreateUserId == sysUser.AdminId {
  832. shareIds = append(shareIds, id)
  833. //hasShareMap[id] = true
  834. continue
  835. }
  836. // 收到共享
  837. if excelClassifyId == excelModel.CustomAnalysisMenuSharedId && int(v.SysUserId) == sysUser.AdminId {
  838. sharedIds = append(sharedIds, id)
  839. continue
  840. }
  841. }
  842. if excelClassifyId > 0 {
  843. switch excelClassifyId {
  844. case excelModel.CustomAnalysisMenuShareId: // 我共享的
  845. excelIds = shareIds
  846. case excelModel.CustomAnalysisMenuSharedId: // 收到共享
  847. excelIds = sharedIds
  848. default: // 用户目录
  849. excelIds = userIds
  850. }
  851. }
  852. if len(excelIds) == 0 {
  853. page := paging.GetPaging(currentIndex, pageSize, 0)
  854. resp := response.ExcelListResp{
  855. Paging: page,
  856. List: make([]*excelModel.MyExcelInfoList, 0),
  857. }
  858. br.Ret = 200
  859. br.Success = true
  860. br.Msg = "获取成功"
  861. br.Data = resp
  862. return
  863. }
  864. condition += fmt.Sprintf(` AND excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  865. pars = append(pars, excelIds)
  866. } else {
  867. condition += ` AND sys_user_id = ?`
  868. pars = append(pars, adminId)
  869. }
  870. // 筛选分类
  871. if !isShare && excelClassifyId > 0 {
  872. _, err := excelModel.GetExcelClassifyById(excelClassifyId)
  873. if err != nil && err.Error() != utils.ErrNoRow() {
  874. br.Msg = "获取表格信息失败"
  875. br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
  876. return
  877. }
  878. childClassify, e, _ := excel2.GetChildClassifyByClassifyId(excelClassifyId, source)
  879. if e != nil && e.Error() != utils.ErrNoRow() {
  880. br.Msg = "获取失败"
  881. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  882. return
  883. }
  884. if len(childClassify) == 0 {
  885. condition += " AND excel_classify_id = ? "
  886. pars = append(pars, excelClassifyId)
  887. } else {
  888. classifyIds := []int{excelClassifyId}
  889. for _, v := range childClassify {
  890. classifyIds = append(classifyIds, v.ExcelClassifyId)
  891. }
  892. condition += fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  893. pars = append(pars, classifyIds)
  894. }
  895. }
  896. if keyword != "" {
  897. condition += ` AND (excel_name LIKE ?)`
  898. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  899. }
  900. // 获取表格信息
  901. var total int
  902. page := paging.GetPaging(currentIndex, pageSize, total)
  903. var startSize int
  904. if pageSize <= 0 {
  905. pageSize = utils.PageSize20
  906. }
  907. if currentIndex <= 0 {
  908. currentIndex = 1
  909. }
  910. startSize = paging.StartIndex(currentIndex, pageSize)
  911. list, err := excelModel.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
  912. if err != nil && err.Error() != utils.ErrNoRow() {
  913. br.Success = true
  914. br.Msg = "获取表格信息失败"
  915. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  916. return
  917. }
  918. for _, v := range list {
  919. v.HaveOperaAuth = true
  920. //v.Button.CancelShareButton = hasShareMap[v.ExcelInfoId]
  921. }
  922. // 总数据量
  923. dataCount, err := excelModel.GetExcelListCountByCondition(condition, pars)
  924. if err != nil && err.Error() != utils.ErrNoRow() {
  925. br.Msg = "获取表格列表信息失败"
  926. br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
  927. return
  928. }
  929. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  930. resp := response.ExcelListResp{
  931. Paging: page,
  932. List: list,
  933. }
  934. br.Ret = 200
  935. br.Success = true
  936. br.Msg = "获取成功"
  937. br.Data = resp
  938. }
  939. // Share
  940. // @Title 分享表格
  941. // @Description 分享表格
  942. // @Param request body request.ShareExcelInfoReq true "type json string"
  943. // @Success 200 string "操作成功"
  944. // @router /excel/share [post]
  945. func (c *CustomAnalysisController) Share() {
  946. br := new(models.BaseResponse).Init()
  947. defer func() {
  948. if br.ErrMsg != "" {
  949. br.IsSendEmail = false
  950. }
  951. c.Data["json"] = br
  952. c.ServeJSON()
  953. }()
  954. var req request.ShareExcelInfoReq
  955. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  956. if err != nil {
  957. br.Msg = "参数解析异常!"
  958. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  959. return
  960. }
  961. sysUser := c.SysUser
  962. if sysUser == nil {
  963. br.Msg = "请登录"
  964. br.ErrMsg = "请登录,SysUser Is Empty"
  965. br.Ret = 408
  966. return
  967. }
  968. if req.ExcelInfoId <= 0 {
  969. br.Msg = "请选择表格"
  970. return
  971. }
  972. excelInfo, e := excelModel.GetExcelInfoById(req.ExcelInfoId)
  973. if e != nil {
  974. if e.Error() == utils.ErrNoRow() {
  975. br.Msg = "表格不存在, 请刷新页面"
  976. return
  977. }
  978. br.Msg = "操作失败"
  979. br.ErrMsg = fmt.Sprintf("获取表格失败, %v", e)
  980. return
  981. }
  982. if excelInfo.SysUserId != sysUser.AdminId {
  983. br.Msg = "无权操作"
  984. br.ErrMsg = fmt.Sprintf("无权设置表格权限, ExcelInfoId: %d, SysAdminId: %d", req.ExcelInfoId, sysUser.AdminId)
  985. return
  986. }
  987. source := utils.CUSTOM_ANALYSIS_TABLE
  988. newPermissions := make([]*excelPermissionModel.ExcelInfoPermission, 0)
  989. for _, v := range req.ViewUserIds {
  990. newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
  991. ExcelInfoId: int32(req.ExcelInfoId),
  992. Source: int32(source),
  993. SysUserId: int32(v),
  994. CreateTime: time.Now().Local(),
  995. ModifyTime: time.Now().Local(),
  996. PermissionType: 1,
  997. })
  998. }
  999. for _, v := range req.EditUserIds {
  1000. newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
  1001. ExcelInfoId: int32(req.ExcelInfoId),
  1002. Source: int32(source),
  1003. SysUserId: int32(v),
  1004. CreateTime: time.Now().Local(),
  1005. ModifyTime: time.Now().Local(),
  1006. PermissionType: 2,
  1007. })
  1008. }
  1009. if e := excelPermissionModel.ClearAndSetExcelInfoPermission(source, req.ExcelInfoId, newPermissions); e != nil {
  1010. br.Msg = "操作失败"
  1011. br.ErrMsg = fmt.Sprintf("设置表格权限失败, %v", e)
  1012. return
  1013. }
  1014. br.Ret = 200
  1015. br.Success = true
  1016. br.Msg = "操作成功"
  1017. }
  1018. // ShareDetail
  1019. // @Title 分享表格-详情
  1020. // @Description 分享表格-详情
  1021. // @Param ExcelInfoId query int true "表格ID"
  1022. // @Success 200 {object} response.ShareExcelInfoDetail
  1023. // @router /excel/share_detail [get]
  1024. func (c *CustomAnalysisController) ShareDetail() {
  1025. br := new(models.BaseResponse).Init()
  1026. defer func() {
  1027. if br.ErrMsg != "" {
  1028. br.IsSendEmail = false
  1029. }
  1030. c.Data["json"] = br
  1031. c.ServeJSON()
  1032. }()
  1033. sysUser := c.SysUser
  1034. if sysUser == nil {
  1035. br.Msg = "请登录"
  1036. br.ErrMsg = "请登录,SysUser Is Empty"
  1037. br.Ret = 408
  1038. return
  1039. }
  1040. excelInfoId, _ := c.GetInt("ExcelInfoId")
  1041. if excelInfoId <= 0 {
  1042. br.Msg = "请选择表格"
  1043. return
  1044. }
  1045. permissions, e := excelPermissionModel.GetExcelPermissionBySourceAndId(excelInfoId, utils.CUSTOM_ANALYSIS_TABLE)
  1046. if e != nil {
  1047. br.Msg = "获取失败"
  1048. br.ErrMsg = fmt.Sprintf("获取表格权限失败, %v", e)
  1049. return
  1050. }
  1051. resp := new(response.ShareExcelInfoDetail)
  1052. resp.ExcelInfoId = excelInfoId
  1053. for _, v := range permissions {
  1054. if v.PermissionType == 1 {
  1055. resp.ViewUserIds = append(resp.ViewUserIds, int(v.SysUserId))
  1056. }
  1057. if v.PermissionType == 2 {
  1058. resp.EditUserIds = append(resp.EditUserIds, int(v.SysUserId))
  1059. }
  1060. }
  1061. br.Data = resp
  1062. br.Ret = 200
  1063. br.Success = true
  1064. br.Msg = "获取成功"
  1065. }