sys_admin.go 14 KB

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