<?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.yeejoin.amos.boot.module.hygf.api.mapper.HygfIcbcRecordMapper">
    <sql id="tableName">
        hygf_icbc_record
    </sql>

    <sql id="baseColumn">
        sequence_nbr,cust_name,id_card,phone,medium_id,project_id,open_account_state,protocol_status,protocol_start_time,protocol_end_time,trx_limit_amount,day_limit_amount,protocol_limit_amount,app_id,out_user_id,out_vendor_id,sign_entrsut_prtl_status
    </sql>

    <select id="listObjectCount" resultType="integer">
        SELECT
            count( DISTINCT ph.amos_user_id )
        FROM
            hygf_peasant_household ph LEFT JOIN hygf_icbc_record icbc ON ph.amos_user_id = icbc.amos_user_id
        <where>
            ph.is_delete = 0
            <if test="param != null">
                <if test="param.openAccountStatus != null and param.openAccountStatus != ''">
                    <if test="param.openAccountStatus != '00'">
                        AND icbc.open_account_status = #{param.openAccountStatus}
                    </if>
                    <if test="param.openAccountStatus == '00'">
                        AND (icbc.open_account_status = #{param.openAccountStatus} OR icbc.open_account_status is null)
                    </if>
                </if>
                <if test="param.custName != null and param.custName != ''">
                    AND (ph.owners_name LIKE CONCAT('%', #{param.custName}, '%') OR icbc.cust_name LIKE CONCAT('%', #{param.custName}, '%'))
                </if>
                <if test="param.idCard != null and param.idCard != ''">
                    AND (ph.id_card LIKE CONCAT('%', #{param.idCard}, '%') OR icbc.id_card LIKE CONCAT('%', #{param.idCard}, '%'))
                </if>
                <if test="param.phone != null and param.phone != ''">
                    AND (ph.telephone LIKE CONCAT('%', #{param.phone}, '%') OR icbc.phone LIKE CONCAT('%', #{param.phone}, '%'))
                </if>
                <if test="param.regionalCompaniesName != null and param.regionalCompaniesName != ''">
                    AND ph.regional_companies_name LIKE CONCAT('%', #{param.regionalCompaniesName}, '%')
                </if>
                <if test="param.addressName != null and param.addressName != ''">
                    AND ph.project_address LIKE CONCAT('%', #{param.addressName}, '%')
                </if>
            </if>
        </where>
    </select>

    <select id="listObject" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.HygfIcbcRecordDTO">
        SELECT DISTINCT
            icbc.sequence_nbr,
            ifnull(icbc.cust_name, ph.owners_name) as custName,
            ifnull(icbc.phone, ph.telephone) as phone,
            ifnull(icbc.id_card, ph.id_card) as idCard,
            ph.amos_user_id,
            icbc.open_account_status,
            icbc.protocol_status,
            ph.rec_date
        FROM
            hygf_peasant_household ph LEFT JOIN hygf_icbc_record icbc ON ph.amos_user_id = icbc.amos_user_id
        <where>
            ph.is_delete = 0
            <if test="param != null">
                <if test="param.openAccountStatus != null and param.openAccountStatus != ''">
                    <if test="param.openAccountStatus != '00'">
                        AND icbc.open_account_status = #{param.openAccountStatus}
                    </if>
                    <if test="param.openAccountStatus == '00'">
                        AND (icbc.open_account_status = #{param.openAccountStatus} OR icbc.open_account_status is null)
                    </if>
                </if>
                <if test="param.custName != null and param.custName != ''">
                    AND (ph.owners_name LIKE CONCAT('%', #{param.custName}, '%') OR icbc.cust_name LIKE CONCAT('%', #{param.custName}, '%'))
                </if>
                <if test="param.idCard != null and param.idCard != ''">
                    AND (ph.id_card LIKE CONCAT('%', #{param.idCard}, '%') OR icbc.id_card LIKE CONCAT('%', #{param.idCard}, '%'))
                </if>
                <if test="param.phone != null and param.phone != ''">
                    AND (ph.telephone LIKE CONCAT('%', #{param.phone}, '%') OR icbc.phone LIKE CONCAT('%', #{param.phone}, '%'))
                </if>
                <if test="param.regionalCompaniesName != null and param.regionalCompaniesName != ''">
                    AND ph.regional_companies_name LIKE CONCAT('%', #{param.regionalCompaniesName}, '%')
                </if>
                <if test="param.addressName != null and param.addressName != ''">
                    AND ph.project_address LIKE CONCAT('%', #{param.addressName}, '%')
                </if>
            </if>
        </where>
        <if test="param.offset != null and param.limit != null">
            LIMIT ${param.offset}, ${param.limit}
        </if>
        ORDER BY
            ph.rec_date DESC
    </select>
</mapper>