<?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.countrygarden.bi.main.dao.performance.UserTargetMapper">
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.performance.UserTarget">
        <constructor>
            <idArg column="utid" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="userCode" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="elementId" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="couldRead" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="couldWrite" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="couldLock" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="couldUnLock" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="isDelete" javaType="java.lang.Integer" jdbcType="INTEGER"/>
            <arg column="createDate" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="createBy" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="updateDate" javaType="java.lang.String" jdbcType="VARCHAR"/>
            <arg column="updateBy" javaType="java.lang.String" jdbcType="VARCHAR"/>
        </constructor>
    </resultMap>
    <sql id="Base_Column_List">
    utid, userCode, elementId, couldRead, couldWrite, couldLock, couldUnLock , isDelete, createDate,
    createBy, updateDate, updateBy
  </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from user_target
        where utid = #{utid,jdbcType=INTEGER}
    </select>

    <!--分页，查询所有的userCode-->
    <select id="selectAllUserCode" resultType="java.lang.String" parameterType="java.util.Map">
        select userCode from user_target where utid in (select max(utid) from user_target group by userCode) and isDelete=0 order by utid desc
        <if test="pageCurrent!=-1">
            limit #{pageCurrent},#{pageSize}
        </if>
    </select>

    <select id="countSelectAllUserCode" resultType="java.lang.Long">
        select count(userCode) from user_target WHERE utid IN (SELECT max(utid) FROM user_target GROUP BY userCode ) AND isDelete = '0' ORDER BY utid DESC
    </select>

    <!--查询是否具有相应权限-->
    <select id="selectCouldPower" parameterType="cn.com.countrygarden.bi.main.model.performance.UserTarget"
            resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from user_target
        where userCode = #{usercode,jdbcType=VARCHAR} and elementId = #{elementid,jdbcType=VARCHAR}
    </select>

    <!-- 验证是否存在userCode-->
    <select id="selectExistUser" parameterType="java.lang.String" resultType="java.lang.Long">
        select count(*) from user_target where userCode = #{usercode}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user_target
    where utid = #{utid,jdbcType=INTEGER}
  </delete>

    <!--删除同一userCode的所有记录-->
    <delete id="deleteUserCodeAll" parameterType="java.lang.String">
        delete from user_target
        where userCode = #{usercode}
    </delete>

    <insert id="insertSelective" parameterType="cn.com.countrygarden.bi.main.model.performance.UserTarget">
        insert into user_target
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="utid != null">
                utid,
            </if>
            <if test="usercode != null">
                userCode,
            </if>
            <if test="elementid != null">
                elementId,
            </if>
            <if test="couldread != null">
                couldRead,
            </if>
            <if test="couldwrite != null">
                couldWrite,
            </if>
            <if test="couldlock != null">
                couldLock,
            </if>
            <if test="couldunlock != null">
                couldUnLock,
            </if>
            <if test="isdelete != null">
                isDelete,
            </if>
            <if test="createdate != null">
                createDate,
            </if>
            <if test="createby != null">
                createBy,
            </if>
            <if test="updatedate != null">
                updateDate,
            </if>
            <if test="updateby != null">
                updateBy,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="utid != null">
                #{utid,jdbcType=INTEGER},
            </if>
            <if test="usercode != null">
                #{usercode,jdbcType=VARCHAR},
            </if>
            <if test="elementid != null">
                #{elementid,jdbcType=VARCHAR},
            </if>
            <if test="couldread != null">
                #{couldread,jdbcType=INTEGER},
            </if>
            <if test="couldwrite != null">
                #{couldwrite,jdbcType=INTEGER},
            </if>
            <if test="couldlock != null">
                #{couldlock,jdbcType=INTEGER},
            </if>
            <if test="couldunlock != null">
                #{couldunlock,jdbcType=INTEGER},
            </if>
            <if test="isdelete != null">
                #{isdelete,jdbcType=INTEGER},
            </if>
            <if test="createdate != null">
                #{createdate,jdbcType=VARCHAR},
            </if>
            <if test="createby != null">
                #{createby,jdbcType=VARCHAR},
            </if>
            <if test="updatedate != null">
                #{updatedate,jdbcType=VARCHAR},
            </if>
            <if test="updateby != null">
                #{updateby,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="cn.com.countrygarden.bi.main.model.performance.UserTarget">
        update user_target
        <set>
            <if test="usercode != null">
                userCode = #{usercode,jdbcType=VARCHAR},
            </if>
            <if test="elementid != null">
                elementId = #{elementid,jdbcType=VARCHAR},
            </if>
            <if test="couldread != null">
                couldRead = #{couldread,jdbcType=INTEGER},
            </if>
            <if test="couldwrite != null">
                couldWrite = #{couldwrite,jdbcType=INTEGER},
            </if>
            <if test="couldlock != null">
                couldLock = #{couldlock,jdbcType=INTEGER},
            </if>
            <if test="couldunlock != null">
                couldUnLock = #{couldUnLock,jdbcType=INTEGER},
            </if>
            <if test="isdelete != null">
                isDelete = #{isdelete,jdbcType=INTEGER},
            </if>
            <if test="createdate != null">
                createDate = #{createdate,jdbcType=VARCHAR},
            </if>
            <if test="createby != null">
                createBy = #{createby,jdbcType=VARCHAR},
            </if>
            <if test="updatedate != null">
                updateDate = #{updatedate,jdbcType=VARCHAR},
            </if>
            <if test="updateby != null">
                updateBy = #{updateby,jdbcType=VARCHAR},
            </if>
        </set>
        where utid = #{utid,jdbcType=INTEGER}
    </update>

    <!--根据userCode更新删除状态-->
    <update id="updateIsDelete" parameterType="java.lang.String">
        UPDATE user_target SET isDelete=1 WHERE userCode = #{usercode,jdbcType=VARCHAR}
    </update>
</mapper>