Admitted I cited Western observations regarding the war casualties in this case as they give a balanced perception of the losses suffered by both sides. In China's case. 30,000 killed against 37,000 Vietnamese losses is not a bad score. And if we are to take China's claim of 8,000 killed. Then we will also have to take Vietnam's claim of suffering around 10,000 killed themselves in to consideration as well. Which considering that the war lasted just 27 days is a pretty impressive toll for both.You last sentence is exactly where you are mistaken.
The Sino-Vietnam War of 1979 paved the way for lasting peace between China and Vietnam.
Your view of this war is heavily influenced by the Western narrative, which seeks to downplay Chinese gains and exaggerate Chinese losses.
The truth is Chinese losses were ~8000 deaths and ~20,000 wounded, far below the 30,000 deaths and 40,000 wounded given by Western estimates.
Furthermore, Cambodia was only one issue leading to the war. The other issue was Vietnamese harassment of Chinese borders, including the commission of war crimes such as massacring a hospital.
If you want to understand this war, you need to look at the regional political situation at the end of the US-Vietnam war. The North Vietnamese were freshly victorious from their civil war, having defeated the world's number 1 superpower, and flush with battle-hardened veterans and modern soviet equipment. They quickly began to cast their ambitions on the surrounding countries.
The invasion of Cambodia was the catalyst that prompted China to take action. China had already been engaged in skirmishes with Vietnam along their borders, and the invasion of Cambodia was proof that Vietnam was beginning a campaign of expansion. Therefore, Deng made the decision to conduct a punitive strike against Vietnam in order to nip their ambitions in the bud.
There's a lot of misinformation in western media about the Chinese performance in this war. While it is true that lack of training (mostly green recruits) and logistic difficulties resulted in heavy casualties for the PLA (again, nowhere near Western propaganda), the Vietnamese forces were crushed. All of the PLA fronts achieved their objectives on schedule despite heavy resistance. The Vietnamese like to claim that they didn't use their regular forces against China, but the truth is they knew their regular forces didn't stand a chance either, and was throwing their militia into the meat grinder in the hope of delaying PLA advance long enough to prompt a Soviet intervention.
The Soviet intervention, of course, never came. The PLA had stationed their most elite units on the Soviet border to deter any notion of military intervention from the USSR. Shortly after capturing Lang Son, China announced a unilateral ceasefire and began withdrawing.
Once again, the Western narrative completely fails to understand the Chinese intentions. From the very outset of the war, China had announced that it was not interested in a single inch of Vietnamese territory. The capture of Lang Son was highly strategic, as the difficult terrain so far had prevented Chinese armor from being effective. With the capture of Lang Son, however, Chinese armor now had an open road to Hanoi, and Chinese commanders knew this sent an effective message to their Vietnamese counterparts. This is a combat philosophy known as "点到即止", meaning you stop your attack when your opponent's defense fails. This is used when you do not want to humiliate your opponent.
China is not interested in conquering Vietnam, it just wants a peaceful border to build their economy. The war of 1979 follows that objective. Of course, real life being rather messy, it took Vietnam another few years of heavy skirmishes along the border to finally accept the fact that it cannot challenge China. When this acceptance finally came, however, the peace that resulted has been long lasting.
It was never a matter of luck.
And we can only speculate on what both sides think or knew of the situation because in the end the war was concluded without a decisive victory on both sides. Invading Hanoi will very likely result in a South East Asian Battle of Stalingrad.
And in the end Vietnam left Cambodia entirely on its' own accord and terms. The war in no way made any significant changes to Vietnam's foreign policy.