<?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.MtCompanyUserMapper">
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.system.user.dao.entity.MtCompanyUser">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="mt_company_id" property="mtCompanyId" jdbcType="BIGINT"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="user_role" property="userRole" jdbcType="TINYINT"/>
        <result column="is_current_company" property="isCurrentCompany" jdbcType="TINYINT"/>
        <result column="is_hx_registered" property="isHxRegistered" jdbcType="TINYINT"/>
        <result column="status" property="status" jdbcType="TINYINT"/>
        <result column="service_total" property="serviceTotal" jdbcType="INTEGER"/>
        <result column="satisfaction_total" property="satisfactionTotal" jdbcType="INTEGER"/>
        <result column="service_count" property="serviceCount" jdbcType="INTEGER"/>
        <result column="maintenance_total" property="maintenanceTotal" jdbcType="INTEGER"/>
        <result column="emergency_total" property="emergencyTotal" jdbcType="INTEGER"/>
        <result column="lift_total" property="liftTotal" jdbcType="INTEGER"/>
        <result column="job" property="job" jdbcType="VARCHAR"/>
        <result column="delete_flag" property="deleteFlag" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
        <result column="delete_user_id" property="deleteUserId" jdbcType="BIGINT"/>
        <result column="repair_flag" property="repairFlag" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
		id, mt_company_id, user_id, user_role, is_current_company, is_hx_registered, status,
		service_total, satisfaction_total, service_count, maintenance_total, emergency_total, 
		lift_total, job, delete_flag, create_time, update_time, create_user_id,
		update_user_id, repair_flag
	</sql>

    <!-- 获取用户在当前公司所处的项目 -->
    <select id="getCompanyUserProject" parameterType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser"
            resultType="java.lang.Long">
		select count(1) from project_user pu
		where pu.mt_company_id = #{companyId,jdbcType=BIGINT}
		and pu.user_id = #{userId,jdbcType=BIGINT}
	</select>

    <!-- 获取用户在当前公司有没有正在进行的急修任务 -->
    <select id="getCompanyUserEmergency" parameterType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser"
            resultType="java.lang.Long">
		select count(1) from emergency_repair er
		where er.mt_company_id = #{companyId,jdbcType=BIGINT}
		and er.worker_id = #{userId, jdbcType=BIGINT}
		and er.status not in (2,3)
	</select>

    <!-- 获取用户电梯操作证信息 -->
    <select id="getLiftCertificateList" parameterType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser"
            resultType="cn.com.ty.lift.system.user.dao.entity.model.LiftCertificate">
        select
        owner_id as userId,
        code as code,
        issuance_agency as issuanceAgency,
        certificate_type as certificateType,
        first_img_url as firstImgUrl,
        second_img_url as secondImgUrl,
        status as status
        from lift_certificate
        where 1=1
        <!-- 判断用户id是否在集合中 -->
        <if test="userIdList != null">
            and owner_id in
            <foreach item="item" index="index" collection="userIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and mt_company_id = #{companyId,jdbcType=BIGINT}
    </select>

    <select id="getLiftCertificateByUserIdAndCompanyId"
            parameterType="cn.com.ty.lift.system.user.dao.entity.model.ProjectUser"
            resultType="cn.com.ty.lift.system.user.dao.entity.model.LiftCertificate">
		select owner_id as userId, mt_company_id as companyId, status as status from lift_certificate
		where owner_id = #{userId,jdbcType=BIGINT}
		and mt_company_id = #{companyId,jdbcType=BIGINT}
	</select>

    <!-- 通过项目id获取项目信息 -->
    <select id="getProjectById"
            parameterType="java.lang.Long"
            resultType="cn.com.ty.lift.system.user.dao.entity.model.ProjectInfo">
        select
            p.id as projectId,
            p.project_name as projectName,
            r.user_id as regionDirectorId
        from project as p
        left join
            region r
        on
            p.region_id = r.id
        where
            p.id = #{projectId}

    </select>

</mapper>