reason.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "go-common/app/admin/main/manager/model"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // CateSecExtList .
  11. func (s *Service) CateSecExtList(c context.Context, e *model.CateSecExt) (res []*model.CateSecExt, err error) {
  12. if res, err = s.dao.CateSecExtList(c, e); err != nil {
  13. log.Error("s.CateSecExtList type (%d) error (%v)", e.Type, err)
  14. }
  15. return
  16. }
  17. // AssociationList .
  18. func (s *Service) AssociationList(c context.Context, e *model.Association) (res []*model.Association, err error) {
  19. var (
  20. aRes []*model.Association
  21. rRes []*model.BusinessRole
  22. catesecRes []*model.CateSecExt
  23. )
  24. // Display all record of Association
  25. if aRes, err = s.dao.AssociationList(c, model.AllState, e.BusinessID); err != nil {
  26. log.Error("s.AssociationList error (%v)", err)
  27. return
  28. }
  29. if len(aRes) <= 0 {
  30. res = []*model.Association{}
  31. return
  32. }
  33. // Get all mapping of role
  34. rMap := make(map[int64]string)
  35. bean := &model.BusinessRole{
  36. BID: e.BusinessID,
  37. Type: model.AllType,
  38. State: model.AllState,
  39. }
  40. if rRes, err = s.dao.RoleListByBID(c, bean); err != nil {
  41. log.Error("s.RoleListByBID error (%v)", err)
  42. return
  43. }
  44. for _, r := range rRes {
  45. rMap[r.RID] = r.Name
  46. }
  47. // GET all mapping of category and second
  48. cMap := make(map[int64]string)
  49. sMap := make(map[int64]*model.CateSecExt)
  50. if catesecRes, err = s.dao.CateSecList(c, e.BusinessID); err != nil {
  51. log.Error("s.CateSecList error (%v)", err)
  52. return
  53. }
  54. for _, cs := range catesecRes {
  55. if cs.Type == model.CategoryCode {
  56. cMap[cs.ID] = cs.Name
  57. } else if cs.Type == model.SecondeCode {
  58. sMap[cs.ID] = cs
  59. }
  60. }
  61. res = []*model.Association{}
  62. for _, value := range aRes {
  63. temp := &model.Association{
  64. ID: value.ID,
  65. RoleID: value.RoleID,
  66. BusinessID: value.BusinessID,
  67. RoleName: rMap[value.RoleID],
  68. CategoryID: value.CategoryID,
  69. CategoryName: cMap[value.CategoryID],
  70. SecondIDs: value.SecondIDs,
  71. Ctime: value.Ctime,
  72. Mtime: value.Mtime,
  73. State: value.State,
  74. }
  75. sids := strings.Split(value.SecondIDs, ",")
  76. temp.Child = []*model.CateSecExt{}
  77. if sids[0] != "" {
  78. for _, sid := range sids {
  79. id, _ := strconv.ParseInt(sid, 10, 64)
  80. if value, ok := sMap[id]; ok {
  81. temp.Child = append(temp.Child, value)
  82. }
  83. }
  84. }
  85. res = append(res, temp)
  86. }
  87. return
  88. }
  89. // AddCateSecExt .
  90. func (s *Service) AddCateSecExt(c context.Context, arg *model.CateSecExt) (err error) {
  91. if err = s.dao.AddCateSecExt(c, arg); err != nil {
  92. log.Error("s.AddCateSecExt (%s) error (%v)", arg.Name, err)
  93. }
  94. return
  95. }
  96. // UpdateCateSecExt .
  97. func (s *Service) UpdateCateSecExt(c context.Context, arg *model.CateSecExt) (err error) {
  98. if err = s.dao.UpdateCateSecExt(c, arg); err != nil {
  99. log.Error("s.UpdateCateSecExt (%s) error (%v)", arg.Name, err)
  100. }
  101. return
  102. }
  103. // BanCateSecExt .
  104. func (s *Service) BanCateSecExt(c context.Context, arg *model.CateSecExt) (err error) {
  105. if err = s.dao.BanCateSecExt(c, arg); err != nil {
  106. log.Error("s.BanCateSecExt (%d) error (%v)", arg.ID, err)
  107. }
  108. return
  109. }
  110. // AddAssociation .
  111. func (s *Service) AddAssociation(c context.Context, arg *model.Association) (err error) {
  112. if err = s.dao.AddAssociation(c, arg); err != nil {
  113. log.Error("s.AddAssociation error %v", err)
  114. }
  115. return
  116. }
  117. // UpdateAssociation .
  118. func (s *Service) UpdateAssociation(c context.Context, arg *model.Association) (err error) {
  119. if err = s.dao.UpdateAssociation(c, arg); err != nil {
  120. log.Error("s.UpdateAssociation error %v", err)
  121. }
  122. return
  123. }
  124. // BanAssociation .
  125. func (s *Service) BanAssociation(c context.Context, arg *model.Association) (err error) {
  126. if err = s.dao.BanAssociation(c, arg); err != nil {
  127. log.Error("s.BanAssociation error %v", err)
  128. }
  129. return
  130. }
  131. // AddReason .
  132. func (s *Service) AddReason(c context.Context, arg *model.Reason) (err error) {
  133. if err = s.dao.AddReason(c, arg); err != nil {
  134. log.Error("s.AddReason (%v) error (%v)", arg, err)
  135. }
  136. return
  137. }
  138. // UpdateReason .
  139. func (s *Service) UpdateReason(c context.Context, arg *model.Reason) (err error) {
  140. if err = s.dao.UpdateReason(c, arg); err != nil {
  141. log.Error("s.UpdateReason (%v) error (%v)", arg, err)
  142. }
  143. return
  144. }
  145. // ReasonList .
  146. func (s *Service) ReasonList(c context.Context, e *model.SearchReasonParams) (res []*model.Reason, total int64, err error) {
  147. var (
  148. rids []int64
  149. csids []int64
  150. eRes map[int64]*model.BusinessRole
  151. csRes map[int64]string
  152. )
  153. // Search the user_id
  154. if e.UName != "" {
  155. if userID, ok := s.userIds[e.UName]; ok {
  156. e.UID = userID
  157. }
  158. }
  159. if res, err = s.dao.ReasonList(c, e); err != nil {
  160. log.Error("s.dao.ReasonList error (%v)", err)
  161. return
  162. }
  163. if len(res) <= 0 {
  164. return
  165. }
  166. // Search relation data
  167. for _, r := range res {
  168. rids = append(rids, r.RoleID)
  169. csids = append(csids, r.CategoryID)
  170. csids = append(csids, r.SecondID)
  171. }
  172. if eRes, err = s.dao.RoleByRIDs(c, e.BusinessID, rids); err != nil {
  173. log.Error("s.dao.ExecutorByIDs error (%v)", err)
  174. return
  175. }
  176. if csRes, err = s.dao.CateSecByIDs(c, csids); err != nil {
  177. log.Error("s.dao.CateSecByIDs error (%v)", err)
  178. return
  179. }
  180. for _, value := range res {
  181. if r, ok := eRes[value.RoleID]; ok {
  182. value.RoleName = r.Name
  183. }
  184. if c, ok := csRes[value.CategoryID]; ok {
  185. value.CategoryName = c
  186. }
  187. if s, ok := csRes[value.SecondID]; ok {
  188. value.SecondName = s
  189. }
  190. if u, ok := s.userNames[value.UID]; ok {
  191. value.UName = u
  192. }
  193. }
  194. total = int64(len(res))
  195. start := (e.PN - 1) * e.PS
  196. if start >= total {
  197. res = []*model.Reason{}
  198. return
  199. }
  200. end := start + e.PS
  201. if end > total {
  202. end = total
  203. }
  204. res = res[start:end]
  205. return
  206. }
  207. // BatchUpdateReasonState .
  208. func (s *Service) BatchUpdateReasonState(c context.Context, b *model.BatchUpdateReasonState) (err error) {
  209. if err = s.dao.BatchUpdateReasonState(c, b); err != nil {
  210. log.Error("s.dao.BatchUpdateReasonState %v error (%v)", b.IDs, err)
  211. }
  212. return
  213. }
  214. // DropDownList .
  215. func (s *Service) DropDownList(c context.Context, e *model.Association) (res []*model.DropList, err error) {
  216. var (
  217. aRes []*model.Association
  218. rRes []*model.BusinessRole
  219. csRes []*model.CateSecExt
  220. )
  221. // Only display validate association
  222. if aRes, err = s.dao.AssociationList(c, model.ValidateState, e.BusinessID); err != nil {
  223. log.Error("s.AssociationList error (%v)", err)
  224. return
  225. }
  226. // Get all association record
  227. rMap := make(map[int64]string)
  228. bean := &model.BusinessRole{
  229. BID: e.BusinessID,
  230. Type: model.AllType,
  231. State: model.AllState,
  232. }
  233. if rRes, err = s.dao.RoleListByBID(c, bean); err != nil {
  234. log.Error("s.RoleListByBID error (%v)", err)
  235. return
  236. }
  237. for _, r := range rRes {
  238. rMap[r.RID] = r.Name
  239. }
  240. // Get all mapping of category and second
  241. csMap := make(map[int64]string)
  242. if csRes, err = s.dao.CateSecList(c, e.BusinessID); err != nil {
  243. log.Error("s.CateSecList error (%v)", err)
  244. return
  245. }
  246. for _, cs := range csRes {
  247. csMap[cs.ID] = cs.Name
  248. }
  249. // Mapping data,use map store unique data
  250. resMap := make(map[int64]map[int64]map[int64]int64)
  251. for _, v := range aRes {
  252. if _, ok := resMap[v.RoleID]; !ok {
  253. //first element
  254. resMap[v.RoleID] = make(map[int64]map[int64]int64)
  255. }
  256. if _, ok := resMap[v.RoleID][v.CategoryID]; !ok {
  257. resMap[v.RoleID][v.CategoryID] = make(map[int64]int64)
  258. }
  259. sids := strings.Split(v.SecondIDs, ",")
  260. for _, sid := range sids {
  261. id, _ := strconv.ParseInt(sid, 10, 64)
  262. resMap[v.RoleID][v.CategoryID][id] = id
  263. }
  264. }
  265. // Output the data
  266. res = []*model.DropList{}
  267. for keyR, valueR := range resMap {
  268. temprRes := &model.DropList{
  269. ID: keyR,
  270. Name: rMap[keyR],
  271. }
  272. childCategory := []*model.DropList{}
  273. for keyC, valueC := range valueR {
  274. tempcRes := &model.DropList{
  275. ID: keyC,
  276. Name: csMap[keyC],
  277. }
  278. childSecond := []*model.DropList{}
  279. for keyS, valueS := range valueC {
  280. tempsRes := &model.DropList{
  281. ID: keyS,
  282. Name: csMap[valueS],
  283. Child: []*model.DropList{},
  284. }
  285. childSecond = append(childSecond, tempsRes)
  286. }
  287. tempcRes.Child = childSecond
  288. childCategory = append(childCategory, tempcRes)
  289. }
  290. temprRes.Child = childCategory
  291. res = append(res, temprRes)
  292. }
  293. return
  294. }
  295. // BusinessAttr .
  296. func (s *Service) BusinessAttr(c context.Context, b *model.BusinessAttr) (res map[string]bool, err error) {
  297. res = make(map[string]bool)
  298. res["isTag"] = false
  299. var tempRes []*model.CateSecExt
  300. if tempRes, err = s.dao.CateSecList(c, b.BID); err != nil {
  301. if err == gorm.ErrRecordNotFound {
  302. err = nil
  303. }
  304. return
  305. }
  306. for _, tr := range tempRes {
  307. if tr.Type == model.ExtensionCode && tr.State == 1 {
  308. res["isTag"] = true
  309. }
  310. }
  311. return
  312. }