<?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="jnpf.franchisee.mapper.FranchiseeMapper">

    <select id="queryPageList" resultType="jnpf.model.franchisee.vo.FranchiseePageVO">
        SELECT DISTINCT
            f.F_Id AS id,
            f.F_FranchiseeCode AS franchiseeCode,
            f.F_FranchiseeName AS franchiseeName,
            f.F_MobilePhone AS mobilePhone,
            f.F_StoreNum AS storeNum,
            f.F_FranchiseeNature AS franchiseeNature,
            f.F_JoinDate AS joinDate,
            f.F_JoinType AS joinType,
            f.F_BusinessNature AS businessNature,
            f.F_InvestAmount AS investAmount,
            f.F_ProfitExpectation AS profitExpectation,
            f.F_AnnualIncome AS annualIncome,
            f.F_FranchiseeStatus as franchiseeStatus,
            f.F_StoreBusinessDistrict as storeBusinessDistrict,
            f.F_CreatorTime
        FROM ftb_franchisee f
        LEFT JOIN ftb_franchisee_join_region r ON r.F_FranchiseeId = f.F_Id
        <where>
            f.F_EnabledMark = 0
            <if test="req != null">
                <if test="req.keyword != null and req.keyword != ''">
                    AND (
                        f.F_FranchiseeCode LIKE CONCAT('%', #{req.keyword}, '%')
                        OR f.F_FranchiseeName LIKE CONCAT('%', #{req.keyword}, '%')
                    )
                </if>
                <if test="req.mobilePhone != null and req.mobilePhone != ''">
                    AND f.F_MobilePhone LIKE CONCAT('%', #{req.mobilePhone}, '%')
                </if>
                <if test="req.minStoreNum != null">
                    AND f.F_StoreNum <![CDATA[>=]]> #{req.minStoreNum}
                </if>
                <if test="req.maxStoreNum != null">
                    AND f.F_StoreNum <![CDATA[<=]]> #{req.maxStoreNum}
                </if>
                <if test="req.franchiseeNature != null and req.franchiseeNature != ''">
                    AND f.F_FranchiseeNature = #{req.franchiseeNature}
                </if>
                <if test="req.storeBusinessDistrict != null and req.storeBusinessDistrict != ''">
                    AND f.F_StoreBusinessDistrict = #{req.storeBusinessDistrict}
                </if>

                <if test="req.joinDateStart != null">
                    AND f.F_JoinDate <![CDATA[>=]]> #{req.joinDateStart}
                </if>
                <if test="req.joinDateEnd != null">
                    AND f.F_JoinDate <![CDATA[<=]]> #{req.joinDateEnd}
                </if>

                <if test="req.profitExpectationStart != null and req.profitExpectationStart != ''">
                    AND f.F_ProfitExpectation <![CDATA[>=]]> #{req.profitExpectationStart}
                </if>
                <if test="req.profitExpectationEnd != null and req.profitExpectationEnd != ''">
                    AND f.F_ProfitExpectation <![CDATA[<=]]> #{req.profitExpectationEnd}
                </if>

                <if test="req.minInvestAmount != null">
                    AND f.F_InvestAmount <![CDATA[>=]]> #{req.minInvestAmount}
                </if>
                <if test="req.maxInvestAmount != null">
                    AND f.F_InvestAmount <![CDATA[<=]]> #{req.maxInvestAmount}
                </if>
                <if test="req.joinType != null and req.joinType != ''">
                    AND f.F_JoinType = #{req.joinType}
                </if>
                <if test="req.franchiseeStatus != null and req.franchiseeStatus != ''">
                    AND f.F_FranchiseeStatus = #{req.franchiseeStatus}
                </if>
                <if test="(req.joinRegion != null and req.joinRegion.size() > 0)
                        or (provinceCityJoinRegion != null and provinceCityJoinRegion.size() > 0)
                        or (provinceList != null and provinceList.size() > 0)">
                    AND (
                        <if test="req.joinRegion != null and req.joinRegion.size() > 0">
                            (
                                IFNULL(r.F_Province, ''),
                                IFNULL(r.F_City, ''),
                                IFNULL(r.F_District, ''),
                                IFNULL(r.F_County, '')
                            ) IN
                            <foreach collection="req.joinRegion" item="item" open="(" separator="," close=")">
                                (
                                    IFNULL(#{item.province}, ''),
                                    IFNULL(#{item.city}, ''),
                                    IFNULL(#{item.district}, ''),
                                    IFNULL(#{item.county}, '')
                                )
                            </foreach>
                        </if>
                        <if test="provinceCityJoinRegion != null and provinceCityJoinRegion.size() > 0">
                            <if test="req.joinRegion != null and req.joinRegion.size() > 0"> OR </if>
                            (
                                IFNULL(r.F_Province, ''),
                                IFNULL(r.F_City, '')
                            ) IN
                            <foreach collection="provinceCityJoinRegion" item="item" open="(" separator="," close=")">
                                (
                                    IFNULL(#{item.province}, ''),
                                    IFNULL(#{item.city}, '')
                                )
                            </foreach>
                        </if>
                        <if test="provinceList != null and provinceList.size() > 0">
                            <if test="(req.joinRegion != null and req.joinRegion.size() > 0)
                                   or (provinceCityJoinRegion != null and provinceCityJoinRegion.size() > 0)"> OR </if>
                            IFNULL(r.F_Province, '') IN
                            <foreach collection="provinceList" item="item" open="(" separator="," close=")">
                                IFNULL(#{item}, '')
                            </foreach>
                        </if>
                    )
                </if>
                <if test="req.businessNature != null and req.businessNature != ''">
                    AND f.F_BusinessNature = #{req.businessNature}
                </if>
                <if test="req.annualIncome != null and req.annualIncome != ''">
                    AND f.F_AnnualIncome = #{req.annualIncome}
                </if>
            </if>
        </where>
        ORDER BY f.F_CreatorTime DESC
    </select>

</mapper>
