custom_analysis.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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)
  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. count, err := excelModel.GetExcelInfoCountByCondition(condition, pars)
  166. if err != nil {
  167. br.Msg = "判断表格名称是否存在失败"
  168. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  169. return
  170. }
  171. if count > 0 {
  172. br.Msg = "表格名称已存在,请重新填写表格名称"
  173. br.IsSendEmail = false
  174. return
  175. }
  176. }
  177. if req.ExcelClassifyId <= 0 {
  178. br.Msg = "分类参数错误!"
  179. br.IsSendEmail = false
  180. return
  181. }
  182. excelInfo, err, errMsg, isSendEmail := excel.AddCustomAnalysisTable(utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, sysUser)
  183. if err != nil {
  184. br.Msg = "保存失败"
  185. if errMsg != `` {
  186. br.Msg = errMsg
  187. }
  188. br.ErrMsg = "保存失败,Err:" + err.Error()
  189. br.IsSendEmail = isSendEmail
  190. return
  191. }
  192. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  193. //if req.Source == 1 {
  194. // go UpdateExcelInfoFileUrl(excelInfo)
  195. //}
  196. //
  197. resp := new(response.AddExcelInfoResp)
  198. resp.ExcelInfoId = excelInfo.ExcelInfoId
  199. resp.UniqueCode = excelInfo.UniqueCode
  200. // 生成excel文件
  201. go excel.UpdateExcelInfoFileUrl(excelInfo)
  202. //新增操作日志
  203. //{
  204. // excelLog := &data_manage.ExcelInfoLog{
  205. // //ExcelInfoLogId: 0,
  206. // ExcelInfoId: excelInfo.ExcelInfoId,
  207. // ExcelName: req.ExcelName,
  208. // ExcelClassifyId: req.ExcelClassifyId,
  209. // SysUserId: sysUser.AdminId,
  210. // SysUserRealName: sysUser.RealName,
  211. // UniqueCode: excelInfo.UniqueCode,
  212. // CreateTime: time.Now(),
  213. // Content: string(c.Ctx.Input.RequestBody),
  214. // Status: "新增表格",
  215. // Method: c.Ctx.Input.URI(),
  216. // }
  217. // go data_manage.AddExcelInfoLog(excelLog)
  218. //}
  219. br.Ret = 200
  220. br.Success = true
  221. br.Msg = "保存成功"
  222. br.Data = resp
  223. br.IsAddLog = false //数据量太大了,不写入日志吧
  224. }
  225. // Save
  226. // @Title 保存表格接口
  227. // @Description 保存表格接口
  228. // @Param request body request.AddExcelInfoReq true "type json string"
  229. // @Success 200 {object} response.AddExcelInfoResp
  230. // @router /save [post]
  231. func (c *CustomAnalysisController) Save() {
  232. br := new(models.BaseResponse).Init()
  233. defer func() {
  234. c.Data["json"] = br
  235. c.ServeJSON()
  236. }()
  237. sysUser := c.SysUser
  238. if sysUser == nil {
  239. br.Msg = "请登录"
  240. br.ErrMsg = "请登录,SysUser Is Empty"
  241. br.Ret = 408
  242. return
  243. }
  244. deleteCache := true
  245. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  246. defer func() {
  247. if deleteCache {
  248. _ = utils.Rc.Delete(cacheKey)
  249. }
  250. }()
  251. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  252. deleteCache = false
  253. br.Msg = "系统处理中,请稍后重试!"
  254. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  255. return
  256. }
  257. var req request.SaveExcelInfoReq
  258. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  259. if err != nil {
  260. br.Msg = "参数解析异常!"
  261. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  262. return
  263. }
  264. req.ExcelName = strings.Trim(req.ExcelName, " ")
  265. if req.ExcelName == "" {
  266. br.Msg = "请填写表格名称!"
  267. br.IsSendEmail = false
  268. return
  269. }
  270. if req.ExcelInfoId <= 0 {
  271. br.Msg = "请选择excel!"
  272. br.IsSendEmail = false
  273. return
  274. }
  275. if req.ExcelClassifyId <= 0 {
  276. br.Msg = "分类参数错误!"
  277. br.IsSendEmail = false
  278. return
  279. }
  280. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  281. if err != nil {
  282. br.Msg = "找不到该EXCEL!"
  283. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  284. return
  285. }
  286. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  287. br.Msg = "EXCEL异常!"
  288. br.IsSendEmail = false
  289. return
  290. }
  291. err, errMsg, isSendEmail := excel.SaveCustomAnalysisTable(excelInfo, utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, req.OpSheetList)
  292. if err != nil {
  293. br.Msg = "保存失败"
  294. if errMsg != `` {
  295. br.Msg = errMsg
  296. }
  297. br.ErrMsg = "保存失败,Err:" + err.Error()
  298. br.IsSendEmail = isSendEmail
  299. return
  300. }
  301. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  302. //if req.Source == 1 {
  303. // go UpdateExcelInfoFileUrl(excelInfo)
  304. //}
  305. //
  306. resp := new(response.AddExcelInfoResp)
  307. resp.ExcelInfoId = excelInfo.ExcelInfoId
  308. resp.UniqueCode = excelInfo.UniqueCode
  309. // 生成excel文件
  310. go excel.UpdateExcelInfoFileUrl(excelInfo)
  311. //新增操作日志
  312. //{
  313. // excelLog := &data_manage.ExcelInfoLog{
  314. // //ExcelInfoLogId: 0,
  315. // ExcelInfoId: excelInfo.ExcelInfoId,
  316. // ExcelName: req.ExcelName,
  317. // ExcelClassifyId: req.ExcelClassifyId,
  318. // SysUserId: sysUser.AdminId,
  319. // SysUserRealName: sysUser.RealName,
  320. // UniqueCode: excelInfo.UniqueCode,
  321. // CreateTime: time.Now(),
  322. // Content: string(c.Ctx.Input.RequestBody),
  323. // Status: "新增表格",
  324. // Method: c.Ctx.Input.URI(),
  325. // }
  326. // go data_manage.AddExcelInfoLog(excelLog)
  327. //}
  328. br.Ret = 200
  329. br.Success = true
  330. br.Msg = "保存成功"
  331. br.Data = resp
  332. br.IsAddLog = false //数据量太大了,不写入日志吧
  333. }
  334. // BaseExcelDetail
  335. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  336. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  337. // @Param UniqueCode query string true "excel唯一编码"
  338. // @Success 200 {object} response.ExcelListResp
  339. // @router /excel/base [get]
  340. func (c *CustomAnalysisController) BaseExcelDetail() {
  341. br := new(models.BaseResponse).Init()
  342. defer func() {
  343. c.Data["json"] = br
  344. c.ServeJSON()
  345. }()
  346. sysUser := c.SysUser
  347. if sysUser == nil {
  348. br.Msg = "请登录"
  349. br.ErrMsg = "请登录,SysUser Is Empty"
  350. br.Ret = 408
  351. return
  352. }
  353. uniqueCode := c.GetString("UniqueCode")
  354. if uniqueCode == `` {
  355. br.Msg = "请选择表格"
  356. br.ErrMsg = "UniqueCode未传"
  357. br.IsSendEmail = false
  358. return
  359. }
  360. resp := response.FindExcelInfoResp{}
  361. // 获取数据详情
  362. excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
  363. if err != nil {
  364. if err.Error() == utils.ErrNoRow() {
  365. br.Ret = 200
  366. br.Success = true
  367. br.Msg = "获取成功"
  368. br.Data = resp
  369. return
  370. }
  371. br.Msg = "获取表格事变"
  372. br.ErrMsg = err.Error()
  373. return
  374. }
  375. // 编辑状态
  376. markStatus, err := services.UpdateExcelEditMark(excelDetail.ExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
  377. if err != nil {
  378. br.Msg = "查询标记状态失败"
  379. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  380. return
  381. }
  382. // 数据权限
  383. //haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelDetail.ExcelInfoId, excelDetail.ExcelClassifyId, excelDetail.IsJoinPermission, c.SysUser.AdminId)
  384. //if err != nil {
  385. // br.Msg = "获取ETA表格失败"
  386. // br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  387. // return
  388. //}
  389. // 自定义分析权限与其他表格略有差别...
  390. permissions, e := excelPermissionModel.GetExcelPermissionByExcelIdAndUserId(excelDetail.ExcelInfoId, sysUser.AdminId)
  391. if e != nil {
  392. br.Msg = "获取失败"
  393. br.ErrMsg = fmt.Sprintf("获取表格权限失败, %v", e)
  394. return
  395. }
  396. var permissionType []int
  397. for _, v := range permissions {
  398. permissionType = append(permissionType, v.PermissionType)
  399. }
  400. opButton := excel.GetCustomAnalysisOpButton(sysUser, excelDetail.SysUserId, permissionType)
  401. resp.IsFind = true
  402. resp.ExcelInfo = response.FindExcelInfo{
  403. ExcelInfoId: excelDetail.ExcelInfoId,
  404. Source: excelDetail.Source,
  405. ExcelType: excelDetail.ExcelType,
  406. ExcelName: excelDetail.ExcelName,
  407. UniqueCode: excelDetail.UniqueCode,
  408. ExcelClassifyId: excelDetail.ExcelClassifyId,
  409. SysUserId: excelDetail.SysUserId,
  410. SysUserRealName: excelDetail.SysUserRealName,
  411. ExcelImage: excelDetail.ExcelImage,
  412. FileUrl: excelDetail.FileUrl,
  413. Sort: excelDetail.Sort,
  414. ModifyTime: excelDetail.ModifyTime,
  415. CreateTime: excelDetail.CreateTime,
  416. Button: opButton,
  417. HaveOperaAuth: true,
  418. }
  419. if markStatus.Status == 0 {
  420. resp.ExcelInfo.CanEdit = true
  421. } else {
  422. resp.ExcelInfo.Editor = markStatus.Editor
  423. }
  424. if excelDetail != nil {
  425. sheetList, err := excelModel.GetAllSheetItemList(excelDetail.ExcelInfoId)
  426. if err != nil {
  427. br.Msg = "获取sheet失败"
  428. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  429. return
  430. }
  431. if len(sheetList) > 0 {
  432. sheetIdList := make([]int, 0)
  433. for _, v := range sheetList {
  434. sheetIdList = append(sheetIdList, v.ExcelSheetId)
  435. }
  436. // 获取所有sheet的第一页的数据
  437. sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, 1)
  438. if err != nil {
  439. br.Msg = "获取sheet中的数据失败"
  440. br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
  441. return
  442. }
  443. sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
  444. for _, v := range sheetDataList {
  445. sheetDataMap[v.ExcelSheetId] = v
  446. }
  447. for k, v := range sheetList {
  448. sheetData, ok := sheetDataMap[v.ExcelSheetId]
  449. if !ok {
  450. continue
  451. }
  452. v.Data = sheetData
  453. sheetList[k] = v
  454. }
  455. }
  456. // TODO 合并单元格信息、计算公式
  457. resp.SheetList = sheetList
  458. }
  459. //resp := response.ExcelListResp{
  460. // Paging: page,
  461. // List: list,
  462. //}
  463. br.Ret = 200
  464. br.Success = true
  465. br.Msg = "获取成功"
  466. br.Data = resp
  467. }
  468. // ExcelDataList
  469. // @Title 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  470. // @Description 根据excel名称获取表格详情(基础信息+第一页初始化数据)
  471. // @Param UniqueCode query string true "excel唯一编码"
  472. // @Param Page query int true "页码"
  473. // @Success 200 {object} response.ExcelListResp
  474. // @router /excel/data [get]
  475. func (c *CustomAnalysisController) ExcelDataList() {
  476. br := new(models.BaseResponse).Init()
  477. defer func() {
  478. c.Data["json"] = br
  479. c.ServeJSON()
  480. }()
  481. sysUser := c.SysUser
  482. if sysUser == nil {
  483. br.Msg = "请登录"
  484. br.ErrMsg = "请登录,SysUser Is Empty"
  485. br.Ret = 408
  486. return
  487. }
  488. uniqueCode := c.GetString("UniqueCode")
  489. if uniqueCode == `` {
  490. br.Msg = "请选择表格"
  491. br.ErrMsg = "UniqueCode未传"
  492. br.IsSendEmail = false
  493. return
  494. }
  495. page, _ := c.GetInt("Page")
  496. if page <= 0 {
  497. br.Msg = "页码异常"
  498. br.ErrMsg = "页码异常"
  499. br.IsSendEmail = false
  500. return
  501. }
  502. sheetList := make([]*excelModel.SheetItem, 0)
  503. // 获取数据详情
  504. excelDetail, err := excelModel.GetNoContentExcelInfoByUniqueCode(uniqueCode)
  505. if err != nil {
  506. if err.Error() == utils.ErrNoRow() {
  507. br.Ret = 200
  508. br.Success = true
  509. br.Msg = "获取成功"
  510. br.Data = sheetList
  511. return
  512. }
  513. br.Msg = "获取表格事变"
  514. br.ErrMsg = err.Error()
  515. return
  516. }
  517. if excelDetail.Source != utils.CUSTOM_ANALYSIS_TABLE {
  518. br.Msg = "excel异常"
  519. br.ErrMsg = "excel异常"
  520. br.IsSendEmail = false
  521. return
  522. }
  523. if excelDetail != nil {
  524. sheetList, err = excelModel.GetAllNoConfigSheetItemList(excelDetail.ExcelInfoId)
  525. if err != nil {
  526. br.Msg = "获取sheet失败"
  527. br.ErrMsg = "获取sheet失败,err:" + err.Error()
  528. return
  529. }
  530. if len(sheetList) > 0 {
  531. sheetIdList := make([]int, 0)
  532. for _, v := range sheetList {
  533. sheetIdList = append(sheetIdList, v.ExcelSheetId)
  534. }
  535. // 获取所有sheet的第一页的数据
  536. sheetDataList, err := excelModel.GetSheetDataListBySheetIdListAndPage(sheetIdList, page)
  537. if err != nil {
  538. br.Msg = "获取sheet中的数据失败"
  539. br.ErrMsg = "获取sheet中的数据失败,err:" + err.Error()
  540. return
  541. }
  542. sheetDataMap := make(map[int]*excelModel.ExcelSheetData)
  543. for _, v := range sheetDataList {
  544. sheetDataMap[v.ExcelSheetId] = v
  545. }
  546. for k, v := range sheetList {
  547. sheetData, ok := sheetDataMap[v.ExcelSheetId]
  548. if !ok {
  549. continue
  550. }
  551. v.Data = sheetData
  552. sheetList[k] = v
  553. }
  554. }
  555. // TODO 合并单元格信息、计算公式
  556. }
  557. //resp := response.ExcelListResp{
  558. // Paging: page,
  559. // List: list,
  560. //}
  561. br.Ret = 200
  562. br.Success = true
  563. br.Msg = "获取成功"
  564. br.Data = sheetList
  565. }
  566. // ClassifyList
  567. // @Title 分类列表
  568. // @Description 分类列表
  569. // @Param IsShare query bool false "是否共享表格: true-是; false-否(默认)"
  570. // @Success 200 {object} response.ExcelClassifyListResp
  571. // @router /excel_classify/list [get]
  572. func (c *CustomAnalysisController) ClassifyList() {
  573. br := new(models.BaseResponse).Init()
  574. defer func() {
  575. if br.ErrMsg == "" {
  576. br.IsSendEmail = false
  577. }
  578. c.Data["json"] = br
  579. c.ServeJSON()
  580. }()
  581. sysUser := c.SysUser
  582. if sysUser == nil {
  583. br.Msg = "请登录"
  584. br.ErrMsg = "请登录,SysUser Is Empty"
  585. br.Ret = 408
  586. return
  587. }
  588. source := utils.CUSTOM_ANALYSIS_TABLE
  589. isShare, _ := c.GetBool("IsShare")
  590. resp := new(response.ExcelClassifyListResp)
  591. // 查询我分享的/分享给我的表格
  592. excels, e := excelPermissionModel.GetAdminAuthExcelInfoPermission(source, sysUser.AdminId, "")
  593. if e != nil {
  594. br.Msg = "获取失败"
  595. br.ErrMsg = fmt.Sprintf("获取我分享的/分享给我的表格失败, %v", e)
  596. return
  597. }
  598. hasShareMap := make(map[int]bool) // 是否已分享标记
  599. for _, v := range excels {
  600. if v.CreateUserId == sysUser.AdminId {
  601. hasShareMap[int(v.ExcelInfoId)] = true
  602. }
  603. }
  604. // 共享
  605. if isShare {
  606. shareExcel := new(excelModel.ExcelClassifyItems)
  607. shareExcel.ExcelClassifyId = excelModel.CustomAnalysisMenuShareId
  608. shareExcel.ExcelClassifyName = "我共享的"
  609. shareExcel.Level = 1
  610. shareExcel.Sort = 1
  611. shareExcel.UniqueCode = fmt.Sprintf("SHARE%d", shareExcel.ExcelClassifyId)
  612. shareExcel.HaveOperaAuth = true
  613. shareExcel.Children = make([]*excelModel.ExcelClassifyItems, 0)
  614. sharedExcel := new(excelModel.ExcelClassifyItems)
  615. sharedExcel.ExcelClassifyId = excelModel.CustomAnalysisMenuSharedId
  616. sharedExcel.ExcelClassifyName = "收到共享"
  617. sharedExcel.Level = 1
  618. sharedExcel.Sort = 2
  619. sharedExcel.UniqueCode = fmt.Sprintf("SHARED%d", sharedExcel.ExcelClassifyId)
  620. sharedExcel.HaveOperaAuth = true
  621. sharedExcel.Children = make([]*excelModel.ExcelClassifyItems, 0)
  622. // 收到共享细分目录
  623. sharedUserClassify := make(map[int]*excelModel.ExcelClassifyItems)
  624. adminIdName := make(map[int]string)
  625. {
  626. adminOb := new(system.Admin)
  627. adminList, e := adminOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  628. if e != nil {
  629. br.Msg = "获取失败"
  630. br.ErrMsg = fmt.Sprintf("获取用户列表失败, %v", e)
  631. return
  632. }
  633. for _, v := range adminList {
  634. adminIdName[v.AdminId] = v.RealName
  635. }
  636. }
  637. sortUser := make([]int, 0)
  638. shareExists := make(map[int]bool)
  639. for _, v := range excels {
  640. item := new(excelModel.ExcelClassifyItems)
  641. item.ExcelInfoId = int(v.ExcelInfoId)
  642. item.ExcelClassifyName = v.ExcelName
  643. item.UniqueCode = v.UniqueCode
  644. item.HaveOperaAuth = true
  645. item.HasShare = hasShareMap[int(v.ExcelInfoId)]
  646. // 我共享的
  647. if v.CreateUserId == sysUser.AdminId && !shareExists[int(v.ExcelInfoId)] {
  648. shareExists[int(v.ExcelInfoId)] = true
  649. item.Level = 2
  650. item.ParentId = shareExcel.ExcelClassifyId
  651. item.ShowShareBtn = true
  652. shareExcel.Children = append(shareExcel.Children, item)
  653. continue
  654. }
  655. // 收到共享
  656. if int(v.SysUserId) == sysUser.AdminId {
  657. if adminIdName[v.CreateUserId] == "" {
  658. // 分享人用户不存在, 那么不显示该分类
  659. continue
  660. }
  661. if !utils.InArrayByInt(sortUser, v.CreateUserId) {
  662. sortUser = append(sortUser, v.CreateUserId)
  663. }
  664. if sharedUserClassify[v.CreateUserId] == nil {
  665. us := new(excelModel.ExcelClassifyItems)
  666. us.ExcelClassifyId = v.CreateUserId // 用户ID作为分类ID
  667. us.ExcelClassifyName = fmt.Sprintf("%s的表格", adminIdName[v.CreateUserId])
  668. us.Level = 2
  669. us.UniqueCode = fmt.Sprintf("%d-%d", sharedExcel.ExcelClassifyId, v.CreateUserId)
  670. us.HaveOperaAuth = true
  671. us.ShowShareBtn = false
  672. us.Children = make([]*excelModel.ExcelClassifyItems, 0)
  673. sharedUserClassify[v.CreateUserId] = us
  674. }
  675. item.Level = 3
  676. item.ParentId = sharedUserClassify[v.CreateUserId].ExcelClassifyId
  677. sharedUserClassify[v.CreateUserId].Children = append(sharedUserClassify[v.CreateUserId].Children, item)
  678. }
  679. }
  680. for _, v := range sortUser {
  681. if sharedUserClassify[v] != nil {
  682. sharedExcel.Children = append(sharedExcel.Children, sharedUserClassify[v])
  683. }
  684. }
  685. resp.AllNodes = make([]*excelModel.ExcelClassifyItems, 0)
  686. resp.AllNodes = append(resp.AllNodes, shareExcel, sharedExcel)
  687. br.Ret = 200
  688. br.Success = true
  689. br.Msg = "获取成功"
  690. br.Data = resp
  691. return
  692. }
  693. // 我的表格
  694. classifyList, err := excelModel.GetAdminExcelClassifyBySource(source, sysUser.AdminId)
  695. if err != nil {
  696. br.Msg = "获取失败"
  697. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  698. return
  699. }
  700. classifyMap := make(map[int]*excelModel.ExcelClassifyItems)
  701. for _, v := range classifyList {
  702. v.HaveOperaAuth = true
  703. classifyMap[v.ExcelClassifyId] = v
  704. }
  705. allExcelInfo, err := excelModel.GetNoContentExcelInfoAll(source, sysUser.AdminId)
  706. if err != nil && err.Error() != utils.ErrNoRow() {
  707. br.Msg = "获取失败"
  708. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  709. return
  710. }
  711. excelInfoMap := make(map[int][]*excelModel.ExcelClassifyItems)
  712. for _, v := range allExcelInfo {
  713. v.HaveOperaAuth = true
  714. v.ShowShareBtn = true
  715. v.HasShare = hasShareMap[v.ExcelInfoId]
  716. excelInfoMap[v.ExcelClassifyId] = append(excelInfoMap[v.ExcelClassifyId], v)
  717. }
  718. classifyListMap := make(map[int][]*excelModel.ExcelClassifyItems)
  719. for _, v := range classifyList {
  720. if v.ParentId > 0 {
  721. classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v)
  722. }
  723. if existItems, ok := excelInfoMap[v.ExcelClassifyId]; ok {
  724. v.Children = existItems
  725. }
  726. }
  727. for key, classify := range classifyList {
  728. subList, ok := classifyListMap[classify.ExcelClassifyId]
  729. if ok {
  730. classifyList[key].Children = append(classifyList[key].Children, subList...)
  731. sort.Slice(classifyList[key].Children, func(i, j int) bool {
  732. return excelModel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j])
  733. })
  734. }
  735. }
  736. nodeAll := make([]*excelModel.ExcelClassifyItems, 0)
  737. for _, v := range classifyList {
  738. if v.ParentId == 0 {
  739. sort.Slice(v.Children, func(i, j int) bool {
  740. return excelModel.ExcelClassifyItemBySort(v.Children[i], v.Children[j])
  741. })
  742. nodeAll = append(nodeAll, v)
  743. }
  744. }
  745. resp.AllNodes = nodeAll
  746. br.Ret = 200
  747. br.Success = true
  748. br.Msg = "获取成功"
  749. br.Data = resp
  750. }
  751. // List
  752. // @Title 自定义分析列表
  753. // @Description ETA表格列表接口
  754. // @Param PageSize query int true "每页数据条数"
  755. // @Param CurrentIndex query int true "当前页页码,从1开始"
  756. // @Param ExcelClassifyId query int true "分类id"
  757. // @Param Keyword query string true "搜索关键词"
  758. // @Param IsShare query bool false "是否共享表格: true-是; false-否(默认)"
  759. // @Success 200 {object} response.ExcelListResp
  760. // @router /excel/list [get]
  761. func (c *CustomAnalysisController) List() {
  762. br := new(models.BaseResponse).Init()
  763. defer func() {
  764. if br.ErrMsg == "" {
  765. br.IsSendEmail = false
  766. }
  767. c.Data["json"] = br
  768. c.ServeJSON()
  769. }()
  770. sysUser := c.SysUser
  771. if sysUser == nil {
  772. br.Msg = "请登录"
  773. br.ErrMsg = "请登录,SysUser Is Empty"
  774. br.Ret = 408
  775. return
  776. }
  777. excelClassifyId, _ := c.GetInt("ExcelClassifyId")
  778. pageSize, _ := c.GetInt("PageSize")
  779. currentIndex, _ := c.GetInt("CurrentIndex")
  780. keyword := c.GetString("Keyword")
  781. keyword = strings.TrimSpace(keyword)
  782. adminId := sysUser.AdminId
  783. isShare, _ := c.GetBool("IsShare")
  784. source := utils.CUSTOM_ANALYSIS_TABLE
  785. var condition string
  786. var pars []interface{}
  787. condition += " AND source = ?"
  788. pars = append(pars, source)
  789. // 共享的
  790. //hasShareMap := make(map[int]bool)
  791. if isShare {
  792. if keyword != "" {
  793. keyword = fmt.Sprint("%", keyword, "%")
  794. }
  795. // 查询我分享的/分享给我的表格
  796. excels, e := excelPermissionModel.GetAdminAuthExcelInfoPermission(source, adminId, keyword)
  797. if e != nil {
  798. br.Msg = "获取失败"
  799. br.ErrMsg = fmt.Sprintf("获取我分享的/分享给我的表格失败, %v", e)
  800. return
  801. }
  802. var excelIds, shareIds, sharedIds, userIds []int
  803. for _, v := range excels {
  804. id := int(v.ExcelInfoId)
  805. // 无分类ID
  806. if excelClassifyId <= 0 && !utils.InArrayByInt(excelIds, id) {
  807. excelIds = append(excelIds, id)
  808. continue
  809. }
  810. // 用户分类
  811. if excelClassifyId == v.CreateUserId {
  812. userIds = append(userIds, id)
  813. continue
  814. }
  815. // 我共享的
  816. if excelClassifyId == excelModel.CustomAnalysisMenuShareId && v.CreateUserId == sysUser.AdminId {
  817. shareIds = append(shareIds, id)
  818. //hasShareMap[id] = true
  819. continue
  820. }
  821. // 收到共享
  822. if excelClassifyId == excelModel.CustomAnalysisMenuSharedId && int(v.SysUserId) == sysUser.AdminId {
  823. sharedIds = append(sharedIds, id)
  824. continue
  825. }
  826. }
  827. if excelClassifyId > 0 {
  828. switch excelClassifyId {
  829. case excelModel.CustomAnalysisMenuShareId: // 我共享的
  830. excelIds = shareIds
  831. case excelModel.CustomAnalysisMenuSharedId: // 收到共享
  832. excelIds = sharedIds
  833. default: // 用户目录
  834. excelIds = userIds
  835. }
  836. }
  837. if len(excelIds) == 0 {
  838. page := paging.GetPaging(currentIndex, pageSize, 0)
  839. resp := response.ExcelListResp{
  840. Paging: page,
  841. List: make([]*excelModel.MyExcelInfoList, 0),
  842. }
  843. br.Ret = 200
  844. br.Success = true
  845. br.Msg = "获取成功"
  846. br.Data = resp
  847. return
  848. }
  849. condition += fmt.Sprintf(` AND excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  850. pars = append(pars, excelIds)
  851. } else {
  852. condition += ` AND sys_user_id = ?`
  853. pars = append(pars, adminId)
  854. }
  855. // 筛选分类
  856. if !isShare && excelClassifyId > 0 {
  857. _, err := excelModel.GetExcelClassifyById(excelClassifyId)
  858. if err != nil && err.Error() != utils.ErrNoRow() {
  859. br.Msg = "获取表格信息失败"
  860. br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
  861. return
  862. }
  863. childClassify, e, _ := excel2.GetChildClassifyByClassifyId(excelClassifyId, source)
  864. if e != nil && e.Error() != utils.ErrNoRow() {
  865. br.Msg = "获取失败"
  866. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  867. return
  868. }
  869. if len(childClassify) == 0 {
  870. condition += " AND excel_classify_id = ? "
  871. pars = append(pars, excelClassifyId)
  872. } else {
  873. classifyIds := []int{excelClassifyId}
  874. for _, v := range childClassify {
  875. classifyIds = append(classifyIds, v.ExcelClassifyId)
  876. }
  877. condition += fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  878. pars = append(pars, classifyIds)
  879. }
  880. }
  881. if keyword != "" {
  882. condition += ` AND (excel_name LIKE ?)`
  883. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  884. }
  885. // 获取表格信息
  886. var total int
  887. page := paging.GetPaging(currentIndex, pageSize, total)
  888. var startSize int
  889. if pageSize <= 0 {
  890. pageSize = utils.PageSize20
  891. }
  892. if currentIndex <= 0 {
  893. currentIndex = 1
  894. }
  895. startSize = paging.StartIndex(currentIndex, pageSize)
  896. list, err := excelModel.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
  897. if err != nil && err.Error() != utils.ErrNoRow() {
  898. br.Success = true
  899. br.Msg = "获取表格信息失败"
  900. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  901. return
  902. }
  903. for _, v := range list {
  904. v.HaveOperaAuth = true
  905. //v.Button.CancelShareButton = hasShareMap[v.ExcelInfoId]
  906. }
  907. // 总数据量
  908. dataCount, err := excelModel.GetExcelListCountByCondition(condition, pars)
  909. if err != nil && err.Error() != utils.ErrNoRow() {
  910. br.Msg = "获取表格列表信息失败"
  911. br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
  912. return
  913. }
  914. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  915. resp := response.ExcelListResp{
  916. Paging: page,
  917. List: list,
  918. }
  919. br.Ret = 200
  920. br.Success = true
  921. br.Msg = "获取成功"
  922. br.Data = resp
  923. }
  924. // Share
  925. // @Title 分享表格
  926. // @Description 分享表格
  927. // @Param request body request.ShareExcelInfoReq true "type json string"
  928. // @Success 200 string "操作成功"
  929. // @router /excel/share [post]
  930. func (c *CustomAnalysisController) Share() {
  931. br := new(models.BaseResponse).Init()
  932. defer func() {
  933. if br.ErrMsg != "" {
  934. br.IsSendEmail = false
  935. }
  936. c.Data["json"] = br
  937. c.ServeJSON()
  938. }()
  939. var req request.ShareExcelInfoReq
  940. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  941. if err != nil {
  942. br.Msg = "参数解析异常!"
  943. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  944. return
  945. }
  946. sysUser := c.SysUser
  947. if sysUser == nil {
  948. br.Msg = "请登录"
  949. br.ErrMsg = "请登录,SysUser Is Empty"
  950. br.Ret = 408
  951. return
  952. }
  953. if req.ExcelInfoId <= 0 {
  954. br.Msg = "请选择表格"
  955. return
  956. }
  957. excelInfo, e := excelModel.GetExcelInfoById(req.ExcelInfoId)
  958. if e != nil {
  959. if e.Error() == utils.ErrNoRow() {
  960. br.Msg = "表格不存在, 请刷新页面"
  961. return
  962. }
  963. br.Msg = "操作失败"
  964. br.ErrMsg = fmt.Sprintf("获取表格失败, %v", e)
  965. return
  966. }
  967. if excelInfo.SysUserId != sysUser.AdminId {
  968. br.Msg = "无权操作"
  969. br.ErrMsg = fmt.Sprintf("无权设置表格权限, ExcelInfoId: %d, SysAdminId: %d", req.ExcelInfoId, sysUser.AdminId)
  970. return
  971. }
  972. source := utils.CUSTOM_ANALYSIS_TABLE
  973. newPermissions := make([]*excelPermissionModel.ExcelInfoPermission, 0)
  974. for _, v := range req.ViewUserIds {
  975. newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
  976. ExcelInfoId: int32(req.ExcelInfoId),
  977. Source: int32(source),
  978. SysUserId: int32(v),
  979. CreateTime: time.Now().Local(),
  980. ModifyTime: time.Now().Local(),
  981. PermissionType: 1,
  982. })
  983. }
  984. for _, v := range req.EditUserIds {
  985. newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
  986. ExcelInfoId: int32(req.ExcelInfoId),
  987. Source: int32(source),
  988. SysUserId: int32(v),
  989. CreateTime: time.Now().Local(),
  990. ModifyTime: time.Now().Local(),
  991. PermissionType: 2,
  992. })
  993. }
  994. if e := excelPermissionModel.ClearAndSetExcelInfoPermission(source, req.ExcelInfoId, newPermissions); e != nil {
  995. br.Msg = "操作失败"
  996. br.ErrMsg = fmt.Sprintf("设置表格权限失败, %v", e)
  997. return
  998. }
  999. br.Ret = 200
  1000. br.Success = true
  1001. br.Msg = "操作成功"
  1002. }
  1003. // ShareDetail
  1004. // @Title 分享表格-详情
  1005. // @Description 分享表格-详情
  1006. // @Param ExcelInfoId query int true "表格ID"
  1007. // @Success 200 {object} response.ShareExcelInfoDetail
  1008. // @router /excel/share_detail [get]
  1009. func (c *CustomAnalysisController) ShareDetail() {
  1010. br := new(models.BaseResponse).Init()
  1011. defer func() {
  1012. if br.ErrMsg != "" {
  1013. br.IsSendEmail = false
  1014. }
  1015. c.Data["json"] = br
  1016. c.ServeJSON()
  1017. }()
  1018. sysUser := c.SysUser
  1019. if sysUser == nil {
  1020. br.Msg = "请登录"
  1021. br.ErrMsg = "请登录,SysUser Is Empty"
  1022. br.Ret = 408
  1023. return
  1024. }
  1025. excelInfoId, _ := c.GetInt("ExcelInfoId")
  1026. if excelInfoId <= 0 {
  1027. br.Msg = "请选择表格"
  1028. return
  1029. }
  1030. permissions, e := excelPermissionModel.GetExcelPermissionBySourceAndId(excelInfoId, utils.CUSTOM_ANALYSIS_TABLE)
  1031. if e != nil {
  1032. br.Msg = "获取失败"
  1033. br.ErrMsg = fmt.Sprintf("获取表格权限失败, %v", e)
  1034. return
  1035. }
  1036. resp := new(response.ShareExcelInfoDetail)
  1037. resp.ExcelInfoId = excelInfoId
  1038. for _, v := range permissions {
  1039. if v.PermissionType == 1 {
  1040. resp.ViewUserIds = append(resp.ViewUserIds, int(v.SysUserId))
  1041. }
  1042. if v.PermissionType == 2 {
  1043. resp.EditUserIds = append(resp.EditUserIds, int(v.SysUserId))
  1044. }
  1045. }
  1046. br.Data = resp
  1047. br.Ret = 200
  1048. br.Success = true
  1049. br.Msg = "获取成功"
  1050. }
  1051. // CustomAnalysisCommonController 修复数据用的
  1052. type CustomAnalysisCommonController struct {
  1053. controllers.BaseCommonController
  1054. }
  1055. //var OnceCustomAnalysisFixed = false
  1056. // FixClassify
  1057. // @Title 修复表格分类(一次性-后续版本可删掉)
  1058. // @Description 修复表格分类
  1059. // @Param ExcelInfoId query int true "表格ID"
  1060. // @Success 200 string "操作成功"
  1061. // @router /excel/fix_classify [get]
  1062. func (c *CustomAnalysisCommonController) FixClassify() {
  1063. br := new(models.BaseResponse).Init()
  1064. defer func() {
  1065. if br.ErrMsg != "" {
  1066. br.IsSendEmail = false
  1067. }
  1068. if br.Ret == 200 {
  1069. //OnceCustomAnalysisFixed = true
  1070. }
  1071. c.Data["json"] = br
  1072. c.ServeJSON()
  1073. }()
  1074. //if OnceCustomAnalysisFixed {
  1075. // br.Ret = 200
  1076. // br.Success = true
  1077. // br.Msg = "请勿重复修复"
  1078. // return
  1079. //}
  1080. source := utils.CUSTOM_ANALYSIS_TABLE
  1081. // 获取所有分类
  1082. classifies, e := excelModel.GetExcelClassifyModelBySource(source)
  1083. if e != nil {
  1084. br.Msg = "操作失败"
  1085. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  1086. return
  1087. }
  1088. classifyMap := make(map[int]*excelModel.ExcelClassify)
  1089. for _, v := range classifies {
  1090. classifyMap[v.ExcelClassifyId] = v
  1091. }
  1092. // 查询所有excel
  1093. excels, e := excelModel.GetAllExcelInfoBySource(source)
  1094. if e != nil {
  1095. br.Msg = "操作失败"
  1096. br.ErrMsg = fmt.Sprintf("获取excel列表失败, %v", e)
  1097. return
  1098. }
  1099. if len(excels) == 0 {
  1100. br.Ret = 200
  1101. br.Success = true
  1102. br.Msg = "无分类需要修复"
  1103. return
  1104. }
  1105. // 先按照用户ID分组
  1106. groupExcels := make(map[int][]*excelModel.ExcelInfo)
  1107. for _, v := range excels {
  1108. //if v.SysUserId != 198 {
  1109. // continue
  1110. //}
  1111. if groupExcels[v.SysUserId] == nil {
  1112. groupExcels[v.SysUserId] = make([]*excelModel.ExcelInfo, 0)
  1113. }
  1114. groupExcels[v.SysUserId] = append(groupExcels[v.SysUserId], v)
  1115. }
  1116. // 递归函数
  1117. findAndReplace := func(replaceId map[int]*excelModel.ExcelClassify, userId int, excelItem *excelModel.ExcelInfo, classifyItem *excelModel.ExcelClassify, firstStep bool) (hasParent, updateChild bool, currentClassify *excelModel.ExcelClassify, err error) {
  1118. // 分类创建人与表格不一致
  1119. currentClassify = classifyItem
  1120. if classifyItem.SysUserId != userId {
  1121. // 是否已存在替换分类
  1122. _, ok := replaceId[classifyItem.ExcelClassifyId]
  1123. if !ok {
  1124. classifyNew := &excelModel.ExcelClassify{
  1125. ExcelClassifyName: fmt.Sprintf("%d%s", userId, classifyItem.ExcelClassifyName),
  1126. ParentId: classifyItem.ParentId, // 根据updateChild递归外更新
  1127. Source: source,
  1128. SysUserId: excelItem.SysUserId,
  1129. SysUserRealName: excelItem.SysUserRealName,
  1130. Level: classifyItem.Level,
  1131. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  1132. Sort: classifyItem.Sort,
  1133. CreateTime: time.Now(),
  1134. ModifyTime: time.Now(),
  1135. }
  1136. _, e = excelModel.AddExcelClassify(classifyNew)
  1137. if e != nil {
  1138. err = fmt.Errorf("AddExcelClassify, %v", e)
  1139. return
  1140. }
  1141. replaceId[classifyItem.ExcelClassifyId] = classifyNew
  1142. excelItem.ExcelClassifyId = classifyNew.ExcelClassifyId
  1143. currentClassify = classifyNew
  1144. classifyMap[classifyNew.ExcelClassifyId] = classifyNew
  1145. } else {
  1146. excelItem.ExcelClassifyId = replaceId[classifyItem.ExcelClassifyId].ExcelClassifyId
  1147. currentClassify = replaceId[classifyItem.ExcelClassifyId]
  1148. }
  1149. // 仅第一次递归时修改
  1150. if firstStep {
  1151. //fmt.Printf("修改ExcelClassifyId: %d\n", excelItem.ExcelClassifyId)
  1152. excelItem.ModifyTime = time.Now().Local()
  1153. if e = excelItem.Update([]string{"ExcelClassifyId", "ModifyTime"}); e != nil {
  1154. err = fmt.Errorf("UpdateExcelInfo, %v", e)
  1155. return
  1156. }
  1157. }
  1158. }
  1159. // 查上级
  1160. if classifyItem.ParentId <= 0 {
  1161. //fmt.Printf("终止递归, ClassifyId: %d", classifyItem.ExcelClassifyId)
  1162. return
  1163. }
  1164. hasParent = true
  1165. parent := classifyMap[classifyItem.ParentId]
  1166. if parent == nil {
  1167. err = fmt.Errorf("未找到上级分类, ClassifyId: %d, ParentId: %d", classifyItem.ExcelClassifyId, classifyItem.ParentId)
  1168. return
  1169. }
  1170. if parent.SysUserId != userId {
  1171. updateChild = true
  1172. }
  1173. return
  1174. }
  1175. for uid, gv := range groupExcels {
  1176. replaceId := make(map[int]*excelModel.ExcelClassify)
  1177. for _, v := range gv {
  1178. classifyItem, e := excelModel.GetExcelClassifyById(v.ExcelClassifyId)
  1179. if e != nil {
  1180. br.Msg = "操作失败"
  1181. br.ErrMsg = fmt.Sprintf("获取表格分类失败, ExcelClassifyId: %d, %v", v.ExcelClassifyId, e)
  1182. return
  1183. }
  1184. // 首次递归
  1185. hasParent, updateChild, currentClassify, e := findAndReplace(replaceId, uid, v, classifyItem, true)
  1186. if e != nil {
  1187. br.Msg = "操作失败"
  1188. br.ErrMsg = fmt.Sprintf("findAndReplace1, %v", e)
  1189. return
  1190. }
  1191. if !hasParent {
  1192. continue
  1193. }
  1194. // 二次递归
  1195. //fmt.Printf("二次递归: %d\n", currentClassify.ParentId)
  1196. parentClassify, e := excelModel.GetExcelClassifyById(currentClassify.ParentId)
  1197. if e != nil {
  1198. br.Msg = "操作失败"
  1199. br.ErrMsg = fmt.Sprintf("2-parent-GetExcelClassifyById, %v", e)
  1200. return
  1201. }
  1202. hasParent2, updateChild2, currentClassify2, e := findAndReplace(replaceId, uid, v, parentClassify, false)
  1203. if e != nil {
  1204. br.Msg = "操作失败"
  1205. br.ErrMsg = fmt.Sprintf("findAndReplace2, %v", e)
  1206. return
  1207. }
  1208. // 更新上次递归的parent_id
  1209. if updateChild {
  1210. currentClassify.ParentId = currentClassify2.ExcelClassifyId
  1211. currentClassify.ModifyTime = time.Now().Local()
  1212. if e = currentClassify.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1213. br.Msg = "操作失败"
  1214. br.ErrMsg = fmt.Sprintf("2-更新父级分类ID失败, %v", e)
  1215. return
  1216. }
  1217. }
  1218. if !hasParent2 {
  1219. continue
  1220. }
  1221. // 三次递归
  1222. //fmt.Printf("三次递归: %d\n", currentClassify2.ParentId)
  1223. parentClassify2, e := excelModel.GetExcelClassifyById(currentClassify2.ParentId)
  1224. if e != nil {
  1225. br.Msg = "操作失败"
  1226. br.ErrMsg = fmt.Sprintf("3-parent-GetExcelClassifyById, %v", e)
  1227. return
  1228. }
  1229. hasParent3, updateChild3, currentClassify3, e := findAndReplace(replaceId, uid, v, parentClassify2, false)
  1230. if e != nil {
  1231. br.Msg = "操作失败"
  1232. br.ErrMsg = fmt.Sprintf("findAndReplace3, %v", e)
  1233. return
  1234. }
  1235. if updateChild2 {
  1236. currentClassify2.ParentId = currentClassify3.ExcelClassifyId
  1237. currentClassify2.ModifyTime = time.Now().Local()
  1238. if e = currentClassify2.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1239. br.Msg = "操作失败"
  1240. br.ErrMsg = fmt.Sprintf("3-更新父级分类ID失败, %v", e)
  1241. return
  1242. }
  1243. }
  1244. if !hasParent3 {
  1245. continue
  1246. }
  1247. // 四次递归
  1248. //fmt.Printf("四次递归: %d\n", currentClassify3.ParentId)
  1249. parentClassify3, e := excelModel.GetExcelClassifyById(currentClassify3.ParentId)
  1250. if e != nil {
  1251. br.Msg = "操作失败"
  1252. br.ErrMsg = fmt.Sprintf("4-parent-GetExcelClassifyById, %v", e)
  1253. return
  1254. }
  1255. hasParent4, updateChild4, currentClassify4, e := findAndReplace(replaceId, uid, v, parentClassify3, false)
  1256. if e != nil {
  1257. br.Msg = "操作失败"
  1258. br.ErrMsg = fmt.Sprintf("findAndReplace4, %v", e)
  1259. return
  1260. }
  1261. if updateChild3 {
  1262. currentClassify3.ParentId = currentClassify4.ExcelClassifyId
  1263. currentClassify3.ModifyTime = time.Now().Local()
  1264. if e = currentClassify3.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1265. br.Msg = "操作失败"
  1266. br.ErrMsg = fmt.Sprintf("4-更新父级分类ID失败, %v", e)
  1267. return
  1268. }
  1269. }
  1270. if !hasParent4 {
  1271. continue
  1272. }
  1273. // 五次递归-理论上没了(自定义分析最多三级目录+最底层的表格)
  1274. //fmt.Printf("五次递归: %d\n", currentClassify4.ParentId)
  1275. parentClassify4, e := excelModel.GetExcelClassifyById(currentClassify4.ParentId)
  1276. if e != nil {
  1277. br.Msg = "操作失败"
  1278. br.ErrMsg = fmt.Sprintf("5-parent-GetExcelClassifyById, %v", e)
  1279. return
  1280. }
  1281. hasParent5, _, currentClassify5, e := findAndReplace(replaceId, uid, v, parentClassify4, false)
  1282. if e != nil {
  1283. br.Msg = "操作失败"
  1284. br.ErrMsg = fmt.Sprintf("findAndReplace5, %v", e)
  1285. return
  1286. }
  1287. if updateChild4 {
  1288. currentClassify4.ParentId = currentClassify5.ExcelClassifyId
  1289. currentClassify4.ModifyTime = time.Now().Local()
  1290. if e = currentClassify4.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1291. br.Msg = "操作失败"
  1292. br.ErrMsg = fmt.Sprintf("5-更新父级分类ID失败, %v", e)
  1293. return
  1294. }
  1295. }
  1296. if !hasParent5 {
  1297. continue
  1298. }
  1299. }
  1300. //fmt.Println("replaceId: ", replaceId)
  1301. }
  1302. br.Ret = 200
  1303. br.Success = true
  1304. br.Msg = "操作成功"
  1305. }