admin.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package system
  2. import (
  3. "eta/eta_forum_admin/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. // Update 更新用户基础信息
  66. func (item *Admin) Update(cols []string) (err error) {
  67. o := orm.NewOrm()
  68. _, err = o.Update(item, cols...)
  69. return
  70. }
  71. func GetSysUserById(sysUserId int) (item *Admin, err error) {
  72. sql := `SELECT * FROM admin WHERE admin_id=? LIMIT 1`
  73. o := orm.NewOrm()
  74. err = o.Raw(sql, sysUserId).QueryRow(&item)
  75. return
  76. }
  77. func ModifyPwd(sysUserId int, newPwd string) (err error) {
  78. sql := `UPDATE admin SET password=?,last_updated_time=NOW() WHERE admin_id=? `
  79. o := orm.NewOrm()
  80. _, err = o.Raw(sql, newPwd, sysUserId).Exec()
  81. return
  82. }
  83. type ModifyPwdReq struct {
  84. OldPwd string `description:"旧密码"`
  85. NewPwd string `description:"新密码"`
  86. }
  87. // AdminWxUserRelation 管理员-微信用户关联
  88. type AdminWxUserRelation struct {
  89. AdminId int `json:"admin_id"`
  90. AdminName string `json:"admin_name"`
  91. RealName string `json:"real_name"`
  92. UserId int `json:"user_id"`
  93. }
  94. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  95. func GetAdminListByIdList(idList []int) (items []*Admin, err error) {
  96. lenNum := len(idList)
  97. if lenNum <= 0 {
  98. return
  99. }
  100. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) and enabled=1 `
  101. o := orm.NewOrm()
  102. _, err = o.Raw(sql, idList).QueryRows(&items)
  103. return
  104. }
  105. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  106. func GetAdminListByIdListWithoutEnable(idList []int) (items []*Admin, err error) {
  107. lenNum := len(idList)
  108. if lenNum <= 0 {
  109. return
  110. }
  111. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) `
  112. o := orm.NewOrm()
  113. _, err = o.Raw(sql, idList).QueryRows(&items)
  114. return
  115. }
  116. func (item *Admin) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  117. o := orm.NewOrm()
  118. sql := fmt.Sprintf(`SELECT COUNT(1) FROM admin WHERE 1=1 %s`, condition)
  119. err = o.Raw(sql, pars).QueryRow(&count)
  120. return
  121. }
  122. func (item *Admin) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  123. o := orm.NewOrm()
  124. fields := strings.Join(fieldArr, ",")
  125. if len(fieldArr) == 0 {
  126. fields = `*`
  127. }
  128. order := `ORDER BY created_time DESC`
  129. if orderRule != "" {
  130. order = ` ORDER BY ` + orderRule
  131. }
  132. sql := fmt.Sprintf(`SELECT %s FROM admin WHERE 1=1 %s %s`, fields, condition, order)
  133. _, err = o.Raw(sql, pars).QueryRows(&items)
  134. return
  135. }
  136. // GetAdminByAdminName 账号获取用户
  137. func GetAdminByAdminName(adminName string) (item *Admin, err error) {
  138. o := orm.NewOrm()
  139. sql := `SELECT
  140. *
  141. FROM
  142. admin
  143. WHERE
  144. admin_name = ?
  145. LIMIT 1`
  146. err = o.Raw(sql, adminName).QueryRow(&item)
  147. return
  148. }
  149. // GetUserUnexpiredSysSession 获取用户未过期的Token
  150. func GetUserUnexpiredSysSession(userName, expiredTime string) (item *SysSession, err error) {
  151. o := orm.NewOrm()
  152. sql := `SELECT
  153. *
  154. FROM
  155. sys_session
  156. WHERE
  157. user_name = ? AND expired_time > ?
  158. ORDER BY expired_time DESC
  159. LIMIT 1`
  160. err = o.Raw(sql, userName, expiredTime).QueryRow(&item)
  161. return
  162. }
  163. type AuthCodeLoginReq struct {
  164. AuthCode string
  165. }
  166. type BusinessAdmin struct {
  167. AdminId string `description:"系统用户id"`
  168. RealName string `description:"用户真实名称"`
  169. ChildrenList []*BusinessAdmin `description:"用户列表"`
  170. }
  171. type BusinessAdminResp struct {
  172. List []*BusinessAdmin `description:"用户列表"`
  173. }
  174. func GetSysAdminById(adminId int) (item *Admin, err error) {
  175. sql := `SELECT * FROM admin WHERE admin_id=? `
  176. o := orm.NewOrm()
  177. err = o.Raw(sql, adminId).QueryRow(&item)
  178. return
  179. }
  180. func GetSysUserItems(condition string, pars []interface{}) (items []*Admin, err error) {
  181. o := orm.NewOrm()
  182. sql := `SELECT * FROM admin WHERE 1=1 `
  183. if condition != "" {
  184. sql += condition
  185. }
  186. sql += `ORDER BY last_updated_time DESC `
  187. _, err = o.Raw(sql, pars).QueryRows(&items)
  188. return
  189. }
  190. type HzEtaBusiness struct {
  191. BusinessName string `description:"商家名称"`
  192. BusinessCode string
  193. }
  194. func GetSysUserItemsOrderByCreated(condition string, pars []interface{}) (items []*Admin, err error) {
  195. o := orm.NewOrm()
  196. sql := `SELECT * FROM admin WHERE 1=1 `
  197. if condition != "" {
  198. sql += condition
  199. }
  200. sql += ` ORDER BY created_time DESC `
  201. _, err = o.Raw(sql, pars).QueryRows(&items)
  202. return
  203. }
  204. // GetTeamSysUserList 根据小组id获取系统用户列表
  205. func GetTeamSysUserList(teamId, enabled int) (items []*Admin, err error) {
  206. o := orm.NewOrm()
  207. sql := `SELECT * FROM admin WHERE group_id=? `
  208. if enabled >= 0 {
  209. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  210. }
  211. sql += ` ORDER BY created_time DESC `
  212. _, err = o.Raw(sql, teamId).QueryRows(&items)
  213. return
  214. }
  215. // GetGrooupsysUserList 根据大组id和小组id获取系统用户列表
  216. func GetGrooupsysUserList(groupId int, roleTypeCodes string, enabled int) (items []*Admin, err error) {
  217. o := orm.NewOrm()
  218. sql := `SELECT * FROM admin WHERE group_id = ? `
  219. if roleTypeCodes != "" {
  220. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  221. }
  222. if enabled >= 0 {
  223. sql += fmt.Sprint(` AND enabled=`, enabled, ` `)
  224. }
  225. sql += ` ORDER BY created_time asc `
  226. _, err = o.Raw(sql, groupId).QueryRows(&items)
  227. return
  228. }
  229. // GetDepartmentGroupSysUserList 根据部门id和分组id获取系统用户列表
  230. func GetDepartmentGroupSysUserList(departmentId, groupId int, roleTypeCodes string) (items []*Admin, err error) {
  231. o := orm.NewOrm()
  232. sql := `SELECT * FROM admin WHERE department_id = ? and group_id=? and enabled=1 `
  233. if roleTypeCodes != "" {
  234. sql += ` and role_type_code in (` + roleTypeCodes + `) `
  235. }
  236. sql += ` ORDER BY created_time asc `
  237. _, err = o.Raw(sql, departmentId, groupId).QueryRows(&items)
  238. return
  239. }
  240. // GetGroupSysUserList 根据分组id获取系统用户列表
  241. func GetGroupSysUserList(groupId int) (items []*Admin, err error) {
  242. o := orm.NewOrm()
  243. sql := `SELECT * FROM admin WHERE group_id=? ORDER BY created_time DESC `
  244. _, err = o.Raw(sql, groupId).QueryRows(&items)
  245. return
  246. }
  247. type DifferentAdmin struct {
  248. AdminId int `description:"系统用户id"`
  249. RealName string `description:"用户真实名称"`
  250. Mobile string `description:"用户手机号"`
  251. CrmAdminId int `description:"crm系统用户id"`
  252. CrmRealName string `description:"crm用户真实名称"`
  253. CrmMobile string `description:"crm用户手机号"`
  254. }
  255. // GetDifferentAdmin 获取系统用户和crm系统用户不同的用户
  256. func GetDifferentAdmin() (items []*DifferentAdmin, err error) {
  257. sql := `SELECT a.admin_id, a.real_name, a.mobile, c.admin_id as crm_admin_id, c.real_name as crm_real_name, c.mobile as crm_mobile FROM admin as a LEFT JOIN admin_crm c on a.admin_name= c.admin_name WHERE a.admin_id != c.admin_id `
  258. o := orm.NewOrm()
  259. _, err = o.Raw(sql).QueryRows(&items)
  260. return
  261. }
  262. // 初始化,把原先crm中的adminId改成社区里的adminId
  263. func SetDifferentAdmin(adminId int, adminRealName string, crmAdminId int, crmAdminRealName string, now time.Time, beforeTime string) (err error) {
  264. // 事务更新
  265. fmt.Println("开始替换 adminId:", adminId, "adminRealName:", adminRealName, "crmAdminId:", crmAdminId, "crmAdminRealName:", crmAdminRealName, "now:", now, "beforeTime:", beforeTime)
  266. o := orm.NewOrm()
  267. to, err := o.Begin()
  268. if err != nil {
  269. return
  270. }
  271. defer func() {
  272. if err != nil {
  273. _ = to.Rollback()
  274. } else {
  275. _ = to.Commit()
  276. }
  277. }()
  278. sql := "UPDATE eta_business SET seller_id = ?, seller_name=?, modify_time=? WHERE seller_id = ? and seller_name = ? and create_time<?"
  279. _, err = to.Raw(sql, adminId, adminRealName, now, crmAdminId, crmAdminRealName, beforeTime).Exec()
  280. if err != nil {
  281. return
  282. }
  283. sql = "UPDATE eta_business_operation_record SET seller_id = ? WHERE seller_id = ? and create_time<?"
  284. _, err = to.Raw(sql, adminId, crmAdminId, beforeTime).Exec()
  285. if err != nil {
  286. return
  287. }
  288. sql = "UPDATE eta_business_operation_record SET sys_user_id = ?, sys_real_name=? WHERE sys_user_id = ? and sys_real_name =? and create_time<?"
  289. _, err = to.Raw(sql, adminId, adminRealName, crmAdminId, crmAdminRealName, beforeTime).Exec()
  290. if err != nil {
  291. return
  292. }
  293. sql = "UPDATE eta_business_remind_record SET seller_id = ?, seller_name=? WHERE seller_id = ? and seller_name = ? and create_time<?"
  294. _, err = to.Raw(sql, adminId, adminRealName, crmAdminId, crmAdminRealName, beforeTime).Exec()
  295. if err != nil {
  296. return
  297. }
  298. sql = "UPDATE eta_training_video_classify SET sys_user_id = ?, sys_real_name=?, modify_time=? WHERE sys_user_id = ? and sys_real_name =? and create_time<? "
  299. _, err = to.Raw(sql, adminId, adminRealName, now, crmAdminId, crmAdminRealName, beforeTime).Exec()
  300. if err != nil {
  301. return
  302. }
  303. sql = "UPDATE eta_training_video_op_log SET sys_user_id = ?, sys_real_name=? WHERE sys_user_id = ? and sys_real_name =? and create_time<?"
  304. _, err = to.Raw(sql, adminId, adminRealName, crmAdminId, crmAdminRealName, beforeTime).Exec()
  305. if err != nil {
  306. return
  307. }
  308. sql = "UPDATE eta_training_video_tag SET sys_user_id = ?, sys_real_name=?, modify_time=? WHERE sys_user_id = ? and sys_real_name =? and create_time<?"
  309. _, err = to.Raw(sql, adminId, adminRealName, now, crmAdminId, crmAdminRealName, beforeTime).Exec()
  310. if err != nil {
  311. return
  312. }
  313. sql = "UPDATE eta_trial SET seller_id = ?, seller=?, modify_time=? WHERE seller_id = ? and seller = ? and create_time<?"
  314. _, err = to.Raw(sql, adminId, adminRealName, now, crmAdminId, crmAdminRealName, beforeTime).Exec()
  315. if err != nil {
  316. return
  317. }
  318. sql = "UPDATE eta_trial_approval SET seller_id = ?, seller=?, modify_time=? WHERE seller_id = ? and seller = ? and create_time<?"
  319. _, err = to.Raw(sql, adminId, adminRealName, now, crmAdminId, crmAdminRealName, beforeTime).Exec()
  320. if err != nil {
  321. return
  322. }
  323. sql = "UPDATE help_doc SET admin_id = ?, admin_real_name=?, modify_time=? WHERE admin_id = ? and admin_real_name = ? and create_time<?"
  324. _, err = to.Raw(sql, adminId, adminRealName, now, crmAdminId, crmAdminRealName, beforeTime).Exec()
  325. if err != nil {
  326. return
  327. }
  328. sql = "UPDATE help_doc_classify SET sys_user_id = ?, sys_user_real_name=?, modify_time=? WHERE sys_user_id = ? and sys_user_real_name =? and create_time<?"
  329. _, err = to.Raw(sql, adminId, adminRealName, now, crmAdminId, crmAdminRealName, beforeTime).Exec()
  330. if err != nil {
  331. return
  332. }
  333. return
  334. }