<?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="cn.com.ty.lift.system.user.dao.mapper.UserAccountMapper">
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.system.user.dao.entity.UserAccount">
        <id column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="type" property="type" jdbcType="TINYINT"/>
        <result column="account" property="account" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="salt" property="salt" jdbcType="VARCHAR"/>
        <result column="mobile" property="mobile" jdbcType="CHAR"/>
        <result column="wechat_open_id" property="wechatOpenId" jdbcType="VARCHAR"/>
        <result column="email" property="email" jdbcType="VARCHAR"/>
        <result column="other_account" property="otherAccount" jdbcType="VARCHAR"/>
        <result column="other_account2" property="otherAccount2" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="TINYINT"/>
        <result column="remarks" property="remarks" jdbcType="VARCHAR"/>
        <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
        <result column="delete_flag" property="deleteFlag" jdbcType="INTEGER"/>
        <result column="expert_flag" property="expertFlag" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
		user_id, type, account, password, salt, mobile, wechat_open_id, email, other_account, 
		other_account2, status, remarks, create_date, delete_flag, expert_flag
	</sql>

    <!--通过用户id和项目id获取用户项目信息 -->
    <select id="getProjectUserByUserIdAndProjectId"
            parameterType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser"
            resultType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser">
		select
		    project_id as projectId,
		    user_id as userId
		from
		    project_user pu
		where
		    pu.user_id = #{userId}
		and
		    pu.project_id = #{projectId}
	</select>

    <!-- 保存用户 -->
    <insert id="saveProjectUser" parameterType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser">
        insert into project_user (
            project_id,
            user_id,
            mt_company_id,
            user_role,
            is_monitor
        )
        values (
            #{projectId},
            #{userId},
            #{companyId},
            #{userRole},
            #{isMonitor})
    </insert>

    <!-- 查询物业用户列表查询条件 -->
    <sql id="projectUserQueryCondition">
        from
            project_user
        where
            mt_company_id = #{companyId}
        <if test = "projectId != null">
            and project_id = #{projectId}
        </if>
        <if test="userIdList != null">
            and user_id in
            <foreach item="item" index="index" collection="userIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and
        <!-- 物业用户才有评价权限 -->
        user_role in ('21','22')
    </sql>

    <!-- 查询物业用户列表 -->
    <select id="getProjectUserList" parameterType="cn.com.ty.lift.system.user.dao.entity.model.PropertyUserRequest"
            resultType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser">
        select
            project_id as projectId,
            user_id as userId,
            user_role as userRole
        <include refid="projectUserQueryCondition"/>
        limit #{pageNum}, #{pageSize}
    </select>

    <!-- 查询物业用户条数 -->
    <select id="getProjectUserCount" parameterType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser"
            resultType="java.lang.Long">
        select
        count(1)
        <include refid="projectUserQueryCondition"/>
    </select>

    <!--获取项目信息-->
    <select id="getProjectInfoList" resultType="cn.com.ty.lift.system.user.dao.entity.model.ProjectInfo">
        select
        id as projectId,
        project_name as projectName,
        region_id as regionId
        from
        project
        where
        id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <!-- 获取项目区域信息 -->
    <select id="getRegionInfoList" resultType="cn.com.ty.lift.system.user.dao.entity.model.RegionInfo">
        select
        id as regionId,
        area_name as areaName
        from
        region
        where
        id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>

    </select>

    <!-- 删除项目用户，条件：用户id，项目id-->
    <delete id="deleteProjectUser" parameterType="cn.com.ty.lift.system.user.dao.entity.model.PropertyUserRequest">
        delete from
            project_user
        where
            user_id = #{userId}
        and
            project_id = #{projectId}
    </delete>
</mapper>