custom_analysis.go 15 KB

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