<?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.pro.mapper.ProUsageMapper">

    <resultMap type="com.ximai.mes.pro.domain.ProUsage" id="ProUsageResult">
        <result property="usageId" column="usage_id"/>
        <result property="usageCode" column="usage_code"/>
        <result property="usageName" column="usage_name"/>
        <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"/>
    </resultMap>

    <sql id="selectProUsageVo">
        select usage_id,
               usage_code,
               usage_name,
               enable_flag,
               remark,
               create_by,
               create_time,
               update_by,
               update_time
        from pro_usage
    </sql>

    <insert id="insertProUsage" parameterType="ProUsage" useGeneratedKeys="true" keyProperty="usageId">
        insert into pro_usage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="usageCode != null and usageCode != ''">usage_code,</if>
            <if test="usageName != null and usageName != ''">usage_name,</if>
            <if test="enableFlag != null and enableFlag != ''">enable_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="usageCode != null and usageCode != ''">#{usageCode},</if>
            <if test="usageName != null and usageName != ''">#{usageName},</if>
            <if test="enableFlag != null and enableFlag != ''">#{enableFlag},</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="updateProUsage" parameterType="ProUsage" >
        update pro_usage
        <trim prefix="SET" suffixOverrides=",">
            <if test="usageCode != null and usageCode != ''">usage_code = #{usageCode},</if>
            <if test="usageName != null and usageName != ''">usage_name = #{usageName},</if>
            <if test="enableFlag != null and enableFlag != ''">enable_flag = #{enableFlag},</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 usage_id = #{usageId}
    </update>

<!-- 批量根据id删除用途 -->
    <delete id="deleteProUsageByUsageIds" parameterType="String">
        delete from pro_usage where usage_id in
        <foreach item="usageId" collection="array" open="(" separator="," close=")">
            #{usageId}
        </foreach>
    </delete>

    <!-- 根据条件查询用途列表 -->
    <select id="selectProUsageList" parameterType="com.ximai.mes.pro.domain.ProUsage" resultMap="ProUsageResult">
        <include refid="selectProUsageVo"/>
        <where>
            <if test="usageCode != null  and usageCode != ''">and usage_code = #{usageCode}</if>
            <if test="usageName != null  and usageName != ''">and usage_name like concat('%', #{usageName}, '%')</if>
            <if test="enableFlag != null  and enableFlag != ''">and enable_flag = #{enableFlag}</if>
        </where>
        order by create_time desc
    </select>

    <!-- 根据用途id查询用途详细信息 -->
    <select id="selectProUsageByUsageId" parameterType="long" resultMap="ProUsageResult">
        <include refid="selectProUsageVo"/>
        where usage_id = #{usageId}
    </select>

    <!-- 查询用途编码是否唯一 -->
    <select id="checkUsageCodeUnique" parameterType="com.ximai.mes.pro.domain.ProUsage" resultMap="ProUsageResult">
        <include refid="selectProUsageVo"/>
        where usage_code = #{usageCode} limit 1
    </select>

    <!-- 查询用途名称是否唯一 -->
    <select id="checkUsageNameUnique" parameterType="com.ximai.mes.pro.domain.ProUsage" resultMap="ProUsageResult">
        <include refid="selectProUsageVo"/>
        where usage_name = #{usageName} limit 1
    </select>
</mapper>
