company_receive.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/system"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // CompanyReceiveController 客户领取管理
  15. type CompanyReceiveController struct {
  16. BaseAuthController
  17. }
  18. // SellerList
  19. // @Title 获取组长/组员下所有销售不包括研究员等(根据部门、分组)
  20. // @Description 获取组长/组员下销售不包括研究员等(根据部门、分组)接口
  21. // @Success 200 {object} company.ReceiveGroupSellersResp
  22. // @router /receive/seller/group [get]
  23. func (this *CompanyReceiveController) SellerList() {
  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.ErrMsg = "请登录,SysUser Is Empty"
  33. br.Ret = 408
  34. return
  35. }
  36. productId := services.GetProductId(sysUser.RoleTypeCode)
  37. condition := " and enabled=1 "
  38. var pars []interface{}
  39. roleCodeTypeStr := ""
  40. switch productId {
  41. case 0:
  42. roleCodeTypeStr = `"` + utils.ROLE_TYPE_CODE_FICC_SELLER + `","` + utils.ROLE_TYPE_CODE_FICC_GROUP + `","` + utils.ROLE_TYPE_CODE_FICC_TEAM + `","` + utils.ROLE_TYPE_CODE_FICC_ADMIN + `","` + utils.ROLE_TYPE_CODE_RAI_SELLER + `","` + utils.ROLE_TYPE_CODE_RAI_GROUP + `","` + utils.ROLE_TYPE_CODE_RAI_ADMIN + `"`
  43. case 1:
  44. roleCodeTypeStr = `"` + utils.ROLE_TYPE_CODE_FICC_SELLER + `","` + utils.ROLE_TYPE_CODE_FICC_GROUP + `","` + utils.ROLE_TYPE_CODE_FICC_TEAM + `","` + utils.ROLE_TYPE_CODE_FICC_ADMIN + `"`
  45. case 2:
  46. roleCodeTypeStr = `"` + utils.ROLE_TYPE_CODE_RAI_SELLER + `","` + utils.ROLE_TYPE_CODE_RAI_GROUP + `","` + utils.ROLE_TYPE_CODE_RAI_ADMIN + `"`
  47. }
  48. if roleCodeTypeStr != "" {
  49. condition += " and role_type_code in (" + roleCodeTypeStr + ")"
  50. }
  51. //先查询出所有的销售,再根据当前用户权限进行区分
  52. //只有ficc/权益管理员以及超管才有访问该接口的权限
  53. fullGroups, err := system.GetFullGroup()
  54. if err != nil {
  55. br.Msg = "获取分组失败"
  56. br.ErrMsg = "获取分组失败,Err:" + err.Error()
  57. return
  58. }
  59. fullGroupMap := make(map[int]*system.SysFullGroup)
  60. for _, v := range fullGroups {
  61. fullGroupMap[v.GroupId] = v
  62. }
  63. var listGroup []company.ReceiveGroupSellers
  64. var listDepartment []company.ReceiveGroupSellers
  65. departmentListMap := make(map[int][]company.ReceiveGroupSellers)
  66. groupListMap := make(map[int][]company.ReceiveGroupSellers)
  67. teamListMap := make(map[int][]company.ReceiveGroupSellers)
  68. departmentHasMap := make(map[int]bool)
  69. groupHasMap := make(map[int]bool)
  70. teamHasMap := make(map[int]bool)
  71. sellerList, err := system.GetSysUserItemsOrderByCreated(condition, pars)
  72. if err != nil {
  73. br.Msg = "获取管理账号失败"
  74. br.ErrMsg = "获取管理账号失败,Err:" + err.Error()
  75. return
  76. }
  77. for _, v := range sellerList {
  78. tmp := company.ReceiveGroupSellers{
  79. AdminId: strconv.Itoa(v.AdminId),
  80. RealName: v.RealName,
  81. }
  82. if v.GroupId > 0 {
  83. if groupInfo, ok := fullGroupMap[v.GroupId]; ok {
  84. if groupInfo.ParentId > 0 {
  85. teamListMap[v.GroupId] = append(teamListMap[v.GroupId], tmp)
  86. } else {
  87. groupListMap[groupInfo.GroupId] = append(groupListMap[groupInfo.GroupId], tmp)
  88. }
  89. } else {
  90. departmentListMap[v.DepartmentId] = append(departmentListMap[v.DepartmentId], tmp)
  91. }
  92. } else if v.DepartmentId > 0 {
  93. departmentListMap[v.DepartmentId] = append(departmentListMap[v.DepartmentId], tmp)
  94. }
  95. }
  96. for _, groupInfo := range fullGroups {
  97. var team1 company.ReceiveGroupSellers
  98. //处理小组
  99. if groupInfo.ParentId > 0 {
  100. if _, ok2 := teamHasMap[groupInfo.GroupId]; !ok2 {
  101. if len(teamListMap[groupInfo.GroupId]) > 0 {
  102. team1 = company.ReceiveGroupSellers{
  103. AdminId: "team_" + strconv.Itoa(groupInfo.GroupId),
  104. RealName: groupInfo.GroupName,
  105. ChildrenList: teamListMap[groupInfo.GroupId],
  106. }
  107. teamHasMap[groupInfo.GroupId] = true
  108. groupListMap[groupInfo.ParentId] = append(groupListMap[groupInfo.ParentId], team1)
  109. }
  110. }
  111. }
  112. }
  113. for _, groupInfo := range fullGroups {
  114. var group1 company.ReceiveGroupSellers
  115. //处理大组
  116. if groupInfo.ParentId == 0 {
  117. if _, ok2 := groupHasMap[groupInfo.GroupId]; !ok2 {
  118. if len(groupListMap[groupInfo.GroupId]) > 0 {
  119. group1 = company.ReceiveGroupSellers{
  120. AdminId: "group_" + strconv.Itoa(groupInfo.GroupId),
  121. RealName: groupInfo.GroupName,
  122. ChildrenList: groupListMap[groupInfo.GroupId],
  123. }
  124. groupHasMap[groupInfo.GroupId] = true
  125. departmentListMap[groupInfo.DepartmentId] = append(departmentListMap[groupInfo.DepartmentId], group1)
  126. listGroup = append(listGroup, group1)
  127. }
  128. }
  129. }
  130. }
  131. departmentList, err := system.GetDepartmentList()
  132. if err != nil {
  133. br.Msg = "获取失败"
  134. br.ErrMsg = "获取部门失败,Err:" + err.Error()
  135. return
  136. }
  137. departmentMap := make(map[int]*system.SysDepartmentList)
  138. for _, v := range departmentList {
  139. departmentMap[v.DepartmentId] = v
  140. }
  141. for _, groupInfo := range departmentList {
  142. var department1 company.ReceiveGroupSellers
  143. //处理部门
  144. if len(departmentListMap[groupInfo.DepartmentId]) > 0 {
  145. if _, ok1 := departmentHasMap[groupInfo.DepartmentId]; !ok1 {
  146. department1 = company.ReceiveGroupSellers{
  147. AdminId: "department_" + strconv.Itoa(groupInfo.DepartmentId),
  148. RealName: groupInfo.DepartmentName,
  149. ChildrenList: departmentListMap[groupInfo.DepartmentId],
  150. }
  151. departmentHasMap[groupInfo.DepartmentId] = true
  152. listDepartment = append(listDepartment, department1)
  153. }
  154. }
  155. }
  156. br.Ret = 200
  157. br.Success = true
  158. br.Msg = "获取成功"
  159. if len(listDepartment) > 1 {
  160. br.Data = company.ReceiveGroupSellersResp{
  161. List: listDepartment,
  162. }
  163. } else {
  164. br.Data = company.ReceiveGroupSellersResp{
  165. List: listGroup,
  166. }
  167. }
  168. return
  169. }
  170. // List
  171. // @Title 配置领取权限的销售列表
  172. // @Description 获取组长/组员下销售不包括研究员等(根据部门、分组)接口
  173. // @Success 200 {object} models.ReceiveSellerListResp
  174. // @router /receive/seller [get]
  175. func (this *CompanyReceiveController) List() {
  176. br := new(models.BaseResponse).Init()
  177. defer func() {
  178. this.Data["json"] = br
  179. this.ServeJSON()
  180. }()
  181. sysUser := this.SysUser
  182. if sysUser == nil {
  183. br.Msg = "请登录"
  184. br.ErrMsg = "请登录,SysUser Is Empty"
  185. br.Ret = 408
  186. return
  187. }
  188. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  189. br.Msg = "无权查看"
  190. return
  191. }
  192. pageSize, _ := this.GetInt("PageSize", utils.PageSize20)
  193. currentIndex, _ := this.GetInt("CurrentIndex", 1)
  194. if currentIndex <= 0 {
  195. currentIndex = 1
  196. }
  197. startSize := paging.StartIndex(currentIndex, pageSize)
  198. condition := " and enabled=1 "
  199. var pars []interface{}
  200. adminIds := this.GetString("AdminIds")
  201. if adminIds != "" {
  202. condition += " and admin_id in (" + adminIds + ")"
  203. }
  204. realName := this.GetString("KeyWord")
  205. if realName != "" {
  206. condition += " and real_name like '%" + realName + "%'"
  207. }
  208. productId := services.GetProductId(sysUser.RoleTypeCode)
  209. roleCodeTypeStr := ""
  210. switch productId {
  211. case 0:
  212. roleCodeTypeStr = `"` + utils.ROLE_TYPE_CODE_FICC_SELLER + `","` + utils.ROLE_TYPE_CODE_FICC_GROUP + `","` + utils.ROLE_TYPE_CODE_FICC_TEAM + `","` + utils.ROLE_TYPE_CODE_FICC_ADMIN + `","` + utils.ROLE_TYPE_CODE_RAI_SELLER + `","` + utils.ROLE_TYPE_CODE_RAI_GROUP + `","` + utils.ROLE_TYPE_CODE_RAI_ADMIN + `"`
  213. case 1:
  214. roleCodeTypeStr = `"` + utils.ROLE_TYPE_CODE_FICC_SELLER + `","` + utils.ROLE_TYPE_CODE_FICC_GROUP + `","` + utils.ROLE_TYPE_CODE_FICC_TEAM + `","` + utils.ROLE_TYPE_CODE_FICC_ADMIN + `"`
  215. case 2:
  216. roleCodeTypeStr = `"` + utils.ROLE_TYPE_CODE_RAI_SELLER + `","` + utils.ROLE_TYPE_CODE_RAI_GROUP + `","` + utils.ROLE_TYPE_CODE_RAI_ADMIN + `"`
  217. }
  218. if roleCodeTypeStr != "" {
  219. condition += " and role_type_code in (" + roleCodeTypeStr + ")"
  220. }
  221. //查询所有部门
  222. departmentList, err := system.GetDepartmentList()
  223. if err != nil {
  224. br.Msg = "获取失败"
  225. br.ErrMsg = "获取部门失败,Err:" + err.Error()
  226. return
  227. }
  228. departmentNameMap := make(map[int]string)
  229. for _, v := range departmentList {
  230. departmentNameMap[v.DepartmentId] = v.DepartmentName
  231. }
  232. //先查询出所有的销售,再根据当前用户权限进行区分
  233. fullGroups, err := system.GetFullGroup()
  234. if err != nil {
  235. br.Msg = "获取分组失败"
  236. br.ErrMsg = "获取分组失败,Err:" + err.Error()
  237. return
  238. }
  239. departmentGroupNameMap := make(map[int]string)
  240. for _, g := range fullGroups {
  241. if g.ParentGroupName != "" {
  242. departmentGroupNameMap[g.GroupId] = g.DepartmentName + "/" + g.ParentGroupName + "/" + g.GroupName
  243. } else {
  244. departmentGroupNameMap[g.GroupId] = g.DepartmentName + "/" + g.GroupName
  245. }
  246. }
  247. var list []*company.ReceiveSellerItemResp
  248. sellerList, err := system.GetSysUserItemsList(condition, pars, startSize, pageSize)
  249. if err != nil {
  250. br.Msg = "获取管理账号失败"
  251. br.ErrMsg = "获取管理账号失败,Err:" + err.Error()
  252. return
  253. }
  254. permissionAminIds := ""
  255. for _, v := range sellerList {
  256. permissionAminIds += "," + strconv.Itoa(v.AdminId)
  257. }
  258. var adminPermissions []*company.SellerCompanyPermission
  259. permissionEnabledMap := make(map[int]int)
  260. if permissionAminIds != "" {
  261. //查询管理员ID对应的领取客户的权限
  262. permissionAminIds = strings.Trim(permissionAminIds, ",")
  263. adminPermissions, err = company.GetSellerCompanyPermissionByAdminIdsType(permissionAminIds, 1)
  264. if err != nil {
  265. br.Msg = "获取权限列表失败"
  266. br.ErrMsg = "获取权限列表失败,Err:" + err.Error()
  267. return
  268. }
  269. for _, v := range adminPermissions {
  270. permissionEnabledMap[v.AdminId] = v.Enabled
  271. }
  272. }
  273. for _, v := range sellerList {
  274. tmp := &company.ReceiveSellerItemResp{
  275. AdminId: v.AdminId,
  276. RealName: v.RealName,
  277. }
  278. tmp.Enabled = 1 //默认是有权限的
  279. if p, ok := permissionEnabledMap[v.AdminId]; ok {
  280. tmp.Enabled = p
  281. }
  282. if v.GroupId > 0 {
  283. if name, ok := departmentGroupNameMap[v.GroupId]; ok {
  284. tmp.DepartmentGroup = name
  285. }
  286. } else {
  287. if name, ok := departmentNameMap[v.DepartmentId]; ok {
  288. tmp.DepartmentGroup = name
  289. }
  290. }
  291. list = append(list, tmp)
  292. }
  293. total, err := system.GetSysUserItemsTotal(condition, pars)
  294. if err != nil {
  295. br.Msg = "获取管理账号总数失败"
  296. br.ErrMsg = "获取管理账号总数失败,Err:" + err.Error()
  297. return
  298. }
  299. page := paging.GetPaging(currentIndex, pageSize, int(total))
  300. br.Ret = 200
  301. br.Success = true
  302. br.Msg = "获取成功"
  303. br.Data = company.ReceiveSellerListResp{
  304. List: list,
  305. Paging: page,
  306. }
  307. return
  308. }
  309. // ChangePermission
  310. // @Title 修改用户添加客户的权限
  311. // @Description 修改用户添加客户的权限
  312. // @Param request body models.ChangeReceivePermissionReq true "type json string"
  313. // @Success 200 操作成功
  314. // @router /receive/permission/change [post]
  315. func (this *CompanyReceiveController) ChangePermission() {
  316. br := new(models.BaseResponse).Init()
  317. defer func() {
  318. this.Data["json"] = br
  319. this.ServeJSON()
  320. }()
  321. sysUser := this.SysUser
  322. if sysUser == nil {
  323. br.Msg = "请登录"
  324. br.ErrMsg = "请登录,SysUser Is Empty"
  325. br.Ret = 408
  326. return
  327. }
  328. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  329. br.Msg = "无权操作"
  330. return
  331. }
  332. var req company.ChangeReceivePermissionReq
  333. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  334. if err != nil {
  335. br.Msg = "参数解析异常!"
  336. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  337. return
  338. }
  339. if req.Enabled != 0 && req.Enabled != 1 {
  340. br.Msg = "请输入正常的操作类型"
  341. return
  342. }
  343. sellerInfo, err := system.GetSysAdminById(req.AdminId)
  344. if err != nil {
  345. if err.Error() == utils.ErrNoRow() {
  346. br.Msg = "销售不存在"
  347. return
  348. } else {
  349. br.Msg = "查询销售出错!"
  350. br.ErrMsg = "查询销售出错,Err:" + err.Error()
  351. return
  352. }
  353. }
  354. if sellerInfo.Enabled == 0 {
  355. br.Msg = "销售账号已无效"
  356. return
  357. }
  358. // 查询当前销售 添加客户权限状态
  359. sellerPermission, err := company.GetSellerCompanyPermissionByAdminIdType(req.AdminId, 1)
  360. if err != nil && err.Error() != utils.ErrNoRow() {
  361. br.Msg = "查询权限状态出错!"
  362. br.ErrMsg = "查询权限状态出错,Err:" + err.Error()
  363. return
  364. }
  365. now := time.Now()
  366. if sellerPermission != nil {
  367. if sellerPermission.Enabled == req.Enabled {
  368. br.Ret = 200
  369. br.Success = true
  370. br.Msg = "操作成功"
  371. return
  372. } else {
  373. err = company.UpdateSellerCompanyPermissionEnabledById(sellerPermission.Id, req.Enabled)
  374. if err != nil {
  375. br.Msg = "更新权限状态失败!"
  376. br.ErrMsg = "更新权限状态失败,Err:" + err.Error()
  377. return
  378. }
  379. }
  380. } else {
  381. if req.Enabled == 1 {
  382. br.Ret = 200
  383. br.Success = true
  384. br.Msg = "操作成功"
  385. return
  386. } else {
  387. // 禁用权限
  388. sellerPermission = &company.SellerCompanyPermission{
  389. AdminId: req.AdminId,
  390. Enabled: req.Enabled,
  391. CreateTime: now,
  392. ModifyTime: now,
  393. PermissionType: 1,
  394. }
  395. err = company.AddSellerCompanyPermission(sellerPermission)
  396. if err != nil {
  397. br.Msg = "更新权限状态失败!"
  398. br.ErrMsg = "更新权限状态失败,Err:" + err.Error()
  399. return
  400. }
  401. }
  402. }
  403. // 权限变更给销售发送模版通知
  404. go services.SendCompanyReceivePermissionWxTemplateMsg(sellerInfo.Mobile, req.Enabled)
  405. br.Ret = 200
  406. br.Success = true
  407. br.Msg = "操作成功"
  408. return
  409. }