<?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.yanzhu.jh.project.mapper.ProjectViewMapper">

    <!--查询项目总包单位-->
    <select id="selectStatisticsProjectList" parameterType="com.yanzhu.jh.project.domain.SurProject"  resultType="com.yanzhu.jh.project.domain.vo.ProjectViewExport">
        select sp.id as projectId,sp.projectName, pui.unitId,pui.unitName,
        (select count(1) from smz_ssp_problemmodify a where a.projectId = sp.id and a.roleType=5 and a.infoType=0 and a.isDel=0
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as aqyhpc_jfdb,
        (select count(1) from smz_ssp_problemmodify a where a.projectId = sp.id and a.roleType=6 and a.infoType=0 and a.isDel=0
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as aqyhpc_jldw,
        (select count(1) from smz_ssp_problemmodify a LEFT JOIN sys_user u on u.user_name=a.createUser where a.isDel=0 and a.projectId = sp.id and a.roleType=7 and u.dept_id = pui.unitId and a.infoType=0
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as aqyhpc_zbdw,
        (select count(1) from work_train a LEFT JOIN work_train_dept b on a.id=b.train_id and b.is_main='Y' where a.project_id=sp.id and b.dept_id=pui.unitId and a.train_type=1 and a.is_del=0 ) as yjyl,
        (select count(1) from work_train a LEFT JOIN work_train_dept b on a.id=b.train_id and b.is_main='Y' where a.project_id=sp.id and b.dept_id=pui.unitId and a.train_type=0 and a.is_del=0 ) as zxpx,
        (select count(1) from smz_ssp_problemmodify a where a.projectId = sp.id and a.roleType=5 and a.infoType=1 and a.isDel=0
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as zlyhpc_jfdb,
        (select count(1) from smz_ssp_problemmodify a where a.projectId = sp.id and a.roleType=6 and a.infoType=1 and a.isDel=0
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as zlyhpc_jldw,
        (select count(1) from smz_ssp_problemmodify a LEFT JOIN sys_user u on u.user_name=a.createUser where a.isDel=0 and a.projectId = sp.id and a.roleType=7 and u.dept_id = pui.unitId and a.infoType=1
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as zlyhpc_zbdw,
        (select count(1) from sur_project_work_special a where a.project_id = sp.id and a.dept_id=pui.unitId and a.is_del=0)as tzry,
        (select count(1) from sur_project_check_detection a where a.project_id = sp.id and a.dept_id=pui.unitId and a.is_del=0)as clqyfs,
        (select count(1) from sur_project_checking a where a.project_id = sp.id and a.dept_id=pui.unitId and a.is_del=0)as jpys,
        (select count(1) from sur_project_measure a where a.project_id = sp.id and a.dept_id=pui.unitId and a.is_del=0)as cscl,
        (select count(1) from sur_project_material_seal a where a.project_id = sp.id and a.dept_id=pui.unitId and a.is_del=0)as clfy,
        (select count(1) from sur_project_insurance a where a.project_id = sp.id and a.dept_id=pui.unitId and a.is_del=0 and a.insurance_type=1)as yqx,
        (select count(1) from sur_project_insurance a where a.project_id = sp.id and a.dept_id=pui.unitId and a.is_del=0 and a.insurance_type=2)as azx,
        (select count(1) from sur_project_video_config a where a.project_id = sp.id and a.dept_id = pui.unitId and a.is_del=0)as ssjk,
        (select count(1) from dev_ai_project_config a where a.project_id = sp.id and a.dept_id = pui.unitId and a.is_del=0)as aijk,
        (select count(1) from dev_ai_project_data a where a.project_id = sp.id and a.dept_id = pui.unitId and a.is_del=0)as aiyjs,
        (select count(1) from sur_project_attendance_cfg a where a.project_id = sp.id and a.sub_dept_id = pui.unitId and a.state=0)as lwsm,
        (select count(1) from sur_project_attendance_user a left join sur_project_attendance_cfg b on a.cfgid = b.id where b.project_id = sp.id and b.sub_dept_id = pui.unitId and b.state=0)as lwry,
        (select count(1) from vw_flow_all a where a.businessKey = sp.id and a.startDeptName = pui.unitName and a.finishTime is null)as gcsq,
        (select count(1) from vw_flow_all a where a.businessKey = sp.id and a.startDeptName = pui.unitName and a.finishTime is not null)as gcsp,
        (select count(1) from sur_project_standard a where a.project_id = sp.id and a.dept_id = pui.unitId and a.is_del=0)as bzh,
        (select count(1) from sur_project_photography a where a.project_id = sp.id and a.is_del=0)as yssys,
        '0' as hjjc,
        '0' as hjyjs,
        (select count(1) from smz_ssp_problemmodify a where a.projectId=sp.id and a.infoType=1 and a.isDel=0 and a.checkState!=4 and date(NOW())<![CDATA[ > ]]> date(a.nickedTime)) as zlyjs,
        (select count(1) from smz_ssp_problemmodify a where a.projectId=sp.id and a.infoType=0 and a.isDel=0 and a.checkState!=4 and date(NOW())<![CDATA[ > ]]> date(a.nickedTime)) as aqyjs,
        (select CASE when b.plan_end_date &gt; now() then 1 else 2 end as jdyj from vw_sur_project_build_node_data_current b where b.id in (select max(id) from vw_sur_project_build_node_data_current a where LENGTH(a.node_lvl)=2
        and a.project_id=sp.id))as jdyj
        from sur_project sp
        left JOIN sur_project_unit_info pui on sp.id=pui.projectId and pui.unitType=2
        where (pui.del_flag=0 or pui.del_flag is null)
        <if test="projectName != null  and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
        <if test="deptId != null "> and sp.deptId = #{deptId}</if>
        <if test="projectType != null  and projectType != ''"> and sp.projectType = #{projectType}</if>
        <if test="projectNature != null  and projectNature != ''"> and sp.projectNature = #{projectNature}</if>
        ORDER by sp.projectSort
    </select>

    <!--查询项目总包单位-->
    <select id="selectStatisticsJlProjectList" parameterType="com.yanzhu.jh.project.domain.SurProject"  resultType="com.yanzhu.jh.project.domain.vo.ProjectViewJlExport">
        select sp.id as projectId,sp.projectName, pui.unitId,pui.unitName,
        (select count(1) from smz_ssp_problemmodify a where a.projectId = sp.id and a.roleType=6 and a.infoType=0 and a.isDel=0
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as aqyhpc_jldw,
        (select count(1) from work_train a LEFT JOIN work_train_dept b on a.id=b.train_id and b.is_main='Y' where a.project_id=sp.id and b.dept_id=pui.unitId and a.train_type=1 and a.is_del=0 ) as yjyl,
        (select count(1) from work_train a LEFT JOIN work_train_dept b on a.id=b.train_id and b.is_main='Y' where a.project_id=sp.id and b.dept_id=pui.unitId and a.train_type=0 and a.is_del=0 ) as zxpx,
        (select count(1) from smz_ssp_problemmodify a where a.projectId = sp.id and a.roleType=6 and a.infoType=1 and a.isDel=0
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and date(a.createTime) between #{params.beginTime} and #{params.endTime}</if>
        )as zlyhpc_jldw,
        (select count(1) from sur_project_check_detection a where a.project_id = sp.id and a.is_del=0 and approve_status=1)as clqyfs,
        (select count(1) from sur_project_check_detection a where a.project_id = sp.id and a.is_del=0 and approve_status=4)as clqyfswc,
        (select count(1) from sur_project_checking a where a.project_id = sp.id and a.is_del=0 and approve_status=1)as jpys,
        (select count(1) from sur_project_checking a where a.project_id = sp.id and a.is_del=0 and approve_status=4)as jpyswc,
        (select count(1) from sur_project_measure a where a.project_id = sp.id and a.is_del=0 and approve_status=1)as cscl,
        (select count(1) from sur_project_measure a where a.project_id = sp.id and a.is_del=0 and approve_status=4)as csclwc,
        (select count(1) from sur_project_material_seal a where a.project_id = sp.id and a.is_del=0 and approve_status=1)as clfy,
        (select count(1) from sur_project_material_seal a where a.project_id = sp.id and a.is_del=0 and approve_status=4)as clfywc,

        (select count(DISTINCT a.procInsId) from vw_flow_await a where a.businessKey = sp.id and a.taskName like '%监理%')as gcsq,
        (select count(DISTINCT a.procInsId) from vw_flow_finished a where a.businessKey = sp.id and a.taskName like '%监理%')as gcsp
        from sur_project sp
        left JOIN sur_project_unit_info pui on sp.id=pui.projectId and pui.unitType=4
        where (pui.del_flag=0 or pui.del_flag is null)
        <if test="projectName != null  and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
        <if test="deptId != null "> and sp.deptId = #{deptId}</if>
        <if test="projectType != null  and projectType != ''"> and sp.projectType = #{projectType}</if>
        <if test="projectNature != null  and projectNature != ''"> and sp.projectNature = #{projectNature}</if>
        ORDER by sp.projectSort
    </select>

</mapper>