<?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.ximai.mes.md.mapper.MdWorkstationMapper">

    <resultMap type="MdWorkstation" id="MdWorkstationResult">
        <result property="workstationId" column="workstation_id"/>
        <result property="workstationCode" column="workstation_code"/>
        <result property="workstationName" column="workstation_name"/>
        <result property="workstationType" column="workstation_type"/>
        <result property="processId" column="process_id"/>
        <result property="processName" column="process_name"/>
        <result property="processCode" column="process_code"/>
        <result property="enableFlag" column="enable_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="stdWorkingTime" column="std_working_time"/>
        <result property="usageCode" column="usage_code"/>
        <result property="responsiblePerson" column="responsible_person"/>
        <result property="isBackflush" column="is_backflush"/>
        <result property="standardValueCode" column="standard_value_code"/>
        <result property="processingMethod" column="processing_method"/>
        <result property="costCenterCode" column="cost_center_code"/>
        <result property="watchingAircraftNum" column="watching_aircraft_num"/>
        <result property="factoryCode" column="factory_code"/>
        <result property="workshopId" column="workshop_id"/>
        <result property="workshopName" column="workshop_name"/>
        <result property="transferTime" column="transfer_time"/>
    </resultMap>

    <sql id="selectMdWorkstationVo">
        select mw.workstation_id,
               mw.workstation_code,
               mw.workstation_name,
               mw.workstation_type,
               mw.factory_code,
               mw.usage_code,
               mw.process_id,
               pp.process_name,
               pp.process_code,
               mw.responsible_person,
               su.nick_name,
               mw.std_working_time,
               mw.standard_value_code,
               mw.processing_method,
               mw.cost_center_code,
               mw.is_backflush,
               mw.watching_aircraft_num,
               mw.enable_flag,
               mw.remark,
               mw.create_by,
               mw.create_time,
               mw.update_by,
               mw.update_time,
               mw.workshop_id,
               mw.workshop_code,
               mw.workshop_name,
               mw.transfer_time
        from md_workstation mw
                 left join pro_process pp on mw.process_id = pp.process_id
                 left join md_workshop m on mw.workshop_id = m.workshop_id
                 left join sys_user su on mw.responsible_person = su.user_name
    </sql>

    <select id="selectMdWorkstationList" parameterType="MdWorkstation" resultMap="MdWorkstationResult">
        <include refid="selectMdWorkstationVo"/>
        <where>
            <if test="workstationName != null  and workstationName != ''">and mw.workstation_name like concat('%',
                #{workstationName}, '%')
            </if>
            <if test="processId != null ">and mw.process_id = #{processId}</if>
            <if test="processName != null ">and mw.process_name = #{processName}</if>
            <if test="enableFlag != null  and enableFlag != ''">and mw.enable_flag = #{enableFlag}</if>
            <if test="workshopId != null ">and mw.workshop_id = #{workshopId}</if>
            <if test="factoryCode != null and factoryCode != ''">and mw.factory_code = #{factoryCode}</if>
            <if test="workstationCode != null and workstationCode != ''">and mw.workstation_code = #{workstationCode}
            </if>
        </where>
    </select>

    <select id="selectMdWorkstationByWorkstationId" parameterType="Long" resultMap="MdWorkstationResult">
        <include refid="selectMdWorkstationVo"/>
        where mw.workstation_id = #{workstationId}
    </select>

    <select id="checkWorkStationCodeUnique" parameterType="MdWorkstation" resultMap="MdWorkstationResult">
        <include refid="selectMdWorkstationVo"/>
        where mw.workstation_code = #{workstationCode} limit 1
    </select>

    <select id="checkWorkStationNameUnique" parameterType="MdWorkstation" resultMap="MdWorkstationResult">
        <include refid="selectMdWorkstationVo"/>
        where mw.workstation_name = #{workstationName} limit 1
    </select>


    <insert id="insertMdWorkstation" parameterType="MdWorkstation" useGeneratedKeys="true" keyProperty="workstationId">
        insert into md_workstation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workstationCode != null and workstationCode != ''">workstation_code,</if>
            <if test="workstationName != null and workstationName != ''">workstation_name,</if>
            <if test="workstationType != null and workstationType != ''">workstation_type,</if>
            <if test="processId != null">process_id,</if>
            <if test="enableFlag != null and enableFlag != ''">enable_flag,</if>
            <if test="remark != null and remark != ''">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>
            <if test="stdWorkingTime != null">std_working_time,</if>
            <if test="usageCode != null and usageCode != ''">usage_code,</if>
            <if test="responsiblePerson != null ">responsible_person,</if>
            <if test="isBackflush != null ">is_backflush,</if>
            <if test="standardValueCode != null and standardValueCode != ''">standard_value_code,</if>
            <if test="processingMethod != null and processingMethod != ''">processing_method,</if>
            <if test="costCenterCode != null and costCenterCode != ''">cost_center_code,</if>
            <if test="watchingAircraftNum != null">watching_aircraft_num,</if>
            <if test="factoryCode != null and factoryCode != ''">factory_code,</if>
            <if test="workshopId != null">workshop_id,</if>
            <if test="processName != null">process_name,</if>
            <if test="processCode != null">process_code,</if>
            <if test="workshopName != null">workshop_name,</if>
            <if test="workshopCode != null">workshop_code,</if>
            <if test="transferTime != null">transfer_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workstationCode != null and workstationCode != ''">#{workstationCode},</if>
            <if test="workstationName != null and workstationName != ''">#{workstationName},</if>
            <if test="workstationType != null and workstationType != ''">#{workstationType},</if>
            <if test="processId != null">#{processId},</if>
            <if test="enableFlag != null and enableFlag != ''">#{enableFlag},</if>
            <if test="remark != null and remark != ''">#{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>
            <if test="stdWorkingTime != null">#{stdWorkingTime},</if>
            <if test="usageCode != null and usageCode != ''">#{usageCode},</if>
            <if test="responsiblePerson != null">#{responsiblePerson},</if>
            <if test="isBackflush != null">#{isBackflush},</if>
            <if test="standardValueCode != null and standardValueCode != ''">#{standardValueCode},</if>
            <if test="processingMethod != null and processingMethod != ''">#{processingMethod},</if>
            <if test="costCenterCode != null and costCenterCode != ''">#{costCenterCode},</if>
            <if test="watchingAircraftNum != null">#{watchingAircraftNum},</if>
            <if test="factoryCode != null and factoryCode != ''">#{factoryCode},</if>
            <if test="workshopId != null">#{workshopId},</if>
            <if test="processName != null">#{processName},</if>
            <if test="processCode != null">#{processCode},</if>
            <if test="workshopName != null">#{workshopName},</if>
            <if test="workshopCode != null">#{workshopCode},</if>
            <if test="transferTime != null">#{transferTime},</if>
        </trim>
    </insert>

    <update id="updateMdWorkstation" parameterType="MdWorkstation">
        update md_workstation
        <trim prefix="SET" suffixOverrides=",">
            <if test="workstationCode != null and workstationCode != ''">workstation_code = #{workstationCode},</if>
            <if test="workstationName != null and workstationName != ''">workstation_name = #{workstationName},</if>
            <if test="workstationType != null and workstationType != ''">workstation_type = #{workstationType},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="enableFlag != null and enableFlag != ''">enable_flag = #{enableFlag},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="stdWorkingTime != null">std_working_time = #{stdWorkingTime},</if>
            <if test="usageCode != null and usageCode != ''">usage_code = #{usageCode},</if>
            <if test="responsiblePerson != null">responsible_person = #{responsiblePerson},</if>
            <if test="isBackflush != null">is_backflush = #{isBackflush},</if>
            <if test="standardValueCode != null and standardValueCode != ''">standard_value_code =
                #{standardValueCode},
            </if>
            <if test="processingMethod != null and processingMethod != ''">processing_method = #{processingMethod},</if>
            <if test="costCenterCode != null and costCenterCode != ''">cost_center_code = #{costCenterCode},</if>
            <if test="watchingAircraftNum != null">watching_aircraft_num = #{watchingAircraftNum},</if>
            <if test="factoryCode != null and factoryCode != ''">factory_code = #{factoryCode},</if>
            <if test="workshopId != null">workshop_id = #{workshopId},</if>
            <if test="processName != null">process_name = #{processName},</if>
            <if test="processCode != null">process_code = #{processCode},</if>
            <if test="workshopName != null">workshop_name = #{workshopName},</if>
            <if test="workshopCode != null">workshop_code = #{workshopCode},</if>
            <if test="transferTime != null">transfer_time = #{transferTime},</if>
        </trim>
        where workstation_id = #{workstationId}
    </update>

    <delete id="deleteMdWorkstationByWorkstationId" parameterType="Long">
        delete
        from md_workstation
        where workstation_id = #{workstationId}
    </delete>

    <delete id="deleteMdWorkstationByWorkstationIds" parameterType="String">
        delete from md_workstation where workstation_id in
        <foreach item="workstationId" collection="array" open="(" separator="," close=")">
            #{workstationId}
        </foreach>
    </delete>
</mapper>
