<?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.zcxk.dao.UserMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.User" id="UserMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="tenantId" column="TENANT_ID" jdbcType="VARCHAR"/>
        <result property="username" column="USERNAME" jdbcType="VARCHAR"/>
        <result property="phone" column="PHONE" jdbcType="VARCHAR"/>
        <result property="companyOrgId" column="COMPANY_ORG_ID" jdbcType="INTEGER"/>
        <result property="deptOrgId" column="DEPT_ORG_ID" jdbcType="INTEGER"/>
        <result property="photo" column="PHOTO" jdbcType="VARCHAR"/>
        <result property="userType" column="USER_TYPE" jdbcType="VARCHAR"/>
        <result property="enableState" column="ENABLE_STATE" jdbcType="VARCHAR"/>
        <result property="remark" column="REMARK" jdbcType="VARCHAR"/>
        <result property="email" column="EMAIL" jdbcType="VARCHAR"/>
        <result property="status" column="STATUS" jdbcType="INTEGER"/>
        <result property="dateCreate" column="DATE_CREATE" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="CREATE_BY" jdbcType="VARCHAR"/>
        <result property="dateUpdate" column="DATE_UPDATE" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/>
        <result property="logo" column="LOGO" jdbcType="VARCHAR"/>
        <result property="webUrl" column="WEB_URL" jdbcType="VARCHAR"/>
        <result property="companyOrgState" column="companyOrgState" jdbcType="INTEGER"/>
        <result property="departmentOrgState" column="departmentOrgState" jdbcType="INTEGER"/>
        <result property="tenantState" column="TENANT_STATE" jdbcType="INTEGER"/>
        <result property="iotPhoneNumber" column="iot_phone_number" jdbcType="VARCHAR"/>
    </resultMap>


    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        ID, TENANT_ID, USERNAME, PHONE, COMPANY_ORG_ID, DEPT_ORG_ID, PHOTO, USER_TYPE, ENABLE_STATE, REMARK, EMAIL, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="UserMap">
        select
        <include refid="Base_Column_List"/>
        from uims_user
        where ID = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="UserMap">
        select
        <include refid="Base_Column_List"/>
        from uims_user
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="UserMap">
        select
       b.TENANT_STATE, a.*, b.LOGO,b.WEB_URL ,c.ORG_STATE companyOrgState,d.ORG_STATE departmentOrgState,
       b.iot_phone_number,b.customer_id from uims_user a join uims_tenant b on a.TENANT_ID=b.CODE
        join uims_org c on a.COMPANY_ORG_ID=c.id left join
        uims_org d on a.DEPT_ORG_ID=d.ID
        <where>
                          and a.status=1
            <if test="id != null">
                and ID = #{id}
            </if>
            <if test="tenantId != null and tenantId != ''">
                and a.TENANT_ID = #{tenantId}
            </if>
            <if test="username != null and username != ''">
                and USERNAME = #{username}
            </if>
            <if test="phone != null and phone != ''">
                and PHONE = #{phone}
            </if>
            <if test="companyOrgId != null">
                and COMPANY_ORG_ID = #{companyOrgId}
            </if>
            <if test="deptOrgId != null">
                and DEPT_ORG_ID = #{deptOrgId}
            </if>
            <if test="photo != null and photo != ''">
                and PHOTO = #{photo}
            </if>
            <if test="userType != null and userType != ''">
                and USER_TYPE = #{userType}
            </if>
            <if test="enableState != null and enableState != ''">
                and ENABLE_STATE = #{enableState}
            </if>
            <if test="remark != null">
                and REMARK = #{remark}
            </if>
            <if test="email != null and email != ''">
                and EMAIL = #{email}
            </if>
            <if test="status != null">
                and STATUS = #{status}
            </if>
            <if test="dateCreate != null">
                and DATE_CREATE = #{dateCreate}
            </if>
            <if test="createBy != null and createBy != ''">
                and CREATE_BY = #{createBy}
            </if>
            <if test="dateUpdate != null">
                and DATE_UPDATE = #{dateUpdate}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and UPDATE_BY = #{updateBy}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_user(ID, TENANT_ID, USERNAME, PHONE, COMPANY_ORG_ID, DEPT_ORG_ID, PHOTO, USER_TYPE, ENABLE_STATE, REMARK, EMAIL, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY)
        values ( #{id}, #{tenantId}, #{username}, #{phone}, #{companyOrgId}, #{deptOrgId}, #{photo}, #{userType}, #{enableState}, #{remark}, #{email}, #{status}, #{dateCreate}, #{createBy}, #{dateUpdate}, #{updateBy})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_user(ID, TENANT_ID, USERNAME, PHONE, COMPANY_ORG_ID, DEPT_ORG_ID, PHOTO, USER_TYPE,
        ENABLE_STATE, REMARK, EMAIL, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY)
        values
        <foreach collection="users" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.tenantId}, #{item.username}, #{item.phone}, #{item.companyOrgId}, #{item.deptOrgId},
            #{item.photo}, #{item.userType}, #{item.enableState}, #{item.remark}, #{item.email}, #{item.status},
            #{item.dateCreate}, #{item.createBy}, #{item.dateUpdate}, #{item.updateBy} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_user
        <set>
            <if test="tenantId != null and tenantId != ''">
                TENANT_ID = #{tenantId},
            </if>
            <if test="username != null and username != ''">
                USERNAME = #{username},
            </if>
            <if test="phone != null and phone != ''">
                PHONE = #{phone},
            </if>
            <if test="companyOrgId != null">
                COMPANY_ORG_ID = #{companyOrgId},
            </if>
            <if test="deptOrgId != null">
                DEPT_ORG_ID = #{deptOrgId},
            </if>
            <if test="photo != null and photo != ''">
                PHOTO = #{photo},
            </if>
            <if test="userType != null and userType != ''">
                USER_TYPE = #{userType},
            </if>
            <if test="enableState != null and enableState != ''">
                ENABLE_STATE = #{enableState},
            </if>
            <if test="remark != null">
                REMARK = #{remark},
            </if>
            <if test="email != null and email != ''">
                EMAIL = #{email},
            </if>
            <if test="status != null">
                STATUS = #{status},
            </if>
            <if test="dateCreate != null">
                DATE_CREATE = #{dateCreate},
            </if>
            <if test="createBy != null and createBy != ''">
                CREATE_BY = #{createBy},
            </if>
            <if test="dateUpdate != null">
                DATE_UPDATE = #{dateUpdate},
            </if>
            <if test="updateBy != null and updateBy != ''">
                UPDATE_BY = #{updateBy},
            </if>
        </set>
        where ID = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from uims_user where ID = #{id}
    </delete>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_user
    </select>
    <select id="selectPage" resultMap="UserMap">
        select
        <include refid="Base_Column_List"/>
        from uims_user
        <where>
            <if test="user.id != null">
                and ID = #{user.id}
            </if>
            <if test="user.tenantId != null and user.tenantId != ''">
                and TENANT_ID = #{user.tenantId}
            </if>
            <if test="user.username != null and user.username != ''">
                and USERNAME = #{user.username}
            </if>
            <if test="user.phone != null and user.phone != ''">
                and PHONE = #{user.phone}
            </if>
            <if test="user.companyOrgId != null">
                and COMPANY_ORG_ID = #{user.companyOrgId}
            </if>
            <if test="user.deptOrgId != null">
                and DEPT_ORG_ID = #{user.deptOrgId}
            </if>
            <if test="user.photo != null and user.photo != ''">
                and PHOTO = #{user.photo}
            </if>
            <if test="user.userType != null and user.userType != ''">
                and USER_TYPE = #{user.userType}
            </if>
            <if test="user.enableState != null and user.enableState != ''">
                and ENABLE_STATE = #{user.enableState}
            </if>
            <if test="user.remark != null">
                and REMARK = #{user.remark}
            </if>
            <if test="user.email != null and user.email != ''">
                and EMAIL = #{user.email}
            </if>
            <if test="user.status != null">
                and STATUS = #{user.status}
            </if>
            <if test="user.dateCreate != null">
                and DATE_CREATE = #{user.dateCreate}
            </if>
            <if test="user.createBy != null and user.createBy != ''">
                and CREATE_BY = #{user.createBy}
            </if>
            <if test="user.dateUpdate != null">
                and DATE_UPDATE = #{user.dateUpdate}
            </if>
            <if test="user.updateBy != null and user.updateBy != ''">
                and UPDATE_BY = #{user.updateBy}
            </if>
        </where>
    </select>
    <select id="findPermission" resultType="com.zcxk.model.Permission">
        select  c.PERMISSION_TYPE ,c.id roleId,c.ROLE_STATE roleState
          from uims_user a join uims_user_role b on a.id=b.USER_ID
        join uims_role c on b.ROLE_ID= c.ID
        where a.STATUS !=0 and b.status!=0 and c.status!=0
        and a.id=#{id}
    </select>
    <select id="findOrgRole" resultType="com.zcxk.model.ProgramItem">
         select d.ORG_ID orgId,c.ORG_TYPE newType from  uims_user_role b

        join uims_role_org d on b.ROLE_ID=d.ROLE_ID join uims_org c
        on c.id=d.ORG_ID
        where b.USER_ID=#{id} and b.status!=0 and d.status!=0
    </select>
    <select id="findOrgs" resultType="com.zcxk.entity.Org">
        select    ID, TENANT_ID, ORG_TYPE,
         PARENT_ORG_ID, ORG_AREA_ID, ORG_NAME, ORG_STATE, REMARK, ORG_LEADER_NAME,
        ORG_LEADER_PHONE, ORG_LEADER_EMAIL,
         ORG_LEADER_SEX, STATUS, DATE_CREATE,
         CREATE_BY, DATE_UPDATE, UPDATE_BY
         from uims_org
         <where>
             <if test="orgType != null and orgType != ''">
                 and ORG_TYPE=#{orgType}
             </if>

            and TENANT_ID=#{tenantId}
            and status!=0
         </where>

    </select>
    <select id="findLoginInfo" resultType="com.zcxk.dto.UserDto">
        select a.id, a.PHONE,b.TENANT_NAME,b.code tenantId,b.WEB_URL ,b.LOGO,a.USERNAME name
        from uims_user a  join uims_tenant b on
        a.TENANT_ID=b.CODE
        where a.PHONE=#{phone} and a.status!=0 and b.status!=0
    </select>

    <!-- 新增注册数据 -->
    <insert id="insertRegister" keyProperty="id" useGeneratedKeys="true">
        insert into uims_user_register( ID, USERNAME, PHONE, USER_TYPE,	REMARK,	STATUS,	CREATE_BY,DATE_CREATE,UPDATE_BY,DATE_UPDATE)
        values ( #{id}, #{username}, #{phone}, #{userType}, #{remark}, 1, #{createBy}, #{dateCreate}, #{updateBy}, #{dateUpdate})
    </insert>
    <!--  超级管理员查询 一级菜单List   -->
    <select id="findAllFirstMenu" resultType="com.zcxk.entity.MenuEntity">
        SELECT
         a.app_id appId
        FROM uims_menu a
        <where>
            a.status=1 and a.PARENT_MENU_ID=0 and a.SHOW_STATE=1 and  a.is_admin=1
        </where>

    </select>
    <!--  租户管理员查询租户菜单  -->
    <select id="findTenantFirstMenu" resultType="com.zcxk.entity.MenuEntity">
        select
         a.app_id appId
        from uims_tenant_menu b
        inner join uims_menu a on a.id= b.MENU_ID and a.status=1
        <where>
            b.status=1 and a.PARENT_MENU_ID=0 and a.show_state=1 and a.is_tenant=1
            <if test="tenantId != null  and tenantId != ''">
                and b.tenant_id = #{tenantId}
            </if>
        </where>
        order by b.MENU_SEQ
    </select>
    <select id="findUserFirstMenu" resultType="com.zcxk.entity.MenuEntity">
        SELECT

        a.app_id as appId

        from uims_user_role  userrole
        inner join uims_role_menu rolemenu on rolemenu.ROLE_ID=userrole.ROLE_ID and rolemenu.status=1
        inner join uims_menu a on a.id=rolemenu.MENU_ID and a.STATUS=1
        inner join uims_tenant_menu b on a.id= b.MENU_ID and b.status=1
        <if test="tenantId != null  and tenantId != ''">
            and b.tenant_id = #{tenantId}
        </if>
        <where>
            userrole.status=1 and a.PARENT_MENU_ID=0 and a.SHOW_STATE=1 and a.is_tenant=1
            <if test="userId != null  and userId != ''">
                and userrole.user_id = #{userId}
            </if>

        </where>
        order by b.MENU_SEQ
    </select>
    <select id="getAppSecret" resultType="string">
        select APP_SECRET_KEY from uims_app where APP_ID=#{appId}
    </select>
    <select id="getUniqueUserID" resultType="string">
        select uniq_id from uims_user where id=#{id}
    </select>
    <update id="updateUniqueUserID">
        update uims_user set uniq_id=#{uid} where id=#{id}
    </update>

    <select id="findThirdPartytLoginInfo" resultType="com.zcxk.entity.User">
        select login_account loginAccount,login_secret_key loginSecretKey,login_code loginCode,login_org_code loginOrgCode from uims_user  where  ID =#{userId}
    </select>
</mapper>