<?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.base.dao.BaseYqsqDao">

    <resultMap id="YqsqResultMap" type="com.dxhy.base.model.BaseYqsqRespVO">
        <result column="id" property="id"/>
        <result column="fpdm" property="fpdm"/>
        <result column="fphm" property="fphm"/>
        <result column="invoiceType" property="invoiceType"/>
        <result column="kprq" property="kprq"/>
        <result column="gfmc" property="gfmc"/>
        <result column="gfsh" property="gfsh"/>
        <result column="xfsh" property="xfsh"/>
        <result column="je" property="je"/>
        <result column="se" property="se"/>
        <result column="shzt" property="shzt"/>
        <result column="rzzt" property="rzzt"/>
        <result column="fpzt" property="fpzt"/>
        <result column="lrsj" property="lrsj"/>
        <result column="shsj" property="shsj"/>
    </resultMap>
    <select id="selectByYqsqlb" parameterType="java.util.Map"
            resultMap="YqsqResultMap" databaseId="mysql">
        select t.id id,t.invoice_code fpdm,t.invoice_no fphm,date_format(t.invoice_date,'%Y-%m-%d') kprq,t.gf_name
        gfmc,t.gf_tax_no gfsh,t.xf_tax_no xfsh,t.invoice_amount je,t.tax_amount se,
        t.yqshzt shzt,t.rzh_yesorno rzzt,t.invoice_status fpzt from t_dx_record_invoice t
        where
        t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="fpdm != null and fpdm != '' and fpdm != 'null' ">
            and t.invoice_code = #{fpdm}
        </if>

        <if test="fphm != null and fphm != '' and fphm != 'null' ">
            and t.invoice_no = #{fphm}
        </if>

        <if test="je != null and je != '' and je != 'null' ">
            and t.invoice_amount = #{je}
        </if>
        <if test="kprq != null and kprq != '' and kprq != 'null' ">
            and t.invoice_date = #{kprq}
        </if>
        <if test="se != null and se != '' and se != 'null' ">
            and t.tax_amount = #{se}
        </if>
        <if test="xfsh != null and xfsh != '' and xfsh != 'null' ">
            and t.xf_tax_no = #{xfsh}
        </if>
        order by t.invoice_code,t.invoice_no,t.create_date desc
    </select>
    <select id="selectByYqsqlb" parameterType="java.util.Map"
            resultMap="YqsqResultMap" databaseId="oracle">
        select t.id id,t.invoice_code fpdm,t.invoice_no fphm,to_char(t.invoice_date,'yyyy-mm-dd') kprq,t.gf_name
        gfmc,t.gf_tax_no gfsh,t.xf_tax_no xfsh,t.invoice_amount je,t.tax_amount se,
        t.yqshzt shzt,t.rzh_yesorno rzzt,t.invoice_status fpzt from t_dx_record_invoice t
        where
        t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="fpdm != null and fpdm != '' and fpdm != 'null' ">
            and t.invoice_code = #{fpdm}
        </if>

        <if test="fphm != null and fphm != '' and fphm != 'null' ">
            and t.invoice_no = #{fphm}
        </if>

        <if test="je != null and je != '' and je != 'null' ">
            and t.invoice_amount = #{je}
        </if>
        <if test="kprq != null and kprq != '' and kprq != 'null' ">
            and to_char(t.invoice_date,'yyyy-mm-dd') = #{kprq}
        </if>
        <if test="se != null and se != '' and se != 'null' ">
            and t.tax_amount = #{se}
        </if>
        <if test="xfsh != null and xfsh != '' and xfsh != 'null' ">
            and t.xf_tax_no = #{xfsh}
        </if>
        order by t.invoice_code,t.invoice_no,t.create_date desc
    </select>
    <select id="selectByCxtj" parameterType="java.util.Map"
            resultMap="YqsqResultMap" databaseId="mysql">
        select t.id id,t.invoice_code fpdm,t.invoice_no fphm,t.invoice_type
        invoiceType,DATE_FORMAT(t.invoice_date,"%Y-%m-%d") kprq,t.gf_name gfmc,t.gf_tax_no gfsh,t.xf_tax_no
        xfsh,t.invoice_amount je,t.tax_amount se,
        t.yqsq_status shzt,t.rzh_yesorno rzzt,t.invoice_status fpzt , date_format(t.yqsq_date,'%Y-%m-%d')
        lrsj,date_format(t.yqsh_date,'%Y-%m-%d') shsj
        from t_dx_record_invoice t left join t_dx_tax_current c on t.gf_tax_no = c.taxno
        where t.yqsq_status in ('1','2','4','5')
        and t.invoice_type in ('01','03','14')
        and t.yq_current_period = c.current_tax_period
        and date_format(t.yqsq_date,'%Y-%m-%d') between #{kprqq} and #{kprqz}
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="fplx != null and fplx != '' and fplx != 'null' ">
            and t.invoice_type = #{fplx}
        </if>

        <if test="fphm != null and fphm != '' and fphm != 'null' ">
            and t.invoice_no = #{fphm}
        </if>

        <if test="xfmc != null and xfmc != '' and xfmc != 'null' ">
            and t.xf_name like concat(#{xfmc},'%')
        </if>
        order by t.yqsq_date desc,t.yqsq_status asc
    </select>

    <select id="selectByCxtj" parameterType="java.util.Map"
            resultMap="YqsqResultMap" databaseId="oracle">
        select t.id id,t.invoice_code fpdm,t.invoice_no fphm,to_char(t.invoice_date,'yyyy-mm-dd') kprq,t.gf_name
        gfmc,t.gf_tax_no gfsh,t.xf_tax_no xfsh,t.invoice_amount je,t.tax_amount se,
        t.yqshzt shzt,t.rzh_yesorno rzzt,t.invoice_status fpzt , to_char(t.yqsq_date,'yyyy-MM-dd')
        lrsj,to_char(t.yqsh_date,'yyyy-MM-dd') shsj
        from t_dx_record_invoice t left join t_dx_tax_current c on t.gf_tax_no = c.taxno
        where t.yqsq_status in ('1','2','4','5')
        and t.invoice_type in ('01','03','14')
        and t.yq_current_period = c.current_tax_period
        and t.yqsq_date between to_date(#{kprqq},'yyyy-mm-dd hh24:mi:ss') and to_date(#{kprqz},'yyyy-mm-dd hh24:mi:ss')
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="fplx != null and fplx != '' and fplx != 'null' ">
            and t.invoice_type = #{fplx}
        </if>

        <if test="fphm != null and fphm != '' and fphm != 'null' ">
            and t.invoice_no = #{fphm}
        </if>

        <if test="xfsh != null and xfsh != '' and xfsh != 'null' ">
            and t.xf_tax_no = #{xfsh}
        </if>
        order by t.yqsq_date desc,t.yqsq_status asc
    </select>
    <select id="selectHjByCxtj" parameterType="java.util.Map"
            resultType="com.dxhy.base.model.BaseYqsqRespVO" databaseId="mysql">
        select count(1) xh,sum(t.invoice_amount) je ,sum(t.tax_amount) se
        from t_dx_record_invoice t left join t_dx_tax_current c on t.gf_tax_no = c.taxno
        where t.yqsq_status in ('1','2','4','5')
        and t.invoice_type in ('01','03','14')
        and t.yq_current_period = c.current_tax_period
        and date_format(t.yqsq_date,'%Y-%m-%d') between #{kprqq} and #{kprqz}
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="fplx != null and fplx != '' and fplx != 'null' ">
            and t.invoice_type = #{fplx}
        </if>

        <if test="fphm != null and fphm != '' and fphm != 'null' ">
            and t.invoice_no = #{fphm}
        </if>

        <if test="xfmc != null and xfmc != '' and xfmc != 'null' ">
            and t.xf_name like concat(#{xfmc},'%')
        </if>
        order by t.invoice_code,t.invoice_no,t.create_date desc
    </select>

    <select id="selectHjByCxtj" parameterType="java.util.Map"
            resultType="com.dxhy.base.model.BaseYqsqRespVO" databaseId="oracle">
        select count(1) xh,sum(t.invoice_amount) je ,sum(t.tax_amount) se
        from t_dx_record_invoice t left join t_dx_tax_current c on t.gf_tax_no = c.taxno
        where t.yqsq_status in ('1','2','4','5')
        and t.invoice_type in ('01','03','14')
        and t.yq_current_period = c.current_tax_period
        and t.yqsq_date between to_date(#{kprqq},'yyyy-mm-dd hh24:mi:ss') and to_date(#{kprqz},'yyyy-mm-dd hh24:mi:ss')
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="fplx != null and fplx != '' and fplx != 'null' ">
            and t.invoice_type = #{fplx}
        </if>

        <if test="fphm != null and fphm != '' and fphm != 'null' ">
            and t.invoice_no = #{fphm}
        </if>

        <if test="xfsh != null and xfsh != '' and xfsh != 'null' ">
            and t.xf_tax_no = #{xfsh}
        </if>
        order by t.invoice_code,t.invoice_no,t.create_date desc
    </select>


    <update id="updateByUuid" parameterType="java.util.Map" databaseId="mysql">
        update t_dx_record_invoice t
        set t.yqsq_status      = '1',
            t.rzlx='5',
            t.yq_current_period=#{skssq},
            t.yqsq_date=now()
        where t.uuid = #{uuid}
    </update>
    <update id="updateByUuid" parameterType="java.util.Map" databaseId="oracle">
        update t_dx_record_invoice t
        set t.yqsq_status      = '1',
            t.rzlx='5',
            t.yq_current_period=#{skssq},
            t.yqsq_date=sysdate
        where t.uuid = #{uuid}
    </update>

    <insert id="insertByFpxx" parameterType="java.util.Map">
        INSERT INTO t_dx_record_invoice (uuid, invoice_code, invoice_no, invoice_amount, invoice_date, tax_amount,
                                         xf_tax_no, gf_tax_no, rzlx, yqsq_status, yq_current_period) value (#{uuid},
                                                                                                            #{fpdm},
                                                                                                            #{fphm},
                                                                                                            #{je},
                                                                                                            #{kprq},
                                                                                                            #{se},
                                                                                                            #{xfsh},
                                                                                                            #{gfsbh},
                                                                                                            '5', '1',
                                                                                                            #{skssq})
    </insert>
</mapper>
