<?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.daqing.financial.crms.dao.CustomerDao">

	<!-- 可根据自己的需求,是否要使用 -->
    <resultMap type="com.daqing.framework.domain.crms.CustomerEntity" id="customerMap">
        <result property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="type" column="type"/>
        <result property="manager" column="manager"/>
        <result property="name" column="name"/>
        <result property="addr" column="addr"/>
        <result property="phone" column="phone"/>
        <result property="password" column="password"/>
        <result property="wechatId" column="wechat_id"/>
        <result property="delOrNot" column="del_or_not"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="motifyTime" column="motify_time"/>
    </resultMap>

    <!-- 查询客户列表(所有)、根据创建时间筛选、根据客户类型筛选、根据客户编号或者名称搜索 -->
    <select id="queryList" parameterType="com.daqing.framework.domain.crms.request.CustomerRequest" resultType="com.daqing.framework.domain.crms.CustomerEntity">
        SELECT id,code,type,name,phone,manager
        FROM crms_customer 
        WHERE del_or_not = 0
        <if test="cr.codeOrName != null and cr.codeOrName != '' ">
            AND (name LIKE CONCAT('%',#{cr.codeOrName},'%') OR code LIKE CONCAT('%',#{cr.codeOrName},'%'))
        </if>
        <if test="cr.customerType != null">
            AND type = #{cr.customerType}
        </if>
        <if test="cr.startTime != null and cr.startTime != '' ">
            AND create_time &gt;= #{cr.startTime}
        </if>
        <if test="cr.endTime != null and cr.endTime != '' ">
            AND create_time &lt;= #{cr.endTime}
        </if>
        ORDER BY create_time DESC
    </select>

    <!-- 根据id查询客户的基本信息 -->
    <select id="queryCustomerById" parameterType="long" resultType="com.daqing.framework.domain.crms.CustomerEntity">
        SELECT id,code,type,manager,name,addr,phone
        FROM crms_customer
        WHERE del_or_not = 0
        AND id = #{id}
    </select>

    <!-- 根据id更新是否删除字段 -->
    <update id="updateCustomerById" >
        UPDATE crms_customer
        SET del_or_not = 1
        WHERE id IN
        <foreach collection="ids" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </update>

    <!-- 根据id查询客户姓名 -->
    <select id="queryCustomerNameById" resultType="string">
        SELECT name
        FROM crms_customer
        WHERE del_or_not = 0
        AND id IN
        <foreach collection="ids" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <!-- 插入客户基本信息 -->
    <insert id="saveCustomer" keyProperty="id" useGeneratedKeys="true" parameterType="com.daqing.framework.domain.crms.CustomerEntity">
        INSERT INTO crms_customer
        (code,type,manager,name,addr,phone,del_or_not,status,create_time,motify_time)
        VALUES (#{code},#{type},#{manager},#{name},#{addr},#{phone},0,0,#{createTime},#{motifyTime})
    </insert>

    <!-- 更新客户基本信息 -->
    <update id="updateCustomer" parameterType="com.daqing.framework.domain.crms.CustomerEntity">
        UPDATE crms_customer
        SET manager=#{manager},name=#{name},addr=#{addr},phone=#{phone},motify_time=#{motifyTime}
        WHERE id = #{id}
    </update>

    <!-- 查询所有的客户基本信息 -->
    <select id="listCustomerId" resultType="long">
        SELECT id
        FROM crms_customer
        WHERE del_or_not = 0;
    </select>

</mapper>