<?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="cn.ezhizao.project.business.payment.mapper.BizPaymentMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.payment.domain.BizPayment"/>

    <!--根据主表id，删除 为的是表单保存时，明细统一物理删除后，再统一插入-->
    <delete id="physicallyDelete">
        DELETE FROM
        biz_payment
        WHERE id > 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="paymentId != null">
            AND payment_id = #{paymentId}
        </if>
    </delete>

    <!--带条件的List查询-->
    <select id="getList" resultMap="BaseResultMap">
        <include refid="getListSql"/>
        order by id desc
    </select>

    <select id="getByIds" resultMap="BaseResultMap">
        SELECT * FROM biz_payment
        WHERE id IN <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <update id="removePaymentDetail">
        UPDATE biz_payment_detail set deleted = 1 FROM biz_payment_detail WHERE payment_id IN
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <!--带条件的List查询 数据总条数-->
    <select id="getTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getListSql"/>) AS C
    </select>

    <sql id="getListSql">
        SELECT t1.id, t1.flow_no, t1.form_date, t1.subsidiary_id, t1.subsidiary, t1.subsidiary_name,
        t1.contract_id,
        t1.verify_date,
        t1.verify_comment,
        t1.subsidiary_bank_account
        , t1.subsidiary_bank_name, t1.company_id, case when b.id is null then t1.company_name else b.name end as
        company_name, t1.amount, t1.payment_cause_id, t1.payment_status
        , t1.payment_subject_id, t1.verify_status, t1.accept_account, t1.accept_account_type_id, t1.accept_account_no
        , t1.account_bank, t1.applier_id, t1.remark, t1.actually_date, t1.service_company_name
        , t1.create_time, t1.creator_id, t1.update_time, t1.updater_id, t1.deleted, t1.version
        , (select sum(payment_amount) from biz_payment_detail pd where pd.payment_id = t1.id and pd.deleted = 0)
        sum_amount
        , (select sum(deal_price) from biz_payment_detail pd where pd.payment_id = t1.id and pd.deleted = 0)
        sum_contract_amount
        , t2.name as payment_cause
        , t3.name as payment_subject
        , t5.nick_name as applier_name
        , t4.contract_no as form_no
        FROM biz_payment t1
        left join biz_company b on b.id = t1.company_id
        left join biz_payment_type t2 on t2.id = t1.payment_cause_id
        left join biz_payment_subject t3 on t3.id = t1.payment_subject_id
        left join biz_archive_input t4 on t4.id = t1.contract_id
        left join sys_user t5 on t5.user_id = t1.applier_id
        WHERE t1.deleted = 0
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="tenantId != null">
            AND t1.tenant_id = #{tenantId}
        </if>
        <if test="contractId != null">
            AND t1.contract_id = #{contractId}
        </if>
        <if test="flowNo != null and flowNo != ''">
            AND t1.flow_no = #{flowNo}
        </if>
        <if test="formDate != null and formDate != ''">
            AND DATE_FORMAT(t1.form_date,'%Y-%m-%d') = #{formDate}
        </if>
        <if test="startDate != null and endDate != null">
            AND (
            DATE_FORMAT(t1.form_date,'%Y-%m-%d') &gt;= #{startDate}
            AND
            DATE_FORMAT(t1.form_date,'%Y-%m-%d') &lt;= #{endDate}
            )
        </if>
        <if test="verifyStartDate != null and verifyEndDate != null">
            AND exists (
            select 1 from work_flow_content where master_id = t1.id
            and master_table_name = 'biz_payment'
            and (
            DATE_FORMAT(create_time,'%Y-%m-%d') &gt;= #{verifyStartDate}
            AND
            DATE_FORMAT(create_time,'%Y-%m-%d') &lt;= #{verifyEndDate}
            ))
        </if>
        <if test="subsidiaryId != null">
            AND t1.subsidiary_id = #{subsidiaryId}
        </if>
        <if test="subsidiary != null and subsidiary != ''">
            AND t1.subsidiary = #{subsidiary}
        </if>
        <if test="subsidiaryName != null and subsidiaryName != ''">
            AND t1.subsidiary_name = #{subsidiaryName}
        </if>
        <if test="subsidiaryBankAccount != null and subsidiaryBankAccount != ''">
            AND t1.subsidiary_bank_account = #{subsidiaryBankAccount}
        </if>
        <if test="subsidiaryBankName != null and subsidiaryBankName != ''">
            AND t1.subsidiary_bank_name = #{subsidiaryBankName}
        </if>
        <if test="companyId != null">
            AND t1.company_id = #{companyId}
        </if>
        <if test="companyName != null and companyName != ''">
            AND t1.company_name = #{companyName}
        </if>
        <if test="amount != null">
            AND t1.amount = #{amount}
        </if>
        <if test="paymentCauseId != null">
            AND t1.payment_cause_id = #{paymentCauseId}
        </if>
        <if test="paymentSubjectId != null">
            AND t1.payment_subject_id = #{paymentSubjectId}
        </if>
        <if test="paymentStatus != null">
            AND t1.payment_status = #{paymentStatus}
        </if>
        <if test="verifyStatus != null">
            AND t1.verify_status = #{verifyStatus}
        </if>
        <if test="acceptAccount != null and acceptAccount != ''">
            AND t1.accept_account = #{acceptAccount}
        </if>
        <if test="applierId != null">
            AND t1.applier_id = #{applierId}
        </if>
        <if test="acceptAccountTypeId != null">
            AND t1.accept_account_type_id = #{acceptAccountTypeId}
        </if>
        <if test="acceptAccountNo != null">
            AND t1.accept_account_no = #{acceptAccountNo}
        </if>
        <if test="accountBank != null and accountBank != ''">
            AND t1.account_bank = #{accountBank}
        </if>
        <if test="remark != null and remark != ''">
            AND t1.remark = #{remark}
        </if>
        <if test="createTime != null">
            AND t1.create_time = #{createTime}
        </if>
        <if test="keyword != null">
            AND (t1.company_name like concat('%', #{keyword},'%') or b.name like concat('%', #{keyword},'%'))
        </if>
        <if test="creatorId != null">
            AND t1.creator_id = #{creatorId}
        </if>
        <if test="updateTime != null">
            AND t1.update_time = #{updateTime}
        </if>
        <if test="updaterId != null">
            AND t1.updater_id = #{updaterId}
        </if>
        <if test="deleted != null">
            AND t1.deleted = #{deleted}
        </if>
        <if test="version != null">
            AND t1.version = #{version}
        </if>
    </sql>
</mapper>
