<?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="com.bonus.material.warningAnalysis.mapper.UseMaintenanceWarningMapper">

    <select id="getList" resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">
        SELECT
        mm.ma_id as maId,
        mm.next_check_time as nextCheckTime,
        mt3.type_name as materialType,
        mt2.type_name as typeName,
        mt.type_name as typeModelName,
        mm.ma_code as maCode,
        bu.unit_name as unitName,
        bp.pro_name as projectName,
        bai.agreement_code as agreementCode,
        DATEDIFF(mm.next_check_time, CURDATE()) AS overDays,
        sd.dept_name as impUnit
        FROM
        ma_machine mm
        LEFT JOIN ma_type mt on mt.type_id=mm.type_id
        LEFT JOIN ma_type mt2 on mt2.type_id=mt.parent_id
        LEFT JOIN ma_type mt3 on mt3.type_id=mt2.parent_id
        LEFT JOIN ma_type mt4 on mt4.type_id=mt3.parent_id
        LEFT JOIN slt_agreement_info sai on sai.ma_id=mm.ma_id and mm.type_id=sai.type_id and sai.`status`='0' and
        sai.end_time is null
        LEFT JOIN bm_agreement_info bai on bai.agreement_id=sai.agreement_id
        LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
        AND bu.del_flag = '0'
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        AND bp.del_flag = '0'
        LEFT JOIN sys_dept sd on sd.dept_id = bp.imp_unit
        WHERE
        mm.ma_status='2' and sai.is_slt = 0
        and bp.pro_name is not null
        AND mm.next_check_time BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
        <if test="keyWord != null  and keyWord != ''">
            and (
            mt4.type_name like concat('%', #{keyWord}, '%') or
            mt3.type_name like concat('%', #{keyWord}, '%') or
            mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            mm.ma_code like concat('%', #{keyWord}, '%') or
            bu.unit_name like concat('%', #{keyWord}, '%') or
            bp.pro_name like concat('%', #{keyWord}, '%') or
            bai.agreement_code like concat('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( mm.next_check_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="typeId != null">
            AND mt.type_id = #{typeId}
        </if>
        <if test="thirdTypeId != null">
            AND mt2.type_id = #{thirdTypeId}
        </if>
        ORDER BY mm.next_check_time ASC
    </select>



    <select id="getWarningLastMonthList"
            resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">



        SELECT
            proId as projectId,
            projectName,
            GROUP_CONCAT(DISTINCT typeName) as typeName ,
            COUNT(DISTINCT maId) as warnNum
        FROM
            (

                SELECT
                    bp.external_id as proId,
                    mm.ma_id as maId,
                    mm.next_check_time as nextCheckTime,
                    mt3.type_name as materialType,
                    mt2.type_name as typeName,
                    mt.type_name as typeModelName,
                    mm.ma_code as maCode,
                    bu.unit_name as unitName,
                    bp.pro_name as projectName,
                    DATEDIFF(mm.next_check_time, CURDATE()) AS overDays
                FROM
                    ma_machine mm
                        LEFT JOIN ma_type mt on mt.type_id=mm.type_id
                        LEFT JOIN ma_type mt2 on mt2.type_id=mt.parent_id
                        LEFT JOIN ma_type mt3 on mt3.type_id=mt2.parent_id
                        LEFT JOIN ma_type mt4 on mt4.type_id=mt3.parent_id
                        LEFT JOIN slt_agreement_info sai on sai.ma_id=mm.ma_id and mm.type_id=sai.type_id
                        LEFT JOIN bm_agreement_info bai on bai.agreement_id=sai.agreement_id
                        LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
                        AND bu.del_flag = '0'
                        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                        AND bp.del_flag = '0'
                        LEFT JOIN sys_dept sd on sd.dept_id = bp.imp_unit
                WHERE
                    sai.`status` =  0 and sai.is_slt = 0 and
                    mm.ma_status='2'
                  and bp.external_id is not null
                  AND mt.jiju_type =2
                  AND mm.next_check_time BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
            ) res
        GROUP BY proId

    </select>

    <select id="getProjectManager" resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">

        SELECT
            dc.id as projectId,
            du1.mobile as leaderPhone,
            du2.mobile as engPhone
        FROM
            `data_center`.dx_fb_son dc
                LEFT JOIN `data_center`.dc_user du1 ON dc.project_leader_id = du1.id
                LEFT JOIN `data_center`.dc_user du2 ON dc.project_engineer_id = du2.id
        WHERE dc.id = #{projectId}
    </select>

    <select id="getUserList" resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">
        SELECT
            sgp.cno as userName
        FROM
            bm_project bp
                LEFT JOIN `data_center`.dx_fb_son dfs on bp.external_id = dfs.id
                LEFT JOIN `sbd_audit`.sg_project_post_personnel sgp on sgp.depart_id = dfs.project_dept_id

        WHERE sgp.post_id = '3de0eb390f3611efa1940242ac130004'

          AND bp.pro_id = #{projectId}
    </select>

    <select id="getUserPhoneById" resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">
        select
            user_id as userId,
            phonenumber AS clzPhone
        from
            sys_user
        where user_name = #{userName}
    </select>

    <select id="getWarningLastDayList" resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">

        SELECT
            proId as projectId,
            deptId,
            projectName,
            GROUP_CONCAT(DISTINCT typeName) as typeName ,
            COUNT(DISTINCT maId) as warnNum
        FROM
            (

                SELECT
                    bp.external_id as proId,
                    bp.imp_unit as deptId,
                    mm.ma_id as maId,
                    mm.next_check_time as nextCheckTime,
                    mt3.type_name as materialType,
                    mt2.type_name as typeName,
                    mt.type_name as typeModelName,
                    mm.ma_code as maCode,
                    bu.unit_name as unitName,
                    bp.pro_name as projectName,
                    DATEDIFF(mm.next_check_time, CURDATE()) AS overDays
                FROM
                    ma_machine mm
                        LEFT JOIN ma_type mt on mt.type_id=mm.type_id
                        LEFT JOIN ma_type mt2 on mt2.type_id=mt.parent_id
                        LEFT JOIN ma_type mt3 on mt3.type_id=mt2.parent_id
                        LEFT JOIN ma_type mt4 on mt4.type_id=mt3.parent_id
                        LEFT JOIN slt_agreement_info sai on sai.ma_id=mm.ma_id and mm.type_id=sai.type_id
                        LEFT JOIN bm_agreement_info bai on bai.agreement_id=sai.agreement_id
                        LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
                        AND bu.del_flag = '0'
                        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                        AND bp.del_flag = '0'
                        LEFT JOIN sys_dept sd on sd.dept_id = bp.imp_unit
                WHERE
                    sai.`status` =  0 and sai.is_slt = 0 and
                    mm.ma_status='2'
                  and bp.external_id is not null
                  AND mt.jiju_type =2
                  AND mm.next_check_time BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 15 day)
            ) res
        GROUP BY proId
    </select>

    <select id="getUserManger" resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">
        SELECT
            su.user_id as userId,
            su.user_name as userName
        FROM
            sys_user su
                LEFT JOIN sys_user_role sr on su.user_id = sr.user_id
        WHERE sr.role_id in (59,81) and dept_id =#{deptId}
    </select>

    <select id="getOverTimeListList" resultType="com.bonus.material.warningAnalysis.domain.UseMaintenanceWarningBean">
        SELECT
        mm.ma_id as maId,
        mm.this_check_time as thisCheckTime,
        mm.next_check_time as nextCheckTime,
        mt3.type_name as materialType,
        mt2.type_name as typeName,
        mt.type_name as typeModelName,
        mm.ma_code as maCode,
        bu.unit_name as unitName,
        bp.pro_name as projectName,
        bai.agreement_code as agreementCode,
        DATEDIFF(mm.next_check_time, CURDATE()) AS overDays,
        sd.dept_name as impUnit
        FROM
        ma_machine mm
        LEFT JOIN ma_type mt on mt.type_id=mm.type_id
        LEFT JOIN ma_type mt2 on mt2.type_id=mt.parent_id
        LEFT JOIN ma_type mt3 on mt3.type_id=mt2.parent_id
        LEFT JOIN ma_type mt4 on mt4.type_id=mt3.parent_id
        LEFT JOIN slt_agreement_info sai on sai.ma_id=mm.ma_id and mm.type_id=sai.type_id and sai.`status`='0' and
        sai.end_time is null
        LEFT JOIN bm_agreement_info bai on bai.agreement_id=sai.agreement_id
        LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
        AND bu.del_flag = '0'
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        AND bp.del_flag = '0'
        LEFT JOIN sys_dept sd on sd.dept_id = bp.imp_unit
        WHERE
        mm.ma_status='2' and sai.is_slt = 0
        AND bp.pro_name is not null
        AND mm.this_check_time is not null
        AND mm.next_check_time is not null
        AND mt.jiju_type = 2
        <if test="status != null and status == 0">
            AND mm.next_check_time &gt;= CURDATE()
        </if>
        <if test="status != null and status == 1">
            AND mm.next_check_time &lt; CURDATE()
        </if>
        <if test="keyWord != null  and keyWord != ''">
            and (
            mt4.type_name like concat('%', #{keyWord}, '%') or
            mt3.type_name like concat('%', #{keyWord}, '%') or
            mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            mm.ma_code like concat('%', #{keyWord}, '%') or
            bu.unit_name like concat('%', #{keyWord}, '%') or
            bp.pro_name like concat('%', #{keyWord}, '%') or
            bai.agreement_code like concat('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( mm.next_check_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="typeId != null">
            AND mt.type_id = #{typeId}
        </if>
        <if test="thirdTypeId != null">
            AND mt2.type_id = #{thirdTypeId}
        </if>
        ORDER BY mm.next_check_time ASC
    </select>
</mapper>
