<?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.ruoyi.project.business.mapper.TcAccountHistoryMapper">
    
    <resultMap type="TcAccountHistory" id="TcAccountHistoryResult">
        <result property="id"    column="id"    />
        <result property="accountId"    column="account_id"    />
        <result property="accountNo"    column="account_no"    />
        <result property="accountName"    column="account_name"    />
        <result property="countryId"    column="country_id"    />
        <result property="area"    column="area"    />
        <result property="areaId"    column="area_id"    />
        <result property="origin"    column="origin"    />
        <result property="phone"    column="phone"    />
        <result property="email"    column="email"    />
        <result property="address"    column="address"    />
        <result property="sector"    column="sector"    />
        <result property="registeredCapital"    column="registered_capital"    />
        <result property="level"    column="level"    />
        <result property="startTime"    column="start_time"    />
        <result property="owner"    column="owner"    />
        <result property="ownerAgent"    column="owner_agent"    />
        <result property="status"    column="status"    />
        <result property="type"    column="type"    />
        <result property="accountDesc"    column="account_desc"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="accountGroup"    column="account_group"    />
        <result property="version"    column="version"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
        <result property="nickName"    column="nickName"    />
        <result property="companyType"    column="company_type"    />
        <result property="intentionalProduct"    column="intentional_product"    />
        <result property="segment"    column="segment"    />
        <result property="companyWebsite"    column="company_website"    />
        <result property="background"    column="background"    />
        <result property="beginYear"    column="begin_year"    />
    </resultMap>

    <sql id="selectTcAccountHistoryVo">
        select id, account_id, account_no, account_name, country_id, area, area_id, origin,
               phone, email, address, sector, registered_capital, level, start_time, owner,
               owner_agent, status, type, account_desc, order_status, account_group, version,
               del_flag, create_time, create_by, update_time, update_by, remark,
               company_type, intentional_product, segment, company_website, background, begin_year
        from tc_account_history
    </sql>

    <select id="selectTcAccountHistoryList" parameterType="TcAccountHistory" resultMap="TcAccountHistoryResult">
        select h.*, u.nick_name as nickName from tc_account_history h
            left join sys_user u on u.user_id = h.create_by
        <where>  
            <if test="accountId != null "> and h.account_id = #{accountId}</if>
            <if test="accountNo != null  and accountNo != ''"> and h.account_no = #{accountNo}</if>
            <if test="accountName != null  and accountName != ''"> and h.account_name = #{accountName}</if>
            <if test="countryId != null "> and h.country_id = #{countryId}</if>
            <if test="area != null  and area != ''"> and h.area = #{area}</if>
            <if test="origin != null "> and h.origin = #{origin}</if>
            <if test="phone != null  and phone != ''"> and h.phone = #{phone}</if>
            <if test="email != null  and email != ''"> and h.email = #{email}</if>
            <if test="address != null  and address != ''"> and h.address = #{address}</if>
            <if test="sector != null  and sector != ''"> and h.sector = #{sector}</if>
            <if test="registeredCapital != null  and registeredCapital != ''"> and h.registered_capital = #{registeredCapital}</if>
            <if test="level != null "> and h.level = #{level}</if>
            <if test="startTime != null "> and h.start_time = #{startTime}</if>
            <if test="owner != null "> and h.owner = #{owner}</if>
            <if test="status != null "> and h.status = #{status}</if>
            <if test="type != null  and type != ''"> and h.type = #{type}</if>
            <if test="accountDesc != null  and accountDesc != ''"> and h.account_desc = #{accountDesc}</if>
            <if test="orderStatus != null "> and h.order_status = #{orderStatus}</if>
            <if test="accountGroup != null  and accountGroup != ''"> and h.account_group = #{accountGroup}</if>
            <if test="version != null "> and h.version = #{version}</if>
        </where>
    </select>
    
    <select id="selectTcAccountHistoryById" parameterType="Long" resultMap="TcAccountHistoryResult">
        <include refid="selectTcAccountHistoryVo"/>
        where id = #{id}
    </select>

    <select id="selectMaxVersion" resultType="java.lang.Long">
        select max(version) from tc_account_history where account_id = #{accountId}
    </select>

    <insert id="insertTcAccountHistory" parameterType="TcAccountHistory" useGeneratedKeys="true" keyProperty="id">
        insert into tc_account_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="accountId != null">account_id,</if>
            <if test="accountNo != null">account_no,</if>
            <if test="accountName != null">account_name,</if>
            <if test="countryId != null">country_id,</if>
            <if test="area != null">area,</if>
            <if test="areaId != null">area_id,</if>
            <if test="origin != null">origin,</if>
            <if test="phone != null">phone,</if>
            <if test="email != null">email,</if>
            <if test="address != null">address,</if>
            <if test="sector != null">sector,</if>
            <if test="registeredCapital != null">registered_capital,</if>
            <if test="level != null">level,</if>
            <if test="startTime != null">start_time,</if>
            <if test="owner != null">owner,</if>
            <if test="ownerAgent != null">owner_agent,</if>
            <if test="status != null">status,</if>
            <if test="type != null">type,</if>
            <if test="accountDesc != null">account_desc,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="accountGroup != null">account_group,</if>
            <if test="version != null">version,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
            <if test="companyType != null">company_type,</if>
            <if test="intentionalProduct != null">intentional_product,</if>
            <if test="segment != null">segment,</if>
            <if test="companyWebsite != null">company_website,</if>
            <if test="background != null">background,</if>
            <if test="beginYear != null">begin_year,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="accountId != null">#{accountId},</if>
            <if test="accountNo != null">#{accountNo},</if>
            <if test="accountName != null">#{accountName},</if>
            <if test="countryId != null">#{countryId},</if>
            <if test="area != null">#{area},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="origin != null">#{origin},</if>
            <if test="phone != null">#{phone},</if>
            <if test="email != null">#{email},</if>
            <if test="address != null">#{address},</if>
            <if test="sector != null">#{sector},</if>
            <if test="registeredCapital != null">#{registeredCapital},</if>
            <if test="level != null">#{level},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="owner != null">#{owner},</if>
            <if test="ownerAgent != null">#{ownerAgent},</if>
            <if test="status != null">#{status},</if>
            <if test="type != null">#{type},</if>
            <if test="accountDesc != null">#{accountDesc},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="accountGroup != null">#{accountGroup},</if>
            <if test="version != null">#{version},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyType != null">#{companyType},</if>
            <if test="intentionalProduct != null">#{intentionalProduct},</if>
            <if test="segment != null">#{segment},</if>
            <if test="companyWebsite != null">#{companyWebsite},</if>
            <if test="background != null">#{background},</if>
            <if test="beginYear != null">#{beginYear},</if>
         </trim>
    </insert>

    <update id="updateTcAccountHistory" parameterType="TcAccountHistory">
        update tc_account_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="accountNo != null">account_no = #{accountNo},</if>
            <if test="accountName != null">account_name = #{accountName},</if>
            <if test="countryId != null">country_id = #{countryId},</if>
            <if test="area != null">area = #{area},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="origin != null">origin = #{origin},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="address != null">address = #{address},</if>
            <if test="sector != null">sector = #{sector},</if>
            <if test="registeredCapital != null">registered_capital = #{registeredCapital},</if>
            <if test="level != null">level = #{level},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="owner != null">owner = #{owner},</if>
            <if test="ownerAgent != null">owner_agent = #{ownerAgent},</if>
            <if test="status != null">status = #{status},</if>
            <if test="type != null">type = #{type},</if>
            <if test="accountDesc != null">account_desc = #{accountDesc},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="accountGroup != null">account_group = #{accountGroup},</if>
            <if test="version != null">version = #{version},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyType != null">company_type = #{companyType},</if>
            <if test="intentionalProduct != null">intentional_product = #{intentionalProduct},</if>
            <if test="segment != null">segment = #{segment},</if>
            <if test="companyWebsite != null">company_website = #{companyWebsite},</if>
            <if test="background != null">background = #{background},</if>
            <if test="beginYear != null">begin_year = #{beginYear},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTcAccountHistoryById" parameterType="Long">
        delete from tc_account_history where id = #{id}
    </delete>

    <delete id="deleteTcAccountHistoryByIds" parameterType="String">
        delete from tc_account_history where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>