<?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.dup.SiteResourcesMapper">

	<resultMap type="cn.com.countrygarden.bi.main.model.dup.SiteResources" id="SiteResourcesMap">
		<result column="id" property="id"/>
		<result column="resources_id" property="resourcesId"/>
		<result column="organ_name" property="organName"/>
		<result column="comm_name" property="commName"/>
		<result column="comm_city_level" property="commCityLevel"/>
		<result column="area" property="area"/>
		<result column="area_detail" property="areaDetail"/>
		
		<result column="comm_id" property="commId"/>
		<result column="organ_code" property="organCode"/>
		
		<result column="public_resources_type" property="publicResourcesType"/>
		<result column="name" property="name"/>
		<result column="add_date" property="addDate"/>
		<result column="location" property="location"/>
		<result column="image" property="image"/>
		<result column="number" property="number"/>
		<result column="unit" property="unit"/>
		<result column="specification" property="specification"/>
		<result column="forthputting_status" property="forthputtingStatus"/>
		<result column="partner" property="partner"/>
		<result column="price" property="price"/>
		<result column="lease_time_limit" property="leaseTimeLimit"/>
		<result column="idle_resources_directions" property="idleResourcesDirections"/>
		<result column="remark" property="remark"/>
	</resultMap>
																																			
	<select id="findAllSiteResourcesControllerLike" parameterType="cn.com.countrygarden.bi.main.model.dup.SiteResources" resultMap="SiteResourcesMap">
	SELECT
		tsr.id,
		
		tsr.`resources_id`,
		tsr.`organ_name`,
		tsr.`comm_name`,
		tsr.`comm_city_level`,
		tsr.`area`,
		tsr.`area_detail`,
        tsr.comm_id,
        tsr.organ_code,
		tsr.public_resources_type,
		tsr.`name`,
		DATE_FORMAT(tsr.`add_date`,'%Y-%m-%d %H:%i:%s') as add_date,
		tsr.location,
		tsr.image,
		tsr.number,
		tsr.unit,
		tsr.specification,
		tsr.forthputting_status,
		tsr.partner,
		tsr.price,
		tsr.lease_time_limit,
		tsr.idle_resources_directions,
		tsr.remark
	FROM
		tb_site_resources as tsr
	<where>
		<if test="publicResourcesType!=null and publicResourcesType!=''" >
		AND	tsr.public_resources_type=#{publicResourcesType}
		</if>
		<if test="name !=null and name != ''">
		AND tsr.name=#{name}
		</if>
		<if test="forthputtingStatus!=null and forthputtingStatus!=''" >
		and	tsr.forthputting_status =#{forthputtingStatus}
		</if>
		
		<if test="resourcesId !=null and resourcesId!=''" >
		AND	 tsr.`resources_id`   like concat(#{resourcesId},'%')  
		</if>

		<if test="organCode !=null and organCode != ''">
			AND	tsr.`organ_code`=#{organCode}
		</if>
		<if test="commId !=null and commId !=''" >
			and	tsr.`comm_id` =#{commId}
		</if>

		<if test="organCodes!=null and organCodes.size()>0">
			AND tsr.`organ_code` in
			<foreach collection="organCodes" index="index" item="item" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>

		<if test="commIds!=null and commIds.size()>0">
			and	tsr.`comm_id` in
			<foreach collection="commIds" index="index" item="item" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>

	</where>	
	order by tsr.`add_date` desc		
	</select>
  	
  	<insert id="insertResources" parameterType="cn.com.countrygarden.bi.main.model.dup.SiteResources">
  	INSERT INTO tb_site_resources (
		`public_resources_type`,
		`name`,
		`add_date`,
		`location`,
		`image`,
		`number`,
		`unit`,
		`specification`,
		`forthputting_status`,
		`partner`,
		`price`,
		`lease_time_limit`,
		`idle_resources_directions`,
		`remark`,
		`organ_code`,
		`comm_id`,
		`organ_name`,
		`comm_name`,
		`resources_id`,
		`comm_city_level`,
		`area`,
		`area_detail`,
		create_by,
		create_date,
		update_by,
		update_date
		)
	VALUES
		(	
			#{publicResourcesType},
			#{name},
			NOW(),
			#{location},
			#{image},
			#{number},
			#{unit},
			#{specification},
			#{forthputtingStatus},
			#{partner},
			#{price},
			#{leaseTimeLimit},
			#{idleResourcesDirections},
			#{remark},
			#{organCode},
			#{commId},
			#{organName},
			#{commName},
			#{resourcesId},
			#{commCityLevel},
			#{area},
			#{areaDetail},
			#{userCode},
			NOW(),
			null,
			null
		)
  	</insert>
  	
  	    <insert id="addSiteList" parameterType="java.util.List" >
  	INSERT INTO tb_site_resources (
		`public_resources_type`,
		`name`,
		`add_date`,
		`location`,
		`image`,
		`number`,
		`unit`,
		`specification`,
		`forthputting_status`,
		`partner`,
		`price`,
		`lease_time_limit`,
		`idle_resources_directions`,
		`remark`,
		`organ_code`,
		`comm_id`,
		`organ_name`,
		`comm_name`,
		`resources_id`,
		`comm_city_level`,
		`area`,
		`area_detail`,
		create_by,
		create_date,
		update_by,
		update_date
		)
        VALUES
        <foreach collection="list" item="item" index="index" separator="," >
     (	
			#{item.publicResourcesType},
			#{item.name},
			NOW(),
			#{item.location},
			#{item.image},
			#{item.number},
			#{item.unit},
			#{item.specification},
			#{item.forthputtingStatus},
			#{item.partner},
			#{item.price},
			#{item.leaseTimeLimit},
			#{item.idleResourcesDirections},
			#{item.remark},
			#{item.organCode},
			#{item.commId},
			#{item.organName},
			#{item.commName},
			#{item.resourcesId},
			#{item.commCityLevel},
			#{item.area},
			#{item.areaDetail},
			#{item.userCode},
			NOW(),
			null,
			null
		)
        </foreach>
    </insert>
  	
  	<update id="updateResources" parameterType="cn.com.countrygarden.bi.main.model.dup.SiteResources">
  	UPDATE tb_site_resources
		SET
		<if test="publicResourcesType !=null and publicResourcesType!=''"> `public_resources_type` = #{publicResourcesType},</if>
		<if test="name !=null and name!=''"> `name` = #{name},</if>
		<if test="location !=null and  location !=''">`location` = #{location},</if>
		<if test="image !=null and image !=''">`image` = #{image},</if>
		<if test="number !=null and number!=''">`number` = #{number},</if>
		<if test="unit !=null and unit !=''">`unit` = #{unit},</if>
		<if test="specification !=null and specification !=''">`specification` = #{specification},</if>
		<if test="forthputtingStatus !=null and forthputtingStatus !=''">`forthputting_status` = #{forthputtingStatus},</if>
		<if test="partner!=null and partner !=''">`partner` = #{partner},</if>
		<if test="price!=null and price!=0">`price` = #{price},</if>
		<if test="leaseTimeLimit !=null and leaseTimeLimit!=0">`lease_time_limit` = #{leaseTimeLimit},</if>
		<if test="idleResourcesDirections !=null and idleResourcesDirections!=''">`idle_resources_directions` = #{idleResourcesDirections},</if>
		 <if test="remark !=null and remark!=''" >`remark` = #{remark},</if>
		 
		 <if test="organCode !=null and organCode !=''"> `organ_code` = #{organCode},</if>
		<if test="commId !=null and  commId !=''"> `comm_id` = #{commId},</if>
		<if test="organName !=null and organName !=''">`organ_name` = #{organName},</if>
		<if test="commName !=null and commName!=''">`comm_name` = #{commName},</if>
		<if test="commCityLevel !=null and commCityLevel !=''">`comm_city_level` = #{commCityLevel},</if>
		<if test="area !=null and area!=''">`area` = #{area},</if>
		<if test="areaDetail !=null and areaDetail !=''">`area_detail` = #{areaDetail},</if>
		<if test="resourcesId !=null and resourcesId !=''">`resources_id` = #{resourcesId},</if>
		 update_by=#{userCode},
		 update_date=NOW()
		WHERE
			(`id` = #{id});
  	</update>
  	
  	<delete id="deleteResources" parameterType="int">
	  DELETE
		FROM
			tb_site_resources
		WHERE
			tb_site_resources.id=#{id}
  	</delete>
  	
  		<select id="resourcesIsRepeat" parameterType="cn.com.countrygarden.bi.main.model.dup.SiteResources" resultType="String">
	SELECT
		tsr.id
	FROM
		tb_site_resources as tsr
	where
	     tsr.location=#{location}
	    AND tsr.comm_id=#{commId}
      AND  tsr.organ_code=#{organCode}
          AND tsr.name=#{name}
          AND tsr.area=#{area}
		<if test="id !=null and id !=''" >
		AND	tsr.id not in(#{id})
		</if>	
	</select>
	
	<select id="selectMaxResourcesId"  resultType="Integer">
	SELECT
	max(id)+1
	FROM
		tb_site_resources	
	</select>
	
	
		<select id="findByDictCode" resultType="map" parameterType="string">
		SELECT
			i.dict_item_name name,
			i.dict_item_value value
		FROM
			tb_site_resources_dict d,
			tb_site_resources_dict_item i
		WHERE
			d.dict_id = i.dict_id
		AND d.dict_code = #{code}
		ORDER BY i.order_no
	</select>
  	
</mapper>