<?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.iotechn.unimall.data.mapper.CardManagementInfoMapper">


    <select id="getCardManagementPage" resultType="com.iotechn.unimall.data.domain.CardManagementInfo">
        SELECT
        cmi.id as id,
        cmi.common_id as receiveId,
        cmi.name AS name,
        cmi.post AS post,
        cmi.company_name as companyName,
        phi.scope_of_business AS scopeOfBusiness,
        cui.look_page AS lookPage,
        case when (cmi.common_id=#{commonId})  then 1
        when IFNULL((SELECT count(chi.id)
        FROM card_holder_info chi
        WHERE chi.delete_flag = '0'
        AND chi.common_id =#{commonId}
        AND chi.card_id =cmi.id ),0) >0 then 1
        when IFNULL((SELECT count(cei.id)
        FROM card_exchange_info cei
        WHERE cei.delete_flag = '0'
        AND cei.send_id =#{commonId}
        AND (cei.status=1 or cei.status=0 or cei.status=7)
        AND cei.receive_card_id =cmi.id ),0) >0 then 1  else 0 end as notDisplay,
        cci.gmt_update AS gmtUpdate,
        cci.gmt_create AS gmtCreate
        FROM
        circle_card_info cci
        LEFT JOIN card_management_info cmi on cmi.id=cci.card_id and cmi.delete_flag=0
        LEFT JOIN personal_homepage_info phi on phi.common_id=cmi.common_id and phi.delete_flag=0
        LEFT JOIN common_user_info cui on cui.id=cmi.common_id and cui.delete_flag=0
        LEFT JOIN card_holder_info ch on ch.common_id=#{commonId} and ch.card_id=cmi.id and ch.delete_flag=0
        WHERE
        cci.circle_id = #{id}
        and cci.delete_flag=0
        <if test="status != null">
            <if test="status == 1">
                AND ch.id is not null
            </if>
            <if test="status == 2">
                AND ch.id is null
            </if>
        </if>
        GROUP BY cci.id
        ORDER BY cci.gmt_update DESC
        LIMIT #{offset}, #{limit}
    </select>
    <select id="getCardManagementCount" resultType="java.lang.Integer">
        select
        count(1)
        from( SELECT
        count(1)
        FROM
        circle_card_info cci
        LEFT JOIN card_management_info cmi on cmi.id=cci.card_id and cmi.delete_flag=0
        LEFT JOIN personal_homepage_info phi on phi.common_id=cmi.common_id and phi.delete_flag=0
        LEFT JOIN common_user_info cui on cui.id=cmi.common_id and cui.delete_flag=0
        LEFT JOIN card_holder_info ch on ch.common_id=#{commonId} and ch.card_id=cmi.id and ch.delete_flag=0
        WHERE
        cci.circle_id = #{id}
        and cci.delete_flag=0
        <if test="status != null">
            <if test="status == 1">
                AND ch.id is not null
            </if>
            <if test="status == 2">
                AND ch.id is null
            </if>
        </if>
        GROUP BY cci.id ) tt
    </select>
</mapper>