<?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.dispose.cst.station.mapper.StationMapper">
    <resultMap id="StationResultPagingMap" autoMapping="true" type="com.njzscloud.dispose.cst.station.pojo.result.StationResult">
        <id column="id" property="id"/>
        <association property="customer" autoMapping="true" javaType="com.njzscloud.dispose.cst.customer.pojo.result.SearchCustomerResult">
            <id column="customer_id" property="id"/>
            <result column="user_id" property="userId"/>
            <result column="identity_category" property="identityCategory"/>
            <result column="customer_name" property="customerName"/>
            <result column="phone" property="phone"/>
            <result column="settlement_way" property="settlementWay"/>
            <result column="manager" property="manager"/>
            <result column="avatar" property="avatar"/>
            <result column="username" property="username"/>
            <result column="uscc" property="uscc"/>
            <result column="org_name" property="orgName"/>
        </association>
    </resultMap>
    <sql id="searchStation">
        SELECT a.id,
               a.org_id,
               a.station_name,
               a.province,
               a.city,
               a.area,
               a.town,
               a.province_name,
               a.city_name,
               a.area_name,
               a.town_name,
               a.address,
               a.lng,
               a.lat,
               a.create_time,
               b.id customer_id,
               b.user_id,
               b.identity_category,
               b.customer_name,
               b.phone,
               b.settlement_way,
               b.manager,
               c.avatar,
               d.id account_id,
               d.username,
               d.wechat_openid,
               d.regdate,
               d.client_code,
               e.org_category,
               e.uscc,
               e.org_name,
               e.business_license,
               e.license_start_time,
               e.license_end_time,
               e.legal_representative,
               e.idcard,
               e.idcard_start_time,
               e.idcard_end_time,
               e.idcard_front,
               e.idcard_back
        FROM cst_station a
                 INNER JOIN cst_station_member f ON f.station_id = a.id
                 INNER JOIN cst_customer b ON b.org_id = a.org_id AND b.id = f.customer_id AND b.deleted = 0
                 INNER JOIN sys_user c ON c.id = b.user_id AND c.deleted = 0
                 INNER JOIN sys_user_account d ON d.user_id = c.id AND d.deleted = 0
                 INNER JOIN cst_org e ON e.id = a.org_id AND e.deleted = 0
    </sql>
    <select id="paging" resultMap="StationResultPagingMap">
        <include refid="searchStation"/>
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="detail" resultMap="StationResultPagingMap">
        <include refid="searchStation"/>
        WHERE a.id = #{id}
    </select>
    <select id="listAll" resultMap="StationResultPagingMap">
        <include refid="searchStation"/>
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="isManager" resultType="java.lang.Boolean">
        SELECT *
        FROM cst_station_member a
                 INNER JOIN cst_customer b ON b.id = a.customer_id AND b.deleted = 0 AND b.manager = 1 AND b.org_id = a.org_id
        WHERE a.user_id = #{userId}
    </select>
</mapper>
