<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moka.gdtauto.mapper.AdAutoExecuteLogMapper">

    <!-- updateExecuteInfo --> 

    <update id="updateExecuteInfo">
        UPDATE ad_auto_execute_log l
        INNER JOIN (
            SELECT 
                parent_log_id,
                SUM(ad_count) AS ad_count,
                SUM(suspend_count) AS suspend_count,
                COUNT(id) AS log_count,
                SUM(CASE WHEN execute_status = 1 THEN 1 ELSE 0 END) AS success_cnt,
                SUM(CASE WHEN execute_status = 2 THEN 1 ELSE 0 END) AS fail_cnt,
                SUM(CASE WHEN execute_status = 0 THEN 1 ELSE 0 END) AS pending_cnt
            FROM ad_auto_execute_log 
            WHERE parent_log_id = #{id} 
            GROUP BY parent_log_id
        ) t
        SET 
            l.ad_count = t.ad_count,
            l.suspend_count = t.suspend_count,
            l.execute_status = CASE
                WHEN t.pending_cnt &gt; 0 THEN 0                  
                WHEN t.success_cnt = t.log_count THEN 1           
                WHEN t.fail_cnt = t.log_count THEN 2              
                ELSE 3
            END
        WHERE l.id = t.parent_log_id;
    </update>
</mapper>
