base_from_baiinfo.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. package data
  2. import (
  3. "errors"
  4. "hongze/hz_crm_api/models/data_manage"
  5. "hongze/hz_crm_api/models/system"
  6. "hongze/hz_crm_api/utils"
  7. "time"
  8. )
  9. // AddBaiinfoClassify 添加SCI分类
  10. func AddBaiinfoClassify(classifyName string, parentId, sysUserId int, sysUserName string) (classifyInfo *data_manage.BaseFromBaiinfoClassify, err error, errMsg string) {
  11. count, err := data_manage.GetBaseFromBaiinfoClassifyCount(classifyName, parentId)
  12. if err != nil {
  13. errMsg = `判断名称是否已存在失败`
  14. return
  15. }
  16. if count > 0 {
  17. errMsg = `分类名称已存在,请重新输入`
  18. return
  19. }
  20. level := 1
  21. if parentId > 0 {
  22. parentClassify, e := data_manage.GetBaseFromBaiinfoClassifyById(parentId)
  23. if e != nil {
  24. if e.Error() == utils.ErrNoRow() {
  25. err = e
  26. errMsg = `一级分类不存在`
  27. return
  28. }
  29. err = e
  30. errMsg = `查询一级分类失败`
  31. return
  32. }
  33. level = parentClassify.Level + 1
  34. }
  35. classifyInfo = &data_manage.BaseFromBaiinfoClassify{
  36. ClassifyName: classifyName,
  37. ParentId: parentId,
  38. SysUserId: sysUserId,
  39. SysUserRealName: sysUserName,
  40. Sort: 0,
  41. Level: level,
  42. ModifyTime: time.Now(),
  43. CreateTime: time.Now(),
  44. }
  45. classifyId, err := data_manage.AddBaseFromBaiinfoClassify(classifyInfo)
  46. if err != nil {
  47. errMsg = "保存分类失败"
  48. return
  49. }
  50. classifyInfo.ClassifyId = int(classifyId)
  51. //更新sort为classifyId
  52. err = data_manage.UpdateBaseFromBaiinfoClassifySort(int(classifyId))
  53. return
  54. }
  55. // EditBaiinfoClassify 编辑Baiinfo分类
  56. func EditBaiinfoClassify(classifyId int, classifyName string, sysUser *system.Admin) (err error, errMsg string) {
  57. item, err := data_manage.GetBaseFromBaiinfoClassifyById(classifyId)
  58. if err != nil {
  59. errMsg = `保存失败`
  60. return
  61. }
  62. // 名字相同,那么就直接返回
  63. if item.ClassifyName == classifyName {
  64. return
  65. }
  66. // 判断名称是否已存在
  67. count, err := data_manage.GetBaseFromBaiinfoClassifyCount(classifyName, item.ParentId)
  68. if err != nil {
  69. errMsg = "判断名称是否已存在失败"
  70. return
  71. }
  72. if count > 0 {
  73. errMsg = "分类名称已存在,请重新输入"
  74. err = errors.New(errMsg)
  75. return
  76. }
  77. // 修改数据
  78. err = data_manage.EditBaseFromBaiinfoClassify(classifyId, classifyName)
  79. if err != nil {
  80. errMsg = "保存失败"
  81. }
  82. return
  83. }
  84. // MoveBaiinfoClassify 移动Baiinfo分类
  85. func MoveBaiinfoClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  86. //判断分类是否存在
  87. classifyInfo, err := data_manage.GetBaseFromBaiinfoClassifyById(classifyId)
  88. if err != nil {
  89. errMsg = "移动失败"
  90. err = errors.New("获取分类信息失败,Err:" + err.Error())
  91. return
  92. }
  93. updateCol := make([]string, 0)
  94. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  95. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  96. parentClassifyInfo, tmpErr := data_manage.GetBaseFromBaiinfoClassifyById(parentClassifyId)
  97. if tmpErr != nil {
  98. errMsg = "移动失败"
  99. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  100. return
  101. }
  102. classifyInfo.ParentId = parentClassifyInfo.ClassifyId
  103. classifyInfo.Level = parentClassifyInfo.Level + 1
  104. updateCol = append(updateCol, "ParentId", "Level")
  105. }
  106. var currentSort, prevSort, nextSort int
  107. currentSort = classifyInfo.Sort
  108. var prevClassify *data_manage.BaseFromBaiinfoClassify
  109. var nextClassify *data_manage.BaseFromBaiinfoClassify
  110. //如果有传入 上一个兄弟节点分类id
  111. if prevClassifyId > 0 {
  112. prevClassify, err = data_manage.GetBaseFromBaiinfoClassifyById(prevClassifyId)
  113. if err != nil {
  114. if err.Error() == utils.ErrNoRow() {
  115. errMsg = "移动失败"
  116. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  117. return
  118. }
  119. errMsg = "移动失败"
  120. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  121. return
  122. }
  123. prevSort = prevClassify.Sort
  124. }
  125. if nextClassifyId > 0 {
  126. //下一个兄弟节点
  127. nextClassify, err = data_manage.GetBaseFromBaiinfoClassifyById(nextClassifyId)
  128. if err != nil {
  129. if err.Error() == utils.ErrNoRow() {
  130. errMsg = "移动失败"
  131. err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  132. return
  133. }
  134. errMsg = "移动失败"
  135. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  136. return
  137. }
  138. nextSort = nextClassify.Sort
  139. }
  140. // 修改历史数据中的排序为0的情况
  141. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  142. //更新为0排序的数据为当前最小排序
  143. if nextClassify != nil {
  144. minSort, e := data_manage.GetBaiinfoIndexClassifyMinSort(parentClassifyId)
  145. if e != nil {
  146. errMsg = "移动失败"
  147. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  148. return
  149. }
  150. if minSort > 1 {
  151. minSort -= 1
  152. }
  153. nextClassify.Sort = minSort
  154. err = nextClassify.Update([]string{"Sort"})
  155. if err != nil {
  156. errMsg = "移动失败"
  157. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  158. return
  159. }
  160. nextSort = minSort
  161. }
  162. }
  163. //移到两个排序值中间操作
  164. if prevSort >= currentSort {
  165. //往下移动
  166. err = data_manage.MoveDownBaiinfoIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  167. if err != nil {
  168. err = errors.New("向下移动出错:" + err.Error())
  169. return
  170. }
  171. classifyInfo.Sort = prevSort
  172. } else if nextSort <= currentSort && nextSort != 0 {
  173. //往上移动
  174. err = data_manage.MoveUpBaiinfoIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  175. if err != nil {
  176. err = errors.New("向上移动出错:" + err.Error())
  177. return
  178. }
  179. classifyInfo.Sort = nextSort
  180. }
  181. classifyInfo.ModifyTime = time.Now()
  182. updateCol = append(updateCol, "Sort", "ModifyTime")
  183. err = classifyInfo.Update(updateCol)
  184. if err != nil {
  185. errMsg = "移动失败"
  186. err = errors.New("修改失败,Err:" + err.Error())
  187. return
  188. }
  189. return
  190. }
  191. // DelBaiinfoClassify 删除SCI分类
  192. func DelBaiinfoClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
  193. //判断分类是否存在
  194. classifyInfo, err := data_manage.GetBaseFromBaiinfoClassifyById(classifyId)
  195. if err != nil {
  196. errMsg = "删除失败"
  197. err = errors.New("获取分类信息失败,Err:" + err.Error())
  198. return
  199. }
  200. // 需要删除的分类id集合
  201. classifyIdList := make([]int, 0)
  202. classifyIdList = append(classifyIdList, classifyId)
  203. // 查找所有的子集id
  204. if classifyInfo.ParentId <= 0 {
  205. childrenClassifyInfoList, tmpErr := data_manage.GetBaseFromBaiinfoClassifyByParentId(classifyId)
  206. if tmpErr != nil {
  207. errMsg = "删除失败"
  208. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  209. return
  210. }
  211. for _, v := range childrenClassifyInfoList {
  212. classifyIdList = append(classifyIdList, v.ClassifyId)
  213. }
  214. }
  215. // 获取分类下的所有指标
  216. count, err := data_manage.GetBaseFromBaiinfoIndexCountByClassifyIds(classifyIdList)
  217. if err != nil {
  218. errMsg = "删除失败"
  219. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  220. return
  221. }
  222. if count > 0 {
  223. errMsg = "该分类下有指标,不可删除"
  224. err = errors.New("该分类下有指标,不可删除")
  225. return
  226. }
  227. // 删除对应的分类
  228. err = data_manage.DeleteBaiinfoClassifyByClassifyId(classifyIdList)
  229. if err != nil {
  230. return
  231. }
  232. return
  233. }
  234. // MoveBaiinfoData 移动SCI指标
  235. func MoveBaiinfoData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  236. //分类信息
  237. baiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  238. if err != nil {
  239. errMsg = `获取数据失败`
  240. if err.Error() == utils.ErrNoRow() {
  241. errMsg = `该指标未入库`
  242. err = nil
  243. }
  244. return
  245. }
  246. //判断分类是否存在
  247. if classifyId > 0 {
  248. _, err = data_manage.GetBaseFromBaiinfoClassifyById(classifyId)
  249. if err != nil {
  250. errMsg = "移动失败"
  251. err = errors.New("获取分类信息失败,Err:" + err.Error())
  252. if err.Error() == utils.ErrNoRow() {
  253. errMsg = "找不到该分类"
  254. err = nil
  255. }
  256. return
  257. }
  258. }
  259. //如果改变了分类,那么移动该图表数据
  260. if baiinfoIndex.ClassifyId != classifyId {
  261. tmpErr := data_manage.MoveBaseFromBaiinfoIndex(indexId, classifyId)
  262. if tmpErr != nil {
  263. errMsg = "移动失败"
  264. err = errors.New("移动失败,Err:" + tmpErr.Error())
  265. return
  266. }
  267. }
  268. //移动排序
  269. updateCol := make([]string, 0)
  270. //如果有传入 上一个兄弟节点分类id
  271. if prevIndexId > 0 {
  272. prevIndex, tmpErr := data_manage.GetBaseFromBaiinfoIndexByIndexId(prevIndexId)
  273. if tmpErr != nil {
  274. errMsg = "移动失败"
  275. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  276. return
  277. }
  278. //如果是移动在两个兄弟节点之间
  279. if nextIndexId > 0 {
  280. //下一个兄弟节点
  281. nextIndex, tmpErr := data_manage.GetBaseFromBaiinfoIndexByIndexId(nextIndexId)
  282. if tmpErr != nil {
  283. errMsg = "移动失败"
  284. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  285. return
  286. }
  287. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  288. if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == baiinfoIndex.Sort {
  289. //变更兄弟节点的排序
  290. updateSortStr := `sort + 2`
  291. err = data_manage.UpdateBaseFromBaiinfoIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromBaiinfoIndexId, updateSortStr)
  292. if err != nil {
  293. errMsg = "移动失败"
  294. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  295. return
  296. }
  297. } else {
  298. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  299. if nextIndex.Sort-prevIndex.Sort == 1 {
  300. //变更兄弟节点的排序
  301. updateSortStr := `sort + 1`
  302. err = data_manage.UpdateBaseFromBaiinfoIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromBaiinfoIndexId, updateSortStr)
  303. if err != nil {
  304. errMsg = "移动失败"
  305. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  306. return
  307. }
  308. }
  309. }
  310. }
  311. baiinfoIndex.Sort = prevIndex.Sort + 1
  312. baiinfoIndex.ModifyTime = time.Now()
  313. updateCol = append(updateCol, "Sort", "ModifyTime")
  314. } else {
  315. firstClassify, tmpErr := data_manage.GetFirstBaseFromBaiinfoIndexByClassifyId(classifyId)
  316. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  317. errMsg = "移动失败"
  318. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  319. return
  320. }
  321. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  322. if firstClassify != nil && firstClassify.Sort == 0 {
  323. updateSortStr := ` sort + 1 `
  324. _ = data_manage.UpdateBaseFromBaiinfoIndexByClassifyId(firstClassify.ClassifyId, 0, firstClassify.BaseFromBaiinfoIndexId-1, updateSortStr)
  325. }
  326. baiinfoIndex.Sort = 0 //那就是排在第一位
  327. baiinfoIndex.ModifyTime = time.Now()
  328. updateCol = append(updateCol, "Sort", "ModifyTime")
  329. }
  330. //更新
  331. if len(updateCol) > 0 {
  332. err = baiinfoIndex.Update(updateCol)
  333. if err != nil {
  334. errMsg = "移动失败"
  335. err = errors.New("修改失败,Err:" + err.Error())
  336. return
  337. }
  338. }
  339. return
  340. }
  341. // MoveBaiinfoData2 移动SCI指标
  342. func MoveBaiinfoData2(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  343. //分类信息
  344. baiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  345. if err != nil {
  346. errMsg = `获取数据失败`
  347. if err.Error() == utils.ErrNoRow() {
  348. errMsg = `该指标未入库`
  349. err = nil
  350. }
  351. return
  352. }
  353. //判断分类是否存在
  354. if classifyId > 0 {
  355. _, err = data_manage.GetBaseFromBaiinfoClassifyById(classifyId)
  356. if err != nil {
  357. errMsg = "移动失败"
  358. err = errors.New("获取分类信息失败,Err:" + err.Error())
  359. if err.Error() == utils.ErrNoRow() {
  360. errMsg = "找不到该分类"
  361. err = nil
  362. }
  363. return
  364. }
  365. }
  366. //如果改变了分类,那么移动该图表数据
  367. if baiinfoIndex.ClassifyId != classifyId {
  368. tmpErr := data_manage.MoveBaseFromBaiinfoIndex(indexId, classifyId)
  369. if tmpErr != nil {
  370. errMsg = "移动失败"
  371. err = errors.New("移动失败,Err:" + tmpErr.Error())
  372. return
  373. }
  374. }
  375. //移动排序
  376. updateCol := make([]string, 0)
  377. var currentSort, prevSort, nextSort int
  378. currentSort = baiinfoIndex.Sort
  379. //如果有传入 上一个兄弟节点分类id
  380. var prevIndex *data_manage.BaseFromBaiinfoIndex
  381. var nextIndex *data_manage.BaseFromBaiinfoIndex
  382. if prevIndexId > 0 {
  383. prevIndex, err = data_manage.GetBaseFromBaiinfoIndexByIndexId(prevIndexId)
  384. if err != nil {
  385. if err.Error() == utils.ErrNoRow() {
  386. errMsg = "移动失败"
  387. err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
  388. return
  389. }
  390. errMsg = "移动失败"
  391. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  392. return
  393. }
  394. prevSort = prevIndex.Sort
  395. }
  396. if nextIndexId > 0 {
  397. //下一个兄弟节点
  398. nextIndex, err = data_manage.GetBaseFromBaiinfoIndexByIndexId(nextIndexId)
  399. if err != nil {
  400. if err.Error() == utils.ErrNoRow() {
  401. errMsg = "移动失败"
  402. err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
  403. return
  404. }
  405. errMsg = "移动失败"
  406. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
  407. return
  408. }
  409. nextSort = nextIndex.Sort
  410. }
  411. // 修改历史数据中的排序为0的情况
  412. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
  413. //更新为0排序的数据为当前最小排序
  414. if nextIndex != nil {
  415. minSort, e := data_manage.GetBaseFromBaiinfoIndexMinSortByClassifyId(classifyId)
  416. if e != nil {
  417. errMsg = "移动失败"
  418. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  419. return
  420. }
  421. if minSort > 1 {
  422. minSort -= 1
  423. }
  424. nextIndex.Sort = minSort
  425. err = nextIndex.Update([]string{"Sort"})
  426. if err != nil {
  427. errMsg = "移动失败"
  428. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  429. return
  430. }
  431. nextSort = minSort
  432. }
  433. }
  434. //移到两个排序值中间操作
  435. if prevSort >= currentSort {
  436. //往下移动
  437. err = data_manage.MoveDownBaiinfoIndexBySort(classifyId, prevSort, currentSort)
  438. if err != nil {
  439. err = errors.New("向下移动出错:" + err.Error())
  440. return
  441. }
  442. baiinfoIndex.Sort = prevSort
  443. } else if nextSort <= currentSort && nextSort != 0 {
  444. //往上移动
  445. err = data_manage.MoveUpBaiinfoIndexBySort(classifyId, nextSort, currentSort)
  446. if err != nil {
  447. err = errors.New("向上移动出错:" + err.Error())
  448. return
  449. }
  450. baiinfoIndex.Sort = nextSort
  451. }
  452. //更新
  453. baiinfoIndex.ModifyTime = time.Now()
  454. updateCol = append(updateCol, "Sort", "ModifyTime")
  455. err = baiinfoIndex.Update(updateCol)
  456. if err != nil {
  457. errMsg = "移动失败"
  458. err = errors.New("修改失败,Err:" + err.Error())
  459. return
  460. }
  461. return
  462. }
  463. // EditBaiinfoIndex 编辑SCI指标
  464. func EditBaiinfoIndex(indexId, classifyId int, frequency, unit string, sysUser *system.Admin) (baseFromBaiinfoIndex *data_manage.BaseFromBaiinfoIndex, err error, errMsg string) {
  465. baseFromBaiinfoIndex, err = data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  466. if err != nil {
  467. errMsg = `获取数据失败`
  468. if err.Error() == utils.ErrNoRow() {
  469. errMsg = `该指标未入库`
  470. err = nil
  471. }
  472. return
  473. }
  474. baseFromBaiinfoIndex.ClassifyId = classifyId
  475. baseFromBaiinfoIndex.ModifyTime = time.Now()
  476. baseFromBaiinfoIndex.Frequency = frequency
  477. baseFromBaiinfoIndex.Unit = unit
  478. err = baseFromBaiinfoIndex.Update([]string{"ClassifyId", "ModifyTime", "frequency", "unit"})
  479. if err != nil {
  480. errMsg = "保存分类失败"
  481. }
  482. return
  483. }
  484. func InitBaseFromBaiinfoClassify() (err error) {
  485. //查询历史数据所有分类
  486. typeList, err := data_manage.GetBaseFromBaiinfoIndexType()
  487. if err != nil {
  488. return
  489. }
  490. classifyList := make([]*data_manage.BaseFromBaiinfoClassify, 0)
  491. classifyMap := make(map[string][]string, 0)
  492. parentClassifyMap := make(map[string]struct{}, 0)
  493. now := time.Now()
  494. for _, v := range typeList {
  495. if _, ok := parentClassifyMap[v.Type2]; !ok {
  496. parentClassifyMap[v.Type2] = struct{}{}
  497. tmp := new(data_manage.BaseFromBaiinfoClassify)
  498. tmp.ClassifyName = v.Type2
  499. tmp.Level = 1
  500. tmp.ModifyTime = now
  501. tmp.CreateTime = now
  502. classifyList = append(classifyList, tmp)
  503. }
  504. classifyMap[v.Type2] = append(classifyMap[v.Type2], v.Type3)
  505. }
  506. //添加一级分类
  507. err = data_manage.AddBaiinfoClassifyMulti(classifyList)
  508. if err != nil {
  509. err = errors.New("新增分类出错:" + err.Error())
  510. return
  511. }
  512. //查询分类表的所有父级
  513. rootList, err := data_manage.GetBaseFromBaiinfoClassifyByParentId(0)
  514. if err != nil {
  515. return
  516. }
  517. classifyIdMap := make(map[string]int)
  518. for _, v := range rootList {
  519. classifyIdMap[v.ClassifyName] = v.ClassifyId
  520. }
  521. classifyList2 := make([]*data_manage.BaseFromBaiinfoClassify, 0)
  522. for _, v := range typeList {
  523. if parentId, ok := classifyIdMap[v.Type2]; ok {
  524. tmp := new(data_manage.BaseFromBaiinfoClassify)
  525. tmp.ParentId = parentId
  526. tmp.ClassifyName = v.Type3
  527. tmp.Level = 2
  528. tmp.ModifyTime = now
  529. tmp.CreateTime = now
  530. classifyList2 = append(classifyList2, tmp)
  531. }
  532. }
  533. //添加二级分类
  534. err = data_manage.AddBaiinfoClassifyMulti(classifyList2)
  535. if err != nil {
  536. err = errors.New("新增二级分类出错:" + err.Error())
  537. return
  538. }
  539. //初始化sort字段
  540. err = data_manage.InitBaiinfoClassifySort()
  541. if err != nil {
  542. return
  543. }
  544. //更新index表里的分类id
  545. err = data_manage.InitBaiinfoIndexClassifyId()
  546. return
  547. }
  548. // DelBaiinfoData 删除SCI指标
  549. func DelBaiinfoData(indexId int) (err error, errMsg string) {
  550. baseFromBaiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  551. if err != nil {
  552. errMsg = `获取数据失败`
  553. if err.Error() == utils.ErrNoRow() {
  554. errMsg = `该指标未入库`
  555. err = nil
  556. }
  557. return
  558. }
  559. // 获取已经加入到EDB指标库的Baiinfo指标
  560. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_BAIINFO, baseFromBaiinfoIndex.IndexCode)
  561. if err != nil && err.Error() != utils.ErrNoRow() {
  562. errMsg = "删除失败"
  563. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  564. return
  565. }
  566. if edbInfo != nil {
  567. errMsg = "当前指标已被引用,不可删除"
  568. err = nil
  569. return
  570. }
  571. // 删除对应的分类和指标
  572. err = data_manage.DeleteBaseFromBaiinfoIndexById(indexId)
  573. if err != nil {
  574. return
  575. }
  576. return
  577. }
  578. // ResetBaiinfoData 指标数据清除分类接口
  579. func ResetBaiinfoData(indexId int) (err error, errMsg string) {
  580. baiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  581. if err != nil {
  582. errMsg = `获取数据失败`
  583. if err.Error() == utils.ErrNoRow() {
  584. errMsg = `该指标未入库`
  585. err = nil
  586. }
  587. return
  588. }
  589. // 更新指标在未分类下的排序,永远排在未分类的最后一个
  590. //移动排序
  591. classifyId := 0
  592. updateCol := make([]string, 0)
  593. var currentSort, prevSort int
  594. currentSort = baiinfoIndex.Sort
  595. //未分类的最大的sort值
  596. prevSort, err = data_manage.GetBaseFromBaiinfoIndexMaxSortByClassifyId(classifyId)
  597. if err != nil {
  598. errMsg = "移动失败"
  599. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  600. return
  601. }
  602. //如果前面一个节点的sort值比当前大,则当前节点往下移动
  603. if prevSort >= currentSort {
  604. //往下移动
  605. err = data_manage.MoveDownBaiinfoIndexBySort(classifyId, prevSort, currentSort)
  606. if err != nil {
  607. err = errors.New("向下移动出错:" + err.Error())
  608. return
  609. }
  610. baiinfoIndex.Sort = prevSort
  611. }
  612. //更新
  613. baiinfoIndex.ModifyTime = time.Now()
  614. baiinfoIndex.ClassifyId = classifyId
  615. updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
  616. err = baiinfoIndex.Update(updateCol)
  617. if err != nil {
  618. errMsg = "移动失败"
  619. err = errors.New("修改失败,Err:" + err.Error())
  620. return
  621. }
  622. return
  623. }