user.go 15 KB

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