custom_analysis.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. excelModel "eta/eta_api/models/data_manage/excel"
  7. "eta/eta_api/models/data_manage/excel/request"
  8. "eta/eta_api/models/data_manage/excel/response"
  9. "eta/eta_api/services"
  10. "eta/eta_api/services/data/data_manage_permission"
  11. "eta/eta_api/services/data/excel"
  12. "eta/eta_api/utils"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // CustomAnalysisController 自定义分析
  18. type CustomAnalysisController struct {
  19. controllers.BaseAuthController
  20. }
  21. // ExcelByName
  22. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  23. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  24. // @Param ExcelName query string true "搜索关键词"
  25. // @Success 200 {object} response.ExcelListResp
  26. // @router /excel_by_name [get]
  27. func (c *CustomAnalysisController) ExcelByName() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. c.Data["json"] = br
  31. c.ServeJSON()
  32. }()
  33. sysUser := c.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser Is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. excelName := c.GetString("ExcelName")
  41. if excelName == `` {
  42. br.Msg = "请选择表格"
  43. br.ErrMsg = "ExcelName未传"
  44. br.IsSendEmail = false
  45. return
  46. }
  47. resp := response.FindExcelInfoResp{}
  48. excelName = utils.TrimLRStr(excelName)
  49. // 获取数据详情
  50. excelDetail, err := excelModel.GetNoContentExcelInfoByName(excelName, utils.CUSTOM_ANALYSIS_TABLE)
  51. if err != nil {
  52. if err.Error() == utils.ErrNoRow() {
  53. br.Ret = 200
  54. br.Success = true
  55. br.Msg = "获取成功"
  56. br.Data = resp
  57. return
  58. }
  59. br.Msg = "获取表格事变"
  60. br.ErrMsg = err.Error()
  61. return
  62. }
  63. // 数据权限
  64. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelDetail.ExcelInfoId, excelDetail.ExcelClassifyId, excelDetail.IsJoinPermission, c.SysUser.AdminId)
  65. if err != nil {
  66. br.Msg = "获取ETA表格失败"
  67. br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  68. return
  69. }
  70. resp.IsFind = true
  71. resp.ExcelInfo = response.FindExcelInfo{
  72. ExcelInfoId: excelDetail.ExcelInfoId,
  73. Source: excelDetail.Source,
  74. ExcelType: excelDetail.ExcelType,
  75. ExcelName: excelDetail.ExcelName,
  76. UniqueCode: excelDetail.UniqueCode,
  77. ExcelClassifyId: excelDetail.ExcelClassifyId,
  78. SysUserId: excelDetail.SysUserId,
  79. SysUserRealName: excelDetail.SysUserRealName,
  80. ExcelImage: excelDetail.ExcelImage,
  81. FileUrl: excelDetail.FileUrl,
  82. Sort: excelDetail.Sort,
  83. ModifyTime: excelDetail.ModifyTime,
  84. CreateTime: excelDetail.CreateTime,
  85. Button: excel.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, haveOperaAuth),
  86. }
  87. if excelDetail != nil {
  88. sheetList, err := excelModel.GetAllSheetItemList(excelDetail.ExcelInfoId)
  89. if err != nil {
  90. br.Msg = "获取sheet失败"
  91. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  92. return
  93. }
  94. resp.SheetList = sheetList
  95. }
  96. //resp := response.ExcelListResp{
  97. // Paging: page,
  98. // List: list,
  99. //}
  100. br.Ret = 200
  101. br.Success = true
  102. br.Msg = "获取成功"
  103. br.Data = resp
  104. }
  105. // Add
  106. // @Title 新增表格接口
  107. // @Description 新增表格接口
  108. // @Param request body request.AddExcelInfoReq true "type json string"
  109. // @Success 200 {object} response.AddExcelInfoResp
  110. // @router /add [post]
  111. func (c *CustomAnalysisController) Add() {
  112. br := new(models.BaseResponse).Init()
  113. defer func() {
  114. c.Data["json"] = br
  115. c.ServeJSON()
  116. }()
  117. sysUser := c.SysUser
  118. if sysUser == nil {
  119. br.Msg = "请登录"
  120. br.ErrMsg = "请登录,SysUser Is Empty"
  121. br.Ret = 408
  122. return
  123. }
  124. deleteCache := true
  125. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  126. defer func() {
  127. if deleteCache {
  128. _ = utils.Rc.Delete(cacheKey)
  129. }
  130. }()
  131. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  132. deleteCache = false
  133. br.Msg = "系统处理中,请稍后重试!"
  134. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  135. return
  136. }
  137. var req request.AddExcelInfoReq
  138. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  139. if err != nil {
  140. br.Msg = "参数解析异常!"
  141. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  142. return
  143. }
  144. req.ExcelName = strings.Trim(req.ExcelName, " ")
  145. if req.ExcelName == "" {
  146. br.Msg = "请填写表格名称!"
  147. br.IsSendEmail = false
  148. return
  149. }
  150. // 获取是否存在该表格名称
  151. {
  152. var condition string
  153. var pars []interface{}
  154. condition += " AND source=? "
  155. pars = append(pars, utils.CUSTOM_ANALYSIS_TABLE)
  156. condition += " AND excel_name=? "
  157. pars = append(pars, req.ExcelName)
  158. count, err := excelModel.GetExcelInfoCountByCondition(condition, pars)
  159. if err != nil {
  160. br.Msg = "判断表格名称是否存在失败"
  161. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  162. return
  163. }
  164. if count > 0 {
  165. br.Msg = "表格名称已存在,请重新填写表格名称"
  166. br.IsSendEmail = false
  167. return
  168. }
  169. }
  170. if req.ExcelClassifyId <= 0 {
  171. br.Msg = "分类参数错误!"
  172. br.IsSendEmail = false
  173. return
  174. }
  175. excelInfo, err, errMsg, isSendEmail := excel.AddCustomAnalysisTable(utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, sysUser)
  176. if err != nil {
  177. br.Msg = "保存失败"
  178. if errMsg != `` {
  179. br.Msg = errMsg
  180. }
  181. br.ErrMsg = "保存失败,Err:" + err.Error()
  182. br.IsSendEmail = isSendEmail
  183. return
  184. }
  185. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  186. //if req.Source == 1 {
  187. // go UpdateExcelInfoFileUrl(excelInfo)
  188. //}
  189. //
  190. resp := new(response.AddExcelInfoResp)
  191. resp.ExcelInfoId = excelInfo.ExcelInfoId
  192. resp.UniqueCode = excelInfo.UniqueCode
  193. // 生成excel文件
  194. go excel.UpdateExcelInfoFileUrl(excelInfo)
  195. //新增操作日志
  196. //{
  197. // excelLog := &data_manage.ExcelInfoLog{
  198. // //ExcelInfoLogId: 0,
  199. // ExcelInfoId: excelInfo.ExcelInfoId,
  200. // ExcelName: req.ExcelName,
  201. // ExcelClassifyId: req.ExcelClassifyId,
  202. // SysUserId: sysUser.AdminId,
  203. // SysUserRealName: sysUser.RealName,
  204. // UniqueCode: excelInfo.UniqueCode,
  205. // CreateTime: time.Now(),
  206. // Content: string(c.Ctx.Input.RequestBody),
  207. // Status: "新增表格",
  208. // Method: c.Ctx.Input.URI(),
  209. // }
  210. // go data_manage.AddExcelInfoLog(excelLog)
  211. //}
  212. br.Ret = 200
  213. br.Success = true
  214. br.Msg = "保存成功"
  215. br.Data = resp
  216. br.IsAddLog = false //数据量太大了,不写入日志吧
  217. }
  218. // Save
  219. // @Title 保存表格接口
  220. // @Description 保存表格接口
  221. // @Param request body request.AddExcelInfoReq true "type json string"
  222. // @Success 200 {object} response.AddExcelInfoResp
  223. // @router /save [post]
  224. func (c *CustomAnalysisController) Save() {
  225. br := new(models.BaseResponse).Init()
  226. defer func() {
  227. c.Data["json"] = br
  228. c.ServeJSON()
  229. }()
  230. sysUser := c.SysUser
  231. if sysUser == nil {
  232. br.Msg = "请登录"
  233. br.ErrMsg = "请登录,SysUser Is Empty"
  234. br.Ret = 408
  235. return
  236. }
  237. deleteCache := true
  238. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  239. defer func() {
  240. if deleteCache {
  241. _ = utils.Rc.Delete(cacheKey)
  242. }
  243. }()
  244. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  245. deleteCache = false
  246. br.Msg = "系统处理中,请稍后重试!"
  247. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  248. return
  249. }
  250. var req request.SaveExcelInfoReq
  251. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  252. if err != nil {
  253. br.Msg = "参数解析异常!"
  254. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  255. return
  256. }
  257. req.ExcelName = strings.Trim(req.ExcelName, " ")
  258. if req.ExcelName == "" {
  259. br.Msg = "请填写表格名称!"
  260. br.IsSendEmail = false
  261. return
  262. }
  263. if req.ExcelInfoId <= 0 {
  264. br.Msg = "请选择excel!"
  265. br.IsSendEmail = false
  266. return
  267. }
  268. if req.ExcelClassifyId <= 0 {
  269. br.Msg = "分类参数错误!"
  270. br.IsSendEmail = false
  271. return
  272. }
  273. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  274. if err != nil {
  275. br.Msg = "找不到该EXCEL!"
  276. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  277. return
  278. }
  279. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  280. br.Msg = "EXCEL异常!"
  281. br.IsSendEmail = false
  282. return
  283. }
  284. err, errMsg, isSendEmail := excel.SaveCustomAnalysisTable(excelInfo, utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, req.OpSheetList)
  285. if err != nil {
  286. br.Msg = "保存失败"
  287. if errMsg != `` {
  288. br.Msg = errMsg
  289. }
  290. br.ErrMsg = "保存失败,Err:" + err.Error()
  291. br.IsSendEmail = isSendEmail
  292. return
  293. }
  294. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  295. //if req.Source == 1 {
  296. // go UpdateExcelInfoFileUrl(excelInfo)
  297. //}
  298. //
  299. resp := new(response.AddExcelInfoResp)
  300. resp.ExcelInfoId = excelInfo.ExcelInfoId
  301. resp.UniqueCode = excelInfo.UniqueCode
  302. // 生成excel文件
  303. go excel.UpdateExcelInfoFileUrl(excelInfo)
  304. //新增操作日志
  305. //{
  306. // excelLog := &data_manage.ExcelInfoLog{
  307. // //ExcelInfoLogId: 0,
  308. // ExcelInfoId: excelInfo.ExcelInfoId,
  309. // ExcelName: req.ExcelName,
  310. // ExcelClassifyId: req.ExcelClassifyId,
  311. // SysUserId: sysUser.AdminId,
  312. // SysUserRealName: sysUser.RealName,
  313. // UniqueCode: excelInfo.UniqueCode,
  314. // CreateTime: time.Now(),
  315. // Content: string(c.Ctx.Input.RequestBody),
  316. // Status: "新增表格",
  317. // Method: c.Ctx.Input.URI(),
  318. // }
  319. // go data_manage.AddExcelInfoLog(excelLog)
  320. //}
  321. br.Ret = 200
  322. br.Success = true
  323. br.Msg = "保存成功"
  324. br.Data = resp
  325. br.IsAddLog = false //数据量太大了,不写入日志吧
  326. }
  327. // BaseExcelDetail
  328. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  329. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  330. // @Param UniqueCode query string true "excel唯一编码"
  331. // @Success 200 {object} response.ExcelListResp
  332. // @router /excel/base [get]
  333. func (c *CustomAnalysisController) BaseExcelDetail() {
  334. br := new(models.BaseResponse).Init()
  335. defer func() {
  336. c.Data["json"] = br
  337. c.ServeJSON()
  338. }()
  339. sysUser := c.SysUser
  340. if sysUser == nil {
  341. br.Msg = "请登录"
  342. br.ErrMsg = "请登录,SysUser Is Empty"
  343. br.Ret = 408
  344. return
  345. }
  346. uniqueCode := c.GetString("UniqueCode")
  347. if uniqueCode == `` {
  348. br.Msg = "请选择表格"
  349. br.ErrMsg = "UniqueCode未传"
  350. br.IsSendEmail = false
  351. return
  352. }
  353. resp := response.FindExcelInfoResp{}
  354. // 获取数据详情
  355. excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
  356. if err != nil {
  357. if err.Error() == utils.ErrNoRow() {
  358. br.Ret = 200
  359. br.Success = true
  360. br.Msg = "获取成功"
  361. br.Data = resp
  362. return
  363. }
  364. br.Msg = "获取表格事变"
  365. br.ErrMsg = err.Error()
  366. return
  367. }
  368. // 编辑状态
  369. markStatus, err := services.UpdateExcelEditMark(excelDetail.ExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
  370. if err != nil {
  371. br.Msg = "查询标记状态失败"
  372. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  373. return
  374. }
  375. // 数据权限
  376. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelDetail.ExcelInfoId, excelDetail.ExcelClassifyId, excelDetail.IsJoinPermission, c.SysUser.AdminId)
  377. if err != nil {
  378. br.Msg = "获取ETA表格失败"
  379. br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  380. return
  381. }
  382. resp.IsFind = true
  383. resp.ExcelInfo = response.FindExcelInfo{
  384. ExcelInfoId: excelDetail.ExcelInfoId,
  385. Source: excelDetail.Source,
  386. ExcelType: excelDetail.ExcelType,
  387. ExcelName: excelDetail.ExcelName,
  388. UniqueCode: excelDetail.UniqueCode,
  389. ExcelClassifyId: excelDetail.ExcelClassifyId,
  390. SysUserId: excelDetail.SysUserId,
  391. SysUserRealName: excelDetail.SysUserRealName,
  392. ExcelImage: excelDetail.ExcelImage,
  393. FileUrl: excelDetail.FileUrl,
  394. Sort: excelDetail.Sort,
  395. ModifyTime: excelDetail.ModifyTime,
  396. CreateTime: excelDetail.CreateTime,
  397. Button: excel.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, haveOperaAuth),
  398. }
  399. if markStatus.Status == 0 {
  400. resp.ExcelInfo.CanEdit = true
  401. } else {
  402. resp.ExcelInfo.Editor = markStatus.Editor
  403. }
  404. if excelDetail != nil {
  405. sheetList, err := excelModel.GetAllSheetItemList(excelDetail.ExcelInfoId)
  406. if err != nil {
  407. br.Msg = "获取sheet失败"
  408. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  409. return
  410. }
  411. if len(sheetList) > 0 {
  412. sheetIdList := make([]int, 0)
  413. for _, v := range sheetList {
  414. sheetIdList = append(sheetIdList, v.ExcelSheetId)
  415. }
  416. // 获取所有sheet的第一页的数据
  417. sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, 1)
  418. if err != nil {
  419. br.Msg = "获取sheet中的数据失败"
  420. br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
  421. return
  422. }
  423. sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
  424. for _, v := range sheetDataList {
  425. sheetDataMap[v.ExcelSheetId] = v
  426. }
  427. for k, v := range sheetList {
  428. sheetData, ok := sheetDataMap[v.ExcelSheetId]
  429. if !ok {
  430. continue
  431. }
  432. v.Data = sheetData
  433. sheetList[k] = v
  434. }
  435. }
  436. // TODO 合并单元格信息、计算公式
  437. resp.SheetList = sheetList
  438. }
  439. //resp := response.ExcelListResp{
  440. // Paging: page,
  441. // List: list,
  442. //}
  443. br.Ret = 200
  444. br.Success = true
  445. br.Msg = "获取成功"
  446. br.Data = resp
  447. }
  448. // ExcelDataList
  449. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  450. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  451. // @Param UniqueCode query string true "excel唯一编码"
  452. // @Param Page query int true "页码"
  453. // @Success 200 {object} response.ExcelListResp
  454. // @router /excel/data [get]
  455. func (c *CustomAnalysisController) ExcelDataList() {
  456. br := new(models.BaseResponse).Init()
  457. defer func() {
  458. c.Data["json"] = br
  459. c.ServeJSON()
  460. }()
  461. sysUser := c.SysUser
  462. if sysUser == nil {
  463. br.Msg = "请登录"
  464. br.ErrMsg = "请登录,SysUser Is Empty"
  465. br.Ret = 408
  466. return
  467. }
  468. uniqueCode := c.GetString("UniqueCode")
  469. if uniqueCode == `` {
  470. br.Msg = "请选择表格"
  471. br.ErrMsg = "UniqueCode未传"
  472. br.IsSendEmail = false
  473. return
  474. }
  475. page, _ := c.GetInt("Page")
  476. if page <= 0 {
  477. br.Msg = "页码异常"
  478. br.ErrMsg = "页码异常"
  479. br.IsSendEmail = false
  480. return
  481. }
  482. sheetList := make([]*excelModel.SheetItem, 0)
  483. // 获取数据详情
  484. excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
  485. if err != nil {
  486. if err.Error() == utils.ErrNoRow() {
  487. br.Ret = 200
  488. br.Success = true
  489. br.Msg = "获取成功"
  490. br.Data = sheetList
  491. return
  492. }
  493. br.Msg = "获取表格事变"
  494. br.ErrMsg = err.Error()
  495. return
  496. }
  497. if excelDetail.Source != utils.CUSTOM_ANALYSIS_TABLE {
  498. br.Msg = "excel异常"
  499. br.ErrMsg = "excel异常"
  500. br.IsSendEmail = false
  501. return
  502. }
  503. if excelDetail != nil {
  504. sheetList, err = excelModel.GetAllNoConfigSheetItemList(excelDetail.ExcelInfoId)
  505. if err != nil {
  506. br.Msg = "获取sheet失败"
  507. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  508. return
  509. }
  510. if len(sheetList) > 0 {
  511. sheetIdList := make([]int, 0)
  512. for _, v := range sheetList {
  513. sheetIdList = append(sheetIdList, v.ExcelSheetId)
  514. }
  515. // 获取所有sheet的第一页的数据
  516. sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, page)
  517. if err != nil {
  518. br.Msg = "获取sheet中的数据失败"
  519. br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
  520. return
  521. }
  522. sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
  523. for _, v := range sheetDataList {
  524. sheetDataMap[v.ExcelSheetId] = v
  525. }
  526. for k, v := range sheetList {
  527. sheetData, ok := sheetDataMap[v.ExcelSheetId]
  528. if !ok {
  529. continue
  530. }
  531. v.Data = sheetData
  532. sheetList[k] = v
  533. }
  534. }
  535. // TODO 合并单元格信息、计算公式
  536. }
  537. //resp := response.ExcelListResp{
  538. // Paging: page,
  539. // List: list,
  540. //}
  541. br.Ret = 200
  542. br.Success = true
  543. br.Msg = "获取成功"
  544. br.Data = sheetList
  545. }