Dynamic detuning, encompassing dynamic Lorentz force detuning (LFD) and microphonics, significantly restricts the operational stability of superconducting radio-frequency (SRF) cavity modules. This paper proposes a novel semianalytical method for calculating the dynamic detuning of SRF cavities, reducing computation time by several orders of magnitude compared to transient finite element analysis (fea). Leveraging this computational efficiency, genetic algorithms (GA) are employed to determine the optimal parameters for active compensation. This work provides a high-efficiency simulation framework for dynamic detuning research, with potential for expansion into integrated low-level radio frequency (LLRF) system models. In this study, the coupling between mechanical loads (including ground vibrations, pressure fluctuations, Lorentz forces, and fast tuning) and dynamic detuning is analyzed to develop a modified mechanical model for SRF cavities. A key advancement of this model is the incorporation of a correction term for high-frequency mechanical modes, which enhances simulation accuracy relative to conventional approaches that neglect them. The proposed method was first validated using a 1.3 GHz TESLA cavity, demonstrating excellent agreement with transient fea solvers and published experimental data. Subsequently, the method was applied to evaluate microphonics in the 499.8 MHz cavity of the Hefei Advanced Light Facility (HALF), specifically characterizing the impact of ground vibrations and pressure fluctuations. Regarding active compensation, an optimized half-sinusoidal pulse suppressed the dynamic LFD of the TESLA cavity from 220 Hz to less than 5 Hz during the beam flattop, and from 600 Hz to less than 40 Hz during the entire rf-on region. The global optimum for this three-parameter optimization problem was effectively identified within a broad search space. Furthermore, an optimized PI controller reduced microphonics of the HALF SRF module induced by stochastic ground vibrations from to , thereby improving the open-loop phase and amplitude stability from and to and , respectively.