<?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.modules.sys.dao.CompanyDao">
    
	<sql id="companyColumns">
		a.id AS "id",
		a.name AS "name",
		a.company_id AS "companyId",
		a.licences AS "licences",
		a.number AS "number",
		a.user_id AS "tuser.id",
		a.legal_person AS "legalPerson",
		a.phone AS "phone",
		a.email AS "email",
		a.sort AS "sort",
		a.useable AS "USEABLE",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.useable_status AS "useableStatus",
		tuser.name AS "tuser.name",
		a.logo AS "logo"
	</sql>
	
	<sql id="companyJoins">
		LEFT JOIN sys_user tuser ON tuser.id = a.user_id
	</sql>
	
    
	<select id="get" resultType="Company" >
		SELECT 
			<include refid="companyColumns"/>
		FROM sys_company a
		<include refid="companyJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="checkNumber" resultType="java.lang.Integer" >
		SELECT
			count(*)
		FROM sys_company a
		WHERE a.number = #{number}
	</select>

	<select id="getByCompanyId" resultType="Company" >
		SELECT
		<include refid="companyColumns"/>
		FROM sys_company a
		<include refid="companyJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_DELETE}
            <if test="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </if>
        </where>
	</select>

    <select id="getByCompany" resultType="Company" >
        SELECT
        <include refid="companyColumns"/>
        FROM sys_company a
        <include refid="companyJoins"/>
        WHERE a.company_id = #{companyId}
    </select>

	<select id="findByName" resultType="Company" >
		SELECT
		<include refid="companyColumns"/>
		FROM sys_company a
		<include refid="companyJoins"/>
		WHERE a.name = #{name} and a.del_flag = 0
	</select>
	
	<select id="findList" resultType="Company" >
		SELECT 
			<include refid="companyColumns"/>
		FROM sys_company a
		<include refid="companyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name like
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
			</if>
			<if test="tuser != null and tuser != ''">
				AND a.user_id = #{tuser.id}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by  = #{createBy.id}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id  = #{companyId}
			</if>
			<if test="USEABLE != null and USEABLE != ''">
				AND a.USEABLE  = #{USEABLE}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findLists" resultType="Company" >
		SELECT
		DISTINCT
		<include refid="companyColumns"/>
		FROM sys_company a
		LEFT JOIN sys_user_office uo ON uo.company_id = a.company_id
		<include refid="companyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND uo.del_flag = #{DEL_FLAG_NORMAL}
			AND uo.status = '3'
			<if test="name != null and name != ''">
				AND a.name like
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
			</if>
			<if test="tuser != null and tuser != ''">
				AND a.user_id = #{tuser.id}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND uo.user_id  = #{createBy.id}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id  = #{companyId}
			</if>
			<if test="USEABLE != null and USEABLE != ''">
				AND a.USEABLE  = #{USEABLE}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="findAllList" resultType="Company" >
		SELECT 
			<include refid="companyColumns"/>
		FROM sys_company a
		<include refid="companyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO sys_company(
			id,
			name,
			company_id,
			licences,
			number,
			user_id,
			legal_person,
			phone,
			email,
			sort,
			useable,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			useable_status,
			logo
		) VALUES (
			#{id},
			#{name},
			#{companyId},
			#{licences},
			#{number},
			#{tuser.id},
			#{legalPerson},
			#{phone},
			#{email},
			#{sort},
			#{USEABLE},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{useableStatus},
			#{logo}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_company SET 	
			name = #{name},
			company_id = #{companyId},
			licences = #{licences},
			number = #{number},
			user_id = #{tuser.id},
			legal_person = #{legalPerson},
			phone = #{phone},
			email = #{email},
			sort = #{sort},
			useable = #{USEABLE},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			useable_status = #{useableStatus},
			remarks = #{remarks},
			logo = #{logo}
		WHERE id = #{id}
	</update>

	<insert id="updateNewCompany">
		INSERT INTO sys_user_role(user_id,role_id) VALUES (#{userId},'1')
	</insert>
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_company
		WHERE id = #{id}
	</update>
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_company SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>

	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Company" statementType="STATEMENT">
		select * FROM sys_company  where ${propertyName} = '${value}'
	</select>

	<!--根据公司id进行逻辑删除企业信息表sys_company-->
	<update id="updateCompanyId" >
		UPDATE sys_company SET
		del_flag = '1'
		WHERE company_id = #{companyId}
	</update>
	<!--根据公司id逻辑删除公司所对应的岗位-->
	<update id="updateRoleId">
		UPDATE sys_role set del_flag = '1' where company_id=#{companyId}
	</update>
	<!--根据公司id逻辑删除公司基本信息-->
	<update id="updateOfficeId">
		UPDATE sys_office set del_flag = '1' where parent_id=#{companyId} or id=#{companyId}
	</update>
	<!--根据公司id逻辑删除公司和用户基本信息-->
	<update id="updateUserOfficeId">
		UPDATE sys_user_office set del_flag = '1' where company_id=#{0} and user_id=#{1}
	</update>
	<!--根据公司id和用户id逻辑删除公司和用户基本信息-->
	<update id="deleteUserOfficeId">
		UPDATE sys_user_office set del_flag = '1' where company_id=#{companyId} and USER_ID=#{userId}
	</update>
	<!--根据公司id查询用户所在公司-->
	<select id="updateUserCompanyId" resultType="Useroffice" >
		select 	*  from sys_user_office	where  company_id = #{companyId} and del_flag='0'
	</select>
	<!--逻辑删除删除用户基本信息-->
	<update id="deleteCompanyInfo">
		UPDATE  sys_user_office set del_flag = '1',status='5'  where company_id = #{companyId}
		<if test="userId!=null and userId!=''">
			and user_id = #{userId}
		</if>
	</update>
	<!--根据公司id查询修改公司所在公司-->
	<update id="updateUserComOffId">
		UPDATE sys_user SET
		com_id = #{0} ,company_id = #{0},office_id=#{1}
		WHERE id = #{2}
	</update>
	<!--物理删除用户和岗位关联表-->
	<delete id="deleteRoleUserId">
		delete  from sys_user_role
		where role_id in (select id from sys_role   where company_id = #{0} and del_flag='0') and user_id =#{1}
	</delete>
	<select id="selectUser" resultType="User">
		select * from sys_user where id = #{userId}
	</select>
	<!--审核未通过和未审核数据进行物理删除-->
	<update id="deleteCompanyId">
		update sys_company set del_flag='1'
		WHERE id = #{id}
	</update>
	<!--手机号是否唯一-->
	<select id="findUniqueBymobile" resultType="User">
		select * from sys_user where mobile = #{mobile} and LENGTH(mobile) &gt; 1 and del_flag='0'
	</select>
	<select id="selectCompanyMobile" resultType="String">
		select mobile from  sys_user
		where id in (select user_id from sys_user_office where company_id=#{comId} and del_flag='0' and status not IN ('3','4'))
				and LENGTH(mobile) &gt; 1
		group by mobile
	</select>
	<!--根据用户id和公司id查询用户所在公司-->
	<select id="selectUserCompanyId" resultType="Useroffice" >
		select 	* from sys_user_office	where  del_flag='0' and user_id = #{userId})
	</select>
	<select id="selectUserCompanyIds"  resultType="Useroffice">
		select * from sys_user_office where user_id=#{id} and  del_flag='0'
	</select>
	<!---->
	<select id="selectCompanyUsers" resultType="User" >
		select user_id from sys_user_role 	where role_id in (select id from sys_role   where company_id = #{companyId} and del_flag='0') and user_id=#{userId}
	</select>
	<!--根据用户id和公司id物理删除用户和岗位关联表-->
	<delete id="deleteRoleUsers">
		delete  from sys_user_role
		where role_id in (select id from sys_role   where company_id = #{companyId}) and user_id=#{userId}
	</delete>
	<select id="selectUserSameRole" resultType="java.lang.String">
		select user_id from sys_user_role where user_id=#{userId} and role_id='1'
	</select>
	<!--根据公司id和用户id查询修改公司所在公司-->
	<delete id="updateUserOffId">
		UPDATE sys_user SET
		com_id = #{1} ,company_id = #{1},office_id=#{2}
		WHERE company_id = #{0} and id = #{3}
	</delete>
	<!--根据公司id和用户id进行逻辑删除企业信息表sys_company-->
	<update id="updateCompanyUserId" >
		UPDATE sys_company SET
		del_flag = '1'
		WHERE company_id = #{companyId} and user_id=#{userId}
	</update>

	<!--查询当前公司下所有员工对应的岗位-->
	<select id="getUserRoleManager" resultType="java.lang.String" >
		select role_id from sys_user_role where user_id in (select user_id from sys_user_office where company_id=#{companyId} and del_flag='0' group by user_id )
	</select>

	<!--查询当前用户在当前公司所在的岗位是不是管理员-->
	<select id="getUserRoleComManager" resultType="Role" >
		select * from sys_role where id in (select role_id from sys_user_role where user_id=#{userId}) and company_id=#{companyId} and  enname = 'gly' and del_flag='0'
	</select>
	<!--删除协同办公-->
	<delete id="deleteWorkAppShow">
		delete from work_app_show where  company_id=#{0} and user_id=#{1}
	</delete>

    <update id="logicDelete">
        UPDATE sys_company SET del_flag = '1' WHERE company_id = #{id}
    </update>
</mapper>