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

    <resultMap id="infoMap" type="com.house365.beans.entity.CloudRuleEntity">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="channel" column="channel"/>
        <result property="channelName" column="channel_name"/>
        <result property="cityCode" column="city_code"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createId" column="create_id"/>
        <result property="createName" column="create_name"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <insert id="save" parameterType="com.house365.beans.entity.CloudRuleEntity">
        INSERT INTO cloud_rule (
            user_id,
            project_id,
            project_name,
            channel,
            channel_name,
            city_code,
            is_delete,
            create_id,
            create_name,
            create_time
        )
        VALUES (
            #{userId},
            #{projectId},
            #{projectName},
            #{channel},
            #{channelName},
            #{cityCode},
            #{isDelete},
            #{createId},
            #{createName},
            #{createTime}
        )
    </insert>

    <select id="getById" resultMap="infoMap" parameterType="map">
        SELECT
          *
        FROM
        cloud_rule
        where is_delete = 0 AND  id = #{id}
    </select>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT
          *
        FROM
        cloud_rule
        where is_delete = 0
        <if test="projectId != null and projectId > 0 ">
            AND project_id = #{projectId}
        </if>
        <if test="userId != null and userId > 0 ">
            AND user_id = #{userId}
        </if>
        <if test="notUserId != null and notUserId > 0 ">
            AND user_id != #{notUserId}
        </if>
        <if test="cityCode != null and cityCode != '' ">
            AND city_code = #{cityCode}
        </if>
        <if test="channel != null and channel > 0 ">
            AND channel = #{channel}
        </if>
        <if test="group != null and group > 0 ">
           group BY user_id
        </if>
        ORDER BY id DESC
    </select>

    <delete id="deleteByUserId" parameterType="Integer">
        DELETE FROM cloud_rule
        WHERE user_id = #{userId}
    </delete>

</mapper>