sys_admin.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 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. AdminAvatar string `description:"用户头像"`
  139. RealName string `description:"系统用户姓名"`
  140. Password string `description:"密码"`
  141. Mobile string `description:"手机号"`
  142. RoleId int `description:"角色id"`
  143. DepartmentId int `description:"部门id"`
  144. GroupId int `description:"分组id"`
  145. TeamId int `description:"小组id"`
  146. Enabled int `description:"1:有效,0:禁用"`
  147. //Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人"`
  148. Position string `description:"职位"`
  149. ResearchGroupIds string `description:"研究方向分组IDs"`
  150. Province string `description:"省"`
  151. ProvinceCode string `description:"省编码"`
  152. City string `description:"市"`
  153. CityCode string `description:"市编码"`
  154. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  155. }
  156. // 用户状态编辑
  157. type SysuserEditEnabledReq struct {
  158. AdminId int `description:"系统用户id"`
  159. Enabled int `description:"1:有效,0:禁用"`
  160. }
  161. func EditAdmin(item *Admin) (err error) {
  162. o := orm.NewOrm()
  163. sql := `UPDATE admin
  164. SET
  165. admin_name= ?,
  166. real_name = ?,
  167. password = ?,
  168. last_updated_password_time = NOW(),
  169. email = ?,
  170. last_updated_time = NOW(),
  171. mobile = ?,
  172. role_type = ?,
  173. role_id = ?,
  174. role_name = ?,
  175. department_id = ?,
  176. department_name = ?,
  177. group_id = ?,
  178. group_name = ?,
  179. authority = ?,
  180. position = ?,
  181. role_type_code=?,
  182. enabled=?,
  183. admin_avatar = ?,
  184. province=?,
  185. province_code=?,
  186. city=?,
  187. city_code=?,
  188. employee_id = ?
  189. WHERE admin_id = ? `
  190. _, err = o.Raw(sql, item.AdminName, item.RealName, item.Password, item.Email, item.Mobile, item.RoleType, item.RoleId, item.RoleName, item.DepartmentId, item.DepartmentName,
  191. 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()
  192. return
  193. }
  194. // 变更状态
  195. func EditAdminEnabled(item *Admin) (err error) {
  196. o := orm.NewOrm()
  197. sql := `UPDATE admin
  198. SET
  199. last_updated_time = NOW(),enabled=? WHERE admin_id = ? `
  200. _, err = o.Raw(sql, item.Enabled, item.AdminId).Exec()
  201. return
  202. }
  203. // DisableAdmin 禁用用户
  204. func DisableAdmin(adminId int) (err error) {
  205. o := orm.NewOrm()
  206. sql := `UPDATE admin
  207. SET
  208. last_updated_time = NOW(),disable_time=now(),enabled=0 WHERE admin_id = ? `
  209. _, err = o.Raw(sql, adminId).Exec()
  210. return
  211. }
  212. type SysuserDeleteReq struct {
  213. AdminId int `description:"系统用户id"`
  214. }
  215. func DeleteSysuser(adminId int) (err error) {
  216. sql := `DELETE FROM admin WHERE admin_id=? `
  217. o := orm.NewOrm()
  218. _, err = o.Raw(sql, adminId).Exec()
  219. return
  220. }
  221. // GetSysAdminCountByMobile 查询系统中是否存在该手机号(如果有传入用户id,那么排除该用户)
  222. func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
  223. sql := `SELECT COUNT(1) AS count FROM admin WHERE mobile=? `
  224. if adminId > 0 {
  225. sql += ` AND admin_id != ` + fmt.Sprint(adminId)
  226. }
  227. o := orm.NewOrm()
  228. err = o.Raw(sql, mobile).QueryRow(&count)
  229. return
  230. }
  231. func GetSysUserItems(condition string, pars []interface{}) (items []*AdminItem, err error) {
  232. o := orm.NewOrm()
  233. sql := `SELECT * FROM admin WHERE 1=1 `
  234. if condition != "" {
  235. sql += condition
  236. }
  237. sql += `ORDER BY last_updated_time DESC `
  238. _, err = o.Raw(sql, pars).QueryRows(&items)
  239. return
  240. }
  241. func GetSysUserItemsOrderByCreated(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 created_time DESC `
  248. _, err = o.Raw(sql, pars).QueryRows(&items)
  249. return
  250. }
  251. func GetSysUserItemsList(condition string, pars []interface{}, startSize, pageSize int) (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 limit ?,?`
  258. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  259. return
  260. }
  261. func GetSysUserItemsTotal(condition string, pars []interface{}) (total int64, err error) {
  262. o := orm.NewOrm()
  263. sql := `SELECT count(*) FROM admin WHERE 1=1 `
  264. if condition != "" {
  265. sql += condition
  266. }
  267. err = o.Raw(sql, pars).QueryRow(&total)
  268. return
  269. }
  270. // GetGroupSysUserList 根据分组id获取系统用户列表
  271. func GetGroupSysUserList(groupId int) (items []*AdminItem, err error) {
  272. o := orm.NewOrm()
  273. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC `
  274. _, err = o.Raw(sql, groupId).QueryRows(&items)
  275. return
  276. }
  277. // GetDepartmentGroupSysUserList 根据部门id和分组id获取系统用户列表
  278. func GetDepartmentGroupSysUserList(departmentId, groupId int, roleTypeCodes string) (items []*AdminItem, err error) {
  279. o := orm.NewOrm()
  280. sql := `SELECT * FROM admin WHERE department_id = ? and group_id=? and enabled=1 `
  281. if roleTypeCodes != "" {
  282. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  283. }
  284. sql += ` ORDER BY created_time asc `
  285. _, err = o.Raw(sql, departmentId, groupId).QueryRows(&items)
  286. return
  287. }
  288. // GetGrooupsysUserList 根据大组id和小组id获取系统用户列表
  289. func GetGrooupsysUserList(groupId int, roleTypeCodes string, enabled int) (items []*AdminItem, err error) {
  290. o := orm.NewOrm()
  291. sql := `SELECT * FROM admin WHERE group_id = ? `
  292. if roleTypeCodes != "" {
  293. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  294. }
  295. if enabled >= 0 {
  296. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  297. }
  298. sql += ` ORDER BY created_time asc `
  299. _, err = o.Raw(sql, groupId).QueryRows(&items)
  300. return
  301. }
  302. // GetSysAdminByIdSlice 根据账户id列表获取账户信息列表
  303. func GetSysAdminByIdSlice(adminIdList []string) (items []*Admin, err error) {
  304. if len(adminIdList) <= 0 {
  305. return
  306. }
  307. sql := `SELECT * FROM admin WHERE admin_id in (` + strings.Join(adminIdList, ",") + `) `
  308. o := orm.NewOrm()
  309. _, err = o.Raw(sql).QueryRows(&items)
  310. return
  311. }
  312. // GetTeamSysUserList 根据小组id获取系统用户列表
  313. func GetTeamSysUserList(teamId, enabled int) (items []*AdminItem, err error) {
  314. o := orm.NewOrm()
  315. sql := `SELECT * FROM admin WHERE group_id=? `
  316. if enabled >= 0 {
  317. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  318. }
  319. sql += ` ORDER BY created_time DESC `
  320. _, err = o.Raw(sql, teamId).QueryRows(&items)
  321. return
  322. }
  323. // GetTeamSysUserList 根据小组id获取系统用户列表
  324. func GetTeamSysUserListByIds(ids string) (items []*AdminItem, err error) {
  325. o := orm.NewOrm()
  326. sql := `SELECT * FROM admin WHERE group_id IN (` + ids + `) ORDER BY created_time DESC `
  327. _, err = o.Raw(sql).QueryRows(&items)
  328. return
  329. }
  330. func GetGroupIdByParentId(groupId int) (items []*string, err error) {
  331. o := orm.NewOrm()
  332. sql := `SELECT group_id FROM sys_group WHERE parent_id=? ORDER BY create_time DESC `
  333. _, err = o.Raw(sql, groupId).QueryRows(&items)
  334. return
  335. }
  336. // GetSysUserByParentIdGroupId 查询主管下的所有组员
  337. func GetSysUserByParentIdGroupId(groupId int) (items []*AdminItem, err error) {
  338. o := orm.NewOrm()
  339. 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=?`
  340. _, err = o.Raw(sql, groupId, groupId).QueryRows(&items)
  341. return
  342. }
  343. func GetAdminByGroupId(groupId int) (items []*AdminItem, err error) {
  344. o := orm.NewOrm()
  345. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC`
  346. _, err = o.Raw(sql, groupId).QueryRows(&items)
  347. return
  348. }
  349. // GetSysAdminByIds 根据主键集合获取管理员信息
  350. func GetSysAdminByIds(adminIds string) (items []*Admin, err error) {
  351. if adminIds == "" {
  352. return
  353. }
  354. o := orm.NewOrm()
  355. sql := `SELECT * FROM admin WHERE admin_id IN (` + adminIds + `)`
  356. _, err = o.Raw(sql).QueryRows(&items)
  357. return
  358. }
  359. type OpenIdList struct {
  360. OpenId string
  361. AdminId int
  362. }
  363. // GetAdminOpenIdListByMobile 根据手机号获取用户的openid列表
  364. func GetAdminOpenIdListByMobile(mobile string) (items []*OpenIdList, err error) {
  365. sql := `SELECT admin_id, open_id FROM admin
  366. WHERE open_id != "" and mobile=? `
  367. _, err = orm.NewOrm().Raw(sql, mobile).QueryRows(&items)
  368. return
  369. }
  370. // ResearcherAdminAndUser 研究员admin信息及wx_user信息
  371. type ResearcherAdminAndUser struct {
  372. UserId int `description:"用户ID"`
  373. UserName string `description:"用户名称"`
  374. AdminId int `description:"管理员ID"`
  375. AdminName string `description:"管理员姓名"`
  376. OpenId string `description:"openid"`
  377. }
  378. // GetResearcherAdminAndWxUserByAdminId 通过adminId获取研究员admin及user信息
  379. func GetResearcherAdminAndWxUserByAdminId(adminId int) (item *ResearcherAdminAndUser, err error) {
  380. sql := `SELECT
  381. a.admin_id,
  382. a.real_name AS admin_name,
  383. a.open_id,
  384. b.user_id,
  385. b.real_name AS user_name
  386. FROM
  387. admin AS a
  388. JOIN wx_user AS b ON a.mobile = b.mobile
  389. WHERE
  390. a.admin_id = ? AND a.open_id != ""`
  391. err = orm.NewOrm().Raw(sql, adminId).QueryRow(&item)
  392. return
  393. }
  394. // 获取本组的销售ID
  395. func GetSelleridWhichGroup(companyId, productId int) (adminId string, err error) {
  396. o := orm.NewOrm()
  397. sql := ` SELECT
  398. GROUP_CONCAT( DISTINCT admin_id SEPARATOR ',' ) AS adminId
  399. FROM
  400. admin
  401. WHERE
  402. group_id IN (
  403. SELECT
  404. a.group_id
  405. FROM
  406. company_product AS a
  407. INNER JOIN admin AS b ON a.seller_id = b.admin_id
  408. WHERE
  409. a.company_id = ?
  410. AND a.product_id = ?
  411. )`
  412. err = o.Raw(sql, companyId, productId).QueryRow(&adminId)
  413. return
  414. }