<?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.sckw.contract.dao.KwcContractTradeMapper">

    <resultMap id="BaseResultMap" type="com.sckw.contract.model.entity.KwcContractTrade">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="ent_id" column="ent_id" jdbcType="BIGINT"/>
            <result property="contract_no" column="contract_no" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="signing_way" column="signing_way" jdbcType="BIGINT"/>
            <result property="trading" column="trading" jdbcType="BIGINT"/>
            <result property="start_time" column="start_time" jdbcType="TIMESTAMP"/>
            <result property="end_time" column="end_time" jdbcType="TIMESTAMP"/>
            <result property="amount" column="amount" jdbcType="DECIMAL"/>
            <result property="performed_amount" column="performed_amount" jdbcType="DECIMAL"/>
            <result property="signing_url" column="signing_url" jdbcType="VARCHAR"/>
            <result property="signed_url" column="signed_url" jdbcType="VARCHAR"/>
            <result property="signing_no" column="signing_no" jdbcType="VARCHAR"/>
            <result property="contract_pid" column="contract_pid" jdbcType="BIGINT"/>
            <result property="remark" column="remark" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="INTEGER"/>
            <result property="create_by" column="create_by" jdbcType="BIGINT"/>
            <result property="create_time" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="update_by" column="update_by" jdbcType="BIGINT"/>
            <result property="update_time" column="update_time" jdbcType="TIMESTAMP"/>
            <result property="del_flag" column="del_flag" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,ent_id,contract_no,
        name,signing_way,trading,
        start_time,end_time,amount,
        performed_amount,signing_url,signed_url,
        signing_no,contract_pid,remark,
        status,create_by,create_time,
        update_by,update_time,del_flag
    </sql>


    <select id="queryList" resultType="com.sckw.contract.model.dto.res.QueryListResDto">
        select a.amount,
               a.trading,
               a.name contractName,
               a.contract_no,
               c.create_by initiateBy,
               c.create_time initiateTime,
               a.create_time,
               a.start_time,
               a.end_time,
               a.id,
               a.remark,
               a.signing_way,
               d.create_time signTime,
               a.status,
               b.unit_type,
               b.ent_id,
               b.firm_name entName,
               e.ent_id targetEntId,
               e.firm_name targetEntName
          from kwc_contract_trade a
          left join kwc_contract_trade_unit b on a.id = b.contract_id and b.unit_type = #{entType} and b.del_flag = 0
          left join kwc_contract_trade_track c on a.id = c.contract_id and c.del_flag = 0 and c.type = 1
          left join kwc_contract_trade_track d on a.id = d.contract_id and d.del_flag = 0 and d.type = 0
          left join kwc_contract_trade_unit e on a.id = b.contract_id and b.unit_type != #{entType} and e.del_flag = 0
         where a.del_flag = 0
           and b.ent_id = #{entId}
        <if test="startTime != null">
            and a.create_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and a.create_time &lt;= #{endTime}
        </if>
        <if test="status != null">
            and a.status = #{status}
        </if>
        <if test="keywords != null and keywords != ''">
            and (b.firm_name like concat('%', #{keywords}, '%') or b.contacts like concat('%', #{keywords}, '%') or b.phone like concat('%', #{keywords}, '%'))
        </if>
        <if test="idList != null and idList.size() > 0">
            and a.id in
            <foreach collection="idList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="trading != null">
            and a.trading = #{trading}
        </if>
        <if test="signingWay != null">
            and a.signing_way = #{signingWay}
        </if>
        <if test="charging != null">
            and a.charging = #{charging}
        </if>
    </select>
</mapper>
