<?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.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
    <resultMap id="get-PaymentEntity-result" type="com.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
        <result property="referenceNum" column="referenceNum" />
        <result property="busType" column="busType" />
        <result property="title" column="title" />
        <result property="amount" column="amount" />
        <result property="currency" column="currency" />
        <result property="payAccount" column="payAccount" jdbcType="VARCHAR" />
        <result property="revAccount" column="revAccount" />
        <result property="revAccountName" column="revAccountName" />
        <result property="revBankType" column="revBankType" />
        <result property="revBankName" column="revBankName" />
        <result property="cnapsCode" column="cnapsCode" />
        <result property="purpose" column="purpose" />
        <result property="personalFlag" column="personalFlag" />
        <result property="tableName" column="tableName" />
        <result property="oaId" column="oaId" />
        <result property="formsonId" column="formsonId" />
        <result property="payCompany" column="payCompany" />
        <result property="billName" column="billName" />
        <result property="payResultField" column="payResultField" />
        <result property="payDateField" column="payDateField" />
        <result property="applyCodeField" column="applyCodeField" />
        <result property="applyCode" column="applyCode" />
        <result property="receiptFiled" column="receiptFiled" />
        <result property="summaryId" column="summaryId" />
        <result property="startDate" column="startDate" />
        <result property="finishedflag" column="finishedflag" />
        <result property="payDate" column="payDate" />
        <result property="payResult" column="payResult" />
        <result property="payBankName" column="payBankName" />
        <result property="receipt" column="receipt" />
        <result property="payType" column="payType" />
        <result property="payCompanyCode" column="payCompanyCode" />
    </resultMap>

    <sql id="PaymentEntity_Base_Column_List">
     oaId,
    formsonId,
     payCompany,
     payCompanyCode,
     title,
     tableName,
     billName,
     referenceNum,
     busType,
     payResultField,
     payDateField,
     applyCodeField,
     receiptFiled,
     summaryId,
     startDate,
     finishedflag,
     payDate,
     payResult,
     applyCode,
     payAccount,
     payBankName,
     amount,
     purpose,
     revAccount,
     revBankName,
     revBankType,
     revAccountName,
     cnapsCode,
     receipt,
     currency,
     personalFlag,
     payType
    </sql>

    <!-- 基础查询语句  -->
    <sql id="base_sql">
        SELECT
            formmain_0209.id as oaId, -- 主表id
            formson_0210.formsonId,
            'formson_0210' as tableName, -- 表名
            COL_SUMMARY.SUBJECT as title, -- 单据标题
            unit.name as payCompany, -- 付款公司
            '差旅费报销单' as billName,
            'field0072' as payResultField, -- 支付结果字段
            'field0073' as payDateField, -- 打款日期字段
            'field0080' AS applyCodeField,-- CBS支付申请单号
            '' as receiptFiled,-- 电子回单字段
            COL_SUMMARY.id as summaryId,
            formmain_0209.field0017||'-'||formson_0210.sort as referenceNum, -- 单据编号
            formmain_0209.START_DATE as startDate, -- 单据日期
            formmain_0209.FINISHEDFLAG as finishedflag, -- 流程状态
            formson_0210.field0073 as payDate, -- 打款日期
            formson_0210.field0072 as payResult, -- 支付结果
            formson_0210.field0080 AS applyCode,-- 支付申请单号
            REGEXP_REPLACE(formmain_0209.field0042, '[[:space:]]', '') as payAccount, -- 付款账户
            REGEXP_REPLACE(formmain_0209.field0041, '[[:space:]]', '') as payBankName, -- 付款开户行
            formson_0210.field0031 as amount, -- 金额
            formmain_0209.field0038 as purpose,  -- 用途
            formmain_0209.field0038 as cbsAbstract, -- 摘要
            REGEXP_REPLACE(formson_0210.field0069, '[[:space:]]', '') as revAccount, -- 收款账户
            formson_0210.field0068 as revBankName, -- 收款开户行
            formson_0210.field0075 as revAccountName, -- 收款人
            REGEXP_REPLACE(formson_0210.field0071, '[[:space:]]', '') as cnapsCode, -- 收款联行号
            item.ENUMVALUE  as personalFlag,-- 公私标记
            formson_0210.field0079 as revBankType,
            '10' as currency,
            '202' as busType,
            '' as receipt -- 电子回单
        FROM
            (
                SELECT
                    WM_CONCAT(id) AS formsonId,
                    formmain_id,
                    SUM(field0031) AS field0031,
                    MIN(sort) AS sort,
                    field0068,
                    field0069,
                    field0071,
                    field0079,
                    field0075,
                    field0070,
                    field0072,
                    field0073,
                    field0080
                FROM
                    formson_0210
                WHERE
                    field0067 = '-5486592002512828355'
                GROUP BY
                    formmain_id,
                    field0068,
                    field0069,
                    field0071,
                    field0079,
                    field0075,
                    field0070,
                    field0072,
                    field0073,
                    field0080
            )formson_0210
                LEFT JOIN formmain_0209 ON formson_0210.FORMMAIN_ID = formmain_0209.id
                LEFT JOIN COL_SUMMARY ON COL_SUMMARY.FORM_RECORDID = formmain_0209.id
                left join CTP_ENUM_ITEM item on item.id =formson_0210.field0070
                left join ORG_UNIT unit on unit.id =formmain_0209.field0002
    </sql>
    <!-- 采用==查询 -->
    <select id="entity_list_base" resultMap="get-PaymentEntity-result" parameterType="com.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
        select v.* from (
        <include refid="base_sql"/>
        ) v
        <trim prefix="where" prefixOverrides="and">
            <if test="referenceNum != null and referenceNum !='' "> v.referenceNum = #{referenceNum} </if>
            <if test="busType != null and busType !='' "> and v.busType = #{busType} </if>
            <if test="amount != null and amount !='' ">and v.amount = #{amount}  </if>
            <if test="currency != null and currency !='' "> and v.currency = #{currency}  </if>
            <if test="payAccount != null and payAccount !='' ">and v.payAccount = #{payAccount} </if>
            <if test="revAccount != null and revAccount !='' "> and v.revAccount = #{revAccount}  </if>
            <if test="revAccountName != null and revAccountName !='' "> and  v.revAccountName = #{revAccountName}  </if>
            <if test="revBankType != null and revBankType !='' "> and v.revBankType = #{revBankType} </if>
            <if test="revBankName != null and revBankName !='' ">and v.revBankName = #{revBankName}  </if>
            <if test="cnapsCode != null and cnapsCode !='' ">and v.cnapsCode = #{cnapsCode}  </if>
            <if test="purpose != null and purpose !='' "> and v.purpose = #{purpose}  </if>
            <if test="personalFlag != null and personalFlag !='' ">and v.personalFlag = #{personalFlag}  </if>
            <if test="tableName != null and tableName !='' "> and v.tableName = #{tableName}  </if>
            <if test="oaId != null and oaId !='' ">and v.oaId = #{oaId} </if>
            <if test="payCompany != null and payCompany !='' "> and v.payCompany = #{payCompany}  </if>
            <if test="payCompanyCode != null and payCompanyCode !='' "> and v.payCompanyCode = #{payCompanyCode}  </if>
            <if test="title != null and title !='' "> and  v.title = #{title}  </if>
            <if test="billName != null and billName !='' "> and v.billName = #{billName} </if>
            <if test="payResult != null and payResult !='' ">and v.payResult = #{payResult}  </if>
            <if test="applyCode != null and applyCode !='' ">and v.applyCode = #{applyCode}  </if>
            <if test="payBankName != null and payBankName !='' ">and v.payBankName = #{payBankName}  </if>
            <if test="payType != null and payType !='' "> and v.payType = #{payType}  </if>
            <if test="finishedflag != null and finishedflag !='' "> and v.finishedflag = #{finishedflag}  </if>

        </trim>
    </select>

    <!-- 查询交易成功，且电子回单为空的，上传电子回单用 -->
    <select id="PaymentEntity_list_base_elec_isnull" resultMap="get-PaymentEntity-result" parameterType="com.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
       <!-- select
        <include refid="PaymentEntity_Base_Column_List"/>
        from
        v_hzya_oa_cbs_all-->
        -- 差旅费报销单
        SELECT
        formson_0210.id as oaId, -- 主表id
        'formson_0210' as tableName, -- 表名
        COL_SUMMARY.SUBJECT as title, -- 单据标题
        unit.name as payCompany, -- 付款公司
        '差旅费报销单' as billName,
        'field0072' as payResultField, -- 支付结果字段
        'field0073' as payDateField, -- 打款日期字段
        '' as receiptFiled,-- 电子回单字段
        COL_SUMMARY.id as summaryId,
        formmain_0209.field0017||'-'||formson_0210.sort as referenceNum, -- 单据编号
        formmain_0209.START_DATE as startDate, -- 单据日期
        formmain_0209.FINISHEDFLAG as finishedflag, -- 流程状态
        formson_0210.field0073 as payDate, -- 打款日期
        formson_0210.field0072 as payResult, -- 支付结果
        REGEXP_REPLACE(formmain_0209.field0042, '[[:space:]]', '') as payAccount, -- 付款账户
        REGEXP_REPLACE(formmain_0209.field0041, '[[:space:]]', '') as payBankName, -- 付款开户行
        formson_0210.field0031 as amount, -- 金额
        formmain_0209.field0038 as purpose,  -- 用途
        formmain_0209.field0038 as cbsAbstract, -- 摘要
        REGEXP_REPLACE(formson_0210.field0069, '[[:space:]]', '') as revAccount, -- 收款账户
        formson_0210.field0068 as revBankName, -- 收款开户行
        '' as revAccountName, -- 收款人
        REGEXP_REPLACE(formson_0210.field0071, '[[:space:]]', '') as cnapsCode, -- 收款联行号
        item.showvalue as personalFlag,-- 公私标记
        '' as revBankType,-- 收款银行类型
        '10' as currency,-- 币种
        '' as busType,-- 业务类型
        '' as receipt -- 电子回单
        from
        formson_0210
        LEFT JOIN formmain_0209 ON formson_0210.FORMMAIN_ID = formmain_0209.id
        LEFT JOIN COL_SUMMARY ON COL_SUMMARY.FORM_RECORDID = formmain_0209.id
        LEFT JOIN CTP_AFFAIR ON CTP_AFFAIR.object_id = COL_SUMMARY.id
        left join CTP_ENUM_ITEM item on item.id =formson_0210.field0070
        left join ORG_UNIT unit on unit.id =formmain_0209.field0002
        -- left join V_USER_VIEW_ALL us on us.staffid=formmain_0209.field0024
        WHERE 1=1
        -- and formson_0210.field0031>0
        and CTP_AFFAIR.node_name = '发起者' and CTP_AFFAIR.COMPLETE_TIME is null and CTP_AFFAIR.STATE = 3
        <trim prefix="where" prefixOverrides="and">
            <if test="referenceNum != null and referenceNum !='' "> referenceNum = #{referenceNum} </if>
            <if test="busType != null and busType !='' "> and busType = #{busType} </if>
            <if test="amount != null and amount !='' ">and amount = #{amount}  </if>
            <if test="currency != null and currency !='' "> and currency = #{currency}  </if>
            <if test="payAccount != null and payAccount !='' ">and payAccount = #{payAccount} </if>
            <if test="revAccount != null and revAccount !='' "> and revAccount = #{revAccount}  </if>
            <if test="revAccountName != null and revAccountName !='' "> and  revAccountName = #{revAccountName}  </if>
            <if test="revBankType != null and revBankType !='' "> and revBankType = #{revBankType} </if>
            <if test="revBankName != null and revBankName !='' ">and revBankName = #{revBankName}  </if>
            <if test="cnapsCode != null and cnapsCode !='' ">and cnapsCode = #{cnapsCode}  </if>
            <if test="purpose != null and purpose !='' "> and purpose = #{purpose}  </if>
            <if test="personalFlag != null and personalFlag !='' ">and personalFlag = #{personalFlag}  </if>
            <if test="tableName != null and tableName !='' "> and tableName = #{tableName}  </if>
            <if test="oaId != null and oaId !='' ">and oaId = #{oaId} </if>
            <if test="payCompany != null and payCompany !='' "> and payCompany = #{payCompany}  </if>
            <if test="payCompanyCode != null and payCompanyCode !='' "> and payCompanyCode = #{payCompanyCode}  </if>
            <if test="title != null and title !='' "> and  title = #{title}  </if>
            <if test="billName != null and billName !='' "> and billName = #{billName} </if>
            <if test="payBankName != null and payBankName !='' ">and payBankName = #{payBankName}  </if>
            <if test="payType != null and payType !='' "> and payType = #{payType}  </if>
            and receipt is null and personalFlag='0' and payResult = '支付成功'
        </trim>
    </select>
    <!-- 查询待支付的 -->
    <select id="PaymentEntity_list_base_unpaid" resultMap="get-PaymentEntity-result" parameterType="com.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
     select v.* from (
        <include refid="base_sql"/>
        ) v
        <trim prefix="where" prefixOverrides="and">
            <if test="referenceNum != null and referenceNum !='' "> v.referenceNum = #{referenceNum} </if>
            <if test="busType != null and busType !='' "> and v.busType = #{busType} </if>
            <if test="amount != null and amount !='' ">and v.amount = #{amount}  </if>
            <if test="currency != null and currency !='' "> and v.currency = #{currency}  </if>
            <if test="payAccount != null and payAccount !='' ">and v.payAccount = #{payAccount} </if>
            <if test="revAccount != null and revAccount !='' "> and v.revAccount = #{revAccount}  </if>
            <if test="revAccountName != null and revAccountName !='' "> and  v.revAccountName = #{revAccountName}  </if>
            <if test="revBankType != null and revBankType !='' "> and v.revBankType = #{revBankType} </if>
            <if test="revBankName != null and revBankName !='' ">and v.revBankName = #{revBankName}  </if>
            <if test="cnapsCode != null and cnapsCode !='' ">and v.cnapsCode = #{cnapsCode}  </if>
            <if test="purpose != null and purpose !='' "> and v.purpose = #{purpose}  </if>
            <if test="personalFlag != null and personalFlag !='' ">and v.personalFlag = #{personalFlag}  </if>
            <if test="tableName != null and tableName !='' "> and v.tableName = #{tableName}  </if>
            <if test="oaId != null and oaId !='' ">and v.oaId = #{oaId} </if>
            <if test="payCompany != null and payCompany !='' "> and v.payCompany = #{payCompany}  </if>
            <if test="payCompanyCode != null and payCompanyCode !='' "> and v.payCompanyCode = #{payCompanyCode}  </if>
            <if test="title != null and title !='' "> and  v.title = #{title}  </if>
            <if test="billName != null and billName !='' "> and v.billName = #{billName} </if>
            <if test="payResult != null and payResult !='' ">and v.payResult = #{payResult}  </if>
            <if test="payBankName != null and payBankName !='' ">and v.payBankName = #{payBankName}  </if>
            <if test="payType != null and payType !='' "> and v.payType = #{payType}  </if>
            <if test="finishedflag != null and finishedflag !='' "> and v.finishedflag = #{finishedflag}  </if>
            and v.amount > 0
            and v.payResult is null
        </trim>
    </select>





    <!-- 查询未完成的 采用==查询 -->
    <select id="PaymentEntity_list_base_in_payment" resultMap="get-PaymentEntity-result" parameterType="com.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
        select
        <include refid="PaymentEntity_Base_Column_List"/>
        from
        v_hzya_oa_cbs
        <trim prefix="where" prefixOverrides="and">
            <if test="referenceNum != null and referenceNum !='' "> referenceNum = #{referenceNum} </if>
            <if test="busType != null and busType !='' "> and busType = #{busType} </if>
            <if test="amount != null and amount !='' ">and amount = #{amount}  </if>
            <if test="currency != null and currency !='' "> and currency = #{currency}  </if>
            <if test="payAccount != null and payAccount !='' ">and payAccount = #{payAccount} </if>
            <if test="revAccount != null and revAccount !='' "> and revAccount = #{revAccount}  </if>
            <if test="revAccountName != null and revAccountName !='' "> and  revAccountName = #{revAccountName}  </if>
            <if test="revBankType != null and revBankType !='' "> and revBankType = #{revBankType} </if>
            <if test="revBankName != null and revBankName !='' ">and revBankName = #{revBankName}  </if>
            <if test="cnapsCode != null and cnapsCode !='' ">and cnapsCode = #{cnapsCode}  </if>
            <if test="purpose != null and purpose !='' "> and purpose = #{purpose}  </if>
            <if test="personalFlag != null and personalFlag !='' ">and personalFlag = #{personalFlag}  </if>
            <if test="tableName != null and tableName !='' "> and tableName = #{tableName}  </if>
            <if test="oaId != null and oaId !='' ">and oaId = #{oaId} </if>
            <if test="payCompany != null and payCompany !='' "> and payCompany = #{payCompany}  </if>
            <if test="payCompanyCode != null and payCompanyCode !='' "> and payCompanyCode = #{payCompanyCode}  </if>
            <if test="title != null and title !='' "> and  title = #{title}  </if>
            <if test="billName != null and billName !='' "> and billName = #{billName} </if>
            <if test="payResult != null and payResult !='' ">and payResult = #{payResult}  </if>
            <if test="payBankName != null and payBankName !='' ">and payBankName = #{payBankName}  </if>
            <if test="payType != null and payType !='' "> and payType = #{payType}  </if>
            and (payResult='支付中'
            or payResult not in ('审批撤销','审批拒绝','处理失败','退票','支付成功','取消支付','修改支付','支付失败'))
            and personalFlag='0'
        </trim>
    </select>




    <!--修改视图支付状态-->
    <update id="PaymentEntity_update_payState" parameterType="com.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
        update ${tableName} set
        <trim suffix="" suffixOverrides=",">
            <if test="payDate != null and payDate !='' ">${payDateField} =#{payDate},</if>
            <if test="payResult != null and payResult !='' ">${payResultField} =#{payResult},</if>
            <if test="applyCodeField != null and applyCodeField !='' ">${applyCodeField} =#{applyCode}</if>
        </trim>
        where id = #{formsonId}
    </update>

    <!--修改电子回单-->
    <update id="PaymentEntity_update_electronic" parameterType="com.hzya.frame.seeyon.cbs8.entity.PaymentEntity">
        update ${tableName} set ${receiptFiled}=#{receipt} where id = #{oaId} and ${receiptFiled} is null
    </update>

</mapper>
