<?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.homepage.dao.mapper.TodoDataMapper">

    <!-- 分页查询sql -->
    <sql id="pageQuerySql">
        limit #{pageNum} , #{pageSize}
    </sql>

    <!-- 获取团队申请待办信息 -->
    <select id="getCompanyApplyTodo" parameterType="cn.com.ty.lift.system.homepage.dao.dto.request.TodoRequest"
            resultType="cn.com.ty.lift.system.homepage.dao.dto.response.CompanyApplyResponse">
        select
        mca.id as applyId,
        mca.mt_company_id as mtCompanyId,
        mca.attestation_name as companyName,
        mca.contacts_name as contractsName,
        mca.contacts_tel as contractsTel,
        mca.remarks as remarks,
        mca.create_time as createTime
        from
        mt_company_attestation mca
        where
        mca.is_certificated = #{isCertificated}
        <include refid="pageQuerySql"/>
    </select>

    <!-- 获取团队成员申请待办信息 -->
    <select id="getCompanyUserApplyTodo" parameterType="cn.com.ty.lift.system.homepage.dao.dto.request.TodoRequest"
            resultType="cn.com.ty.lift.system.homepage.dao.dto.response.CompanyUserApplyResponse">
        select
        ua.id as userApplyId,
        ui.avatar_url as avatarUrl,
        ui.name as userName,
        u.type as userType,
        u.mobile as mobile,
        u.type as type,
        u.create_date as applyDate
        from
        user_application ua
        left join
        user_info ui
        on
        ua.user_id = ui.user_id
        left join
        user_account u
        on
        ua.user_id = u.user_id
        where
        ua.mt_company_id = #{mtCompanyId}
        and
        ua.status = ${status}
        <include refid="pageQuerySql"/>
    </select>

    <!-- 获取急修待办条数 -->
    <select id="getEmergencyTodoCount"
            parameterType="cn.com.ty.lift.system.homepage.dao.dto.request.TodoRequest"
            resultType="java.lang.Long">
        SELECT
            COUNT(1)
        FROM
        emergency_repair er
        LEFT JOIN user_info ui ON ui.user_id = er.creator_id
        LEFT JOIN lift li ON li.id = er.lift_id
        LEFT JOIN project pr ON pr.id = er.project_id
        LEFT JOIN platform_company_lift_relevance pclr ON pclr.lift_id = er.lift_id AND pclr.mt_company_id = er.mt_company_id
        WHERE
            er.`status` IN (0,1)
        AND
            er.mt_company_id = #{mtCompanyId}
    </select>

    <!-- 获取维保待办条数 -->
    <select id="getMaintenanceTodoCount"
            parameterType="cn.com.ty.lift.system.homepage.dao.dto.request.TodoRequest"
            resultType="java.lang.Long">
        SELECT
            COUNT(1)
		FROM
		project_lift_relevance plr
		LEFT JOIN platform_company_lift_relevance pclr ON plr.mt_company_id = pclr.mt_company_id AND plr.lift_id = pclr.lift_id
		LEFT JOIN lift l ON plr.lift_id = l.id
		LEFT JOIN project p ON plr.mt_company_id = p.mt_company_id AND plr.project_id = p.id
		LEFT JOIN maintenance_plan mp ON plr.mt_company_id = mp.mt_company_id AND plr.project_id = mp.project_id
		AND mp.status = 0 AND mp.plan_date = (SELECT MIN(plan_date) FROM maintenance_plan WHERE status = 0
		AND mt_company_id = #{mtCompanyId,jdbcType=VARCHAR} AND lift_id = plr.lift_id) AND plr.lift_id = mp.lift_id
		LEFT JOIN user_info ui ON plr.worker_id = ui.user_id
		LEFT JOIN region r ON p.region_id = r.id
		WHERE plr.mt_company_id = #{mtCompanyId,jdbcType=VARCHAR}
		AND pclr.lift_company_status != '1'
    </select>

    <!-- 获取大修待办条数 -->
    <select id="getCapitalTodoCount"
            parameterType="cn.com.ty.lift.system.homepage.dao.dto.request.TodoRequest"
            resultType="java.lang.Long">
        SELECT COUNT(1)
        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 = #{mtCompanyId,jdbcType=BIGINT}
    </select>

    <!-- 获取年检待办条数 -->
    <select id="getAnnualTodoCount"
            parameterType="cn.com.ty.lift.system.homepage.dao.dto.request.TodoRequest"
            resultType="java.lang.Long">
            SELECT
                COUNT(1)
            FROM
                annual_inspection ai
            LEFT JOIN project_lift_relevance plr ON ai.lift_id = plr.lift_id AND ai.mt_company_id = plr.mt_company_id
            LEFT JOIN lift li ON ai.lift_id = li.id
            LEFT JOIN project pr ON pr.id = plr.project_id
            LEFT JOIN region re ON pr.region_id = re.id
            LEFT JOIN user_info ui ON ui.user_id = re.user_id
            WHERE
                ai.mt_company_id = #{mtCompanyId}
            AND
                ai.status = 0
    </select>

</mapper>