sys_admin.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package system
  2. import (
  3. "eta_gn/eta_api/global"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  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. sql := `SELECT * FROM "admin" WHERE 1=1 `
  46. if condition != "" {
  47. sql += condition
  48. }
  49. sql += `ORDER BY enabled DESC, last_updated_time DESC, created_time DESC LIMIT ?,?`
  50. pars = append(pars, startSize)
  51. pars = append(pars, pageSize)
  52. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  53. return
  54. }
  55. func GetSysuserListCount(condition string, pars []interface{}) (count int, err error) {
  56. sql := `SELECT COUNT(1) AS count FROM "admin" WHERE 1=1 `
  57. if condition != "" {
  58. sql += condition
  59. }
  60. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  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. Enabled int `description:"1:有效,0:禁用"`
  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. err = global.DEFAULT_DmSQL.Raw(sql, adminName).Scan(&count).Error
  120. return
  121. }
  122. func GetSysAdminByName(adminName string) (item *Admin, err error) {
  123. sql := `SELECT * FROM "admin" WHERE admin_name=? `
  124. err = global.DEFAULT_DmSQL.Raw(sql, adminName).First(&item).Error
  125. return
  126. }
  127. func GetSysAdminById(adminId int) (item *Admin, err error) {
  128. sql := `SELECT * FROM "admin" WHERE admin_id=? `
  129. err = global.DEFAULT_DmSQL.Raw(sql, adminId).First(&item).Error
  130. return
  131. }
  132. func AddAdmin(item *Admin) (err error) {
  133. err = global.DEFAULT_DmSQL.Create(item).Error
  134. return
  135. }
  136. type SysuserEditReq struct {
  137. AdminId int `description:"系统用户id"`
  138. AdminName string `description:"系统用户名称"`
  139. RealName string `description:"系统用户姓名"`
  140. Mobile string `description:"手机号"`
  141. RoleId int `description:"角色id"`
  142. Enabled int `description:"1:有效,0:禁用"`
  143. Position string `description:"职位"`
  144. ResearchGroupIds string `description:"研究方向分组IDs"`
  145. Province string `description:"省"`
  146. ProvinceCode string `description:"省编码"`
  147. City string `description:"市"`
  148. CityCode string `description:"市编码"`
  149. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  150. Email string `description:"邮箱"`
  151. TelAreaCode string `description:"手机区号"`
  152. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  153. }
  154. type SysUserMoveReq struct {
  155. AdminId int `description:"系统用户id"`
  156. DepartmentId int `description:"部门id"`
  157. GroupId int `description:"分组id"`
  158. TeamId int `description:"小组id"`
  159. }
  160. type SysUserResetPassReq struct {
  161. AdminId int `description:"系统用户id"`
  162. Password string `description:"密码"`
  163. RePassword string `description:"重复密码"`
  164. }
  165. // SysuserEditEnabledReq 用户状态编辑
  166. type SysuserEditEnabledReq struct {
  167. AdminId int `description:"系统用户id"`
  168. Enabled int `description:"1:有效,0:禁用"`
  169. }
  170. // 变更状态
  171. func EditAdminEnabled(item *Admin) (err error) {
  172. sql := `UPDATE "admin"
  173. SET
  174. last_updated_time = NOW(),enabled=? WHERE admin_id = ? `
  175. err = global.DEFAULT_DmSQL.Exec(sql, item.Enabled, item.AdminId).Error
  176. return
  177. }
  178. // DisableAdmin 禁用用户
  179. func DisableAdmin(adminId int) (err error) {
  180. sql := `UPDATE "admin"
  181. SET
  182. last_updated_time = NOW(),disable_time=now(),enabled=0 WHERE admin_id = ? `
  183. err = global.DEFAULT_DmSQL.Exec(sql, adminId).Error
  184. return
  185. }
  186. type SysuserDeleteReq struct {
  187. AdminId int `description:"系统用户id"`
  188. }
  189. func DeleteSysuser(adminId int) (err error) {
  190. sql := `DELETE FROM "admin" WHERE admin_id=? `
  191. err = global.DEFAULT_DmSQL.Exec(sql, adminId).Error
  192. return
  193. }
  194. // GetSysAdminCountByMobile 查询系统中是否存在该手机号(如果有传入用户id,那么排除该用户)
  195. func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
  196. sql := `SELECT COUNT(1) AS count FROM "admin" WHERE mobile=? `
  197. if adminId > 0 {
  198. sql += ` AND admin_id != ` + fmt.Sprint(adminId)
  199. }
  200. err = global.DEFAULT_DmSQL.Raw(sql, mobile).First(&count).Error
  201. return
  202. }
  203. func GetSysUserItems(condition string, pars []interface{}) (items []*AdminItem, err error) {
  204. sql := `SELECT * FROM "admin" WHERE 1=1 `
  205. if condition != "" {
  206. sql += condition
  207. }
  208. sql += `ORDER BY last_updated_time DESC `
  209. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  210. return
  211. }
  212. // GetGroupSysUserList 根据分组id获取系统用户列表
  213. func GetGroupSysUserList(groupId int) (items []*AdminItem, err error) {
  214. sql := `SELECT * FROM "admin" WHERE group_id=? ORDER BY created_time DESC `
  215. err = global.DEFAULT_DmSQL.Raw(sql, groupId).Find(&items).Error
  216. return
  217. }
  218. // GetDepartmentGroupSysUserList 根据部门id和分组id获取系统用户列表
  219. func GetDepartmentGroupSysUserList(departmentId, groupId int, roleTypeCodes string) (items []*AdminItem, err error) {
  220. sql := `SELECT * FROM "admin" WHERE department_id = ? and group_id=? and enabled=1 `
  221. if roleTypeCodes != "" {
  222. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  223. }
  224. sql += ` ORDER BY created_time asc `
  225. err = global.DEFAULT_DmSQL.Raw(sql, departmentId, groupId).Find(&items).Error
  226. return
  227. }
  228. // GetGrooupsysUserList 根据大组id和小组id获取系统用户列表
  229. func GetGrooupsysUserList(groupId int, roleTypeCodes string, enabled int) (items []*AdminItem, err error) {
  230. sql := `SELECT * FROM "admin" WHERE group_id = ? `
  231. if roleTypeCodes != "" {
  232. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  233. }
  234. if enabled >= 0 {
  235. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  236. }
  237. sql += ` ORDER BY created_time asc `
  238. err = global.DEFAULT_DmSQL.Raw(sql, groupId).Find(&items).Error
  239. return
  240. }
  241. // GetSysAdminByIdSlice 根据账户id列表获取账户信息列表
  242. func GetSysAdminByIdSlice(adminIdList []string) (items []*Admin, err error) {
  243. if len(adminIdList) <= 0 {
  244. return
  245. }
  246. sql := `SELECT * FROM "admin" WHERE admin_id in (` + strings.Join(adminIdList, ",") + `) `
  247. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  248. return
  249. }
  250. // GetTeamSysUserList 根据小组id获取系统用户列表
  251. func GetTeamSysUserList(teamId, enabled int) (items []*AdminItem, err error) {
  252. sql := `SELECT * FROM "admin" WHERE group_id=? `
  253. if enabled >= 0 {
  254. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  255. }
  256. sql += ` ORDER BY created_time DESC `
  257. err = global.DEFAULT_DmSQL.Raw(sql, teamId).Find(&items).Error
  258. return
  259. }
  260. func GetGroupIdByParentId(groupId int) (items []*string, err error) {
  261. sql := `SELECT group_id FROM sys_group WHERE parent_id=? ORDER BY create_time DESC `
  262. err = global.DEFAULT_DmSQL.Raw(sql, groupId).Find(&items).Error
  263. return
  264. }
  265. type OpenIdList struct {
  266. OpenId string
  267. AdminId int
  268. }
  269. // GetRaiAdmin 获取权益内部人员(技术组+私募组)
  270. func GetRaiAdmin() (items []*AdminItem, err error) {
  271. sql := `SELECT
  272. admin_id,
  273. real_name,
  274. mobile
  275. FROM
  276. "admin"
  277. WHERE
  278. role_type_code LIKE '%rai%'
  279. AND group_id NOT IN (19, 10)
  280. AND enabled = 1
  281. OR (
  282. department_name = '产品技术部'
  283. AND enabled = 1
  284. )`
  285. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  286. return
  287. }
  288. // SyncRoleData 同步角色数据
  289. type SyncRoleData struct {
  290. Source int `description:"来源: 1-CRM; 2-ETA"`
  291. RoleId int `description:"角色ID"`
  292. }
  293. // SyncAdminData 同步用户数据
  294. type SyncAdminData struct {
  295. Source int `description:"来源: 1-CRM; 2-ETA"`
  296. AdminName string `description:"用户名"`
  297. }
  298. // SyncDepartmentData 同步部门数据
  299. type SyncDepartmentData struct {
  300. Source int `description:"来源: 1-CRM; 2-ETA"`
  301. DepartmentId int `description:"部门ID"`
  302. }
  303. // SyncGroupData 同步分组数据
  304. type SyncGroupData struct {
  305. Source int `description:"来源: 1-CRM; 2-ETA"`
  306. GroupId int `description:"分组ID"`
  307. }
  308. // GetSysAdminList 获取admin列表
  309. func GetSysAdminList(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  310. fields := "*"
  311. if len(fieldArr) > 0 {
  312. fields = strings.Join(fieldArr, ",")
  313. }
  314. order := `enabled DESC, last_updated_time DESC`
  315. if orderRule != "" {
  316. order = orderRule
  317. }
  318. sql := fmt.Sprintf(`SELECT %s FROM "admin" WHERE 1=1 %s ORDER BY %s`, fields, condition, order)
  319. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  320. return
  321. }
  322. // GetSysAdminListAll 获取所有admin列表
  323. func GetSysAdminListAll() (items []*Admin, err error) {
  324. sql := `SELECT admin_id, admin_name, real_name FROM "admin" `
  325. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  326. return
  327. }