<?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.dxhy.extend.dao.ExtTdxCheckLogDao">
    <resultMap id="BaseResultMap" type="com.dxhy.extend.entity.ExtTdxCheckLog">
        <result column="id" property="id"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_no" property="invoiceNo"/>
        <result column="invoice_date" property="invoiceDate"/>
        <result column="buyer_name" property="buyerName"/>
        <result column="saler_name" property="salerName"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="tax_amount" property="taxAmount"/>
        <result column="invoice_amount" property="invoiceAmount"/>
        <result column="check_massege" property="checkMassege"/>
        <result column="check_code" property="checkCode"/>
        <result column="invoice_type" property="invoiceType"/>
        <result column="handle_code" property="handleCode"/>
        <result column="handle_date" property="handleDate"/>
    </resultMap>

    <resultMap id="CheckLogHjMap" type="com.dxhy.extend.entity.ExtTdxCheckLog">
        <result column="hjsl" property="id"/>
        <result column="hjje" property="invoiceAmount"/>
        <result column="hjse" property="taxAmount"/>
    </resultMap>

    <resultMap id="CheckStatisticsMap" type="com.dxhy.extend.entity.ExtTdxCheckLog">
        <result column="invoiceYearMonth" property="checkMonth"/>
        <result column="sumCount" property="checkCount"/>
        <result column="sumSuccess" property="checkSuccessCount"/>
        <result column="sumError" property="checkErrorCount"/>
    </resultMap>

    <resultMap id="CheckRecordInvoiceMap" type="com.dxhy.extend.entity.ExtTdxCheckLog">
        <result column="id" property="id"/>
        <result column="FPDM" property="invoiceCode"/>
        <result column="FPHM" property="invoiceNo"/>
        <result column="KPRQ" property="invoiceDate"/>
        <result column="GFMC" property="buyerName"/>
        <result column="XFMC" property="salerName"/>
        <result column="JSHJ" property="totalAmount"/>
        <result column="SE" property="taxAmount"/>
        <result column="JE" property="invoiceAmount"/>
        <result column="CYJG_TIP" property="checkMassege"/>
        <result column="JYM" property="checkCode"/>
        <result column="FPZL" property="invoiceType"/>
        <result column="CYJG_CODE" property="handleCode"/>
        <result column="create_date" property="handleDate"/>
        <result column="ZFBZ" property="invoiceStatus"/>
        <result column="scan_id" property="scanId"/>
    </resultMap>

    <select id="selectCheckHistory" parameterType="java.util.Map"
            resultMap="CheckRecordInvoiceMap" databaseId="mysql">
        SELECT t.id,t.FPDM,t.FPHM,date_format(t.KPRQ,'%Y-%m-%d')
        KPRQ,t.GFMC,t.XFMC,t.JE,t.SE,t.JSHJ,t.JYM,t.CYJG_TIP,t.FPZL,t.CYJG_CODE
        FROM t_dx_check_record_invoice t
        WHERE t.check_user = #{userid}
        <if test="invoiceCode != null and invoiceCode != '' and invoiceCode != 'null' ">
            AND t.FPDM = #{invoiceCode}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>

        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND date_format(t.KPRQ,'%Y-%m-%d') BETWEEN #{kprqq} AND #{kprqz}
        </if>
        AND t.create_date BETWEEN #{cyrqq} AND #{cyrqz}
        ORDER BY t.PDF_MD5 DESC,t.create_date DESC
    </select>

    <select id="selectCheckHistory" parameterType="java.util.Map"
            resultMap="CheckRecordInvoiceMap" databaseId="oracle">
        SELECT t.id,t.FPDM,t.FPHM,to_date(KPRQ,'yyyy-mm-dd') KPRQ
        ,t.GFMC,t.XFMC,t.JE,t.SE,t.JSHJ,t.JYM,t.CYJG_TIP,t.FPZL,t.CYJG_CODE
        FROM t_dx_check_record_invoice t
        WHERE t.check_user = #{userid}
        <if test="invoiceCode != null and invoiceCode != '' and invoiceCode != 'null' ">
            AND t.FPDM = #{invoiceCode}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>
        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND TO_CHAR(TO_DATE(t.KPRQ, 'yyyy-MM-dd'),'yyyy-MM-dd HH24:mm:dd' ) BETWEEN #{kprqq} AND
            #{kprqz}
        </if>
        AND TO_CHAR(t.create_date, 'yyyy-MM-dd HH24:mm:dd') between #{cyrqq} and #{cyrqz}

        ORDER BY t.PDF_MD5 DESC,t.create_date DESC
    </select>

    <select id="selectCheckHistoryHj" parameterType="java.util.Map"
            resultMap="CheckLogHjMap" databaseId="mysql">
        SELECT count(1) hjsl,ifnull(sum(t.JE), 0) hjje,ifnull(sum(t.SE), 0) hjse
        FROM t_dx_check_record_invoice t
        WHERE t.check_user = #{userid}
        <if test="invoiceCode != null and invoiceCode != '' and invoiceCode != 'null' ">
            AND t.FPDM = #{invoiceCode}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>
        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND date_format(t.KPRQ,'%Y-%m-%d') BETWEEN #{kprqq} AND #{kprqz}
        </if>
        AND t.create_date BETWEEN #{cyrqq} AND #{cyrqz}
    </select>
    <select id="selectCheckHistoryHj" parameterType="java.util.Map"
            resultMap="CheckLogHjMap" databaseId="oracle">
        SELECT count(1) hjsl,nvl(sum(t.JE), 0) hjje,nvl(sum(t.SE), 0) hjse
        FROM t_dx_check_record_invoice t
        WHERE t.check_user = #{userid}
        <if test="invoiceCode != null and invoiceCode != '' and invoiceCode != 'null' ">
            AND t.FPDM = #{invoiceCode}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>
        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND TO_CHAR(TO_DATE(t.KPRQ, 'yyyy-MM-dd'),'yyyy-MM-dd HH24:mm:dd' ) BETWEEN #{kprqq} AND
            #{kprqz}
        </if>
        AND TO_CHAR(t.create_date, 'yyyy-MM-dd HH24:mm:dd') between #{cyrqq} and #{cyrqz}
    </select>

    <select id="selectCheckStatisticsCount" parameterType="java.util.Map"
            resultMap="CheckStatisticsMap" databaseId="mysql">
        SELECT a.createdate                        as invoiceYearMonth,
               sum(a.errorSum)                        sumError,
               sum(a.successSum)                      sumSuccess,
               sum(a.errorSum) + sum(a.successSum) as sumCount
        from (
                 SELECT t.createdate,
                        case when handle_code = '0001' then count(handle_code) else 0 end  successSum,
                        case when handle_code != '0001' then count(handle_code) else 0 end errorSum
                 FROM (
                          SELECT DATE_FORMAT(handle_date, "%Y-%m") AS createdate,
                                 handle_code,
                                 MAX(handle_course)                AS handle_course
                          FROM t_dx_check_log
                          where DATE_FORMAT(handle_date, "%Y") = #{checkYear}
                            AND check_user = #{userid}
                          GROUP BY invoice_code,
                                   handle_code,
                                   invoice_no
                      ) t
                 group by createdate, handle_code) a
        GROUP BY a.createdate
        order by a.createdate desc
    </select>
    <select id="selectCheckStatisticsCount" parameterType="java.util.Map"
            resultMap="CheckStatisticsMap" databaseId="oracle">
        SELECT a.createdate                        as invoiceYearMonth,
               sum(a.errorSum)                        sumError,
               sum(a.successSum)                      sumSuccess,
               sum(a.errorSum) + sum(a.successSum) as sumCount
        from (
                 SELECT t.createdate,
                        case when handle_code = '0001' then count(handle_code) else 0 end  successSum,
                        case when handle_code != '0001' then count(handle_code) else 0 end errorSum
                 FROM (
                          SELECT to_char(handle_date, 'yyyy-MM') AS createdate,
                                 handle_code,
                                 MAX(handle_course)              AS handle_course
                          FROM t_dx_check_log
                          where to_char(handle_date, 'yyyy') = #{checkYear}
                            AND check_user = #{userid}
                          GROUP BY handle_date,
                                   handle_code
                      ) t
                 group by createdate, handle_code) a
        GROUP BY a.createdate
        order by a.createdate desc
    </select>

    <select id="selectCheckHistoryDetail" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="mysql">
        SELECT
        t.id,t.invoice_code,t.invoice_no,t.invoice_date,t.buyer_name,t.saler_name,t.invoice_amount,t.tax_amount,t.total_amount,t.check_code,t.check_massege,t.invoice_type,t.handle_code,t.handle_date
        FROM t_dx_check_log t
        WHERE t.check_user = #{userid}
        <if test="invoiceCode != null and invoiceCode != '' and invoiceCode != 'null' ">
            AND t.invoice_code = #{invoiceCode}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.invoice_no = #{invoiceNo}
        </if>
        ORDER BY t.handle_date DESC
    </select>

    <select id="selectCheckHistoryDetail" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="oracle">
        SELECT
        t.id,t.invoice_code,t.invoice_no,t.invoice_date,t.buyer_name,t.saler_name,t.invoice_amount,t.tax_amount,t.total_amount,t.check_code,t.check_massege,t.invoice_type,t.handle_code,t.handle_date
        FROM t_dx_check_log t
        WHERE t.check_user = #{userid}
        <if test="invoiceCode != null and invoiceCode != '' and invoiceCode != 'null' ">
            AND t.invoice_code = #{invoiceCode}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.invoice_no = #{invoiceNo}
        </if>
        ORDER BY t.handle_date DESC
    </select>

    <select id="getCheckList" parameterType="java.util.Map"
            resultMap="CheckRecordInvoiceMap" databaseId="mysql">
        SELECT t.id,t.FPDM,t.FPHM,date_format(t.KPRQ,'%Y-%m-%d')
        KPRQ,t.GFMC,t.XFMC,t.JE,t.SE,t.JSHJ,t.JYM,t.CYJG_TIP,t.FPZL,t.CYJG_CODE,date_format(t.create_date,'%Y-%m-%d')
        create_date,t.ZFBZ,t.check_user,t.scan_id
        FROM t_dx_check_record_invoice t
        WHERE t.CYJG_CODE = '0001'
        <if test="gfsh != null and gfsh.size()>0 ">

            and t.GFSH in
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="company != null and company != '' and company != 'null' ">
            AND t.company = #{company}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>
        <if
                test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '99'">
            AND t.FPZL = #{invoiceType}
        </if>
        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND date_format(t.KPRQ,'%Y-%m-%d') BETWEEN #{kprqq} AND #{kprqz}
        </if>
        AND t.create_date BETWEEN #{cyrqq} AND #{cyrqz}
        ORDER BY t.PDF_MD5 DESC,t.create_date DESC
    </select>

    <select id="getCheckList" parameterType="java.util.Map"
            resultMap="CheckRecordInvoiceMap" databaseId="oracle">
        SELECT t.id,t.FPDM,t.FPHM,to_date(KPRQ,'yyyy-mm-dd') KPRQ
        ,t.GFMC,t.XFMC,t.JE,t.SE,t.JSHJ,t.JYM,t.CYJG_TIP,t.FPZL,t.CYJG_CODE,date_format(t.create_date,'%Y-%m-%d')
        create_date,t.ZFBZ,t.check_user,t.scan_id
        FROM t_dx_check_record_invoice t
        WHERE t.CYJG_CODE = '0001'
        <if test="gfsh != null and gfsh.size()>0 ">

            and t.GFSH in
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="company != null and company != '' and company != 'null' ">
            AND t.company = #{company}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>
        <if
                test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '99'">
            AND t.FPZL = #{invoiceType}
        </if>
        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND TO_CHAR(TO_DATE(t.KPRQ, 'yyyy-MM-dd'),'yyyy-MM-dd HH24:mm:dd' ) BETWEEN #{kprqq} AND
            #{kprqz}
        </if>
        AND TO_CHAR(t.create_date, 'yyyy-MM-dd HH24:mm:dd') between #{cyrqq} and #{cyrqz}

        ORDER BY t.PDF_MD5 DESC,t.create_date DESC
    </select>

    <select id="getCheckListHj" parameterType="java.util.Map"
            resultMap="CheckLogHjMap" databaseId="mysql">
        SELECT count(1) hjsl,ifnull(sum(t.JE), 0) hjje,ifnull(sum(t.SE), 0) hjse
        FROM t_dx_check_record_invoice t
        WHERE t.CYJG_CODE = '0001'
        <if test="gfsh != null and gfsh.size()>0 ">

            and t.GFSH in
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="company != null and company != '' and company != 'null' ">
            AND t.company = #{company}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>
        <if
                test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '99'">
            AND t.FPZL = #{invoiceType}
        </if>
        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND date_format(t.KPRQ,'%Y-%m-%d') BETWEEN #{kprqq} AND #{kprqz}
        </if>
        AND t.create_date BETWEEN #{cyrqq} AND #{cyrqz}
    </select>
    <select id="getCheckListHj" parameterType="java.util.Map"
            resultMap="CheckLogHjMap" databaseId="oracle">
        SELECT count(1) hjsl,nvl(sum(t.JE), 0) hjje,nvl(sum(t.SE), 0) hjse
        FROM t_dx_check_record_invoice t
        WHERE t.CYJG_CODE = '0001'
        <if test="gfsh != null and gfsh.size()>0 ">

            and t.GFSH in
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="company != null and company != '' and company != 'null' ">
            AND t.company = #{company}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            AND t.FPHM = #{invoiceNo}
        </if>
        <if
                test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '99'">
            AND t.FPZL = #{invoiceType}
        </if>
        <if
                test="(kprqq != null and kprqq != '' and kprqq != 'null') and (kprqz != null and kprqz != '' and kprqz != 'null') ">
            AND TO_CHAR(TO_DATE(t.KPRQ, 'yyyy-MM-dd'),'yyyy-MM-dd HH24:mm:dd' ) BETWEEN #{kprqq} AND
            #{kprqz}
        </if>
        AND TO_CHAR(t.create_date, 'yyyy-MM-dd HH24:mm:dd') between #{cyrqq} and #{cyrqz}
    </select>

</mapper>
