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 //权限 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 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.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 { 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) 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, }) 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 := 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) UpdateUserName(entId, userId int, user_name string) { //entniche_user表,用户名字段 user_name //base_user表,用户名字段 user_name //mog库user表,用户名字段 user_name if user_name != "" && len(user_name) > 0 { user := u.GetById(entId, userId) Mysql.UpdateOrDeleteBySql(`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 := 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_name, }) } } } } // 修改用户信息包括用户名和手机号 20210429海康威视需求新增的 func (u *User) UpdateInfo(mail string, phone string, name string, userId, deptId, entId int, entPhone string, entName, user_name string, 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=? where id=? and ent_id=?`, mail, phone, name, NowFormat(Date_Full_Layout), user_name, 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(entId, userId, user_name) 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) u.UpdateUserName(entId, userId, user_name) 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=? where id=? and ent_id=?`, mail, phone, name, 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)) u.UpdateUserName(entId, userId, user_name) 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...) }