<?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.simuwang.base.mapper.FundNavAssetMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.FundNavAssetDO">
        <result column="fund_id" property="fundId"/>
        <result column="fund_short_name" property="fundName"/>
        <result column="price_date" property="priceDate"/>
        <result column="nav" property="nav"/>
        <result column="cumulative_nav_withdrawal" property="cumulativeNavWithdrawal"/>
        <result column="asset_share" property="assetShare"/>
        <result column="asset_net" property="assetNet"/>
        <result column="updatetime" property="updateTime"/>
    </resultMap>
    <select id="searchNavAssetList" resultMap="BaseResultMap"
            parameterType="com.simuwang.base.pojo.dto.query.FundNavAssetPageQuery">
        SELECT distinct
        info.fund_id,
        info.fund_short_name,
        IFNULL(
        nav.price_date,
        asset.price_date
        ) AS price_date,
        nav.nav,
        nav.cumulative_nav_withdrawal,
        asset.asset_share,
        asset.asset_net,
        CASE
        WHEN nav.updatetime is null
        THEN asset.updatetime
        WHEN asset.updatetime is null
        THEN nav.updatetime
        WHEN asset.updatetime >= nav.updatetime
        THEN asset.updatetime
        ELSE nav.updatetime
        END AS updatetime
        FROM
        PPW_EMAIL.pvn_fund_info info
        LEFT JOIN PPW_EMAIL.nav nav
        ON info.fund_id = nav.fund_id
        AND nav.isvalid = 1
        LEFT JOIN PPW_EMAIL.asset asset
        ON info.fund_id = asset.fund_id
        AND nav.price_date = asset.price_date
        AND asset.isvalid = 1
        WHERE info.isvalid = 1
        and (nav.nav is not null or asset.asset_share is not null or asset.asset_net is not null)
        <if test="fundName != null and fundName !=''">
            and (info.fund_name like concat('%',#{fundName},'%') or info.fund_short_name like concat('%',#{fundName},'%') or info.register_number like concat('%',#{fundName},'%'))
        </if>
        <if test="priceStartDate != null and priceStartDate !=''">
            and (nav.price_date >= #{priceStartDate} or asset.price_date >= #{priceStartDate})
        </if>
        <if test="priceEndDate != null and priceEndDate !=''">
            and (nav.price_date <![CDATA[ <= ]]> #{priceEndDate} or asset.price_date <![CDATA[ <= ]]> #{priceEndDate})
        </if>
        <if test="updateStartDate != null and updateStartDate !=''">
            and (nav.updatetime >= #{updateStartDate} or asset.updatetime >= #{updateStartDate})
        </if>
        <if test="updateEndDate != null and updateEndDate !=''">
            and (nav.updatetime <![CDATA[ <= ]]> #{updateEndDate} or asset.updatetime <![CDATA[ <= ]]> #{updateEndDate})
        </if>
        order by updatetime desc
        limit #{offset},#{pageSize}
    </select>
    <select id="countNavAssetList" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.FundNavAssetPageQuery">
        SELECT count(1)
        FROM
        PPW_EMAIL.pvn_fund_info info
        LEFT JOIN PPW_EMAIL.nav nav
        ON info.fund_id = nav.fund_id
        AND nav.isvalid = 1
        LEFT JOIN PPW_EMAIL.asset asset
        ON info.fund_id = asset.fund_id
        AND nav.price_date = asset.price_date
        AND asset.isvalid = 1
        WHERE info.isvalid = 1
        and (nav.nav is not null or asset.asset_share is not null or asset.asset_net is not null)
        <if test="fundName != null and fundName !=''">
            and (info.fund_name like concat('%',#{fundName},'%') or info.fund_short_name like concat('%',#{fundName},'%') or info.register_number like concat('%',#{fundName},'%'))
        </if>
        <if test="priceStartDate != null and priceStartDate !=''">
            and (nav.price_date >= #{priceStartDate} or asset.price_date >= #{priceStartDate})
        </if>
        <if test="priceEndDate != null and priceEndDate !=''">
            and (nav.price_date <![CDATA[ <= ]]> #{priceEndDate} or asset.price_date <![CDATA[ <= ]]> #{priceEndDate})
        </if>
        <if test="updateStartDate != null and updateStartDate !=''">
            and (nav.updatetime >= #{updateStartDate} or asset.updatetime >= #{updateStartDate})
        </if>
        <if test="updateEndDate != null and updateEndDate !=''">
            and (nav.updatetime <![CDATA[ <= ]]> #{updateEndDate} or asset.updatetime <![CDATA[ <= ]]> #{updateEndDate})
        </if>
    </select>
</mapper>