<?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.ics.admin.mapper.IcsCustomerStaffMapper">

    <resultMap type="com.ics.common.core.domain.IcsCustomerStaff" id="IcsCustomerStaffResult">
        <result property="id"    column="id"    />
        <result property="username"    column="username"    />
        <result property="name"    column="name"    />
        <result property="photo"    column="photo"    />
        <result property="address"    column="address"    />
        <result property="email"    column="email"    />
        <result property="degree"    column="degree"    />
        <result property="urgent"    column="urgent"    />
        <result property="mobile"    column="mobile"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deleteFlag"    column="delete_flag"    />
        <result property="icsCustomerId"    column="ics_customer_id"    />
        <result property="openid"    column="openid"    />
        <result property="avatar"    column="avatar"    />
        <result property="gender"    column="gender"    />
        <result property="status"    column="status"    />
        <result property="parkId"    column="park_id"    />
        <result property="cardNo"    column="card_no"    />
        <result property="visitTime"    column="visit_time"    />
        <result property="leaveTime"    column="leave_time"    />
        <result property="visitContent"    column="visit_content"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="toName"    column="to_name"    />
        <result property="toPhone"    column="to_phone"    />
        <result property="toCustomer"    column="to_customer"    />
        <result property="toCustomerId"    column="to_customer_id"    />
        <result property="dataType"    column="data_type"    />
    </resultMap>

    <sql id="selectIcsCustomerStaffVo">
        SELECT id, username, mobile, create_by, create_time, update_by,name,photo,address,email,degree,urgent, update_time, delete_flag, ics_customer_id, openid, avatar, gender, status, park_id,card_no, visit_time,
               leave_time,visit_content,to_name,to_phone,to_customer,to_customer_id,data_type,park_id,tenant_id
        FROM ics_customer_staff
    </sql>

    <select id="selectIcsCustomerStaffList" parameterType="com.ics.common.core.domain.IcsCustomerStaff" resultMap="IcsCustomerStaffResult">
        SELECT cs.id, cs.username, cs.mobile, cs.create_by, cs.create_time, cs.update_by,cs.name,cs.photo,cs.address,cs.email,cs.degree,cs.urgent,cs.update_time,
        cs.delete_flag, cs.ics_customer_id, cs.openid, cs.avatar, cs.gender, cs.status, cs.park_id,cs.card_no,cs.data_type
        FROM ics_customer_staff cs
        left join  tb_staff_customer tsc on cs.id = tsc.staff_id
        left join ics_customer icc on tsc.ics_customer_id = icc.id
        <where>
            <if test="username != null  and username != ''"> AND cs.username LIKE CONCAT('%', #{username}, '%') </if>
            <if test="icsCustomerId != null  and icsCustomerId != ''"> AND tsc.ics_customer_id = #{icsCustomerId} </if>
            <if test="mobile != null  and mobile != ''"> AND cs.mobile LIKE CONCAT('%', #{mobile}, '%') </if>
            <if test="name != null  and name != ''"> AND cs.name LIKE CONCAT('%', #{name}, '%') </if>
            <if test="parkId != null  and parkId != ''"> AND icc.park_id = #{parkId} </if>
            <if test="tenantId != null  and tenantId != ''"> AND cs.tenant_id = #{tenantId} </if>
            <if test="dataType != null  and dataType != ''"> AND cs.data_type = #{dataType} </if>
        </where>
    </select>

    <select id="selectIcsCustomerStaffById" parameterType="Long" resultMap="IcsCustomerStaffResult">
        <include refid="selectIcsCustomerStaffVo"/>
        WHERE id = #{id}
    </select>

    <insert id="insertIcsCustomerStaff" parameterType="com.ics.common.core.domain.IcsCustomerStaff">
        INSERT INTO ics_customer_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null ">id,</if>
            <if test="username != null  and username != ''">username,</if>
            <if test="name != null  and name != ''">`name`,</if>
            <if test="photo != null  and photo != ''">`photo`,</if>
            <if test="address != null  and address != ''">address,</if>
            <if test="email != null  and email != ''">email,</if>
            <if test="degree != null  and degree != ''">`degree`,</if>
            <if test="urgent != null  and urgent != ''">urgent,</if>
            <if test="mobile != null  and mobile != ''">mobile,</if>
            <if test="password != null  and password != ''">password,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="deleteFlag != null ">delete_flag,</if>
            <if test="icsCustomerId != null ">ics_customer_id,</if>
            <if test="openid != null  and openid != ''">openid,</if>
            <if test="avatar != null  and avatar != ''">avatar,</if>
            <if test="gender != null  and gender != ''">gender,</if>
            <if test="status != null  and status != ''">status,</if>
            <if test="parkId != null ">park_id,</if>
            <if test="cardNo != null and cardNo !=''">,</if>
            <if test="visitTime != null ">,</if>
            <if test="leaveTime != null ">,</if>
            <if test="visitContent != null and visitContent !=''">,</if>
            <if test="toName != null and toName!=''">,</if>
            <if test="toPhone != null and toPhone!=''">,</if>
            <if test="toCustomer != null and toCustomer!=''">,</if>
            <if test="toCustomerId != null and toCustomerId!=''">,</if>
            <if test="dataType != null dataType!=''">,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null ">#{id},</if>
            <if test="username != null  and username != ''">#{username},</if>
            <if test="mobile != null  and mobile != ''">#{mobile},</if>
            <if test="name != null  and name != ''">#{name},</if>
            <if test="photo != null  and photo != ''">#{photo},</if>
            <if test="address != null  and address != ''">#{address},</if>
            <if test="email != null  and email != ''">#{email},</if>
            <if test="degree != null  and degree != ''">#{degree},</if>
            <if test="urgent != null  and urgent != ''">#{urgent},</if>
            <if test="password != null  and password != ''">#{password},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="deleteFlag != null ">#{deleteFlag},</if>
            <if test="icsCustomerId != null ">#{icsCustomerId},</if>
            <if test="openid != null  and openid != ''">#{openid},</if>
            <if test="avatar != null  and avatar != ''">#{avatar},</if>
            <if test="gender != null  and gender != ''">#{gender},</if>
            <if test="status != null  and status != ''">#{status},</if>
            <if test="parkId != null ">#{parkId},</if>
            <if test="cardNo != null and cardNo !=''">#{cardNo}</if>
            <if test="visitTime != null ">#{visitTime}</if>
            <if test="leaveTime != null ">#{leaveTime}</if>
            <if test="visitContent != null and visitContent !=''">#{visitContent}</if>
            <if test="toName != null and toName!=''">#{toName}</if>
            <if test="toPhone != null and toPhone!=''">#{toPhone}</if>
            <if test="toCustomer != null and toCustomer!=''">#{toCustomer}</if>
            <if test="toCustomerId != null and toCustomerId!=''">#{toCustomerId}</if>
            <if test="dataType != null dataType!=''">#{dataType}</if>
         </trim>
    </insert>

    <update id="updateIcsCustomerStaff" parameterType="com.ics.common.core.domain.IcsCustomerStaff">
        UPDATE ics_customer_staff
        <trim prefix="SET" suffixOverrides=",">
            <if test="username != null  and username != ''">username = #{username},</if>
            <if test="mobile != null  and mobile != ''">mobile = #{mobile},</if>
            <if test="password != null  and password != ''">password = #{password},</if>
            <if test="name != null  and name != ''">`name` = #{name},</if>
            <if test="photo != null  and photo != ''">`photo` = #{photo},</if>
            <if test="address != null  and address != ''">`address` = #{address},</if>
            <if test="email != null  and email != ''">`email` = #{email},</if>
            <if test="degree != null  and degree != ''">`degree` = #{degree},</if>
            <if test="urgent != null  and urgent != ''">`urgent` = #{urgent},</if>
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="deleteFlag != null ">delete_flag = #{deleteFlag},</if>
            <if test="icsCustomerId != null ">ics_customer_id = #{icsCustomerId},</if>
            <if test="openid != null  and openid != ''">openid = #{openid},</if>
            <if test="avatar != null  and avatar != ''">avatar = #{avatar},</if>
            <if test="gender != null  and gender != ''">gender = #{gender},</if>
            <if test="status != null  and status != ''">status = #{status},</if>
            <if test="parkId != null ">park_id = #{parkId},</if>
            <if test="cardNo != null and cardNo !=''">card_no = #{cardNo}</if>
            <if test="visitTime != null ">visit_time = #{visitTime}</if>
            <if test="leaveTime != null ">leave_time = #{leaveTime}</if>
            <if test="visitContent != null and visitContent !=''">visit_content = #{visitContent}</if>
            <if test="toName != null and toName!=''">to_name = #{toName}</if>
            <if test="toPhone != null and toPhone!=''">to_phone = #{toPhone}</if>
            <if test="toCustomer != null and toCustomer!=''">to_customer = #{toCustomer}</if>
            <if test="toCustomerId != null and toCustomerId!=''">to_customer_id = #{toCustomerId}</if>
        </trim>
        WHERE id = #{id}
    </update>
    <update id="updateByCustomer" parameterType="Long">
        UPDATE ics_customer_staff set ics_customer_id = null where id = #{id}
    </update>

    <delete id="deleteIcsCustomerStaffById" parameterType="Long">
        DELETE FROM ics_customer_staff WHERE id = #{id}
    </delete>

    <delete id="deleteIcsCustomerStaffByIds" parameterType="String">
        DELETE FROM ics_customer_staff where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="checkPhoneUnique" parameterType="String" resultType="com.ics.common.core.domain.IcsCustomerStaff">
        <include refid="selectIcsCustomerStaffVo"/>
         WHERE mobile=#{mobile}
    </select>
    <select id="selectUserByOpenid" resultType="com.ics.common.core.domain.IcsCustomerStaff">
        <include refid="selectIcsCustomerStaffVo"/>
        WHERE openid=#{openId}
    </select>
    <select id="checkMobileUnique" resultType="java.lang.String">
        SELECT count(1) FROM ics_customer_staff WHERE mobile=#{mobile}
    </select>
    <select id="selectUserByMobile" resultType="com.ics.common.core.domain.IcsCustomerStaff">
        <include refid="selectIcsCustomerStaffVo"/>
        WHERE mobile=#{mobile}
    </select>
    <select id="getUserList" resultType="com.ics.common.core.domain.IcsCustomerStaff">
        SELECT cs.id, cs.username, cs.mobile, cs.create_by, cs.create_time, cs.update_by,cs.name,cs.photo,cs.address,cs.email,cs.degree,cs.urgent,cs.update_time,
        cs.delete_flag, cs.ics_customer_id, cs.openid, cs.avatar, cs.gender, cs.status, cs.park_id,cs.card_no,cs.data_type
        FROM ics_customer_staff cs
        left join  tb_staff_customer tsc on cs.id = tsc.staff_id
        <where>
            <if test="username != null  and username != ''"> AND cs.username LIKE CONCAT('%', #{username}, '%') </if>
            <if test="icsCustomerId != null  and icsCustomerId != ''"> AND tsc.ics_customer_id = #{icsCustomerId} </if>
            <if test="mobile != null  and mobile != ''"> AND cs.mobile LIKE CONCAT('%', #{mobile}, '%') </if>
            <if test="name != null  and name != ''"> AND cs.name LIKE CONCAT('%', #{name}, '%') </if>
            <if test="dataType != null  and dataType != ''"> AND cs.data_type = #{dataType} </if>
            <if test="parkId != null  and parkId != ''"> AND cs.park_id = #{parkId} </if>
            <if test="tenantId != null  and tenantId != ''"> AND cs.tenant_id = #{tenantId} </if>
        </where>
        group by cs.id
    </select>
    <select id="selectUserByMobileAndOpenId" resultType="com.ics.common.core.domain.IcsCustomerStaff">
        <include refid="selectIcsCustomerStaffVo"/> where mobile = #{mobile} and park_id = #{parkId}
    </select>
    <select id="selectListByCustomerId" resultType="com.ics.common.core.domain.IcsCustomerStaff">
        SELECT cs.id, cs.username, cs.mobile, cs.create_by, cs.create_time, cs.update_by,cs.name,cs.photo,cs.address,cs.email,cs.degree,cs.urgent,cs.update_time,
               cs.delete_flag, cs.ics_customer_id, cs.openid, cs.avatar, cs.gender, cs.status, cs.park_id,cs.card_no,cs.data_type
        FROM ics_customer_staff cs
                 left join  tb_staff_customer tsc on cs.id = tsc.staff_id
        WHERE tsc.ics_customer_id = 85
        ORDER BY CONVERT(cs.name USING GBK) ASC
    </select>
    <select id="selectCustomerStaff" resultType="com.ics.common.core.domain.IcsCustomerStaff">
        SELECT cs.id, cs.username, cs.mobile, cs.create_by, cs.create_time, cs.update_by,cs.name,cs.photo,cs.address,cs.email,cs.degree,cs.urgent,cs.update_time,
        cs.delete_flag, cs.ics_customer_id, cs.openid, cs.avatar, cs.gender, cs.status, cs.park_id,cs.card_no,cs.data_type
        FROM ics_customer_staff cs
        left join  tb_staff_customer tsc on cs.id = tsc.staff_id
        left join ics_customer icc on tsc.ics_customer_id = icc.id
        left join tb_wisdom_staff tws on tws.staff_id = cs.id
        <where>
            tws.data_type = 0
            <if test="username != null  and username != ''"> AND cs.username LIKE CONCAT('%', #{username}, '%') </if>
            <if test="icsCustomerId != null  and icsCustomerId != ''"> AND tsc.ics_customer_id = #{icsCustomerId} </if>
            <if test="mobile != null  and mobile != ''"> AND cs.mobile LIKE CONCAT('%', #{mobile}, '%') </if>
            <if test="name != null  and name != ''"> AND cs.name LIKE CONCAT('%', #{name}, '%') </if>
            <if test="parkId != null  and parkId != ''"> AND icc.park_id = #{parkId} </if>
            <if test="tenantId != null  and tenantId != ''"> AND cs.tenant_id = #{tenantId} </if>
            <if test="dataType != null  and dataType != ''"> AND cs.data_type = #{dataType} </if>

        </where>
    </select>


</mapper>
