<?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.EmailFundAssetMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.EmailFundAssetDO">
        <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="asset_net" property="assetNet"/>
        <result column="asset_share" property="assetShare"/>
        <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"/>
        <result column="email_title" property="emailTitle"/>
        <result column="email_id" property="emailId"/>
    </resultMap>

    <insert id="batchInsert" parameterType="com.simuwang.base.pojo.dos.EmailFundAssetDO">
        insert into PPW_EMAIL.email_fund_asset(file_id, fund_id, fund_name,register_number,price_date,asset_net,asset_share,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.assetNet},#{itemDo.assetShare},#{itemDo.isStored},#{itemDo.exceptionStatus},
            #{itemDo.isvalid}, #{itemDo.creatorId}, #{itemDo.createTime}, #{itemDo.updaterId}, #{itemDo.updateTime})
        </foreach>

    </insert>
    <update id="batchUpdate">
        <foreach collection="itemDoList" item="itemDo" index="index" open="" close="" separator=";">
            update PPW_EMAIL.email_fund_asset
            <set>
                fund_id = #{itemDo.fundId},
                exception_status = #{itemDo.exceptionStatus},
                updatetime=#{itemDo.updateTime},
                is_stored=#{itemDo.isStored}
            </set>
            where isvalid = 1
            and id = #{itemDo.id}
        </foreach>
    </update>
    <select id="searchAssetDetail" resultMap="BaseResultMap"
            parameterType="com.simuwang.base.pojo.dto.query.ParseDetailPageQuery">
        select distinct asset.id,
                        asset.fund_id,
                        asset.fund_name,
                        asset.register_number,
                        asset.price_date,
                        asset.asset_net,
                        asset.asset_share,
                        asset.is_stored,
                        asset.exception_status,
                        asset.updatetime,
                        parse.email_title,
                        parse.id as email_id
        from PPW_EMAIL.email_fund_asset asset
        join PPW_EMAIL.email_file_info file
        on asset.file_id = file.id
        join PPW_EMAIL.email_parse_info parse
        on file.email_id = parse.id
        where asset.isvalid=1 and file.isvalid=1 and parse.isvalid=1
        <if test="fundName != null and fundName !=''">
            and asset.fund_name like concat('%',#{fundName},'%')
        </if>
        <if test="priceStartDate != null and priceStartDate !=''">
            and asset.price_date >= #{priceStartDate}
        </if>
        <if test="priceEndDate != null and priceEndDate !=''">
            and asset.price_date  <![CDATA[ <= ]]> #{priceEndDate}
        </if>
        <if test="updateStartDate != null and updateStartDate !=''">
            and asset.updatetime  <![CDATA[ >= ]]> #{updateStartDate}
        </if>
        <if test="updateEndDate != null and updateEndDate !=''">
            and asset.updatetime  <![CDATA[ <= ]]> #{updateEndDate}
        </if>
        <if test="isStore != null">
            and asset.is_stored = #{isStore}
        </if>
        <if test="emailTitle != null and emailTitle != ''">
            and parse.email_title like concat('%',#{emailTitle},'%')
        </if>
        <if test="emailId != null and emailId !=''">
            and parse.id =#{emailId}
        </if>
        <if test="exceptionStatusList != null">
            and asset.exception_status in
            <foreach collection="exceptionStatusList" index="index" item="exceptionStatus" separator="," open="(" close=")">
                #{exceptionStatus}
            </foreach>
        </if>
        order by asset.updatetime desc
        limit #{offset},#{pageSize}
    </select>
    <select id="countAssetDetail" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.ParseDetailPageQuery">
        select count(1) from (
        select distinct asset.id
        from PPW_EMAIL.email_fund_asset asset
        join PPW_EMAIL.email_file_info file
        on asset.file_id = file.id
        join PPW_EMAIL.email_parse_info parse
        on file.email_id = parse.id
        where asset.isvalid=1 and file.isvalid=1 and parse.isvalid=1
        <if test="fundName != null and fundName !=''">
            and asset.fund_name like concat('%',#{fundName},'%')
        </if>
        <if test="priceStartDate != null and priceStartDate !=''">
            and asset.price_date >= #{priceStartDate}
        </if>
        <if test="priceEndDate != null and priceEndDate !=''">
            and asset.price_date  <![CDATA[ <= ]]> #{priceEndDate}
        </if>
        <if test="updateStartDate != null and updateStartDate !=''">
            and asset.updatetime  <![CDATA[ >= ]]> #{updateStartDate}
        </if>
        <if test="updateEndDate != null and updateEndDate !=''">
            and asset.updatetime  <![CDATA[ <= ]]> #{updateEndDate}
        </if>
        <if test="isStore != null">
            and asset.is_stored = #{isStore}
        </if>
        <if test="emailTitle != null and emailTitle != ''">
            and parse.email_title like concat('%',#{emailTitle},'%')
        </if>
        <if test="emailId != null and emailId !=''">
            and parse.id =#{emailId}
        </if>
        <if test="exceptionStatusList != null">
            and asset.exception_status in
            <foreach collection="exceptionStatusList" index="index" item="exceptionStatus" separator="," open="(" close=")">
                #{exceptionStatus}
            </foreach>
        </if>
        )a
    </select>
    <select id="countNoStoreAsset" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(id)
        from PPW_EMAIL.email_fund_asset
        where isvalid =1 and exception_status=3
          and fund_name=#{fundName}
    </select>
    <select id="selectNotMappingAsset" resultMap="BaseResultMap"
            parameterType="java.lang.String">
        select distinct id,fund_id,file_id, fund_name,register_number,price_date,asset_net,asset_share,is_stored,exception_status,
                        updatetime
        from PPW_EMAIL.email_fund_asset
        where isvalid =1 and exception_status=3
          and fund_name=#{fundName}
    </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="selectFundAssetByFielId" resultMap="BaseDetailMap">
        SELECT DISTINCT
            asset.fund_id,
            asset.fund_name,
            asset.register_number,
            asset.price_date,
            null as nav,
            null as cumulative_nav_withdrawal,
            asset.asset_net as asset_net,
            asset.asset_share as asset_share,
            null AS nav_is_stored,
            null AS nav_exception_status,
            asset.is_stored as asset_is_stored,
            asset.exception_status as asset_exception_status,
            asset.updatetime
        FROM
            PPW_EMAIL.EMAIL_FILE_INFO efi
                JOIN PPW_EMAIL.email_fund_asset asset
                     ON efi.id = asset.file_id
                         AND asset.isvalid = 1
        WHERE efi.isvalid = 1
          AND efi.id = #{fileId} and asset.price_date=#{priceDate}
    </select>

    <delete id="deleteByFileId">
        update PPW_EMAIL.email_fund_asset
        set isvalid   = 0,
            updatetime= now()
        where isvalid = 1
          and file_id = #{fileId}
    </delete>

</mapper>