<?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.yunfeiyun.agmp.fms.mapper.SysCustomerMapper">
    
    <resultMap type="SysCustomer" id="SysCustomerResult">
        <result property="customerId"    column="customerId"    />
        <result property="customerRealname"    column="customerRealname"    />
        <result property="customerProvince"    column="customerProvince"    />
        <result property="customerCity"    column="customerCity"    />
        <result property="customerArea"    column="customerArea"    />
        <result property="customerContactaddress"    column="customerContactaddress"    />
        <result property="customerContanctway"    column="customerContanctway"    />
        <result property="customerContancts"    column="customerContancts"    />
        <result property="customerRemark"    column="customerRemark"    />
        <result property="customerDelstatus" column="customerDelstatus"/>
    </resultMap>

    <sql id="selectSysCustomerVo">
        select customerId, customerRealname, customerProvince, customerCity, customerArea, customerContactaddress, customerContanctway, customerContancts, customerRemark from SysCustomer
    </sql>

    <select id="selectSysCustomerList" parameterType="SysCustomer" resultType="SysCustomerResVo">
        select c.customerId, c.customerRealname, c.customerProvince, c.customerCity, c.customerArea, c.customerContactaddress, c.customerContanctway, c.customerContancts, c.customerRemark,
        (select GROUP_CONCAT(resUrl) from SysRes r where r.resBusId = c.customerId) customerPreview,
        (select GROUP_CONCAT(resId) from SysRes r where r.resBusId = c.customerId) resIds
        from SysCustomer c

        <where>  
            <if test="customerRealname != null  and customerRealname != ''"> and customerRealname like concat('%', #{customerRealname}, '%')</if>
            <if test="customerProvince != null  and customerProvince != ''"> and customerProvince = #{customerProvince}</if>
            <if test="customerCity != null  and customerCity != ''"> and customerCity = #{customerCity}</if>
            <if test="customerArea != null  and customerArea != ''"> and customerArea = #{customerArea}</if>
            <if test="customerContactaddress != null  and customerContactaddress != ''"> and customerContactaddress = #{customerContactaddress}</if>
            <if test="customerContanctway != null  and customerContanctway != ''"> and customerContanctway = #{customerContanctway}</if>
            <if test="customerContancts != null  and customerContancts != ''"> and customerContancts = #{customerContancts}</if>
            <if test="customerRemark != null  and customerRemark != ''"> and customerRemark = #{customerRemark}</if>
            <if test="customerDelstatus != null ">and customerDelstatus = #{customerDelstatus}</if>
        </where>
    </select>
    
    <select id="selectSysCustomerByCustomerId" parameterType="String" resultMap="SysCustomerResult">
        <include refid="selectSysCustomerVo"/>
        where customerId = #{customerId}
    </select>
        
    <insert id="insertSysCustomer" parameterType="SysCustomer">
        insert into SysCustomer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerId != null">customerId,</if>
            <if test="customerRealname != null">customerRealname,</if>
            <if test="customerProvince != null">customerProvince,</if>
            <if test="customerCity != null">customerCity,</if>
            <if test="customerArea != null">customerArea,</if>
            <if test="customerContactaddress != null">customerContactaddress,</if>
            <if test="customerContanctway != null">customerContanctway,</if>
            <if test="customerContancts != null">customerContancts,</if>
            <if test="customerRemark != null">customerRemark,</if>
            <if test="customerDelstatus != null"> customerDelstatus,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerId != null">#{customerId},</if>
            <if test="customerRealname != null">#{customerRealname},</if>
            <if test="customerProvince != null">#{customerProvince},</if>
            <if test="customerCity != null">#{customerCity},</if>
            <if test="customerArea != null">#{customerArea},</if>
            <if test="customerContactaddress != null">#{customerContactaddress},</if>
            <if test="customerContanctway != null">#{customerContanctway},</if>
            <if test="customerContancts != null">#{customerContancts},</if>
            <if test="customerRemark != null">#{customerRemark},</if>
            <if test="customerDelstatus != null"> #{customerDelstatus},</if>
         </trim>
    </insert>

    <update id="updateSysCustomer" parameterType="SysCustomer">
        update SysCustomer
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerRealname != null">customerRealname = #{customerRealname},</if>
            <if test="customerProvince != null">customerProvince = #{customerProvince},</if>
            <if test="customerCity != null">customerCity = #{customerCity},</if>
            <if test="customerArea != null">customerArea = #{customerArea},</if>
            <if test="customerContactaddress != null">customerContactaddress = #{customerContactaddress},</if>
            <if test="customerContanctway != null">customerContanctway = #{customerContanctway},</if>
            <if test="customerContancts != null">customerContancts = #{customerContancts},</if>
            <if test="customerRemark != null">customerRemark = #{customerRemark},</if>
            <if test="customerDelstatus != null">customerDelstatus = #{customerDelstatus},</if>
        </trim>
        where customerId = #{customerId}
    </update>

    <delete id="deleteSysCustomerByCustomerId" parameterType="String">
        UPDATE SysCustomer set customerDelstatus = '1' where customerId = #{customerId}
    </delete>

    <delete id="deleteSysCustomerByCustomerIds" parameterType="String">
        UPDATE SysCustomer set customerDelstatus = '1' where customerId in
        <foreach item="customerId" collection="array" open="(" separator="," close=")">
            #{customerId}
        </foreach>
    </delete>
</mapper>