custom_analysis.go 16 KB

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