manual.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "eta/eta_api/controllers"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/data_manage"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 手工数据管理
  16. type ManualController struct {
  17. controllers.BaseAuthController
  18. }
  19. // @Title 手工数据权限用户搜索
  20. // @Description 手工数据权限用户搜索
  21. // @Param KeyWord query string false "关键词搜索"
  22. // @Success 200 {object} data_manage.ManualSysUser
  23. // @router /manual/sysuser/search [get]
  24. func (this *ManualController) ManualSysUserSearch() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.Ret = 408
  34. return
  35. }
  36. keyWord := this.GetString("KeyWord")
  37. list, err := data.GetManualSysUser(keyWord)
  38. if err != nil {
  39. br.Msg = "获取失败"
  40. br.ErrMsg = "获取失败,Err:" + err.Error()
  41. return
  42. }
  43. br.Ret = 200
  44. br.Success = true
  45. br.Msg = "获取成功"
  46. br.Data = list
  47. }
  48. // @Title 获取手工数据权限用户
  49. // @Description 获取手工数据权限用户
  50. // @Success 200 {object} data_manage.ManualUserList
  51. // @router /manual/sysuser/all [get]
  52. func (this *ManualController) ManualSysUserAll() {
  53. br := new(models.BaseResponse).Init()
  54. defer func() {
  55. this.Data["json"] = br
  56. this.ServeJSON()
  57. }()
  58. sysUser := this.SysUser
  59. if sysUser == nil {
  60. br.Msg = "请登录"
  61. br.Ret = 408
  62. return
  63. }
  64. fmt.Println("start")
  65. list, err := data_manage.GetManualUserList()
  66. if err != nil {
  67. br.Msg = "获取失败"
  68. br.ErrMsg = "获取失败,Err:" + err.Error()
  69. return
  70. }
  71. fmt.Println("end")
  72. br.Ret = 200
  73. br.Success = true
  74. br.Msg = "获取成功"
  75. br.Data = list
  76. }
  77. // @Title 获取所有手工数据分类
  78. // @Description 获取所有手工数据分类
  79. // @Param AdminId query int false "初始化,AdminId 传0,如果对应具体用户,传对应的id"
  80. // @Success 200 {object} data_manage.ManualClassifyList
  81. // @router /manual/classify/all [get]
  82. func (this *ManualController) ClassifyList() {
  83. br := new(models.BaseResponse).Init()
  84. defer func() {
  85. this.Data["json"] = br
  86. this.ServeJSON()
  87. }()
  88. sysUser := this.SysUser
  89. if sysUser == nil {
  90. br.Msg = "请登录"
  91. br.Ret = 408
  92. return
  93. }
  94. adminId, _ := this.GetInt("AdminId")
  95. list, err := data_manage.GetManualClassifyAll(adminId)
  96. if err != nil {
  97. br.Msg = "获取失败"
  98. br.ErrMsg = "获取失败,Err:" + err.Error()
  99. return
  100. }
  101. br.Ret = 200
  102. br.Success = true
  103. br.Msg = "获取成功"
  104. br.Data = list
  105. }
  106. // @Title 保存手工数据权限-用户对应分类
  107. // @Description 保存手工数据权限-用户对应分类
  108. // @Param request body data_manage.ManualSysUserClassifySaveReq true "type json string"
  109. // @Success Ret=200 保存成功
  110. // @router /manual/sysuser/classify/save [post]
  111. func (this *ManualController) ManualSysUserClassifySave() {
  112. br := new(models.BaseResponse).Init()
  113. defer func() {
  114. this.Data["json"] = br
  115. this.ServeJSON()
  116. }()
  117. sysUser := this.SysUser
  118. if sysUser == nil {
  119. br.Msg = "请登录"
  120. br.Ret = 408
  121. return
  122. }
  123. var req data_manage.ManualSysUserClassifySaveReq
  124. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  125. if err != nil {
  126. br.Msg = "参数解析异常!"
  127. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  128. return
  129. }
  130. if req.AdminId <= 0 {
  131. br.Msg = "参数错误!"
  132. return
  133. }
  134. sysUserItem, err := system.GetSysUserById(req.AdminId)
  135. if err != nil {
  136. br.Msg = "获取用户信息失败!"
  137. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  138. return
  139. }
  140. err = data_manage.DeleteManualUserClassifyCount(req.AdminId)
  141. if err != nil {
  142. br.Msg = "保存失败!"
  143. br.ErrMsg = "删除用户分类信息失败,Err:" + err.Error()
  144. return
  145. }
  146. list := make([]*data_manage.ManualUserClassify, 0)
  147. for _, v := range req.ClassifyId {
  148. item := new(data_manage.ManualUserClassify)
  149. item.AdminId = req.AdminId
  150. item.ClassifyId = v
  151. item.CreateTime = time.Now()
  152. list = append(list, item)
  153. }
  154. if len(list) > 0 {
  155. err = data_manage.AddManualUserClassifyMulti(list)
  156. if err != nil {
  157. br.Msg = "保存失败!"
  158. br.ErrMsg = "保存失败,Err:" + err.Error()
  159. return
  160. }
  161. }
  162. count, err := data_manage.GetManualUserCount(req.AdminId)
  163. if err != nil {
  164. br.Msg = "保存失败!"
  165. br.ErrMsg = "判断用户是否存在失败,Err:" + err.Error()
  166. return
  167. }
  168. if count <= 0 {
  169. manualUser := new(data_manage.ManualUser)
  170. manualUser.AdminId = req.AdminId
  171. manualUser.CreateTime = time.Now()
  172. manualUser.SysUserId = sysUser.AdminId
  173. manualUser.AdminRealName = sysUserItem.RealName
  174. manualUser.SysUserRealName = sysUser.RealName
  175. err = data_manage.AddManualUser(manualUser)
  176. if err != nil {
  177. br.Msg = "保存失败!"
  178. br.ErrMsg = "保存用户信息失败,Err:" + err.Error()
  179. return
  180. }
  181. }
  182. br.Ret = 200
  183. br.Success = true
  184. br.Msg = "保存成功"
  185. }
  186. // AddManualClassify
  187. // @Title 新增手工指标分类
  188. // @Description 新增手工指标分类接口
  189. // @Param request body data_manage.AddManualClassifyReq true "type json string"
  190. // @Success 200 Ret=200 保存成功
  191. // @router /manual/classify/add [post]
  192. func (this *ChartClassifyController) AddManualClassify() {
  193. br := new(models.BaseResponse).Init()
  194. defer func() {
  195. this.Data["json"] = br
  196. this.ServeJSON()
  197. }()
  198. var req data_manage.AddManualClassifyReq
  199. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  200. if err != nil {
  201. br.Msg = "参数解析异常!"
  202. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  203. return
  204. }
  205. if req.ClassifyName == "" {
  206. br.Msg = "请输入分类名称"
  207. br.IsSendEmail = false
  208. return
  209. }
  210. if req.ParentId < 0 {
  211. br.Msg = "参数错误"
  212. br.IsSendEmail = false
  213. return
  214. }
  215. classifyItem, err := data_manage.GetManualClassifyByClassifyName(req.ClassifyName)
  216. if err != nil && err.Error() != utils.ErrNoRow() {
  217. br.Msg = "判断名称是否已存在失败"
  218. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  219. return
  220. }
  221. if classifyItem != nil {
  222. if classifyItem.IsShow == 1 {
  223. br.Msg = "分类名称已存在,请重新输入"
  224. br.IsSendEmail = false
  225. return
  226. } else {
  227. err = data_manage.ModifyManualClassifyIsShow(1, classifyItem.ClassifyId, req.ParentId)
  228. if err != nil {
  229. br.Msg = "保存失败"
  230. br.ErrMsg = "保存失败,Err:" + err.Error()
  231. br.IsSendEmail = false
  232. return
  233. }
  234. parentItem, _ := data_manage.GetManualClassifyById(req.ParentId)
  235. if parentItem != nil && parentItem.IsShow == 0 {
  236. err = data_manage.ModifyManualClassifyIsShow(1, parentItem.ClassifyId, req.ParentId)
  237. if err != nil {
  238. br.Msg = "保存失败"
  239. br.ErrMsg = "保存失败,Err:" + err.Error()
  240. br.IsSendEmail = false
  241. return
  242. }
  243. }
  244. br.Msg = "保存成功"
  245. br.Success = true
  246. br.Ret = 200
  247. return
  248. }
  249. }
  250. classify := new(data_manage.EdbdataClassify)
  251. classify.IsShow = 1
  252. classify.ClassifyName = req.ClassifyName
  253. classify.ParentId = req.ParentId
  254. classify.CreateTime = time.Now()
  255. classify.Sort = 0
  256. _, err = data_manage.AddEdbdataClassify(classify)
  257. if err != nil {
  258. br.Msg = "保存失败"
  259. br.ErrMsg = "保存失败,Err:" + err.Error()
  260. br.IsSendEmail = false
  261. return
  262. }
  263. br.Ret = 200
  264. br.Msg = "保存成功"
  265. br.Success = true
  266. }
  267. // ManualClassifyEdit
  268. // @Title 修改手工指标分类
  269. // @Description 修改手工指标分类接口
  270. // @Param request body data_manage.EditManualClassifyReq true "type json string"
  271. // @Success 200 Ret=200 修改成功
  272. // @router /manual/classify/edit [post]
  273. func (this *ChartClassifyController) ManualClassifyEdit() {
  274. br := new(models.BaseResponse).Init()
  275. defer func() {
  276. this.Data["json"] = br
  277. this.ServeJSON()
  278. }()
  279. var req data_manage.EditManualClassifyReq
  280. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  281. if err != nil {
  282. br.Msg = "参数解析异常!"
  283. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  284. return
  285. }
  286. if req.ClassifyName == "" {
  287. br.Msg = "请输入分类名称"
  288. br.IsSendEmail = false
  289. return
  290. }
  291. if req.ClassifyId <= 0 {
  292. br.Msg = "参数错误"
  293. br.IsSendEmail = false
  294. return
  295. }
  296. item, err := data_manage.GetManualClassifyById(req.ClassifyId)
  297. if err != nil {
  298. br.Msg = "修改失败"
  299. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  300. return
  301. }
  302. classifyItem, err := data_manage.GetManualClassifyByClassifyName(req.ClassifyName)
  303. if err != nil && err.Error() != utils.ErrNoRow() {
  304. br.Msg = "修改失败"
  305. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  306. return
  307. }
  308. if classifyItem != nil {
  309. if item.ClassifyId != classifyItem.ClassifyId {
  310. br.Msg = "名称已存在,请重新输入"
  311. return
  312. }
  313. }
  314. err = data_manage.ModifyManualClassifyName(req.ClassifyName, req.ClassifyId, req.ParentId)
  315. if err != nil {
  316. br.Msg = "修改失败"
  317. br.ErrMsg = "修改失败,Err:" + err.Error()
  318. return
  319. }
  320. br.Ret = 200
  321. br.Msg = "修改成功"
  322. br.Success = true
  323. }
  324. // ManualClassifyDel
  325. // @Title 删除手工指标分类
  326. // @Description 删除手工指标分类接口
  327. // @Param request body data_manage.DelManualClassifyReq true "type json string"
  328. // @Success 200 Ret=200 删除成功
  329. // @router /manual/classify/delete [post]
  330. func (this *ChartClassifyController) ManualClassifyDel() {
  331. br := new(models.BaseResponse).Init()
  332. defer func() {
  333. this.Data["json"] = br
  334. this.ServeJSON()
  335. }()
  336. var req data_manage.DelManualClassifyReq
  337. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  338. if err != nil {
  339. br.Msg = "参数解析异常!"
  340. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  341. return
  342. }
  343. if req.ClassifyId <= 0 {
  344. br.Msg = "请选择分类"
  345. br.IsSendEmail = false
  346. return
  347. }
  348. classifyItem, err := data_manage.GetManualClassifyById(req.ClassifyId)
  349. if err != nil && err.Error() != utils.ErrNoRow() {
  350. br.Msg = "查找分类失败"
  351. br.ErrMsg = "查找分类失败,Err:" + err.Error()
  352. return
  353. }
  354. if classifyItem.IsShow == 0 {
  355. br.Msg = "该分类已删除"
  356. br.IsSendEmail = false
  357. return
  358. }
  359. // 获取当前分类下是否存在子分类
  360. count, err := data_manage.GetChildManualClassifyCount(req.ClassifyId)
  361. if err != nil && err.Error() != utils.ErrNoRow() {
  362. br.Msg = "删除失败"
  363. br.ErrMsg = "根据分类id获取下面存在的子分类数量失败,Err:" + err.Error()
  364. return
  365. }
  366. if count > 0 {
  367. br.Msg = "该分类下包含子分类,不允许删除!"
  368. br.IsSendEmail = false
  369. return
  370. }
  371. if classifyItem.ParentId > 0 {
  372. // 获取当前分类下的指标数
  373. count, err = data_manage.GetManualEdbInfoCountByClassifyId(req.ClassifyId)
  374. if err != nil && err.Error() != utils.ErrNoRow() {
  375. br.Msg = "删除失败"
  376. br.ErrMsg = "根据分类id获取下面存在的指标数量失败,Err:" + err.Error()
  377. return
  378. }
  379. if count > 0 {
  380. br.Msg = "该分类下包含指标,不允许删除!"
  381. br.IsSendEmail = false
  382. return
  383. }
  384. }
  385. // 修改是否展示
  386. err = data_manage.ModifyManualClassifyIsShow(0, classifyItem.ClassifyId, classifyItem.ParentId)
  387. if err != nil {
  388. br.Msg = "删除失败"
  389. br.ErrMsg = "删除失败,Err:" + err.Error()
  390. return
  391. }
  392. br.Ret = 200
  393. br.Msg = "删除成功"
  394. br.Success = true
  395. }
  396. // @Title 获取用户设置的所有分类
  397. // @Description 获取用户设置的所有分类(手工数据录入页面)
  398. // @Success 200 {object} data_manage.EdbdataClassify
  399. // @router /manual/set_classify/all [get]
  400. func (this *ManualController) ManualSetClassifyAll() {
  401. br := new(models.BaseResponse).Init()
  402. defer func() {
  403. this.Data["json"] = br
  404. this.ServeJSON()
  405. }()
  406. sysUser := this.SysUser
  407. if sysUser == nil {
  408. br.Msg = "请登录"
  409. br.Ret = 408
  410. return
  411. }
  412. adminId := sysUser.AdminId
  413. setList, err := data_manage.GetManualUserSetClassify(adminId)
  414. if err != nil {
  415. br.Msg = "获取失败"
  416. br.ErrMsg = "获取失败,Err:" + err.Error()
  417. return
  418. }
  419. listLen := len(setList)
  420. classifyArr := make([]string, 0)
  421. for i := 0; i < listLen; i++ {
  422. classifyArr = append(classifyArr, strconv.Itoa(setList[i].ClassifyId))
  423. }
  424. classifyIdStr := strings.Join(classifyArr, ",")
  425. condition := ""
  426. items := make([]*data_manage.EdbdataClassifyList, 0)
  427. if classifyIdStr != "" {
  428. condition += ` AND classify_id IN(` + classifyIdStr + `)`
  429. condition += ` AND parent_id<>0 `
  430. items, err = data_manage.GetManualClassifyAllByCondition(condition)
  431. if err != nil {
  432. br.Msg = "获取失败"
  433. br.ErrMsg = "获取失败,Err:" + err.Error()
  434. return
  435. }
  436. }
  437. classifyEdbTotalList, _ := data_manage.GetManualClassifyEdbInfoCount()
  438. edbTotalMap := make(map[int]int)
  439. for _, v := range classifyEdbTotalList {
  440. edbTotalMap[v.ClassifyId] = v.Total
  441. }
  442. itemsLen := len(items)
  443. for i := 0; i < itemsLen; i++ {
  444. items[i].Total = edbTotalMap[items[i].ClassifyId]
  445. }
  446. br.Ret = 200
  447. br.Success = true
  448. br.Msg = "获取成功"
  449. br.Data = items
  450. }
  451. // FirstClassifyList
  452. // @Title 获取所有手工数据的一级分类
  453. // @Description 获取所有手工数据的一级分类
  454. // @Success 200 {object} data_manage.ManualClassifyList
  455. // @router /manual/classify/first [get]
  456. func (this *ManualController) FirstClassifyList() {
  457. br := new(models.BaseResponse).Init()
  458. defer func() {
  459. this.Data["json"] = br
  460. this.ServeJSON()
  461. }()
  462. sysUser := this.SysUser
  463. if sysUser == nil {
  464. br.Msg = "请登录"
  465. br.Ret = 408
  466. return
  467. }
  468. condition := " AND parent_id =0 "
  469. list, err := data_manage.GetManualClassifyAllByCondition(condition)
  470. if err != nil {
  471. br.Msg = "获取失败"
  472. br.ErrMsg = "获取失败,Err:" + err.Error()
  473. return
  474. }
  475. br.Ret = 200
  476. br.Success = true
  477. br.Msg = "获取成功"
  478. br.Data = list
  479. }
  480. // AllClassifyList
  481. // @Title 获取所有手工数据分类
  482. // @Description 获取所有手工数据分类
  483. // @Success 200 {object} data_manage.ManualClassifyList
  484. // @router /manual/classify/all_list [get]
  485. func (this *ManualController) AllClassifyList() {
  486. br := new(models.BaseResponse).Init()
  487. defer func() {
  488. this.Data["json"] = br
  489. this.ServeJSON()
  490. }()
  491. sysUser := this.SysUser
  492. if sysUser == nil {
  493. br.Msg = "请登录"
  494. br.Ret = 408
  495. return
  496. }
  497. list, err := data_manage.GetManualClassifyAllV2()
  498. if err != nil {
  499. br.Msg = "获取失败"
  500. br.ErrMsg = "获取失败,Err:" + err.Error()
  501. return
  502. }
  503. br.Ret = 200
  504. br.Success = true
  505. br.Msg = "获取成功"
  506. br.Data = list
  507. }
  508. //func init() {
  509. // fmt.Println("start")
  510. // adminId:=137
  511. // setList, err := data_manage.GetManualUserSetClassify(adminId)
  512. // if err != nil {
  513. // fmt.Println("err:", err)
  514. // return
  515. // }
  516. // listLen := len(setList)
  517. // classifyArr := make([]string, 0)
  518. // for i := 0; i < listLen; i++ {
  519. // classifyArr = append(classifyArr, strconv.Itoa(setList[i].ClassifyId))
  520. // }
  521. // classifyIdStr := strings.Join(classifyArr, ",")
  522. // condition := ""
  523. // if classifyIdStr != "" {
  524. // condition += ` AND classify_id IN(` + classifyIdStr + `)`
  525. // }
  526. // items, err := data_manage.GetManualClassifyAllByCondition(condition)
  527. // if err != nil {
  528. // fmt.Println("err:", err)
  529. // return
  530. // }
  531. // fmt.Println(items)
  532. // fmt.Println("end")
  533. //}