<?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.dayou.mapper.CustomerMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.Customer">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="name" property="name" />
        <result column="city" property="city" />
        <result column="county" property="county" />
        <result column="department" property="department" />
        <result column="position" property="position" />
        <result column="section" property="section" />
        <result column="mobile" property="mobile" />
        <result column="wechat_no" property="wechatNo" />
        <result column="QQ" property="qq" />
        <result column="level" property="level" />
        <result column="address" property="address" />
        <result column="user_id" property="userId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        name, city, county, department, position, section, mobile, wechat_no, QQ, level, address,user_id
    </sql>

    <select id="getPage" parameterType="com.dayou.dto.CustomerDTO" resultType="com.dayou.dto.CustomerDTO">
        select c.*,u.name as userName
            from customer c left join user u on c.user_id = u.id
        <where>
            and c.deleted = 0 and u.deleted = 0
            <if test="keyword!=null and keyword!='' ">
                and(
                c.name like concat ('%',#{keyword},'%')
                or c.department like concat('%',#{keyword},'%')
                or c.position like concat('%',#{keyword},'%')
                or c.section like concat('%',#{keyword},'%')
                   or JSON_EXTRACT(c.city,'$[1]') = (select id from districts where ext_name =#{keyword})
                or JSON_EXTRACT(c.city,'$[2]') = (select id from districts where ext_name =#{keyword})
                )
            </if>
            <if test="customer!=null and customer.name!=null and customer.name!='' ">
                and c.name like concat ('%',#{customer.name},'%')
            </if>
            <if test="customer!=null and customer.level!=null and customer.level!='' ">
                and c.level = #{customer.level}
            </if>
            <if test="customer!=null and customer.userIds!=null and customer.userIds.size!=0">
               and  c.user_id in
                <foreach collection="customer.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getList" parameterType="com.dayou.dto.CustomerDTO" resultType="com.dayou.dto.CustomerDTO">
        select c.*,u.name as userName
        from customer c left join user u on c.user_id = u.id
        <where>
            and c.deleted = 0 and u.deleted = 0
            <if test="customer!=null and customer.name!=null and customer.name!='' ">
                and c.name like concat ('%',#{customer.name},'%')
            </if>
            <if test="customer!=null and customer.level!=null and customer.level!='' ">
                and c.level = #{customer.level}
            </if>
            <if test="customer!=null and customer.userIds!=null and customer.userIds.size!=0">
                and  c.user_id in
                <foreach collection="customer.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        </where>
        order by c.id DESC
    </select>
</mapper>
