We are applying Newton's second law here, but there is one extra feature: the acceleration of the box comes in as relative motion. So whatever expression we get for the net force, we need to add a to it. I will simply put that into the equation "by hand."
I have a coordinate system with the positive direction upward. So in the Free Body diagram of the mass there is a weight (w) acting downward, a spring force (f) acting upward, and a resistive force (R) from the piston acting downward.
So Newton's second in the vertical direction gives:
(Your sign on mg was wrong. Typo I presume.)