<?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.njzscloud.supervisory.customer.mapper.CustomerMapper">

    <resultMap id="SearchCompanyResultMap" autoMapping="true" type="com.njzscloud.supervisory.customer.pojo.result.SearchCompanyResult">
        <result column="contacts_phone" property="phone"/>
        <result column="company_id" property="id"/>
        <result column="audit_status" property="auditStatus" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
    </resultMap>
    <resultMap id="SearchCustomerResultMap" autoMapping="true" type="com.njzscloud.supervisory.customer.pojo.result.SearchCustomerResult">
        <result column="biz_obj" property="bizObj" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <result column="gender" property="gender" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <association property="company" resultMap="SearchCompanyResultMap"/>
    </resultMap>

    <select id="paging" resultMap="SearchCustomerResultMap">
        SELECT a.id,
        IF(c.contacts IS NOT NULL, c.contacts, IF(d.driver_name IS NOT NULL, d.driver_name, a.nickname)) customer_name,
        IF(c.phone IS NOT NULL, c.phone, IF(d.phone IS NOT NULL, d.phone, a.phone)) customer_phone,
        a.avatar,
        a.gender,
        a.email,
        a.biz_obj,
        b.regdate,
        b.username,
        c.id company_id,
        c.uscc,
        c.company_name,
        c.business_license,
        c.license_start_time,
        c.license_end_time,
        c.legal_representative,
        c.province,
        c.city,
        c.area,
        c.town,
        c.province_name,
        c.city_name,
        c.area_name,
        c.town_name,
        c.address,
        c.lng,
        c.lat,
        c.contacts,
        c.phone contacts_phone,
        c.audit_status,
        c.audit_memo
        FROM sys_user a
        INNER JOIN sys_user_account b ON b.user_id = a.id AND b.deleted = 0
        LEFT JOIN biz_company c ON c.user_id = a.id AND c.station = 0 AND c.deleted = 0
        LEFT JOIN biz_driver d ON d.user_id = a.id AND d.deleted = 0
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="shortBargeInfo"
            resultType="com.njzscloud.supervisory.customer.pojo.result.ShortBargeInfoResult">
        SELECT a.user_id,
        IF(COUNT(1) > 0, 1, 0) short_barge
        FROM sys_user_role a
        INNER JOIN sys_role b ON b.id = a.role_id AND b.role_code = 'ROLE_SHORT_BARGE' AND b.deleted = 0
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>
</mapper>
