user.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package entity
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "net/rpc"
  6. "regexp"
  7. "strings"
  8. "time"
  9. . "app.yhyue.com/moapp/jybase/api"
  10. qutil "app.yhyue.com/moapp/jybase/common"
  11. . "app.yhyue.com/moapp/jybase/date"
  12. . "app.yhyue.com/moapp/jybase/mongodb"
  13. "app.yhyue.com/moapp/jybase/redis"
  14. qrpc "app.yhyue.com/moapp/jybase/rpc"
  15. . "app.yhyue.com/moapp/jypkg/ent/util"
  16. )
  17. var (
  18. VarUser = &User{}
  19. passwordReg = regexp.MustCompile(".{6,}")
  20. )
  21. type User struct {
  22. Id int
  23. Name string //员工姓名
  24. Mail string //邮箱
  25. Phone string //手机号
  26. Dept_id int //部门id
  27. Dept_name string //部门名称
  28. Role string //角色
  29. Power int //权限
  30. User_name string
  31. }
  32. type CompletionUserInfo struct {
  33. IsEnt bool `json:"isEnt"`
  34. PersonName string `json:"personName"`
  35. EntName string `json:"entName"`
  36. Phone string `json:"phone"`
  37. Mail string `json:"mail"`
  38. EntId int `json:"entId"`
  39. }
  40. type Resp struct {
  41. Fool bool `json:"fool"`
  42. EntPositionId int64 `json:"entPositionId"`
  43. UserPositionId int64 `json:"userpositionId"`
  44. }
  45. //根据id获取员工信息
  46. func (u *User) GetById(entId, userId int) *User {
  47. r := Mysql.FindOne(Entniche_user, M{"id": userId, "ent_id": entId}, "*", "")
  48. user, _ := JsonUnmarshal(r, &User{}).(*User)
  49. if user == nil {
  50. return &User{}
  51. }
  52. return user
  53. }
  54. //根据手机号获取员工信息
  55. func (u *User) GetByPhone(phone string) *[]*User {
  56. r := Mysql.SelectBySql(`SELECT * from entniche_user where phone=?`, phone)
  57. users, _ := JsonUnmarshal(r, &[]*User{}).(*[]*User)
  58. if users == nil || len(*users) == 0 {
  59. return &[]*User{}
  60. }
  61. return users
  62. }
  63. //根据id获取带有角色的员工信息
  64. func (u *User) GetAllInfoById(entId, userId int) *User {
  65. r := Mysql.SelectBySql(`SELECT a.id,a.name,a.user_name,a.phone,a.mail,c.name as dept_name,c.id as dept_id,e.name as role from entniche_user a
  66. INNER JOIN entniche_department_user b on (a.id=? and a.ent_id=? and a.id=b.user_id)
  67. INNER JOIN entniche_department c on (b.dept_id=c.id)
  68. LEFT JOIN entniche_user_role d on (a.id=d.user_id)
  69. LEFT JOIN entniche_role e on (d.role_id=e.id)`, userId, entId)
  70. users, _ := JsonUnmarshal(r, &[]*User{}).(*[]*User)
  71. if users == nil || len(*users) == 0 {
  72. return &User{}
  73. }
  74. return (*users)[0]
  75. }
  76. //移动到别的部门,并取消管理员角色
  77. func (u *User) Move(tx *sql.Tx, entId, deptId int, userId string) bool {
  78. if tx == nil {
  79. return false
  80. }
  81. args, ws := GetInForComma(userId)
  82. //取消管理员角色
  83. args_2 := []interface{}{}
  84. args_2 = append(args_2, args...)
  85. args_2 = append(args_2, entId, Role_admin_department, deptId)
  86. args_2 = append(args_2, args...)
  87. ok_2 := Mysql.UpdateOrDeleteBySqlByTx(tx, `delete a from entniche_user_role a
  88. INNER JOIN entniche_user b on (b.id in (`+ws+`) and b.ent_id=? and a.role_id=? and a.user_id=b.id)
  89. where a.user_id not in (SELECT c.user_id from entniche_department_user c where c.dept_id=? and c.user_id in (`+ws+`))`, args_2...)
  90. args_1 := []interface{}{}
  91. args_1 = append(args_1, args...)
  92. args_1 = append(args_1, entId, deptId)
  93. //移动到别的部门
  94. ok_1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_department_user a
  95. INNER JOIN entniche_department b on (a.user_id in (`+ws+`) and b.ent_id=? and a.dept_id=b.id)
  96. set a.dept_id=?`, args_1...)
  97. return ok_1 > -1 && ok_2 > -1
  98. }
  99. //手机号是否存在
  100. func (u *User) AddIsExists(entId int, phone string) bool {
  101. if Mysql.CountBySql("SELECT count(1) from entniche_user where ent_id=? and phone=?", entId, phone) > 0 {
  102. return true
  103. }
  104. return false
  105. }
  106. //员工删除
  107. func (u *User) Del(entId int, ids string) bool {
  108. args, ws := GetInForComma(ids)
  109. args = append(args, Role_admin_system)
  110. del_dept_user := Mysql.SelectBySql(`select a.* from entniche_department_user a left join entniche_user_role b on (a.user_id=b.user_id) where a.user_id in (`+ws+`) and IFNULL(b.role_id,0)<>?`, args...)
  111. args = append(args, entId)
  112. del_user := Mysql.SelectBySql(`select a.* from entniche_user a left join entniche_user_role b on (a.id=b.user_id) where a.id in (`+ws+`) and IFNULL(b.role_id,0)<>? and a.ent_id=?`, args...)
  113. if Mysql.UpdateOrDeleteBySql(`DELETE a from entniche_user a left join entniche_user_role b on (a.id=b.user_id) where a.id in (`+ws+`) and IFNULL(b.role_id,0)<>? and a.ent_id=?`, args...) > 0 {
  114. go func() {
  115. VarRule.DelUserRule(entId, args)
  116. VarBackup.Save(Entniche_department_user, del_dept_user)
  117. VarBackup.Save(Entniche_user, del_user)
  118. }()
  119. return true
  120. }
  121. return false
  122. }
  123. //新增员工
  124. func (u *User) Add(tx *sql.Tx, entId int, user *User, paymentAddress, source, nsq, nsq_Topic string) (int64, bool) {
  125. nowFormat := NowFormat(Date_Full_Layout)
  126. user_id := Mysql.InsertByTx(tx, Entniche_user, map[string]interface{}{
  127. "name": user.Name,
  128. "phone": user.Phone,
  129. "mail": user.Mail,
  130. "ent_id": entId,
  131. "createtime": nowFormat,
  132. "timestamp": nowFormat,
  133. "user_name": user.User_name,
  134. })
  135. dept_user_id := Mysql.InsertByTx(tx, Entniche_department_user, map[string]interface{}{
  136. "dept_id": user.Dept_id,
  137. "user_id": user_id,
  138. })
  139. if user_id <= 0 || dept_user_id < 0 {
  140. return user_id, false
  141. }
  142. //自然人 账号 职位添加
  143. data := CompletionUserInfo{
  144. IsEnt: false,
  145. PersonName: user.Name,
  146. Phone: user.Phone,
  147. EntId: entId,
  148. Mail: user.Mail,
  149. }
  150. respData := Resp{
  151. Fool: false,
  152. }
  153. r, err := rpc.DialHTTP("tcp", paymentAddress)
  154. err = r.Call("JyUser.AddUserInfo", &data, &respData)
  155. if err != nil {
  156. return user_id, false
  157. }
  158. if respData.Fool {
  159. fmt.Println("data:", data)
  160. fmt.Println("respData:", respData)
  161. if source == "report" {
  162. //企业职位id,用户职位id处理
  163. entPostionId := respData.EntPositionId
  164. userPositionId := respData.UserPositionId
  165. Publish(Mgo_Log, nsq, nsq_Topic, map[string]interface{}{
  166. "entPostionId": entPostionId,
  167. "userPositionId": userPositionId,
  168. "entId": entId,
  169. "entUserName": user.Name,
  170. })
  171. }
  172. //entniche_user表,用户名字段 user_name
  173. //base_user表,用户名字段 user_name
  174. //mog库user表,用户名字段 user_name
  175. if user.User_name != "" && len(user.User_name) > 0 {
  176. Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_user set user_name=? where phone=? and ent_id!=?`, user.User_name, user.Phone, entId)
  177. data, _ := MQFW.Find("user", map[string]interface{}{
  178. "i_appid": 2,
  179. "$or": []map[string]interface{}{
  180. {"s_phone": user.Phone},
  181. {"s_m_phone": user.Phone},
  182. },
  183. }, `{"s_phone":-1}`, `{"_id":1,"base_user_id":1}`, false, 0, 1)
  184. if data != nil && len(*data) > 0 {
  185. for _, v := range *data {
  186. userId := v["_id"]
  187. baseUserId := v["base_user_id"]
  188. MQFW.UpdateById("user", userId, map[string]interface{}{
  189. "$set": map[string]interface{}{
  190. "user_name": user.User_name,
  191. },
  192. })
  193. Base.Update("base_user", map[string]interface{}{
  194. "id": baseUserId,
  195. }, map[string]interface{}{
  196. "user_name": user.User_name,
  197. })
  198. }
  199. }
  200. }
  201. }
  202. return user_id, respData.Fool
  203. }
  204. //修改员工
  205. func (u *User) Update(mail string, userId, deptId, entId int) bool {
  206. return Mysql.ExecTx("修改员工", func(tx *sql.Tx) bool {
  207. ok_1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_user set mail=?,timestamp=? where id=? and ent_id=?`, mail, NowFormat(Date_Full_Layout), userId, entId)
  208. MQFW.Update("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": userId}, map[string]interface{}{
  209. "$set": map[string]interface{}{
  210. "o_pushset.s_email": mail,
  211. },
  212. }, true, false)
  213. ok_2 := VarUser.Move(tx, entId, deptId, fmt.Sprint(userId))
  214. return ok_1 == 1 && ok_2
  215. })
  216. }
  217. //重置密码
  218. func (u *User) ResetPasswords(power_virtual_account int, virtual_account_rule, phone, newPassword, appPushServiceRpc string) int {
  219. status := func() int {
  220. if power_virtual_account != 1 || phone == "" || !regexp.MustCompile(virtual_account_rule).MatchString(phone) {
  221. return 2
  222. }
  223. //校验是否是深信服虚拟手机号,不是不允许修改
  224. if !passwordReg.MatchString(newPassword) {
  225. return 3
  226. }
  227. users, ok := MQFW.Find("user", map[string]interface{}{
  228. "i_appid": 2,
  229. "$or": []map[string]interface{}{
  230. map[string]interface{}{"s_phone": phone},
  231. map[string]interface{}{"s_m_phone": phone}},
  232. }, `{"s_phone":-1}`, `{"_id":1,"s_jpushid":1,"s_opushid":1,"s_appponetype":1}`, false, 0, 1)
  233. if ok {
  234. if users == nil || len(*users) == 0 { //用户不存在
  235. //将用户信息添加到用户中
  236. if MQFW.Save("user", map[string]interface{}{
  237. "i_appid": 2,
  238. "s_phone": phone,
  239. "s_password": qutil.GetMd5String(newPassword),
  240. "s_regsource": "entbase",
  241. "o_jy": map[string]interface{}{
  242. "i_apppush": 1,
  243. "i_ratemode": 2,
  244. "l_modifydate": time.Now().Unix(),
  245. },
  246. "l_registedate": time.Now().Unix(),
  247. "l_updatepwdtime": time.Now().Unix(),
  248. }) != "" {
  249. return 1
  250. }
  251. } else {
  252. userId := BsonIdToSId((*users)[0]["_id"])
  253. update := map[string]interface{}{
  254. "$set": map[string]interface{}{
  255. "s_password": qutil.GetMd5String(newPassword),
  256. "l_updatepwdtime": time.Now().Unix(),
  257. },
  258. }
  259. jgPushId := qutil.ObjToString((*users)[0]["s_jpushid"])
  260. if jgPushId != "" {
  261. update["$unset"] = map[string]interface{}{"s_jpushid": "", "s_opushid": ""}
  262. update["$addToSet"] = map[string]interface{}{"a_jpushid": jgPushId}
  263. }
  264. if MQFW.UpdateById("user", userId, update) {
  265. go func() {
  266. //增加标识,pc端需要登录
  267. redis.PutCKV("other", "resetpwd_"+userId, 1)
  268. if jgPushId != "" {
  269. qrpc.AppPush(appPushServiceRpc, map[string]interface{}{
  270. "type": "signOut",
  271. "descript": "密码被重置,请重新登录。",
  272. "jgPushId": jgPushId,
  273. "otherPushId": qutil.ObjToString((*users)[0]["s_opushid"]),
  274. "phoneType": qutil.ObjToString((*users)[0]["s_appponetype"]),
  275. "userId": userId,
  276. })
  277. }
  278. }()
  279. return 1
  280. }
  281. }
  282. }
  283. return 6
  284. }()
  285. //返回
  286. return status
  287. }
  288. // 查询手机号是否已存在该企业 20210429海康威视需求新增的
  289. func (u *User) PhoneExist(phone string, entId int) *[]map[string]interface{} {
  290. return Mysql.SelectBySql("select id from entniche_user where phone=? and ent_id=?", phone, entId)
  291. }
  292. func (u *User) UpdateUserName(entId, userId int, user_name string) {
  293. //entniche_user表,用户名字段 user_name
  294. //base_user表,用户名字段 user_name
  295. //mog库user表,用户名字段 user_name
  296. if user_name != "" && len(user_name) > 0 {
  297. user := u.GetById(entId, userId)
  298. Mysql.UpdateOrDeleteBySql(`update entniche_user set user_name=? where phone=? and ent_id!=?`, user.User_name, user.Phone, entId)
  299. data, _ := MQFW.Find("user", map[string]interface{}{
  300. "i_appid": 2,
  301. "$or": []map[string]interface{}{
  302. {"s_phone": user.Phone},
  303. {"s_m_phone": user.Phone},
  304. },
  305. }, `{"s_phone":-1}`, `{"_id":1,"base_user_id":1}`, false, 0, 1)
  306. if data != nil && len(*data) > 0 {
  307. for _, v := range *data {
  308. userId := v["_id"]
  309. baseUserId := v["base_user_id"]
  310. MQFW.UpdateById("user", userId, map[string]interface{}{
  311. "$set": map[string]interface{}{
  312. "user_name": user.User_name,
  313. },
  314. })
  315. Base.Update("base_user", map[string]interface{}{
  316. "id": baseUserId,
  317. }, map[string]interface{}{
  318. "user_name": user_name,
  319. })
  320. }
  321. }
  322. }
  323. }
  324. // 修改用户信息包括用户名和手机号 20210429海康威视需求新增的
  325. func (u *User) UpdateInfo(mail string, phone string, name string, userId, deptId, entId int, entPhone string, entName, user_name string, isAdmin bool) bool {
  326. if isAdmin {
  327. ok := Mysql.ExecTx("修改员工", func(tx *sql.Tx) bool {
  328. ok_1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_user set mail=?,phone=?,name=?,timestamp=? ,user_name=? where id=? and ent_id=?`, mail, phone, name, NowFormat(Date_Full_Layout), user_name, userId, entId)
  329. MQFW.Update("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": userId}, map[string]interface{}{
  330. "$set": map[string]interface{}{
  331. "o_pushset.s_email": mail,
  332. },
  333. }, true, false)
  334. ok_2 := Mysql.UpdateOrDeleteBySqlByTx(tx, "update entniche_info set admin=?,phone=? where id=?", name, phone, entId)
  335. ok_3 := VarUser.Move(tx, entId, deptId, fmt.Sprint(userId))
  336. u.UpdateUserName(entId, userId, user_name)
  337. return ok_1 != -1 && ok_3 && ok_2 != -1
  338. })
  339. if ok {
  340. f := MJYQYFW.Update("user", map[string]interface{}{"phone": entPhone, "username": entName}, map[string]interface{}{
  341. "$set": map[string]interface{}{
  342. "phone": phone,
  343. },
  344. }, false, false)
  345. u.UpdateUserName(entId, userId, user_name)
  346. return f
  347. } else {
  348. return false
  349. }
  350. } else {
  351. return Mysql.ExecTx("修改员工", func(tx *sql.Tx) bool {
  352. ok_1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_user set mail=?,phone=?,name=?,timestamp=? where id=? and ent_id=?`, mail, phone, name, NowFormat(Date_Full_Layout), userId, entId)
  353. MQFW.Update("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": userId}, map[string]interface{}{
  354. "$set": map[string]interface{}{
  355. "o_pushset.s_email": mail,
  356. },
  357. }, true, false)
  358. ok_2 := VarUser.Move(tx, entId, deptId, fmt.Sprint(userId))
  359. u.UpdateUserName(entId, userId, user_name)
  360. return ok_1 == 1 && ok_2
  361. })
  362. }
  363. }
  364. //是否是我的人员
  365. func (u *User) IsMyPerson(entId int, args ...interface{}) bool {
  366. array := []string{}
  367. for _, v := range args {
  368. array = append(array, strings.Split(fmt.Sprint(v), ",")...)
  369. }
  370. userIds := strings.Join(array, ",")
  371. return Mysql.CountBySql(`select count(1) as count from entniche_user where id in (`+userIds+`) and ent_id=?`, entId) == int64(len(array))
  372. }
  373. // 查询是否有用户的额度不为空 20210429 海康威视需求新增的
  374. func (u *User) AccountLeft(entId int, ids string) *[]map[string]interface{} {
  375. args, ws := GetInForComma(ids)
  376. args = append(args, entId)
  377. return Mysql.SelectBySql(`SELECT a.name,a.id,b.left_num from entniche_user a,user_account b WHERE b.user_id in (`+ws+`) and b.ent_id=? and b.left_num >0 and a.id=b.user_id`, args...)
  378. }