<?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.CloudActivityMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.CloudActivityEntity">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="customerCount" column="customer_count"/>
        <result property="waitCount" column="wait_count"/>
        <result property="sentCount" column="sent_count"/>
        <result property="city" column="city"/>
        <result property="createId" column="create_id"/>
        <result property="createName" column="create_name"/>
        <result property="createTime" column="create_time"/>
        <result property="isDelete" column="is_delete"/>

        <result property="totalPriceMin" column="total_price_min"/>
        <result property="totalPriceMax" column="total_price_max"/>
        <result property="areaMin" column="area_min"/>
        <result property="areaMax" column="area_max"/>
        <result property="block" column="block"/>
        <result property="blockName" column="block_name"/>
        <result property="district" column="district"/>
        <result property="districtName" column="district_name"/>
        <result property="channel" column="channel"/>
        <result property="channelName" column="channel_name"/>
        <result property="seeProjectName" column="see_project_name"/>
        <result property="seeStartTime" column="see_start_time"/>
        <result property="seeEndTime" column="see_end_time"/>
        <result property="seeCount" column="see_count"/>
        <result property="type" column="type"/>
    </resultMap>

    <sql id="sqlId">
        id,name,customer_count,wait_count,sent_count,city,create_id,create_name,create_time,is_delete,total_price_min,total_price_max,
        area_min,area_max,block,block_name,district,district_name,channel,channel_name,see_project_name,see_start_time,
        see_end_time,see_count,type
    </sql>

    <insert id="save" useGeneratedKeys="true" keyProperty="id" parameterType="com.house365.beans.entity.CloudActivityEntity">
        INSERT INTO cloud_activity (
            name,
            customer_count,
            wait_count,
            sent_count,
            city,
            create_id,
            create_name,
            create_time,
            is_delete,
            total_price_min,
            total_price_max,
            area_min,
            area_max,
            block,
            block_name,
            district,
            district_name,
            channel,
            channel_name,
            see_project_name,
            see_start_time,
            see_end_time,
            see_count,
            type
        )
        VALUES (
            #{name},
            #{customerCount},
            #{waitCount},
            #{sentCount},
            #{city},
            #{createId},
            #{createName},
            #{createTime},
            #{isDelete},
            #{totalPriceMin},
            #{totalPriceMax},
            #{areaMin},
            #{areaMax},
            #{block},
            #{blockName},
            #{district},
            #{districtName},
            #{channel},
            #{channelName},
            #{seeProjectName},
            #{seeStartTime},
            #{seeEndTime},
            #{seeCount},
            #{type}
        )
    </insert>

    <select id="getById" resultMap="infoMap" parameterType="map">
        SELECT
        <include refid="sqlId"/>
        FROM
        cloud_activity
        where is_delete = 0 AND id = #{id}
    </select>

    <select id="getByIdAndDel" resultMap="infoMap" parameterType="map">
        SELECT
        <include refid="sqlId"/>
        FROM
        cloud_activity
        where id = #{id}
    </select>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT
        <include refid="sqlId"/>
        FROM
        cloud_activity
        where is_delete = 0
        <if test="name != null and name != ''">
            AND name LIKE CONCAT('%',#{name},'%')
        </if>
        <if test="likeName != null and likeName != ''">
            AND name LIKE CONCAT('',#{likeName},'%')
        </if>
        <if test="createId != null and createId > 0 ">
            AND create_id = #{createId}
        </if>
        <if test="createName != null and createName != ''">
            AND create_name = #{createName}
        </if>
        <if test="startTime != null and startTime != ''">
            AND create_time between #{startTime} AND #{endTime}
        </if>
        <if test="city != null and city != ''">
            AND city = #{city}
        </if>
        <if test="id != null and id > 0 ">
            AND id = #{id}
        </if>
        <if test="type != null and type >= 0 ">
            AND type = #{type}
        </if>
        <if test="group != null and group > 0 ">
            group BY create_id
        </if>
    </select>

    <update id="updateCount" parameterType="com.house365.beans.entity.CloudActivityEntity">
        UPDATE cloud_activity
        SET
        sent_count = #{sentCount},
        customer_count = #{customerCount},
        wait_count = #{waitCount}
        WHERE id = #{id}
    </update>

    <select id="queryList" resultType="java.util.HashMap" parameterType="map">
        SELECT
        id,name,customer_count as customerCount,wait_count as waitCount,
        sent_count as sentCount,city,create_id as createId,create_name as createName,
        create_time as createTime,is_delete as isDelete
        FROM
        cloud_activity
        where is_delete = 0
        <if test="name != null and name != ''">
            AND name LIKE CONCAT('%',#{name},'%')
        </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="createName != null and createName != ''">
            AND create_name = #{createName}
        </if>
        <if test="createTime != null and createTime != ''">
            AND create_time between #{startTime} AND #{endTime}
        </if>
        <if test="city != null and city != ''">
            AND city = #{city}
        </if>
        <if test="type != null and type >= 0 ">
            AND type = #{type}
        </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 cloud_activity
        where is_delete = 0
        <if test="name != null and name != ''">
            AND name LIKE CONCAT('%',#{name},'%')
        </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="createName != null and createName != ''">
            AND create_name = #{createName}
        </if>
        <if test="createTime != null and createTime != ''">
            AND create_time between #{startTime} AND #{endTime}
        </if>
        <if test="city != null and city != ''">
            AND city = #{city}
        </if>
        <if test="type != null and type >= 0 ">
            AND type = #{type}
        </if>
    </select>

    <update id="updateStatus" parameterType="map">
        UPDATE cloud_activity
        SET
        is_delete = #{isDelete}
        WHERE id = #{id}
    </update>

</mapper>