<?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.core.job.dao.ApplyStatDao">
    <!-- 获取需要申请统计的数据 -->
    <select id="getApplyData" resultType="com.dxhy.core.job.entity.TDxDkCount">
        select *
        from t_dx_dk_count
        where (tj_status = '1' and apply_status = '0')
           or (tj_status = '2' and apply_type = '1' and apply_status = '0' and cxtj_status = '1')
    </select>
    <!-- 插入已经申请的记录 -->
    <insert id="insertApplyRecord" parameterType="java.lang.String" databaseId="mysql">
        insert into t_dx_apply_stat_record (batch_no, taxno, apply_type, result_status, create_date, skssq,
                                            interface_type)
        values (#{batchNo}, #{buyerTaxNo}, #{statisticSymbol}, 'N', now(), #{skssq}, #{interfaceType})
    </insert>
    <insert id="insertApplyRecord" parameterType="java.lang.String" databaseId="oracle">
        insert into t_dx_apply_stat_record (batch_no, taxno, apply_type, result_status, create_date, skssq,
                                            interface_type)
        values (#{batchNo}, #{buyerTaxNo}, #{statisticSymbol}, 'N', sysdate, #{skssq}, #{interfaceType})
    </insert>
    <!-- 获取统计表当前属期的所有信息 -->
    <select id="getAllOrg" resultType="com.dxhy.core.job.entity.TDxDkCount">
        select id, taxno, taxname, skssq
        from t_dx_dk_count
        where sfdqskssq = '1'
    </select>
    <!-- 更新申请统计表中的状态  更细为申请中 -->
    <update id="updateApplyStatus" parameterType="com.dxhy.core.job.entity.TDxDkCount">
        update t_dx_dk_count
        set apply_status='1'
        where taxno = #{taxno}
          and skssq = #{skssq}
    </update>
    <!-- 查询处于申请中的记录 -->
    <select id="getApplyRecord" resultType="com.dxhy.core.job.pojo.ApplyStatRecord">
        select id, batch_no, taxno, apply_type, result_status, skssq
        from t_dx_apply_stat_record
        where result_status = 'N'
          and interface_type = '0'
    </select>
    <!-- 更新申请记录的状态 -->
    <update id="updateApplyRecord" databaseId="mysql">
        update t_dx_apply_stat_record
        set result_status='Y',
            update_date  = now()
        where id = #{id}
    </update>
    <update id="updateApplyRecord" databaseId="oracle">
        update t_dx_apply_stat_record
        set result_status='Y',
            update_date  = sysdate
        where id = #{id}
    </update>
    <!-- 更新申请表的状态 失败记录  申请统计-->
    <update id="updateFailStatus">
        update t_dx_dk_count
        set apply_msg=#{result.failReason},
            tj_status=#{result.status}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>
    <update id="updateCxCount">
        update t_dx_dk_count
        set apply_status='0',
            apply_msg=null,
            tj_status='0',
            qs_status='0',
            apply_type=null,
            tj_date=null,
            tj_user=null,
            qr_user=null,
            qr_date=null,
            cxtj_status='0',
            cxqs_status='0'
        where taxno = #{taxno}
          and skssq = #{skssq}
    </update>
    <!-- 更新申请表的状态 失败记录  撤销统计-->
    <update id="updateRebackFailStatus">
        update t_dx_dk_count
        set apply_msg=#{result.failReason},
            cxtj_status=#{result.status}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>


    <!-- 更新申请表的状态 成功记录-->
    <update id="updateSuccessStatus">
        update t_dx_dk_count
        set tj_status=#{result.status},
            tj_date=#{result.statisticTime}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>
    <!-- 更新统计信息-->
    <update id="updateCountMsg">
        update t_dx_dk_count set tj_status=#{result.tjStatus},tj_date=#{result.tjDate}
        <if test="result.qsStatus !=null and result.qsStatus != ''">
            ,qs_status = #{result.qsStatus},qr_date = #{result.qrDate}
        </if>
        <if test="result.cxtjStatus != null and result.cxtjStatus != ''">
            ,cxtj_status = #{result.cxtjStatus}
        </if>
        <if test="result.cxqsStatus != null and result.cxqsStatus != ''">
            ,cxqs_status = #{result.cxqsStatus}
        </if>
        where taxno=#{result.taxno} and skssq=#{skssq}
    </update>
    <!-- 新增统计信息-->
    <update id="insertCountMsg">
        insert into t_dx_dk_count (taxno, taxname, skssq, create_date, sfdqskssq)
        set tj_status=#{result.tjStatus},
            tj_date=#{result.tjDate} where taxno=#{result.taxno} and skssq=#{skssq}
    </update>
    <!-- 更新统计确认信息-->
    <update id="updateConfirmMsg">
        update t_dx_dk_count set
        tj_status=#{result.tjStatus},tj_date=#{result.tjDate},qs_status=#{result.qsStatus},qr_date=#{result.qrDate}
        <if test="result.cxtjStatus != null and result.cxtjStatus != ''">
            ,cxtj_status = #{result.cxtjStatus}
        </if>
        <if test="result.cxqsStatus != null and result.cxqsStatus != ''">
            ,cxqs_status = #{result.cxqsStatus}
        </if>
        where taxno=#{result.taxno} and skssq=#{skssq}
    </update>
    <!-- 更新申请撤销统计返回结果 更新记录 -->
    <update id="updateCxtjSuccess">
        update t_dx_dk_count
        set tj_status='0',
            qs_status='0',
            cxtj_status=#{result.status},
            tj_date=null,
            tj_user=null,
            cxtj_date=#{result.qrDate}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>

    <!-- 撤销统计返回成功 删除统计明细 -->
    <delete id="deleteDetail">
        delete
        from t_dx_dk_count_detail
        where taxno = #{result.buyerTaxNo}
          and tj_date = #{result.statisticTime}
    </delete>
    <delete id="deleteDetailBySkssq">
        delete
        from t_dx_dk_count_detail
        where taxno = #{result.buyerTaxNo}
          and tj_month = #{skssq}
    </delete>
    <!-- 插入申请统计结果明细数据 -->
    <insert id="insertDkDetail">
        insert into t_dx_dk_count_detail
        (taxno,tj_month,tj_date,invoice_type,dk_invoice_count,dk_amount_count,dk_tax_amount_count,bdk_invoice_count,bdk_amount_count,bdk_tax_amount_count)
        values
        <foreach item="info" collection="reslist" separator=",">
            (
            #{result.buyerTaxNo},#{result.statisticMonth},#{result.statisticTime},
            #{info.invoiceType},#{info.dkInvoiceNum},#{info.dkTotalAmount},#{info.totalYxAmount},
            #{info.bdkInvoiceNum},#{info.bdkTotalAmount},#{info.bdkTotalYxAmount}
            )
        </foreach>

    </insert>
    <!-- 获取需要申请确认的数据 -->
    <select id="getApplyConfirmData" resultType="com.dxhy.core.job.entity.TDxDkCount">
        select *
        from t_dx_dk_count
        where (qs_status = '1' and tj_status = '2' and apply_status = '0')
           or (qs_status = '2' and apply_type = '1' and apply_status = '0' and cxqs_status = '1')
    </select>

    <!-- 更新申请统计表中的确认签字状态  更细为申请中 -->
    <update id="updateApplyConfrimStatus" parameterType="com.dxhy.core.job.entity.TDxDkCount">
        update t_dx_dk_count
        set apply_status='1'
        where taxno = #{taxno}
          and skssq = #{skssq}
    </update>

    <!-- 获取确认接口  申请中的数据 -->
    <select id="getApplyConfirmRecord" resultType="com.dxhy.core.job.pojo.ApplyStatRecord">
        select id, batch_no, taxno, apply_type, result_status, skssq
        from t_dx_apply_stat_record
        where result_status = 'N'
          and interface_type = '1'
    </select>


    <!-- 确认接口  更新申请表的状态 成功记录-->
    <update id="updateConfirmSuccessStatus">
        update t_dx_dk_count
        set qs_status=#{result.status},
            qr_date=#{result.qrDate}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>
    <update id="updateCxConfirmSuccessStatus">
        update t_dx_dk_count
        set cxqs_status=#{result.status},
            qs_status='0',
            qr_user=null,
            qr_date=null,
            cxqr_date=#{result.qrDate}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>
    <!-- 更新申请表的状态 失败记录  确认结果获取-->
    <update id="updateComfirmFailStatus">
        update t_dx_dk_count
        set apply_msg=#{result.failReason},
            qs_status=#{result.status}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>
    <!-- 更新撤销确认结果返回 -->
    <update id="updateCxComfirmFailStatus">
        update t_dx_dk_count
        set apply_msg=#{result.failReason},
            cxqs_status=#{result.status}
        where taxno = #{result.buyerTaxNo}
          and skssq = #{skssq}
    </update>

    <update id="updatePwd" parameterType="com.dxhy.core.job.pojo.TaxCurrent">
        update t_dx_tax_current
        set bussi_pwd=#{bussiPwd},
            new_bussi_pwd=#{newBussiPwd}
        where taxno = #{curTaxno}
    </update>

    <update id="updateYwmm" parameterType="com.dxhy.core.job.entity.TAcOrg">
        update sys_dept
        set password=#{password}
        where taxno = #{taxno}
          and org_type = '5'
    </update>

    <update id="resetPwd" parameterType="com.dxhy.core.job.pojo.TaxCurrent">
        update t_dx_tax_current
        set bussi_pwd=null,
            new_bussi_pwd=null
        where taxno = #{curTaxno}
    </update>

    <update id="resetYwmm" parameterType="com.dxhy.core.job.entity.TAcOrg">
        update sys_dept
        set password=null
        where taxno = #{taxno}
          and org_type = '5'
    </update>
</mapper>
