<?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.business.library.dao.mapper.LiftMapper">
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.business.library.dao.entity.Lift">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="registration_code" property="registrationCode" jdbcType="VARCHAR"/>
        <result column="category" property="category" jdbcType="INTEGER"/>
        <result column="lift_type" property="liftType" jdbcType="INTEGER"/>
        <result column="lift_code" property="liftCode" jdbcType="CHAR"/>
        <result column="manufacture_date" property="manufactureDate" jdbcType="DATE"/>
        <result column="factory_code" property="factoryCode" jdbcType="VARCHAR"/>
        <result column="device_usage" property="deviceUsage" jdbcType="INTEGER"/>
        <result column="lift_brand" property="liftBrand" jdbcType="VARCHAR"/>
        <result column="install_company" property="installCompany" jdbcType="VARCHAR"/>
        <result column="lift_model" property="liftModel" jdbcType="VARCHAR"/>
        <result column="pulley_diameter" property="pulleyDiameter" jdbcType="DECIMAL"/>
        <result column="rope_num" property="ropeNum" jdbcType="INTEGER"/>
        <result column="lock_model" property="lockModel" jdbcType="VARCHAR"/>
        <result column="rated_load" property="ratedLoad" jdbcType="INTEGER"/>
        <result column="promote_height" property="promoteHeight" jdbcType="DECIMAL"/>
        <result column="step_width" property="stepWidth" jdbcType="DECIMAL"/>
        <result column="sidewalk_length" property="sidewalkLength" jdbcType="DECIMAL"/>
        <result column="tilt_angle" property="tiltAngle" jdbcType="DECIMAL"/>
        <result column="motor_power" property="motorPower" jdbcType="DECIMAL"/>
        <result column="rated_speed" property="ratedSpeed" jdbcType="DECIMAL"/>
        <result column="layer_station_door" property="layerStationDoor" jdbcType="VARCHAR"/>
        <result column="clamp_type" property="clampType" jdbcType="INTEGER"/>
        <result column="reform_company" property="reformCompany" jdbcType="VARCHAR"/>
        <result column="device_position" property="devicePosition" jdbcType="VARCHAR"/>
        <result column="coordinate" property="coordinate" jdbcType="VARCHAR"/>
        <result column="remarks" property="remarks" jdbcType="VARCHAR"/>
        <result column="creator_id" property="creatorId" jdbcType="BIGINT"/>
        <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
        <result column="steel_belt" property="steelBelt" jdbcType="INTEGER"/>
        <result column="cylinder_type" property="cylinderType" jdbcType="VARCHAR"/>
        <result column="cylinder_num" property="cylinderNum" jdbcType="INTEGER"/>
        <result column="top_type" property="topType" jdbcType="INTEGER"/>
        <result column="control_type" property="controlType" jdbcType="VARCHAR"/>
        <result column="mpa" property="mpa" jdbcType="INTEGER"/>
        <result column="factory" property="factory" jdbcType="VARCHAR"/>
        <result column="custom_number" property="customNumber" jdbcType="VARCHAR"/>
        <result column="use_company_code" property="useCompanyCode" jdbcType="VARCHAR"/>
        <result column="device_position_code" property="devicePositionCode" jdbcType="CHAR"/>
        <result column="agency" property="agency" jdbcType="VARCHAR"/>
        <result column="reform_date" property="reformDate" jdbcType="TIMESTAMP"/>
        <result column="install_date" property="installDate" jdbcType="TIMESTAMP"/>
        <result column="inner_floor" property="innerFloor" jdbcType="INTEGER"/>
        <result column="annual_inspection_date" property="annualInspectionDate" jdbcType="DATE"/>
        <result column="is_check_governer" property="isCheckGoverner" jdbcType="TINYINT" />
        <result column="load_inspection_setting" property="loadInspectionSetting" jdbcType="TINYINT" />
        <result column="area_code" property="areaCode" jdbcType="CHAR"/>
        <result column="device_id" property="deviceId" jdbcType="VARCHAR"/>
        <result column="device_type" property="deviceType" jdbcType="TINYINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, registration_code, category, lift_type, lift_code, manufacture_date, factory_code,
		device_usage, lift_brand, install_company, lift_model, pulley_diameter, rope_num, 
		lock_model, rated_load, promote_height, step_width, sidewalk_length, tilt_angle, 
		motor_power, rated_speed, layer_station_door, clamp_type, reform_company, device_position, 
		coordinate, remarks, creator_id, create_date, steel_belt, cylinder_type, cylinder_num, 
		top_type, control_type, mpa, factory, custom_number, use_company_code, device_position_code, 
		agency, reform_date, install_date, inner_floor, annual_inspection_date, area_code, device_id, device_type
    </sql>

    <!-- 根据区域，项目，电梯号，电梯品牌，电梯类型，维保工查询电梯列表信息-->
    <select id="findByCondition" resultType="cn.com.ty.lift.business.library.dao.entity.model.response.LiftResponse"
            parameterType="cn.com.ty.lift.business.library.dao.entity.model.request.LiftRequest">
        SELECT l.id              AS id,
        pclr.id                  AS companyRelevanceId,
        plr.id                   AS projectRelevanceId,
        l.lift_code              AS liftCode,
        l.registration_code      AS registrationCode,
        l.lift_type              AS liftType,
        l.device_position        AS devicePosition,
        lb.name                  AS liftBrand,
        pclr.lift_company_status AS liftStatus,
        plr.first_time           AS firstMaintenanceTime,
        p.project_name           AS projectName,
        p.id                     AS projectId,
        plr.lift_locked          AS liftLocked,
        plr.worker_id            AS workerId,
        ac.mobile                AS mobile
        FROM platform_company_lift_relevance pclr
        LEFT JOIN lift l ON pclr.lift_id = l.id
        LEFT JOIN project_lift_relevance plr ON pclr.mt_company_id = plr.mt_company_id AND pclr.lift_id = plr.lift_id
        LEFT JOIN project p ON pclr.mt_company_id = p.mt_company_id AND plr.project_id = p.id
        LEFT JOIN lift_brand lb ON l.lift_brand = lb.code
        LEFT JOIN user_account ac ON ac.user_id = plr.worker_id
        <where>
        <if test="request.mtCompanyId!=null and request.mtCompanyId!=''">
            AND pclr.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
        </if>
        <if test="request.liftStatus == 1">
            AND pclr.lift_company_status > 1 AND pclr.delete_flag = 0
        </if>
        <if test="request.liftStatus == 0">
            AND pclr.lift_company_status = 1 AND pclr.delete_flag = 0
        </if>
        <if test="request.liftType!=null and request.liftType!=''">
            AND l.lift_type = #{request.liftType,jdbcType=VARCHAR}
        </if>
        <if test="request.liftBrand!=null and request.liftBrand!=''">
            AND lb.code = #{request.liftBrand,jdbcType=VARCHAR}
        </if>
        <if test="request.registrationCode!=null and request.registrationCode!=''">
            AND l.registration_code LIKE #{request.registrationCode,jdbcType=VARCHAR}
        </if>
        <if test="request.regionId!=null and request.regionId!=''">
            AND p.region_id = #{request.regionId,jdbcType=BIGINT}
        </if>
        <if test="request.projectId!=null and request.projectId!=''">
            AND p.id = #{request.projectId,jdbcType=BIGINT}
        </if>
        </where>
    </select>

    <!-- 查询项目下电梯列表 -->
    <select id="findLiftListByProjectId"
            resultType="cn.com.ty.lift.business.library.dao.entity.model.response.LiftResponse"
            parameterType="cn.com.ty.lift.business.library.dao.entity.model.request.LiftRequest">
        SELECT
        l.id AS id,
        l.lift_code AS liftCode,
        l.registration_code AS registrationCode,
        l.lift_type AS liftType,
        l.device_position AS devicePosition,
        lb.name AS liftBrand,
        l.annual_inspection_date AS annualInspectionDate,
        pclr.id AS companyRelevanceId,
        pclr.lift_company_status AS liftStatus,
        plr.id AS projectRelevanceId,
        plr.project_id AS projectId,
        plr.worker_id AS workerId,
        plr.lift_locked AS liftLocked,
        ui.name AS workerName
        FROM project_lift_relevance plr
        LEFT JOIN lift l ON plr.lift_id = l.id
        LEFT JOIN lift_brand lb ON l.lift_brand = lb.code
        LEFT JOIN user_info ui ON plr.worker_id = ui.user_id
        LEFT JOIN platform_company_lift_relevance pclr ON plr.lift_id = pclr.lift_id AND pclr.id = plr.relevance_id
        WHERE 1=1
        <if test="request.mtCompanyId!=null and request.mtCompanyId!=''">
            AND plr.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
        </if>
        <if test="request.projectId!=null and request.projectId!=''">
            AND plr.project_id = #{request.projectId,jdbcType=BIGINT}
        </if>
    </select>

    <!-- 选择电梯 -->
    <select id="chooseLiftList" resultType="cn.com.ty.lift.business.library.dao.entity.model.response.LiftResponse"
            parameterType="cn.com.ty.lift.business.library.dao.entity.model.request.LiftRequest">
        SELECT  l.id                     AS id,
                l.lift_code              AS liftCode,
                l.registration_code      AS registrationCode,
                l.lift_type              AS liftType,
                l.device_position        AS devicePosition,
                lb.name                  AS liftBrand,
                l.annual_inspection_date AS annualInspectionDate,
                pclr.lift_company_status AS liftStatus,
                pclr.id                  AS companyRelevanceId,
                ui.name                  AS workerName,
                plr.worker_id            AS workerId
        FROM platform_company_lift_relevance pclr
                LEFT JOIN lift l ON pclr.lift_id = l.id
                LEFT JOIN lift_brand lb ON l.lift_brand = lb.code
                LEFT JOIN project_lift_relevance plr ON pclr.lift_id = plr.lift_id
        AND pclr.id = plr.relevance_id
                LEFT JOIN user_info ui ON plr.worker_id = ui.user_id
        WHERE pclr.lift_company_status != '1'
        AND l.lift_status = '2'
        <if test="request.mtCompanyId!=null and request.mtCompanyId!=''">
            AND pclr.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
        </if>
        AND plr.project_id IS NULL
    </select>

    <!--根据公司和项目查询电梯 -->
    <select id="listByCompanyAndProject" resultType="cn.com.ty.lift.business.library.dao.entity.model.LiftProjectModel">
        SELECT plr.id,
               plr.project_id,
               plr.lift_id,
               li.registration_code,
               plr.relevance_id,
               plr.worker_id,
               ui.`name` AS worker_name,
               plr.mt_company_id
        FROM lift li
                 LEFT JOIN project_lift_relevance plr ON li.id = plr.lift_id
                 LEFT JOIN user_info ui ON plr.worker_id = ui.user_id
        WHERE plr.mt_company_id = #{mtCompanyId}
          AND plr.project_id = #{projectId}
    </select>

    <select id="queryById" resultType="cn.com.ty.lift.business.library.dao.entity.model.response.LiftExtendResponse"
            parameterType="java.lang.Long">
        SELECT l.*,
               plr.id  AS relevanceId,
               plr.worker_id AS workerId,
               ui.name AS workerName
        FROM lift l
                 LEFT JOIN project_lift_relevance plr ON l.id = plr.lift_id
                 LEFT JOIN user_info ui ON plr.worker_id = ui.user_id
        WHERE l.id = #{id,jdbcType=BIGINT}
          AND plr.project_id = #{projectId}
    </select>

    <select id="queryLiftListByIdList" parameterType="java.util.List"
            resultType="cn.com.ty.lift.business.maintenance.dao.entity.model.response.LiftPrintResponse">
        SELECT l.id AS liftId,
        plr.project_id AS projectId,
        plr.mt_company_id AS mtCompanyId,
        l.registration_code AS registrationCode,
        l.device_position AS devicePosition
        FROM lift l
        LEFT JOIN project_lift_relevance plr ON l.id = plr.lift_id
        WHERE l.id IN
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <!--lift中的年检时间都是月初, 所以相隔时间(45)-->
    <select id="toConfirmAnnualInspection" parameterType="java.lang.Long" resultType="cn.com.ty.lift.business.library.dao.entity.model.response.LiftAnnualInspectionResponse">
        SELECT
            li.id                     AS lift_id,
            li.registration_code,
            pr.id                     AS project_id,
            pr.project_name,
            re.area_name,
            li.use_company_code,
            li.device_position,
            ui.name                   AS director_name,
            li.is_check_governer,
            li.load_inspection_setting,
            li.annual_inspection_date AS plan_date
        FROM lift li
        LEFT JOIN project_lift_relevance plr ON li.id = plr.lift_id
        LEFT JOIN project pr ON plr.project_id = pr.id
        LEFT JOIN region re ON re.id = pr.region_id
        LEFT JOIN user_info ui ON re.user_id = ui.user_id
        LEFT JOIN annual_inspection ai ON ai.lift_id = li.id AND ai.mt_company_id = plr.mt_company_id AND ai.plan_date = li.annual_inspection_date
        WHERE
            li.annual_inspection_date &lt;= date_add(curdate(), INTERVAL #{days} DAY)
        AND li.annual_inspection_date &gt;= curdate() AND isnull(ai.id)
        <if test="mtCompanyId != null and mtCompanyId > 0">
            AND plr.mt_company_id = #{mtCompanyId}
        </if>
    </select>
    <select id="groupByRegistrationCode" resultType="cn.com.ty.lift.business.library.dao.entity.Lift">
        SELECT
            *
        FROM
            lift
        WHERE
            registration_code IN (
                SELECT
                    registration_code
                FROM
                    lift
                GROUP BY
                    registration_code
                HAVING
                    count( * ) > 1
            )
        ORDER BY
            registration_code
    </select>
</mapper>