<?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.EmailFileInfoMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.EmailFileInfoDO">
        <id column="id" property="id"/>
        <result column="email_id" property="emailId"/>
        <result column="file_name" property="fileName"/>
        <result column="file_path" property="filePath"/>
        <result column="isvalid" property="isvalid"/>
        <result column="creatorid" property="creatorId"/>
        <result column="createtime" property="createTime"/>
        <result column="updaterid" property="updaterId"/>
        <result column="updatetime" property="updateTime"/>
    </resultMap>

    <insert id="insert" parameterType="com.simuwang.base.pojo.dos.EmailFileInfoDO" useGeneratedKeys="true"
            keyProperty="id" keyColumn="id">
        insert into PPW_EMAIL.email_file_info(email_id, file_name, file_path,
                                              isvalid, creatorid, createtime, updaterid, updatetime)
        values (#{itemDo.emailId}, #{itemDo.fileName}, #{itemDo.filePath},
                #{itemDo.isvalid}, #{itemDo.creatorId}, #{itemDo.createTime}, #{itemDo.updaterId}, #{itemDo.updateTime})
    </insert>

    <select id="getEmailFileById" resultMap="BaseResultMap"
            parameterType="java.lang.Integer">
        select id ,email_id, file_name, file_path,
               isvalid, creatorid, createtime, updaterid, updatetime
        from PPW_EMAIL.email_file_info where id=#{id} and isvalid=1
    </select>

    <select id="getEmailFileByEmailId" resultMap="BaseResultMap"
            parameterType="java.lang.Integer">
        select id ,email_id, file_name, file_path,
               isvalid, creatorid, createtime, updaterid, updatetime
        from PPW_EMAIL.email_file_info where email_id=#{emailId} and isvalid=1
    </select>

    <select id="queryByEmailId" resultMap="BaseResultMap">
        select id, file_name, file_path
        from PPW_EMAIL.email_file_info
        where email_id = #{emailId}
          and isvalid = 1
    </select>
    <select id="searchFundFileInfo" resultType="com.simuwang.base.pojo.vo.FundFileInfoVO">
        SELECT DISTINCT
            efn.fund_id as "fundId",
            efn.fund_name as "fundName",
            efi.file_name as "fileName",
            efi.file_path as "filePath"
        FROM
            PPW_EMAIL.EMAIL_FILE_INFO efi
                JOIN PPW_EMAIL.EMAIL_FUND_NAV efn
                     ON efi.id = efn.file_id
                JOIN PPW_EMAIL.EMAIL_PARSE_INFO epi
                     ON epi.id = efi.email_id
        WHERE efi.isvalid = 1
          AND efn.isvalid = 1
          AND epi.isvalid = 1
          AND epi.EMAIL_TYPE = #{fileType} and efn.fund_id=#{fundId}
        limit #{offset},#{pageSize}
    </select>

    <resultMap id="BaseDetailMap" type="com.simuwang.base.pojo.dos.EmailParseDetailDO">
        <result column="fund_id" property="fundId"/>
        <result column="fund_name" property="fundName"/>
        <result column="register_number" property="registerNumber"/>
        <result column="price_date" property="priceDate"/>
        <result column="nav" property="nav"/>
        <result column="cumulative_nav_withdrawal" property="cumulativeNavWithdrawal"/>
        <result column="asset_net" property="assetNet"/>
        <result column="asset_share" property="assetShare"/>
        <result column="nav_is_stored" property="navIsStored"/>
        <result column="nav_exception_status" property="navExceptionStatus"/>
        <result column="asset_is_stored" property="assetIsStored"/>
        <result column="asset_exception_status" property="assetExceptionStatus"/>
        <result column="updatetime" property="updateTime"/>
    </resultMap>
    <select id="searchEmailDetailById" resultMap="BaseDetailMap">
        SELECT distinct
            IFNULL(nav.fund_id,asset.fund_id) as fund_id,
            IFNULL(nav.fund_name,asset.fund_name) as fund_name,
            IFNULL(nav.register_number,asset.register_number) as register_number,
            IFNULL(nav.price_date,asset.price_date) as price_date,
            nav.nav,
            nav.cumulative_nav_withdrawal,
            asset.asset_net,
            asset.asset_share,
            nav.is_stored as nav_is_stored,
            nav.exception_status as nav_exception_status,
            asset.is_stored as asset_is_stored,
            asset.exception_status as asset_exception_status,
            case
            when nav.updatetime is null then
            asset.updatetime
            when asset.updatetime is null then
            nav.updatetime
            when nav.updatetime >= asset.updatetime then
                nav.updatetime
            else
                asset.updatetime
         end as updatetime
        FROM
            PPW_EMAIL.EMAIL_FILE_INFO efi
                LEFT JOIN PPW_EMAIL.email_fund_nav nav
                          ON efi.id = nav.file_id
                              AND nav.isvalid = 1
                LEFT JOIN PPW_EMAIL.email_fund_asset asset
                          ON efi.id = asset.file_id
                              AND asset.isvalid = 1 AND (nav.fund_id=asset.fund_id OR nav.register_number=asset.register_number OR nav.fund_name= asset.fund_name) AND nav.price_date=asset.price_date
        WHERE efi.isvalid = 1
          AND efi.id =#{fileId}
        order by fund_name desc,price_date desc
        limit #{offset},#{pageSize}
    </select>
    <select id="countFundFileInfo" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.FundFilePageQuery">
        SELECT count(DISTINCT efi.file_path)
        FROM
            PPW_EMAIL.EMAIL_FILE_INFO efi
                JOIN PPW_EMAIL.EMAIL_FUND_NAV efn
                     ON efi.id = efn.file_id
                JOIN PPW_EMAIL.EMAIL_PARSE_INFO epi
                     ON epi.id = efi.email_id
        WHERE efi.isvalid = 1
          AND efn.isvalid = 1
          AND epi.isvalid = 1
          AND epi.EMAIL_TYPE = #{fileType} and efn.fund_id=#{fundId}
    </select>
    <select id="countEmailDetailById" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.EmailFileQuery">
            select count(1) from (
            SELECT distinct
            IFNULL(nav.fund_id,asset.fund_id) as fund_id,
            IFNULL(nav.fund_name,asset.fund_name) as fund_name,
            IFNULL(nav.register_number,asset.register_number) as register_number,
            IFNULL(nav.price_date,asset.price_date) as price_date,
            nav.nav,
            nav.cumulative_nav_withdrawal,
            asset.asset_net,
            asset.asset_share
        FROM
            PPW_EMAIL.EMAIL_FILE_INFO efi
                LEFT JOIN PPW_EMAIL.email_fund_nav nav
                          ON efi.id = nav.file_id
                              AND nav.isvalid = 1
                LEFT JOIN PPW_EMAIL.email_fund_asset asset
                          ON efi.id = asset.file_id
                              AND asset.isvalid = 1 AND (nav.fund_id=asset.fund_id OR nav.register_number=asset.register_number OR nav.fund_name= asset.fund_name) AND nav.price_date=asset.price_date
        WHERE efi.isvalid = 1
          AND efi.id =#{fileId}) a
    </select>
    <select id="selectValuationFileId" resultType="java.lang.Integer"
            parameterType="java.util.List">
        SELECT
            distinct file.id
        FROM
            PPW_EMAIL.EMAIL_FILE_INFO file
                JOIN PPW_EMAIL.EMAIL_PARSE_INFO info
                     ON file.email_id = info.id
        WHERE info.email_type = 2
          AND file.id IN
        <foreach collection="fileIdList" item="fileId" index="index" separator=",">
            #{fileId}
        </foreach>
    </select>

    <update id="updateTimeById">
        update PPW_EMAIL.EMAIL_FILE_INFO
        set updatetime = #{parseDate}
        where isvalid = 1
          and id = #{id}
    </update>

</mapper>