<?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.capital.dao.mapper.CapitalRepairMapper" >
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.business.capital.dao.entity.CapitalRepair" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="mt_company_id" property="mtCompanyId" jdbcType="BIGINT" />
        <result column="pp_company_id" property="ppCompanyId" jdbcType="BIGINT" />
        <result column="pp_contact_id" property="ppContactId" jdbcType="BIGINT" />
        <result column="project_code" property="code" jdbcType="CHAR" />
        <result column="project_name" property="name" jdbcType="VARCHAR" />
        <result column="province_code" property="provinceCode" jdbcType="CHAR" />
        <result column="province" property="province" jdbcType="VARCHAR" />
        <result column="city_code" property="cityCode" jdbcType="CHAR" />
        <result column="city" property="city" jdbcType="VARCHAR" />
        <result column="district_code" property="districtCode" jdbcType="CHAR" />
        <result column="district" property="district" jdbcType="VARCHAR" />
        <result column="project_address" property="projectAddress" jdbcType="VARCHAR" />
        <result column="project_usage" property="projectUsage" jdbcType="INTEGER" />
        <result column="coordinate" property="coordinate" jdbcType="VARCHAR" />
        <result column="start_date" property="startDate" jdbcType="DATE" />
        <result column="end_date" property="endDate" jdbcType="DATE" />
        <result column="overdue_date" property="overdueDate" jdbcType="DATE" />
        <result column="overdue_reason" property="overdueReason" jdbcType="VARCHAR" />
        <result column="overdue_flag" property="overdueFlag" jdbcType="VARCHAR" />
        <result column="project_status" property="projectStatus" jdbcType="INTEGER" />
        <result column="push_flag" property="pushFlag" jdbcType="VARCHAR" />
        <result column="creator_id" property="creatorId" jdbcType="BIGINT" />
        <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
        <result column="update_id" property="updateId" jdbcType="TIMESTAMP" />
        <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
        <result column="region_id" property="regionId" jdbcType="BIGINT" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, mt_company_id, pp_company_id, pp_contact_id, project_code, project_name, province_code, province,
		city_code, city, district_code, district, project_address, project_usage, coordinate, start_date, end_date,
        overdue_date, overdue_reason, overdue_flag, project_status, push_flag, creator_id, create_date,
        update_id, update_date, region_id
    </sql>

    <select id="findByCondition" resultType="cn.com.ty.lift.business.capital.dao.entity.model.response.CapitalRepairResponse"
            parameterType="cn.com.ty.lift.business.capital.dao.entity.model.request.CapitalRepairRequest" >
        SELECT c.id              AS id,
               c.project_name    AS projectName,
               c.project_address AS projectAddress,
               c.start_date      AS startDate,
               c.end_date        AS endDate,
               c.region_id       AS regionId,
               c.project_status  AS projectStatus,
               r.area_name       AS regionName,
               ui.name           AS userName,
               pc.name           AS companyName,
               c.overdue_date    AS overdueDate,
               c.overdue_reason  AS overdueReason,
               c.overdue_flag    AS overdueFlag,
               c.start_work_datetime AS startWorkDatetime
        FROM capital_repair c
        LEFT JOIN region r ON c.region_id = r.id
        LEFT JOIN user_info ui ON r.user_id = ui.user_id
        LEFT JOIN property_company pc ON c.pp_company_id = pc.id
        WHERE c.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
        <if test="request.projectStatus!=null and request.projectStatus!=''">
            AND project_status = #{request.projectStatus,jdbcType=VARCHAR}
        </if>
        <if test="request.projectCode!=null and request.projectCode!=''">
            AND project_code LIKE concat('%',#{request.projectCode,jdbcType=VARCHAR},'%')
        </if>
        <if test="request.projectName!=null and request.projectName!=''">
            AND project_name LIKE concat('%',#{request.projectName,jdbcType=VARCHAR},'%')
        </if>
    </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.coordinate             AS coordinate,
                l.annual_inspection_date AS annualInspectionDate,
                pclr.id                  AS companyRelevanceId,
                crlr.id                  AS projectRelevanceId,
                pclr.lift_company_status AS liftStatus,
                crlr.project_id          AS projectId,
                crlr.worker_id           AS workerId,
                ui.name                  AS workerName,
                crlr.lift_locked         AS liftLocked
        FROM capital_repair_lift_relevance crlr
                LEFT JOIN lift l ON crlr.lift_id = l.id
                LEFT JOIN lift_brand lb ON l.lift_brand = lb.code
                LEFT JOIN user_info ui ON crlr.worker_id = ui.user_id
                LEFT JOIN platform_company_lift_relevance pclr ON crlr.lift_id = pclr.lift_id
                AND crlr.mt_company_id = pclr.mt_company_id
       <where>
           <if test="request.mtCompanyId!=null and request.mtCompanyId > 0">
               AND crlr.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
           </if>
           <if test="request.projectId!=null and request.projectId > 0">
               AND crlr.project_id = #{request.projectId,jdbcType=BIGINT}
           </if>
       </where>
    </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,
                crlr.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 capital_repair_lift_relevance crlr ON pclr.lift_id = crlr.lift_id
        AND pclr.mt_company_id = crlr.mt_company_id
                LEFT JOIN user_info ui ON crlr.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 crlr.project_id IS NULL
    </select>
</mapper>
