<?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.pj.project.tb_costomer.TbCostomerMapper">

    <!-- 增 [G] -->
    <insert id="add">
		insert into 
		tb_costomer (id, name, phone, duty_people, address_ids, address_str, business_licence, status, creare_time, judge_status, judge_time, judge_content) 
		values (#{id}, #{name}, #{phone}, #{dutyPeople}, #{addressIds}, #{addressStr}, #{businessLicence}, #{status}, #{creareTime}, #{judgeStatus}, #{judgeTime}, #{judgeContent}) 
	</insert>

    <!-- 删 -->
    <delete id="delete">
		delete from tb_costomer 
		where id = #{id}
	</delete>

    <!-- 改 [G] -->
    <update id="update">
		update tb_costomer set
		id = #{id}, 
		name = #{name}, 
		phone = #{phone}, 
		duty_people = #{dutyPeople}, 
		address_ids = #{addressIds}, 
		address_str = #{addressStr}, 
		business_licence = #{businessLicence}, 
		status = #{status}, 
		creare_time = #{creareTime}, 
		judge_status = #{judgeStatus}, 
		judge_time = #{judgeTime}, 
		judge_content = #{judgeContent}
		where id = #{id}
	</update>


    <!-- ================================== 查询相关 ================================== -->


    <!-- 通用映射：手动模式 -->
    <resultMap id="model" type="com.pj.project.tb_costomer.TbCostomer">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
        <result property="dutyPeople" column="duty_people"/>
        <result property="addressIds" column="address_ids"/>
        <result property="addressStr" column="address_str"/>
        <result property="businessLicence" column="business_licence"/>
        <result property="status" column="status"/>
        <result property="creareTime" column="creare_time"/>
        <result property="judgeStatus" column="judge_status"/>
        <result property="judgeTime" column="judge_time"/>
        <result property="judgeContent" column="judge_content"/>
    </resultMap>

    <!-- 公共查询sql片段 -->
    <sql id="select_sql">
		select 	*
		from tb_costomer
	</sql>

    <!-- 查 - 根据id -->
    <select id="getById" resultMap="model">
        <include refid="select_sql"></include>
        where id = #{id}
    </select>

    <!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
    <select id="getList" resultMap="model">
        <include refid="select_sql"></include>
        <where>
            <if test=' this.has("id") '>and id = #{id}</if>
            <if test=' this.has("name") '>and name like concat('%',#{name},'%')</if>
            <if test=' this.has("phone") '>and phone like concat('%',#{phone},'%')</if>
            <if test=' this.has("dutyPeople") '>and duty_people like concat('%',#{dutyPeople},'%')</if>
            <if test=' this.has("addressIds") '>and address_ids = #{addressIds}</if>
            <if test=' this.has("addressStr") '>and address_str = #{addressStr}</if>
            <if test=' this.has("status") '>and status = #{status}</if>
            <if test=' this.has("creareTime") '>and creare_time = #{creareTime}</if>
            <if test=' this.has("judgeStatus") '>and judge_status = #{judgeStatus}</if>
            <if test=' this.has("judgeTime") '>and judge_time = #{judgeTime}</if>
            <if test=' this.has("type") '>and type = #{type}</if>
            <if test=' this.has("partner") '>and type != 0</if>
            <if test=' this.has("judgeContent") '>and judge_content = #{judgeContent}</if>
            <if test=' this.has("current") '>
                <if test=' current != 0 '>and judge_status = #{current}</if>
            </if>
        </where>
        order by
        <choose>
            <when test='sortType == 1'>id desc</when>
            <when test='sortType == 2'>name desc</when>
            <when test='sortType == 3'>phone desc</when>
            <when test='sortType == 4'>duty_people desc</when>
            <when test='sortType == 5'>address_ids desc</when>
            <when test='sortType == 6'>address_str desc</when>
            <when test='sortType == 7'>status desc</when>
            <when test='sortType == 8'>creare_time desc</when>
            <when test='sortType == 9'>judge_status desc</when>
            <when test='sortType == 10'>judge_time desc</when>
            <when test='sortType == 11'>judge_content desc</when>
            <otherwise>id desc</otherwise>
        </choose>
    </select>
    <select id="findByBusinessTypeOpenid" resultType="java.lang.String">
		SELECT b.openid from tb_costomer a LEFT JOIN sp_admin b on a.id=b.customer_id
        WHERE a.type like concat('%',#{businessType},'%') and b.openid !='' and b.openid is not null
	</select>


</mapper>
