<?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.gxwebsoft.tower.mapper.TowerContractMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,
               b.project_name,
               c.name AS customer_name,
               d.company_name
        FROM tower_contract a
        LEFT JOIN tower_project b ON a.project_id = b.project_id
        LEFT JOIN tower_customer c ON a.customer_id = c.customer_id
        LEFT JOIN sys_company d ON a.company_id = d.company_id
        <where>
            <if test="param.contractId != null">
                AND a.contract_id = #{param.contractId}
            </if>
            <if test="param.projectId != null">
                AND a.project_id = #{param.projectId}
            </if>
            <if test="param.companyId != null">
                AND a.company_id = #{param.companyId}
            </if>
            <if test="param.customerId != null">
                AND a.customer_id = #{param.customerId}
            </if>
            <if test="param.keywords != null">
                AND (a.contact_number LIKE CONCAT('%', #{param.keywords}, '%')  OR b.project_name LIKE CONCAT('%', #{param.keywords}, '%'))
            </if>
            <if test="param.customerContact != null">
                AND a.customer_contact LIKE CONCAT('%', #{param.customerContact}, '%')
            </if>
            <if test="param.contactNumber != null">
                AND a.contact_number LIKE CONCAT('%', #{param.contactNumber}, '%')
            </if>
            <if test="param.signDate != null">
                AND a.sign_date LIKE CONCAT('%', #{param.signDate}, '%')
            </if>
            <if test="param.startDate != null">
                AND a.start_date LIKE CONCAT('%', #{param.startDate}, '%')
            </if>
            <if test="param.endDate != null">
                AND a.end_date LIKE CONCAT('%', #{param.endDate}, '%')
            </if>
            <if test="param.contractAmount != null">
                AND a.contract_amount = #{param.contractAmount}
            </if>
            <if test="param.contactAgreeAmount != null">
                AND a.contact_agree_amount = #{param.contactAgreeAmount}
            </if>
            <if test="param.isInStock != null">
                AND a.is_in_stock = #{param.isInStock}
            </if>
            <if test="param.autoSettle != null">
                AND a.auto_settle = #{param.autoSettle}
            </if>
            <if test="param.settleMethod != null">
                AND a.settle_method = #{param.settleMethod}
            </if>
            <if test="param.extendMonthDate != null">
                AND a.extend_month_date = #{param.extendMonthDate}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.tower.entity.TowerContract">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.tower.entity.TowerContract">
        <include refid="selectSql"></include>
    </select>

</mapper>
