<?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="cn.roadm.mapper.YiZhangTongMapper">

    <resultMap id="yiZhangTongChannel" type="cn.roadm.bo.YiZhangTongChannel">
        <result column="id" property="id"/>
        <result column="channel_id" property="channelId"/>
        <result column="create_time" property="createTime"/>
        <result column="status" property="status"/>
    </resultMap>

    <insert id="saveRequestLog">
        insert into yi_zhang_tong_log (taskId, mdn, result, msg, call_time)
        values (#{taskId}, #{mdn}, #{result}, #{msg}, #{callTime})
    </insert>

    <insert id="addTaskRecord">
        insert into yi_zhang_tong_task (task_id, `count`, create_time, state, child_id)
        values (#{taskId}, #{size,javaType=long, jdbcType=INTEGER}, #{createTime}, #{state}, #{childId})
    </insert>

    <select id="queryTaskSuccessCount" resultType="long">
        select count(1) from yi_zhang_tong_log where taskId = #{taskId} and result = 'Success'
    </select>

    <select id="queryTaskFailCount" resultType="long">
        select count(1) from yi_zhang_tong_log where taskId = #{taskId} and result = 'Fail'
    </select>

    <select id="queryExceptionCount" resultType="long">
        select count(1) from yi_zhang_tong_log where taskId = #{taskId} and result = 'Exception'
    </select>

    <update id="updateTaskState">
        update yi_zhang_tong_task set success = #{successCount} , fail = #{failCount}, exception = #{exceptionCount},
        end_time = #{endTime}, state = #{state} where task_id = #{taskId}
    </update>

    <select id="getTaskCount" resultType="long">
        select count from yi_zhang_tong_task where task_id = #{taskId}
    </select>

    <select id="querySaveResult" resultType="cn.roadm.util.yizhangtong.SaveResultDO">
        select taskId, mdn, result, msg, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime, call_time as callTime
        from yi_zhang_tong_log
        <where>
            taskId = #{taskId}
            and id &gt; #{lastId}
        </where>
    </select>

    <select id="getUnSuccessMdn" resultType="cn.roadm.util.yizhangtong.SaveResultDO">
        select id,taskId, mdn, result, msg, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime, call_time as callTime
        from yi_zhang_tong_log
        <where>
            taskId = #{taskId}
            and result != 'Success'
            <if test="lastId != null">
                and id &gt; #{lastId}
            </if>
        </where>
    </select>

    <update id="updateTaskResult">
        update yi_zhang_tong_task set error_file = #{fileName} where task_id = #{taskId}
    </update>

    <select id="listPageYiZhangTongTask" resultType="cn.roadm.bo.YiZhangTongTaskEntity">
        select task_id                                       as taskId,
               `count`,
               success,
               fail,
               `exception`,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime,
               DATE_FORMAT(end_time, '%Y-%m-%d %H:%i:%s')    as endTime,
               error_file                                    as errorFile,
               state,
            child_id as childId
        from yi_zhang_tong_task order by create_time desc
    </select>

    <select id="queryChannelList" resultMap="yiZhangTongChannel">
        select id, channel_id, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as create_time, `status`
        from yi_zhang_tong_channel
    </select>

    <select id="getChannelNameList" resultType="java.lang.String">
        select channel_id from yi_zhang_tong_channel where `status` = 0
    </select>

    <insert id="addChannelName">
        insert into yi_zhang_tong_channel (channel_id, create_time)
        values (#{fullChannelName}, now())
    </insert>

    <update id="updateChannelStatus">
        update yi_zhang_tong_channel set status = #{status} where id = #{id}
    </update>

    <delete id="deleteChannelNameById">
        delete from yi_zhang_tong_channel where id = #{id}
    </delete>

    <select id="getYiZhangTongDataByDay" resultType="cn.roadm.bo.YiZhangTongTotal">
        select DATE_FORMAT(create_time, '%Y-%m-%d') as createTime, sum(IFNULL(`success`, 0)) as successCount,
                sum(IFNULL(`fail`, 0)) as failCount, sum(IFNULL(`exception`, 0)) as exceptionCount,
                child_id as channelId
        from yi_zhang_tong_task group by DATE_FORMAT(create_time, '%Y-%m-%d'), child_id
    </select>
</mapper>