<?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.hzs.ground.margin.mapper.ThMarginMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.ground.margin.ThMargin">
        <id column="PK_ID" property="pkId" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_SUPPLIER" property="pkSupplier" />
        <result column="BILL_DATE" property="billDate" />
        <result column="SALE_TYPE" property="saleType" />
        <result column="SALE_SCOPE" property="saleScope" />
        <result column="PHONE" property="phone" />
        <result column="LINK_MAN" property="linkMan" />
        <result column="MARGIN" property="margin" />
        <result column="EDIT_TYPE" property="editType" />
        <result column="APPROVE_STATUS" property="approveStatus" />
        <result column="TH_BUSINESS_CODE" property="thBusinessCode" />



    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        CREATION_TIME,
        MODIFIED_TIME,
        DEL_FLAG,
        PK_CREATOR,
        PK_MODIFIED,
        PK_COUNTRY,
        PK_ID, PK_SUPPLIER, BILL_DATE, SALE_TYPE, SALE_SCOPE, PHONE, LINK_MAN, MARGIN
    </sql>


<!--    保证金缴纳列表-->
    <select id="queryThMarginList" resultType="com.hzs.ground.margin.vo.ThMarginVO">
        select
        cm.member_code memberCode,
        cm.member_name memberName,
        tm.pk_id pkId,
        bc.CUST_CODE custCode,
        bc.CUST_NAME custName,
        bc.CUST_TYPE custType,
        bc.short_name shortName,
        bc.legal_body legalBody,
        bc.link_man linkMan,
        bc.taxpayerid taxpayerid,
        bc.address address,
        bc.trade_scope tradeScope,
        bc.creation_time creationTime,
        bc.pk_user pkMember,
        p.name provinceName,
        c.name cityName,
        co.name countyName,
        tm.bill_date billDate,
        tm.sale_scope saleScope,
        tm.sale_type saleType,
        tm.phone phone,
        tm.APPROVE_STATUS approveStatus
        from TH_MARGIN tm
        inner join BD_CUBASDOC bc on bc.pk_id=tm.PK_SUPPLIER
        left join cu_member cm on cm.pk_id=bc.pk_user and cm.del_flag=0 and cm.CATEGORY=0
        LEFT JOIN BD_AREA p on p.PK_ID=bc.PROVINCE
        LEFT JOIN BD_AREA c on c.PK_ID=bc.CITY
        LEFT JOIN BD_AREA co on co.PK_ID=bc.COUNTY
        where tm.del_flag=0
        and bc.del_flag=0
        <if test="pkCountry != null">
            and bc.pk_country=#{pkCountry}
        </if>
        <if test="pkCreator != null">
            and tm.PK_CREATOR=#{pkCreator}
        </if>
        <if test="custName != null and custName != ''">
            and bc.CUST_NAME like #{custName} || '%'
        </if>
        order by bc.pk_id desc
    </select>

    <select id="queryThMarginByCode" resultType="com.hzs.ground.margin.vo.ThMarginVO">
        select
        cm.member_code memberCode,
        cm.member_name memberName,
        tm.pk_id pkId,
        bc.CUST_CODE custCode,
        bc.CUST_NAME custName,
        bc.CUST_TYPE custType,
        bc.short_name shortName,
        bc.legal_body legalBody,
        bc.link_man linkMan,
        bc.taxpayerid taxpayerid,
        bc.address address,
        bc.trade_scope tradeScope,
        tm.creation_time creationTime,
        bc.pk_user pkMember,
        p.name provinceName,
        c.name cityName,
        co.name countyName,
        tm.bill_date billDate,
        tm.sale_scope saleScope,
        tm.sale_type saleType,
        tm.phone phone,
        tm.margin
        from TH_MARGIN tm
        inner join BD_CUBASDOC bc on bc.pk_id=tm.PK_SUPPLIER
        left join cu_member cm on cm.pk_id=bc.pk_user and cm.del_flag=0 and cm.CATEGORY=0
        LEFT JOIN BD_AREA p on p.PK_ID=bc.PROVINCE
        LEFT JOIN BD_AREA c on c.PK_ID=bc.CITY
        LEFT JOIN BD_AREA co on co.PK_ID=bc.COUNTY
        where tm.del_flag=0
        and bc.del_flag=0
        <if test="pkCountry != null">
            and bc.pk_country=#{pkCountry}
        </if>
        <if test="businessCode != null and businessCode != ''">
            and tm.TH_BUSINESS_CODE=#{businessCode}
        </if>

    </select>



</mapper>
