package service import ( "database/sql" "time" "bp.jydev.jianyu360.cn/BaseService/userCenter/entity" . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter" ) func UserAdd(this *UserAddReq) *UserAddResp { userId := entity.BaseMysql.Insert(entity.UserTable, map[string]interface{}{ "appid": this.Appid, "phone": this.Phone, "nickname": this.Nickname, "headimg": this.Headimg, "company": this.Company, "position": this.Position, "password": this.Password, "openid": this.Openid, "unionid": this.Unionid, "create_time": time.Now().Format("2006-01-02 15:04:05"), }) status, msg := 0, "" if userId > 0 { status = 1 } else { msg = "新增用户失败" } return &UserAddResp{ ErrorCode: 1, 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: 1, 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: 1, 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 } if this.Nickname != "" { set["nickname"] = this.Nickname } if this.Headimg != "" { set["headimg"] = this.Headimg } if this.Company != "" { set["company"] = this.Company } if this.Position != "" { set["position"] = this.Position } if this.Password != "" { set["password"] = this.Password } if this.Openid != "" { set["openid"] = this.Openid } if this.Unionid != "" { set["unionid"] = this.Unionid } ok1 := entity.BaseMysql.UpdateByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id}, set) snapshot := entity.BaseMysql.InsertByTx(tx, entity.UserSnapshotTable, map[string]interface{}{ "appid": this.Appid, "user_id": this.Id, "phone": this.Phone, "nickname": this.Nickname, "headimg": this.Headimg, "company": this.Company, "position": this.Position, "password": this.Password, "openid": this.Openid, "unionid": this.Unionid, "create_time": time.Now().Format("2006-01-02 15:04:05"), }) return ok1 && snapshot > 0 }) if flag { ok = true } return ok } func UserDels(this *UserIdReq) bool { ok := false userData := entity.BaseMysql.FindOne(entity.UserTable, map[string]interface{}{"id": this.Id}, "", "") if userData != nil && len(*userData) > 0 { flag := entity.BaseMysql.ExecTx("", func(tx *sql.Tx) bool { thisdata := *userData ok1 := entity.BaseMysql.DeleteByTx(tx, entity.UserTable, map[string]interface{}{"id": this.Id}) snapshot := entity.BaseMysql.InsertByTx(tx, entity.UserSnapshotTable, map[string]interface{}{ "appid": thisdata["appid"], "user_id": this.Id, "phone": thisdata["phone"], "nickname": thisdata["nickname"], "headimg": thisdata["headimg"], "company": thisdata["company"], "position": thisdata["position"], "password": thisdata["password"], "openid": thisdata["openid"], "unionid": thisdata["unionid"], "create_time": time.Now().Format("2006-01-02 15:04:05"), }) return ok1 && snapshot > 0 }) if flag { ok = true } } return ok }