<?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.jeeplus.test.reimbursementAssess.mapper.AssessReimbursementSysMapper">
    <sql id="reimbursementColumns">
        a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.parent_id AS "parent.id",
		a.parent_id AS "parentId",
		a.parent_ids AS "parentIds",
        a.reimbursement_batch AS "reimbursementBatch",
        a.invoice_code AS "invoiceCode",
        a.invoice_number AS "invoiceNumber",
        a.firm_name AS "firmName",
        DATE_FORMAT(a.make_time,'%Y-%m-%d') AS "makeTime",
		a.name AS "name",
        a.money AS "money",
        a.tax AS "tax",
        a.income AS "income",
        a.business_code AS "businessCode",
        DATE_FORMAT(a.gathering_time,'%Y-%m-%d') AS "gatheringTime",
        a.proposer AS "proposer",
        a.partner AS "partner",
        a.business_type AS "businessType",
        a.reimbursement_ratio AS "reimbursementRatio",
        a.reimbursement_status AS "reimbursementStatus",
        a.batch_flag as "batchFlag",
        a.year AS "year",
        a.random_type AS "randomType"
    </sql>

    <select id="findList" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        left join zs_reimbursement_assess_info b on a.parent_id = b.id
        <where>
            a.del_flag = 0
            and a.batch_flag = 0
            and (b.business_code is null or b.business_code = '')
            <if test="reimbursement.invoiceNumber != null and reimbursement.invoiceNumber != ''">
                and a.invoice_number like concat('%',#{reimbursement.invoiceNumber},'%')
            </if>
            <if test="reimbursement.proposer != null and reimbursement.proposer != ''">
                and a.proposer like concat('%',#{reimbursement.proposer},'%')
            </if>
            <if test="reimbursement.partner != null and reimbursement.partner != ''">
                and a.partner like concat('%',#{reimbursement.partner},'%')
            </if>
        </where>
    </select>

    <select id="queryAllList" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
            <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_assess_info a
        <where>
            a.del_flag = 0
            <if test="reimbursement.businessCode != null and reimbursement.businessCode != ''">
            and a.business_code like concat('%',#{reimbursement.businessCode},'%')
            </if>
            <if test="reimbursement.idList != null and reimbursement.idList.size>0">
                and a.id in
                <foreach collection="reimbursement.idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by a.business_code asc ,a.sort asc
    </select>

    <select id="getInvoiceList" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
            <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            <if test="invoiceNumber != null and invoiceNumber != ''">
                and a.invoice_number like concat('%',#{invoiceNumber},'%')
            </if>
            <if test="reimbursementBatch != null and reimbursementBatch != ''">
                and a.reimbursement_batch like concat('%',#{reimbursementBatch},'%')
            </if>
            <if test="proposer != null and proposer != ''">
                and a.proposer like concat('%',#{proposer},'%')
            </if>
            <if test="partner != null and partner != ''">
                and a.partner like concat('%',#{partner},'%')
            </if>
            <if test="firmName != null and firmName != ''">
                and a.firm_name like concat('%',#{firmName},'%')
            </if>
            <if test="beginIncome != null and beginIncome != ''">
                and a.income >= #{beginIncome}
            </if>
            <if test="endIncome != null and endIncome != ''">
                and a.income &lt;= #{endIncome}
            </if>
            <if test="beginDate != null">
                and a.make_time >= #{beginDate}
            </if>
            <if test="endDate != null">
                and a.make_time &lt;= #{endDate}
            </if>
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <select id="getInvoiceByIdList" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
            <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            <if test="idList != null and idList.size>0">
                and a.id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <select id="getReceiptInvoiceIncomeByReimbursementId" resultType="java.lang.String">
        SELECT
            ifnull(sum(a.income),0) as "income"
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            and a.parent_id =#{parentId}
            and a.gathering_time is not null
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <select id="getReimbursementRatioByReimbursementId" resultType="java.lang.String">
        SELECT
            a.reimbursement_ratio as "reimbursementRatio"
        FROM zs_reimbursement_assess_info a
        <where>
            a.del_flag = 0
            and a.id =#{id}
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <select id="getInvoiceByParentIdList" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
            <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            <if test="parentIdList.size>0">
                and a.parent_id in
                <foreach collection="parentIdList" item="parentId" separator="," open="(" close=")">
                    #{parentId}
                </foreach>
            </if>
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <select id="selectOldInvoiceNumber" resultType="java.lang.String">
        SELECT
            a.invoice_number as "invoiceNumber"
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            <if test="invoiceNumberList.size>0">
                and a.invoice_number in
                <foreach collection="invoiceNumberList" item="invoiceNumber" separator="," open="(" close=")">
                    #{invoiceNumber}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectOldBusinessCode" resultType="java.lang.String">
        SELECT
            a.business_code
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            <if test="businessCodeList.size>0">
                and a.business_code in
                <foreach collection="businessCodeList" item="businessCode" separator="," open="(" close=")">
                    #{businessCode}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectOldBusinessCodeByInfo" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
            a.id,a.business_code
        FROM zs_reimbursement_assess_info a
        <where>
            a.del_flag = 0
            <if test="businessCodeList.size>0">
                and a.business_code in
                <foreach collection="businessCodeList" item="businessCode" separator="," open="(" close=")">
                    #{businessCode}
                </foreach>
            </if>
            and a.year = #{year}
        </where>
    </select>

    <insert id="insertBusinessCodeList">
        insert into zs_reimbursement_assess_info (
          id,
          create_by,
          create_date,
          update_by,
          update_date,
          remarks,
          del_flag,
          parent_id,
          parent_ids,
          business_code,
          year
        )
        values
        <foreach collection="businessCodeList" item="item" separator=",">
            ( #{item.id},
            #{item.createBy},
            #{item.createDate},
            #{item.updateBy},
            #{item.updateDate},
            #{item.remarks},
            #{item.delFlag},
            #{item.parentId},
            #{item.parentIds},
            #{item.businessCode},
            #{item.year}
            )
        </foreach>
    </insert>

    <insert id="insertInvoiceList">
        insert into zs_reimbursement_invoice_assess_info (
            id,
            create_by,
            create_date,
            update_by,
            update_date,
            remarks,
            del_flag,
            parent_id,
            parent_ids,
            reimbursement_batch,
            invoice_code,
            invoice_number,
            firm_name,
            make_time,
            name,
            money,
            tax,
            income,
            business_code,
            gathering_time,
            proposer,
            partner,
            business_type
        )
        values
        <foreach collection="invoiceInfoList" item="item" separator=",">
            ( #{item.id},
            #{item.createBy},
            #{item.createDate},
            #{item.updateBy},
            #{item.updateDate},
            #{item.remarks},
            #{item.delFlag},
            #{item.parentId},
            #{item.parentIds},
            #{item.reimbursementBatch},
            #{item.invoiceCode},
            #{item.invoiceNumber},
            #{item.firmName},
            #{item.makeTime},
            #{item.name},
            #{item.money},
            #{item.tax},
            #{item.income},
            #{item.businessCode},
            #{item.gatheringTime},
            #{item.proposer},
            #{item.partner},
            #{item.businessType}
            )
        </foreach>
    </insert>

    <select id="queryBusinessById" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_assess_info a
        <where>
            a.del_flag = 0
            and a.id = #{id}
        </where>
    </select>

    <select id="queryBusinessByInvoiceId" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_assess_info a
        <where>
            a.del_flag = 0
            and a.id = (select parent_id from zs_reimbursement_invoice_assess_info where id = #{id})
        </where>
    </select>

    <select id="queryInvoiceById" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            and a.id = #{id}
        </where>
    </select>

    <update id="saveBusiness">
        update zs_reimbursement_assess_info
        set
            update_by = #{updateBy},
            update_date = #{updateDate},
            business_code = #{businessCode}
            <if test="randomBusinessCodeIndex != null and randomBusinessCodeIndex != ''">
                ,random_business_code_index = #{randomBusinessCodeIndex}
            </if>
            <if test="randomType != null and randomType != ''">
                ,random_type = #{randomType}
            </if>
            ,sort = 2
        where
            id = #{id} and del_flag = 0
    </update>

    <update id="saveInvoice">
        update zs_reimbursement_invoice_assess_info
        set
            update_by = #{updateBy},
            update_date = #{updateDate},
            gathering_time = #{gatheringTime}
        where
            id = #{id} and del_flag = 0
    </update>

    <update id="modifyInvoice">
        update zs_reimbursement_invoice_assess_info
        set
            update_by = #{updateBy},
            update_date = #{updateDate}
        <if test="proposer != null and proposer != ''">
            ,proposer = #{proposer}
        </if>
        <if test="partner != null and partner != ''">
            ,partner = #{partner}
        </if>
        where
            id = #{id} and del_flag = 0
    </update>

    <update id="updateById">
        update zs_reimbursement_assess_info
        set
            update_by = #{updateBy},
            update_date = #{updateDate},
            sort = 0
        <if test="batchFlag != null and batchFlag != ''">
            ,batch_flag = #{batchFlag}
        </if>
        where
            id = #{id} and del_flag = 0
    </update>

    <select id="getInfoByBusinessCode" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_assess_info a
        <where>
            a.del_flag = 0
            and a.business_code = #{businessCode}
            and year = #{year}
            and a.random_type = 0
        </where>
        limit 1;
    </select>

    <update id="updateInvoiceList">
        <foreach collection="invoiceInfoList" item="item" index="index" open="" close="" separator=";">
            update zs_reimbursement_invoice_assess_info
            <set >
                    update_by = #{item.updateBy}
                    ,update_date = #{item.updateDate}
                <if test="item.businessCode != null and item.businessCode != ''">
                    ,business_code = #{item.businessCode}
                </if>
                <if test="item.parentId != null and item.parentId != ''">
                    ,parent_id = #{item.parentId}
                </if>
                <if test="item.parentIds != null and item.parentIds != ''">
                    ,parent_ids = #{item.parentIds}
                </if>
                <if test="item.batchFlag != null and item.batchFlag != ''">
                    ,batch_flag = #{item.batchFlag}
                </if>
            </set>
            where id = #{item.id} and del_flag = 0
        </foreach>
    </update>

    <delete id="deleteBusinessById">
        delete from zs_reimbursement_assess_info
        where
            id = #{id}
    </delete>

    <delete id="deleteBusinessByIdList">
        delete from zs_reimbursement_assess_info
        <where>
            <if test="idList != null and idList.size>0">
                and id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </delete>

    <update id="removeBusinessByIds">
        update zs_reimbursement_assess_info
        set
            del_flag = 1
        <where>
            <if test="idList != null and idList.size>0">
                and id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </update>

    <update id="removeBusinessByParentIds">
        update zs_reimbursement_invoice_assess_info
        set
        del_flag = 1
        <where>
            <if test="idList != null and idList.size>0">
                and parent_id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </update>

    <update id="removeInvoiceByIds">
        update zs_reimbursement_invoice_assess_info
        set
        del_flag = 1
        <where>
            <if test="idList != null and idList.size>0">
                and id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </update>

    <select id="getValidBusinessIdByInvoiceId" resultType="java.lang.String">
        select parent_id from zs_reimbursement_invoice_assess_info
        where parent_id = (
        select parent_id from zs_reimbursement_invoice_assess_info
        where id = #{id}
        ) and del_flag = 0 limit 1
    </select>

    <update id="updateReimbursementRatio">
        update zs_reimbursement_assess_info
        set
        reimbursement_ratio = #{reimbursementRatio}
        <where>
            id = #{id}
        </where>
    </update>

    <select id="getGatheringTimeInvoiceList" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            <if test="idList != null and idList.size>0">
                and a.id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
            and gathering_time is not null
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <select id="getNotGatheringTimeInvoice" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            and business_code = (
            select business_code from zs_reimbursement_invoice_assess_info where id = #{businessCode}
            ) and gathering_time is null
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <update id="updateGatheringTime">
        <foreach collection="idList" item="id" index="index" open="" close="" separator=";">
            update zs_reimbursement_invoice_assess_info
            <set >
                update_by = #{reimbursement.updateBy}
                ,update_date = #{reimbursement.updateDate}
                ,gathering_time = #{reimbursement.gatheringTime}
            </set>
            where id = #{id}
        </foreach>
    </update>

    <update id="updateGatheringStatusById">
        update zs_reimbursement_assess_info
        set gathering_status = 1
        where id = #{id}
    </update>



    <select id="getNotGatheringTimeInvoiceBybusinessCodeId" resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        SELECT
        <include refid="reimbursementColumns"/>
        FROM zs_reimbursement_invoice_assess_info a
        <where>
            a.del_flag = 0
            and parent_id = #{businessCodeId}
            and gathering_time is null
        </where>
        order by a.invoice_number asc ,a.sort asc
    </select>

    <select id="getLastRandomBusinessCodeIndex" resultType="java.lang.String">
        select ifnull(max(random_business_code_index),0) as randomBusinessCodeIndex
        from zs_reimbursement_assess_info
        <where>
            year = #{year}
        </where>
    </select>
</mapper>
