base_from_clarksons.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "time"
  9. )
  10. func AddClarksonsClassify(classifyName string, parentId int) (ok bool, msg string, err error) {
  11. var count int
  12. count, err = data_manage.GetBaseFromClarksonsClassifyCount(classifyName, parentId)
  13. if err != nil {
  14. return
  15. }
  16. if count > 0 {
  17. return false, "分类已存在", nil
  18. }
  19. sort, err := data_manage.GetChildClarksonsClassifyMaxSortById(parentId)
  20. if err != nil {
  21. return false, "", err
  22. }
  23. if parentId > 0 {
  24. count, err = data_manage.GetClarksonsClassifyCountById(parentId)
  25. if err != nil {
  26. return false, "", err
  27. }
  28. if count == 0 {
  29. return false, "", errors.New("父分类不存在")
  30. }
  31. clarksonsClassify := &data_manage.BaseFromClarksonsClassify{
  32. ClassifyName: classifyName,
  33. ParentId: parentId,
  34. Level: 2,
  35. Sort: sort + 1,
  36. ModifyTime: time.Now(),
  37. CreateTime: time.Now(),
  38. }
  39. _, err = clarksonsClassify.Add()
  40. if err != nil {
  41. return
  42. }
  43. } else {
  44. clarksonsClassify := &data_manage.BaseFromClarksonsClassify{
  45. ClassifyName: classifyName,
  46. ParentId: 0,
  47. Level: 1,
  48. Sort: sort + 1,
  49. ModifyTime: time.Now(),
  50. CreateTime: time.Now(),
  51. }
  52. _, er := clarksonsClassify.Add()
  53. if er != nil {
  54. return false, "", er
  55. }
  56. }
  57. return true, "", nil
  58. }
  59. func DelClarksonsClassify(classifyId int) (err error) {
  60. classify, err := data_manage.GetClarksonsClassifyById(classifyId)
  61. if err != nil {
  62. return
  63. }
  64. var classifyIds []int
  65. classifyIds = append(classifyIds, classify.BaseFromClassifyId)
  66. if classify.ParentId == 0 {
  67. tmpClassifyIds, er := data_manage.GetChildClarksonsClassifyIdsById(classify.BaseFromClassifyId)
  68. if er != nil {
  69. err = er
  70. return
  71. }
  72. classifyIds = append(classifyIds, tmpClassifyIds...)
  73. }
  74. // 获取分类下的所有指标
  75. count, err := data_manage.GetClarksonsIndexCountByClassifyIds(classifyIds)
  76. if err != nil {
  77. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  78. return
  79. }
  80. if count > 0 {
  81. err = errors.New("该分类下有指标,不可删除")
  82. return
  83. }
  84. // 删除对应的分类
  85. err = data_manage.DeleteClarksonsClassifyByClassifyId(classifyIds)
  86. if err != nil {
  87. return
  88. }
  89. return
  90. }
  91. // ResetClarksonsIndex 指标数据清除分类接口
  92. func ResetClarksonsIndex(indexId int) (err error) {
  93. clarksonsIndex, err := data_manage.GetClarksonsIndexByIndexId(indexId)
  94. if err != nil {
  95. return
  96. }
  97. // 更新指标在未分类下的排序,永远排在未分类的最后一个
  98. //移动排序
  99. classifyId := 0
  100. updateCol := make([]string, 0)
  101. var currentSort, prevSort int
  102. currentSort = clarksonsIndex.Sort
  103. //未分类的最大的sort值
  104. prevSort, err = data_manage.GetClarksonsIndexMaxSortByClassifyId(classifyId)
  105. if err != nil {
  106. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  107. return
  108. }
  109. //如果前面一个节点的sort值比当前大,则当前节点往下移动
  110. if prevSort >= currentSort {
  111. //往下移动
  112. err = data_manage.MoveDownSciIndexBySort(classifyId, prevSort, currentSort)
  113. if err != nil {
  114. err = errors.New("向下移动出错:" + err.Error())
  115. return
  116. }
  117. clarksonsIndex.Sort = prevSort
  118. }
  119. //更新
  120. clarksonsIndex.ModifyTime = time.Now()
  121. clarksonsIndex.ClassifyId = classifyId
  122. updateCol = append(updateCol, "Sort", "ModifyTime", "BaseFromClassifyId")
  123. err = clarksonsIndex.Update(updateCol)
  124. if err != nil {
  125. err = errors.New("移动失败,Err:" + err.Error())
  126. return
  127. }
  128. return
  129. }
  130. // 批量删除克拉克森指标
  131. func BatchDelClarksonsData(indexIds []int) (existIndex []*data_manage.BaseFromClarksonsIndex, err error) {
  132. clarksonsIndexList, err := data_manage.GetClarksonsIndexListByIndexIds(indexIds)
  133. if err != nil {
  134. return
  135. }
  136. indexCodes := make([]string, 0)
  137. codeToIndex := make(map[string]*data_manage.BaseFromClarksonsIndex)
  138. for _, v := range clarksonsIndexList {
  139. indexCodes = append(indexCodes, v.IndexCode)
  140. codeToIndex[v.IndexCode] = v
  141. }
  142. edbInfoList, err := data_manage.GetEdbInfoListByEdbCodes(utils.DATA_SOURCE_CLARKSONS, indexCodes)
  143. if err != nil {
  144. return
  145. }
  146. deleteIndexIds := make([]int, 0)
  147. for _, v := range edbInfoList {
  148. if index, ok := codeToIndex[v.EdbCode]; ok {
  149. existIndex = append(existIndex, index)
  150. delete(codeToIndex, v.EdbCode)
  151. }
  152. }
  153. for _, v := range codeToIndex {
  154. deleteIndexIds = append(deleteIndexIds, v.BaseFromClarksonsIndexId)
  155. }
  156. // 删除对应的指标
  157. err = data_manage.DeleteClarksonsIndexByIds(deleteIndexIds)
  158. if err != nil {
  159. return
  160. }
  161. return
  162. }
  163. // DelClarksonsData 删除克拉克森指标
  164. func DelClarksonsData(indexId int) (err error, errMsg string) {
  165. baseFromClarksonsIndex, err := data_manage.GetClarksonsIndexByIndexId(indexId)
  166. if err != nil {
  167. errMsg = `获取数据失败`
  168. if err.Error() == utils.ErrNoRow() {
  169. errMsg = `该指标未入库`
  170. err = nil
  171. }
  172. return
  173. }
  174. // 获取已经加入到EDB指标库的clarksons指标
  175. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_CLARKSONS, baseFromClarksonsIndex.IndexCode)
  176. if err != nil && err.Error() != utils.ErrNoRow() {
  177. errMsg = "删除失败"
  178. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  179. return
  180. }
  181. if edbInfo != nil {
  182. errMsg = "当前指标已被引用,不可删除"
  183. err = nil
  184. return
  185. }
  186. // 删除对应的指标
  187. err = data_manage.DeleteClarksonsIndexById(indexId)
  188. if err != nil {
  189. return
  190. }
  191. return
  192. }
  193. // EditClarksonsIndex 编辑克拉克森指标
  194. func EditClarksonsIndex(indexId, classifyId int) (baseFromClarksonsIndex *data_manage.BaseFromClarksonsIndex, errMsg string, err error) {
  195. baseFromClarksonsIndex, err = data_manage.GetClarksonsIndexByIndexId(indexId)
  196. if err != nil {
  197. if err.Error() == utils.ErrNoRow() {
  198. errMsg = `该指标未入库`
  199. err = nil
  200. }
  201. return
  202. }
  203. if classifyId > 0 {
  204. classify, e := data_manage.GetClarksonsClassifyById(classifyId)
  205. if e != nil {
  206. err = e
  207. if err.Error() == utils.ErrNoRow() {
  208. errMsg = `该分类不存在或已删除`
  209. err = nil
  210. }
  211. return
  212. }
  213. if classify.Level == 1 { // 顶级分类
  214. errMsg = `不合法的编辑`
  215. err = nil
  216. return
  217. }
  218. }
  219. maxSort, err := data_manage.GetChildClarksonsClassifyMaxSortById(classifyId)
  220. if err != nil {
  221. return
  222. }
  223. baseFromClarksonsIndex.ClassifyId = classifyId
  224. baseFromClarksonsIndex.Sort = maxSort + 1
  225. baseFromClarksonsIndex.ModifyTime = time.Now()
  226. err = baseFromClarksonsIndex.Update([]string{"classify_id", "sort", "modify_time"})
  227. return
  228. }
  229. type ClarksonsIndexSource2EdbReq struct {
  230. EdbCode string
  231. EdbName string
  232. Frequency string
  233. Unit string
  234. ClassifyId int
  235. AdminId int
  236. AdminRealName string
  237. }
  238. // ClarksonsIndexSource2Edb 新增克拉克森数据从数据源到指标库
  239. func ClarksonsIndexSource2Edb(req ClarksonsIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, errMsg string, skip bool, err error) {
  240. if req.EdbCode == "" {
  241. err = fmt.Errorf("指标ID为空")
  242. return
  243. }
  244. defer func() {
  245. if err != nil {
  246. tips := fmt.Sprintf("ClarksonsIndexSource2Edb新增失败, Err: %s", err.Error())
  247. fmt.Println(tips)
  248. utils.FileLog.Info(tips)
  249. }
  250. }()
  251. source := utils.DATA_SOURCE_CLARKSONS
  252. // 是否新增过指标
  253. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  254. if e != nil && e.Error() != utils.ErrNoRow() {
  255. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  256. return
  257. }
  258. if exist != nil {
  259. skip = true
  260. return
  261. }
  262. // 开始结束时间
  263. var startDate, endDate string
  264. // 新增指标库
  265. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  266. if e != nil {
  267. errMsg = msg
  268. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  269. return
  270. }
  271. edb = edbInfo
  272. return
  273. }
  274. // MoveClarksonsClassify 移动克拉克森分类
  275. func MoveClarksonsClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  276. //判断分类是否存在
  277. classifyInfo, err := data_manage.GetClarksonsClassifyById(classifyId)
  278. if err != nil {
  279. errMsg = "移动失败"
  280. err = errors.New("获取分类信息失败,Err:" + err.Error())
  281. return
  282. }
  283. updateCol := make([]string, 0)
  284. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  285. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  286. // 校验移动的父级目录下是否有重名分类
  287. count, e := data_manage.GetBaseFromClarksonsClassifyCount(classifyInfo.ClassifyName, parentClassifyId)
  288. if e != nil && e.Error() != utils.ErrNoRow() {
  289. errMsg = "移动失败"
  290. err = errors.New("获取父级目录下的同名分类失败, Err: " + e.Error())
  291. return
  292. }
  293. if count > 0 {
  294. errMsg = "移动失败,分类名称已存在"
  295. err = errors.New("该父级目录下已存在同名分类,请重新命名")
  296. return
  297. }
  298. parentClassifyInfo, tmpErr := data_manage.GetClarksonsClassifyById(parentClassifyId)
  299. if tmpErr != nil {
  300. errMsg = "移动失败"
  301. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  302. return
  303. }
  304. classifyInfo.ParentId = parentClassifyInfo.BaseFromClassifyId
  305. classifyInfo.Level = parentClassifyInfo.Level + 1
  306. updateCol = append(updateCol, "ParentId", "Level")
  307. }
  308. var currentSort, prevSort, nextSort int
  309. currentSort = classifyInfo.Sort
  310. var prevClassify *data_manage.BaseFromClarksonsClassify
  311. var nextClassify *data_manage.BaseFromClarksonsClassify
  312. //如果有传入 上一个兄弟节点分类id
  313. if prevClassifyId > 0 {
  314. prevClassify, err = data_manage.GetClarksonsClassifyById(prevClassifyId)
  315. if err != nil {
  316. if err.Error() == utils.ErrNoRow() {
  317. errMsg = "移动失败"
  318. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  319. return
  320. }
  321. errMsg = "移动失败"
  322. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  323. return
  324. }
  325. prevSort = prevClassify.Sort
  326. }
  327. if nextClassifyId > 0 {
  328. //下一个兄弟节点
  329. nextClassify, err = data_manage.GetClarksonsClassifyById(nextClassifyId)
  330. if err != nil {
  331. if err.Error() == utils.ErrNoRow() {
  332. errMsg = "移动失败"
  333. err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  334. return
  335. }
  336. errMsg = "移动失败"
  337. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  338. return
  339. }
  340. nextSort = nextClassify.Sort
  341. }
  342. // 修改历史数据中的排序为0的情况
  343. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  344. //更新为0排序的数据为当前最小排序
  345. if nextClassify != nil {
  346. minSort, e := data_manage.GetClarksonsIndexClassifyMinSort(parentClassifyId)
  347. if e != nil {
  348. errMsg = "移动失败"
  349. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  350. return
  351. }
  352. if minSort > 1 {
  353. minSort -= 1
  354. }
  355. nextClassify.Sort = minSort
  356. err = nextClassify.Update([]string{"sort"})
  357. if err != nil {
  358. errMsg = "移动失败"
  359. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  360. return
  361. }
  362. nextSort = minSort
  363. }
  364. }
  365. //移到两个排序值中间操作
  366. if prevSort >= currentSort {
  367. //往下移动
  368. err = data_manage.MoveDownClarksonsIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  369. if err != nil {
  370. err = errors.New("向下移动出错:" + err.Error())
  371. return
  372. }
  373. classifyInfo.Sort = prevSort
  374. } else if nextSort <= currentSort && nextSort != 0 {
  375. //往上移动
  376. err = data_manage.MoveUpClarksonsIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  377. if err != nil {
  378. err = errors.New("向上移动出错:" + err.Error())
  379. return
  380. }
  381. classifyInfo.Sort = nextSort
  382. }
  383. classifyInfo.ModifyTime = time.Now()
  384. updateCol = append(updateCol, "Sort", "ModifyTime")
  385. err = classifyInfo.Update(updateCol)
  386. if err != nil {
  387. errMsg = "移动失败"
  388. err = errors.New("修改失败,Err:" + err.Error())
  389. return
  390. }
  391. return
  392. }
  393. // MoveClarksonsData 移动克拉克森指标
  394. func MoveClarksonsData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  395. //分类信息
  396. clarksonsIndex, err := data_manage.GetClarksonsIndexByIndexId(indexId)
  397. if err != nil {
  398. errMsg = `获取数据失败`
  399. if err.Error() == utils.ErrNoRow() {
  400. errMsg = `该指标未入库`
  401. err = nil
  402. }
  403. return
  404. }
  405. //判断分类是否存在
  406. if classifyId > 0 {
  407. _, err = data_manage.GetClarksonsClassifyById(classifyId)
  408. if err != nil {
  409. errMsg = "移动失败"
  410. err = errors.New("获取分类信息失败,Err:" + err.Error())
  411. if err.Error() == utils.ErrNoRow() {
  412. errMsg = "找不到该分类"
  413. err = nil
  414. }
  415. return
  416. }
  417. }
  418. //如果改变了分类,那么移动该图表数据
  419. if clarksonsIndex.ClassifyId != classifyId {
  420. tmpErr := data_manage.MoveClarksonsIndex(indexId, classifyId)
  421. if tmpErr != nil {
  422. errMsg = "移动失败"
  423. err = errors.New("移动失败,Err:" + tmpErr.Error())
  424. return
  425. }
  426. }
  427. //移动排序
  428. updateCol := make([]string, 0)
  429. var currentSort, prevSort, nextSort int
  430. currentSort = clarksonsIndex.Sort
  431. //如果有传入 上一个兄弟节点分类id
  432. var prevIndex *data_manage.BaseFromClarksonsIndex
  433. var nextIndex *data_manage.BaseFromClarksonsIndex
  434. if prevIndexId > 0 {
  435. prevIndex, err = data_manage.GetClarksonsIndexByIndexId(prevIndexId)
  436. if err != nil {
  437. if err.Error() == utils.ErrNoRow() {
  438. errMsg = "移动失败"
  439. err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
  440. return
  441. }
  442. errMsg = "移动失败"
  443. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  444. return
  445. }
  446. prevSort = prevIndex.Sort
  447. }
  448. if nextIndexId > 0 {
  449. //下一个兄弟节点
  450. nextIndex, err = data_manage.GetClarksonsIndexByIndexId(nextIndexId)
  451. if err != nil {
  452. if err.Error() == utils.ErrNoRow() {
  453. errMsg = "移动失败"
  454. err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
  455. return
  456. }
  457. errMsg = "移动失败"
  458. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
  459. return
  460. }
  461. nextSort = nextIndex.Sort
  462. }
  463. // 修改历史数据中的排序为0的情况
  464. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
  465. //更新为0排序的数据为当前最小排序
  466. if nextIndex != nil {
  467. minSort, e := data_manage.GetClarksonsIndexMinSortByClassifyId(classifyId)
  468. if e != nil {
  469. errMsg = "移动失败"
  470. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  471. return
  472. }
  473. if minSort > 1 {
  474. minSort -= 1
  475. }
  476. nextIndex.Sort = minSort
  477. err = nextIndex.Update([]string{"Sort"})
  478. if err != nil {
  479. errMsg = "移动失败"
  480. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  481. return
  482. }
  483. nextSort = minSort
  484. }
  485. }
  486. //移到两个排序值中间操作
  487. if prevSort >= currentSort {
  488. //往下移动
  489. err = data_manage.MoveDownClarksonsIndexBySort(classifyId, prevSort, currentSort)
  490. if err != nil {
  491. err = errors.New("向下移动出错:" + err.Error())
  492. return
  493. }
  494. clarksonsIndex.Sort = prevSort
  495. } else if nextSort <= currentSort && nextSort != 0 {
  496. //往上移动
  497. err = data_manage.MoveUpClarksonsIndexBySort(classifyId, nextSort, currentSort)
  498. if err != nil {
  499. err = errors.New("向上移动出错:" + err.Error())
  500. return
  501. }
  502. clarksonsIndex.Sort = nextSort
  503. }
  504. //更新
  505. clarksonsIndex.ModifyTime = time.Now()
  506. updateCol = append(updateCol, "Sort", "ModifyTime")
  507. err = clarksonsIndex.Update(updateCol)
  508. if err != nil {
  509. errMsg = "移动失败"
  510. err = errors.New("修改失败,Err:" + err.Error())
  511. return
  512. }
  513. return
  514. }
  515. // GetClarksonsIndexInfo 获取指标信息-分页
  516. func GetClarksonsIndexInfo(keyWord string, classifyIdList []string, frequencyList []string, currentIndex, startSize, pageSize int) (rzdIndexInfoList *data_manage.BaseFromRzdIndexPage, err error) {
  517. // 获取指标
  518. var condition string
  519. var pars []interface{}
  520. if keyWord != "" {
  521. condition += ` AND (index_name like ? or index_code like ?)`
  522. pars = append(pars, "%"+keyWord+"%", "%"+keyWord+"%")
  523. }
  524. if len(classifyIdList) > 0 {
  525. condition += ` AND classify_id IN (`
  526. for _, v := range classifyIdList {
  527. condition += `?,`
  528. pars = append(pars, v)
  529. }
  530. condition = condition[:len(condition)-1] + `)`
  531. }
  532. if len(frequencyList) > 0 {
  533. condition += ` AND frequency IN (`
  534. for _, v := range frequencyList {
  535. condition += `?,`
  536. pars = append(pars, v)
  537. }
  538. condition = condition[:len(condition)-1] + `)`
  539. }
  540. count, err := data_manage.GetClarksonsIndexInfoCount(condition, pars)
  541. if err != nil {
  542. return nil, err
  543. }
  544. indexPage := data_manage.BaseFromRzdIndexPage{}
  545. page := paging.GetPaging(currentIndex, pageSize, count)
  546. if count <= 0 {
  547. indexPage.Paging = page
  548. return &indexPage, nil
  549. }
  550. condition += ` ORDER BY base_from_clarksons_index_id asc`
  551. // 分页
  552. condition += ` LIMIT ?, ?`
  553. pars = append(pars, startSize, pageSize)
  554. indexInfoPage, err := data_manage.GetClarksonsIndexInfoPage(condition, pars)
  555. if err != nil {
  556. return nil, err
  557. }
  558. var indexCodes []string
  559. for _, indexInfo := range indexInfoPage {
  560. indexCodes = append(indexCodes, indexInfo.IndexCode)
  561. }
  562. IndexDataList, err := data_manage.GetClarksonsLastUpdateTimeLastByIndexCode(indexCodes)
  563. if err != nil {
  564. return nil, err
  565. }
  566. var indexDataMap = make(map[string]*data_manage.BaseFromClarksonsData, 0)
  567. for _, data := range IndexDataList {
  568. indexDataMap[data.IndexCode] = data
  569. }
  570. for _, indexInfo := range indexInfoPage {
  571. if indexDataMap[indexInfo.IndexCode] == nil {
  572. continue
  573. }
  574. indexInfo.ModifyTimeMax = indexDataMap[indexInfo.IndexCode].DataTime
  575. indexInfo.Value = indexDataMap[indexInfo.IndexCode].Value
  576. }
  577. indexPage.List = indexInfoPage
  578. indexPage.Paging = page
  579. return &indexPage, nil
  580. }
  581. // GetClarkssonsIndexList 获取指标列表
  582. func GetClarkssonsIndexList(searchParams string) (rzdIndexInfoList []*data_manage.BaseFromRzdIndexList, err error) {
  583. // 获取指标
  584. var condition string
  585. var pars []interface{}
  586. if searchParams != "" {
  587. condition += ` and index_code like ? or index_name like ?`
  588. pars = append(pars, "%"+searchParams+"%", "%"+searchParams+"%")
  589. }
  590. rzdIndexList, err := data_manage.GetRzdIndex(condition, pars)
  591. if err != nil {
  592. return nil, err
  593. }
  594. return rzdIndexList, nil
  595. }
  596. // MoveClarksonsClassify 移动克拉克森分类
  597. func MoveClarksonsClassifyV2(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  598. //判断分类是否存在
  599. classifyInfo, err := data_manage.GetClarksonsClassifyById(classifyId)
  600. if err != nil {
  601. errMsg = "移动失败"
  602. err = errors.New("获取分类信息失败,Err:" + err.Error())
  603. return
  604. }
  605. updateCol := make([]string, 0)
  606. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  607. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  608. // 校验移动的父级目录下是否有重名分类
  609. count, e := data_manage.GetBaseFromClarksonsClassifyCount(classifyInfo.ClassifyName, parentClassifyId)
  610. if e != nil && e.Error() != utils.ErrNoRow() {
  611. errMsg = "移动失败"
  612. err = errors.New("获取父级目录下的同名分类失败, Err: " + e.Error())
  613. return
  614. }
  615. if count > 0 {
  616. errMsg = "移动失败,分类名称已存在"
  617. err = errors.New("该父级目录下已存在同名分类,请重新命名")
  618. return
  619. }
  620. parentClassifyInfo, tmpErr := data_manage.GetClarksonsClassifyById(parentClassifyId)
  621. if tmpErr != nil {
  622. errMsg = "移动失败"
  623. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  624. return
  625. }
  626. classifyInfo.ParentId = parentClassifyInfo.BaseFromClassifyId
  627. classifyInfo.Level = parentClassifyInfo.Level + 1
  628. updateCol = append(updateCol, "ParentId", "Level")
  629. }
  630. //var currentSort, prevSort, nextSort int
  631. //currentSort = classifyInfo.Sort
  632. var prevClassify *data_manage.BaseFromClarksonsClassify
  633. var nextClassify *data_manage.BaseFromClarksonsClassify
  634. //如果有传入 上一个兄弟节点分类id
  635. if prevClassifyId > 0 {
  636. prevClassify, err = data_manage.GetClarksonsClassifyById(prevClassifyId)
  637. if err != nil {
  638. if err.Error() == utils.ErrNoRow() {
  639. errMsg = "移动失败"
  640. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  641. return
  642. }
  643. errMsg = "移动失败"
  644. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  645. return
  646. }
  647. //如果是移动在两个兄弟节点之间
  648. if nextClassifyId > 0 {
  649. //下一个兄弟节点
  650. nextClassify, err = data_manage.GetClarksonsClassifyById(nextClassifyId)
  651. if err != nil {
  652. errMsg = "移动失败"
  653. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  654. return
  655. }
  656. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  657. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == classifyInfo.Sort {
  658. //变更兄弟节点的排序
  659. updateSortStr := `sort + 2`
  660. _ = data_manage.UpdateClarksonsClassifySortByParentId(prevClassify.ParentId, prevClassify.BaseFromClassifyId, prevClassify.Sort, updateSortStr)
  661. } else {
  662. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  663. if nextClassify.Sort-prevClassify.Sort == 1 {
  664. //变更兄弟节点的排序
  665. updateSortStr := `sort + 1`
  666. _ = data_manage.UpdateClarksonsClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  667. }
  668. }
  669. }
  670. classifyInfo.Sort = prevClassify.Sort + 1
  671. classifyInfo.ModifyTime = time.Now()
  672. updateCol = append(updateCol, "Sort", "ModifyTime")
  673. } else {
  674. firstClassify, e := data_manage.GetFirstClarksonsClassifyByParentId(classifyInfo.ParentId)
  675. if e != nil && e.Error() != utils.ErrNoRow() {
  676. err = e
  677. errMsg = "移动失败"
  678. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error())
  679. return
  680. }
  681. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  682. if firstClassify != nil && firstClassify.Sort == 0 {
  683. updateSortStr := ` sort + 1 `
  684. _ = data_manage.UpdateClarksonsClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
  685. }
  686. classifyInfo.Sort = 0 //那就是排在第一位
  687. classifyInfo.ModifyTime = time.Now()
  688. updateCol = append(updateCol, "Sort", "ModifyTime")
  689. }
  690. //if nextClassifyId > 0 {
  691. // //下一个兄弟节点
  692. // nextClassify, err = data_manage.GetClarksonsClassifyById(nextClassifyId)
  693. // if err != nil {
  694. // if err.Error() == utils.ErrNoRow() {
  695. // errMsg = "移动失败"
  696. // err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  697. // return
  698. // }
  699. // errMsg = "移动失败"
  700. // err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  701. // return
  702. // }
  703. // nextSort = nextClassify.Sort
  704. //}
  705. //
  706. //// 修改历史数据中的排序为0的情况
  707. //if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  708. // //更新为0排序的数据为当前最小排序
  709. // if nextClassify != nil {
  710. // minSort, e := data_manage.GetClarksonsIndexClassifyMinSort(parentClassifyId)
  711. // if e != nil {
  712. // errMsg = "移动失败"
  713. // err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  714. // return
  715. // }
  716. // if minSort > 1 {
  717. // minSort -= 1
  718. // }
  719. // nextClassify.Sort = minSort
  720. // err = nextClassify.Update([]string{"sort"})
  721. // if err != nil {
  722. // errMsg = "移动失败"
  723. // err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  724. // return
  725. // }
  726. // nextSort = minSort
  727. // }
  728. //}
  729. ////移到两个排序值中间操作
  730. //if prevSort >= currentSort {
  731. // //往下移动
  732. // err = data_manage.MoveDownClarksonsIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  733. // if err != nil {
  734. // err = errors.New("向下移动出错:" + err.Error())
  735. // return
  736. // }
  737. // classifyInfo.Sort = prevSort
  738. //} else if nextSort <= currentSort && nextSort != 0 {
  739. // //往上移动
  740. // err = data_manage.MoveUpClarksonsIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  741. // if err != nil {
  742. // err = errors.New("向上移动出错:" + err.Error())
  743. // return
  744. // }
  745. // classifyInfo.Sort = nextSort
  746. //}
  747. //
  748. //classifyInfo.ModifyTime = time.Now()
  749. //updateCol = append(updateCol, "Sort", "ModifyTime")
  750. //err = classifyInfo.Update(updateCol)
  751. //if err != nil {
  752. // errMsg = "移动失败"
  753. // err = errors.New("修改失败,Err:" + err.Error())
  754. // return
  755. //}
  756. //更新
  757. if len(updateCol) > 0 {
  758. err = classifyInfo.Update(updateCol)
  759. if err != nil {
  760. errMsg = "移动失败"
  761. err = errors.New("修改失败,Err:" + err.Error())
  762. return
  763. }
  764. }
  765. return
  766. }