sys_admin.go 13 KB

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