custom_analysis.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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. // 按照分享人第一次分享时间降序=_=!, 查询的时候是升序, 这里反过来
  681. for i, j := 0, len(sortUser)-1; i < j; i, j = i+1, j-1 {
  682. sortUser[i], sortUser[j] = sortUser[j], sortUser[i]
  683. }
  684. for _, v := range sortUser {
  685. if sharedUserClassify[v] != nil {
  686. sharedExcel.Children = append(sharedExcel.Children, sharedUserClassify[v])
  687. }
  688. }
  689. resp.AllNodes = make([]*excelModel.ExcelClassifyItems, 0)
  690. resp.AllNodes = append(resp.AllNodes, shareExcel, sharedExcel)
  691. br.Ret = 200
  692. br.Success = true
  693. br.Msg = "获取成功"
  694. br.Data = resp
  695. return
  696. }
  697. // 我的表格
  698. classifyList, err := excelModel.GetAdminExcelClassifyBySource(source, sysUser.AdminId)
  699. if err != nil {
  700. br.Msg = "获取失败"
  701. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  702. return
  703. }
  704. classifyMap := make(map[int]*excelModel.ExcelClassifyItems)
  705. for _, v := range classifyList {
  706. v.HaveOperaAuth = true
  707. classifyMap[v.ExcelClassifyId] = v
  708. }
  709. allExcelInfo, err := excelModel.GetNoContentExcelInfoAll(source, sysUser.AdminId)
  710. if err != nil && err.Error() != utils.ErrNoRow() {
  711. br.Msg = "获取失败"
  712. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  713. return
  714. }
  715. excelInfoMap := make(map[int][]*excelModel.ExcelClassifyItems)
  716. for _, v := range allExcelInfo {
  717. v.HaveOperaAuth = true
  718. v.ShowShareBtn = true
  719. v.HasShare = hasShareMap[v.ExcelInfoId]
  720. excelInfoMap[v.ExcelClassifyId] = append(excelInfoMap[v.ExcelClassifyId], v)
  721. }
  722. classifyListMap := make(map[int][]*excelModel.ExcelClassifyItems)
  723. for _, v := range classifyList {
  724. if v.ParentId > 0 {
  725. classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v)
  726. }
  727. if existItems, ok := excelInfoMap[v.ExcelClassifyId]; ok {
  728. v.Children = existItems
  729. }
  730. }
  731. for key, classify := range classifyList {
  732. subList, ok := classifyListMap[classify.ExcelClassifyId]
  733. if ok {
  734. classifyList[key].Children = append(classifyList[key].Children, subList...)
  735. sort.Slice(classifyList[key].Children, func(i, j int) bool {
  736. return excelModel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j])
  737. })
  738. }
  739. }
  740. nodeAll := make([]*excelModel.ExcelClassifyItems, 0)
  741. for _, v := range classifyList {
  742. if v.ParentId == 0 {
  743. sort.Slice(v.Children, func(i, j int) bool {
  744. return excelModel.ExcelClassifyItemBySort(v.Children[i], v.Children[j])
  745. })
  746. nodeAll = append(nodeAll, v)
  747. }
  748. }
  749. resp.AllNodes = nodeAll
  750. br.Ret = 200
  751. br.Success = true
  752. br.Msg = "获取成功"
  753. br.Data = resp
  754. }
  755. // List
  756. // @Title 自定义分析列表
  757. // @Description ETA表格列表接口
  758. // @Param PageSize query int true "每页数据条数"
  759. // @Param CurrentIndex query int true "当前页页码,从1开始"
  760. // @Param ExcelClassifyId query int true "分类id"
  761. // @Param Keyword query string true "搜索关键词"
  762. // @Param IsShare query bool false "是否共享表格: true-是; false-否(默认)"
  763. // @Success 200 {object} response.ExcelListResp
  764. // @router /excel/list [get]
  765. func (c *CustomAnalysisController) List() {
  766. br := new(models.BaseResponse).Init()
  767. defer func() {
  768. if br.ErrMsg == "" {
  769. br.IsSendEmail = false
  770. }
  771. c.Data["json"] = br
  772. c.ServeJSON()
  773. }()
  774. sysUser := c.SysUser
  775. if sysUser == nil {
  776. br.Msg = "请登录"
  777. br.ErrMsg = "请登录,SysUser Is Empty"
  778. br.Ret = 408
  779. return
  780. }
  781. excelClassifyId, _ := c.GetInt("ExcelClassifyId")
  782. pageSize, _ := c.GetInt("PageSize")
  783. currentIndex, _ := c.GetInt("CurrentIndex")
  784. keyword := c.GetString("Keyword")
  785. keyword = strings.TrimSpace(keyword)
  786. adminId := sysUser.AdminId
  787. isShare, _ := c.GetBool("IsShare")
  788. source := utils.CUSTOM_ANALYSIS_TABLE
  789. var condition string
  790. var pars []interface{}
  791. condition += " AND source = ?"
  792. pars = append(pars, source)
  793. // 共享的
  794. //hasShareMap := make(map[int]bool)
  795. if isShare {
  796. if keyword != "" {
  797. keyword = fmt.Sprint("%", keyword, "%")
  798. }
  799. // 查询我分享的/分享给我的表格
  800. excels, e := excelPermissionModel.GetAdminAuthExcelInfoPermission(source, adminId, keyword)
  801. if e != nil {
  802. br.Msg = "获取失败"
  803. br.ErrMsg = fmt.Sprintf("获取我分享的/分享给我的表格失败, %v", e)
  804. return
  805. }
  806. var excelIds, shareIds, sharedIds, userIds []int
  807. for _, v := range excels {
  808. id := int(v.ExcelInfoId)
  809. // 无分类ID
  810. if excelClassifyId <= 0 && !utils.InArrayByInt(excelIds, id) {
  811. excelIds = append(excelIds, id)
  812. continue
  813. }
  814. // 用户分类
  815. if excelClassifyId == v.CreateUserId {
  816. userIds = append(userIds, id)
  817. continue
  818. }
  819. // 我共享的
  820. if excelClassifyId == excelModel.CustomAnalysisMenuShareId && v.CreateUserId == sysUser.AdminId {
  821. shareIds = append(shareIds, id)
  822. //hasShareMap[id] = true
  823. continue
  824. }
  825. // 收到共享
  826. if excelClassifyId == excelModel.CustomAnalysisMenuSharedId && int(v.SysUserId) == sysUser.AdminId {
  827. sharedIds = append(sharedIds, id)
  828. continue
  829. }
  830. }
  831. if excelClassifyId > 0 {
  832. switch excelClassifyId {
  833. case excelModel.CustomAnalysisMenuShareId: // 我共享的
  834. excelIds = shareIds
  835. case excelModel.CustomAnalysisMenuSharedId: // 收到共享
  836. excelIds = sharedIds
  837. default: // 用户目录
  838. excelIds = userIds
  839. }
  840. }
  841. if len(excelIds) == 0 {
  842. page := paging.GetPaging(currentIndex, pageSize, 0)
  843. resp := response.ExcelListResp{
  844. Paging: page,
  845. List: make([]*excelModel.MyExcelInfoList, 0),
  846. }
  847. br.Ret = 200
  848. br.Success = true
  849. br.Msg = "获取成功"
  850. br.Data = resp
  851. return
  852. }
  853. condition += fmt.Sprintf(` AND excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  854. pars = append(pars, excelIds)
  855. } else {
  856. condition += ` AND sys_user_id = ?`
  857. pars = append(pars, adminId)
  858. }
  859. // 筛选分类
  860. if !isShare && excelClassifyId > 0 {
  861. _, err := excelModel.GetExcelClassifyById(excelClassifyId)
  862. if err != nil && err.Error() != utils.ErrNoRow() {
  863. br.Msg = "获取表格信息失败"
  864. br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
  865. return
  866. }
  867. childClassify, e, _ := excel2.GetChildClassifyByClassifyId(excelClassifyId, source)
  868. if e != nil && e.Error() != utils.ErrNoRow() {
  869. br.Msg = "获取失败"
  870. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  871. return
  872. }
  873. if len(childClassify) == 0 {
  874. condition += " AND excel_classify_id = ? "
  875. pars = append(pars, excelClassifyId)
  876. } else {
  877. classifyIds := []int{excelClassifyId}
  878. for _, v := range childClassify {
  879. classifyIds = append(classifyIds, v.ExcelClassifyId)
  880. }
  881. condition += fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  882. pars = append(pars, classifyIds)
  883. }
  884. }
  885. if keyword != "" {
  886. condition += ` AND (excel_name LIKE ?)`
  887. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  888. }
  889. // 获取表格信息
  890. var total int
  891. page := paging.GetPaging(currentIndex, pageSize, total)
  892. var startSize int
  893. if pageSize <= 0 {
  894. pageSize = utils.PageSize20
  895. }
  896. if currentIndex <= 0 {
  897. currentIndex = 1
  898. }
  899. startSize = paging.StartIndex(currentIndex, pageSize)
  900. list, err := excelModel.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
  901. if err != nil && err.Error() != utils.ErrNoRow() {
  902. br.Success = true
  903. br.Msg = "获取表格信息失败"
  904. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  905. return
  906. }
  907. for _, v := range list {
  908. v.HaveOperaAuth = true
  909. //v.Button.CancelShareButton = hasShareMap[v.ExcelInfoId]
  910. }
  911. // 总数据量
  912. dataCount, err := excelModel.GetExcelListCountByCondition(condition, pars)
  913. if err != nil && err.Error() != utils.ErrNoRow() {
  914. br.Msg = "获取表格列表信息失败"
  915. br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
  916. return
  917. }
  918. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  919. resp := response.ExcelListResp{
  920. Paging: page,
  921. List: list,
  922. }
  923. br.Ret = 200
  924. br.Success = true
  925. br.Msg = "获取成功"
  926. br.Data = resp
  927. }
  928. // Share
  929. // @Title 分享表格
  930. // @Description 分享表格
  931. // @Param request body request.ShareExcelInfoReq true "type json string"
  932. // @Success 200 string "操作成功"
  933. // @router /excel/share [post]
  934. func (c *CustomAnalysisController) Share() {
  935. br := new(models.BaseResponse).Init()
  936. defer func() {
  937. if br.ErrMsg != "" {
  938. br.IsSendEmail = false
  939. }
  940. c.Data["json"] = br
  941. c.ServeJSON()
  942. }()
  943. var req request.ShareExcelInfoReq
  944. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  945. if err != nil {
  946. br.Msg = "参数解析异常!"
  947. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  948. return
  949. }
  950. sysUser := c.SysUser
  951. if sysUser == nil {
  952. br.Msg = "请登录"
  953. br.ErrMsg = "请登录,SysUser Is Empty"
  954. br.Ret = 408
  955. return
  956. }
  957. if req.ExcelInfoId <= 0 {
  958. br.Msg = "请选择表格"
  959. return
  960. }
  961. excelInfo, e := excelModel.GetExcelInfoById(req.ExcelInfoId)
  962. if e != nil {
  963. if e.Error() == utils.ErrNoRow() {
  964. br.Msg = "表格不存在, 请刷新页面"
  965. return
  966. }
  967. br.Msg = "操作失败"
  968. br.ErrMsg = fmt.Sprintf("获取表格失败, %v", e)
  969. return
  970. }
  971. if excelInfo.SysUserId != sysUser.AdminId {
  972. br.Msg = "无权操作"
  973. br.ErrMsg = fmt.Sprintf("无权设置表格权限, ExcelInfoId: %d, SysAdminId: %d", req.ExcelInfoId, sysUser.AdminId)
  974. return
  975. }
  976. source := utils.CUSTOM_ANALYSIS_TABLE
  977. newPermissions := make([]*excelPermissionModel.ExcelInfoPermission, 0)
  978. for _, v := range req.ViewUserIds {
  979. newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
  980. ExcelInfoId: int32(req.ExcelInfoId),
  981. Source: int32(source),
  982. SysUserId: int32(v),
  983. CreateTime: time.Now().Local(),
  984. ModifyTime: time.Now().Local(),
  985. PermissionType: 1,
  986. })
  987. }
  988. for _, v := range req.EditUserIds {
  989. newPermissions = append(newPermissions, &excelPermissionModel.ExcelInfoPermission{
  990. ExcelInfoId: int32(req.ExcelInfoId),
  991. Source: int32(source),
  992. SysUserId: int32(v),
  993. CreateTime: time.Now().Local(),
  994. ModifyTime: time.Now().Local(),
  995. PermissionType: 2,
  996. })
  997. }
  998. if e := excelPermissionModel.ClearAndSetExcelInfoPermission(source, req.ExcelInfoId, newPermissions); e != nil {
  999. br.Msg = "操作失败"
  1000. br.ErrMsg = fmt.Sprintf("设置表格权限失败, %v", e)
  1001. return
  1002. }
  1003. br.Ret = 200
  1004. br.Success = true
  1005. br.Msg = "操作成功"
  1006. }
  1007. // ShareDetail
  1008. // @Title 分享表格-详情
  1009. // @Description 分享表格-详情
  1010. // @Param ExcelInfoId query int true "表格ID"
  1011. // @Success 200 {object} response.ShareExcelInfoDetail
  1012. // @router /excel/share_detail [get]
  1013. func (c *CustomAnalysisController) ShareDetail() {
  1014. br := new(models.BaseResponse).Init()
  1015. defer func() {
  1016. if br.ErrMsg != "" {
  1017. br.IsSendEmail = false
  1018. }
  1019. c.Data["json"] = br
  1020. c.ServeJSON()
  1021. }()
  1022. sysUser := c.SysUser
  1023. if sysUser == nil {
  1024. br.Msg = "请登录"
  1025. br.ErrMsg = "请登录,SysUser Is Empty"
  1026. br.Ret = 408
  1027. return
  1028. }
  1029. excelInfoId, _ := c.GetInt("ExcelInfoId")
  1030. if excelInfoId <= 0 {
  1031. br.Msg = "请选择表格"
  1032. return
  1033. }
  1034. permissions, e := excelPermissionModel.GetExcelPermissionBySourceAndId(excelInfoId, utils.CUSTOM_ANALYSIS_TABLE)
  1035. if e != nil {
  1036. br.Msg = "获取失败"
  1037. br.ErrMsg = fmt.Sprintf("获取表格权限失败, %v", e)
  1038. return
  1039. }
  1040. resp := new(response.ShareExcelInfoDetail)
  1041. resp.ExcelInfoId = excelInfoId
  1042. for _, v := range permissions {
  1043. if v.PermissionType == 1 {
  1044. resp.ViewUserIds = append(resp.ViewUserIds, int(v.SysUserId))
  1045. }
  1046. if v.PermissionType == 2 {
  1047. resp.EditUserIds = append(resp.EditUserIds, int(v.SysUserId))
  1048. }
  1049. }
  1050. br.Data = resp
  1051. br.Ret = 200
  1052. br.Success = true
  1053. br.Msg = "获取成功"
  1054. }
  1055. // CustomAnalysisCommonController 修复数据用的
  1056. type CustomAnalysisCommonController struct {
  1057. controllers.BaseCommonController
  1058. }
  1059. //var OnceCustomAnalysisFixed = false
  1060. // FixClassify
  1061. // @Title 修复表格分类(一次性-后续版本可删掉)
  1062. // @Description 修复表格分类
  1063. // @Param ExcelInfoId query int true "表格ID"
  1064. // @Success 200 string "操作成功"
  1065. // @router /excel/fix_classify [get]
  1066. func (c *CustomAnalysisCommonController) FixClassify() {
  1067. br := new(models.BaseResponse).Init()
  1068. defer func() {
  1069. if br.ErrMsg != "" {
  1070. br.IsSendEmail = false
  1071. }
  1072. if br.Ret == 200 {
  1073. //OnceCustomAnalysisFixed = true
  1074. }
  1075. c.Data["json"] = br
  1076. c.ServeJSON()
  1077. }()
  1078. //if OnceCustomAnalysisFixed {
  1079. // br.Ret = 200
  1080. // br.Success = true
  1081. // br.Msg = "请勿重复修复"
  1082. // return
  1083. //}
  1084. source := utils.CUSTOM_ANALYSIS_TABLE
  1085. // 获取所有分类
  1086. classifies, e := excelModel.GetExcelClassifyModelBySource(source)
  1087. if e != nil {
  1088. br.Msg = "操作失败"
  1089. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  1090. return
  1091. }
  1092. classifyMap := make(map[int]*excelModel.ExcelClassify)
  1093. for _, v := range classifies {
  1094. classifyMap[v.ExcelClassifyId] = v
  1095. }
  1096. // 查询所有excel
  1097. excels, e := excelModel.GetAllExcelInfoBySource(source)
  1098. if e != nil {
  1099. br.Msg = "操作失败"
  1100. br.ErrMsg = fmt.Sprintf("获取excel列表失败, %v", e)
  1101. return
  1102. }
  1103. if len(excels) == 0 {
  1104. br.Ret = 200
  1105. br.Success = true
  1106. br.Msg = "无分类需要修复"
  1107. return
  1108. }
  1109. // 先按照用户ID分组
  1110. groupExcels := make(map[int][]*excelModel.ExcelInfo)
  1111. for _, v := range excels {
  1112. //if v.SysUserId != 198 {
  1113. // continue
  1114. //}
  1115. if groupExcels[v.SysUserId] == nil {
  1116. groupExcels[v.SysUserId] = make([]*excelModel.ExcelInfo, 0)
  1117. }
  1118. groupExcels[v.SysUserId] = append(groupExcels[v.SysUserId], v)
  1119. }
  1120. // 递归函数
  1121. 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) {
  1122. // 分类创建人与表格不一致
  1123. currentClassify = classifyItem
  1124. if classifyItem.SysUserId != userId {
  1125. // 是否已存在替换分类
  1126. _, ok := replaceId[classifyItem.ExcelClassifyId]
  1127. if !ok {
  1128. classifyNew := &excelModel.ExcelClassify{
  1129. ExcelClassifyName: fmt.Sprintf("%d%s", userId, classifyItem.ExcelClassifyName),
  1130. ParentId: classifyItem.ParentId, // 根据updateChild递归外更新
  1131. Source: source,
  1132. SysUserId: excelItem.SysUserId,
  1133. SysUserRealName: excelItem.SysUserRealName,
  1134. Level: classifyItem.Level,
  1135. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  1136. Sort: classifyItem.Sort,
  1137. CreateTime: time.Now(),
  1138. ModifyTime: time.Now(),
  1139. }
  1140. _, e = excelModel.AddExcelClassify(classifyNew)
  1141. if e != nil {
  1142. err = fmt.Errorf("AddExcelClassify, %v", e)
  1143. return
  1144. }
  1145. replaceId[classifyItem.ExcelClassifyId] = classifyNew
  1146. excelItem.ExcelClassifyId = classifyNew.ExcelClassifyId
  1147. currentClassify = classifyNew
  1148. classifyMap[classifyNew.ExcelClassifyId] = classifyNew
  1149. } else {
  1150. excelItem.ExcelClassifyId = replaceId[classifyItem.ExcelClassifyId].ExcelClassifyId
  1151. currentClassify = replaceId[classifyItem.ExcelClassifyId]
  1152. }
  1153. // 仅第一次递归时修改
  1154. if firstStep {
  1155. //fmt.Printf("修改ExcelClassifyId: %d\n", excelItem.ExcelClassifyId)
  1156. excelItem.ModifyTime = time.Now().Local()
  1157. if e = excelItem.Update([]string{"ExcelClassifyId", "ModifyTime"}); e != nil {
  1158. err = fmt.Errorf("UpdateExcelInfo, %v", e)
  1159. return
  1160. }
  1161. }
  1162. }
  1163. // 查上级
  1164. if classifyItem.ParentId <= 0 {
  1165. //fmt.Printf("终止递归, ClassifyId: %d", classifyItem.ExcelClassifyId)
  1166. return
  1167. }
  1168. hasParent = true
  1169. parent := classifyMap[classifyItem.ParentId]
  1170. if parent == nil {
  1171. err = fmt.Errorf("未找到上级分类, ClassifyId: %d, ParentId: %d", classifyItem.ExcelClassifyId, classifyItem.ParentId)
  1172. return
  1173. }
  1174. if parent.SysUserId != userId {
  1175. updateChild = true
  1176. }
  1177. return
  1178. }
  1179. for uid, gv := range groupExcels {
  1180. replaceId := make(map[int]*excelModel.ExcelClassify)
  1181. for _, v := range gv {
  1182. classifyItem, e := excelModel.GetExcelClassifyById(v.ExcelClassifyId)
  1183. if e != nil {
  1184. br.Msg = "操作失败"
  1185. br.ErrMsg = fmt.Sprintf("获取表格分类失败, ExcelClassifyId: %d, %v", v.ExcelClassifyId, e)
  1186. return
  1187. }
  1188. // 首次递归
  1189. hasParent, updateChild, currentClassify, e := findAndReplace(replaceId, uid, v, classifyItem, true)
  1190. if e != nil {
  1191. br.Msg = "操作失败"
  1192. br.ErrMsg = fmt.Sprintf("findAndReplace1, %v", e)
  1193. return
  1194. }
  1195. if !hasParent {
  1196. continue
  1197. }
  1198. // 二次递归
  1199. //fmt.Printf("二次递归: %d\n", currentClassify.ParentId)
  1200. parentClassify, e := excelModel.GetExcelClassifyById(currentClassify.ParentId)
  1201. if e != nil {
  1202. br.Msg = "操作失败"
  1203. br.ErrMsg = fmt.Sprintf("2-parent-GetExcelClassifyById, %v", e)
  1204. return
  1205. }
  1206. hasParent2, updateChild2, currentClassify2, e := findAndReplace(replaceId, uid, v, parentClassify, false)
  1207. if e != nil {
  1208. br.Msg = "操作失败"
  1209. br.ErrMsg = fmt.Sprintf("findAndReplace2, %v", e)
  1210. return
  1211. }
  1212. // 更新上次递归的parent_id
  1213. if updateChild {
  1214. currentClassify.ParentId = currentClassify2.ExcelClassifyId
  1215. currentClassify.ModifyTime = time.Now().Local()
  1216. if e = currentClassify.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1217. br.Msg = "操作失败"
  1218. br.ErrMsg = fmt.Sprintf("2-更新父级分类ID失败, %v", e)
  1219. return
  1220. }
  1221. }
  1222. if !hasParent2 {
  1223. continue
  1224. }
  1225. // 三次递归
  1226. //fmt.Printf("三次递归: %d\n", currentClassify2.ParentId)
  1227. parentClassify2, e := excelModel.GetExcelClassifyById(currentClassify2.ParentId)
  1228. if e != nil {
  1229. br.Msg = "操作失败"
  1230. br.ErrMsg = fmt.Sprintf("3-parent-GetExcelClassifyById, %v", e)
  1231. return
  1232. }
  1233. hasParent3, updateChild3, currentClassify3, e := findAndReplace(replaceId, uid, v, parentClassify2, false)
  1234. if e != nil {
  1235. br.Msg = "操作失败"
  1236. br.ErrMsg = fmt.Sprintf("findAndReplace3, %v", e)
  1237. return
  1238. }
  1239. if updateChild2 {
  1240. currentClassify2.ParentId = currentClassify3.ExcelClassifyId
  1241. currentClassify2.ModifyTime = time.Now().Local()
  1242. if e = currentClassify2.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1243. br.Msg = "操作失败"
  1244. br.ErrMsg = fmt.Sprintf("3-更新父级分类ID失败, %v", e)
  1245. return
  1246. }
  1247. }
  1248. if !hasParent3 {
  1249. continue
  1250. }
  1251. // 四次递归
  1252. //fmt.Printf("四次递归: %d\n", currentClassify3.ParentId)
  1253. parentClassify3, e := excelModel.GetExcelClassifyById(currentClassify3.ParentId)
  1254. if e != nil {
  1255. br.Msg = "操作失败"
  1256. br.ErrMsg = fmt.Sprintf("4-parent-GetExcelClassifyById, %v", e)
  1257. return
  1258. }
  1259. hasParent4, updateChild4, currentClassify4, e := findAndReplace(replaceId, uid, v, parentClassify3, false)
  1260. if e != nil {
  1261. br.Msg = "操作失败"
  1262. br.ErrMsg = fmt.Sprintf("findAndReplace4, %v", e)
  1263. return
  1264. }
  1265. if updateChild3 {
  1266. currentClassify3.ParentId = currentClassify4.ExcelClassifyId
  1267. currentClassify3.ModifyTime = time.Now().Local()
  1268. if e = currentClassify3.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1269. br.Msg = "操作失败"
  1270. br.ErrMsg = fmt.Sprintf("4-更新父级分类ID失败, %v", e)
  1271. return
  1272. }
  1273. }
  1274. if !hasParent4 {
  1275. continue
  1276. }
  1277. // 五次递归-理论上没了(自定义分析最多三级目录+最底层的表格)
  1278. //fmt.Printf("五次递归: %d\n", currentClassify4.ParentId)
  1279. parentClassify4, e := excelModel.GetExcelClassifyById(currentClassify4.ParentId)
  1280. if e != nil {
  1281. br.Msg = "操作失败"
  1282. br.ErrMsg = fmt.Sprintf("5-parent-GetExcelClassifyById, %v", e)
  1283. return
  1284. }
  1285. hasParent5, _, currentClassify5, e := findAndReplace(replaceId, uid, v, parentClassify4, false)
  1286. if e != nil {
  1287. br.Msg = "操作失败"
  1288. br.ErrMsg = fmt.Sprintf("findAndReplace5, %v", e)
  1289. return
  1290. }
  1291. if updateChild4 {
  1292. currentClassify4.ParentId = currentClassify5.ExcelClassifyId
  1293. currentClassify4.ModifyTime = time.Now().Local()
  1294. if e = currentClassify4.Update([]string{"ParentId", "ModifyTime"}); e != nil {
  1295. br.Msg = "操作失败"
  1296. br.ErrMsg = fmt.Sprintf("5-更新父级分类ID失败, %v", e)
  1297. return
  1298. }
  1299. }
  1300. if !hasParent5 {
  1301. continue
  1302. }
  1303. }
  1304. //fmt.Println("replaceId: ", replaceId)
  1305. }
  1306. br.Ret = 200
  1307. br.Success = true
  1308. br.Msg = "操作成功"
  1309. }