<?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.countrygarden.bi.main.dao.common.CommMapper">
  <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.common.Comm">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="organ_id" jdbcType="VARCHAR" property="organId" />
    <result column="is_delete" jdbcType="TINYINT" property="isDelete" />
    <result column="project_type" jdbcType="VARCHAR" property="projectType" />
    <result column="corp_type" jdbcType="VARCHAR" property="corpType" />
    <result column="project_nature" jdbcType="VARCHAR" property="projectNature" />
    <result column="develop_subject" jdbcType="VARCHAR" property="developSubject" />
    <result column="area" jdbcType="DECIMAL" property="area" />
    <result column="contract_area" jdbcType="DECIMAL" property="contractArea" />
    <result column="charge_area" jdbcType="DECIMAL" property="chargeArea" />
    <result column="nc_account" jdbcType="VARCHAR" property="ncAccount" />
    <result column="nc_number" jdbcType="VARCHAR" property="ncNumber" />
    <result column="contacts" jdbcType="VARCHAR" property="contacts" />
    <result column="proposal_organ_id" jdbcType="VARCHAR" property="proposalOrganId" />
    <result column="comm_code" jdbcType="INTEGER" property="commCode" />
    <result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified" javaType="java.util.Date"/>
    <result column="modified_by" jdbcType="VARCHAR" property="modifiedBy" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="review_status" jdbcType="VARCHAR" property="reviewStatus" />
    <result column="sign_date" jdbcType="TIMESTAMP" property="signDate" javaType="java.util.Date"/>
    <result column="contract_cancel_date" jdbcType="TIMESTAMP" property="contractCancelDate" />
    <result column="contractor_code" jdbcType="VARCHAR" property="contractorCode" />
    <result column="contractor_name" jdbcType="VARCHAR" property="contractorName" />
    <result column="comm_num" jdbcType="VARCHAR" property="commNum" />
    <result column="proposal_date" jdbcType="DATE" property="proposalDate" javaType="java.util.Date"/>
    <result column="comment" jdbcType="LONGVARCHAR" property="comment" />
    <result column="prophase" jdbcType="LONGVARCHAR" property="prophase" />
    <result column="additional_charge" jdbcType="TINYINT" property="additionalCharge" />
    <result column="schedule_name" jdbcType="VARCHAR" property="scheduleName" />
    
  </resultMap>

  <resultMap id="SearchResultMap" type="cn.com.countrygarden.bi.main.model.common.CommData">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="projectName" />
    <result column="comm_num" jdbcType="VARCHAR" property="serialNumber"/>
    <result column="organ_id" jdbcType="VARCHAR" property="takeOverOrganId" />
    <result column="proposal_organ_id" jdbcType="VARCHAR" property="organId" />
    <result column="project_type" jdbcType="VARCHAR" property="projectTypeCode" />
    <result column="project_type_name" jdbcType="VARCHAR" property="projectType" />
    <result column="corp_type" jdbcType="VARCHAR" property="cooperationTypeCode" />
    <result column="contacts" jdbcType="VARCHAR" property="contact" />
    
    <result column="corp_type_name" jdbcType="VARCHAR" property="cooperationType" />
    <result column="project_nature" jdbcType="VARCHAR" property="projectNatureCode" />
    <result column="project_nature_name" jdbcType="VARCHAR" property="projectNature" />
    <result column="area" jdbcType="DECIMAL" property="planningArea" />
    <result column="contract_area" jdbcType="DECIMAL" property="contractArea" />
    <result column="charge_area" jdbcType="DECIMAL" property="chargeArea" />
    <result column="contractor_name" jdbcType="VARCHAR" property="contact" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="comment" jdbcType="LONGVARCHAR" property="introduction" />
    <result column="prophase" jdbcType="LONGVARCHAR" property="prophase" />
     <result column="general_type" jdbcType="DECIMAL" property="generalType" />
       <result column="cooperationf_name" jdbcType="VARCHAR" property="cooperationfName" />
        <result column="saturation" jdbcType="VARCHAR" property="saturation" />
         <result column="cooperationf_contact" jdbcType="VARCHAR" property="cooperationfContact" />
           <result column="cooperationf_number" jdbcType="VARCHAR" property="cooperationfNumber" />
            <result column="proportion" jdbcType="VARCHAR" property="proportion" />
              <result column="project_number" jdbcType="DECIMAL" property="projectNumber" />
              <result column="target_area" jdbcType="DECIMAL" property="targetArea" />
                 <result column="target_income" jdbcType="DECIMAL" property="targetIncome" />
                   <result column="schedule" jdbcType="VARCHAR" property="schedule" />
                     <result column="region" jdbcType="DECIMAL" property="region" />
                      <result column="city" jdbcType="VARCHAR" property="city" />
                      <result column="remark" jdbcType="VARCHAR" property="remark" />
                       <result column="schedule_name" jdbcType="VARCHAR" property="scheduleName" />
                        <result column="project_plan_code" jdbcType="VARCHAR" property="projectPlanCode" />
                        
                        
                       
  </resultMap>
  
    <resultMap id="SearchOneResultMap" type="cn.com.countrygarden.bi.main.service.project.impl.vo.OneProject">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="organName" jdbcType="VARCHAR" property="organName" />
    <result column="modified_by" jdbcType="VARCHAR" property="modifiedBy" />
    
    <result column="regionName" jdbcType="VARCHAR" property="regionName"/>
    <result column="contractorName" jdbcType="VARCHAR" property="contractorName" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="cooperationfName" jdbcType="VARCHAR" property="cooperationfName" />
    <result column="statusName" jdbcType="VARCHAR" property="statusName" />
    <result column="contractArea" jdbcType="VARCHAR" property="contractArea" />
    <result column="saturation" jdbcType="VARCHAR" property="saturation" />
    <result column="city" jdbcType="VARCHAR" property="city" />
    <result column="joint" jdbcType="VARCHAR" property="joint" />
    <result column="jointDuty" jdbcType="VARCHAR" property="jointDuty" />
    <result column="jointRightName" jdbcType="VARCHAR" property="jointRightName" />
    <result column="corpTypeName" jdbcType="VARCHAR" property="corpTypeName" />
    <result column="significanceName" jdbcType="VARCHAR" property="significanceName" />
    <result column="demandName" jdbcType="VARCHAR" property="demandName" />
    <result column="propertyFee" jdbcType="VARCHAR" property="propertyFee" />
    <result column="messageFee" jdbcType="VARCHAR" property="messageFee" />
     <result column="isSubmit" jdbcType="VARCHAR" property="isSubmit" />
       <result column="competitor" jdbcType="VARCHAR" property="competitor" />
        <result column="opinion" jdbcType="VARCHAR" property="opinion" />
         <result column="callFile" jdbcType="VARCHAR" property="callFile" />
           <result column="callOrganization" jdbcType="VARCHAR" property="callOrganization" />
            <result column="callEnvironmen" jdbcType="VARCHAR" property="callEnvironmen" />
              <result column="businessName" jdbcType="VARCHAR" property="businessName" />
              <result column="projectClass" jdbcType="VARCHAR" property="projectClass" />
                 <result column="projectPlan" jdbcType="VARCHAR" property="projectPlan" />
                   <result column="projectPlanCode" jdbcType="VARCHAR" property="projectPlanCode" />
                     <result column="remark" jdbcType="VARCHAR" property="remark" />
                       <result column="warningLight" jdbcType="VARCHAR" property="warningLight" />
                  <result column="isManage" jdbcType="INTEGER" property="isManage" /> 
                   <result column="ruserCode" jdbcType="VARCHAR" property="ruserCode" /> 
                        <result column="commCode" jdbcType="VARCHAR" property="commCode" /> 
                         <result column="projectClassCode" jdbcType="VARCHAR" property="projectClassCode" />
                           <result column="contractorCode" jdbcType="VARCHAR" property="contractorCode" />
                            <result column="updateTime" jdbcType="TIMESTAMP" property="updateTime" />
                             <result column="erp_organ_name" jdbcType="VARCHAR" property="erpOrganName" />
                          	
                        
  </resultMap>
  <select id = "selectByOneCount" resultType="int">
  	SELECT
  		count(c.id)
		
	FROM
		comm c
	LEFT JOIN organ o ON o.organ_code = c.organ_id
	LEFT JOIN organ_region ore ON ore.organ_code = c.organ_id
	LEFT JOIN region r ON r.region_code = ore.region_code
	LEFT JOIN project_joint_plan bus ON c.business_type = bus.id
	LEFT JOIN project_joint_plan sta ON c.`status` =  sta.id
	LEFT JOIN project_joint_plan jr ON c.joint_right = jr.id
	LEFT JOIN project_joint_plan cy ON c.corp_type_one = cy.id
	LEFT JOIN project_joint_plan s ON s.id = c.significance
	LEFT JOIN project_joint_plan de ON de.id = c.demand
	LEFT JOIN project_joint_plan pf ON pf.id = c.property_fee
	LEFT JOIN project_joint_plan mf ON mf.id = c.message_fee
	LEFT JOIN project_joint_plan sub ON sub.id = c.is_submit
	LEFT JOIN project_joint_plan comp ON comp.id = c.competitor
	LEFT JOIN project_joint_plan opi ON opi.id = c.opinion
	LEFT JOIN project_joint_plan cf ON cf.id = c.call_file
	LEFT JOIN project_joint_plan co ON co.id = c.call_organization
	LEFT JOIN project_joint_plan ce ON ce.id = c.call_environmen
	LEFT JOIN project_class pclass ON pclass.`code` = c.project_class_code
	LEFT JOIN project_plan_one pplan ON pplan.`code` = c.project_plan_code

	<where>
		c.general_type = 1
	  <if test="name !=null and name != ''">
       	 AND (c.name like concat(concat('%',#{name}),'%') OR c.comm_code LIKE concat(concat('%',#{name}),'%') )
      </if>
       <if test="organId !=null and organId != ''">
        AND c.organ_id = #{organId}
      </if>
       <if test="warningLight !=null">
       	 AND c.warning_light = #{warningLight}
      </if>
       <if test="projectClassCode !=null and projectClassCode != ''">
       	 AND c.project_class_code = #{projectClassCode}
      </if>
      <if test="contractorCode !=null and contractorCode != ''">
       		AND (c.contractor_code = #{contractorCode} or r.user_code = #{contractorCode})
      </if>
      
	</where>
  </select>
  <select id = "selectByOne" resultMap="SearchOneResultMap">
  	SELECT
  		c.id,
		o.`name` organName, 
		r.region_name regionName, 
		r.user_code ruserCode,
		c.contractor_name contractorName,
		c.contractor_code contractorCode,
		c.`name`, 
		c.cooperationf_name cooperationfName,
		sta.`name` statusName,
		c.contract_area contractArea,
		c.saturation saturation,
		c.comm_code commCode,
		c.city, 
		c.joint,
		c.joint_duty jointDuty, 
		jr.`name` jointRightName, 
		cy.`name` corpTypeName, 
		s.`name` significanceName,
		de.`name` demandName, 
		pf.`name` propertyFee, 
		mf.`name` messageFee,
		sub.`name` isSubmit,
		comp.`name` competitor, 
		opi.`name` opinion,
		cf.`name` callFile,
		co.`name` callOrganization,
		ce.`name` callEnvironmen,
		bus.`name` businessName,
		pclass.`name` projectClass,
		pclass.`code` projectClassCode, 
		pplan.`name` projectPlan, 
		pplan.`code` projectPlanCode,
		c.remark,
		c.warning_light warningLight,
		c.this_stage updateTime,
		c.erp_organ_name,
		c.modified_by
	FROM
		comm c
	LEFT JOIN organ o ON o.organ_code = c.organ_id
	LEFT JOIN organ_region ore ON ore.organ_code = c.organ_id
	LEFT JOIN region r ON r.region_code = ore.region_code
	LEFT JOIN project_joint_plan bus ON c.business_type = bus.id
	LEFT JOIN project_joint_plan sta ON c.`status` =  sta.id
	LEFT JOIN project_joint_plan jr ON c.joint_right = jr.id
	LEFT JOIN project_joint_plan cy ON c.corp_type_one = cy.id
	LEFT JOIN project_joint_plan s ON s.id = c.significance
	LEFT JOIN project_joint_plan de ON de.id = c.demand
	LEFT JOIN project_joint_plan pf ON pf.id = c.property_fee
	LEFT JOIN project_joint_plan mf ON mf.id = c.message_fee
	LEFT JOIN project_joint_plan sub ON sub.id = c.is_submit
	LEFT JOIN project_joint_plan comp ON comp.id = c.competitor
	LEFT JOIN project_joint_plan opi ON opi.id = c.opinion
	LEFT JOIN project_joint_plan cf ON cf.id = c.call_file
	LEFT JOIN project_joint_plan co ON co.id = c.call_organization
	LEFT JOIN project_joint_plan ce ON ce.id = c.call_environmen
	LEFT JOIN project_class pclass ON pclass.`code` = c.project_class_code
	LEFT JOIN project_plan_one pplan ON pplan.`code` = c.project_plan_code

	<where>
		c.general_type = 1
	  <if test="name !=null and name != ''">
       	 AND (c.name like concat(concat('%',#{name}),'%') OR c.comm_code LIKE concat(concat('%',#{name}),'%') )
      </if>
       <if test="organId !=null and organId != ''">
        AND c.organ_id = #{organId}
      </if>
       <if test="warningLight !=null">
       	 AND c.warning_light = #{warningLight}
      </if>
       <if test="projectClassCode !=null and projectClassCode != ''">
       	 AND c.project_class_code = #{projectClassCode}
       	 and c.project_plan_code <![CDATA[<>]]> 'over'
       	 and c.project_plan_code <![CDATA[<>]]> 'checkReStart' 
		 and c.project_plan_code <![CDATA[<>]]> 'checkStop'
      </if>
      <if test="projectPlanCode !=null and projectPlanCode == 1">
      	and c.project_plan_code = 'over'
      </if>
       <if test="projectPlanCode !=null and projectPlanCode == 2">
      	and (c.project_plan_code = 'checkReStart' or c.project_plan_code = 'checkStop' )
      </if>
      <if test="erpOrganCode != null and erpOrganCode != '' ">
      	AND c.erp_organ_code=#{erpOrganCode}
      </if>
      <if test="contractorCode !=null and contractorCode != ''">
       		AND (c.contractor_code = #{contractorCode} or r.user_code = #{contractorCode}
       			<if test="organCode!=null">
       				or c.organ_id = #{organCode}
       			</if>
       		)
      </if>
      
	</where>
	order by c.warning_light DESC,c.id DESC
  </select>
 <select id = "selectByOneId" resultMap="SearchOneResultMap">
  	SELECT
  		c.id,
		o.`name` organName, 
		r.region_name regionName, 
		r.user_code ruserCode,
		c.contractor_name contractorName,
		c.contractor_code contractorCode,
		c.`name`, 
		c.cooperationf_name cooperationfName,
		sta.`name` statusName,
		c.contract_area contractArea,
		c.saturation saturation,
		c.comm_code commCode,
		c.city, 
		c.joint,
		c.joint_duty jointDuty, 
		jr.`name` jointRightName, 
		cy.`name` corpTypeName, 
		s.`name` significanceName,
		de.`name` demandName, 
		pf.`name` propertyFee, 
		mf.`name` messageFee,
		sub.`name` isSubmit,
		comp.`name` competitor, 
		opi.`name` opinion,
		cf.`name` callFile,
		co.`name` callOrganization,
		ce.`name` call_environmen,
		bus.`name` businessName,
		pclass.`name` projectClass,
		pclass.`code` projectClassCode, 
		pplan.`name` projectPlan, 
		pplan.`code` projectPlanCode,
		c.remark,
		c.warning_light warningLight
		
	FROM
		comm c
	LEFT JOIN organ o ON o.organ_code = c.organ_id
	LEFT JOIN organ_region ore ON ore.organ_code = c.organ_id
	LEFT JOIN region r ON r.region_code = ore.region_code
	LEFT JOIN project_joint_plan bus ON c.business_type = bus.id
	LEFT JOIN project_joint_plan sta ON c.`status` =  sta.id
	LEFT JOIN project_joint_plan jr ON c.joint_right = jr.id
	LEFT JOIN project_joint_plan cy ON c.corp_type_one = cy.id
	LEFT JOIN project_joint_plan s ON s.id = c.significance
	LEFT JOIN project_joint_plan de ON de.id = c.demand
	LEFT JOIN project_joint_plan pf ON pf.id = c.property_fee
	LEFT JOIN project_joint_plan mf ON mf.id = c.message_fee
	LEFT JOIN project_joint_plan sub ON sub.id = c.is_submit
	LEFT JOIN project_joint_plan comp ON comp.id = c.competitor
	LEFT JOIN project_joint_plan opi ON opi.id = c.opinion
	LEFT JOIN project_joint_plan cf ON cf.id = c.call_file
	LEFT JOIN project_joint_plan co ON co.id = c.call_organization
	LEFT JOIN project_joint_plan ce ON ce.id = c.call_environmen
	LEFT JOIN project_class pclass ON pclass.`code` = c.project_class_code
	LEFT JOIN project_plan_one pplan ON pplan.`code` = c.project_plan_code

	WHERE c.id = #{id}
  </select>
  <sql id="selectList">
    <![CDATA[
      SELECT c.id,
        c.organ_id,
        c.name,
        c.comm_num,
        c.proposal_organ_id,
        c.project_type,
        p.name AS  project_type_name,
        c.corp_type,
        CASE WHEN c.general_type = 1 THEN co.name
			ELSE cor.name END corp_type_name,
        c.project_nature,
        n.name as project_nature_name,
        c.area,
        c.contract_area,
        c.charge_area,
        c.contractor_name,
        c.status,
        c.prophase,
        c.comment,
        c.general_type,
        c.cooperationf_name,
        c.saturation,
        c.contacts,
        c.cooperationf_contact,
        c.cooperationf_number,
        c.proportion,
        c.project_number,
        c.target_area,
        c.target_income,
        c.schedule,
        c.region,
        c.city,
        c.remark,
        cor.name corp_type_name,
        co.name schedule_name
      FROM comm AS c
      LEFT JOIN project_joint_plan AS p ON p.id = c.project_type AND p.is_delete = 0 AND p.menu_id = 3
      LEFT JOIN project_joint_plan AS co ON co.id = c.schedule AND co.is_delete = 0 
      LEFT JOIN project_joint_plan AS n ON n.id = c.project_nature AND n.is_delete=0 AND n.menu_id = 2
       LEFT JOIN project_joint_plan AS cor ON cor.id = c.corp_type_one AND cor.is_delete = 0 AND cor.menu_id = 5
    ]]>
  </sql>

  <select id="searchByListCondition" resultMap="SearchResultMap">
    <include refid="selectList" />
    <![CDATA[
      WHERE c.is_delete =0  
    ]]>
	AND  c.general_type = #{generalType}

      <if test="contractorCode != null and contractorCode !='' ">
        AND c.contractor_code = #{contractorCode}
      </if>
   
      <if test="searchKey !=null and searchKey != ''">
        AND (c.name like concat(concat('%',#{searchKey}),'%') OR c.comm_num LIKE concat(concat('%',#{searchKey}),'%') )
      </if>
      
      <if test="organCode !=null and organCode != ''">
        AND c.proposal_organ_id = #{organCode}
      </if>
      <if test="takeOverOrganCode !=null and takeOverOrganCode != ''">
        AND c.organ_id = #{takeOverOrganCode}
      </if>
      <if test="proposalBefore !=null and proposalAfter!=null ">
        <![CDATA[
          AND  c.proposal_date >= #{proposalBefore} AND proposal_date <= #{proposalAfter}
        ]]>
      </if>
      <if test="contractBefore !=null and contractAfter!=null ">
        <![CDATA[
          AND c.sign_date >= #{contractBefore} AND sign_date <= #{contractAfter}
        ]]>
      </if>
      <if test="status !=null and status != ''">
        AND c.status = #{status}
      </if>
      <if test="isCharge !=null ">
        <![CDATA[
          AND c.charge_area >0
        ]]>
      </if>
    <![CDATA[
        ORDER BY c.id DESC
    ]]>
  </select>

  <select id="searchByCommNums" resultMap="BaseResultMap">
    <![CDATA[
      SELECT * FROM comm WHERE is_delete = 0
    ]]>
    <if test="commNums!=null">
      AND comm_num IN
      <foreach item="item" index="index" collection="commNums" open="("     separator="," close=")">
        #{item}
      </foreach>
    </if>
  </select>

  <select id="sumCurrentContactArea" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT IFNULL(SUM(contract_area),0)
      FROM comm
      WHERE status = 'contract' AND is_delete = 0
    ]]>
  </select>
  <select id="sumContractRegional" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT COUNT(DISTINCT proposal_organ_id)
      FROM comm
      WHERE status = 'contract' AND is_delete = 0
    ]]>
  </select>
  <select id="sumContractProject" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT COUNT(*)
      FROM comm
      WHERE status = 'contract' AND is_delete = 0
    ]]>
  </select>
  <select id="sumCurrentChargeArea" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT IFNULL(SUM(charge_area),0)
      FROM comm
      WHERE is_delete = 0
    ]]>
  </select>
  <select id="sumChargeRegional" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT COUNT(DISTINCT organ_id)
      FROM comm
      WHERE is_delete = 0 and charge_area>0
    ]]>
  </select>
  <select id="sumChargeProject" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT COUNT(*)
      FROM comm
      WHERE is_delete = 0 and charge_area>0
    ]]>
  </select>

  <select id="sumRegularFormatCharge" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT IFNULL(SUM(charge_area),0)
      FROM comm
      WHERE is_delete = 0 and additional_charge=0
    ]]>
  </select>
  <select id="sumSpecialFormatCharge" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT IFNULL(SUM(charge_area),0)
      FROM comm
      WHERE is_delete = 0 and additional_charge=1
    ]]>
  </select>
  <select id="sumContractAreaByTime" resultType="java.math.BigDecimal">
    <![CDATA[
      SELECT IFNULL(SUM(contract_area),0)
      FROM comm
      WHERE is_delete = 0 AND sign_date>=#{startDate} AND sign_date<=#{endDate}
    ]]>
  </select>

  <select id="sumContractProjectByTime" resultType="java.lang.Integer">
    <![CDATA[
      SELECT COUNT(*)
      FROM comm
      WHERE is_delete = 0 AND sign_date>=#{startDate} AND sign_date<=#{endDate}
    ]]>
  </select>
  
  <!--市场拓展2期   huangmin  -->
  <select id="getProject" resultType="cn.com.countrygarden.bi.main.model.project.CommVO">
	  SELECT *
	 FROM (SELECT 
	  id,
	  commCode,
	  NAME,
	  contractorName,
	  isLev,
	  projectClassCode,
	  projectPlanCode,
	  jointRight,
	  propertyFee,
	  messageFee,
	  callFile,
	  thisStage,
	  erpOrganName,
	  opinion,
	  organ_id,
	  isUpdate,
	  organName,
	  erp_organ_code,
	  contractorCode,
	  ruserCode,
	  upperProjectPlanCode,
	  projectClassA,
	  projectClassB,
	  projectClassC,
	  projectClassD,
	  projectClassY,
	  A,B,C,D,Y,
	  CASE WHEN A = 1 OR A = 2  THEN thisStage  ELSE a_date END a_date,
	  CASE WHEN B = 1 THEN thisStage WHEN B=2 THEN IFNULL(a_date,thisStage) ELSE NULL END b_date,
	  CASE WHEN C = 1 THEN thisStage WHEN C=2 THEN IFNULL(b_date,thisStage) ELSE NULL END c_date,
	  CASE WHEN D = 1 THEN thisStage WHEN D=2 THEN IFNULL(c_date,thisStage) ELSE NULL  END d_date,
	  CASE WHEN Y = 1 THEN thisStage WHEN Y=2 THEN projectClassY END y_date
	FROM
	  (SELECT cm.id,comm_code as commCode,
	  cm.name,
	  contractor_name as contractorName,
	  (CASE WHEN saturation >= 10000000 THEN '千万级' ELSE '' END) AS isLev,
	  project_class_code as projectClassCode,
	  CASE WHEN  project_class_code = 'classByA' THEN 1 WHEN project_class_code = 'classByY' THEN 2 ELSE 3  END A,
	  CASE WHEN  project_class_code = 'classByB' THEN 1 WHEN project_class_code = 'classByY' OR project_class_code = 'classByA' THEN 2 ELSE 3   END B,
	  CASE WHEN  project_class_code = 'classByC' THEN 1 WHEN project_class_code  = 'classByY' OR project_class_code = 'classByB' OR project_class_code = 'classByA' THEN 2 ELSE 3   END C,
	  CASE WHEN  project_class_code = 'classByD' THEN 1 WHEN  project_class_code  = 'classByY' OR project_class_code = 'classByB' OR project_class_code = 'classByA' OR project_class_code = 'classByC' THEN 2 ELSE 3   END D,
	  CASE WHEN  project_class_code = 'classByY' THEN 1 ELSE 2 END Y,
	  cr.a_date,
	  cr.b_date,
	  cr.c_date,
	  cr.d_date,
	  project_plan_code as projectPlanCode,
	  cr.class_a as projectClassA,
	  cr.class_b as projectClassB,
	  cr.class_c as projectClassC,
	  cr.class_d as projectClassD,
	  CASE WHEN project_class_code='classByY' THEN 'green' 
	   WHEN project_class_code='classByA' THEN DATE_FORMAT(DATE_ADD(cm.this_stage, INTERVAL IFNULL(IFNULL(a.red,pa.red),6) DAY), '%Y-%m-%d') 
	   WHEN project_class_code='classByB' THEN DATE_FORMAT(DATE_ADD(cm.this_stage, INTERVAL IFNULL(IFNULL(b.red,pb.red),6) + IFNULL(IFNULL(a.red,pa.red),6) DAY), '%Y-%m-%d') 
	   WHEN project_class_code='classByC' THEN DATE_FORMAT(DATE_ADD(cm.this_stage, INTERVAL IFNULL(IFNULL(c.red,pc.red),6) + IFNULL(IFNULL(a.red,pa.red),6)  + IFNULL(IFNULL(b.red,pb.red),6) DAY) , '%Y-%m-%d') 
	   WHEN project_class_code='classByD' THEN DATE_FORMAT(DATE_ADD(cm.this_stage, INTERVAL IFNULL(IFNULL(d.red,pd.red),6) + IFNULL(IFNULL(a.red,pa.red),6)  + IFNULL(IFNULL(b.red,pb.red),6)  + IFNULL(IFNULL(c.red,pc.red),6)  DAY), '%Y-%m-%d')    
	    END  AS projectClassY,
	  cm.joint_right as jointRight,
	  cm.property_fee as propertyFee,
	  cm.message_fee as messageFee,
	  cm.call_file as callFile,
	  cm.this_stage as thisStage,
	  cm.erp_organ_name as erpOrganName,
	  opinion,
	  case when a.red is not null and a.yellow is not null and a.black is not null then 'true'
	  when b.red is not null and b.yellow is not null and b.black is not null then 'true'
	  when c.red is not null and c.yellow is not null and c.black is not null then 'true'
	  when d.red is not null and d.yellow is not null and d.black is not null then 'true' 
	  end as isUpdate,
	  cm.organ_id,
	  org.name AS organName,
	  erp_organ_code,
	  contractor_code contractorCode,
	  r.user_code ruserCode,
	  cm.upper_project_plan_code upperProjectPlanCode
	FROM comm cm
	LEFT JOIN comm_record cr ON cm.id=cr.comm_id
	LEFT JOIN organ_region ore ON ore.organ_code = cm.organ_id
	LEFT JOIN region r ON r.region_code = ore.region_code
	LEFT JOIN (SELECT yellow,red,black,comm_id,TYPE FROM comm_info a WHERE TYPE='classByA') a ON cm.id=a.comm_id
 	LEFT JOIN (SELECT yellow,red,black,comm_id,TYPE FROM comm_info a WHERE TYPE='classByB') b ON cm.id=b.comm_id
 	LEFT JOIN (SELECT yellow,red,black,comm_id,TYPE FROM comm_info a WHERE TYPE='classByC') c ON cm.id=c.comm_id
 	LEFT JOIN (SELECT yellow,red,black,comm_id,TYPE FROM comm_info a WHERE TYPE='classByD') d ON cm.id=d.comm_id
 	LEFT JOIN organ org ON cm.organ_id=org.organ_code,
	(SELECT yellow,red,CODE FROM project_class WHERE CODE='classByA') pa,
	(SELECT yellow,red,CODE FROM project_class WHERE CODE='classByC') pc,
	(SELECT yellow,red,CODE FROM project_class WHERE CODE='classByB') pb,
	(SELECT yellow,red,CODE FROM project_class WHERE CODE='classByD') pd
	where  cm.general_type='1' and cm.is_delete=0
	) t where 1=1
	<if test="erpOrganCode != null and  erpOrganCode != ''">
		and erp_organ_code=#{erpOrganCode}
	</if>
	<if test="organId != null and organId != '' ">
		and organ_id = #{organId} 
	</if>
  <!-- 	<if test="name != null and name != ''">
  		and name like '%${name}%'
  	</if> -->
  	<if test="commCode != null and commCode != ''">
  		and commCode like concat('%',#{commCode},'%') or name like concat('%',#{commCode},'%')
  	</if>
  	  <if test="contractorCode !=null and contractorCode != ''">
       		AND (contractorCode = #{contractorCode}  or ruserCode = #{contractorCode}
       		<if test="areaCode != null">
       			or organ_id=#{areaCode}
       		</if>  )
      </if> 
  	) t 
  	where 1=1 
  	 <if test="colour != null and colour != '' and node == 4 ">
  	    and projectClassA=#{colour}
  	</if>
  	 <if test="colour != null and colour != ''  and node == 3 ">
  	   and projectClassB=#{colour}
  	</if>
  	 <if test="colour != null and colour != ''  and node == 2 ">
  	   and projectClassC=#{colour}
  	</if>
  	 <if test="colour != null and colour != ''  and node == 1 ">
  	   and projectClassD=#{colour}
  	</if>
      <if test="sort != null">
      	 ORDER  BY id DESC
      </if>
     <if test="ysort !=null and ysort == 1">
      	ORDER  BY  y_date IS  NULL,Y ASC,y_date DESC
     </if>
      <if test="ysort !=null and ysort == 2">
      	ORDER  BY y_date IS  NULL, Y ASC,y_date ASC
     </if>
     <if test="asort !=null and asort == 1">
      	ORDER BY A ASC,a_date DESC
     </if>
      <if test="asort !=null and asort == 2">
      	ORDER  BY A ASC,a_date  ASC
     </if>
     
     <if test="bsort !=null and bsort == 1">
      	ORDER  BY B ASC,b_date DESC
     </if>
      <if test="bsort !=null and bsort == 2">
      	ORDER  BY B ASC,b_date ASC
     </if>
     
     <if test="csort !=null and csort == 1">
      	ORDER  BY C ASC,c_date DESC
     </if>
      <if test="csort !=null and csort == 2">
      	ORDER  BY C ASC,c_date ASC
     </if>
     
     <if test="dsort !=null and dsort == 1">
      	ORDER  BY D ASC,d_date DESC
     </if>
      <if test="dsort !=null and dsort == 2">
      	ORDER  BY D ASC,d_date ASC
     </if>
  	   <if test="limit > 0 and offset >= 0">
            <![CDATA[
            LIMIT #{offset, jdbcType = INTEGER}, #{limit, jdbcType = INTEGER}
           ]]>
    </if>
  </select>
	<select id="getJointPlan" resultType="java.util.HashMap" >
		SELECT id,is_suspicious FROM  project_joint_plan 
		WHERE menu_id IN('10','13','14','17','18') and is_suspicious is not null
	</select>
	
	<select id="getlist" resultType="String">
		SELECT NAME FROM comm WHERE NAME LIKE concat('%',#{commCode},'%')
	</select>
	<select id="findOrganRegion" resultType="String">
		SELECT CONCAT('0',region_code) FROM organ_region WHERE organ_code=#{organId}  LIMIT 1
	</select>
	<select id="findMaxCode" resultType="int">
		SELECT CAST(MAX( RIGHT (comm_code,4)) AS SIGNED INTEGER) FROM comm
	</select>
	<update id="updateErpOrgan"   >
		update comm set erp_organ_code=#{code},erp_organ_name=#{name} where id=#{id}
	</update>
	<update id="updateCommUser">
		update comm set 
		<trim  suffixOverrides="," >
		<if test="organCode != null">erp_organ_code=#{organCode},</if>
		<if test="organName != null">erp_organ_name=#{organName},</if> 
		<if test="userName != null">contractor_code=#{userCode},</if> 
		<if test="userCode != null">contractor_name=#{userName},</if> 
		</trim>
		where id=#{id}
	</update>
	<select id="getUserCode" resultType="String" >
		SELECT user_code FROM user_roles WHERE role_code LIKE '%marketing%' GROUP BY user_code
	</select>
	<select id="getCommByCount" resultType="Map" >
		SELECT t.code,COUNT(t.code) as count FROM (
		SELECT CASE WHEN project_plan_code='over' THEN project_plan_code ELSE project_class_code END AS CODE  
		FROM comm cm
		LEFT JOIN organ_region ore ON ore.organ_code = cm.organ_id
		LEFT JOIN region r ON r.region_code = ore.region_code
		where
		1=1 and cm.general_type = 1 and cm.is_delete=0
		 <if test="contractorCode !=null and contractorCode != ''">
       		and (contractor_code = #{contractorCode}  or user_code = #{contractorCode}
       		<if test="areaCode != null">
       			or organ_id=#{areaCode}
       		</if>  )
      </if> 
		   ) t
		GROUP BY t.code
	</select>
	
	<select id="getRegion" resultType="String" >
		SELECT user_code FROM region
	</select>
</mapper>