custom_analysis.go 15 KB

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