<?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.MesBaseProcessInfoMapper">

    <resultMap type="MesBaseProcessInfo" id="MesBaseProcessInfoResult">
        <result property="processId" column="process_id"/>
        <result property="processCode" column="process_code"/>
        <result property="processName" column="process_name"/>
        <result property="processType" column="process_type"/>
        <result property="productionTime" column="production_time"/>
        <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"/>
    </resultMap>

    <resultMap id="MesBaseProcessInfoMesBaseProcessProdlineResult" type="MesBaseProcessInfo"
               extends="MesBaseProcessInfoResult">
        <collection property="mesBaseProcessProdlineList" notNullColumn="sub_process_id" javaType="java.util.List"
                    resultMap="MesBaseProcessProdlineResult"/>
    </resultMap>

    <resultMap type="MesBaseProcessProdline" id="MesBaseProcessProdlineResult">
        <result property="processId" column="sub_process_id"/>
        <result property="prodlineId" column="sub_prodline_id"/>
        <result property="createBy" column="sub_create_by"/>
        <result property="createTime" column="sub_create_time"/>
    </resultMap>

    <resultMap id="MesBaseProcessInfoMesBaseProcessUserResult" type="MesBaseProcessInfo"
               extends="MesBaseProcessInfoResult">
        <collection property="mesBaseProcessUserList" notNullColumn="sub_process_id" javaType="java.util.List"
                    resultMap="MesBaseProcessUserResult"/>
    </resultMap>

    <resultMap type="MesBaseProcessUser" id="MesBaseProcessUserResult">
        <result property="processId" column="sub_process_id"/>
        <result property="userId" column="sub_user_id"/>
        <result property="userName" column="sub_user_name"/>
        <result property="createBy" column="sub_create_by"/>
        <result property="createTime" column="sub_create_time"/>
    </resultMap>

    <sql id="selectMesBaseProcessInfoVo">
        select process_id,
               process_code,
               process_name,
               process_type,
               production_time,
               active_flag,
               remark,
               create_by,
               create_time,
               update_by,
               update_time
        from mes_base_process_info
    </sql>

    <select id="selectMesBaseProcessInfoList" parameterType="MesBaseProcessInfo" resultMap="MesBaseProcessInfoResult">
        <include refid="selectMesBaseProcessInfoVo"/>
        <where>
            <if test="processCode != null  and processCode != ''">and process_code = #{processCode}</if>
            <if test="processName != null  and processName != ''">and process_name like concat('%', #{processName},
                '%')
            </if>
            <if test="processType != null  and processType != ''">and process_type = #{processType}</if>
            <if test="productionTime != null ">and production_time = #{productionTime}</if>
            <if test="activeFlag != null  and activeFlag != ''">and active_flag = #{activeFlag}</if>
            <if test="remark != null  and remark != ''">and remark = #{remark}</if>
        </where>
    </select>

    <select id="selectMesBaseProcessInfoByProcessId" parameterType="Long"
            resultMap="MesBaseProcessInfoMesBaseProcessProdlineResult">
        select a.process_id,
               a.process_code,
               a.process_name,
               a.process_type,
               a.production_time,
               a.active_flag,
               a.remark,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               b.process_id  as sub_process_id,
               b.prodline_id as sub_prodline_id,
               b.create_by   as sub_create_by,
               b.create_time as sub_create_time
        from mes_base_process_info a
                 left join mes_base_process_prodline b on b.process_id = a.process_id
        where a.process_id = #{processId}
    </select>

    <select id="selectMesBaseProcessInfoUserByProcessId" parameterType="Long"
            resultMap="MesBaseProcessInfoMesBaseProcessUserResult">
        select a.process_id,
               a.process_code,
               a.process_name,
               a.process_type,
               a.production_time,
               a.active_flag,
               a.remark,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               b.process_id  as sub_process_id,
               b.user_id     as sub_user_id,
               b.user_name   as sub_user_name,
               b.create_by   as sub_create_by,
               b.create_time as sub_create_time
        from mes_base_process_info a
                 left join mes_base_process_user b on b.process_id = a.process_id
        where a.process_id = #{processId}
    </select>

    <insert id="insertMesBaseProcessInfo" parameterType="MesBaseProcessInfo" useGeneratedKeys="true"
            keyProperty="processId">
        insert into mes_base_process_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="processCode != null">process_code,</if>
            <if test="processName != null and processName != ''">process_name,</if>
            <if test="processType != null and processType != ''">process_type,</if>
            <if test="productionTime != null">production_time,</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="processCode != null">#{processCode},</if>
            <if test="processName != null and processName != ''">#{processName},</if>
            <if test="processType != null and processType != ''">#{processType},</if>
            <if test="productionTime != null">#{productionTime},</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="updateMesBaseProcessInfo" parameterType="MesBaseProcessInfo">
        update mes_base_process_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="processCode != null">process_code = #{processCode},</if>
            <if test="processName != null and processName != ''">process_name = #{processName},</if>
            <if test="processType != null and processType != ''">process_type = #{processType},</if>
            <if test="productionTime != null">production_time = #{productionTime},</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 process_id = #{processId}
    </update>

    <delete id="deleteMesBaseProcessInfoByProcessId" parameterType="Long">
        delete
        from mes_base_process_info
        where process_id = #{processId}
    </delete>

    <delete id="deleteMesBaseProcessInfoByProcessIds" parameterType="String">
        delete from mes_base_process_info where process_id in
        <foreach item="processId" collection="array" open="(" separator="," close=")">
            #{processId}
        </foreach>
    </delete>

    <delete id="deleteMesBaseProcessProdlineByProcessIds" parameterType="String">
        delete from mes_base_process_prodline where process_id in
        <foreach item="processId" collection="array" open="(" separator="," close=")">
            #{processId}
        </foreach>
    </delete>

    <delete id="deleteMesBaseProcessProdlineByProcessId" parameterType="Long">
        delete
        from mes_base_process_prodline
        where process_id = #{processId}
    </delete>

    <insert id="batchMesBaseProcessProdline">
        insert into mes_base_process_prodline( process_id, prodline_id, create_by, create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.processId}, #{item.prodlineId}, #{item.createBy}, #{item.createTime})
        </foreach>
    </insert>

    <delete id="deleteMesBaseProcessUserByProcessIds" parameterType="String">
        delete from mes_base_process_user where process_id in
        <foreach item="processId" collection="array" open="(" separator="," close=")">
            #{processId}
        </foreach>
    </delete>

    <delete id="deleteMesBaseProcessUserByProcessId" parameterType="Long">
        delete from mes_base_process_user where process_id = #{processId}
    </delete>

    <insert id="batchMesBaseProcessUser">
        insert into mes_base_process_user( process_id, user_id, user_name, create_by, create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.processId}, #{item.userId}, #{item.userName}, #{item.createBy}, #{item.createTime})
        </foreach>
    </insert>

</mapper>