In this paper we first consider the expectation of the total discounted claim costs up to the time of ruin, and then, more generally, we study the expectation of the total discounted operating costs up to the time of default, which is the first passage time of a surplus process downcrossing a given level. These two quantities include the expected discounted penalty function at ruin or the Gerber–Shiu function, the expected total discounted dividends up to ruin, and other interesting quantities as special cases among a class of risk processes. As an illustration, we consider a piecewise-deterministic compound Poisson risk model. This model recovers many risk models appearing in the literature such as the compound Poisson risk models with interest, absolute ruin, dividends, multiple thresholds, and their dual models. We derive and solve the integro-differential equation for the expected present value of the total discounted operating costs up to default. The solutions to the expected present value of the total discounted operating costs up to default can be used as a unified approach to solving many ruin-related quantities. As applications, we derive explicit solutions for the expected accumulated utility up to ruin, the absolute ruin probability with varying borrowing rates, the expected total discounted claim costs up to ruin, the Gerber–Shiu function with two-sided jumps, and the price for a perpetual American put option with two-sided jumps.