<?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.project.dao.mapper.ProjectMapper" >
	<resultMap id="BaseResultMap" type="cn.com.ty.lift.business.project.dao.entity.Project" >
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="mt_company_id" property="mtCompanyId" jdbcType="BIGINT" />
		<result column="pp_company_id" property="ppCompanyId" jdbcType="BIGINT" />
		<result column="pp_contact_id" property="ppContactId" jdbcType="BIGINT" />
		<result column="project_code" property="projectCode" jdbcType="CHAR" />
		<result column="project_name" property="projectName" jdbcType="VARCHAR" />
		<result column="province_code" property="provinceCode" jdbcType="CHAR" />
		<result column="province" property="province" jdbcType="VARCHAR" />
		<result column="city_code" property="cityCode" jdbcType="CHAR" />
		<result column="city" property="city" jdbcType="VARCHAR" />
		<result column="district_code" property="districtCode" jdbcType="CHAR" />
		<result column="district" property="district" jdbcType="VARCHAR" />
		<result column="address" property="address" jdbcType="VARCHAR" />

		<!--新增开始-->
		<result column="first_sign_time" property="firstSignTime" jdbcType="DATE" />
		<!--新增结束-->


		<result column="num" property="num" jdbcType="INTEGER" />
		<result column="actual_num" property="actualNum" jdbcType="INTEGER" />
		<result column="project_usage" property="projectUsage" jdbcType="TINYINT" />
		<result column="start_date" property="startDate" jdbcType="DATE" />
		<result column="end_date" property="endDate" jdbcType="DATE" />
		<result column="project_status" property="projectStatus" jdbcType="TINYINT" />
		<result column="remarks" property="remarks" jdbcType="VARCHAR" />
		<result column="is_locked" property="isLocked" jdbcType="TINYINT" />
		<result column="creator_id" property="creatorId" jdbcType="BIGINT" />
		<result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
		<result column="evaluate" property="evaluate" jdbcType="INTEGER" />
		<result column="end_remarks" property="endRemarks" jdbcType="VARCHAR" />
		<result column="if_monitor" property="ifMonitor" jdbcType="INTEGER" />
		<result column="all_project" property="allProject" jdbcType="INTEGER" />
		<result column="region_id" property="regionId" jdbcType="BIGINT" />
	</resultMap>

	<sql id="Base_Column_List" >
		id, mt_company_id, pp_company_id, pp_contact_id, project_code, project_name, province_code,
		province, city_code, city, district_code, district, address, num, actual_num, project_usage,
		start_date, end_date, project_status, remarks, is_locked, creator_id, create_date,
		evaluate, end_remarks, if_monitor, all_project, region_id
		<!--新增开始-->
		,first_sign_time
	</sql>

	<!-- 根据省，市，区，区域，区域主管，项目名称、项目编号、甲方名称、项目地址查询项目管理列表-->
	<select id="findByCondition" resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectResponse"
			parameterType="cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest" >
        SELECT DISTINCT
        p.id            AS id,
        p.project_code  AS projectCode,
        p.project_name  AS projectName,
        p.project_usage AS projectUsage,
        p.province      AS province,
        p.city          AS city,
        p.district      AS district,
		p.district_code AS districtCode,
        p.num           AS num,
        plr.actualNum   AS actualNum,
        p.address       AS address,
        p.start_date    AS startDate,
        p.end_date      AS endDate,
		p.region_id     AS regionId,
		p.pp_company_id AS ppCompanyId,
		p.pp_contact_id AS ppContactId,
		p.save          AS save,
		p.locked        AS locked,
		p.project_status AS projectStatus,
        r.area_name     AS regionName,
        ui.name         AS userName,
        pc.name         AS companyName,
		if(isnull(c.id),'0','1') AS existContract
        FROM project p
        LEFT JOIN region r ON p.region_id = r.id
        LEFT JOIN user_info ui ON r.user_id = ui.user_id
        LEFT JOIN property_company pc ON p.pp_company_id = pc.id
		LEFT JOIN contracts c ON p.id = c.project_id
		LEFT JOIN (SELECT count(1) AS actualNum, project_id
					FROM project_lift_relevance plr
					LEFT JOIN platform_company_lift_relevance pclr
					ON plr.lift_id = pclr.lift_id AND pclr.mt_company_id = plr.mt_company_id
					WHERE plr.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
					AND pclr.lift_company_status != '1'
					AND pclr.delete_flag = '0'
					GROUP BY project_id) plr ON plr.project_id = p.id
        WHERE p.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		<if test="request.provinceCode!=null and request.provinceCode!=''">
			AND p.province_code= #{request.provinceCode,jdbcType=VARCHAR}
		</if>
		<if test="request.cityCode!=null and request.cityCode!=''">
			AND p.city_code = #{request.cityCode,jdbcType=VARCHAR}
		</if>
		<if test="request.districtCode!=null and request.districtCode!=''">
			AND p.district_code = #{request.districtCode,jdbcType=VARCHAR}
		</if>
		<if test="request.regionId!=null and request.regionId!=''">
			AND p.region_id = #{request.regionId,jdbcType=BIGINT}
		</if>
        <if test="request.userId!=null and request.userId!=''">
			AND r.user_id = #{request.userId,jdbcType=BIGINT}
        </if>
		<if test="request.condition!=null and request.condition!=''">
			AND (p.project_name LIKE CONCAT('%',#{request.condition,jdbcType=VARCHAR},'%')
			OR p.project_code LIKE CONCAT('%',#{request.condition,jdbcType=VARCHAR},'%')
			OR p.address LIKE CONCAT('%',#{request.condition,jdbcType=VARCHAR},'%')
		    OR ui.name LIKE CONCAT('%',#{request.condition,jdbcType=VARCHAR},'%'))
		</if>
		<if test="request.projectStatus!=null and request.projectStatus!=''">
			AND p.project_status = #{request.projectStatus,jdbcType=VARCHAR}
		</if>
        ORDER BY p.end_date
	</select>

	<!-- 根据甲方公司ID和项目状态查询公司项目管理列表-->
	<select id="findCompanyListByCondition" resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectResponse"
			parameterType="cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest" >
		SELECT
		       a.project_code AS projectCode,
			   a.project_name AS projectName,
			   a.province     AS province,
			   a.city         AS city,
			   a.district     AS district,
			   a.num          AS num,
			   a.actual_num   AS actualNum,
			   a.address      AS address,
			   a.start_date   AS startDate,
			   a.end_date     AS endDate,
			   b.name         AS contactName,
			   b.telephone    AS contactTelephone
		FROM project a,
		     property_contact b
		WHERE a.pp_contact_id = b.id
		<if test="request.ppCompanyId!=null and request.ppCompanyId!=''">
			AND a.pp_company_id = #{request.ppCompanyId,jdbcType=BIGINT}
		</if>
		<!--查询项目状态为2：服务中的项目-->
		<if test="request.projectStatus!=null and request.projectStatus!='' and request.projectStatus == 2">
			AND a.project_status = #{request.projectStatus,jdbcType=TINYINT}
		</if>
		<!--查询项目状态为3、4：停止服务和逾期的项目-->
		<if test="request.projectStatus!=null and request.projectStatus!='' and request.projectStatus == 4">
			AND (a.project_status = 3 or a.project_status = 4)
		</if>
	</select>

	<select id="queryProjectListByIdList" parameterType="java.util.List"
			resultType="cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenancePlanPrintResponse">
		SELECT p.id            AS projectId,
			   p.project_name  AS projectName,
			   p.project_code  AS projectCode,
		       p.mt_company_id AS mtCompanyId,
			   mc.name         AS mtCompanyName,
			   pc.name         AS ppCompanyName
		FROM project p
				 LEFT JOIN maintenance_company mc ON p.mt_company_id = mc.id
				 LEFT JOIN property_company pc ON p.pp_company_id = pc.id
		WHERE p.id IN
		<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</select>

	<!--app端 根据公司id查询公司下项目列表 -->
	<select id="findListByCompanyId" parameterType="cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest"
			resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectCompanyResponse">
		SELECT DISTINCT
	    p.id            AS projectId,
		p.project_code  AS projectCode,
		p.project_name  AS projectName,
		p.project_usage AS projectUsage,
		ifnull(t.actualNum, 0) actualNum,
		p.project_status AS projectStatus
		FROM project p
		LEFT JOIN (SELECT count(1) actualNum, project_id, mt_company_id
					FROM project_lift_relevance plr
					WHERE plr.delete_flag = '0'
					GROUP BY project_id) t
		ON t.project_id = p.id AND t.mt_company_id = p.mt_company_id
		<if test="request.currentRoleCode == 'REGION_DIRECTOR' or request.currentRoleCode == 'ROLEMTWORK' or request.currentRoleCode == 'CLERK' ">
			LEFT JOIN project_user pu ON p.id = pu.project_id
		</if>
		WHERE (project_status = '1' OR project_status = '2' OR project_status = '4')
		<if test="request.mtCompanyId!=null and request.mtCompanyId!=''">
			AND p.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		</if>
		<if test="request.projectName!=null and request.projectName!=''">
			AND p.project_name LIKE CONCAT('%',#{request.projectName,jdbcType=VARCHAR},'%')
		</if>
		<if test="request.currentRoleCode == 'REGION_DIRECTOR' or request.currentRoleCode == 'ROLEMTWORK' or request.currentRoleCode == 'CLERK' ">
			AND pu.user_id = #{request.userId,jdbcType=BIGINT}
		</if>
	</select>

	<!--app端 根据项目id查询项目下电梯列表 -->
	<select id="findListByProjectId" parameterType="cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest"
			resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectLiftResponse">
		SELECT
		  l.id                AS id,
		  l.registration_code AS registrationCode,
		  l.lift_code         AS liftCode,
		  lb.name             AS liftBrand,
		  l.lift_type         AS liftType,
		  l.use_company_code  AS useCompanyCode,
		  plr.lift_locked     AS liftLocked
		FROM project_lift_relevance plr
		<if test="request.currentRoleCode == 'REGION_DIRECTOR' or request.currentRoleCode == 'CLERK' or request.look == 1 ">
			LEFT JOIN project_user pu ON plr.project_id = pu.project_id
		</if>
		  LEFT JOIN lift l ON plr.lift_id = l.id
		  LEFT JOIN lift_brand lb ON l.lift_brand = lb.code
		  WHERE 1=1
		<if test="request.id!=null and request.id!=''">
			AND plr.project_id = #{request.id,jdbcType=BIGINT}
		</if>
		<if test="request.registrationCode!=null and request.registrationCode!=''">
			AND l.registration_code LIKE CONCAT('%',#{request.registrationCode,jdbcType=VARCHAR},'%')
		</if>
		<choose>
			<when test="request.currentRoleCode == 'REGION_DIRECTOR' or request.currentRoleCode == 'CLERK' or request.look == 1 ">
				AND pu.user_id = #{request.userId,jdbcType=BIGINT}
			</when>
			<when test="request.currentRoleCode == 'ROLEMTWORK' and request.look == 0 ">
				AND plr.worker_id = #{request.userId,jdbcType=BIGINT}
			</when>
			<otherwise/>
		</choose>
	</select>

	<!--app端 根据项目id查询项目详情 -->
	<select id="findProjectById" parameterType="java.lang.Long"
			resultType="cn.com.ty.lift.business.project.dao.entity.model.response.ProjectDetailResponse">
		SELECT t.*,
			   ui.name         AS clerkName,
			   pcc.name        AS companyContact,
			   pcc.telephone   AS telephone
		FROM (SELECT p.project_name  AS projectName,
					 p.project_usage AS projectUsage,
					 p.address       AS address,
					 p.actual_num    AS actualNum,
		             p.num           As num,
					 p.province      AS province,
					 p.city          AS city,
					 p.district      AS district,
					 p.start_date    AS startDate,
					 p.end_date      AS endDate,
					 pc.name         AS companyName,
					 ui.name         AS areaDirector,
					 r.area_name     AS areaName,
					 r.clerk         AS clerk_id,
		             r.id            AS regionId,
		             p.pp_contact_id AS contact_id
		FROM project p
				 LEFT JOIN region r ON p.region_id = r.id
				 LEFT JOIN user_info ui ON r.user_id = ui.user_id
				 LEFT JOIN property_company pc ON p.pp_company_id = pc.id
		WHERE 1=1
		<if test="id!=null and id!=''">
			AND p.id = #{id,jdbcType=BIGINT}
		</if>
		    ) t
		LEFT JOIN property_contact pcc ON t.contact_id = pcc.id
		LEFT JOIN user_info ui ON ui.user_id = t.clerk_id
	</select>

	<select id="findProjectByIdAndCompanyId" parameterType="cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest"
			resultType="cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenancePlanPrintResponse">
		SELECT p.id            AS projectId,
			   p.mt_company_id AS mtCompanyId,
			   p.project_name  AS projectName,
			   p.project_code  AS projectCode,
			   pc.name         AS ppCompanyName,
			   mc.name         AS mtCompanyName
		FROM project p
				 LEFT JOIN property_company pc ON p.pp_company_id = pc.id
				 LEFT JOIN maintenance_company mc ON p.mt_company_id = mc.id
        WHERE p.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
        AND p.id = #{request.id,jdbcType=BIGINT}
	</select>

	<select id="exportList" resultType="java.util.Map" parameterType="java.util.List">
		SELECT 	project_code AS projectCode,
				project_name AS projectName,
				province     AS province,
				city         AS city,
				district     AS district,
				num          AS num,
				actual_num   AS actualNum,
				address      AS address,
				DATE_FORMAT(start_date,'%Y-%m-%d') AS startDate,
				DATE_FORMAT(end_date,'%Y-%m-%d') AS endDate
		FROM project
		<where>
			<if test="exportList != null and exportList.size > 0">
				id IN
				<foreach collection="exportList" item="id" open="(" separator="," close=")">
					#{id}
				</foreach>
			</if>
		</where>
	</select>

	<select id="exportLiftList" resultType="java.util.Map" parameterType="java.util.List">
		SELECT  p.project_name           AS projectName,
				l.registration_code      AS registrationCode,
				if(l.lift_type = 1, '直梯', '扶梯') AS liftType,
				l.device_position        AS devicePosition,
				lb.name                  AS liftBrand,
				l.annual_inspection_date AS annualInspectionDate,
				CASE pclr.lift_company_status
				WHEN '1' THEN '停保'
				WHEN '2' THEN '正常'
				WHEN '3' THEN '保养中'
				WHEN '4' THEN '维修中'
				WHEN '5' THEN '年检中'
				WHEN '6' THEN '大修中'
				END AS liftStatus,
				ui.name                  AS workerName
		FROM project_lift_relevance plr
		LEFT JOIN project p ON plr.project_id = p.id
		LEFT JOIN lift l ON plr.lift_id = l.id
		LEFT JOIN lift_brand lb ON l.lift_brand = lb.code
		LEFT JOIN user_info ui ON plr.worker_id = ui.user_id
		LEFT JOIN platform_company_lift_relevance pclr ON plr.lift_id = pclr.lift_id AND pclr.id = plr.relevance_id
		<where>
			<if test="exportLiftList != null and exportLiftList.size > 0">
				plr.project_id IN
				<foreach collection="exportLiftList" item="id" open="(" separator="," close=")">
					#{id}
				</foreach>
			</if>
		</where>
	</select>

</mapper>
