<?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.FundInfoMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.FundInfoDO">
        <id column="id" property="id"/>
        <result column="p_fund_id" property="pFundId"/>
        <result column="fund_name" property="fundName"/>
        <result column="fund_short_name" property="fundShortName"/>
        <result column="fund_type" property="fundType"/>
        <result column="manager_type" property="managerType"/>
        <result column="base_currency" property="baseCurrency"/>
        <result column="inception_date" property="inceptionDate"/>
        <result column="advisor_id" property="advisorId"/>
        <result column="issuer_id" property="issuerId"/>
        <result column="custodian_id" property="custodianId"/>
        <result column="register_number" property="registerNumber"/>
        <result column="register_date" property="registerDate"/>
        <result column="is_amac_show" property="isAmacShow"/>
        <result column="fund_status" property="fundStatus"/>
        <result column="amac_source_type" property="amacSourceType"/>
        <result column="investment_type" property="investmentType"/>
        <result column="liquidate_date" property="liquidateDate"/>
        <result column="performance_start_date" property="performanceStartDate"/>
        <result column="is_shareclass" property="isShareclass"/>
        <result column="trust_id" property="trustId"/>
        <result column="createtime" property="createTime"/>
        <result column="updatetime" property="updateTime"/>
        <result column="isvalid" property="isvalid"/>
    </resultMap>
    <select id="searchFundInfoByKeyword" resultType="java.util.Map">
        select f.fund_id as "fundId",
        f.fund_short_name as "fundShortName",
        f.register_number as "registerNumber"
        from PPW_EMAIL.pvn_fund_info f where f.isvalid =1
        <if test="keyword != null and keyword !=''">
            and (f.fund_name like concat('%',#{keyword},'%') or f.fund_short_name like concat('%',#{keyword},'%') or f.register_number like concat('%',#{keyword},'%') or f.fund_id like concat('%',#{keyword},'%'))
        </if>
        limit #{offset},#{pageSize}
    </select>
    <select id="searchFundInfoList" resultType="com.simuwang.base.pojo.vo.FundInformationVO">
        select * from (
        select f.fund_id as fundId,
               f.fund_short_name as fundShortName,
               c.company_short_name as companyShortName,
               f.inception_date as inceptionDate,
               ifnull(n.last_price_date,'') as lastPriceDate,
               r.nav_frequency as navFrequency,
               r.asset_Frequency as assetFrequency,
               r.frequency_remark as frequencyRemark
        from PPW_EMAIL.pvn_fund_info f
        join PPW_EMAIL.pvn_company_info c on f.trust_id = c.company_id
        left join (select nv.fund_id,max(nv.price_date) as last_price_date from PPW_EMAIL.nav nv where nv.isvalid =1 group by nv.fund_id) n
        on f.fund_id = n .fund_id
        left join PPW_EMAIL.fund_report_frequency r on r.fund_id = f.fund_id and r.isvalid =1
        where f.isvalid =1 and c.isvalid =1
        <if test="fundId != null and fundId !=''">
            and f.fund_id like concat('%',#{fundId},'%')
        </if>
        <if test="fundShortName != null and fundShortName !=''">
            and (f.fund_name like concat('%',#{fundShortName},'%') or f.fund_short_name like concat('%',#{fundShortName},'%') or f.register_number like concat('%',#{fundShortName},'%'))
        </if>
        <if test="companyShortName != null and companyShortName !=''">
            and (c.company_name like concat('%',#{companyShortName},'%') or c.company_short_name like concat('%',#{companyShortName},'%')  or c.register_number like concat('%',#{companyShortName},'%'))
        </if>
        <if test="navFrequency != null and navFrequency !=''">
            and r.nav_frequency = #{navFrequency}
        </if>
        <if test="assetFrequency != null and assetFrequency !=''">
            and r.asset_Frequency = #{assetFrequency}
        </if>
        <if test="startDate != null and startDate !=''">
            and f.inception_date <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null and endDate !=''">
            and f.inception_date <![CDATA[ <= ]]> #{endDate}
        </if>) a
        limit #{offset},#{pageSize}
        order by a.lastPriceDate desc
    </select>

    <select id="queryFundByNameAndRegisterNumber" resultType="com.simuwang.base.pojo.dos.FundInfoDO">
        select fund_id         as fundId,
               fund_name       as fundName,
               register_number as registerNumber
        from PPW_EMAIL.pvn_fund_info
        where isvalid = 1
          and fund_name = #{fundName}
          and register_number = #{registerNumber}
    </select>

    <select id="queryFundByName" resultType="com.simuwang.base.pojo.dos.FundInfoDO">
        select fund_id         as fundId,
               fund_name       as fundName,
               register_number as registerNumber
        from PPW_EMAIL.pvn_fund_info
        where isvalid = 1
          and fund_name = #{fundName} and register_number is null
    </select>

    <select id="queryFundByRegisterNumber" resultType="com.simuwang.base.pojo.dos.FundInfoDO">
        select fund_id         as fundId,
               fund_name       as fundName,
               register_number as registerNumber
        from PPW_EMAIL.pvn_fund_info
        where isvalid = 1
          and register_number = #{registerNumber} and fund_name is null
    </select>
    <select id="countFundInfoByKeyword" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.FundInputPageQuery">
        select count(f.fund_id)
        from PPW_EMAIL.pvn_fund_info f where f.isvalid =1
        <if test="keyword != null and keyword !=''">
            and (f.fund_name like concat('%',#{keyword},'%') or f.fund_short_name like concat('%',#{keyword},'%') or f.register_number like concat('%',#{keyword},'%') or f.fund_id like concat('%',#{keyword},'%'))
        </if>
    </select>
    <select id="countFundInfo" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.FundInfoPageQuery">
        select count(f.fund_id)
        from PPW_EMAIL.pvn_fund_info f
        join PPW_EMAIL.pvn_company_info c on f.trust_id = c.company_id
        left join (select nv.fund_id,max(nv.price_date) as last_price_date from PPW_EMAIL.nav nv where nv.isvalid =1 group by nv.fund_id) n
        on f.fund_id = n .fund_id
        left join PPW_EMAIL.fund_report_frequency r on r.fund_id = f.fund_id and r.isvalid =1
        where f.isvalid =1 and c.isvalid =1
        <if test="fundId != null and fundId !=''">
            and f.fund_id like concat('%',#{fundId},'%')
        </if>
        <if test="fundShortName != null and fundShortName !=''">
            and (f.fund_name like concat('%',#{fundShortName},'%') or f.fund_short_name like concat('%',#{fundShortName},'%')  or f.register_number like concat('%',#{fundShortName},'%'))
        </if>
        <if test="companyShortName != null and companyShortName !=''">
            and (c.company_name like concat('%',#{companyShortName},'%') or c.company_short_name like concat('%',#{companyShortName},'%')  or c.register_number like concat('%',#{companyShortName},'%'))
        </if>
        <if test="navFrequency != null and navFrequency !=''">
            and r.nav_frequency = #{navFrequency}
        </if>
        <if test="assetFrequency != null and assetFrequency !=''">
            and r.asset_Frequency = #{assetFrequency}
        </if>
        <if test="startDate != null and startDate !=''">
            and f.inception_date <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null and endDate !=''">
            and f.inception_date <![CDATA[ <= ]]> #{endDate}
        </if>
    </select>
    <select id="getFundNameByFundId" resultType="java.lang.String" parameterType="java.lang.String">
        select fund_name from PPW_EMAIL.pvn_fund_info where fund_id=#{fundId} and isvalid=1
    </select>
    <select id="queryFundIdByName" resultType="java.lang.String" parameterType="java.lang.String">
        select fund_id
        from PPW_EMAIL.pvn_fund_info
        where isvalid = 1
          and fund_name = #{fundName} limit 1
    </select>
    <select id="getCompanyNameByFundId" resultType="java.lang.String" parameterType="java.lang.String">
        select c.company_name from PPW_EMAIL.pvn_company_info c
        join PPW_EMAIL.pvn_fund_info info on info.trust_id=c.company_id
        where info.fund_id=#{fundId} and info.isvalid=1 and c.isvalid=1
    </select>
    <select id="getLiquidateDateByFundId" resultType="java.lang.String" parameterType="java.lang.String">
        select liquidate_date from PPW_EMAIL.pvn_fund_info where fund_id=#{fundId} and isvalid=1
    </select>
    <select id="getFundIdByCompanyId" resultType="java.lang.String" parameterType="java.lang.String">
        select info.fund_id from PPW_EMAIL.pvn_fund_info info join PPW_EMAIL.pvn_company_info c on info.trust_id=c.company_id
        where info.isvalid=1 and c.isvalid=1 and c.company_id=#{companyId}
    </select>
    <select id="getInceptionDateByFundId" resultType="java.lang.String" parameterType="java.lang.String">
        select inception_date from PPW_EMAIL.pvn_fund_info where fund_id=#{fundId} and isvalid=1
    </select>

</mapper>