sys_admin.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. Position string `description:"职位"`
  106. ResearchGroupIds string `description:"研究方向分组IDs"`
  107. Province string `description:"省"`
  108. ProvinceCode string `description:"省编码"`
  109. City string `description:"市"`
  110. CityCode string `description:"市编码"`
  111. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  112. Email string `description:"邮箱"`
  113. TelAreaCode string `description:"手机区号"`
  114. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  115. }
  116. func GetSysAdminCount(adminName string) (count int, err error) {
  117. sql := `SELECT COUNT(1) AS count FROM "admin" WHERE admin_name=? `
  118. err = global.DEFAULT_DmSQL.Raw(sql, adminName).Scan(&count).Error
  119. return
  120. }
  121. func GetSysAdminByName(adminName string) (item *Admin, err error) {
  122. sql := `SELECT * FROM "admin" WHERE admin_name=? `
  123. err = global.DEFAULT_DmSQL.Raw(sql, adminName).First(&item).Error
  124. return
  125. }
  126. func GetSysAdminById(adminId int) (item *Admin, err error) {
  127. sql := `SELECT * FROM "admin" WHERE admin_id=? `
  128. err = global.DEFAULT_DmSQL.Raw(sql, adminId).First(&item).Error
  129. return
  130. }
  131. func AddAdmin(item *Admin) (err error) {
  132. err = global.DEFAULT_DmSQL.Create(item).Error
  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. Email string `description:"邮箱"`
  150. TelAreaCode string `description:"手机区号"`
  151. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  152. }
  153. type SysUserMoveReq struct {
  154. AdminId int `description:"系统用户id"`
  155. DepartmentId int `description:"部门id"`
  156. GroupId int `description:"分组id"`
  157. TeamId int `description:"小组id"`
  158. }
  159. type SysUserResetPassReq struct {
  160. AdminId int `description:"系统用户id"`
  161. Password string `description:"密码"`
  162. RePassword string `description:"重复密码"`
  163. }
  164. type SysuserEditEnabledReq struct {
  165. AdminId int `description:"系统用户id"`
  166. Enabled int `description:"1:有效,0:禁用"`
  167. }
  168. func EditAdminEnabled(item *Admin) (err error) {
  169. sql := `UPDATE "admin"
  170. SET
  171. last_updated_time = NOW(),enabled=? WHERE admin_id = ? `
  172. err = global.DEFAULT_DmSQL.Exec(sql, item.Enabled, item.AdminId).Error
  173. return
  174. }
  175. func DisableAdmin(adminId int) (err error) {
  176. sql := `UPDATE "admin"
  177. SET
  178. last_updated_time = NOW(),disable_time=now(),enabled=0 WHERE admin_id = ? `
  179. err = global.DEFAULT_DmSQL.Exec(sql, adminId).Error
  180. return
  181. }
  182. type SysuserDeleteReq struct {
  183. AdminId int `description:"系统用户id"`
  184. }
  185. func DeleteSysuser(adminId int) (err error) {
  186. sql := `DELETE FROM "admin" WHERE admin_id=? `
  187. err = global.DEFAULT_DmSQL.Exec(sql, adminId).Error
  188. return
  189. }
  190. func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
  191. sql := `SELECT COUNT(1) AS count FROM "admin" WHERE mobile=? `
  192. if adminId > 0 {
  193. sql += ` AND admin_id != ` + fmt.Sprint(adminId)
  194. }
  195. err = global.DEFAULT_DmSQL.Raw(sql, mobile).First(&count).Error
  196. return
  197. }
  198. func GetSysUserItems(condition string, pars []interface{}) (items []*AdminItem, err error) {
  199. sql := `SELECT * FROM "admin" WHERE 1=1 `
  200. if condition != "" {
  201. sql += condition
  202. }
  203. sql += `ORDER BY last_updated_time DESC `
  204. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  205. return
  206. }
  207. func GetGroupSysUserList(groupId int) (items []*AdminItem, err error) {
  208. sql := `SELECT * FROM "admin" WHERE group_id=? ORDER BY created_time DESC `
  209. err = global.DEFAULT_DmSQL.Raw(sql, groupId).Find(&items).Error
  210. return
  211. }
  212. func GetDepartmentGroupSysUserList(departmentId, groupId int, roleTypeCodes string) (items []*AdminItem, err error) {
  213. sql := `SELECT * FROM "admin" WHERE department_id = ? and group_id=? and enabled=1 `
  214. if roleTypeCodes != "" {
  215. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  216. }
  217. sql += ` ORDER BY created_time asc `
  218. err = global.DEFAULT_DmSQL.Raw(sql, departmentId, groupId).Find(&items).Error
  219. return
  220. }
  221. func GetGrooupsysUserList(groupId int, roleTypeCodes string, enabled int) (items []*AdminItem, err error) {
  222. sql := `SELECT * FROM "admin" WHERE group_id = ? `
  223. if roleTypeCodes != "" {
  224. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  225. }
  226. if enabled >= 0 {
  227. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  228. }
  229. sql += ` ORDER BY created_time asc `
  230. err = global.DEFAULT_DmSQL.Raw(sql, groupId).Find(&items).Error
  231. return
  232. }
  233. func GetSysAdminByIdSlice(adminIdList []string) (items []*Admin, err error) {
  234. if len(adminIdList) <= 0 {
  235. return
  236. }
  237. sql := `SELECT * FROM "admin" WHERE admin_id in (` + strings.Join(adminIdList, ",") + `) `
  238. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  239. return
  240. }
  241. func GetTeamSysUserList(teamId, enabled int) (items []*AdminItem, err error) {
  242. sql := `SELECT * FROM "admin" WHERE group_id=? `
  243. if enabled >= 0 {
  244. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  245. }
  246. sql += ` ORDER BY created_time DESC `
  247. err = global.DEFAULT_DmSQL.Raw(sql, teamId).Find(&items).Error
  248. return
  249. }
  250. func GetGroupIdByParentId(groupId int) (items []*string, err error) {
  251. sql := `SELECT group_id FROM sys_group WHERE parent_id=? ORDER BY create_time DESC `
  252. err = global.DEFAULT_DmSQL.Raw(sql, groupId).Find(&items).Error
  253. return
  254. }
  255. type OpenIdList struct {
  256. OpenId string
  257. AdminId int
  258. }
  259. func GetRaiAdmin() (items []*AdminItem, err error) {
  260. sql := `SELECT
  261. admin_id,
  262. real_name,
  263. mobile
  264. FROM
  265. "admin"
  266. WHERE
  267. role_type_code LIKE '%rai%'
  268. AND group_id NOT IN (19, 10)
  269. AND enabled = 1
  270. OR (
  271. department_name = '产品技术部'
  272. AND enabled = 1
  273. )`
  274. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  275. return
  276. }
  277. type SyncRoleData struct {
  278. Source int `description:"来源: 1-CRM; 2-ETA"`
  279. RoleId int `description:"角色ID"`
  280. }
  281. type SyncAdminData struct {
  282. Source int `description:"来源: 1-CRM; 2-ETA"`
  283. AdminName string `description:"用户名"`
  284. }
  285. type SyncDepartmentData struct {
  286. Source int `description:"来源: 1-CRM; 2-ETA"`
  287. DepartmentId int `description:"部门ID"`
  288. }
  289. type SyncGroupData struct {
  290. Source int `description:"来源: 1-CRM; 2-ETA"`
  291. GroupId int `description:"分组ID"`
  292. }
  293. func GetSysAdminList(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  294. fields := "*"
  295. if len(fieldArr) > 0 {
  296. fields = strings.Join(fieldArr, ",")
  297. }
  298. order := `enabled DESC, last_updated_time DESC`
  299. if orderRule != "" {
  300. order = orderRule
  301. }
  302. sql := fmt.Sprintf(`SELECT %s FROM "admin" WHERE 1=1 %s ORDER BY %s`, fields, condition, order)
  303. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  304. return
  305. }