123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- package entity
- import (
- "database/sql"
- "fmt"
- "net/rpc"
- "regexp"
- "strings"
- "time"
- . "app.yhyue.com/moapp/jybase/api"
- qutil "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/date"
- . "app.yhyue.com/moapp/jybase/mongodb"
- "app.yhyue.com/moapp/jybase/redis"
- qrpc "app.yhyue.com/moapp/jybase/rpc"
- . "app.yhyue.com/moapp/jypkg/ent/util"
- )
- var (
- VarUser = &User{}
- passwordReg = regexp.MustCompile(".{6,}")
- )
- type User struct {
- Id int
- Name string //员工姓名
- Mail string //邮箱
- Phone string //手机号
- Dept_id int //部门id
- Dept_name string //部门名称
- Role string //角色
- Power int //权限
- NicheDis int //商机分配权限
- Role_id int
- User_name string
- }
- type CompletionUserInfo struct {
- IsEnt bool `json:"isEnt"`
- PersonName string `json:"personName"`
- EntName string `json:"entName"`
- Phone string `json:"phone"`
- Mail string `json:"mail"`
- EntId int `json:"entId"`
- }
- type Resp struct {
- Fool bool `json:"fool"`
- EntPositionId int64 `json:"entPositionId"`
- UserPositionId int64 `json:"userpositionId"`
- }
- //根据id获取员工信息
- func (u *User) GetById(entId, userId int) *User {
- r := Mysql.FindOne(Entniche_user, M{"id": userId, "ent_id": entId}, "*", "")
- user, _ := JsonUnmarshal(r, &User{}).(*User)
- if user == nil {
- return &User{}
- }
- return user
- }
- //根据手机号获取员工信息
- func (u *User) GetByPhone(phone string) *[]*User {
- r := Mysql.SelectBySql(`SELECT * from entniche_user where phone=?`, phone)
- users, _ := JsonUnmarshal(r, &[]*User{}).(*[]*User)
- if users == nil || len(*users) == 0 {
- return &[]*User{}
- }
- return users
- }
- //根据id获取带有角色的员工信息
- func (u *User) GetAllInfoById(entId, userId int) *User {
- 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
- INNER JOIN entniche_department_user b on (a.id=? and a.ent_id=? and a.id=b.user_id)
- INNER JOIN entniche_department c on (b.dept_id=c.id)
- LEFT JOIN entniche_user_role d on (a.id=d.user_id)
- LEFT JOIN entniche_role e on (d.role_id=e.id)`, userId, entId)
- users, _ := JsonUnmarshal(r, &[]*User{}).(*[]*User)
- if users == nil || len(*users) == 0 {
- return &User{}
- }
- return (*users)[0]
- }
- //移动到别的部门,并取消管理员角色
- func (u *User) Move(tx *sql.Tx, entId, deptId int, userId string) bool {
- if tx == nil {
- return false
- }
- args, ws := GetInForComma(userId)
- //取消管理员角色
- args_2 := []interface{}{}
- args_2 = append(args_2, args...)
- args_2 = append(args_2, entId, Role_admin_department, deptId)
- args_2 = append(args_2, args...)
- ok_2 := Mysql.UpdateOrDeleteBySqlByTx(tx, `delete a from entniche_user_role a
- INNER JOIN entniche_user b on (b.id in (`+ws+`) and b.ent_id=? and a.role_id=? and a.user_id=b.id)
- 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...)
- args_1 := []interface{}{}
- args_1 = append(args_1, args...)
- args_1 = append(args_1, entId, deptId)
- //移动到别的部门
- ok_1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_department_user a
- INNER JOIN entniche_department b on (a.user_id in (`+ws+`) and b.ent_id=? and a.dept_id=b.id)
- set a.dept_id=?`, args_1...)
- return ok_1 > -1 && ok_2 > -1
- }
- //手机号是否存在
- func (u *User) AddIsExists(entId int, phone string) bool {
- if Mysql.CountBySql("SELECT count(1) from entniche_user where ent_id=? and phone=?", entId, phone) > 0 {
- return true
- }
- return false
- }
- //员工删除
- func (u *User) Del(entId int, ids string) bool {
- args, ws := GetInForComma(ids)
- args = append(args, Role_admin_system)
- 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...)
- args = append(args, entId)
- 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...)
- if Mysql.ExecTx("员工删除", func(tx *sql.Tx) bool {
- ok1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `delete a,b,c from entniche_user a left join entniche_user_role b on (a.id=b.user_id)
- left join entniche_department_user c on (a.id=c.user_id)
- where a.id IN (`+ws+`) AND IFNULL(b.role_id,0)<>? AND a.ent_id=?`, args...) > 0
- return ok1 && u.AfterDel(tx, ids)
- }) {
- go func() {
- VarRule.DelUserRule(entId, args)
- VarBackup.Save(Entniche_department_user, del_dept_user)
- VarBackup.Save(Entniche_user, del_user)
- }()
- return true
- }
- return false
- }
- //处理对应的权益
- func (u *User) AfterDel(tx *sql.Tx, ids string) bool {
- if ids == "" {
- return true
- }
- args1, ws := GetInForComma(ids)
- ok1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `delete from base_service.base_ent_empower where ent_user_id in (`+ws+`)`, args1...)
- //
- args2 := []interface{}{-1, NowFormat(Date_Full_Layout)}
- args2 = append(args2, args1...)
- ok2 := Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_power set status=?,update_time=? where ent_user_id in (`+ws+`)`, args2...)
- return ok1 > -1 && ok2 > -1
- }
- //新增员工
- func (u *User) Add(tx *sql.Tx, entId int, user *User, paymentAddress, source, nsq, nsq_Topic string) (int64, bool) {
- nowFormat := NowFormat(Date_Full_Layout)
- user_id := Mysql.InsertByTx(tx, Entniche_user, map[string]interface{}{
- "name": user.Name,
- "phone": user.Phone,
- "mail": user.Mail,
- "ent_id": entId,
- "createtime": nowFormat,
- "timestamp": nowFormat,
- "user_name": user.User_name,
- "niche_dis": user.NicheDis,
- })
- dept_user_id := Mysql.InsertByTx(tx, Entniche_department_user, map[string]interface{}{
- "dept_id": user.Dept_id,
- "user_id": user_id,
- })
- if user_id <= 0 || dept_user_id < 0 {
- return user_id, false
- }
- //自然人 账号 职位添加
- data := CompletionUserInfo{
- IsEnt: false,
- PersonName: user.Name,
- Phone: user.Phone,
- EntId: entId,
- Mail: user.Mail,
- }
- respData := Resp{
- Fool: false,
- }
- r, err := rpc.DialHTTP("tcp", paymentAddress)
- err = r.Call("JyUser.AddUserInfo", &data, &respData)
- if err != nil {
- return user_id, false
- }
- if respData.Fool {
- fmt.Println("data:", data)
- fmt.Println("respData:", respData)
- if source == "report" {
- //企业职位id,用户职位id处理
- entPostionId := respData.EntPositionId
- userPositionId := respData.UserPositionId
- Publish(Mgo_Log, nsq, nsq_Topic, map[string]interface{}{
- "entPostionId": entPostionId,
- "userPositionId": userPositionId,
- "entId": entId,
- "entUserName": user.Name,
- })
- }
- //entniche_user表,用户名字段 user_name
- //base_user表,用户名字段 user_name
- //mog库user表,用户名字段 user_name
- if user.User_name != "" && len(user.User_name) > 0 {
- Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_user set user_name=? where phone=? and ent_id!=?`, user.User_name, user.Phone, entId)
- data, _ := MQFW.Find("user", map[string]interface{}{
- "i_appid": 2,
- "$or": []map[string]interface{}{
- {"s_phone": user.Phone},
- {"s_m_phone": user.Phone},
- },
- }, `{"s_phone":-1}`, `{"_id":1,"base_user_id":1}`, false, 0, 1)
- if data != nil && len(*data) > 0 {
- for _, v := range *data {
- userId := BsonIdToSId(v["_id"])
- baseUserId := v["base_user_id"]
- MQFW.UpdateById("user", userId, map[string]interface{}{
- "$set": map[string]interface{}{
- "user_name": user.User_name,
- },
- })
- Base.Update("base_user", map[string]interface{}{
- "id": baseUserId,
- }, map[string]interface{}{
- "user_name": user.User_name,
- })
- }
- }
- }
- }
- return user_id, respData.Fool
- }
- //修改员工
- func (u *User) Update(mail string, userId, deptId, entId int) bool {
- return Mysql.ExecTx("修改员工", func(tx *sql.Tx) bool {
- ok_1 := Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_user set mail=?,timestamp=? where id=? and ent_id=?`, mail, NowFormat(Date_Full_Layout), userId, entId)
- MQFW.Update("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": userId}, map[string]interface{}{
- "$set": map[string]interface{}{
- "o_pushset.s_email": mail,
- },
- }, true, false)
- ok_2 := VarUser.Move(tx, entId, deptId, fmt.Sprint(userId))
- return ok_1 == 1 && ok_2
- })
- }
- //重置密码
- func (u *User) ResetPasswords(power_virtual_account int, virtual_account_rule, phone, newPassword, appPushServiceRpc string) int {
- status := func() int {
- if power_virtual_account != 1 || phone == "" || !regexp.MustCompile(virtual_account_rule).MatchString(phone) {
- return 2
- }
- //校验是否是深信服虚拟手机号,不是不允许修改
- if !passwordReg.MatchString(newPassword) {
- return 3
- }
- users, ok := MQFW.Find("user", map[string]interface{}{
- "i_appid": 2,
- "$or": []map[string]interface{}{
- map[string]interface{}{"s_phone": phone},
- map[string]interface{}{"s_m_phone": phone}},
- }, `{"s_phone":-1}`, `{"_id":1,"s_jpushid":1,"s_opushid":1,"s_appponetype":1}`, false, 0, 1)
- if ok {
- if users == nil || len(*users) == 0 { //用户不存在
- //将用户信息添加到用户中
- if MQFW.Save("user", map[string]interface{}{
- "i_appid": 2,
- "s_phone": phone,
- "s_password": qutil.GetMd5String(newPassword),
- "s_regsource": "entbase",
- "o_jy": map[string]interface{}{
- "i_apppush": 1,
- "i_ratemode": 2,
- "l_modifydate": time.Now().Unix(),
- },
- "l_registedate": time.Now().Unix(),
- "l_updatepwdtime": time.Now().Unix(),
- }) != "" {
- return 1
- }
- } else {
- userId := BsonIdToSId((*users)[0]["_id"])
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "s_password": qutil.GetMd5String(newPassword),
- "l_updatepwdtime": time.Now().Unix(),
- },
- }
- jgPushId := qutil.ObjToString((*users)[0]["s_jpushid"])
- if jgPushId != "" {
- update["$unset"] = map[string]interface{}{"s_jpushid": "", "s_opushid": ""}
- update["$addToSet"] = map[string]interface{}{"a_jpushid": jgPushId}
- }
- if MQFW.UpdateById("user", userId, update) {
- go func() {
- //增加标识,pc端需要登录
- redis.PutCKV("other", "resetpwd_"+userId, 1)
- if jgPushId != "" {
- qrpc.AppPush(appPushServiceRpc, map[string]interface{}{
- "type": "signOut",
- "descript": "密码被重置,请重新登录。",
- "jgPushId": jgPushId,
- "otherPushId": qutil.ObjToString((*users)[0]["s_opushid"]),
- "phoneType": qutil.ObjToString((*users)[0]["s_appponetype"]),
- "userId": userId,
- })
- }
- }()
- return 1
- }
- }
- }
- return 6
- }()
- //返回
- return status
- }
- // 查询手机号是否已存在该企业 20210429海康威视需求新增的
- func (u *User) PhoneExist(phone string, entId int) *[]map[string]interface{} {
- return Mysql.SelectBySql("select id from entniche_user where phone=? and ent_id=?", phone, entId)
- }
- func (u *User) UserNameExist(userName, phone string) bool {
- user, ok := MQFW.FindOne("user", map[string]interface{}{
- "i_appid": 2,
- "user_name": userName,
- })
- if ok && user != nil && len(*user) > 0 {
- s_phone := qutil.InterfaceToStr((*user)["s_phone"])
- s_m_phone := qutil.InterfaceToStr((*user)["s_m_phone"])
- if s_phone != "" {
- if s_phone == phone {
- return true
- }
- return false
- }
- if s_m_phone != "" {
- if s_phone == phone {
- return true
- }
- return false
- }
- }
- return true
- }
- func (u *User) UpdateUserName(tx *sql.Tx, entId, userId int, user_name, phone string) {
- //entniche_user表,用户名字段 user_name
- //base_user表,用户名字段 user_name
- //mog库user表,用户名字段 user_name
- if user_name != "" && len(user_name) > 0 {
- Mysql.UpdateOrDeleteBySqlByTx(tx, `update entniche_user set user_name=? where phone=? and ent_id!=?`, user_name, phone, entId)
- data, _ := MQFW.Find("user", map[string]interface{}{
- "i_appid": 2,
- "$or": []map[string]interface{}{
- {"s_phone": phone},
- {"s_m_phone": phone},
- },
- }, `{"s_phone":-1}`, `{"_id":1,"base_user_id":1}`, false, 0, 1)
- if data != nil && len(*data) > 0 {
- for _, v := range *data {
- userId := BsonIdToSId(v["_id"])
- baseUserId := v["base_user_id"]
- MQFW.UpdateById("user", userId, map[string]interface{}{
- "$set": map[string]interface{}{
- "user_name": user_name,
- },
- })
- Base.Update("base_user", map[string]interface{}{
- "id": baseUserId,
- }, map[string]interface{}{
- "user_name": user_name,
- })
- }
- }
- }
- }
- // 修改用户信息包括用户名和手机号 20210429海康威视需求新增的
- 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 {
- if isAdmin {
- ok := Mysql.ExecTx("修改员工", func(tx *sql.Tx) bool {
- 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)
- MQFW.Update("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": userId}, map[string]interface{}{
- "$set": map[string]interface{}{
- "o_pushset.s_email": mail,
- },
- }, true, false)
- ok_2 := Mysql.UpdateOrDeleteBySqlByTx(tx, "update entniche_info set admin=?,phone=? where id=?", name, phone, entId)
- ok_3 := VarUser.Move(tx, entId, deptId, fmt.Sprint(userId))
- u.UpdateUserName(tx, entId, userId, user_name, phone)
- return ok_1 != -1 && ok_3 && ok_2 != -1
- })
- if ok {
- f := MJYQYFW.Update("user", map[string]interface{}{"phone": entPhone, "username": entName}, map[string]interface{}{
- "$set": map[string]interface{}{
- "phone": phone,
- },
- }, false, false)
- return f
- } else {
- return false
- }
- } else {
- return Mysql.ExecTx("修改员工", func(tx *sql.Tx) bool {
- 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)
- MQFW.Update("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": userId}, map[string]interface{}{
- "$set": map[string]interface{}{
- "o_pushset.s_email": mail,
- },
- }, true, false)
- ok_2 := VarUser.Move(tx, entId, deptId, fmt.Sprint(userId))
- u.UpdateUserName(tx, entId, userId, user_name, phone)
- return ok_1 == 1 && ok_2
- })
- }
- }
- //是否是我的人员
- func (u *User) IsMyPerson(entId int, args ...interface{}) bool {
- array := []string{}
- for _, v := range args {
- array = append(array, strings.Split(fmt.Sprint(v), ",")...)
- }
- userIds := strings.Join(array, ",")
- return Mysql.CountBySql(`select count(1) as count from entniche_user where id in (`+userIds+`) and ent_id=?`, entId) == int64(len(array))
- }
- // 查询是否有用户的额度不为空 20210429 海康威视需求新增的
- func (u *User) AccountLeft(entId int, ids string) *[]map[string]interface{} {
- args, ws := GetInForComma(ids)
- args = append(args, entId)
- 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...)
- }
|