admin.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package system
  2. import (
  3. "eta/eta_forum_hub/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. type LoginReq struct {
  10. Username string `description:"账号"`
  11. Password string `description:"密码"`
  12. IsRemember bool `description:"是否属于受信设备"`
  13. }
  14. type LoginResp struct {
  15. Authorization string
  16. AdminName string `description:"系统用户名称"`
  17. RealName string `description:"系统用户姓名"`
  18. RoleName string `description:"角色名称"`
  19. RoleTypeCode string `description:"角色类型编码"`
  20. SysRoleTypeCode string `description:"角色类型编码"`
  21. AdminId int `description:"系统用户id"`
  22. ProductName string `description:"产品名称:admin,ficc,权益"`
  23. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  24. }
  25. type Admin struct {
  26. AdminId int `orm:"column(admin_id);pk" description:"系统用户id"`
  27. AdminName string `description:"系统用户名称"`
  28. AdminAvatar string `description:"用户头像"`
  29. RealName string `description:"系统用户姓名"`
  30. Password string `json:"-"`
  31. LastUpdatedPasswordTime string `json:"-"`
  32. Enabled int
  33. Email string `description:"系统用户邮箱"`
  34. LastLoginTime string
  35. CreatedTime time.Time
  36. LastUpdatedTime string
  37. Role string `description:"系统用户角色"`
  38. Mobile string `description:"手机号"`
  39. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  40. RoleId int `description:"角色ID"`
  41. RoleName string `description:"角色名称"`
  42. RoleTypeCode string `description:"角色类型编码"`
  43. DepartmentId int `description:"部门id"`
  44. DepartmentName string `description:"部门名称"`
  45. GroupId int `description:"分组id"`
  46. GroupName string `description:"分组名称"`
  47. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,3:超级管理员,4:ficc销售组长"`
  48. Position string `description:"职位"`
  49. DisableTime time.Time `description:"禁用时间"`
  50. ChartPermission int8 `description:"图表权限id"`
  51. OpenId string `description:"弘则部门公众号的openid"`
  52. UnionId string `description:"微信公众平台唯一标识"`
  53. EdbPermission int8 `description:"指标库操作权限,0:只能操作 自己的,1:所有指标可操作"`
  54. MysteelChemicalPermission int8 `description:"钢联化工指标操作权限,0:只能操作 自己的,1:所有指标可操作"`
  55. PredictEdbPermission int8 `description:"预测指标库操作权限,0:只能操作 自己的,1:所有预测指标可操作"`
  56. Province string `description:"省"`
  57. ProvinceCode string `description:"省编码"`
  58. City string `description:"市"`
  59. CityCode string `description:"市编码"`
  60. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  61. TelAreaCode string `description:"手机区号"`
  62. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  63. BusinessCode string `description:"商家编码"`
  64. }
  65. type AddAdminReq struct {
  66. AdminId int `description:"系统用户id"`
  67. AdminName string `description:"系统用户名称"`
  68. AdminAvatar string `description:"用户头像"`
  69. RealName string `description:"系统用户姓名"`
  70. Password string `json:"-"`
  71. LastUpdatedPasswordTime string `json:"-"`
  72. Enabled int
  73. Email string `description:"系统用户邮箱"`
  74. LastLoginTime string
  75. CreatedTime string
  76. LastUpdatedTime string
  77. Role string `description:"系统用户角色"`
  78. Mobile string `description:"手机号"`
  79. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  80. RoleId int `description:"角色ID"`
  81. RoleName string `description:"角色名称"`
  82. RoleTypeCode string `description:"角色类型编码"`
  83. DepartmentId int `description:"部门id"`
  84. DepartmentName string `description:"部门名称"`
  85. GroupId int `description:"分组id"`
  86. GroupName string `description:"分组名称"`
  87. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,3:超级管理员,4:ficc销售组长"`
  88. Position string `description:"职位"`
  89. DisableTime string `description:"禁用时间"`
  90. ChartPermission int8 `description:"图表权限id"`
  91. OpenId string `description:"弘则部门公众号的openid"`
  92. UnionId string `description:"微信公众平台唯一标识"`
  93. EdbPermission int8 `description:"指标库操作权限,0:只能操作 自己的,1:所有指标可操作"`
  94. MysteelChemicalPermission int8 `description:"钢联化工指标操作权限,0:只能操作 自己的,1:所有指标可操作"`
  95. PredictEdbPermission int8 `description:"预测指标库操作权限,0:只能操作 自己的,1:所有预测指标可操作"`
  96. Province string `description:"省"`
  97. ProvinceCode string `description:"省编码"`
  98. City string `description:"市"`
  99. CityCode string `description:"市编码"`
  100. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  101. TelAreaCode string `description:"手机区号"`
  102. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  103. BusinessCode string `description:"商家编码"`
  104. }
  105. // Update 更新用户基础信息
  106. func (item *Admin) Update(cols []string) (err error) {
  107. o := orm.NewOrm()
  108. _, err = o.Update(item, cols...)
  109. return
  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 CheckSysUser(userName, password string) (item *Admin, err error) {
  118. sql := ` SELECT a.*,b.role_type_code FROM admin AS a
  119. INNER JOIN sys_role AS b ON a.role_id=b.role_id WHERE a.admin_name=? AND a.password=? LIMIT 1`
  120. o := orm.NewOrm()
  121. err = o.Raw(sql, userName, password).QueryRow(&item)
  122. return
  123. }
  124. func GetSysUserById(sysUserId int) (item *Admin, err error) {
  125. sql := `SELECT * FROM admin WHERE admin_id=? LIMIT 1`
  126. o := orm.NewOrm()
  127. err = o.Raw(sql, sysUserId).QueryRow(&item)
  128. return
  129. }
  130. func ModifyPwd(sysUserId int, newPwd string) (err error) {
  131. sql := `UPDATE admin SET password=?,last_updated_time=NOW() WHERE admin_id=? `
  132. o := orm.NewOrm()
  133. _, err = o.Raw(sql, newPwd, sysUserId).Exec()
  134. return
  135. }
  136. type ModifyPwdReq struct {
  137. OldPwd string `description:"旧密码"`
  138. NewPwd string `description:"新密码"`
  139. }
  140. // AdminWxUserRelation 管理员-微信用户关联
  141. type AdminWxUserRelation struct {
  142. AdminId int `json:"admin_id"`
  143. AdminName string `json:"admin_name"`
  144. RealName string `json:"real_name"`
  145. UserId int `json:"user_id"`
  146. }
  147. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  148. func GetAdminListByIdList(idList []int) (items []*Admin, err error) {
  149. lenNum := len(idList)
  150. if lenNum <= 0 {
  151. return
  152. }
  153. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) and enabled=1 `
  154. o := orm.NewOrm()
  155. _, err = o.Raw(sql, idList).QueryRows(&items)
  156. return
  157. }
  158. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  159. func GetAdminListByIdListWithoutEnable(idList []int) (items []*Admin, err error) {
  160. lenNum := len(idList)
  161. if lenNum <= 0 {
  162. return
  163. }
  164. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) `
  165. o := orm.NewOrm()
  166. _, err = o.Raw(sql, idList).QueryRows(&items)
  167. return
  168. }
  169. func (item *Admin) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  170. o := orm.NewOrm()
  171. sql := fmt.Sprintf(`SELECT COUNT(1) FROM admin WHERE 1=1 %s`, condition)
  172. err = o.Raw(sql, pars).QueryRow(&count)
  173. return
  174. }
  175. func (item *Admin) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  176. o := orm.NewOrm()
  177. fields := strings.Join(fieldArr, ",")
  178. if len(fieldArr) == 0 {
  179. fields = `*`
  180. }
  181. order := `ORDER BY created_time DESC`
  182. if orderRule != "" {
  183. order = ` ORDER BY ` + orderRule
  184. }
  185. sql := fmt.Sprintf(`SELECT %s FROM admin WHERE 1=1 %s %s`, fields, condition, order)
  186. _, err = o.Raw(sql, pars).QueryRows(&items)
  187. return
  188. }
  189. // GetSysUserByMobile 手机号获取用户
  190. func GetSysUserByMobile(mobile string) (item *Admin, err error) {
  191. o := orm.NewOrm()
  192. sql := `SELECT
  193. a.*, b.role_type_code
  194. FROM
  195. admin AS a
  196. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  197. WHERE
  198. a.mobile = ?
  199. LIMIT 1`
  200. err = o.Raw(sql, mobile).QueryRow(&item)
  201. return
  202. }
  203. // GetSysUserByEmail 邮箱获取用户
  204. func GetSysUserByEmail(email string) (item *Admin, err error) {
  205. o := orm.NewOrm()
  206. sql := `SELECT
  207. a.*
  208. FROM
  209. admin AS a
  210. WHERE
  211. a.email = ?
  212. LIMIT 1`
  213. err = o.Raw(sql, email).QueryRow(&item)
  214. return
  215. }
  216. // GetAdminByAdminName 账号获取用户
  217. func GetAdminByAdminName(adminName string) (item *Admin, err error) {
  218. o := orm.NewOrm()
  219. sql := `SELECT
  220. *
  221. FROM
  222. admin
  223. WHERE
  224. admin_name = ?
  225. LIMIT 1`
  226. err = o.Raw(sql, adminName).QueryRow(&item)
  227. return
  228. }
  229. // GetAdminByAdminNameAndBusinessCode 账号获取用户
  230. func GetAdminByAdminNameAndBusinessCode(adminName, businessCode string) (item *Admin, err error) {
  231. o := orm.NewOrm()
  232. sql := `SELECT
  233. *
  234. FROM
  235. admin
  236. WHERE
  237. admin_name = ?
  238. and business_code = ?
  239. LIMIT 1`
  240. err = o.Raw(sql, adminName, businessCode).QueryRow(&item)
  241. return
  242. }
  243. func DeleteSysuser(adminId int) (err error) {
  244. sql := `DELETE FROM admin WHERE admin_id=? `
  245. o := orm.NewOrm()
  246. _, err = o.Raw(sql, adminId).Exec()
  247. return
  248. }
  249. type CreateAuthCodeReq struct {
  250. Source int `description:"来源: 1-CRM; 2-ETA"`
  251. AdminName string
  252. }
  253. type CreateAuthCodeResp struct {
  254. AuthCode string `json:"auth_code" description:"登录凭证"`
  255. }
  256. func AddAdmin(item *Admin) (err error) {
  257. o := orm.NewOrm()
  258. adminId, err := o.Insert(item)
  259. item.AdminId = int(adminId)
  260. return
  261. }
  262. func AddOrUpdateAdmin(item *Admin) (err error) {
  263. o := orm.NewOrm()
  264. adminId, err := o.InsertOrUpdate(item)
  265. item.AdminId = int(adminId)
  266. return
  267. }
  268. // GetSysAdminCountByMobile 查询系统中是否存在该手机号(如果有传入用户id,那么排除该用户)
  269. func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
  270. sql := `SELECT COUNT(1) AS count FROM admin WHERE mobile=? `
  271. if adminId > 0 {
  272. sql += ` AND admin_id != ` + fmt.Sprint(adminId)
  273. }
  274. o := orm.NewOrm()
  275. err = o.Raw(sql, mobile).QueryRow(&count)
  276. return
  277. }
  278. type AdminDeleteReq struct {
  279. AdminName string
  280. BusinessCode string `description:"商家编码"`
  281. }