package service import ( "database/sql" "time" "app.yhyue.com/moapp/jybase/common" "bp.jydev.jianyu360.cn/BaseService/userCenter/entity" . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter" ) func UserAdd(this *UserAddReq) *UserAddResp { userId := int64(0) ok := entity.BaseMysql.ExecTx("新增用户相关表字段", func(tx *sql.Tx) bool { //自然人 personId := entity.BaseMysql.InsertByTx(tx, entity.BasePerson, map[string]interface{}{ "name": this.Nickname, "headimg": this.Headimg, "idcard": this.IdCard, "address": this.Address, "sex": this.Sex, "contact": this.Phone, "create_time": time.Now().Format("2006-01-02 15:04:05"), "update_time": time.Now().Format("2006-01-02 15:04:05"), }) //账户表 accountId := entity.BaseMysql.InsertByTx(tx, entity.BaseAccount, map[string]interface{}{ "person_id": personId, "type": 0, }) //用户表 baseUserId := entity.BaseMysql.InsertByTx(tx, entity.UserTable, map[string]interface{}{ "appid": this.Appid, "person_id": personId, "phone": this.Phone, "password": this.Password, "s_openid": this.SOpenid, "a_openid": this.AOpenid, "unionid": this.Unionid, "create_time": time.Now().Format("2006-01-02 15:04:05"), "update_time": time.Now().Format("2006-01-02 15:04:05"), }) //职位表 positionId := entity.BaseMysql.InsertByTx(tx, entity.BasePosition, map[string]interface{}{ "type": 0, "account_id": accountId, "user_id": baseUserId, "person_name": this.Nickname, }) userId = baseUserId return personId > 0 && accountId > 0 && baseUserId > 0 && positionId > 0 }) status, msg := 0, "" if userId > 0 && ok { status = 1 } else { msg = "新增用户失败" } return &UserAddResp{ ErrorCode: entity.SuccessCode, ErrorMsg: msg, Data: &UserAdds{Status: int64(status), Id: userId}, } } func UserUpdate(this *UserIdReq) *ExamineResp { ok := UserUpdates(this) status, msg := 0, "" if ok { status = 1 } else { msg = "更新用户失败" } return &ExamineResp{ ErrorCode: entity.SuccessCode, ErrorMsg: msg, Data: &ExamineData{Status: int64(status)}, } } func UserDel(this *UserIdReq) *ExamineResp { ok := UserDels(this) status, msg := 0, "" if ok { status = 1 } else { msg = "删除用户失败" } return &ExamineResp{ ErrorCode: entity.SuccessCode, ErrorMsg: msg, Data: &ExamineData{Status: int64(status)}, } } func UserUpdates(this *UserIdReq) bool { ok := false flag := entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool { set := map[string]interface{}{} if this.Phone != "" { set["phone"] = this.Phone } else { set["phone"] = "" } if this.Nickname != "" { set["nickname"] = this.Nickname } else { set["nickname"] = "" } if this.Headimg != "" { set["headimg"] = this.Headimg } else { set["headimg"] = "" } if this.Company != "" { set["company"] = this.Company } else { set["company"] = "" } if this.Position != "" { set["position"] = this.Position } else { set["position"] = "" } if this.Password != "" { set["password"] = this.Password } else { set["password"] = "" } if this.AOpenid != "" { set["a_openid"] = this.AOpenid } else { set["a_openid"] = "" } if this.SOpenid != "" { set["s_openid"] = this.SOpenid } else { set["s_openid"] = "" } if this.Unionid != "" { set["unionid"] = this.Unionid } else { set["unionid"] = "" } //base_user ok1 := entity.BaseMysql.UpdateByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id}, set) //查询出personid ok2 := true ok3 := true rdata := entity.BaseMysql.SelectBySqlByTx(tx, `select person_id from base_user where id =?`, this.Id) if rdata != nil && len(*rdata) > 0 { person_id := common.Int64All((*rdata)[0]["person_id"]) ok2 = entity.BaseMysql.UpdateByTx(tx, entity.BasePerson, map[string]interface{}{ "id": person_id, }, map[string]interface{}{ "contact": this.Phone, "name": this.Nickname, "headimg": this.Headimg, }) //获取accountid mdata := entity.BaseMysql.SelectBySqlByTx(tx, `select id from base_account where person_id =?`, person_id) if mdata != nil && len(*mdata) > 0 { accountId := common.Int64All((*mdata)[0]["id"]) ok3 = entity.BaseMysql.UpdateByTx(tx, entity.BasePosition, map[string]interface{}{ "user_id": this.Id, "account_id": accountId, }, map[string]interface{}{ "person_name": this.Nickname, }) } } return ok1 && ok2 && ok3 }) if flag { ok = true } return ok } func UserDels(this *UserIdReq) bool { return entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool { _, e1 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_user_backup select * from base_user where id=?`, this.Id) _, e2 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_position_backup select * from base_position where user_id=?`, this.Id) _, e3 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_person_backup select a.* from base_person a inner join base_user b on (b.id=? and a.id=b.person_id)`, this.Id) _, e4 := entity.BaseMysql.ExecBySqlByTx(tx, `insert into base_account_backup select a.* from base_account a inner join base_user b on (b.id=? and a.person_id=b.person_id)`, this.Id) d1 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete a from base_person a inner join base_user b on (b.id=? and a.id=b.person_id)`, this.Id) d2 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete a from base_account a inner join base_user b on (b.id=? and a.person_id=b.person_id)`, this.Id) d3 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete from base_user where id=?`, this.Id) d4 := entity.BaseMysql.UpdateOrDeleteBySqlByTx(tx, `delete from base_position where user_id=?`, this.Id) return e1 == nil && e2 == nil && e3 == nil && e4 == nil && d1 >= 0 && d2 >= 0 && d3 >= 0 && d4 >= 0 }) } // /* int64 error_code = 1; string error_msg = 2; int64 personId=3;//自然人id int64 userAccountId=4;//个人账户id int64 entAccountId =5; //企业账户id int64 entUserAccountId =6;//企业雇员账户id int64 userPositionId =7; // 个人职位id int64 entUserPositionId =8;// 企业雇员职位id string userName=9; //昵称 */ func UserIdentity(this *UserIdentityReq) *UserIdentityResp { resp := &UserIdentityResp{} //个人账户 data := entity.BaseMysql.SelectBySql(`SELECT a.person_id personId,c.id userAccountId ,d.id userPositionId,b.name userName FROM base_user a INNER JOIN base_person b ON a.person_id =b.id INNER JOIN base_account c ON c.person_id = b.id AND c.type=0 INNER JOIN base_position d ON d.account_id = c.id AND d.user_id = a.id AND d.type=0 WHERE a.id =? AND a.appid =?`, this.BaseUserId, this.AppId) if data != nil && len(*data) > 0 { r := (*data)[0] resp.PersonId = common.Int64All(r["personId"]) resp.UserAccountId = common.Int64All(r["userAccountId"]) resp.UserPositionId = common.Int64All(r["userPositionId"]) resp.UserName = common.ObjToString(r["userName"]) } else { resp.ErrorCode = -1 resp.ErrorMsg = "暂无数据" } //企业账户 data2 := entity.BaseMysql.SelectBySql(`select c.id entUserAccountId ,d.id entUserPositionId, e.id entAccountId from base_user a inner join base_person b on a.person_id =b.id inner join base_account c on c.person_id = b.id and c.type=1 inner join base_position d on d.account_id = c.id and d.user_id = a.id and d.type=1 inner join base_account e on e.type=1 and (e.person_id is null or e.person_id =0) where a.id =? and a.appid =? and c.ent_id =?`, this.BaseUserId, this.AppId, this.EntId) if data2 != nil && len(*data2) > 0 { r := (*data2)[0] resp.EntAccountId = common.Int64All(r["entAccountId"]) resp.EntUserAccountId = common.Int64All(r["entUserAccountId"]) resp.EntUserPositionId = common.Int64All(r["entUserPositionId"]) } return resp }