manual.go 17 KB

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