business.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/main/manager/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. // AddBusiness .
  12. func (s *Service) AddBusiness(c context.Context, b *model.Business) (err error) {
  13. if err = s.dao.AddBusiness(c, b); err != nil {
  14. log.Error("s.dao.AddBusiness error(%v)", err)
  15. }
  16. return
  17. }
  18. // UpdateBusiness .
  19. func (s *Service) UpdateBusiness(c context.Context, b *model.Business) (err error) {
  20. flowState := []int64{}
  21. binStr := strconv.FormatInt(b.Flow, 2)
  22. for k, v := range []byte(binStr) {
  23. if string(v) == "1" {
  24. flowState = append(flowState, int64(len(binStr)-k))
  25. }
  26. }
  27. if err = s.dao.BatchUpdateChildState(c, b.ID, flowState); err != nil {
  28. log.Error("s.dao.BatchUpdateChildState error(%v)", err)
  29. return
  30. }
  31. if err = s.dao.UpdateBusiness(c, b); err != nil {
  32. log.Error("s.dao.UpdateBusiness error(%v)", err)
  33. }
  34. return
  35. }
  36. // AddRole .
  37. func (s *Service) AddRole(c context.Context, br *model.BusinessRole) (err error) {
  38. var (
  39. maxRid interface{}
  40. maxRidInt int64
  41. )
  42. if maxRid, err = s.dao.MaxRidByBid(c, br.BID); err != nil {
  43. return
  44. }
  45. if maxRid != nil {
  46. maxRidInt = maxRid.(int64)
  47. }
  48. maxRidInt++
  49. br.RID = maxRidInt
  50. if err = s.dao.AddRole(c, br); err != nil {
  51. log.Error("s.dao.AddRole error(%v)", err)
  52. }
  53. return
  54. }
  55. // UpdateRole .
  56. func (s *Service) UpdateRole(c context.Context, br *model.BusinessRole) (err error) {
  57. if err = s.dao.UpdateRole(c, br); err != nil {
  58. log.Error("s.dao.UpdateRole error(%v)", err)
  59. }
  60. return
  61. }
  62. // AddUser .
  63. func (s *Service) AddUser(c context.Context, bur *model.BusinessUserRole) (err error) {
  64. for _, uid := range bur.UIDs {
  65. if _, ok := s.userNames[uid]; !ok {
  66. err = ecode.ManagerUIDNOTExist
  67. return
  68. }
  69. }
  70. if err = s.dao.AddUser(c, bur); err != nil {
  71. log.Error("s.dao.AddUser error(%v)", err)
  72. }
  73. return
  74. }
  75. // UpdateUser .
  76. func (s *Service) UpdateUser(c context.Context, bur *model.BusinessUserRole) (err error) {
  77. if err = s.dao.UpdateUser(c, bur); err != nil {
  78. log.Error("s.dao.Update error(%v)", err)
  79. }
  80. return
  81. }
  82. // UpdateState .
  83. func (s *Service) UpdateState(c context.Context, su *model.StateUpdate) (err error) {
  84. var (
  85. childInfo *model.BusinessList
  86. parentInfo *model.BusinessList
  87. )
  88. if su.Type == model.BusinessOpenType { // business
  89. if su.State == model.BusinessOpenState {
  90. if childInfo, err = s.dao.BusinessByID(c, su.ID); err != nil {
  91. log.Error("s.dao.BusinessByID param id (%d) error(%v)", su.ID, err)
  92. return
  93. }
  94. if childInfo.PID != 0 {
  95. if parentInfo, err = s.dao.BusinessByID(c, childInfo.PID); err != nil {
  96. log.Error("s.dao.BusinessByID param id (%d) error(%v)", childInfo, err)
  97. return
  98. }
  99. flowState := map[int64]int64{}
  100. binStr := strconv.FormatInt(parentInfo.Flow, 2)
  101. for k, v := range []byte(binStr) {
  102. if string(v) == "1" {
  103. flowState[int64(len(binStr)-k)] = int64(len(binStr) - k)
  104. }
  105. }
  106. if _, ok := flowState[childInfo.FlowState]; !ok {
  107. err = ecode.ManagerFlowForbidden
  108. return
  109. }
  110. }
  111. }
  112. if err = s.dao.UpdateBusinessState(c, su); err != nil {
  113. log.Error("s.dao.UpdateBusinessState error(%v)", err)
  114. }
  115. return
  116. }
  117. // role
  118. if err = s.dao.UpdateBusinessRoleState(c, su); err != nil {
  119. log.Error("s.dao.UpdateBusinessRoleState error(%v)", err)
  120. }
  121. return
  122. }
  123. // BusinessList .
  124. func (s *Service) BusinessList(c context.Context, bp *model.BusinessListParams) (res []*model.BusinessList, err error) {
  125. var (
  126. pids []int64
  127. sKeys []int
  128. cKeys []int
  129. pidChildRes map[int64][]*model.BusinessList
  130. fKeys = make(map[int64][]int)
  131. pidsMap = make(map[int64]int64)
  132. tempRes = make(map[int64]*model.BusinessList)
  133. childRes = make(map[int64]*model.BusinessList)
  134. )
  135. flowBusinessMap := make(map[int64]map[int64][]*model.BusinessList)
  136. if tempRes, err = s.dao.ParentBusiness(c, bp.State); err != nil {
  137. log.Error("s.dao.ParentBusiness error(%v)", err)
  138. return
  139. }
  140. if len(tempRes) <= 0 {
  141. res = []*model.BusinessList{}
  142. return
  143. }
  144. if childRes, err = s.dao.ChildBusiness(c, bp); err != nil {
  145. log.Error("s.dao.ChildBusiness error(%v)", err)
  146. return
  147. }
  148. for _, cr := range childRes {
  149. cKeys = append(cKeys, int(cr.ID))
  150. }
  151. // auth verify
  152. if bp.Auth > 0 {
  153. up := &model.UserListParams{
  154. UID: bp.UID,
  155. Role: model.UserRoleDefaultVal,
  156. }
  157. var userBusiness []*model.BusinessUserRoleList
  158. if userBusiness, err = s.dao.UserList(c, up); err != nil {
  159. log.Error("s.dao.UserList error(%v)", err)
  160. return
  161. }
  162. if len(userBusiness) <= 0 {
  163. res = []*model.BusinessList{}
  164. return
  165. }
  166. bidsMap := make(map[int64]struct{})
  167. for _, u := range userBusiness {
  168. bidsMap[u.BID] = struct{}{}
  169. }
  170. cKeys = []int{}
  171. for _, cr := range childRes {
  172. if _, ok := bidsMap[cr.BID]; ok {
  173. cKeys = append(cKeys, int(cr.ID))
  174. }
  175. }
  176. }
  177. sort.Ints(cKeys)
  178. for _, c := range cKeys {
  179. cidR := childRes[int64(c)]
  180. if _, ok := pidsMap[cidR.PID]; !ok {
  181. pidsMap[cidR.PID] = cidR.PID
  182. pids = append(pids, cidR.PID)
  183. }
  184. cidR.FlowChild = []*model.FlowBusiness{}
  185. if _, ok := flowBusinessMap[cidR.PID]; !ok {
  186. flowBusinessMap[cidR.PID] = make(map[int64][]*model.BusinessList)
  187. }
  188. fKeys[cidR.PID] = append(fKeys[cidR.PID], int(cidR.FlowState))
  189. flowBusinessMap[cidR.PID][cidR.FlowState] = append(flowBusinessMap[cidR.PID][cidR.FlowState], cidR)
  190. }
  191. if bp.Check != 0 {
  192. if pidChildRes, err = s.dao.ChildBusinessByPIDs(c, pids); err != nil {
  193. log.Error("s.dao.ChildBusinessByPIDs error(%v)", err)
  194. return
  195. }
  196. }
  197. for k, r := range tempRes {
  198. r.FlowChild = []*model.FlowBusiness{}
  199. if _, ok := pidChildRes[k]; !ok && bp.Check != 0 {
  200. delete(tempRes, k)
  201. continue
  202. }
  203. if _, ok := flowBusinessMap[k]; !ok && bp.Check != 0 {
  204. continue
  205. }
  206. flowsMap := make(map[int]int)
  207. flowsSlice := []int{}
  208. if flows, ok := fKeys[r.ID]; ok {
  209. for _, f := range flows {
  210. if _, ok := flowsMap[f]; ok {
  211. continue
  212. }
  213. flowsMap[f] = f
  214. flowsSlice = append(flowsSlice, f)
  215. }
  216. sort.Ints(flowsSlice)
  217. for _, f := range flowsSlice {
  218. r.FlowChild = append(r.FlowChild, &model.FlowBusiness{
  219. FlowState: int64(f),
  220. Child: flowBusinessMap[r.ID][int64(f)],
  221. })
  222. }
  223. }
  224. sKeys = append(sKeys, int(k))
  225. }
  226. sort.Ints(sKeys)
  227. for _, k := range sKeys {
  228. if _, ok := tempRes[int64(k)]; !ok {
  229. continue
  230. }
  231. res = append(res, tempRes[int64(k)])
  232. }
  233. return
  234. }
  235. // FlowList .
  236. func (s *Service) FlowList(c context.Context, bp *model.BusinessListParams) (res []*model.BusinessList, err error) {
  237. var (
  238. tempRes = []*model.BusinessList{}
  239. userRes = []*model.BusinessUserRoleList{}
  240. )
  241. res = []*model.BusinessList{}
  242. if tempRes, err = s.dao.FlowList(c, bp); err != nil {
  243. log.Error("s.dao.FlowList error(%v)", err)
  244. return
  245. }
  246. if bp.Auth <= 0 {
  247. res = tempRes
  248. return
  249. }
  250. up := &model.UserListParams{
  251. UID: bp.UID,
  252. Role: model.UserRoleDefaultVal,
  253. }
  254. if userRes, err = s.dao.UserList(c, up); err != nil {
  255. log.Error("s.dao.UserList error(%v)", err)
  256. return
  257. }
  258. if len(userRes) <= 0 {
  259. return
  260. }
  261. tMap := make(map[int64]*model.BusinessList)
  262. for _, t := range tempRes {
  263. tMap[t.BID] = t
  264. }
  265. for _, u := range userRes {
  266. if u.Role == "" {
  267. continue
  268. }
  269. if t, ok := tMap[u.BID]; ok {
  270. res = append(res, t)
  271. }
  272. }
  273. return
  274. }
  275. // RoleList .
  276. func (s *Service) RoleList(c context.Context, br *model.BusinessRole) (res []*model.BusinessRole, err error) {
  277. var (
  278. rids = []int{}
  279. tempRes = []*model.BusinessRole{}
  280. userRes = []*model.BusinessUserRoleList{}
  281. tempResMap = make(map[int64]*model.BusinessRole)
  282. )
  283. res = []*model.BusinessRole{}
  284. if tempRes, err = s.dao.RoleListByBID(c, br); err != nil {
  285. log.Error("s.dao.RoleList error(%v)", err)
  286. return
  287. }
  288. for _, tr := range tempRes {
  289. rids = append(rids, int(tr.RID))
  290. tempResMap[tr.RID] = tr
  291. }
  292. if br.Auth <= 0 {
  293. res = tempRes
  294. return
  295. }
  296. up := &model.UserListParams{
  297. BID: br.BID,
  298. UID: br.UID,
  299. Role: model.UserRoleDefaultVal,
  300. }
  301. if userRes, err = s.dao.UserList(c, up); err != nil {
  302. log.Error("s.dao.UserList error(%v)", err)
  303. return
  304. }
  305. if len(userRes) <= 0 {
  306. return
  307. }
  308. roleStr := userRes[0].Role
  309. roleSlice := strings.Split(roleStr, ",")
  310. sort.Ints(rids)
  311. for _, r := range rids {
  312. for _, rs := range roleSlice {
  313. rsInt64, _ := strconv.ParseInt(rs, 10, 64)
  314. if int64(r) == rsInt64 {
  315. res = append(res, tempResMap[int64(r)])
  316. }
  317. }
  318. }
  319. return
  320. }
  321. // UserList .
  322. func (s *Service) UserList(c context.Context, u *model.UserListParams) (res []*model.BusinessUserRoleList, total int64, err error) {
  323. if u.UName != "" {
  324. if uid, ok := s.userIds[u.UName]; ok {
  325. u.UID = uid
  326. }
  327. }
  328. if res, err = s.dao.UserList(c, u); err != nil {
  329. log.Error("s.dao.UserList error(%v)", err)
  330. return
  331. }
  332. rids := []int64{}
  333. for _, r := range res {
  334. if uname, ok := s.userNames[r.UID]; ok {
  335. r.UName = uname
  336. }
  337. if unickname, ok := s.userNicknames[r.UID]; ok {
  338. r.UNickname = unickname
  339. }
  340. if cuname, ok := s.userNames[r.CUID]; ok {
  341. r.CUName = cuname
  342. }
  343. roleSlice := strings.Split(r.Role, ",")
  344. for _, ridStr := range roleSlice {
  345. ridInt64, _ := strconv.ParseInt(ridStr, 10, 64)
  346. rids = append(rids, ridInt64)
  347. }
  348. }
  349. var roleRes []*model.BusinessRole
  350. if roleRes, err = s.dao.RoleListByRIDs(c, u.BID, rids); err != nil {
  351. log.Error("s.dao.RoleListByIDs error(%v)", err)
  352. return
  353. }
  354. for _, r := range res {
  355. r.RoleName = []string{}
  356. rids := strings.Split(r.Role, ",")
  357. for _, rid := range rids {
  358. ridInt64, _ := strconv.ParseInt(rid, 10, 64)
  359. for _, rr := range roleRes {
  360. if ridInt64 == rr.RID && r.BID == rr.BID {
  361. r.RoleName = append(r.RoleName, rr.Name)
  362. }
  363. }
  364. }
  365. }
  366. total = int64(len(res))
  367. start := (u.PN - 1) * u.PS
  368. if start >= total {
  369. res = []*model.BusinessUserRoleList{}
  370. return
  371. }
  372. end := start + u.PS
  373. if end > total {
  374. end = total
  375. }
  376. res = res[start:end]
  377. return
  378. }
  379. // DeleteUser .
  380. func (s *Service) DeleteUser(c context.Context, bur *model.BusinessUserRole) (err error) {
  381. if err = s.dao.DeleteUser(c, bur); err != nil {
  382. log.Error("s.dao.DeleteUser error(%v)", err)
  383. }
  384. return
  385. }
  386. // UserRole .
  387. func (s *Service) UserRole(c context.Context, brl *model.BusinessUserRoleList) (res []*model.UserRole, err error) {
  388. var (
  389. ridsInt64 []int64
  390. userRole []*model.BusinessUserRoleList
  391. roleList []*model.BusinessRole
  392. u = &model.UserListParams{
  393. BID: brl.BID,
  394. UID: brl.UID,
  395. Role: -1,
  396. }
  397. )
  398. if userRole, err = s.dao.UserList(c, u); err != nil {
  399. log.Error("s.dao.UserList error(%v)", err)
  400. return
  401. }
  402. if len(userRole) <= 0 {
  403. res = []*model.UserRole{}
  404. return
  405. }
  406. rids := strings.Split(userRole[0].Role, ",")
  407. for _, rid := range rids {
  408. ridInt64, _ := strconv.ParseInt(rid, 10, 64)
  409. ridsInt64 = append(ridsInt64, ridInt64)
  410. }
  411. if roleList, err = s.dao.RoleListByRIDs(c, brl.BID, ridsInt64); err != nil {
  412. log.Error("s.dao.RoleListByRIDs error(%v)", err)
  413. return
  414. }
  415. for _, rid := range ridsInt64 {
  416. for _, rl := range roleList {
  417. if rid == rl.RID {
  418. r := &model.UserRole{
  419. ID: rl.ID,
  420. BID: rl.BID,
  421. RID: rl.RID,
  422. Type: rl.Type,
  423. Name: rl.Name,
  424. }
  425. res = append(res, r)
  426. }
  427. }
  428. }
  429. return
  430. }
  431. // StateUp .
  432. func (s *Service) StateUp(c context.Context, p *model.UserStateUp) (err error) {
  433. err = s.dao.DB().Table("manager_business_user_role").Where("bid = ? AND uid = ?", p.BID, p.AdminID).Update("state = ?", p.State).Error
  434. return
  435. }
  436. // UserRoles .
  437. func (s *Service) UserRoles(c context.Context, uid int64) (res []*model.UserRole, err error) {
  438. var roleRes []*model.BusinessUserRoleList
  439. if roleRes, err = s.dao.UserRoles(c, uid); err != nil {
  440. log.Error("s.dao.UserRoles error(%v)", err)
  441. return
  442. }
  443. if len(roleRes) <= 0 {
  444. res = []*model.UserRole{}
  445. return
  446. }
  447. var roleList []*model.BusinessRole
  448. for _, r := range roleRes {
  449. bid := r.BID
  450. rids := []int64{}
  451. ridsStr := strings.Split(r.Role, ",")
  452. for _, r := range ridsStr {
  453. rid, _ := strconv.ParseInt(r, 10, 64)
  454. rids = append(rids, rid)
  455. }
  456. var tmpRoleList []*model.BusinessRole
  457. if tmpRoleList, err = s.dao.RoleListByRIDs(c, bid, rids); err != nil {
  458. log.Error("s.dao.RoleListByRIDs error(%v)", err)
  459. return
  460. }
  461. roleList = append(roleList, tmpRoleList...)
  462. }
  463. for _, rl := range roleList {
  464. r := &model.UserRole{
  465. ID: rl.ID,
  466. BID: rl.BID,
  467. RID: rl.RID,
  468. Type: rl.Type,
  469. Name: rl.Name,
  470. }
  471. res = append(res, r)
  472. }
  473. return
  474. }
  475. // AllRoles .
  476. func (s *Service) AllRoles(c context.Context, pid, uid int64) (res []*model.UserRole, err error) {
  477. var childs []*model.Business
  478. if childs, err = s.dao.BusinessChilds(c, pid); err != nil {
  479. log.Error("s.dao.BusinessChilds error(%v)", err)
  480. return
  481. }
  482. var childIDs []int64
  483. for _, c := range childs {
  484. childIDs = append(childIDs, c.BID)
  485. }
  486. var roleRes []*model.BusinessUserRoleList
  487. if roleRes, err = s.dao.UserRoleByBIDs(c, uid, childIDs); err != nil {
  488. log.Error("s.dao.UserRoleByBIDs error(%v)", err)
  489. return
  490. }
  491. if len(roleRes) <= 0 {
  492. res = []*model.UserRole{}
  493. return
  494. }
  495. var roleList []*model.BusinessRole
  496. for _, r := range roleRes {
  497. bid := r.BID
  498. rids := []int64{}
  499. ridsStr := strings.Split(r.Role, ",")
  500. for _, r := range ridsStr {
  501. rid, _ := strconv.ParseInt(r, 10, 64)
  502. rids = append(rids, rid)
  503. }
  504. var tmpRoleList []*model.BusinessRole
  505. if tmpRoleList, err = s.dao.RoleListByRIDs(c, bid, rids); err != nil {
  506. log.Error("s.dao.RoleListByRIDs error(%v)", err)
  507. return
  508. }
  509. roleList = append(roleList, tmpRoleList...)
  510. }
  511. for _, rl := range roleList {
  512. r := &model.UserRole{
  513. ID: rl.ID,
  514. BID: rl.BID,
  515. RID: rl.RID,
  516. Type: rl.Type,
  517. Name: rl.Name,
  518. }
  519. res = append(res, r)
  520. }
  521. return
  522. }
  523. // IsAdmin .
  524. func (s *Service) IsAdmin(c context.Context, bid, uid int64) bool {
  525. var (
  526. err error
  527. res []*model.UserRole
  528. )
  529. p := &model.BusinessUserRoleList{
  530. BID: bid,
  531. UID: uid,
  532. }
  533. if res, err = s.UserRole(c, p); err != nil {
  534. log.Error("s.UserRole error(%v)", err)
  535. return false
  536. }
  537. for _, r := range res {
  538. if r.Type == 1 {
  539. return true
  540. }
  541. }
  542. return false
  543. }