<?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.jeeplus.sys.mapper.UserMapper">

    <resultMap id="userResult" type="com.jeeplus.sys.service.dto.UserDTO">
        <id property="id" column="id"/>
        <result property="loginName" column="loginName"/>
        <result property="password" column="password"/>
        <result property="no" column="no"/>
        <result property="name" column="name"/>
        <result property="email" column="email"/>
        <result property="phone" column="phone"/>
        <result property="mobile" column="mobile"/>
        <result property="photo" column="photo"/>
        <result property="sign" column="sign"/>
        <result property="remarks" column="remarks"/>
        <result property="loginFlag" column="login_flag"/>
        <result property="loginIp" column="loginIp"/>
        <result property="isAdmin" column="isAdmin"/>
        <result property="loginDate" column="loginDate"/>
        <result property="manageOfficeIds" column="manage_office_ids"/>
        <result property="tenantDTO.id" column="tenantDTO.id"/>
        <result property="tenantDTO.name" column="tenantDTO.name"/>
        <result property="companyDTO.id" column="companyDTO.id"/>
        <result property="companyDTO.name" column="companyDTO.name"/>
        <result property="officeDTO.id" column="officeDTO.id"/>
        <result property="officeDTO.name" column="officeDTO.name"/>
        <result property="officeDTO.isPublic" column="officeDTO.isPublic"/>
        <result property="officeDTO.parentIds" column="officeDTO.parentIds"/>
        <collection property="roleDTOList" javaType="java.util.List" ofType="com.jeeplus.sys.service.dto.RoleDTO">
            <id property="id" column="roleDTO.id"/>
            <result property="name" column="roleDTO.name"/>
            <result property="enName" column="roleDTO.enName"/>
        </collection>
        <collection property="postDTOList" javaType="java.util.List" ofType="com.jeeplus.sys.service.dto.PostDTO">
            <id property="id" column="postDTO.id"/>
            <result property="name" column="postDTO.name"/>
            <result property="code" column="postDTO.code"/>
        </collection>
        <collection property="certDTOList" ofType="com.jeeplus.sys.service.dto.CertDTO" column="id" select="getCertListByUserId"></collection>
    </resultMap>

    <sql id="userColumns">
        a.id,
		a.tenant_id AS "tenantDTO.id",
    	a.company_id AS "companyDTO.id",
    	a.office_id AS "officeDTO.id",
    	a.login_name AS "loginName",
		a.is_admin AS "isAdmin",
    	a.password,
    	a.no,
		a.name,
		a.email,
		a.phone,
		a.mobile,
		a.login_ip AS "loginIp",
		a.login_date AS "loginDate",
		a.remarks,
		a.login_flag,
		a.other_service_flag,
		a.photo,
		a.qr_code,
		a.sign,
		a.create_by_id AS "createBy.id",
		a.create_time,
		a.update_by_id AS "updateBy.id",
		a.manage_office_ids AS "manageOfficeIds",
		a.update_time,
    	c.name AS "companyDTO.name",
    	c.parent_id AS "companyDTO.parent.id",
    	c.parent_ids AS "companyDTO.parentIds",
    	o.name AS "officeDTO.name",
    	o.is_public AS "officeDTO.isPublic",
    	o.parent_id AS "officeDTO.parent.id",
    	o.parent_ids AS "officeDTO.parentIds"
    </sql>

    <sql id="Cert_Column_List">
        cert.id,
        cert.create_by_id,
        cert.create_time,
        cert.update_by_id,
        cert.update_time,
        cert.del_flag,
        cert.remarks,
        cert.user_id,
        cert.type,
        cert.no,
        cert.authorities,
        cert.issued_date,
        cert.enroll_date,
        cert.expire_date,
        cert.enroll_cert_no,
        cert.profession,
        cert.file_url
    </sql>

    <sql id="userJoins">
        LEFT JOIN sys_office c ON c.id = a.company_id
		LEFT JOIN sys_office o ON o.id = a.office_id
    </sql>

    <select id="getCertListByUserId" resultType="com.jeeplus.sys.service.dto.CertDTO">
        select distinct
        <include refid="Cert_Column_List"></include>,
        (select
        group_concat(dv.label)
        from sys_cert sc
        left join sys_dict_value dv on sc.type=dv.value
        LEFT JOIN sys_dict_type dt on dv.dict_type_id=dt.id and dt.type='sys_cert_type'
        where dv.dict_type_id=(SELECT id FROM sys_dict_type where type='sys_cert_type')
        and sc.del_flag = '0'
        and su.id = sc.user_id
        ) as certType
        from sys_cert cert
        left join sys_dict_value dv on cert.type=dv.value and dv.del_flag = '0'
        left join sys_user su on su.id=cert.user_id and cert.del_flag = '0'
        where cert.del_flag = '0' and cert.user_id = #{id} and dv.dict_type_id=(SELECT id FROM sys_dict_type where type='sys_cert_type')
    </select>

    <!-- 获得用户 -->
    <select id="get" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>,
        r.id AS "roleDTO.id",
        r.name AS "roleDTO.name",
        r.en_name AS "roleDTO.enname",
        p.id AS "postDTO.id",
        p.code AS "postDTO.code",
        p.name AS "postDTO.name"
        FROM sys_user a
        <include refid="userJoins"/>
        LEFT JOIN sys_user_role ur ON ur.user_id = a.id
        LEFT JOIN sys_role r ON r.id = ur.role_id
        LEFT JOIN sys_user_post up ON up.user_id = a.id
        LEFT JOIN sys_post p ON p.id = up.post_id
        ${ew.customSqlSegment}
    </select>

    <select id="getManageOfficeIdsById" resultType="java.lang.String">
        select manage_office_ids from sys_user where id = #{id}
    </select>

    <!-- 获得用户 -->
    <select id="getFlowAbleById" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>,
        r.id AS "roleDTO.id",
        r.name AS "roleDTO.name",
        r.en_name AS "roleDTO.enname",
        p.id AS "postDTO.id",
        p.code AS "postDTO.code",
        p.name AS "postDTO.name"
        FROM sys_user a
        <include refid="userJoins"/>
        LEFT JOIN sys_user_role ur ON ur.user_id = a.id
        LEFT JOIN sys_role r ON r.id = ur.role_id
        LEFT JOIN sys_user_post up ON up.user_id = a.id
        LEFT JOIN sys_post p ON p.id = up.post_id
        ${ew.customSqlSegment}
    </select>


    <!-- 分页查询用户信息 -->
    <select id="findList" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>
        FROM sys_user a
        left join sys_cert sc on a.id = sc.user_id and sc.del_flag = '0'
        <include refid="userJoins"/>
        ${ew.customSqlSegment}
    </select>

    <!-- 分页查询用户信息 -->
    <select id="userFindPage" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>
        FROM sys_user a
        left join sys_cert sc on a.id = sc.user_id and sc.del_flag = '0'
        <include refid="userJoins"/>
        ${ew.customSqlSegment}
    </select>


    <!-- 分页查询用户信息 -->
    <select id="findList2" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>
        FROM sys_user a
        <include refid="userJoins"/>
        ${ew.customSqlSegment}
        and (a.is_admin is null or a.is_admin = '' or a.is_admin != 1)
    </select>

    <!-- 查询角色下的用户 -->
    <select id="findListByRole" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>,
        r.id AS "roleDTO.id",
        r.name AS "roleDTO.name",
        r.en_name AS "roleDTO.enname"
        FROM sys_user a
        <include refid="userJoins"/>
        LEFT JOIN sys_user_role ur ON ur.user_id = a.id
        LEFT JOIN sys_role r ON r.id = ur.role_id
        ${ew.customSqlSegment}
    </select>

    <!-- 查询岗位下的用户 -->
    <select id="findListByPostId" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>,
        p.id AS "postDTO.id",
        p.name AS "postDTO.name"
        FROM sys_user a
        <include refid="userJoins"/>
        LEFT JOIN sys_user_post up ON up.user_id = a.id
        LEFT JOIN sys_post p ON p.id = up.post_id
        <where>
            p.id = #{postId} and a.del_flag = 0
        </where>
    </select>

    <!-- 查询岗位下的用户 -->
    <select id="findListFlowAbleByPostId" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>,
        p.id AS "postDTO.id",
        p.name AS "postDTO.name"
        FROM sys_user a
        <include refid="userJoins"/>
        LEFT JOIN sys_user_post up ON up.user_id = a.id
        LEFT JOIN sys_post p ON p.id = up.post_id
        <where>
            p.id = #{postId} and a.del_flag = 0
        </where>
    </select>
    <select id="getUserByName" resultType="com.jeeplus.sys.service.dto.UserDTO">
select a.id, a.company_id as "companyDTO.id", a.office_id as "officeDTO.id", a.login_name as "loginName",
		a.password,
		a.no,
		a.name,
		a.email, a.phone,
		a.mobile,
		a.login_ip as "loginIp", a.login_date as "loginDate",
		a.remarks,
		a.login_flag, a.photo, a.qr_code, a.sign,
		a.create_by_id as "createBy.id", a.create_time, a.update_by_id as "updateBy.id",
		a.update_time
		from sys_user a
		where `name` = #{name}
    </select>
    <select id="getUserInfoByEnName" resultType="com.jeeplus.sys.service.dto.UserDTO">
        select a.id, a.company_id as "companyDTO.id", a.office_id as "officeDTO.id", a.login_name as "loginName", a.is_admin as "isAdmin", a.password, a.no, a.name, a.email, a.phone, a.mobile, a.login_ip as "loginIp", a.login_date as "loginDate", a.remarks, a.login_flag, a.photo, a.qr_code, a.sign, a.create_by_id as "createBy.id", a.create_time, a.update_by_id as "updateBy.id", a.update_time, c.name as "companyDTO.name", c.parent_id as "companyDTO.parent.id", c.parent_ids as "companyDTO.parentIds", o.name as "officeDTO.name", o.parent_id as "officeDTO.parent.id", o.parent_ids as "officeDTO.parentIds", r.id as "roleDTO.id", r.name as "roleDTO.name", r.en_name as "roleDTO.enname"
        from sys_user a
        left join sys_office c
        on c.id = a.company_id
        left join sys_office o
        on o.id = a.office_id
        left join sys_user_role ur
        on ur.user_id = a.id
        left join sys_role r
        on r.id = ur.role_id
        <where>
            r.id = (
            select id from sys_role where del_flag= 0
            <if test="enName != null and enName != ''">
                and en_name = #{enName}
            </if>
            ) and a.del_flag = 0
        </where>
        order by a.id
    </select>


    <!--删除用户角色关联数据-->
    <delete id="deleteUserRole">
        DELETE
        FROM sys_user_role
        WHERE user_id = #{userId}
    </delete>

    <!--插入用户角色关联数据-->
    <insert id="insertUserRole">
        INSERT INTO sys_user_role(user_id, role_id)
        VALUES (#{userId}, #{roleId})
    </insert>

    <!--删除用户岗位关联数据-->
    <delete id="deleteUserPost">
        DELETE
        FROM sys_user_post
        WHERE user_id = #{userId}
    </delete>

    <!--插入用户角色关联数据-->
    <insert id="insertUserPost">
        INSERT INTO sys_user_post(user_id, post_id)
        VALUES (#{userId}, #{postId})
    </insert>
    <insert id="insertWorkAttachment">
        INSERT INTO work_attachment (
            id,
            create_by_id,
            create_time,
            update_by_id,
            update_time,
            del_flag,
            url,
            type,
            attachment_id,
            attachment_name,
            attachment_flag,
            file_size,
            sort
            )
        VALUES(
            #{workAttachment.id},
            #{userDto.id},
            #{workAttachment.createTime},
            #{userDto.id},
            #{workAttachment.updateTime},
            #{workAttachment.delFlag},
            #{workAttachment.url},
            #{workAttachment.type},
            #{workAttachment.attachmentId},
            #{workAttachment.attachmentName},
            #{workAttachment.attachmentFlag},
            #{workAttachment.fileSize},
            #{workAttachment.sort}
            )
    </insert>

    <select id="getUserIdByName" resultType="string">
		select id from sys_user su where su.name = #{name} limit 1
	</select>

    <sql id="userOfficeColumns">
    	a.id,
    	a.login_name AS "loginName",
		a.is_admin AS "isAdmin",
    	a.password,
    	a.no,
		a.name,
		a.email,
		a.phone,
		a.mobile,
		a.login_ip AS "loginIp",
		a.login_date AS "loginDate",
		a.remarks,
		a.login_flag,
		a.other_service_flag,
		a.photo,
		a.qr_code,
		a.sign,
		a.create_by_id AS "createBy.id",
		a.create_time,
		a.update_by_id AS "updateBy.id",
		a.update_time,
    	o.id AS "parent.id",
    	o.parent_ids AS "parentIds"
    </sql>

    <!-- 查询岗位下的用户 -->
    <select id="getUserByOffice" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        SELECT
        <include refid="userOfficeColumns"/>
        ,#{type} as "type"
        FROM sys_user a
        <include refid="userJoins"/>
        <where>
            a.del_flag = 0
            <if test="officeIds != null and officeIds.size>0">
                and a.office_id in
                <foreach collection="officeIds" item="officeId" separator="," open="(" close=")">
                    #{officeId}
                </foreach>
            </if>
            and a.is_admin IS NULL
        </where>
    </select>
    <select id="selectListByName" resultType="com.jeeplus.sys.domain.User">
        select
        <include refid="userColumns"/>
        from sys_user a
        <include refid="userJoins"/>
        where a.name = #{name}
    </select>
    <select id="getAccountantUserCount" resultType="java.lang.Integer">
        select count(id) from cw_project_report
		    where del_flag = 0 and (signature_annotator1 = #{userId} or signature_annotator2 = #{userId})
		    and document_no like concat('%',#{yearStr},'%')
    </select>
    <select id="findList3" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        a.id,
        a.company_id AS "companyDTO.id",
        a.office_id AS "officeDTO.id",
        a.login_name AS "loginName",
        a.is_admin AS "isAdmin",
        a.PASSWORD,
        a.NO,
        a.NAME,
        a.email,
        a.phone,
        a.mobile,
        a.login_ip AS "loginIp",
        a.login_date AS "loginDate",
        a.remarks,
        a.login_flag,
        a.other_service_flag,
        a.photo,
        a.qr_code,
        a.sign,
        a.create_by_id AS "createBy.id",
        a.create_time,
        a.update_by_id AS "updateBy.id",
        a.update_time,
        a.manage_office_ids ,
        c.NAME AS "companyDTO.name",
        c.parent_id AS "companyDTO.parent.id",
        c.parent_ids AS "companyDTO.parentIds",
        o.NAME AS "officeDTO.name",
        o.parent_id AS "officeDTO.parent.id",
        o.parent_ids AS "officeDTO.parentIds",
        o.is_public AS "officeDTO.isPublic",
        a.roster_id
        FROM
        sys_post AS post
        LEFT JOIN sys_user_post AS user_post ON user_post.post_id = post.id
        LEFT JOIN sys_user AS a ON a.id = user_post.user_id
        AND a.del_flag = 0
        LEFT JOIN sys_cert sc ON a.id = sc.user_id
        AND sc.del_flag = '0'
        LEFT JOIN sys_office c ON c.id = a.company_id
        LEFT JOIN sys_office o ON o.id = a.office_id
        <where>
            AND post.NAME = '签字注师2'
            <if test="id != null and id != '' ">
                AND a.office_id = #{id}
            </if>
            <if test="loginName != null and loginName != '' ">
                AND a.login_name LIKE CONCAT('%', #{loginName}, '%')
            </if>
            AND post.del_flag = 0
        </where>
    </select>
    <select id="findList1" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
			a.id,
			a.company_id AS "companyDTO.id",
			a.office_id AS "officeDTO.id",
			a.login_name AS "loginName",
			a.is_admin AS "isAdmin",
			a.PASSWORD,
			a.NO,
			a.NAME,
			a.email,
			a.phone,
			a.mobile,
			a.login_ip AS "loginIp",
			a.login_date AS "loginDate",
			a.remarks,
			a.login_flag,
			a.other_service_flag,
			a.photo,
			a.qr_code,
			a.sign,
			a.create_by_id AS "createBy.id",
			a.create_time,
			a.update_by_id AS "updateBy.id",
			a.update_time,
			a.manage_office_ids ,
			c.NAME AS "companyDTO.name",
			c.parent_id AS "companyDTO.parent.id",
			c.parent_ids AS "companyDTO.parentIds",
			o.NAME AS "officeDTO.name",
			o.parent_id AS "officeDTO.parent.id",
			o.parent_ids AS "officeDTO.parentIds",
			o.is_public AS "officeDTO.isPublic",
			a.roster_id
		FROM
			sys_post AS post
				LEFT JOIN sys_user_post AS user_post ON user_post.post_id = post.id
				LEFT JOIN sys_user AS a ON a.id = user_post.user_id
				AND a.del_flag = 0
				LEFT JOIN sys_cert sc ON a.id = sc.user_id
				AND sc.del_flag = '0'
				LEFT JOIN sys_office c ON c.id = a.company_id
				LEFT JOIN sys_office o ON o.id = a.office_id
		WHERE
			post.NAME = '签字注师2'
            <if test="companyId != null and companyId != '' ">
                AND a.company_id = #{companyId}
            </if>
		  AND post.del_flag = 0
    </select>

    <update id="updateUserUpPassword">
		update sys_user set
		up_password = 1
		where id = #{id}
	</update>

    <select id="isUpdatePassword" resultType="string">
		select up_password from sys_user where del_flag = '0' and id = #{userId}
	</select>
    <select id="findAllList" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        a.id,
        a.company_id AS "companyDTO.id",
        a.office_id AS "officeDTO.id",
        a.login_name AS "loginName",
        a.is_admin AS "isAdmin",
        a.PASSWORD,
        a.NO,
        a.NAME,
        a.email,
        a.phone,
        a.mobile,
        a.login_ip AS "loginIp",
        a.login_date AS "loginDate",
        a.remarks,
        a.login_flag,
        a.other_service_flag,
        a.photo,
        a.qr_code,
        a.sign,
        a.create_by_id AS "createBy.id",
        a.create_time,
        a.update_by_id AS "updateBy.id",
        a.update_time,
        a.manage_office_ids ,
        c.NAME AS "companyDTO.name",
        c.parent_id AS "companyDTO.parent.id",
        c.parent_ids AS "companyDTO.parentIds",
        o.NAME AS "officeDTO.name",
        o.parent_id AS "officeDTO.parent.id",
        o.parent_ids AS "officeDTO.parentIds",
        o.is_public AS "officeDTO.isPublic",
        a.roster_id
        FROM
        sys_post AS post
        LEFT JOIN sys_user_post AS user_post ON user_post.post_id = post.id
        LEFT JOIN sys_user AS a ON a.id = user_post.user_id
        AND a.del_flag = 0
        LEFT JOIN sys_cert sc ON a.id = sc.user_id
        AND sc.del_flag = '0'
        LEFT JOIN sys_office c ON c.id = a.company_id
        LEFT JOIN sys_office o ON o.id = a.office_id
        WHERE
        <if test="companyId != null and companyId != '' ">
             a.company_id = #{companyId}
        </if>
        AND post.del_flag = 0
    </select>
    <select id="getAllUserInfo" resultType="com.jeeplus.sys.service.dto.UserDTO">
        select distinct id,name from sys_user where del_flag = '0'
    </select>
    <select id="getHasUser" resultType="java.lang.String">
        SELECT DISTINCT
    	a.office_id AS "id"
         FROM sys_user a
         LEFT JOIN sys_cert sc
         ON a.id = sc.user_id and sc.del_flag = '0'
         LEFT JOIN sys_office c
         ON c.id = a.company_id
         LEFT JOIN sys_office o
         ON o.id = a.office_id
         WHERE (sc.type = '6' AND a.del_flag = 0)
    </select>
    <select id="getHasUserFather" resultType="java.lang.String">
        SELECT DISTINCT
                o.parent_id AS "id"
         FROM sys_user a
         LEFT JOIN sys_cert sc
         ON a.id = sc.user_id and sc.del_flag = '0'
         LEFT JOIN sys_office c
         ON c.id = a.company_id
         LEFT JOIN sys_office o
         ON o.id = a.office_id
         WHERE (sc.type = '6' AND a.del_flag = 0)
    </select>

    <!-- 分页资质用户信息 -->
    <select id="findCertList" resultMap="userResult">
        SELECT
        <include refid="userColumns"/>
        FROM sys_user a
        left join sys_cert sc on a.id = sc.user_id and sc.del_flag = '0'
        <include refid="userJoins"/>
        ${ew.customSqlSegment}
    </select>
    <select id="getByIdNoTenantId" resultType="com.jeeplus.sys.service.dto.UserDTO">

    </select>

    <update id="updateEmail">
        update sys_user set email=#{userEmail} where id=#{id}
    </update>
    <update id="updateDelFlagById">
        update sys_user set del_flag = '1',login_flag = '0' where id=#{id}
    </update>

    <select id="getUserPostCountById" resultType="java.lang.Integer">
        SELECT
            count(a.id)
        FROM
            sys_user a
            LEFT JOIN sys_user_post sup ON sup.user_id = a.id
            LEFT JOIN sys_post sp ON sp.id = sup.post_id
        WHERE
            sp.CODE = 'bmzr'
            AND a.id = #{id}
    </select>
    <select id="getAllUserInfoByTentId" resultType="com.jeeplus.sys.service.dto.UserDTO">
        select distinct id,name from sys_user where del_flag = '0' and tenant_id IN (#{tentId1},#{tentId2})
    </select>
    <select id="getMobileById" resultType="java.lang.String">
        select mobile from sys_user where del_flag = '0' and id = #{id}
    </select>
    <select id="getByAttachmentId" resultType="com.jeeplus.sys.service.dto.WorkAttachmentDto2">
        SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
    </select>

</mapper>
