<?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.dayou.mapper.DyoaRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.DyoaRecord">
        <result column="id" property="id" />
        <result column="modified" property="modified" />
        <result column="created" property="created" />
        <result column="deleted" property="deleted" />
        <result column="mbs_type" property="mbsType" />
        <result column="mbs_id" property="mbsId" />
        <result column="dyoa_id" property="dyoaId" />
        <result column="user_id" property="userId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        modified,
        created,
        deleted,
        mbs_type, mbs_id, dyoa_id, user_id
    </sql>

    <select id="getMaxOrderTime" resultType="java.util.Date">
        select max(order_datetime) from dyoa_record where mbs_type = 'PERSONAL_ORDER'
    </select>

    <select id="getMaxPersonalTargetTime" resultType="java.util.Date">
        select max(order_datetime) from dyoa_record where mbs_type = 'PERSONAL_TARGET'
    </select>

    <select id="getMaxRealAmountCreatedTime" resultType="java.util.Date">
        select max(order_datetime) from dyoa_record where mbs_type = 'REAL_AMOUNT'
    </select>

    <select id="getMaxClaimDate" resultType="java.util.Date">
        select max(order_datetime) from dyoa_record where mbs_type = 'REAL_AMOUNT_CLAIM'
    </select>

    <select id="selectOrderFundIdByDyoaOrderId" resultType="com.dayou.entity.DyoaRecord">
        SELECT
            m.dyoa_id,
            f.id as mbsId
        FROM
            (
                SELECT
                    mbs_id,
                    dyoa_id,
                    ( CASE mbs_type WHEN 'MAJOR_ORDER' THEN 'MAJOR_BUSINESS' WHEN 'PERSONAL_ORDER' THEN 'PERSONAL_BUSINESS'
                        WHEN 'ASSETS_ORDER' THEN 'ASSET_BUSINESS'
                        END ) AS mbs_type
                FROM
                    dyoa_record
                WHERE
                    mbs_type IN ( 'MAJOR_ORDER','PERSONAL_ORDER','ASSETS_ORDER' )) m
                LEFT JOIN order_fund f ON (
                f.business_id = m.mbs_id
                    AND f.business_type = m.mbs_type) where f.id is not null GROUP BY m.dyoa_id
    </select>


    <select id="getMaxFinanceInvoiceCreatedTime" resultType="java.util.Date">
        select max(order_datetime) from dyoa_record where mbs_type = 'FINANCE_INVOICE'
    </select>

</mapper>
