<?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.DeletionInfoMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.DeletionInfoDO">
        <id column="id" property="id"/>
        <result column="fund_id" property="fundId"/>
        <result column="deletion_date" property="deletionDate"/>
        <result column="deletion_type" property="deletionType"/>
        <result column="company_name" property="companyName"/>
        <result column="fund_name" property="fundName"/>
        <result column="deletion_num" property="deletionNum"/>
        <result column="processed_num" property="processedNum"/>
        <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="saveDeletionInfoDO" parameterType="com.simuwang.base.pojo.dos.DeletionInfoDO">
        insert into PPW_EMAIL.deletion_info(fund_id,deletion_type,deletion_date,isvalid,createtime,updatetime)
        values (#{fundId},#{deletionType},#{deletionDate},#{isvalid},#{createTime},#{updateTime})
    </insert>
    <update id="update" parameterType="com.simuwang.base.pojo.dos.FundDeletionInfoDO">
        update PPW_EMAIL.deletion_info set fund_id=#{fundId},deletion_type=#{deletionType},deletion_date=#{deletionDate},remark=#{remark},updatetime=#{updateTime}
        where id=#{id} and isvalid=1
    </update>
    <update id="batchUpdate">
        <foreach collection="itemDoList" item="itemDo" index="index" open="" close="" separator=";">
            update PPW_EMAIL.deletion_info
            <set>
                fund_id=#{itemDo.fundId},
                deletion_type = #{itemDo.deletionType},
                deletion_date=#{itemDo.deletionDate},
                remark=#{itemDo.remark},
                updatetime=#{itemDo.updateTime}
            </set>
            where isvalid = 1 and id=#{itemDo.id}
        </foreach>
    </update>
    <update id="updateRemark">
        update PPW_EMAIL.deletion_info set remark=#{remark} where fund_id=#{fundId} and deletion_type=#{deletionType} and isvalid=1
    </update>
    <select id="searchDeletionList" resultMap="BaseResultMap"
            parameterType="com.simuwang.base.pojo.dto.query.DeletionPageQuery">
        SELECT
        d.fund_id,
        info.fund_name,
        c.company_name,
        d.deletion_type,
        ddn.deletion_num,
        ddp.processed_num
        FROM
        PPW_EMAIL.deletion_info 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
        LEFT JOIN
        (SELECT
        di.fund_id,
        di.deletion_type,
        COUNT(di.fund_id) AS deletion_num
        FROM
        PPW_EMAIL.deletion_info di
        WHERE di.isvalid = 1
        AND di.remark IS NULL
        GROUP BY di.fund_id,
        di.deletion_type) ddn
        ON ddn.fund_id = d.fund_id
        AND d.deletion_type = ddn.deletion_type
        LEFT JOIN
        (SELECT
        dip.fund_id,
        dip.deletion_type,
        COUNT(dip.fund_id) AS processed_num
        FROM
        PPW_EMAIL.deletion_info dip
        WHERE dip.isvalid = 1
        AND dip.remark IS NOT NULL
        GROUP BY dip.fund_id,
        dip.deletion_type) ddp
        ON ddp.fund_id = d.fund_id
        AND d.deletion_type = ddp.deletion_type
        WHERE d.isvalid = 1
        AND info.isvalid = 1
        AND c.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="deletionType != null and deletionType != -1">
            and d.deletion_type=#{deletionType}
        </if>
        <if test="deletionStartNum != null and deletionStartNum != ''">
            and ddn.deletion_num >= #{deletionStartNum}
        </if>
        <if test="deletionEndNum != null and deletionEndNum != ''">
            and ddn.deletion_num <![CDATA[ <= ]]> #{deletionEndNum}
        </if>
        group by d.fund_id,d.deletion_type
        limit #{offset},#{pageSize}
    </select>
    <select id="countDeletion" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.DeletionPageQuery">
        select count(1) from (select distinct d.fund_id,d.deletion_type
        FROM
        PPW_EMAIL.deletion_info 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
        LEFT JOIN
        (SELECT
        di.fund_id,
        di.deletion_type,
        COUNT(di.fund_id) AS deletion_num
        FROM
        PPW_EMAIL.deletion_info di
        WHERE di.isvalid = 1
        AND di.remark IS NULL
        GROUP BY di.fund_id,
        di.deletion_type) ddn
        ON ddn.fund_id = d.fund_id
        AND d.deletion_type = ddn.deletion_type
        LEFT JOIN
        (SELECT
        dip.fund_id,
        dip.deletion_type,
        COUNT(dip.fund_id) AS processed_num
        FROM
        PPW_EMAIL.deletion_info dip
        WHERE dip.isvalid = 1
        AND dip.remark IS NOT NULL
        GROUP BY dip.fund_id,
        dip.deletion_type) ddp
        ON ddp.fund_id = d.fund_id
        AND d.deletion_type = ddp.deletion_type
        WHERE d.isvalid = 1
        AND info.isvalid = 1
        AND c.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="deletionType != null and deletionType != -1">
            and d.deletion_type=#{deletionType}
        </if>
        <if test="deletionStartNum != null and deletionStartNum != ''">
            and ddn.deletion_num >= #{deletionStartNum}
        </if>
        <if test="deletionEndNum != null and deletionEndNum != ''">
            and ddn.deletion_num <![CDATA[ <= ]]> #{deletionEndNum}
        </if>
        group by d.fund_id,d.deletion_type) a
    </select>
    <select id="getLastDeletionDateByFundId" resultType="java.lang.String">
        select max(deletion_date) from PPW_EMAIL.deletion_info where fund_id=#{fundId} and deletion_type=#{deletionType}
    </select>

    <resultMap id="BaseMap" type="com.simuwang.base.pojo.dos.FundDeletionInfoDO">
        <id column="id" property="id"/>
        <result column="fund_id" property="fundId"/>
        <result column="deletion_date" property="deletionDate"/>
        <result column="deletion_type" property="deletionType"/>
        <result column="remark" property="remark"/>
        <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>
    <select id="searchFundDeletionList" resultMap="BaseMap"
            parameterType="com.simuwang.base.pojo.dto.query.FundDeletionPageQuery">
        select id,fund_id,deletion_type,deletion_date,remark,isvalid,creatorid,updaterid,createtime,updatetime
        from PPW_EMAIL.deletion_info where isvalid=1 and fund_id=#{fundId} and remark is null
        limit #{offset},#{pageSize}
    </select>
    <select id="countFundDeletionList" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.FundDeletionPageQuery">
        select count(1)
            from PPW_EMAIL.deletion_info where isvalid=1 and fund_id=#{fundId} and remark is null
    </select>
    <select id="selectFundDeletionInfoVOList" resultType="com.simuwang.base.pojo.dos.FundDeletionInfoDO"
            parameterType="java.util.List">
        select id,fund_id,deletion_type,deletion_date,remark,isvalid,creatorid,updaterid,createtime,updatetime
        from PPW_EMAIL.deletion_info where isvalid=1 and remark is null
        <if test="fundIdList.size() > 0">
            and fund_id in
            <foreach collection="fundIdList" index="index" item="fundId" separator="," open="(" close=")">
                #{fundId}
            </foreach>
        </if>
    </select>
    <select id="getDeletionInfoDO" resultType="com.simuwang.base.pojo.dos.DeletionInfoDO"
            parameterType="com.simuwang.base.pojo.dos.DeletionInfoDO">
        select id,fund_id,deletion_type,deletion_date,remark,isvalid,creatorid,updaterid,createtime,updatetime
        from PPW_EMAIL.deletion_info where isvalid=1
        and fund_id=#{fundId} and deletion_date=#{deletionDate} and deletion_type=#{deletionType}
    </select>

</mapper>