<?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="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
	</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,
        p.actual_num    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
        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},'%'))
		</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
	    p.id            AS projectId,
		p.project_code  AS projectCode,
		p.project_name  AS projectName,
		p.project_usage AS projectUsage,
		p.actual_num    AS actualNum,
		p.project_status AS projectStatus
		FROM project p
		WHERE 1=1
		<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>
	</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
		FROM project_lift_relevance plr
		  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>
	</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>

</mapper>