sys_admin.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. // 用户状态编辑
  167. type SysuserEditEnabledReq struct {
  168. AdminId int `description:"系统用户id"`
  169. Enabled int `description:"1:有效,0:禁用"`
  170. }
  171. func EditAdmin(item *Admin) (err error) {
  172. o := orm.NewOrm()
  173. sql := `UPDATE admin
  174. SET
  175. admin_name= ?,
  176. real_name = ?,
  177. password = ?,
  178. last_updated_password_time = NOW(),
  179. email = ?,
  180. last_updated_time = NOW(),
  181. mobile = ?,
  182. role_type = ?,
  183. role_id = ?,
  184. role_name = ?,
  185. department_id = ?,
  186. department_name = ?,
  187. group_id = ?,
  188. group_name = ?,
  189. authority = ?,
  190. position = ?,
  191. role_type_code=?,
  192. enabled=?,
  193. admin_avatar = ?,
  194. province=?,
  195. province_code=?,
  196. city=?,
  197. city_code=?,
  198. employee_id = ?
  199. WHERE admin_id = ? `
  200. _, err = o.Raw(sql, item.AdminName, item.RealName, item.Password, item.Email, item.Mobile, item.RoleType, item.RoleId, item.RoleName, item.DepartmentId, item.DepartmentName,
  201. 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()
  202. return
  203. }
  204. // 变更状态
  205. func EditAdminEnabled(item *Admin) (err error) {
  206. o := orm.NewOrm()
  207. sql := `UPDATE admin
  208. SET
  209. last_updated_time = NOW(),enabled=? WHERE admin_id = ? `
  210. _, err = o.Raw(sql, item.Enabled, item.AdminId).Exec()
  211. return
  212. }
  213. // DisableAdmin 禁用用户
  214. func DisableAdmin(adminId int) (err error) {
  215. o := orm.NewOrm()
  216. sql := `UPDATE admin
  217. SET
  218. last_updated_time = NOW(),disable_time=now(),enabled=0 WHERE admin_id = ? `
  219. _, err = o.Raw(sql, adminId).Exec()
  220. return
  221. }
  222. type SysuserDeleteReq struct {
  223. AdminId int `description:"系统用户id"`
  224. }
  225. func DeleteSysuser(adminId int) (err error) {
  226. sql := `DELETE FROM admin WHERE admin_id=? `
  227. o := orm.NewOrm()
  228. _, err = o.Raw(sql, adminId).Exec()
  229. return
  230. }
  231. // GetSysAdminCountByMobile 查询系统中是否存在该手机号(如果有传入用户id,那么排除该用户)
  232. func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
  233. sql := `SELECT COUNT(1) AS count FROM admin WHERE mobile=? `
  234. if adminId > 0 {
  235. sql += ` AND admin_id != ` + fmt.Sprint(adminId)
  236. }
  237. o := orm.NewOrm()
  238. err = o.Raw(sql, mobile).QueryRow(&count)
  239. return
  240. }
  241. func GetSysUserItems(condition string, pars []interface{}) (items []*AdminItem, err error) {
  242. o := orm.NewOrm()
  243. sql := `SELECT * FROM admin WHERE 1=1 `
  244. if condition != "" {
  245. sql += condition
  246. }
  247. sql += `ORDER BY last_updated_time DESC `
  248. _, err = o.Raw(sql, pars).QueryRows(&items)
  249. return
  250. }
  251. func GetSysUserItemsOrderByCreated(condition string, pars []interface{}) (items []*AdminItem, err error) {
  252. o := orm.NewOrm()
  253. sql := `SELECT * FROM admin WHERE 1=1 `
  254. if condition != "" {
  255. sql += condition
  256. }
  257. sql += ` ORDER BY created_time DESC `
  258. _, err = o.Raw(sql, pars).QueryRows(&items)
  259. return
  260. }
  261. func GetSysUserItemsList(condition string, pars []interface{}, startSize, pageSize int) (items []*AdminItem, err error) {
  262. o := orm.NewOrm()
  263. sql := `SELECT * FROM admin WHERE 1=1 `
  264. if condition != "" {
  265. sql += condition
  266. }
  267. sql += ` ORDER BY created_time DESC limit ?,?`
  268. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  269. return
  270. }
  271. func GetSysUserItemsTotal(condition string, pars []interface{}) (total int64, err error) {
  272. o := orm.NewOrm()
  273. sql := `SELECT count(*) FROM admin WHERE 1=1 `
  274. if condition != "" {
  275. sql += condition
  276. }
  277. err = o.Raw(sql, pars).QueryRow(&total)
  278. return
  279. }
  280. // GetGroupSysUserList 根据分组id获取系统用户列表
  281. func GetGroupSysUserList(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. // GetDepartmentGroupSysUserList 根据部门id和分组id获取系统用户列表
  288. func GetDepartmentGroupSysUserList(departmentId, groupId int, roleTypeCodes string) (items []*AdminItem, err error) {
  289. o := orm.NewOrm()
  290. sql := `SELECT * FROM admin WHERE department_id = ? and group_id=? and enabled=1 `
  291. if roleTypeCodes != "" {
  292. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  293. }
  294. sql += ` ORDER BY created_time asc `
  295. _, err = o.Raw(sql, departmentId, groupId).QueryRows(&items)
  296. return
  297. }
  298. // GetGrooupsysUserList 根据大组id和小组id获取系统用户列表
  299. func GetGrooupsysUserList(groupId int, roleTypeCodes string, enabled int) (items []*AdminItem, err error) {
  300. o := orm.NewOrm()
  301. sql := `SELECT * FROM admin WHERE group_id = ? `
  302. if roleTypeCodes != "" {
  303. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  304. }
  305. if enabled >= 0 {
  306. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  307. }
  308. sql += ` ORDER BY created_time asc `
  309. _, err = o.Raw(sql, groupId).QueryRows(&items)
  310. return
  311. }
  312. // GetSysAdminByIdSlice 根据账户id列表获取账户信息列表
  313. func GetSysAdminByIdSlice(adminIdList []string) (items []*Admin, err error) {
  314. if len(adminIdList) <= 0 {
  315. return
  316. }
  317. sql := `SELECT * FROM admin WHERE admin_id in (` + strings.Join(adminIdList, ",") + `) `
  318. o := orm.NewOrm()
  319. _, err = o.Raw(sql).QueryRows(&items)
  320. return
  321. }
  322. // GetTeamSysUserList 根据小组id获取系统用户列表
  323. func GetTeamSysUserList(teamId, enabled int) (items []*AdminItem, err error) {
  324. o := orm.NewOrm()
  325. sql := `SELECT * FROM admin WHERE group_id=? `
  326. if enabled >= 0 {
  327. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  328. }
  329. sql += ` ORDER BY created_time DESC `
  330. _, err = o.Raw(sql, teamId).QueryRows(&items)
  331. return
  332. }
  333. // GetTeamSysUserList 根据小组id获取系统用户列表
  334. func GetTeamSysUserListByIds(ids string) (items []*AdminItem, err error) {
  335. o := orm.NewOrm()
  336. sql := `SELECT * FROM admin WHERE group_id IN (` + ids + `) ORDER BY created_time DESC `
  337. _, err = o.Raw(sql).QueryRows(&items)
  338. return
  339. }
  340. func GetGroupIdByParentId(groupId int) (items []*string, err error) {
  341. o := orm.NewOrm()
  342. sql := `SELECT group_id FROM sys_group WHERE parent_id=? ORDER BY create_time DESC `
  343. _, err = o.Raw(sql, groupId).QueryRows(&items)
  344. return
  345. }
  346. // GetSysUserByParentIdGroupId 查询主管下的所有组员
  347. func GetSysUserByParentIdGroupId(groupId int) (items []*AdminItem, err error) {
  348. o := orm.NewOrm()
  349. 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=?`
  350. _, err = o.Raw(sql, groupId, groupId).QueryRows(&items)
  351. return
  352. }
  353. func GetAdminByGroupId(groupId int) (items []*AdminItem, err error) {
  354. o := orm.NewOrm()
  355. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC`
  356. _, err = o.Raw(sql, groupId).QueryRows(&items)
  357. return
  358. }
  359. // GetSysAdminByIds 根据主键集合获取管理员信息
  360. func GetSysAdminByIds(adminIds string) (items []*Admin, err error) {
  361. if adminIds == "" {
  362. return
  363. }
  364. o := orm.NewOrm()
  365. sql := `SELECT * FROM admin WHERE admin_id IN (` + adminIds + `)`
  366. _, err = o.Raw(sql).QueryRows(&items)
  367. return
  368. }
  369. type OpenIdList struct {
  370. OpenId string
  371. AdminId int
  372. }
  373. // GetAdminOpenIdListByMobile 根据手机号获取用户的openid列表
  374. func GetAdminOpenIdListByMobile(mobile string) (items []*OpenIdList, err error) {
  375. sql := `SELECT admin_id, open_id FROM admin
  376. WHERE open_id != "" and mobile=? `
  377. _, err = orm.NewOrm().Raw(sql, mobile).QueryRows(&items)
  378. return
  379. }
  380. // ResearcherAdminAndUser 研究员admin信息及wx_user信息
  381. type ResearcherAdminAndUser struct {
  382. UserId int `description:"用户ID"`
  383. UserName string `description:"用户名称"`
  384. AdminId int `description:"管理员ID"`
  385. AdminName string `description:"管理员姓名"`
  386. OpenId string `description:"openid"`
  387. }
  388. // GetResearcherAdminAndWxUserByAdminId 通过adminId获取研究员admin及user信息
  389. func GetResearcherAdminAndWxUserByAdminId(adminId int) (item *ResearcherAdminAndUser, err error) {
  390. sql := `SELECT
  391. a.admin_id,
  392. a.real_name AS admin_name,
  393. a.open_id,
  394. b.user_id,
  395. b.real_name AS user_name
  396. FROM
  397. admin AS a
  398. JOIN wx_user AS b ON a.mobile = b.mobile
  399. WHERE
  400. a.admin_id = ? AND a.open_id != ""`
  401. err = orm.NewOrm().Raw(sql, adminId).QueryRow(&item)
  402. return
  403. }
  404. // 获取本组的销售ID
  405. func GetSelleridWhichGroup(companyId, productId int) (adminId string, err error) {
  406. o := orm.NewOrm()
  407. sql := ` SELECT
  408. GROUP_CONCAT( DISTINCT admin_id SEPARATOR ',' ) AS adminId
  409. FROM
  410. admin
  411. WHERE
  412. group_id IN (
  413. SELECT
  414. a.group_id
  415. FROM
  416. company_product AS a
  417. INNER JOIN admin AS b ON a.seller_id = b.admin_id
  418. WHERE
  419. a.company_id = ?
  420. AND a.product_id = ?
  421. )`
  422. err = o.Raw(sql, companyId, productId).QueryRow(&adminId)
  423. return
  424. }
  425. // GetSysuserRaiList 获取权益销售
  426. func GetSysuserRaiList() (items []*AdminItem, err error) {
  427. o := orm.NewOrm()
  428. sql := `SELECT
  429. real_name,
  430. mobile,
  431. group_name
  432. FROM
  433. admin
  434. WHERE
  435. role_type_code LIKE '%rai%'
  436. AND group_id NOT IN ( 19, 10, 17 )
  437. AND enabled = 1
  438. OR real_name IN ( '沈涛', '张传星' )
  439. OR mobile IN ( '15011263967', '18935318900' , '15600639653', '18010097689', '13520990566') ` // 先写死,看情况要不要改
  440. _, err = o.Raw(sql).QueryRows(&items)
  441. return
  442. }
  443. func GetSellerIdsByDepId(depId int) (ids []string, err error) {
  444. o := orm.NewOrm()
  445. sql := `SELECT
  446. admin_id
  447. FROM
  448. admin
  449. WHERE
  450. department_id = ? `
  451. _, err = o.Raw(sql, depId).QueryRows(&ids)
  452. return
  453. }
  454. // GetRaiAdmin 获取权益内部人员(技术组+私募组)
  455. func GetRaiAdmin() (items []*AdminItem, err error) {
  456. o := orm.NewOrm()
  457. sql := `SELECT
  458. admin_id,
  459. real_name,
  460. mobile
  461. FROM
  462. admin
  463. WHERE
  464. role_type_code LIKE '%rai%'
  465. AND group_id NOT IN (19, 10)
  466. AND enabled = 1
  467. OR (
  468. department_name = '产品技术部'
  469. AND enabled = 1
  470. )`
  471. _, err = o.Raw(sql).QueryRows(&items)
  472. return
  473. }
  474. // GetFiccEnterScoreAdmin 获取Ficc需要录分的研究员
  475. func GetFiccEnterScoreAdmin() (items []*AdminItem, err error) {
  476. o := orm.NewOrm()
  477. sql := `SELECT * FROM admin_cygx WHERE group_name IN('宏观组','建材组','有色组','能化组','海外组') AND enabled = 1 `
  478. _, err = o.Raw(sql).QueryRows(&items)
  479. return
  480. }
  481. // SyncRoleData 同步角色数据
  482. type SyncRoleData struct {
  483. Source int `description:"来源: 1-CRM; 2-ETA"`
  484. RoleId int `description:"角色ID"`
  485. }
  486. // SyncAdminData 同步用户数据
  487. type SyncAdminData struct {
  488. Source int `description:"来源: 1-CRM; 2-ETA"`
  489. AdminName string `description:"用户名"`
  490. }
  491. // SyncDepartmentData 同步部门数据
  492. type SyncDepartmentData struct {
  493. Source int `description:"来源: 1-CRM; 2-ETA"`
  494. DepartmentId int `description:"部门ID"`
  495. }
  496. // SyncGroupData 同步分组数据
  497. type SyncGroupData struct {
  498. Source int `description:"来源: 1-CRM; 2-ETA"`
  499. GroupId int `description:"分组ID"`
  500. }