<?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.CapitalRepairLiftRelevanceMapper" >
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.business.capital.dao.entity.CapitalRepairLiftRelevance" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="mt_company_id" property="mtCompanyId" jdbcType="BIGINT" />
        <result column="project_id" property="projectId" jdbcType="BIGINT" />
        <result column="lift_id" property="liftId" jdbcType="VARCHAR" />
        <result column="worker_id" property="workerId" jdbcType="CHAR" />
        <result column="delete_flag" property="deleteFlag" 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" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, mt_company_id, project_id, lift_id, worker_id, delete_flag, creator_id, create_date, update_id, update_date
    </sql>

    <select id="findListByProjectId" resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectLiftResponse"
            parameterType="cn.com.ty.lift.business.capital.dao.entity.model.request.CapitalRepairRequest">
        SELECT
            l.id                AS id,
            l.registration_code AS registrationCode,
            l.lift_code         AS liftCode,
            lb.name             AS liftBrand,
            l.lift_type         AS liftType,
            l.use_company_code  AS useCompanyCode
        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
        WHERE crlr.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
        <if test="request.id!=null and request.id!=''">
            AND crlr.project_id = #{request.id,jdbcType=BIGINT}
        </if>
        <if test="request.registrationCode!=null and request.registrationCode!=''">
            AND l.registration_code LIKE CONCAT('%',#{request.registrationCode,jdbcType=VARCHAR},'%')
        </if>
    </select>

    <!--app端 根据公司id查询公司下大修项目列表 -->
    <select id="findCapitalListByCompanyId" parameterType="cn.com.ty.lift.business.capital.dao.entity.model.request.CapitalRepairRequest"
            resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectCompanyResponse">
        SELECT
        cr.id             AS projectId,
        cr.project_code   AS projectCode,
        cr.project_name   AS projectName,
        cr.project_usage  AS projectUsage,
        cr.project_status AS projectStatus,
        pu.user_id        AS userId,
        pu.is_monitor     AS isMonitor
        FROM capital_repair cr
        LEFT JOIN project_user pu ON cr.id = pu.project_id
        WHERE 1=1
        <if test="request.mtCompanyId!=null and request.mtCompanyId!=''">
            AND cr.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
        </if>
        <if test="request.userId!=null and request.userId!=''">
            AND pu.user_id = #{request.userId,jdbcType=BIGINT}
        </if>
        <if test="request.projectStatus!=null and request.projectStatus!=''">
            AND cr.project_status = #{request.projectStatus,jdbcType=VARCHAR}
        </if>
        <if test="request.projectName!=null and request.projectName!=''">
            AND cr.project_name LIKE CONCAT('%',#{request.projectName,jdbcType=VARCHAR},'%')
        </if>
    </select>

    <!--app端 根据项目id查询项目详情 -->
    <select id="findProjectById" parameterType="cn.com.ty.lift.business.capital.dao.entity.model.request.CapitalRepairRequest"
            resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectDetailResponse">
        SELECT t.*,
        ui.name         AS clerkName,
        pcc.name        AS companyContact,
        pcc.telephone   AS telephone
        FROM (SELECT cr.project_name  AS projectName,
                    cr.project_usage AS projectUsage,
                    cr.project_address AS address,
                    cr.province      AS province,
                    cr.city          AS city,
                    cr.district      AS district,
                    cr.start_date    AS startDate,
                    cr.end_date      AS endDate,
                    pc.name         AS companyName,
                    ui.name         AS areaDirector,
                    r.area_name     AS areaName,
                    r.clerk         AS clerk_id,
                    r.id            AS regionId,
                    cr.pp_contact_id AS contact_id,
                    cr.project_status AS projectStatus
        FROM capital_repair cr
                LEFT JOIN region r ON cr.region_id = r.id
                LEFT JOIN user_info ui ON r.user_id = ui.user_id
                LEFT JOIN property_company pc ON cr.pp_company_id = pc.id
        WHERE 1=1
        <if test="request.id!=null and request.id!=''">
            AND cr.id = #{request.id,jdbcType=BIGINT}
        </if>
        ) t
        LEFT JOIN property_contact pcc ON t.contact_id = pcc.id
        LEFT JOIN user_info ui ON ui.user_id = t.clerk_id
    </select>

    <select id="count" resultType="java.lang.Integer">
        SELECT count(1)
        FROM capital_repair_lift_relevance crlr
                 LEFT JOIN capital_repair cr ON crlr.project_id = cr.id
        WHERE crlr.mt_company_id = #{mtCompanyId}
          AND cr.project_status = #{projectStatus}
    </select>

    <select id="countDoingByUser" resultType="java.lang.Long" parameterType="cn.com.ty.lift.business.common.CommonRequest">
        SELECT
        count(1)
        FROM capital_repair cr
        LEFT JOIN project_user pu ON cr.id = pu.project_id AND cr.mt_company_id = pu.mt_company_id
        WHERE cr.project_status = '2'
        <if test="request.userId != null and request.userId > 0">
            AND pu.user_id = #{request.userId}
        </if>
        <if test="request.mtCompanyId != null and request.mtCompanyId > 0">
            AND cr.mt_company_id = #{request.mtCompanyId}
        </if>
    </select>
</mapper>
