<?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.smppw.modaq.domain.mapper.EmailFileInfoMapper">
    <resultMap id="BaseResultMap" type="com.smppw.modaq.domain.entity.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="ai_parse" property="aiParse"/>
        <result column="ai_file_id" property="aiFileId"/>
        <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.smppw.modaq.domain.entity.EmailFileInfoDO" useGeneratedKeys="true"
            keyProperty="id" keyColumn="id">
        insert into mo_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,ai_parse,ai_file_id,-->
<!--               isvalid, creatorid, createtime, updaterid, updatetime-->
<!--        from mo_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,ai_parse,ai_file_id,-->
<!--               isvalid, creatorid, createtime, updaterid, updatetime-->
<!--        from mo_email_file_info where email_id=#{emailId} and isvalid=1-->
<!--    </select>-->

    <select id="queryByEmailId" resultMap="BaseResultMap">
        select id, file_name, file_path,ai_parse,ai_file_id
        from mo_email_file_info
        where email_id = #{emailId}
          and isvalid = 1
    </select>
<!--    <select id="searchFundFileInfo" resultType="com.simuwang.base.pojo.vo.FundFileInfoVO">-->
<!--        SELECT DISTINCT efi.id as "fileId",-->
<!--        <if test="fileType != 3">-->
<!--            efn.fund_id as "fundId",-->
<!--            efn.fund_name as "fundName",-->
<!--        </if>-->
<!--        <if test="fileType == 3">-->
<!--            info.fund_id as "fundId",-->
<!--            efn.fund_name as "fundName",-->
<!--        </if>-->
<!--            efi.file_name as "fileName",-->
<!--            efi.file_path as "filePath",-->
<!--            efi.createtime as "createTime"-->
<!--        FROM-->
<!--            PPW_EMAIL.EMAIL_FILE_INFO efi-->
<!--            <if test="fileType != 3">-->
<!--                JOIN PPW_EMAIL.EMAIL_FUND_NAV efn-->
<!--                ON efi.id = efn.file_id-->
<!--            </if>-->
<!--            <if test="fileType == 3">-->
<!--                JOIN PPW_EMAIL.AMAC_REPORT_FUND_INFO efn-->
<!--                ON efi.id = efn.file_id-->
<!--                left join PPW_EMAIL.PVN_FUND_INFO info-->
<!--                on efn.register_number=info.register_number and info.isvalid=1-->
<!--            </if>-->
<!--                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}-->
<!--        <if test="fileType != 3">-->
<!--            and efn.fund_id=#{fundId}-->
<!--        </if>-->
<!--        <if test="fileType == 3">-->
<!--            and info.fund_id=#{fundId}-->
<!--        </if>-->
<!--        order by efi.createtime desc-->
<!--        limit #{offset},#{pageSize}-->
<!--    </select>-->

<!--    <resultMap id="BaseDetailMap" type="com.smppw.modaq.domain.entity.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(1) from (-->
<!--        SELECT DISTINCT efi.id as "fileId",-->
<!--        <if test="fileType != 3">-->
<!--            efn.fund_id as "fundId",-->
<!--            efn.fund_name as "fundName",-->
<!--        </if>-->
<!--        <if test="fileType == 3">-->
<!--            info.fund_id as "fundId",-->
<!--            efn.fund_name as "fundName",-->
<!--        </if>-->
<!--        efi.file_name as "fileName",-->
<!--        efi.file_path as "filePath",-->
<!--        efi.createtime as "createTime"-->
<!--        FROM-->
<!--        PPW_EMAIL.EMAIL_FILE_INFO efi-->
<!--        <if test="fileType != 3">-->
<!--            JOIN PPW_EMAIL.EMAIL_FUND_NAV efn-->
<!--            ON efi.id = efn.file_id-->
<!--        </if>-->
<!--        <if test="fileType == 3">-->
<!--            JOIN PPW_EMAIL.AMAC_REPORT_FUND_INFO efn-->
<!--            ON efi.id = efn.file_id-->
<!--            left join PPW_EMAIL.PVN_FUND_INFO info-->
<!--            on efn.register_number=info.register_number and info.isvalid=1-->
<!--        </if>-->
<!--        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}-->
<!--        <if test="fileType != 3">-->
<!--            and efn.fund_id=#{fundId}-->
<!--        </if>-->
<!--        <if test="fileType == 3">-->
<!--            and info.fund_id=#{fundId}-->
<!--        </if>) a-->
<!--    </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>-->
<!--    <select id="getAllPriceDateByFileId" resultType="java.lang.String" parameterType="java.lang.Integer">-->
<!--        select * from (select distinct price_date from PPW_EMAIL.EMAIL_FUND_NAV WHERE file_id=#{fileId} and isvalid=1-->
<!--        union-->
<!--        select distinct price_date from PPW_EMAIL.EMAIL_FUND_ASSET WHERE file_id=#{fileId} and isvalid=1) a order by price_date desc-->
<!--    </select>-->

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

    <update id="updateAiParseByFileId">
        update mo_email_file_info
        set updatetime = now(),
            ai_parse = #{aiParse},
            ai_file_id = #{aiFileId}
        where isvalid = 1
          and id = #{fileId}
    </update>

    <select id="getLetterFilenameSuccessCount" resultType="int">
        select count(1)
        from mo_email_file_info a
                 join mo_email_parse_info e on e.id = a.email_id and e.email_title = #{emailTitle}
                 join mo_report_base_info b on b.file_id = a.id and b.report_type = 'LETTER'
                 join mo_report_fund_transaction c on a.id = c.file_id and c.fund_name is not null
                 join mo_report_investor_info d on a.id = d.file_id and d.investor_name is not null
        where a.file_name = #{filename} and a.isvalid = 1
    </select>

    <select id="getAmacFilenameSuccessCount" resultType="int">
        select count(1)
        from mo_email_file_info a
                 join mo_email_parse_info e on e.id = a.email_id and e.email_title = #{emailTitle} and e.attr_size = #{fileSize}
                 join mo_report_base_info b on b.file_id = a.id and b.report_type in ('MONTHLY', 'QUARTERLY', 'ANNUALLY')
                 join mo_report_fund_info c on a.id = c.file_id and (c.fund_name is not null or c.fund_code is not null)
        where a.file_name = #{filename} and a.isvalid = 1
    </select>

    <select id="getWeeklyFilenameSuccessCount" resultType="int">
        select count(1)
        from mo_email_file_info a
                 join mo_email_parse_info e on e.id = a.email_id and e.email_title = #{emailTitle} and e.attr_size = #{fileSize}
                 join mo_report_base_info b on b.file_id = a.id and b.report_type = 'WEEKLY'
                 join mo_report_fund_info c on a.id = c.file_id and (c.fund_name is not null or c.fund_code is not null)
        where a.file_name = #{filename} and a.isvalid = 1
    </select>

    <select id="getOtherFilenameSuccessCount" resultType="int">
        select count(1)
        from mo_email_file_info a
                 join mo_email_parse_info e on e.id = a.email_id and e.email_title = #{emailTitle} and e.attr_size = #{fileSize}
                 join mo_report_base_info b on b.file_id = a.id and b.report_type = 'OTHER'
        where a.file_name = #{filename} and a.isvalid = 1
    </select>
</mapper>