<?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.sign.dao.SignRecordInvoiceDao">
    <resultMap id="BaseResultMap" type="com.dxhy.sign.entity.TDxRecordInvoice">
        <id column="id" property="id"/>
        <result column="invoice_type" property="invoiceType"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_no" property="invoiceNo"/>
        <result column="ele_invoice_no" property="eleInvoiceNo"/>
        <result column="invoice_date" property="invoiceDate"/>
        <result column="gf_name" property="gfName"/>
        <result column="xf_name" property="xfName"/>
        <result column="invoice_amount" property="invoiceAmount"/>
        <result column="tax_amount" property="taxAmount"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="qs_status" property="qsStatus"/>
        <result column="dept_id" property="deptId"/>
        <result column="remark" property="remark"/>
        <result column="qs_date" property="qsDate"/>
        <result column="qs_name" property="qsName"/>
    </resultMap>

    <resultMap id="WqshjResultMap" type="com.dxhy.sign.entity.TDxRecordInvoice">
        <result column="hjsl" property="id"/>
        <result column="hjje" property="invoiceAmount"/>
        <result column="hjse" property="taxAmount"/>
    </resultMap>

    <select id="selectWqsfp" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="mysql">
        select
        t.id,t.invoice_type,t.invoice_code,t.invoice_no,t.ele_invoice_no,t.invoice_date,t.gf_name,t.xf_name,t.invoice_amount,t.tax_amount,t.dept_id,t.qs_status
        from t_dx_record_invoice t
        where t.company = #{company} and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and (t.invoice_type in('01','03','08','14','31','283','161','183','185') or (t.invoice_type ='32' and t.lq_tdyslxDm='08'))
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType == '14' ">
            and (t.invoice_type = '14' or (t.invoice_type = '32' and t.lq_tdyslxDm='08') )
        </if>
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '14' ">
            and t.invoice_type = #{invoiceType}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        <if test="invoiceSource!=null and invoiceSource !='' and invoiceSource!='99'">
            and t.invoice_source = #{invoiceSource}
        </if>
        <if test="ids != null and ids.size()>0">
            and t.id in
            <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and t.qs_status = '0'
        and t.source_system='0'
        and t.invoice_date between #{kprqq} and #{kprqz}
        order by t.invoice_date desc
    </select>


<!--    <select id="selectWqsfpByQsyx" parameterType="java.util.Map"-->
<!--            resultMap="BaseResultMap" databaseId="mysql">-->
<!--        select-->
<!--        t.id,t.invoice_type,t.invoice_code,t.invoice_no,t.invoice_date,t.gf_name,t.xf_name,t.invoice_amount,t.tax_amount,t.dept_id,t.qs_status-->
<!--        from t_dx_record_invoice t-->
<!--        where t.company = #{company}-->
<!--        <if test="ids != null and ids.size()>0">-->
<!--            and t.id in-->
<!--            <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->
<!--        and t.qs_status = '0'-->
<!--        and t.source_system='0'-->
<!--    </select>-->


    <select id="selectWqsfpByQsyx" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="mysql">
        select
        t.id,t.invoice_type,t.invoice_code,t.invoice_no,t.invoice_date,t.total_amount,t.remark,t.qs_date,
        t.gf_name,t.xf_name,t.invoice_amount,t.tax_amount,t.dept_id,t.qs_status,t.qs_name
        from t_dx_record_invoice t left join t_dx_invoice r on t.uuid = r.uuid
        where 1=1
        <if test="ids != null and ids.size()>0">
            and r.id in
            <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
<!--        <if test="company !=null and company != ''">-->
<!--           and t.company = #{company}-->
<!--        </if>-->
<!--        and t.qs_status = '0'-->
<!--        and t.source_system='0'-->
    </select>

    <select id="selectWqsfp" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="oracle">
        select
        t.id,t.invoice_type,t.invoice_code,t.invoice_no,t.ele_invoice_no,t.invoice_date,t.gf_name,t.xf_name,t.invoice_amount,t.tax_amount,t.dept_id,t.qs_status
        from t_dx_record_invoice t
        where t.company = #{company} and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and (t.invoice_type in('01','03','08','14','31','283','161','183','185') or (t.invoice_type ='32' and t.lq_tdyslxDm='08'))
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType == '14' ">
            and (t.invoice_type = '14' or (t.invoice_type = '32' and t.lq_tdyslxDm='08') )
        </if>
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '14' ">
            and t.invoice_type = #{invoiceType}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        and t.qs_status = '0'
        and t.source_system='0'
        AND TO_CHAR(t.invoice_date, 'yyyy-MM-dd HH24:mm:dd') between #{kprqq} and #{kprqz}

        order by t.invoice_date desc
    </select>

    <select id="selectWqsfpHj" parameterType="java.util.Map"
            resultMap="WqshjResultMap" databaseId="mysql">
        select count(1) hjsl,sum(t.invoice_amount)hjje,sum(t.tax_amount)hjse
        from t_dx_record_invoice t
        where t.company = #{company} and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and (t.invoice_type in('01','03','08','14','31','283','161','183','185') or (t.invoice_type ='32' and t.lq_tdyslxDm='08'))
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType == '14' ">
            and (t.invoice_type = '14' or (t.invoice_type = '32' and t.lq_tdyslxDm='08') )
        </if>
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '14' ">
            and t.invoice_type = #{invoiceType}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        <if test="invoiceSource!=null and invoiceSource!='' and invoiceSource!='99'">
            and t.invoice_source = #{invoiceSource}
        </if>
        and t.qs_status = '0'
        and t.source_system='0'
        and t.invoice_date between #{kprqq} and #{kprqz}
    </select>
    <select id="selectWqsfpHj" parameterType="java.util.Map"
            resultMap="WqshjResultMap" databaseId="oracle">
        select count(1) hjsl,sum(t.invoice_amount)hjje,sum(t.tax_amount)hjse
        from t_dx_record_invoice t
        where t.company = #{company} and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and (t.invoice_type in('01','03','08','14','31','283','161','183','185') or (t.invoice_type ='32' and t.lq_tdyslxDm='08'))
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType == '14' ">
            and (t.invoice_type = '14' or (t.invoice_type = '32' and t.lq_tdyslxDm='08') )
        </if>
        <if test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '14' ">
            and t.invoice_type = #{invoiceType}
        </if>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        and t.qs_status = '0'
        and t.source_system='0'
        AND TO_CHAR(t.invoice_date, 'yyyy-MM-dd HH24:mm:dd') between #{kprqq} and #{kprqz}
    </select>

    <delete id="deleteBatchInvoice">
        delete
        from t_dx_invoice
        where scan_id = #{scanId}
    </delete>

    <delete id="deleteBatchImg">
        delete
        from t_dx_invoice_img
        where scan_id = #{scanId}
    </delete>

    <select id="getRules" resultType="com.dxhy.sign.entity.SignRulesEntity">
        select *
        from sys_sign_rules
        where rule_id in (select rule_id from sys_rules_dept where company = #{company})
        order by rule_code
    </select>

    <update id="updateQsxx" databaseId="mysql">
        UPDATE t_dx_record_invoice
        SET qs_status='0',
            qs_date=null,
            qs_name=null,
            qs_type=null,
            settle_source=null,
            settlement_no=null,
            settlement_status='0',
            last_update_date = now()
        WHERE uuid = #{uuid}
    </update>
    <update id="updateQsxx" databaseId="oracle">
        UPDATE t_dx_record_invoice
        SET qs_status='0',
            qs_date=null,
            qs_name=null,
            qs_type=null,
            settle_source=null,
            settlement_no=null,
            settlement_status='0',
            last_update_date = sysdate
        WHERE uuid = #{uuid}
    </update>

    <select id="getUser" resultType="map">
        SELECT user_id  userId,
               username userName
        FROM sys_user
        WHERE company = #{company}

    </select>

    <select id="selectYlqsfp" parameterType="java.util.Map"
            resultType="com.dxhy.sign.model.SgqsWqsfpVO" databaseId="mysql">
        select
        t.id,t.invoice_type,DATE_FORMAT(t.invoice_date,'%Y-%m-%d') as
        invoiceDate,t.invoice_code,t.invoice_no,t.gf_name,t.xf_name,truncate(t.invoice_amount,2) as
        invoiceAmount,truncate(t.tax_amount,2) as taxAmount,DATE_FORMAT(t.qs_date,'%Y-%m-%d %T') as qsDate,t.uuid
        from t_dx_record_invoice t left join t_dx_invoice d on d.uuid = t.uuid
        LEFT JOIN ${dxhyAdmin}.sys_user u on d.user_account = u.user_id
        where u.push_yesorno=1
        and t.company = 'YL' and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        and t.qs_status = '1'
        and t.qs_date between #{qsrqq} and #{qsrqz}
        order by t.qs_date desc
    </select>

    <select id="selectYlqsfpHj" parameterType="java.util.Map"
            resultMap="WqshjResultMap" databaseId="mysql">
        select count(1) hjsl,sum(t.invoice_amount)hjje,sum(t.tax_amount)hjse
        from t_dx_record_invoice t left join t_dx_invoice d on d.uuid = t.uuid
        LEFT JOIN ${dxhyAdmin}.sys_user u on d.user_account = u.user_id
        where u.push_yesorno=1
        and t.company = 'YL' and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        and t.qs_status = '1'
        and t.qs_date between #{qsrqq} and #{qsrqz}
    </select>

    <delete id="deleteInvoiceByid">
        delete
        from t_dx_invoice
        where id = #{Id}
    </delete>
    <select id="selectInvoice" resultType="com.dxhy.sign.entity.TDxInvoice">
        select scan_id as scanId from t_dx_invoice where id = #{Id}
    </select>
</mapper>
