sys_admin.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. }
  42. func GetSysuserList(condition string, pars []interface{}, startSize, pageSize int) (items []*AdminItem, err error) {
  43. o := orm.NewOrm()
  44. sql := `SELECT * FROM admin WHERE 1=1 `
  45. if condition != "" {
  46. sql += condition
  47. }
  48. sql += `ORDER BY enabled DESC, last_updated_time DESC, created_time DESC LIMIT ?,?`
  49. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  50. return
  51. }
  52. func GetSysuserListCount(condition string, pars []interface{}) (count int, err error) {
  53. o := orm.NewOrm()
  54. sql := `SELECT COUNT(1) AS count FROM admin WHERE 1=1 `
  55. if condition != "" {
  56. sql += condition
  57. }
  58. err = o.Raw(sql, pars).QueryRow(&count)
  59. return
  60. }
  61. type AdminRespItem struct {
  62. AdminId int `description:"系统用户id"`
  63. AdminName string `description:"系统用户名称"`
  64. RealName string `description:"系统用户姓名"`
  65. Password string
  66. LastUpdatedPasswordTime string `json:"-"`
  67. Enabled int `description:"1:有效,0:禁用"`
  68. Email string `description:"系统用户邮箱"`
  69. LastLoginTime string
  70. CreatedTime time.Time
  71. LastUpdatedTime string
  72. Role string `description:"系统用户角色"`
  73. Mobile string `description:"手机号"`
  74. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  75. RoleId int `description:"角色id"`
  76. RoleName string `description:"角色名称"`
  77. RoleTypeCode string `description:"角色编码"`
  78. DepartmentId int `description:"部门id"`
  79. DepartmentName string `json:"-" description:"部门名称"`
  80. parentId int `description:"父级id"`
  81. GroupId int `description:"分组id"`
  82. GroupName string `json:"-" description:"分组名称"`
  83. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  84. Position string `description:"职位"`
  85. DepartmentGroup string `description:"部门分组"`
  86. LabelVal int `description:"标签:1:超级管理员,2:管理员,3:部门经理,4:组长,5:ficc销售主管"`
  87. }
  88. type SysuserListResp struct {
  89. List []*AdminItem
  90. Paging *paging.PagingItem `description:"分页数据"`
  91. }
  92. type SysuserAddReq struct {
  93. AdminName string `description:"系统用户名称"`
  94. AdminAvatar string `description:"用户头像"`
  95. RealName string `description:"系统用户姓名"`
  96. Password string `description:"密码"`
  97. Mobile string `description:"手机号"`
  98. RoleId int `description:"角色id"`
  99. DepartmentId int `description:"部门id"`
  100. GroupId int `description:"分组id"`
  101. TeamId int `description:"小组id"`
  102. //Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人"`
  103. Position string `description:"职位"`
  104. ResearchGroupIds string `description:"研究方向分组IDs"`
  105. Province string `description:"省"`
  106. ProvinceCode string `description:"省编码"`
  107. City string `description:"市"`
  108. CityCode string `description:"市编码"`
  109. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  110. }
  111. func GetSysAdminCount(adminName string) (count int, err error) {
  112. sql := `SELECT COUNT(1) AS count FROM admin WHERE admin_name=? `
  113. o := orm.NewOrm()
  114. err = o.Raw(sql, adminName).QueryRow(&count)
  115. return
  116. }
  117. func GetSysAdminByName(adminName string) (item *Admin, err error) {
  118. sql := `SELECT * FROM admin WHERE admin_name=? `
  119. o := orm.NewOrm()
  120. err = o.Raw(sql, adminName).QueryRow(&item)
  121. return
  122. }
  123. func GetSysAdminById(adminId int) (item *Admin, err error) {
  124. sql := `SELECT * FROM admin WHERE admin_id=? `
  125. o := orm.NewOrm()
  126. err = o.Raw(sql, adminId).QueryRow(&item)
  127. return
  128. }
  129. func AddAdmin(item *Admin) (err error) {
  130. o := orm.NewOrm()
  131. adminId, err := o.Insert(item)
  132. item.AdminId = int(adminId)
  133. return
  134. }
  135. type SysuserEditReq struct {
  136. AdminId int `description:"系统用户id"`
  137. AdminName string `description:"系统用户名称"`
  138. RealName string `description:"系统用户姓名"`
  139. Mobile string `description:"手机号"`
  140. RoleId int `description:"角色id"`
  141. Enabled int `description:"1:有效,0:禁用"`
  142. Position string `description:"职位"`
  143. ResearchGroupIds string `description:"研究方向分组IDs"`
  144. Province string `description:"省"`
  145. ProvinceCode string `description:"省编码"`
  146. City string `description:"市"`
  147. CityCode string `description:"市编码"`
  148. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  149. }
  150. type SysUserMoveReq struct {
  151. AdminId int `description:"系统用户id"`
  152. DepartmentId int `description:"部门id"`
  153. GroupId int `description:"分组id"`
  154. TeamId int `description:"小组id"`
  155. }
  156. type SysUserResetPassReq struct {
  157. AdminId int `description:"系统用户id"`
  158. Password string `description:"密码"`
  159. RePassword string `description:"重复密码"`
  160. }
  161. // 用户状态编辑
  162. type SysuserEditEnabledReq struct {
  163. AdminId int `description:"系统用户id"`
  164. Enabled int `description:"1:有效,0:禁用"`
  165. }
  166. func EditAdmin(item *Admin) (err error) {
  167. o := orm.NewOrm()
  168. sql := `UPDATE admin
  169. SET
  170. admin_name= ?,
  171. real_name = ?,
  172. password = ?,
  173. last_updated_password_time = NOW(),
  174. email = ?,
  175. last_updated_time = NOW(),
  176. mobile = ?,
  177. role_type = ?,
  178. role_id = ?,
  179. role_name = ?,
  180. department_id = ?,
  181. department_name = ?,
  182. group_id = ?,
  183. group_name = ?,
  184. authority = ?,
  185. position = ?,
  186. role_type_code=?,
  187. enabled=?,
  188. admin_avatar = ?,
  189. province=?,
  190. province_code=?,
  191. city=?,
  192. city_code=?,
  193. employee_id = ?
  194. WHERE admin_id = ? `
  195. _, err = o.Raw(sql, item.AdminName, item.RealName, item.Password, item.Email, item.Mobile, item.RoleType, item.RoleId, item.RoleName, item.DepartmentId, item.DepartmentName,
  196. item.GroupId, item.GroupName, item.Authority, item.Position, item.RoleTypeCode, item.Enabled, item.AdminAvatar, item.Province, item.ProvinceCode, item.City, item.CityCode, item.EmployeeId, item.AdminId).Exec()
  197. return
  198. }
  199. // 变更状态
  200. func EditAdminEnabled(item *Admin) (err error) {
  201. o := orm.NewOrm()
  202. sql := `UPDATE admin
  203. SET
  204. last_updated_time = NOW(),enabled=? WHERE admin_id = ? `
  205. _, err = o.Raw(sql, item.Enabled, item.AdminId).Exec()
  206. return
  207. }
  208. // DisableAdmin 禁用用户
  209. func DisableAdmin(adminId int) (err error) {
  210. o := orm.NewOrm()
  211. sql := `UPDATE admin
  212. SET
  213. last_updated_time = NOW(),disable_time=now(),enabled=0 WHERE admin_id = ? `
  214. _, err = o.Raw(sql, adminId).Exec()
  215. return
  216. }
  217. type SysuserDeleteReq struct {
  218. AdminId int `description:"系统用户id"`
  219. }
  220. func DeleteSysuser(adminId int) (err error) {
  221. sql := `DELETE FROM admin WHERE admin_id=? `
  222. o := orm.NewOrm()
  223. _, err = o.Raw(sql, adminId).Exec()
  224. return
  225. }
  226. // GetSysAdminCountByMobile 查询系统中是否存在该手机号(如果有传入用户id,那么排除该用户)
  227. func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
  228. sql := `SELECT COUNT(1) AS count FROM admin WHERE mobile=? `
  229. if adminId > 0 {
  230. sql += ` AND admin_id != ` + fmt.Sprint(adminId)
  231. }
  232. o := orm.NewOrm()
  233. err = o.Raw(sql, mobile).QueryRow(&count)
  234. return
  235. }
  236. func GetSysUserItems(condition string, pars []interface{}) (items []*AdminItem, err error) {
  237. o := orm.NewOrm()
  238. sql := `SELECT * FROM admin WHERE 1=1 `
  239. if condition != "" {
  240. sql += condition
  241. }
  242. sql += `ORDER BY last_updated_time DESC `
  243. _, err = o.Raw(sql, pars).QueryRows(&items)
  244. return
  245. }
  246. func GetSysUserItemsOrderByCreated(condition string, pars []interface{}) (items []*AdminItem, err error) {
  247. o := orm.NewOrm()
  248. sql := `SELECT * FROM admin WHERE 1=1 `
  249. if condition != "" {
  250. sql += condition
  251. }
  252. sql += ` ORDER BY created_time DESC `
  253. _, err = o.Raw(sql, pars).QueryRows(&items)
  254. return
  255. }
  256. func GetSysUserItemsList(condition string, pars []interface{}, startSize, pageSize int) (items []*AdminItem, err error) {
  257. o := orm.NewOrm()
  258. sql := `SELECT * FROM admin WHERE 1=1 `
  259. if condition != "" {
  260. sql += condition
  261. }
  262. sql += ` ORDER BY created_time DESC limit ?,?`
  263. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  264. return
  265. }
  266. func GetSysUserItemsTotal(condition string, pars []interface{}) (total int64, err error) {
  267. o := orm.NewOrm()
  268. sql := `SELECT count(*) FROM admin WHERE 1=1 `
  269. if condition != "" {
  270. sql += condition
  271. }
  272. err = o.Raw(sql, pars).QueryRow(&total)
  273. return
  274. }
  275. // GetGroupSysUserList 根据分组id获取系统用户列表
  276. func GetGroupSysUserList(groupId int) (items []*AdminItem, err error) {
  277. o := orm.NewOrm()
  278. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC `
  279. _, err = o.Raw(sql, groupId).QueryRows(&items)
  280. return
  281. }
  282. // GetDepartmentGroupSysUserList 根据部门id和分组id获取系统用户列表
  283. func GetDepartmentGroupSysUserList(departmentId, groupId int, roleTypeCodes string) (items []*AdminItem, err error) {
  284. o := orm.NewOrm()
  285. sql := `SELECT * FROM admin WHERE department_id = ? and group_id=? and enabled=1 `
  286. if roleTypeCodes != "" {
  287. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  288. }
  289. sql += ` ORDER BY created_time asc `
  290. _, err = o.Raw(sql, departmentId, groupId).QueryRows(&items)
  291. return
  292. }
  293. // GetGrooupsysUserList 根据大组id和小组id获取系统用户列表
  294. func GetGrooupsysUserList(groupId int, roleTypeCodes string, enabled int) (items []*AdminItem, err error) {
  295. o := orm.NewOrm()
  296. sql := `SELECT * FROM admin WHERE group_id = ? `
  297. if roleTypeCodes != "" {
  298. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  299. }
  300. if enabled >= 0 {
  301. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  302. }
  303. sql += ` ORDER BY created_time asc `
  304. _, err = o.Raw(sql, groupId).QueryRows(&items)
  305. return
  306. }
  307. // GetSysAdminByIdSlice 根据账户id列表获取账户信息列表
  308. func GetSysAdminByIdSlice(adminIdList []string) (items []*Admin, err error) {
  309. if len(adminIdList) <= 0 {
  310. return
  311. }
  312. sql := `SELECT * FROM admin WHERE admin_id in (` + strings.Join(adminIdList, ",") + `) `
  313. o := orm.NewOrm()
  314. _, err = o.Raw(sql).QueryRows(&items)
  315. return
  316. }
  317. // GetTeamSysUserList 根据小组id获取系统用户列表
  318. func GetTeamSysUserList(teamId, enabled int) (items []*AdminItem, err error) {
  319. o := orm.NewOrm()
  320. sql := `SELECT * FROM admin WHERE group_id=? `
  321. if enabled >= 0 {
  322. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  323. }
  324. sql += ` ORDER BY created_time DESC `
  325. _, err = o.Raw(sql, teamId).QueryRows(&items)
  326. return
  327. }
  328. // GetTeamSysUserList 根据小组id获取系统用户列表
  329. func GetTeamSysUserListByIds(ids string) (items []*AdminItem, err error) {
  330. o := orm.NewOrm()
  331. sql := `SELECT * FROM admin WHERE group_id IN (` + ids + `) ORDER BY created_time DESC `
  332. _, err = o.Raw(sql).QueryRows(&items)
  333. return
  334. }
  335. func GetGroupIdByParentId(groupId int) (items []*string, err error) {
  336. o := orm.NewOrm()
  337. sql := `SELECT group_id FROM sys_group WHERE parent_id=? ORDER BY create_time DESC `
  338. _, err = o.Raw(sql, groupId).QueryRows(&items)
  339. return
  340. }
  341. // GetSysUserByParentIdGroupId 查询主管下的所有组员
  342. func GetSysUserByParentIdGroupId(groupId int) (items []*AdminItem, err error) {
  343. o := orm.NewOrm()
  344. sql := `select real_name, role_id, role_type_code, group_id, admin_id from admin where group_id in (SELECT group_id from sys_group where parent_id=?) OR group_id=?`
  345. _, err = o.Raw(sql, groupId, groupId).QueryRows(&items)
  346. return
  347. }
  348. func GetAdminByGroupId(groupId int) (items []*AdminItem, err error) {
  349. o := orm.NewOrm()
  350. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC`
  351. _, err = o.Raw(sql, groupId).QueryRows(&items)
  352. return
  353. }
  354. // GetSysAdminByIds 根据主键集合获取管理员信息
  355. func GetSysAdminByIds(adminIds string) (items []*Admin, err error) {
  356. if adminIds == "" {
  357. return
  358. }
  359. o := orm.NewOrm()
  360. sql := `SELECT * FROM admin WHERE admin_id IN (` + adminIds + `)`
  361. _, err = o.Raw(sql).QueryRows(&items)
  362. return
  363. }
  364. type OpenIdList struct {
  365. OpenId string
  366. AdminId int
  367. }
  368. // GetAdminOpenIdListByMobile 根据手机号获取用户的openid列表
  369. func GetAdminOpenIdListByMobile(mobile string) (items []*OpenIdList, err error) {
  370. sql := `SELECT admin_id, open_id FROM admin
  371. WHERE open_id != "" and mobile=? `
  372. _, err = orm.NewOrm().Raw(sql, mobile).QueryRows(&items)
  373. return
  374. }
  375. // ResearcherAdminAndUser 研究员admin信息及wx_user信息
  376. type ResearcherAdminAndUser struct {
  377. UserId int `description:"用户ID"`
  378. UserName string `description:"用户名称"`
  379. AdminId int `description:"管理员ID"`
  380. AdminName string `description:"管理员姓名"`
  381. OpenId string `description:"openid"`
  382. }
  383. // GetResearcherAdminAndWxUserByAdminId 通过adminId获取研究员admin及user信息
  384. func GetResearcherAdminAndWxUserByAdminId(adminId int) (item *ResearcherAdminAndUser, err error) {
  385. sql := `SELECT
  386. a.admin_id,
  387. a.real_name AS admin_name,
  388. a.open_id,
  389. b.user_id,
  390. b.real_name AS user_name
  391. FROM
  392. admin AS a
  393. JOIN wx_user AS b ON a.mobile = b.mobile
  394. WHERE
  395. a.admin_id = ? AND a.open_id != ""`
  396. err = orm.NewOrm().Raw(sql, adminId).QueryRow(&item)
  397. return
  398. }
  399. // 获取本组的销售ID
  400. func GetSelleridWhichGroup(companyId, productId int) (adminId string, err error) {
  401. o := orm.NewOrm()
  402. sql := ` SELECT
  403. GROUP_CONCAT( DISTINCT admin_id SEPARATOR ',' ) AS adminId
  404. FROM
  405. admin
  406. WHERE
  407. group_id IN (
  408. SELECT
  409. a.group_id
  410. FROM
  411. company_product AS a
  412. INNER JOIN admin AS b ON a.seller_id = b.admin_id
  413. WHERE
  414. a.company_id = ?
  415. AND a.product_id = ?
  416. )`
  417. err = o.Raw(sql, companyId, productId).QueryRow(&adminId)
  418. return
  419. }
  420. // GetSysuserRaiList 获取权益销售
  421. func GetSysuserRaiList() (items []*AdminItem, err error) {
  422. o := orm.NewOrm()
  423. sql := `SELECT
  424. real_name,
  425. mobile,
  426. group_name
  427. FROM
  428. admin
  429. WHERE
  430. role_type_code LIKE '%rai%'
  431. AND group_id NOT IN ( 19, 10, 17 )
  432. AND enabled = 1
  433. OR real_name IN ( '沈涛', '张传星' )`
  434. _, err = o.Raw(sql).QueryRows(&items)
  435. return
  436. }
  437. func GetSellerIdsByDepId(depId int) (ids []string, err error) {
  438. o := orm.NewOrm()
  439. sql := `SELECT
  440. admin_id
  441. FROM
  442. admin
  443. WHERE
  444. department_id = ? `
  445. _, err = o.Raw(sql, depId).QueryRows(&ids)
  446. return
  447. }
  448. // GetRaiAdmin 获取权益内部人员(技术组+私募组)
  449. func GetRaiAdmin() (items []*AdminItem, err error) {
  450. o := orm.NewOrm()
  451. sql := `SELECT
  452. admin_id,
  453. real_name,
  454. mobile
  455. FROM
  456. admin
  457. WHERE
  458. role_type_code LIKE '%rai%'
  459. AND group_id NOT IN (19, 10)
  460. AND enabled = 1
  461. OR (
  462. department_name = '产品技术部'
  463. AND enabled = 1
  464. )`
  465. _, err = o.Raw(sql).QueryRows(&items)
  466. return
  467. }