sys_admin.go 13 KB

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