<?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.iformall.mapper.InvestOperateRecordMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.iformall.domain.po.invest.InvestOperateRecordEntity" id="investOperateRecordMap">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="tbl" column="tbl"/>
        <result property="tid" column="tid"/>
        <result property="operator" column="operator"/>
        <result property="operateType" column="operate_type"/>
        <result property="content" column="content"/>
        <result property="createDate" column="create_date"/>
    </resultMap>

    <select id="selectCustomerPageCount" resultType="long">
        SELECT count(*)
        FROM (SELECT o.`id`,
                     o.`tbl`,
                     d.`customer_id` AS tid,
                     o.`operator`,
                     o.`operate_type`,
                     o.`content`,
                     o.`tenant_id`,
                     o.`create_date`
              FROM `invest_operate_record` o
                       LEFT JOIN `invest_demand` d ON d.`id` = o.`tid`
              WHERE tbl = 'invest_demand'
              UNION ALL
              SELECT `id`,
                     `tbl`,
                     `tid`,
                     `operator`,
                     `operate_type`,
                     `content`,
                     `tenant_id`,
                     `create_date`
              FROM `invest_operate_record` o
              WHERE o.tbl = 'invest_customer') t
        WHERE t.`tid` = #{tid}
    </select>

    <select id="selectCustomerPageByTid" resultMap="investOperateRecordMap">
        SELECT t.*
        FROM (SELECT o.`id`,
                     o.`tbl`,
                     d.`customer_id` AS tid,
                     o.`operator`,
                     o.`operate_type`,
                     o.`content`,
                     o.`tenant_id`,
                     o.`create_date`
              FROM `invest_operate_record` o
                       LEFT JOIN `invest_demand` d ON d.`id` = o.`tid`
              WHERE tbl = 'invest_demand'
              UNION ALL
              SELECT `id`,
                     `tbl`,
                     `tid`,
                     `operator`,
                     `operate_type`,
                     `content`,
                     `tenant_id`,
                     `create_date`
              FROM `invest_operate_record` o
              WHERE o.tbl = 'invest_customer') t
        WHERE t.`tid` = #{tid}
        ORDER BY t.`create_date` DESC
        LIMIT #{limitStart},#{limitSize}
    </select>

</mapper>