<?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.ruoyi.sapAccount.mapper.SapAccountMapper">

    <resultMap type="SapAccount" id="SapAccountResult">
        <result property="id"    column="id"    />
        <result property="userName"    column="user_name"    />
        <result property="nickName"    column="nick_name"    />

        <result property="sapBm"    column="sap_bm"    />
        <result property="sapName"    column="sap_name"    />
        <result property="sapArea"    column="sap_area"    />
    </resultMap>

    <sql id="SapAccountJoins">
        left join sys_user u on u.user_name = a.user_name
    </sql>

    <sql id="selectSapAccountVo">
        select a.id, a.user_name, case when u.status is null then a.user_name+'(登录账号不存在)' when u.status = '1' then u.nick_name+'(停用)' else u.nick_name end nick_name, a.sap_bm, a.sap_name, a.sap_area
        from sap_account a
        <include refid="SapAccountJoins"/>
    </sql>

    <select id="selectSapAccountList" parameterType="SapAccount" resultMap="SapAccountResult">
        <include refid="selectSapAccountVo"/>
        <where>
            <if test="userName != null  and userName != ''"> and a.user_name = #{userName}</if>
            <if test="sapBm != null  and sapBm != ''"> and a.sap_bm = #{sapBm}</if>
            <if test="sapName != null  and sapName != ''"> and a.sap_name like concat('%', #{sapName}, '%')</if>
            <if test="sapArea != null  and sapArea != ''"> and a.sap_area = #{sapArea}</if>
        </where>
    </select>

    <select id="selectSapAccountById" parameterType="Long" resultMap="SapAccountResult">
        <include refid="selectSapAccountVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertSapAccount" parameterType="SapAccount" useGeneratedKeys="true" keyProperty="id">
        insert into sap_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="sapBm != null and sapBm != ''">sap_bm,</if>
            <if test="sapName != null and sapName != ''">sap_name,</if>
            <if test="sapArea != null and sapArea != ''">sap_area,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="sapBm != null and sapBm != ''">#{sapBm},</if>
            <if test="sapName != null and sapName != ''">#{sapName},</if>
            <if test="sapArea != null and sapArea != ''">#{sapArea},</if>
         </trim>
    </insert>

    <update id="updateSapAccount" parameterType="SapAccount">
        update sap_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="sapBm != null and sapBm != ''">sap_bm = #{sapBm},</if>
            <if test="sapName != null and sapName != ''">sap_name = #{sapName},</if>
            <if test="sapArea != null and sapArea != ''">sap_area = #{sapArea},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSapAccountById" parameterType="Long">
        delete from sap_account where id = #{id}
    </delete>

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