<?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.hw.mes.mapper.MesBaseRouteMapper">

    <resultMap type="MesBaseRoute" id="MesBaseRouteResult">
        <result property="routeId" column="route_id"/>
        <result property="routeName" column="route_name"/>
        <result property="routeDesc" column="route_desc"/>
        <result property="activeFlag" column="active_flag"/>
        <result property="remark" column="remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="productionTime" column="production_time"/>
    </resultMap>

    <resultMap id="MesBaseRouteMesBaseRouteProcessResult" type="MesBaseRoute" extends="MesBaseRouteResult">
        <collection property="mesBaseRouteProcessList" notNullColumn="sub_route_process_id" javaType="java.util.List"
                    resultMap="MesBaseRouteProcessResult"/>
    </resultMap>

    <resultMap type="MesBaseRouteProcess" id="MesBaseRouteProcessResult">
        <result property="routeProcessId" column="sub_route_process_id"/>
        <result property="routeId" column="sub_route_id"/>
        <result property="processId" column="sub_process_id"/>
        <result property="processOrder" column="sub_process_order"/>
        <result property="remark" column="sub_remark"/>
        <result property="createBy" column="sub_create_by"/>
        <result property="createTime" column="sub_create_time"/>
        <result property="updateBy" column="sub_update_by"/>
        <result property="updateTime" column="sub_update_time"/>
    </resultMap>

    <sql id="selectMesBaseRouteVo">
        select a.route_id,
               a.route_name,
               a.route_desc,
               a.active_flag,
               a.remark,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               sum(p.production_time) production_time
        from mes_base_route a
                 left join mes_base_route_process b on b.route_id = a.route_id
                 left join mes_base_process_info p on p.process_id = b.process_id
    </sql>

    <select id="selectMesBaseRouteList" parameterType="MesBaseRoute" resultMap="MesBaseRouteResult">
        <include refid="selectMesBaseRouteVo"/>
        <where>
            <if test="routeName != null  and routeName != ''">and a.route_name like concat('%', #{routeName}, '%')</if>
            <if test="routeDesc != null  and routeDesc != ''">and a.route_desc = #{routeDesc}</if>
            <if test="activeFlag != null  and activeFlag != ''">and a.active_flag = #{activeFlag}</if>
            <if test="createBy != null  and createBy != ''">and a.create_by = #{createBy}</if>
            <if test="createTime != null ">and a.create_time = #{createTime}</if>
            <if test="updateBy != null  and updateBy != ''">and a.update_by = #{updateBy}</if>
            <if test="updateTime != null ">and a.update_time = #{updateTime}</if>
        </where>
        group by a.route_id, a.route_name, a.route_desc, a.active_flag, a.remark, a.create_by, a.create_time, a.update_by,
        a.update_time
    </select>

    <select id="selectMesBaseRouteByRouteId" parameterType="Long" resultMap="MesBaseRouteMesBaseRouteProcessResult">
        select a.route_id,
               a.route_name,
               a.route_desc,
               a.active_flag,
               a.remark,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               b.route_process_id as sub_route_process_id,
               b.route_id         as sub_route_id,
               b.process_id       as sub_process_id,
               b.process_order    as sub_process_order,
               b.remark           as sub_remark,
               b.create_by        as sub_create_by,
               b.create_time      as sub_create_time,
               b.update_by        as sub_update_by,
               b.update_time      as sub_update_time
        from mes_base_route a
                 left join mes_base_route_process b on b.route_id = a.route_id
        where a.route_id = #{routeId}
    </select>

    <insert id="insertMesBaseRoute" parameterType="MesBaseRoute" useGeneratedKeys="true" keyProperty="routeId">
        insert into mes_base_route
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="routeName != null and routeName != ''">route_name,</if>
            <if test="routeDesc != null">route_desc,</if>
            <if test="activeFlag != null and activeFlag != ''">active_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="routeName != null and routeName != ''">#{routeName},</if>
            <if test="routeDesc != null">#{routeDesc},</if>
            <if test="activeFlag != null and activeFlag != ''">#{activeFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateMesBaseRoute" parameterType="MesBaseRoute">
        update mes_base_route
        <trim prefix="SET" suffixOverrides=",">
            <if test="routeName != null and routeName != ''">route_name = #{routeName},</if>
            <if test="routeDesc != null">route_desc = #{routeDesc},</if>
            <if test="activeFlag != null and activeFlag != ''">active_flag = #{activeFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where route_id = #{routeId}
    </update>

    <delete id="deleteMesBaseRouteByRouteId" parameterType="Long">
        delete
        from mes_base_route
        where route_id = #{routeId}
    </delete>

    <delete id="deleteMesBaseRouteByRouteIds" parameterType="String">
        delete from mes_base_route where route_id in
        <foreach item="routeId" collection="array" open="(" separator="," close=")">
            #{routeId}
        </foreach>
    </delete>

    <delete id="deleteMesBaseRouteProcessByRouteIds" parameterType="String">
        delete from mes_base_route_process where route_id in
        <foreach item="routeId" collection="array" open="(" separator="," close=")">
            #{routeId}
        </foreach>
    </delete>

    <delete id="deleteMesBaseRouteProcessByRouteId" parameterType="Long">
        delete
        from mes_base_route_process
        where route_id = #{routeId}
    </delete>

    <insert id="batchMesBaseRouteProcess">
        insert into mes_base_route_process( route_process_id, route_id, process_id, process_order, remark, create_by,
        create_time, update_by, update_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.routeProcessId}, #{item.routeId}, #{item.processId}, #{item.processOrder}, #{item.remark},
            #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>

    <select id="getStationByRouteId" parameterType="Long" resultType="java.util.HashMap">
        select brp.process_id      processId,
               bpi.process_name    processName,
               brp.process_order   processOrder,
               bsi.station_id      stationId,
               bsi.station_name    stationName,
               bsi.production_time productionTime,
               mf.multiStationFlag
        from mes_base_route_process brp
                 left join mes_base_process_info bpi on bpi.process_id = brp.process_id
                 left join mes_base_station_info bsi on bsi.process_id = bpi.process_id
                 left join (select brp.process_id, count(*) multiStationFlag
                            from mes_base_route_process brp
                                     left join mes_base_station_info bsi on bsi.process_id = brp.process_id
                            where brp.route_id = #{routeId}
                            group by brp.process_id) mf on mf.process_id = bsi.process_id
        where brp.route_id = #{routeId}
        order by brp.process_order
    </select>
</mapper>