<?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.EmailFundNavMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.EmailFundNavDO">
        <id column="id" property="id"/>
        <result column="file_id" property="fileId"/>
        <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="is_stored" property="isStored"/>
        <result column="exception_status" property="exceptionStatus"/>
        <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="batchInsert" parameterType="com.simuwang.base.pojo.dos.EmailFundNavDO">
        insert into PPW_EMAIL.email_fund_nav(file_id, fund_id, fund_name,register_number,price_date,
                                              nav,cumulative_nav_withdrawal,is_stored,exception_status,
                                              isvalid, creatorid, createtime, updaterid, updatetime)
        values
        <foreach collection="itemDoList" item="itemDo" index="index" separator=",">
            (#{itemDo.fileId},#{itemDo.fundId},#{itemDo.fundName},#{itemDo.registerNumber},#{itemDo.priceDate},
             #{itemDo.nav},#{itemDo.cumulativeNavWithdrawal},#{itemDo.isStored},#{itemDo.exceptionStatus},
            #{itemDo.isvalid}, #{itemDo.creatorId}, #{itemDo.createTime}, #{itemDo.updaterId}, #{itemDo.updateTime})
        </foreach>
    </insert>
    <update id="batchUpdate">
        <foreach collection="navList" item="itemDo" index="index" open="" close="" separator=";">
            update PPW_EMAIL.email_fund_nav
            <set>
                fund_id = #{itemDo.fundId},
                exception_status = #{itemDo.exceptionStatus},
                updatetime=#{itemDo.updateTime}
            </set>
            where isvalid = 1
            and id = #{itemDo.id}
        </foreach>
    </update>
    <delete id="deleteByFileId" parameterType="java.lang.Integer">
        update PPW_EMAIL.email_fund_nav nav set isvalid = 0,updatetime=sysdate() where file_id=#{fileId}
    </delete>
    <select id="searchNavDetail" resultMap="BaseResultMap">
        SELECT distinct nav.id,
            nav.fund_id,
            nav.fund_name,
            nav.register_number,
            nav.price_date,
            nav.nav,
            nav.cumulative_nav_withdrawal,
            nav.exception_status,
            nav.is_stored,
            nav.updatetime
        FROM
            PPW_EMAIL.email_fund_nav nav
        WHERE nav.isvalid = 1
        <if test="fundName != null and fundName !=''">
            and nav.fund_name like concat('%',#{fundName},'%')
        </if>
        <if test="priceStartDate != null and priceStartDate !=''">
            and nav.price_date >= #{priceStartDate}
        </if>
        <if test="priceEndDate != null and priceEndDate !=''">
            and nav.price_date  <![CDATA[ <= ]]> #{priceEndDate}
        </if>
        <if test="exceptionStatus != null">
            and nav.exception_status = #{exceptionStatus}
        </if>
        <if test="updateStartDate != null and updateStartDate !=''">
            and nav.updatetime  <![CDATA[ >= ]]> #{updateStartDate}
        </if>
        <if test="updateEndDate != null and updateEndDate !=''">
            and nav.updatetime  <![CDATA[ <= ]]> #{updateEndDate}
        </if>
        <if test="isStore != null">
            and nav.is_stored = #{isStore}
        </if>
        order by nav.fund_name desc,nav.price_date desc
        limit #{offset},#{pageSize}
    </select>
    <select id="countNavDetail" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.ParseDetailPageQuery">
        select count(1) from (
            SELECT distinct nav.id,
            nav.fund_id,
            nav.fund_name,
            nav.register_number,
            nav.price_date,
            nav.nav,
            nav.cumulative_nav_withdrawal,
            nav.exception_status,
            nav.is_stored,
            nav.updatetime
            FROM
            PPW_EMAIL.email_fund_nav nav
            WHERE nav.isvalid = 1
            <if test="fundName != null and fundName !=''">
                and nav.fund_name like concat('%',#{fundName},'%')
            </if>
            <if test="priceStartDate != null and priceStartDate !=''">
                and nav.price_date >= #{priceStartDate}
            </if>
            <if test="priceEndDate != null and priceEndDate !=''">
                and nav.price_date  <![CDATA[ <= ]]> #{priceEndDate}
            </if>
            <if test="exceptionStatus != null">
                and nav.exception_status = #{exceptionStatus}
            </if>
            <if test="updateStartDate != null and updateStartDate !=''">
                and nav.updatetime  <![CDATA[ >= ]]> #{updateStartDate}
            </if>
            <if test="updateEndDate != null and updateEndDate !=''">
                and nav.updatetime  <![CDATA[ <= ]]> #{updateEndDate}
            </if>
            <if test="isStore != null">
                and nav.is_stored = #{isStore}
            </if>
        ) a
    </select>
    <select id="countNoStoreNav" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(nav.id)
        from PPW_EMAIL.email_fund_nav nav
        where nav.isvalid =1 and nav.exception_status=3
            and nav.fund_name=#{fundName}
    </select>
    <select id="selectFileIdByFundName" resultType="java.lang.Integer" parameterType="java.lang.String">
        select distinct nav.file_id from PPW_EMAIL.email_fund_nav nav where nav.isvalid =1 and nav.exception_status=3
            and nav.fund_name=#{fundName}
    </select>
    <select id="selectNotMappingNav" resultMap="BaseResultMap"
            parameterType="java.lang.String">
        select nav.id,
               nav.file_id,
               nav.fund_id,
               nav.fund_name,
               nav.register_number,
               nav.price_date,
               nav.nav,
               nav.cumulative_nav_withdrawal,
               nav.exception_status,
               nav.is_stored,
               nav.isvalid,
               nav.creatorid,
               nav.updaterid,
               nav.updatetime,
               nav.createtime
        from PPW_EMAIL.email_fund_nav nav
        where nav.isvalid =1 and nav.exception_status=3
          and nav.fund_name=#{fundName}
    </select>
    <select id="getMaxPriceDate" resultType="java.lang.String" parameterType="java.lang.String">
        select max(nav.price_date) from PPW_EMAIL.email_fund_nav nav where nav.isvalid =1 and nav.exception_status=3
                                                                        and nav.fund_name=#{fundName}
    </select>


</mapper>