<?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.house365.ws.dao.mapper.PropertyReportMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.PropertyReportEntity">
        <result property="id" column="id"/>
        <result property="customerId" column="customer_id"/>
        <result property="customerName" column="customer_name"/>
        <result property="sex" column="sex"/>
        <result property="phone" column="phone"/>
        <result property="areaMin" column="area_min"/>
        <result property="areaMax" column="area_max"/>
        <result property="district" column="district"/>
        <result property="districtId" column="district_id"/>
        <result property="totalPriceMin" column="total_price_min"/>
        <result property="totalPriceMax" column="total_price_max"/>
        <result property="downPaymentMin" column="down_payment_min"/>
        <result property="downPaymentMax" column="down_payment_max"/>
        <result property="channel" column="channel"/>
        <result property="roomType" column="room_type"/>
        <result property="hasAttach" column="has_attach"/>
        <result property="buyPurpose" column="buy_purpose"/>
        <result property="isLoan" column="is_loan"/>
        <result property="downPaymentRatio" column="down_payment_ratio"/>
        <result property="sendStatus" column="send_status"/>
        <result property="sendTime" column="send_time"/>

        <result property="createId" column="create_id"/>
        <result property="createName" column="create_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateId" column="update_id"/>
        <result property="updateName" column="update_name"/>
        <result property="updateTime" column="update_time"/>
        <result property="reportUrl" column="report_url"/>
        <result property="city" column="city"/>
    </resultMap>

    <insert id="save" useGeneratedKeys="true" keyProperty="id"  keyColumn="id" parameterType="com.house365.beans.entity.PropertyReportEntity">
        INSERT INTO property_report (
            customer_id,
            customer_name,
            sex,
            phone,
            area_min,
            area_max,
            district,
            district_id,
            total_price_min,
            total_price_max,
            down_payment_min,
            down_payment_max,
            channel,
            room_type,
            has_attach,
            buy_purpose,
            is_loan,
            down_payment_ratio,
            send_status,
            send_time,
            create_id,
            create_name,
            create_time,
            update_id,
            update_name,
            update_time,
            report_url,
            city
        )
        VALUES (
            #{customerId},
            #{customerName},
            #{sex},
            #{phone},
            #{areaMin},
            #{areaMax},
            #{district},
            #{districtId},
            #{totalPriceMin},
            #{totalPriceMax},
            #{downPaymentMin},
            #{downPaymentMax},
            #{channel},
            #{roomType},
            #{hasAttach},
            #{buyPurpose},
            #{isLoan},
            #{downPaymentRatio},
            #{sendStatus},
            #{sendTime},
            #{createId},
            #{createName},
            #{createTime},
            #{updateId},
            #{updateName},
            #{updateTime},
            #{reportUrl},
            #{city}
        )
    </insert>

    <select id="getById" resultMap="infoMap" parameterType="map">
        SELECT
        *
        FROM
        property_report
        where  id = #{id}
    </select>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT
        *
        FROM
        property_report
        where 1 = 1
        <if test="customerName != null and customerName != ''">
            AND customer_name LIKE CONCAT('%',#{customerName},'%')
        </if>
        <if test="sendStatus != null and sendStatus > 0 ">
            AND send_status = #{sendStatus}
        </if>
        <if test="phone != null and phone != ''">
            AND phone = #{phone}
        </if>
        <if test="city != null and city != ''">
            AND city = #{city}
        </if>
        <if test="startTime != null and startTime != ''">
            AND create_time between #{startTime} AND #{endTime}
        </if>
        <if test="customerId != null and customerId > 0 ">
            AND customer_id = #{customerId}
        </if>
        <if test="id != null and id > 0 ">
            AND id = #{id}
        </if>
    </select>

    <update id="update" parameterType="com.house365.beans.entity.PropertyReportEntity">
        UPDATE property_report
        SET
        sex = #{sex},
        total_price_min = #{totalPriceMin},
        total_price_max = #{totalPriceMax},
        down_payment_min = #{downPaymentMin},
        down_payment_max = #{downPaymentMax},
        area_min = #{areaMin},
        area_max = #{areaMax},
        channel = #{channel},
        room_type = #{roomType},
        has_attach = #{hasAttach},
        buy_purpose = #{buyPurpose},
        district = #{district},
        district_id = #{districtId},
        is_loan = #{isLoan},
        down_payment_ratio = #{downPaymentRatio},
        update_id = #{updateId},
        update_name = #{updateName},
        update_time = #{updateTime}
        WHERE id = #{id}
    </update>

    <update id="updateUrl" parameterType="map">
        UPDATE property_report
        SET
        report_url = #{reportUrl}
        WHERE id = #{id}
    </update>

    <update id="updateSendStatus" parameterType="map">
        UPDATE property_report
        SET
        send_status = #{sendStatus},
        send_time = #{sendTime}
        WHERE id = #{id}
    </update>

    <select id="queryList" resultType="java.util.HashMap" parameterType="map">
        SELECT
        id,customer_id AS customerId,customer_name as customerName,sex,phone,area_min as areaMin,area_max as areaMax,
        district,total_price_min as totalPriceMin,total_price_max as totalPriceMax,down_payment_min as downPaymentMin,
        down_payment_max as downPaymentMax,channel,room_type as roomType,has_attach as hasAttach,buy_purpose as buyPurpose,
        is_loan as isLoan,down_payment_ratio AS downPaymentRatio,send_status as sendStatus,send_time as sendTime,
        update_time as updateTime, create_time as createTime,update_id as updateId,report_url as reportUrl,city
        FROM
        property_report
        where 1 = 1
        <if test="customerName != null and customerName != ''">
            AND customer_name LIKE CONCAT('%',#{customerName},'%')
        </if>
        <if test="createId != null and createId > 0 ">
            AND (create_id = #{createId} OR create_id IN (SELECT id FROM user WHERE depturlpath LIKE (SELECT CONCAT(depturlpath,'/%') FROM user WHERE id = #{createId})))
        </if>
        <if test="sendStatus != null and sendStatus >= 0 ">
            AND send_status = #{sendStatus}
        </if>
        <if test="phone != null and phone != ''">
            AND phone = #{phone}
        </if>
        <if test="city != null and city != ''">
            AND city = #{city}
        </if>
        <if test="startTime != null and startTime != ''">
            AND create_time between #{startTime} AND #{endTime}
        </if>
        <if test="id != null and id > 0 ">
            AND id = #{id}
        </if>
        ORDER BY id DESC
        <if test="start != null and start >= 0 ">
            limit #{start},#{limit}
        </if>
    </select>

    <select id="queryCount" resultType="Integer" parameterType="map">
        SELECT count(*)
        FROM property_report
        where 1 = 1
        <if test="customerName != null and customerName != ''">
            AND customer_name LIKE CONCAT('%',#{customerName},'%')
        </if>
        <if test="createId != null and createId > 0 ">
            AND (create_id = #{createId} OR create_id IN (SELECT id FROM user WHERE depturlpath LIKE (SELECT CONCAT(depturlpath,'/%') FROM user WHERE id = #{createId})))
        </if>
        <if test="sendStatus != null and sendStatus >= 0 ">
            AND send_status = #{sendStatus}
        </if>
        <if test="phone != null and phone != ''">
            AND phone = #{phone}
        </if>
        <if test="city != null and city != ''">
            AND city = #{city}
        </if>
        <if test="startTime != null and startTime != ''">
            AND create_time between #{startTime} AND #{endTime}
        </if>
        <if test="id != null and id > 0 ">
            AND id = #{id}
        </if>
    </select>

</mapper>