<?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.DistributionMapper">
    <resultMap id="BaseMap" type="com.simuwang.base.pojo.dos.DistributionDO">
        <id column="id" property="id"/>
        <result column="fund_id" property="fundId"/>
        <result column="distribute_date" property="distributeDate"/>
        <result column="distribute_type" property="distributeType"/>
        <result column="distribution" property="distribution"/>
        <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>

    <resultMap id="BasePageMap" type="com.simuwang.base.pojo.dos.DistributionTablePageDO">
        <id column="id" property="id"/>
        <result column="fund_name" property="fundName"/>
        <result column="company_name" property="companyName"/>
        <result column="register_number" property="registerNumber"/>
        <result column="fund_id" property="fundId"/>
        <result column="distribute_date" property="distributeDate"/>
        <result column="distribute_type" property="distributeType"/>
        <result column="distribution" property="distribution"/>
        <result column="nav" property="nav"/>
        <result column="cumulative_nav_withdrawal" property="cumulativeNavWithdrawal"/>
        <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="saveDistribution" parameterType="com.simuwang.base.pojo.dos.DistributionDO">
        insert into PPW_EMAIL.distribution(fund_id,distribute_date,distribute_type,distribution,isvalid,creatorid,createtime,updatetime,updaterid)
        values (#{fundId},#{distributeDate},#{distributeType},#{distribution},#{isvalid},#{creatorId},#{createTime},#{updateTime},#{updaterId})
    </insert>
    <update id="updateDistributionById" parameterType="com.simuwang.base.pojo.dos.DistributionDO">
        update PPW_EMAIL.distribution set distribute_date=#{distributeDate},distribute_type=#{distributeType},distribution=#{distribution},updatetime=#{updateTime}
        where id=#{id} and isvalid =1
    </update>
    <delete id="deleteDistribution" parameterType="com.simuwang.base.pojo.vo.IdListVO">
        update PPW_EMAIL.distribution set isvalid =0,updatetime=sysdate() where isvalid =1 and id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="searchDistributionList" resultMap="BasePageMap"
            parameterType="com.simuwang.base.pojo.dto.query.DistributionPageQuery">
        SELECT DISTINCT
            d.id,
            d.fund_id,
            info.fund_name,
            info.register_number,
            c.company_name,
            d.distribute_date,
            d.distribute_type,
            d.distribution,
            n.nav,
            n.cumulative_nav_withdrawal,
            d.updatetime,
            d.isvalid,
            d.creatorid,
            d.createtime,
            d.updatetime
        FROM
            PPW_EMAIL.distribution d
            JOIN PPW_EMAIL.pvn_fund_info info
            ON d.fund_id = info.fund_id
            JOIN PPW_EMAIL.pvn_company_info c
            ON info.trust_id = c.company_id
            JOIN PPW_EMAIL.nav n
            ON n.fund_id = d.fund_id and d.distribute_date=n.price_date
        WHERE d.isvalid = 1
            AND info.isvalid = 1
            AND c.isvalid = 1
            AND n.isvalid = 1
        <if test="companyName != null and companyName !=''">
            and (c.company_name like concat('%',#{companyName},'%') or c.company_short_name like concat('%',#{companyName},'%'))
        </if>
        <if test="fundName != null and fundName !=''">
            and (info.fund_name like concat('%',#{fundName},'%') or info.fund_short_name like concat('%',#{fundName},'%'))
        </if>
        <if test="distributeType != null">
            and d.distribute_type = #{distributeType}
        </if>
        <if test="startDate != null and startDate !=''">
            and d.distribute_date >= #{startDate}
        </if>
        <if test="endDate != null and endDate !=''">
            and d.distribute_date <![CDATA[ <= ]]> #{endDate}
        </if>
        limit #{offset},#{pageSize}
    </select>
    <select id="countDistributionList" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.DistributionPageQuery">
        select count(distinct d.id)
        from PPW_EMAIL.distribution d
        join PPW_EMAIL.pvn_fund_info info on d.fund_id = info.fund_id
        join PPW_EMAIL.pvn_company_info c on info.trust_id = c.company_id
        join PPW_EMAIL.nav n on n.fund_id = d.fund_id
        where d.isvalid=1 and info.isvalid =1 and c.isvalid =1 and n.isvalid=1
        <if test="companyName != null and companyName !=''">
            and (c.company_name like concat('%',#{companyName},'%') or c.company_short_name like concat('%',#{companyName},'%'))
        </if>
        <if test="fundName != null and fundName !=''">
            and (info.fund_name like concat('%',#{fundName},'%') or info.fund_short_name like concat('%',#{fundName},'%'))
        </if>
        <if test="distributeType != null">
            and d.distribute_type = #{distributeType}
        </if>
        <if test="startDate != null and startDate !=''">
            and d.distribute_date >= #{startDate}
        </if>
        <if test="endDate != null and endDate !=''">
            and d.distribute_date <![CDATA[ <= ]]> #{endDate}
        </if>
    </select>
    <select id="selectDistributionById" resultMap="BaseMap"
            parameterType="java.lang.Integer">
        select id,fund_id,distribute_date,distribute_type,distribution,isvalid,creatorid,createtime,updatetime,updaterid
        from PPW_EMAIL.distribution where isvalid =1 and id=#{id}
    </select>
    <select id="selectDistributionByDate" resultType="com.simuwang.base.pojo.dos.DistributionDO">
        select id,fund_id,distribute_date,distribute_type,distribution,isvalid,creatorid,createtime,updatetime,updaterid
        from PPW_EMAIL.distribution where isvalid =1 and fund_id=#{fundId} and distribute_date=#{distributeDate}
    </select>
</mapper>