<?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.FundAliasMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.FundAliasDO">
        <id column="id" property="id"/>
        <result column="source_fund_name" property="sourceFundName"/>
        <result column="source_register_number" property="sourceRegisterNumber"/>
        <result column="target_fund_id" property="targetFundId"/>
        <result column="target_fund_name" property="targetFundName"/>
        <result column="target_register_number" property="targetRegisterNumber"/>
        <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>
    <update id="saveFundAlias" parameterType="com.simuwang.base.pojo.dos.FundAliasDO">
        update PPW_EMAIL.fund_alias set target_fund_id=#{targetFundId},target_fund_name=#{targetFundName},target_register_number=#{targetRegisterNumber},updatetime=#{updateTime}
        where isvalid =1 and id=#{id}
    </update>
    <update id="batchDelete">
        update PPW_EMAIL.fund_alias set isvalid=0,updatetime=sysdate(),updaterid=#{updaterId} where isvalid =1 and id in
        <foreach collection="idList" index="index" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </update>

    <select id="queryFundByNameAndRegisterNumber" resultMap="BaseResultMap">
        select target_fund_id, target_fund_name, target_register_number
        from PPW_EMAIL.fund_alias
        where isvalid = 1
          and source_fund_name = #{fundName}
          and source_register_number = #{registerNumber}
    </select>

    <select id="queryFundByName" resultMap="BaseResultMap">
        select target_fund_id, target_fund_name, target_register_number
        from PPW_EMAIL.fund_alias
        where isvalid = 1
          and source_fund_name = #{fundName} and source_register_number is null
    </select>

    <select id="queryFundByRegisterNumber" resultMap="BaseResultMap">
        select target_fund_id, target_fund_name, target_register_number
        from PPW_EMAIL.fund_alias
        where isvalid = 1
          and source_register_number = #{registerNumber} and source_fund_name is null
    </select>

    <select id="searchFundAlias" resultMap="BaseResultMap"
            parameterType="com.simuwang.base.pojo.dto.query.FundAliasPageQuery">
        select id,source_fund_name,source_register_number,target_fund_id,target_fund_name,target_register_number,isvalid,
        creatorid,createtime,updatetime,updaterid
        from PPW_EMAIL.fund_alias where isvalid=1
        <if test="fundName != null and fundName !=''">
            and (source_fund_name like concat('%',#{fundName},'%') or source_register_number like concat('%',#{fundName},'%') )
        </if>
        <if test="fundAliasName != null and fundAliasName !=''">
            and (target_fund_name like concat('%',#{fundAliasName},'%')  or target_register_number like concat('%',#{fundAliasName},'%') )
        </if>
        <if test="isMapping != null and isMapping == 0">
            and target_fund_id is null
        </if>
        <if test="isMapping != null and isMapping == 1">
            and target_fund_id is not null
        </if>
        order by updatetime desc
        limit #{offset},#{pageSize}
    </select>
    <select id="countFundAlias" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.FundAliasPageQuery">
        select count(1) from PPW_EMAIL.fund_alias where isvalid=1
        <if test="fundName != null and fundName !=''">
            and (source_fund_name like concat('%',#{fundName},'%') or source_register_number like concat('%',#{fundName},'%') )
        </if>
        <if test="fundAliasName != null and fundAliasName !=''">
            and (target_fund_name like concat('%',#{fundAliasName},'%')  or target_register_number like concat('%',#{fundAliasName},'%') )
        </if>
        <if test="isMapping != null and isMapping == 0">
            and target_fund_id is null
        </if>
        <if test="isMapping != null and isMapping == 1">
            and target_fund_id is not null
        </if>
    </select>
    <select id="searchFundAliasById" resultType="com.simuwang.base.pojo.dos.FundAliasDO"
            parameterType="java.lang.Integer">
        select id,source_fund_name,source_register_number,target_fund_id,target_fund_name,target_register_number,isvalid,
               creatorid,createtime,updatetime,updaterid
        from PPW_EMAIL.fund_alias where isvalid=1 and id=#{id}
    </select>

    <insert id="insert">
        insert into PPW_EMAIL.fund_alias(source_fund_name, source_register_number)
        values (#{fundName}, #{registerNumber})
    </insert>


</mapper>