<?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.ruoyi.web.project.mapper.WxProjectInfoMapper">

    <!-- 查询组织架构下一层级 -->
    <select id="getDeptSubordinate" parameterType="map" resultType="map">
        SELECT
            ca.cityId AS id,
            ca.cityName AS text
        FROM
            sur_project spi
            LEFT JOIN china_area ca ON spi.projectRegional = ca.id
            left join sur_project_userinfo spu on spu.project_id = spi.id
        WHERE
            spi.isDel = 0
            and spu.is_del=0
            <if test="userId != null and userId != ''"> AND spu.user_id =#{userId} </if>
            <if test="provinceId != null and provinceId != ''">AND ca.provinceId =#{provinceId}</if>
        GROUP BY ca.cityId,ca.cityName
    </select>

    <!-- 查询区级层级 -->
    <select id="getQUDeptSubordinate" parameterType="map" resultType="map">
        SELECT
            #{id} AS id,
            '全部' AS text
        UNION ALL
        SELECT
            ca.id,
            ca.areaName AS text
        FROM
            sur_project spi
            LEFT JOIN china_area ca ON spi.projectRegional = ca.id
            left join sur_project_userinfo spu on spu.project_id = spi.id
        WHERE
            spi.isDel = 0
            and spu.is_del=0
            AND spu.user_id =#{userId}
            AND ca.cityId =#{id}
        GROUP BY ca.id,ca.areaName
    </select>

    <!--根据所选层级或者公司查询登录用户的项目-->
    <select id="getProjectByDeptIdAndCompanyId" parameterType="map" resultType="map">
        SELECT
            spi.id AS projectId,
            spi.projectName AS projectName,
            spi.longitude,
            spi.latitude,
            spi.projectAddress,
            IFNULL( spi.projectCode, '' ) AS streamNumber,
            sd.dept_id AS deptId,
            sd.dept_name AS companyName,
            0 AS projectCount
        FROM
            sur_project spi
            LEFT JOIN china_area ca ON spi.projectRegional = ca.id
            LEFT JOIN sys_dept sd ON spi.deptId = sd.dept_id
            <!--监理单位/总包公司/分包单位-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> left join sur_project_unit_info spui on spui.projectId = spi.id</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> left join sur_project_userinfo spu on spu.project_id = spi.id</if>
        WHERE
         spi.isDel = 0
        <!--检索条件-->
        <if test="projectName != null and projectName != ''"> and spi.projectName like  concat('%', #{projectName}, '%')</if>
        <!--子部门数据-->
        <if test='nowRole == "4"'> and spi.deptId = #{nowDeptId}</if>
        <!--监理单位/总包公司/分包单位查询当前关联数据-->
        <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and spui.unitId = #{nowDeptId}</if>
        <!--普通用户查询项目人员-->
        <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and spu.user_id = #{userId} and spu.is_del=0</if>
        <!--子公司选择-->
        <if test='nowRole == "3"'>
            <if test='deptId != null and deptId != "0"'>AND spi.deptId =#{deptId}</if>
        </if>
        GROUP BY spi.id
        order by spi.projectSort
    </select>

    <!-- 查询项目的概况信息 -->
    <select id="selectProjectOverview" parameterType="String" resultType="map">
        SELECT
            spi.id,
            IFNULL( spi.projectName, '' ) AS projectName,
            IFNULL( spi.simpleName, '' ) AS simpleName,
            CONCAT(
            IFNULL(sdd1.dict_label, ''),
            ' ',
            IFNULL(sdd2.dict_label, ''),
            ' ',
            IFNULL(sdd3.dict_label, '')
            ) AS projectType,
            IFNULL( CONCAT( ca.provinceName, '-', ca.cityName, '-', ca.areaName ), '' ) AS areaName,
            IFNULL( spi.projectAddress, '' ) AS projectAddress,
            IFNULL( spi.longitude,'') AS longitude,
	        IFNULL( spi.latitude,'') AS latitude,
            IFNULL( spi.floorArea, '' ) AS totalBuildingArea,
            IFNULL( spi.licenceNumber, '' ) AS licenceNumber,
            IFNULL( spi.projectApproval, '' ) AS projectApproval,
            IFNULL( sdd4.dict_label, '' ) AS projectSchedule,
            CONCAT(IFNULL(spi.projectTimeLimit, ''),' 天') AS projectTimeLimit,
            IFNULL( spi.projectSummarize, '' ) AS projectSummarize,
            '' AS scopeOfWork,
            IFNULL( spi.scheduledStartTime, '' ) AS planStartTime,
            IFNULL( spi.actualOperatingTime, '' ) AS actualStartTime,
            IFNULL( spi.plannedCompletionTime, '' ) AS planCompletedTime,
            '' AS totalManHours,
            IFNULL( spi.projectPerson, '' ) AS projectPerson,
            IFNULL( spi.projectPhone, '' ) AS projectPhone
        FROM
            sur_project spi
            LEFT JOIN china_area ca ON spi.projectRegional = ca.id
            left join sys_dict_data sdd1 on sdd1.dict_type = 'project_category' and sdd1.dict_value = spi.projectType
            left join sys_dict_data sdd2 on sdd2.dict_type = 'project_level' and sdd2.dict_value = spi.projiectLevel
            left join sys_dict_data sdd3 on sdd3.dict_type = 'project_build_type' and sdd3.dict_value = spi.projectNature
            left join sys_dict_data sdd4 on sdd4.dict_type = 'sur_project_xmjd' and sdd4.dict_value = spi.projectSchedule
        WHERE
            spi.id =#{id}
            AND spi.isDel = 0
    </select>

    <!--查询公司信息-->
    <select id="getZGSDeptInfoByMap" parameterType="map" resultType="map">
        SELECT
            sd.dept_id,
            sd.dept_name
        FROM
            sys_dept sd
        WHERE
            sd.del_flag = 0
            AND sd.com_flag = '1'
    </select>

    <!--获取建设单位-->
    <select id="getProjectUnitList" parameterType="String" resultType="map">
        SELECT
            IFNULL( spi.projectName, '' ) AS projectName,
            IFNULL( spui.unitType, '' ) AS unitType,
            IFNULL( sdd.dict_label,'') AS unitTypeName,
            IFNULL( spuii.unit_icon,'') AS unitTypeIcon,
            IFNULL( spui.unitName, '' ) AS unitName,
            IFNULL( spui.unitPersonName, '' ) AS unitPersonName,
            IFNULL( spui.unitPersonPhone, '' ) AS unitPersonPhone
        FROM
            `sur_project_unit_info` spui
            LEFT JOIN sur_project_unit_icon spuii ON spui.unitType = spuii.id
            left join sys_dict_data sdd on sdd.dict_type = 'sys_dept_type' and sdd.dict_value = spui.unitType
            LEFT JOIN sur_project spi ON spui.projectId = spi.id
        WHERE
            spui.projectId =#{projectId}
            AND spui.del_flag = 0
        ORDER BY sdd.dict_sort asc
    </select>

    <select id="selectDeptByProjectId" parameterType="Long" resultType="map">
        select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.com_flag, d.type_flag
        from sys_dept d
        left join sur_project sp on sp.deptId = d.dept_id
        where sp.id = #{projectId}
    </select>

    <select id="selectDeptByUserName" parameterType="String" resultType="map">
        select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.com_flag, d.type_flag
        from sys_dept d
        left join sys_user u on u.dept_id = d.dept_id
        where u.user_name = #{userName}
    </select>

</mapper>