sys_admin.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package system
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. type AdminItem struct {
  11. AdminId int `description:"系统用户id"`
  12. AdminName string `description:"系统用户名称"`
  13. RealName string `description:"系统用户姓名"`
  14. Password string
  15. LastUpdatedPasswordTime string `json:"-"`
  16. Enabled int `description:"1:有效,0:禁用"`
  17. Email string `description:"系统用户邮箱"`
  18. LastLoginTime string
  19. CreatedTime time.Time
  20. LastUpdatedTime string
  21. Role string `description:"系统用户角色"`
  22. Mobile string `description:"手机号"`
  23. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  24. RoleId int `description:"角色id"`
  25. RoleName string `description:"角色名称"`
  26. RoleTypeCode string `description:"角色编码"`
  27. DepartmentId int `description:"部门id"`
  28. DepartmentName string `json:"-" description:"部门名称"`
  29. TeamId int `description:"三级id"`
  30. GroupId int `description:"分组id"`
  31. GroupName string `json:"-" description:"分组名称"`
  32. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  33. Position string `description:"职位"`
  34. DepartmentGroup string `description:"部门分组"`
  35. LabelVal int `description:"标签:1:超级管理员,2:管理员,3:部门经理,4:组长,5:ficc销售主管"`
  36. ResearchGroupName string `description:"研究方向分组名称"`
  37. Province string `description:"省"`
  38. ProvinceCode string `description:"省编码"`
  39. City string `description:"市"`
  40. CityCode string `description:"市编码"`
  41. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  42. TelAreaCode string `description:"手机区号"`
  43. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  44. }
  45. func GetAdminItemByIdList(adminId []int) (items []*AdminItem, err error) {
  46. if len(adminId) == 0 {
  47. return
  48. }
  49. o := global.DbMap[utils.DbNameMaster]
  50. sql := `SELECT * FROM admin WHERE admin_id IN (` + utils.GetOrmInReplace(len(adminId)) + `) ORDER BY created_time DESC`
  51. sql = utils.ReplaceDriverKeywords("", sql)
  52. err = o.Raw(sql, adminId).Find(&items).Error
  53. return
  54. }
  55. func GetSysuserList(condition string, pars []interface{}, startSize, pageSize int) (items []*AdminItem, err error) {
  56. o := global.DbMap[utils.DbNameMaster]
  57. sql := `SELECT * FROM admin WHERE 1=1 `
  58. if condition != "" {
  59. sql += condition
  60. }
  61. sql += `ORDER BY enabled DESC, last_updated_time DESC, created_time DESC LIMIT ?,?`
  62. sql = utils.ReplaceDriverKeywords("", sql)
  63. pars = append(pars, startSize, pageSize)
  64. err = o.Raw(sql, pars...).Find(&items).Error
  65. return
  66. }
  67. func GetSysuserListCount(condition string, pars []interface{}) (count int, err error) {
  68. o := global.DbMap[utils.DbNameMaster]
  69. sql := `SELECT COUNT(1) AS count FROM admin WHERE 1=1 `
  70. if condition != "" {
  71. sql += condition
  72. }
  73. sql = utils.ReplaceDriverKeywords("", sql)
  74. err = o.Raw(sql, pars...).Scan(&count).Error
  75. return
  76. }
  77. type AdminRespItem struct {
  78. AdminId int `description:"系统用户id"`
  79. AdminName string `description:"系统用户名称"`
  80. RealName string `description:"系统用户姓名"`
  81. Password string
  82. LastUpdatedPasswordTime string `json:"-"`
  83. Enabled int `description:"1:有效,0:禁用"`
  84. Email string `description:"系统用户邮箱"`
  85. LastLoginTime string
  86. CreatedTime time.Time
  87. LastUpdatedTime string
  88. Role string `description:"系统用户角色"`
  89. Mobile string `description:"手机号"`
  90. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  91. RoleId int `description:"角色id"`
  92. RoleName string `description:"角色名称"`
  93. RoleTypeCode string `description:"角色编码"`
  94. DepartmentId int `description:"部门id"`
  95. DepartmentName string `json:"-" description:"部门名称"`
  96. parentId int `description:"父级id"`
  97. GroupId int `description:"分组id"`
  98. GroupName string `json:"-" description:"分组名称"`
  99. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  100. Position string `description:"职位"`
  101. DepartmentGroup string `description:"部门分组"`
  102. LabelVal int `description:"标签:1:超级管理员,2:管理员,3:部门经理,4:组长,5:ficc销售主管"`
  103. }
  104. type SysuserListResp struct {
  105. List []*AdminItem
  106. Paging *paging.PagingItem `description:"分页数据"`
  107. }
  108. type SysuserAddReq struct {
  109. AdminName string `description:"系统用户名称"`
  110. AdminAvatar string `description:"用户头像"`
  111. RealName string `description:"系统用户姓名"`
  112. Password string `description:"密码"`
  113. Mobile string `description:"手机号"`
  114. RoleId int `description:"角色id"`
  115. DepartmentId int `description:"部门id"`
  116. GroupId int `description:"分组id"`
  117. TeamId int `description:"小组id"`
  118. //Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人"`
  119. Position string `description:"职位"`
  120. ResearchGroupIds string `description:"研究方向分组IDs"`
  121. Province string `description:"省"`
  122. ProvinceCode string `description:"省编码"`
  123. City string `description:"市"`
  124. CityCode string `description:"市编码"`
  125. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  126. Email string `description:"邮箱"`
  127. TelAreaCode string `description:"手机区号"`
  128. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  129. }
  130. func GetSysAdminCount(adminName string) (count int, err error) {
  131. sql := `SELECT COUNT(1) AS count FROM admin WHERE admin_name=? `
  132. sql = utils.ReplaceDriverKeywords("", sql)
  133. o := global.DbMap[utils.DbNameMaster]
  134. err = o.Raw(sql, adminName).Scan(&count).Error
  135. return
  136. }
  137. func GetSysAdminByName(adminName string) (item *Admin, err error) {
  138. sql := `SELECT * FROM admin WHERE admin_name=? `
  139. o := global.DbMap[utils.DbNameMaster]
  140. sql = utils.ReplaceDriverKeywords("", sql)
  141. err = o.Raw(sql, adminName).First(&item).Error
  142. return
  143. }
  144. func GetSysAdminById(adminId int) (item *Admin, err error) {
  145. sql := `SELECT * FROM admin WHERE admin_id=? `
  146. sql = utils.ReplaceDriverKeywords("", sql)
  147. o := global.DbMap[utils.DbNameMaster]
  148. err = o.Raw(sql, adminId).First(&item).Error
  149. return
  150. }
  151. func AddAdmin(item *Admin) (err error) {
  152. o := global.DbMap[utils.DbNameMaster]
  153. err = o.Create(item).Error
  154. return
  155. }
  156. type SysuserEditReq struct {
  157. AdminId int `description:"系统用户id"`
  158. AdminName string `description:"系统用户名称"`
  159. RealName string `description:"系统用户姓名"`
  160. Mobile string `description:"手机号"`
  161. RoleId int `description:"角色id"`
  162. Enabled int `description:"1:有效,0:禁用"`
  163. Position string `description:"职位"`
  164. ResearchGroupIds string `description:"研究方向分组IDs"`
  165. Province string `description:"省"`
  166. ProvinceCode string `description:"省编码"`
  167. City string `description:"市"`
  168. CityCode string `description:"市编码"`
  169. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  170. Email string `description:"邮箱"`
  171. TelAreaCode string `description:"手机区号"`
  172. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  173. }
  174. type SysUserMoveReq struct {
  175. AdminId int `description:"系统用户id"`
  176. DepartmentId int `description:"部门id"`
  177. GroupId int `description:"分组id"`
  178. TeamId int `description:"小组id"`
  179. }
  180. type SysUserResetPassReq struct {
  181. AdminId int `description:"系统用户id"`
  182. Password string `description:"密码"`
  183. RePassword string `description:"重复密码"`
  184. }
  185. // SysuserEditEnabledReq 用户状态编辑
  186. type SysuserEditEnabledReq struct {
  187. AdminId int `description:"系统用户id"`
  188. Enabled int `description:"1:有效,0:禁用"`
  189. }
  190. // 变更状态
  191. func EditAdminEnabled(item *Admin) (err error) {
  192. o := global.DbMap[utils.DbNameMaster]
  193. sql := `UPDATE admin
  194. SET
  195. last_updated_time = NOW(),enabled=? WHERE admin_id = ? `
  196. sql = utils.ReplaceDriverKeywords("", sql)
  197. err = o.Exec(sql, item.Enabled, item.AdminId).Error
  198. return
  199. }
  200. // DisableAdmin 禁用用户
  201. func DisableAdmin(adminId int) (err error) {
  202. o := global.DbMap[utils.DbNameMaster]
  203. sql := `UPDATE admin
  204. SET
  205. last_updated_time = NOW(),disable_time=now(),enabled=0 WHERE admin_id = ? `
  206. sql = utils.ReplaceDriverKeywords("", sql)
  207. err = o.Exec(sql, adminId).Error
  208. return
  209. }
  210. type SysuserDeleteReq struct {
  211. AdminId int `description:"系统用户id"`
  212. }
  213. func DeleteSysuser(adminId int) (err error) {
  214. sql := `DELETE FROM admin WHERE admin_id=? `
  215. o := global.DbMap[utils.DbNameMaster]
  216. sql = utils.ReplaceDriverKeywords("", sql)
  217. err = o.Exec(sql, adminId).Error
  218. return
  219. }
  220. // GetSysAdminCountByMobile 查询系统中是否存在该手机号(如果有传入用户id,那么排除该用户)
  221. func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
  222. sql := `SELECT COUNT(1) AS count FROM admin WHERE mobile=? `
  223. if adminId > 0 {
  224. sql += ` AND admin_id != ` + fmt.Sprint(adminId)
  225. }
  226. sql = utils.ReplaceDriverKeywords("", sql)
  227. o := global.DbMap[utils.DbNameMaster]
  228. err = o.Raw(sql, mobile).Scan(&count).Error
  229. return
  230. }
  231. func GetSysUserItems(condition string, pars []interface{}) (items []*AdminItem, err error) {
  232. o := global.DbMap[utils.DbNameMaster]
  233. sql := `SELECT * FROM admin WHERE 1=1 `
  234. if condition != "" {
  235. sql += condition
  236. }
  237. sql += `ORDER BY last_updated_time DESC `
  238. sql = utils.ReplaceDriverKeywords("", sql)
  239. err = o.Raw(sql, pars...).Find(&items).Error
  240. return
  241. }
  242. // GetGroupSysUserList 根据分组id获取系统用户列表
  243. func GetGroupSysUserList(groupId int) (items []*AdminItem, err error) {
  244. o := global.DbMap[utils.DbNameMaster]
  245. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC `
  246. sql = utils.ReplaceDriverKeywords("", sql)
  247. err = o.Raw(sql, groupId).Find(&items).Error
  248. return
  249. }
  250. // GetDepartmentGroupSysUserList 根据部门id和分组id获取系统用户列表
  251. func GetDepartmentGroupSysUserList(departmentId, groupId int, roleTypeCodes string) (items []*AdminItem, err error) {
  252. o := global.DbMap[utils.DbNameMaster]
  253. sql := `SELECT * FROM admin WHERE department_id = ? and group_id=? and enabled=1 `
  254. if roleTypeCodes != "" {
  255. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  256. }
  257. sql += ` ORDER BY created_time asc `
  258. sql = utils.ReplaceDriverKeywords("", sql)
  259. err = o.Raw(sql, departmentId, groupId).Find(&items).Error
  260. return
  261. }
  262. // GetGrooupsysUserList 根据大组id和小组id获取系统用户列表
  263. func GetGrooupsysUserList(groupId int, roleTypeCodes string, enabled int) (items []*AdminItem, err error) {
  264. o := global.DbMap[utils.DbNameMaster]
  265. sql := `SELECT * FROM admin WHERE group_id = ? `
  266. if roleTypeCodes != "" {
  267. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  268. }
  269. if enabled >= 0 {
  270. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  271. }
  272. sql += ` ORDER BY created_time asc `
  273. sql = utils.ReplaceDriverKeywords("", sql)
  274. err = o.Raw(sql, groupId).Find(&items).Error
  275. return
  276. }
  277. // GetSysAdminByIdSlice 根据账户id列表获取账户信息列表
  278. func GetSysAdminByIdSlice(adminIdList []string) (items []*Admin, err error) {
  279. if len(adminIdList) <= 0 {
  280. return
  281. }
  282. sql := `SELECT * FROM admin WHERE admin_id in (` + strings.Join(adminIdList, ",") + `) `
  283. sql = utils.ReplaceDriverKeywords("", sql)
  284. o := global.DbMap[utils.DbNameMaster]
  285. err = o.Raw(sql).Find(&items).Error
  286. return
  287. }
  288. // GetTeamSysUserList 根据小组id获取系统用户列表
  289. func GetTeamSysUserList(teamId, enabled int) (items []*AdminItem, err error) {
  290. o := global.DbMap[utils.DbNameMaster]
  291. sql := `SELECT * FROM admin WHERE group_id=? `
  292. if enabled >= 0 {
  293. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  294. }
  295. sql += ` ORDER BY created_time DESC `
  296. sql = utils.ReplaceDriverKeywords("", sql)
  297. err = o.Raw(sql, teamId).Find(&items).Error
  298. return
  299. }
  300. func GetGroupIdByParentId(groupId int) (items []*string, err error) {
  301. o := global.DbMap[utils.DbNameMaster]
  302. sql := `SELECT group_id FROM sys_group WHERE parent_id=? ORDER BY create_time DESC `
  303. err = o.Raw(sql, groupId).Find(&items).Error
  304. return
  305. }
  306. func GetAdminByGroupId(groupId int) (items []*AdminItem, err error) {
  307. o := global.DbMap[utils.DbNameMaster]
  308. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC`
  309. sql = utils.ReplaceDriverKeywords("", sql)
  310. err = o.Raw(sql, groupId).Find(&items).Error
  311. return
  312. }
  313. type OpenIdList struct {
  314. OpenId string
  315. AdminId int
  316. }
  317. // GetRaiAdmin 获取权益内部人员(技术组+私募组)
  318. func GetRaiAdmin() (items []*AdminItem, err error) {
  319. o := global.DbMap[utils.DbNameMaster]
  320. sql := `SELECT
  321. admin_id,
  322. real_name,
  323. mobile
  324. FROM
  325. "admin"
  326. WHERE
  327. role_type_code LIKE '%rai%'
  328. AND group_id NOT IN (19, 10)
  329. AND enabled = 1
  330. OR (
  331. department_name = '产品技术部'
  332. AND enabled = 1
  333. )`
  334. err = o.Raw(sql).Find(&items).Error
  335. return
  336. }
  337. // SyncRoleData 同步角色数据
  338. type SyncRoleData struct {
  339. Source int `description:"来源: 1-CRM; 2-ETA"`
  340. RoleId int `description:"角色ID"`
  341. }
  342. // SyncAdminData 同步用户数据
  343. type SyncAdminData struct {
  344. Source int `description:"来源: 1-CRM; 2-ETA"`
  345. AdminName string `description:"用户名"`
  346. }
  347. // SyncDepartmentData 同步部门数据
  348. type SyncDepartmentData struct {
  349. Source int `description:"来源: 1-CRM; 2-ETA"`
  350. DepartmentId int `description:"部门ID"`
  351. }
  352. // SyncGroupData 同步分组数据
  353. type SyncGroupData struct {
  354. Source int `description:"来源: 1-CRM; 2-ETA"`
  355. GroupId int `description:"分组ID"`
  356. }
  357. // GetSysAdminList 获取admin列表
  358. func GetSysAdminList(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  359. fields := "*"
  360. if len(fieldArr) > 0 {
  361. fields = strings.Join(fieldArr, ",")
  362. }
  363. order := `enabled DESC, last_updated_time DESC`
  364. if orderRule != "" {
  365. order = orderRule
  366. }
  367. sql := fmt.Sprintf(`SELECT %s FROM admin WHERE 1=1 %s ORDER BY %s`, fields, condition, order)
  368. sql = utils.ReplaceDriverKeywords("", sql)
  369. o := global.DbMap[utils.DbNameMaster]
  370. err = o.Raw(sql, pars...).Find(&items).Error
  371. return
  372. }
  373. // GetSysAdminByIdList
  374. // @Description: 根据账户id列表获取账户信息列表
  375. // @author: Roc
  376. // @datetime 2024-06-05 10:49:50
  377. // @param adminIdList []int
  378. // @return items []*Admin
  379. // @return err error
  380. func GetSysAdminByIdList(adminIdList []int) (items []*Admin, err error) {
  381. num := len(adminIdList)
  382. if num <= 0 {
  383. return
  384. }
  385. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(num) + `) `
  386. sql = utils.ReplaceDriverKeywords("", sql)
  387. o := global.DbMap[utils.DbNameMaster]
  388. err = o.Raw(sql, adminIdList).Find(&items).Error
  389. return
  390. }