<?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.goafanti.common.dao.OrgIntellectualPropertyMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.OrgIntellectualProperty" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="uid" property="uid" jdbcType="VARCHAR" />
    <result column="pid" property="pid" jdbcType="VARCHAR" />
    <result column="intellectual_property_number" property="intellectualPropertyNumber" jdbcType="VARCHAR" />
    <result column="intellectual_property_name" property="intellectualPropertyName" jdbcType="VARCHAR" />
    <result column="sort_number" property="sortNumber" jdbcType="VARCHAR" />
    <result column="catagory" property="catagory" jdbcType="INTEGER" />
    <result column="obtain_way" property="obtainWay" jdbcType="INTEGER" />
    <result column="authorization_number" property="authorizationNumber" jdbcType="VARCHAR" />
    <result column="authorization_date" property="authorizationDate" jdbcType="TIMESTAMP" />
    <result column="evaluation_category" property="evaluationCategory" jdbcType="INTEGER" />
    <result column="property_right_url" property="propertyRightUrl" jdbcType="VARCHAR" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="deleted_sign" property="deletedSign" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, uid, pid, intellectual_property_number, intellectual_property_name, sort_number, catagory, 
    obtain_way, authorization_number, authorization_date, evaluation_category, property_right_url, 
    type, deleted_sign
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from org_intellectual_property
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from org_intellectual_property
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.OrgIntellectualProperty" >
    insert into org_intellectual_property (id, uid, pid, intellectual_property_number, 
      intellectual_property_name, sort_number, catagory, 
      obtain_way, authorization_number, authorization_date, 
      evaluation_category, property_right_url, type, deleted_sign)
    values (#{id,jdbcType=VARCHAR}, #{uid,jdbcType=VARCHAR}, #{pid,jdbcType=VARCHAR}, #{intellectualPropertyNumber,jdbcType=VARCHAR}, 
      #{intellectualPropertyName,jdbcType=VARCHAR}, #{sortNumber,jdbcType=VARCHAR}, #{catagory,jdbcType=INTEGER}, 
      #{obtainWay,jdbcType=INTEGER}, #{authorizationNumber,jdbcType=VARCHAR}, #{authorizationDate,jdbcType=TIMESTAMP}, 
      #{evaluationCategory,jdbcType=INTEGER}, #{propertyRightUrl,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, 
      #{deletedSign,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.OrgIntellectualProperty" >
    insert into org_intellectual_property
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="uid != null" >
        uid,
      </if>
      <if test="pid != null" >
        pid,
      </if>
      <if test="intellectualPropertyNumber != null" >
        intellectual_property_number,
      </if>
      <if test="intellectualPropertyName != null" >
        intellectual_property_name,
      </if>
      <if test="sortNumber != null" >
        sort_number,
      </if>
      <if test="catagory != null" >
        catagory,
      </if>
      <if test="obtainWay != null" >
        obtain_way,
      </if>
      <if test="authorizationNumber != null" >
        authorization_number,
      </if>
      <if test="authorizationDate != null" >
        authorization_date,
      </if>
      <if test="evaluationCategory != null" >
        evaluation_category,
      </if>
      <if test="propertyRightUrl != null" >
        property_right_url,
      </if>
       <if test="type != null" >
        type,
      </if>
      <if test="deletedSign != null" >
        deleted_sign,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="uid != null" >
        #{uid,jdbcType=VARCHAR},
      </if>
       <if test="pid != null" >
        #{pid,jdbcType=VARCHAR},
      </if>
      <if test="intellectualPropertyNumber != null" >
        #{intellectualPropertyNumber,jdbcType=VARCHAR},
      </if>
      <if test="intellectualPropertyName != null" >
        #{intellectualPropertyName,jdbcType=VARCHAR},
      </if>
      <if test="sortNumber != null" >
        #{sortNumber,jdbcType=VARCHAR},
      </if>
      <if test="catagory != null" >
        #{catagory,jdbcType=INTEGER},
      </if>
      <if test="obtainWay != null" >
        #{obtainWay,jdbcType=INTEGER},
      </if>
      <if test="authorizationNumber != null" >
        #{authorizationNumber,jdbcType=VARCHAR},
      </if>
      <if test="authorizationDate != null" >
        #{authorizationDate,jdbcType=TIMESTAMP},
      </if>
      <if test="evaluationCategory != null" >
        #{evaluationCategory,jdbcType=INTEGER},
      </if>
      <if test="propertyRightUrl != null" >
        #{propertyRightUrl,jdbcType=VARCHAR},
      </if>
        <if test="type != null" >
        #{type,jdbcType=INTEGER},
      </if>
      <if test="deletedSign != null" >
        #{deletedSign,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.OrgIntellectualProperty" >
    update org_intellectual_property
    <set >
      <if test="uid != null" >
        uid = #{uid,jdbcType=VARCHAR},
      </if>
       <if test="pid != null" >
        pid = #{pid,jdbcType=VARCHAR},
      </if>
      <if test="intellectualPropertyNumber != null" >
        intellectual_property_number = #{intellectualPropertyNumber,jdbcType=VARCHAR},
      </if>
      <if test="intellectualPropertyName != null" >
        intellectual_property_name = #{intellectualPropertyName,jdbcType=VARCHAR},
      </if>
      <if test="sortNumber != null" >
        sort_number = #{sortNumber,jdbcType=VARCHAR},
      </if>
      <if test="catagory != null" >
        catagory = #{catagory,jdbcType=INTEGER},
      </if>
      <if test="obtainWay != null" >
        obtain_way = #{obtainWay,jdbcType=INTEGER},
      </if>
      <if test="authorizationNumber != null" >
        authorization_number = #{authorizationNumber,jdbcType=VARCHAR},
      </if>
      <if test="authorizationDate != null" >
        authorization_date = #{authorizationDate,jdbcType=TIMESTAMP},
      </if>
      <if test="evaluationCategory != null" >
        evaluation_category = #{evaluationCategory,jdbcType=INTEGER},
      </if>
      <if test="propertyRightUrl != null" >
        property_right_url = #{propertyRightUrl,jdbcType=VARCHAR},
      </if>
      <if test="type != null" >
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="deletedSign != null" >
        deleted_sign = #{deletedSign,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.OrgIntellectualProperty" >
    update org_intellectual_property
    set uid = #{uid,jdbcType=VARCHAR},
      pid = #{pid,jdbcType=VARCHAR},
      intellectual_property_number = #{intellectualPropertyNumber,jdbcType=VARCHAR},
      intellectual_property_name = #{intellectualPropertyName,jdbcType=VARCHAR},
      sort_number = #{sortNumber,jdbcType=VARCHAR},
      catagory = #{catagory,jdbcType=INTEGER},
      obtain_way = #{obtainWay,jdbcType=INTEGER},
      authorization_number = #{authorizationNumber,jdbcType=VARCHAR},
      authorization_date = #{authorizationDate,jdbcType=TIMESTAMP},
      evaluation_category = #{evaluationCategory,jdbcType=INTEGER},
      property_right_url = #{propertyRightUrl,jdbcType=VARCHAR},
      type = #{type,jdbcType=INTEGER},
      deleted_sign = #{deletedSign,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  
  <select id="findOrgIntellectualPropertyListByPage" parameterType="java.lang.String" resultMap="BaseResultMap">
  	 select 
    <include refid="Base_Column_List" />
    from org_intellectual_property
    where deleted_sign = 0
    <if test="uid != null">
	    and  uid = #{uid,jdbcType=VARCHAR}
	</if>
	<if test="sDate != null">
		and authorization_date <![CDATA[ >= ]]> #{sDate,jdbcType=TIMESTAMP}
	</if>
	<if test="eDate != null">
		and authorization_date <![CDATA[ < ]]> #{eDate,jdbcType=TIMESTAMP}
	</if>
	order by authorization_date desc
    <if test="page_sql != null">
    	${page_sql}
    </if>
    
  </select>
  
  <select id="findOrgIntellectualPropertyCount" resultType="java.lang.Integer" parameterType="String">
   	select count(1) 
	    from org_intellectual_property
	     where deleted_sign = 0
	    <if test="uid != null">
		    and  uid = #{uid,jdbcType=VARCHAR}
		</if>
		<if test="sDate != null">
		and authorization_date <![CDATA[ >= ]]> #{sDate,jdbcType=TIMESTAMP}
	</if>
	<if test="eDate != null">
		and authorization_date <![CDATA[ < ]]> #{eDate,jdbcType=TIMESTAMP}
	</if>
    </select>
    
  <update id="batchDeleteByPrimaryKey" parameterType="java.util.List">
  	update org_intellectual_property set deleted_sign = 1
  	where  id in 
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
		#{item}
	</foreach>
  </update>
  
   <select id="selectOrgIntellectualPropertyByPid" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from org_intellectual_property
    where pid = #{pid,jdbcType=VARCHAR} and deleted_sign = 0
  </select>
  
  <select id="selectPatentTypeDetail" parameterType="java.lang.String" resultType="com.goafanti.cognizance.bo.OrgIntellectualPropertyDetailBo">
  	select 
    o.id, o.uid, o.pid, o.intellectual_property_number as intellectualPropertyNumber, o.intellectual_property_name as intellectualPropertyName,
    o.sort_number as sortNumber, o.catagory, 
    o.obtain_way as obtainWay, o.authorization_number as authorizationNumber, o.authorization_date as authorizationDate, 
    o.evaluation_category as evaluationCategory, o.property_right_url as propertyRightUrl, 
    o.type, p.patent_writing_url as manuscript
    from org_intellectual_property o LEFT JOIN patent_info p on o.pid = p.id
    where o.id = #{id,jdbcType=VARCHAR} and o.deleted_sign = 0 and p.deleted_sign = 0
  </select>
  
  <select id="selectCopyrightTypeDetail" parameterType="java.lang.String" resultType="com.goafanti.cognizance.bo.OrgIntellectualPropertyDetailBo">
  	select 
     o.id, o.uid, o.pid, o.intellectual_property_number as intellectualPropertyNumber, o.intellectual_property_name as intellectualPropertyName,
    o.sort_number as sortNumber, o.catagory, 
    o.obtain_way as obtainWay, o.authorization_number as authorizationNumber, o.authorization_date as authorizationDate, 
    o.evaluation_category as evaluationCategory, o.property_right_url as propertyRightUrl, 
    o.type, c.application_url as manuscript
    from org_intellectual_property o LEFT JOIN copyright_info c on o.pid = c.id
    where o.id = #{id,jdbcType=VARCHAR} and o.deleted_sign = 0 and c.delete_sign = 0
  </select>
  
  <select id="selectIntellectualPropertyList" resultMap="BaseResultMap">
   select 
    <include refid="Base_Column_List" />
    from org_intellectual_property
    where uid = #{1} and authorization_date 
		 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H'))
				and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
	and deleted_sign = 0 order by authorization_date
  </select>
  
   <select id="selectIntellectualPropertyCount" resultType="com.goafanti.cognizance.bo.AnnualReportPropertyRightBo">
  	select 
  		u.id as uid, 
  		a.utilityPatent, 
  		b.exteriorPatent, 
  		c.softwareWorks, 
  		d.defensePatent, 
  		e.newPlantCariety, 
  		f.nationalCrop,
		g.nationalDrug, 
		h.chineseMedicine, 
		i.circuitDesign, 
		j.inventionPatent from 
		(
			select 
				id 
			from user 
			where id = #{1}
		) 
		u LEFT JOIN
		
		(
			select 
				uid, 
				count(1) as utilityPatent 
			from org_intellectual_property 
			where uid = #{1} 
			and catagory = 2 
			and authorization_date >= 
				(
					select  
						str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H')
				)
			and authorization_date  <![CDATA[ < ]]> 
				(
					select  
						str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H')
				)
					and deleted_sign = 0
		) a on a.uid = u.id  
		LEFT JOIN
		(
		select uid, count(1) as exteriorPatent 
			from org_intellectual_property 
			where uid = #{1} and catagory = 3 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) b on u.id = b.uid 
		LEFT JOIN
		(
			select uid, count(1) as softwareWorks 
			from org_intellectual_property 
			where uid = #{1} and catagory = 4 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) c on u.id = c.uid  
		LEFT JOIN
		(
		select uid, count(1) as defensePatent 
			from org_intellectual_property 
			where uid = #{1} and catagory = 5 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) d on u.id = d.uid 
		LEFT JOIN
		(
			select uid, count(1) as newPlantCariety 
			from org_intellectual_property 
			where uid = #{1} and catagory = 6 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) e on u.id = e.uid  
		LEFT JOIN
		(
		select uid, count(1) as nationalCrop 
			from org_intellectual_property 
			where uid = #{1} and catagory = 7 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) f on u.id = f.uid 
		LEFT JOIN
		(
			select uid, count(1) as nationalDrug 
			from org_intellectual_property 
			where uid = #{1} and catagory = 8 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) g on u.id = g.uid  
		LEFT JOIN
		(
		select uid, count(1) as chineseMedicine 
			from org_intellectual_property 
			where uid = #{1} and catagory = 9 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) h on u.id = h.uid 
		LEFT JOIN
		(
			select uid, count(1) as circuitDesign 
			from org_intellectual_property 
			where uid = #{1} and catagory = 10 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) i on u.id = i.uid  
		LEFT JOIN
		(
		select uid, count(1) as inventionPatent 
			from org_intellectual_property 
			where uid = #{1} and catagory = 11 and authorization_date 
				 >= (select  str_to_date(CONCAT(#{0}-2,'-01-01'), '%Y-%m-%d %H') )
						and authorization_date  <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H'))
			and deleted_sign = 0
		) j on u.id = j.uid 
  </select>
  
  <select id="findIntellectualPropertyNum" parameterType="java.lang.String" resultType="java.lang.Integer">
  	select 
  		count(1)
  	from 
  		org_intellectual_property
  	where 
  		deleted_sign = 0
  	and uid = #{uid,jdbcType=VARCHAR}
  </select>
  
   <select id="findByUidAndIntellectualPropertyNumber" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from org_intellectual_property
    where uid = #{uid,jdbcType=VARCHAR}
    and intellectual_Property_number = #{intellectualPropertyNumber,jdbcType=VARCHAR}
    and deleted_sign = 0
  </select>
</mapper>