First law of thermodynamics applied to a electrical conduction My book says that for current flowing in a wire
$Ri^2dt = mcdT + hS(T-T_0)dt $
and when equilibrium is reached
$Ri^2dt =  hS(T-T_0)dt $
where R is the resistance of the conductor, i is the current, dt is a time differential, m is the mass of the conductor, c is the specific heat, dT is the differential of temperature, h is the thermal conductivity, S the section of the wire, and external environment temperature.
The books says the electrical work done by the generator provoques an increase in temperature of the wire and heat is given to the external environment 
I am trying to make sense of this equation by using the first law of thermodynamics,$W=Q-\Delta U $ but signs are not coming out well  and I am not sure of how should I do the energy balance or if I should make the external environment part of my system or not.
My try:
If the system is composed of just the wire
The work done by the generator should be - because it enters the system, so
 $W=- Ri^2dt  $
I am not sure if the increase of temperature of the wire should be $\Delta U$ or $Q$, because internal energy is due to temperature but this is also  heat being given to the environment, right? Considering it corrisponds to $\Delta U$ we have  $\Delta U =mcdT $
and so the heat should be given by Newton's conduction law, and since it leaves the system it is negative, so, $Q= - hS(T-T_0)dt $, then 
$W=Q-\Delta U $
yields
$-Ri^2dt=- hS(T-T_0)dt-mcdT $
which would give the right answer, but 
Why can't I consider $Ri^2dt$ to be instead the heat produced by the circuit and released to the environment, how would the energy balance be in that case?
One more thing, why does $ mcdT $ has temperature expressed with a differential, while  $ hS(T-T_0)dt $  does not?
 A: This starts out with the time derivative of the first law equation:
$$\frac{dU}{dt}=\frac{dQ}{dt}-\frac{dW}{dt}$$where $$\frac{dU}{dt}=mC\frac{dT}{dt}\tag{1}$$$$\frac{dQ}{dt}=hS(T_0-T)\tag{2}$$and$$\frac{dW}{dt}=-i^2R\tag{3}$$
With regard to Eqn.2, this does not represent conductive heat transfer.  It represents the rate of convective heat transfer from the surroundings to the wire.  In this equation, S represents the surface area of the wire and h represents the convective heat transfer coefficient.  It says that the rate of heat flow from the surroundings to the wire is proportional to the temperature difference between the surroundings and the wire.
With regard to Eqn.3, $i^2R$ represents the power supplied to the wire by the surroundings (via a potential difference imposed across the wire by an electrical source in the surroundings).  This is the rate at which the surroundings do work on the wire (which comprises the system).
If we combine the previous equations, we obtain:  $$mC\frac{dT}{dt}=hS(T_0-T)+i^2R$$Multiplying this equation by dt gives back your original equation.
