sys_admin.go 14 KB

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