custom_analysis.go 32 KB

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