DELETE FROM ec_promotion_fee_stat_daily WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date = #{statDate} INSERT INTO ec_promotion_fee_stat_daily ( company_id, shop_id, product_id, category_id, stat_date, fee_amount ) VALUES (#{r.companyId}, #{r.shopId}, #{r.productId}, #{r.categoryId}, #{r.statDate}, #{r.feeAmount}) INSERT INTO ec_promotion_fee_stat_monthly ( company_id, shop_id, product_id, category_id, stat_month, fee_amount ) SELECT company_id, shop_id, product_id, category_id, #{statMonth}, SUM(fee_amount) FROM ec_promotion_fee_stat_daily WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date >= #{statMonth} AND stat_date < DATE_ADD(#{statMonth}, INTERVAL 1 MONTH) GROUP BY company_id, shop_id, product_id, category_id ON DUPLICATE KEY UPDATE fee_amount = VALUES(fee_amount), update_time = CURRENT_TIMESTAMP INSERT INTO ec_promotion_fee_stat_yearly ( company_id, shop_id, product_id, category_id, stat_year, fee_amount ) SELECT company_id, shop_id, product_id, category_id, #{statYear}, SUM(fee_amount) FROM ec_promotion_fee_stat_monthly WHERE company_id = #{companyId} AND shop_id = #{shopId} AND YEAR(stat_month) = #{statYear} GROUP BY company_id, shop_id, product_id, category_id ON DUPLICATE KEY UPDATE fee_amount = VALUES(fee_amount), update_time = CURRENT_TIMESTAMP DELETE FROM ec_brush_fee_stat_daily WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date = #{statDate} INSERT INTO ec_brush_fee_stat_daily (company_id, shop_id, stat_date, brush_amount) VALUES (#{companyId}, #{shopId}, #{statDate}, #{brushAmount}) ON DUPLICATE KEY UPDATE brush_amount = VALUES(brush_amount), update_time = CURRENT_TIMESTAMP INSERT INTO ec_brush_fee_stat_monthly (company_id, shop_id, stat_month, brush_amount) SELECT company_id, shop_id, #{statMonth}, SUM(brush_amount) FROM ec_brush_fee_stat_daily WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date >= #{statMonth} AND stat_date < DATE_ADD(#{statMonth}, INTERVAL 1 MONTH) GROUP BY company_id, shop_id ON DUPLICATE KEY UPDATE brush_amount = VALUES(brush_amount), update_time = CURRENT_TIMESTAMP INSERT INTO ec_brush_fee_stat_yearly (company_id, shop_id, stat_year, brush_amount) SELECT company_id, shop_id, #{statYear}, SUM(brush_amount) FROM ec_brush_fee_stat_monthly WHERE company_id = #{companyId} AND shop_id = #{shopId} AND YEAR(stat_month) = #{statYear} GROUP BY company_id, shop_id ON DUPLICATE KEY UPDATE brush_amount = VALUES(brush_amount), update_time = CURRENT_TIMESTAMP