<?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.trade.service.trade.salesorder.mapper.CustomsDeclarationGoodsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.trade.service.trade.salesorder.domain.CustomsDeclarationGoods">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="sale_order_no" property="saleOrderNo" />
        <result column="hgcksp_dm" property="hgckspDm" />
        <result column="xh4" property="xh4" />
        <result column="ckbgdh" property="ckbgdh" />
        <result column="bgsbzt_dm" property="bgsbztDm" />
        <result column="hgspmc" property="hgspmc" />
        <result column="ggxh" property="ggxh" />
        <result column="hgjldwmc" property="hgjldwmc" />
        <result column="dyjldw_dm" property="dyjldwDm" />
        <result column="cksl" property="cksl" />
        <result column="bgdhgbh" property="bgdhgbh" />
        <result column="hgqy_dm" property="hgqyDm" />
        <result column="ckrq1" property="ckrq1" />
        <result column="sbhggqka_dm" property="sbhggqkaDm" />
        <result column="sbhggqkajc" property="sbhggqkajc" />
        <result column="yfje" property="yfje" />
        <result column="yfhghbzm_dm" property="yfhghbzmDm" />
        <result column="bfje" property="bfje" />
        <result column="bfhghbzm_dm" property="bfhghbzmDm" />
        <result column="zfje1" property="zfje1" />
        <result column="zfhghbzm_dm" property="zfhghbzmDm" />
        <result column="js1" property="js1" />
        <result column="mz2" property="mz2" />
        <result column="jz" property="jz" />
        <result column="hgckhwbgdsbrq" property="hgckhwbgdsbrq" />
        <result column="sbdwmc" property="sbdwmc" />
        <result column="jzxh" property="jzxh" />
        <result column="sbjldw_dm" property="sbjldwDm" />
        <result column="sbjldwmc" property="sbjldwmc" />
        <result column="sbdj" property="sbdj" />
        <result column="sbsl1" property="sbsl1" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        sale_order_no, hgcksp_dm, xh4, ckbgdh, bgsbzt_dm, hgspmc, ggxh, hgjldwmc, dyjldw_dm, cksl, bgdhgbh, hgqy_dm, ckrq1, sbhggqka_dm, sbhggqkajc, yfje,
yfhghbzm_dm, bfje, bfhghbzm_dm, zfje1, zfhghbzm_dm, js1, mz2, jz, hgckhwbgdsbrq, sbdwmc, jzxh, sbjldw_dm, sbjldwmc, sbdj, sbsl1, gmt_create, gmt_modified
    </sql>
    <select id="listWithCustomsDeclarationDtlByOrderId"
            resultType="com.trade.service.trade.salesorder.vo.CustomsDeclarationGoodsDtlVO">
        select a.hgcksp_dm hs_code, a.sbdj price, a.sbjldwmc unit, a.sbsl1 count, a.sbdj * a.sbsl1  price_total, a.xh4, a.hgspmc, a.ggxh,
               CONCAT(a.hgjldwmc,',',a.dyjldw_dm) as fdjldw,
               a.cksl,e.payment_currency from customs_declaration_goods a
            left join sales_order b on a.sale_order_no = b.order_no
            left join sales_contract d on b.sale_contract_id = d.id
            left join contract_terms e on d.id = e.contract_id
        where b.id = #{orderId} and a.deleted = 0
    </select>

    <resultMap id="BaseResultBelongsMap" type="com.trade.service.trade.salesorder.vo.CustomsDeclarationGoodsBelongsVO">
        <result column="id" property="id"/>
        <result column="sale_order_no" property="salesOrderNo"/>
        <result column="hgcksp_dm" property="hgckspDm"/>
        <result column="ckbgdh" property="ckbgdh"/>
        <result column="bgdhgbh" property="bgdhgbh"/>
        <result column="customer_user_id" property="customerUserId"/>
        <result column="importer_cert_no" property="importerCertNo"/>
        <result column="agency_cert_no" property="agencyCertNo"/>
        <result column="has_purchase_contract" property="hasPurchaseContract"/>
        <result column="sales_contract_id" property="salesContractId"/>
        <result column="sales_contract_no" property="salesContractNo"/>
        <result column="xt_user_id" property="xtUserId"/>
        <result column="xt_eid" property="xtEid"/>
    </resultMap>

    <select id="getAllNeedPullTaxRebateRecords"
            resultMap="BaseResultBelongsMap">
        SELECT a.id,
               a.sale_order_no,
               o.sale_contract_id AS sales_contract_id,
               c.contract_no AS sales_contract_no,
               a.hgcksp_dm,
               a.ckbgdh,
               a.bgdhgbh,
               c.customer_user_id,
               c.importer_cert_no,
               c.agency_cert_no,
               c.has_purchase_contract,
               IF
               ( c.agency_cert_no IS NOT NULL AND c.agency_cert_no != '', p.xt_user_id, u.xt_uid ) AS xt_user_id,
               IF
               ( c.agency_cert_no IS NOT NULL AND c.agency_cert_no != '', p.xt_eid, e.xt_eid ) AS xt_eid
        FROM customs_declaration_goods a
                 LEFT JOIN sales_order o ON a.sale_order_no = o.order_no
                 LEFT JOIN sales_contract c ON o.sale_contract_id = c.id
                 LEFT JOIN trading_platform p ON p.cert_no = c.agency_cert_no
                 LEFT JOIN customer_user u ON  u.id= c.customer_user_id
                 LEFT JOIN enterprise e ON c.customer_user_id = e.customer_id
        WHERE a.deleted = 0 AND a.tsjltbbs = 0 AND a.bgsbzt_dm = '10'
    </select>

</mapper>
