<?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.push.mapper.PushUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.common.model.PushUserInfo">
        <id column="user_id" property="userId"/>
        <result column="username" property="userName"/>
        <result column="mobile" property="mobile"/>
        <result column="device_model" property="deviceModel"/>
        <result column="device_flag" property="deviceFlag"/>
        <result column="role_name" property="roleName"/>
    </resultMap>

    <select id="listByUserId" resultMap="BaseResultMap">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="userId != null and userId > 0">
                AND ui.user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="listByUserIds" resultMap="BaseResultMap">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="userIds != null and userIds.size > 0">
                AND ui.user_id IN
                <foreach collection="userIds" item="userId" open="(" separator="," close=")">
                    #{userId, jdbcType=BIGINT}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listByRoleCodes" resultMap="BaseResultMap">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="roleCodes != null">
                AND ro.code IN
                <foreach collection="roleCodes" item="roleCode" open="(" separator="," close=")">
                    #{roleCode, jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listByRoleCode" resultMap="BaseResultMap">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="roleCode != null and roleCode != ''">
                AND ro.code = #{roleCode, jdbcType=VARCHAR}
            </if>
        </where>
    </select>

    <select id="findRegionByEmergencyRepair" resultType="cn.com.ty.lift.business.project.dao.entity.Region">
        SELECT
            re.user_id AS userId,
            re.clerk
        FROM emergency_repair er
            LEFT JOIN project pr ON pr.id = er.project_id
            LEFT JOIN region re ON re.id = pr.region_id
        <where>
            <if test="id != null and id > 0">
                AND er.id = #{id}
            </if>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND er.mt_company_id = #{mtCompanyId}
            </if>
        </where>
    </select>

    <select id="findRegionByAnnualInspection" resultType="cn.com.ty.lift.business.project.dao.entity.Region">
        SELECT
            re.user_id AS userId,
            re.clerk
        FROM annual_inspection ai
            LEFT JOIN project_lift_relevance plr ON plr.lift_id = ai.lift_id AND plr.mt_company_id = ai.mt_company_id
            LEFT JOIN project pr ON pr.id = plr.project_id
            LEFT JOIN region re ON re.id = pr.region_id
        <where>
            <if test="id != null and id > 0">
                AND ai.id = #{id}
            </if>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ai.mt_company_id = #{mtCompanyId}
            </if>
        </where>
    </select>

    <select id="findRegionByProject" resultType="cn.com.ty.lift.business.project.dao.entity.Region">
        SELECT
            re.user_id AS userId,
            re.clerk
        FROM project pr
            LEFT JOIN region re ON re.id = pr.region_id
        <where>
            <if test="id != null and id > 0">
                AND pr.id = #{id}
            </if>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND pr.mt_company_id = #{mtCompanyId}
            </if>
        </where>
    </select>

    <select id="pageMtCompanyByAnnualInspection" resultType="cn.com.ty.lift.business.push.entity.InspectionTips">
        SELECT
            mc.id AS mt_company_id,
            plr.worker_id,
            pr.project_name,
            li.device_position,
            li.registration_code,
            re.user_id AS director_id,
            re.clerk
        FROM
            maintenance_company mc
            LEFT JOIN annual_inspection ai ON ai.mt_company_id = mc.id
            LEFT JOIN project_lift_relevance plr ON plr.lift_id = ai.lift_id AND ai.mt_company_id = plr.mt_company_id
            LEFT JOIN lift li ON li.id = ai.lift_id
            LEFT JOIN project pr ON pr.id = plr.project_id
            LEFT JOIN region re ON pr.region_id = re.id
        WHERE
            ai.id > 0
            AND plr.project_id > 0
            AND re.id > 0
            AND li.id > 0
            AND li.annual_inspection_date &lt;= date_add(CURDATE(), INTERVAL #{days} DAY)
            AND li.annual_inspection_date >= CURDATE()
    </select>

    <select id="listMtCompanyByEmergencyRepairForRegion" resultType="cn.com.ty.lift.business.push.entity.RepairTips">
        SELECT
            t.mt_company_id,
            t.area_name,
            t.director_id,
            t.clerk,
            SUM(t.total) AS total,
            SUM( CASE t.STATUS WHEN '1' THEN t.total ELSE 0 END ) AS repairing,
            SUM( CASE t.STATUS WHEN '2' THEN t.total ELSE 0 END ) AS finish
        FROM
            (
                SELECT
                    mc.id AS mt_company_id,
                    er.`status`,
                    re.area_name,
                    re.user_id AS director_id,
                    re.clerk,
                    count( * ) AS total
                FROM
                    maintenance_company mc
                    LEFT JOIN emergency_repair er ON er.mt_company_id = mc.id
                    LEFT JOIN project_lift_relevance plr ON plr.project_id = er.project_id
                                                            AND plr.mt_company_id = er.mt_company_id
                    LEFT JOIN project pr ON pr.id = er.project_id
                    LEFT JOIN region re ON pr.region_id = re.id
                WHERE
                    er.id > 0
                    AND re.id > 0
                    AND TO_DAYS( er.create_date ) = TO_DAYS( NOW( ) )
                GROUP BY
                    mc.id,
                    re.id,
                    er.`status`
                HAVING
                    count( * ) > 0
            ) t
        GROUP BY
            t.mt_company_id,t.director_id
    </select>

    <select id="listMtCompanyByEmergencyRepairForDirector" resultType="cn.com.ty.lift.business.push.entity.RepairTips">
        SELECT
            t.mt_company_id,
            t.area_name,
            t.director_id,
            t.clerk,
            SUM(t.total) AS total,
            SUM( CASE t.STATUS WHEN '1' THEN t.total ELSE 0 END ) AS repairing,
            SUM( CASE t.STATUS WHEN '2' THEN t.total ELSE 0 END ) AS finish
        FROM
            (
                SELECT
                    mc.id AS mt_company_id,
                    er.`status`,
                    re.area_name,
                    re.user_id AS director_id,
                    re.clerk,
                    count( * ) AS total
                FROM
                    maintenance_company mc
                    LEFT JOIN emergency_repair er ON er.mt_company_id = mc.id
                    LEFT JOIN project_lift_relevance plr ON plr.project_id = er.project_id AND plr.mt_company_id = er.mt_company_id
                    LEFT JOIN project pr ON pr.id = er.project_id
                    LEFT JOIN region re ON pr.region_id = re.id
                WHERE
                    er.id > 0
                    AND re.id > 0
                    AND TO_DAYS( er.create_date ) = TO_DAYS( NOW( ) )
                GROUP BY
                    mc.id,
                    re.id,
                    er.`status`
                HAVING
                    count( * ) > 0
            ) t
        GROUP BY
            t.mt_company_id
    </select>

    <select id="pageRepairAndMaintainForClerk" resultType="cn.com.ty.lift.business.push.entity.TaskTips">
        SELECT
            mp.mt_company_id,
            pu.user_id,
            count(*) AS maintain_total,
            (SELECT count(*) FROM emergency_repair WHERE STATUS &lt; 2 AND mt_company_id = mp.mt_company_id ) AS repair_total
        FROM
            maintenance_plan mp
        LEFT JOIN project_user pu ON mp.mt_company_id = pu.mt_company_id
            AND mp.project_id = pu.project_id
            AND pu.user_role = 'CLERK'
        WHERE
          mp.STATUS = 0
          AND mp.plan_date = curdate()
        GROUP BY
            mp.mt_company_id,
            pu.user_id
    </select>

    <select id="listMaintainForEnterpriseAdmin" resultType="cn.com.ty.lift.business.push.entity.TaskTips">
        SELECT
            p.mt_company_id,
            count(*) AS maintain_total
        FROM
            maintenance_plan mp
            LEFT JOIN project p ON mp.project_id = p.id
        WHERE
            mp.plan_date = curdate()
            AND mp.`status` = 0
        GROUP BY
            p.mt_company_id
    </select>
</mapper>
