<?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.OrgAnnualReportMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.OrgAnnualReport" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="uid" property="uid" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="last_update_time" property="lastUpdateTime" jdbcType="TIMESTAMP" />
    <result column="total_tax" property="totalTax" jdbcType="DECIMAL" />
    <result column="income_tax_relief" property="incomeTaxRelief" jdbcType="DECIMAL" />
    <result column="total_export_volume" property="totalExportVolume" jdbcType="DECIMAL" />
    <result column="basic_research_cost" property="basicResearchCost" jdbcType="DECIMAL" />
    <result column="year" property="year" jdbcType="INTEGER" />
    <result column="state" property="state" jdbcType="INTEGER" />
    <result column="deleted_sign" property="deletedSign" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, uid, create_time, last_update_time, total_tax, income_tax_relief, total_export_volume, basic_research_cost,
    year, state, deleted_sign
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
	    select 
	    <include refid="Base_Column_List" />
	    from org_annual_report
	    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from org_annual_report
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.OrgAnnualReport" >
    insert into org_annual_report (id, uid, create_time, 
      last_update_time, total_tax, income_tax_relief, 
      total_export_volume, basic_research_cost, year, state, 
      deleted_sign)
    values (#{id,jdbcType=VARCHAR}, #{uid,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, 
      #{lastUpdateTime,jdbcType=TIMESTAMP}, #{totalTax,jdbcType=DECIMAL}, #{incomeTaxRelief,jdbcType=DECIMAL}, 
      #{totalExportVolume,jdbcType=DECIMAL}, #{basicResearchCost,jdbcType=DECIMAL},
      #{year,jdbcType=INTEGER}, #{state,jdbcType=INTEGER}, 
      #{deletedSign,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.OrgAnnualReport" >
    insert into org_annual_report
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="uid != null" >
        uid,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="lastUpdateTime != null" >
        last_update_time,
      </if>
      <if test="totalTax != null" >
        total_tax,
      </if>
      <if test="incomeTaxRelief != null" >
        income_tax_relief,
      </if>
      <if test="totalExportVolume != null" >
        total_export_volume,
      </if>
      <if test="basicResearchCost != null">
      	basic_research_cost
      </if>
      <if test="year != null" >
        year,
      </if>
      <if test="state != null" >
        state,
      </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="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastUpdateTime != null" >
        #{lastUpdateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="totalTax != null" >
        #{totalTax,jdbcType=DECIMAL},
      </if>
      <if test="incomeTaxRelief != null" >
        #{incomeTaxRelief,jdbcType=DECIMAL},
      </if>
      <if test="totalExportVolume != null" >
        #{totalExportVolume,jdbcType=DECIMAL},
      </if>
      <if test="basicResearchCost != null">
      	#{basicResearchCost,jdbcType=DECIMAL},
      </if>
      <if test="year != null" >
        #{year,jdbcType=INTEGER},
      </if>
      <if test="state != null" >
        #{state,jdbcType=INTEGER},
      </if>
      <if test="deletedSign != null" >
        #{deletedSign,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.OrgAnnualReport" >
    update org_annual_report
    <set >
      <if test="uid != null" >
        uid = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastUpdateTime != null" >
        last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="totalTax != null" >
        total_tax = #{totalTax,jdbcType=DECIMAL},
      </if>
      <if test="incomeTaxRelief != null" >
        income_tax_relief = #{incomeTaxRelief,jdbcType=DECIMAL},
      </if>
      <if test="totalExportVolume != null" >
        total_export_volume = #{totalExportVolume,jdbcType=DECIMAL},
      </if>
      <if test="basicResearchCost != null" >
        basic_research_cost = #{basicResearchCost,jdbcType=DECIMAL},
      </if>
      <if test="year != null" >
        year = #{year,jdbcType=INTEGER},
      </if>
      <if test="state != null" >
        state = #{state,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.OrgAnnualReport" >
    update org_annual_report
    set uid = #{uid,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP},
      total_tax = #{totalTax,jdbcType=DECIMAL},
      income_tax_relief = #{incomeTaxRelief,jdbcType=DECIMAL},
      total_export_volume = #{totalExportVolume,jdbcType=DECIMAL},
      basic_research_cost = #{basicResearchCost,jdbcType=DECIMAL},
      year = #{year,jdbcType=INTEGER},
      state = #{state,jdbcType=INTEGER},
      deleted_sign = #{deletedSign,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <select id="findOrgAnnualReportListByPage" parameterType="java.lang.String" resultMap="BaseResultMap">
  	 select 
    <include refid="Base_Column_List" />
    from org_annual_report
    where deleted_sign = 0
    <if test="year != null">
	    and  year = #{year,jdbcType=INTEGER}
	</if>
	<if test="state != null">
		and state = #{state,jdbcType=INTEGER}
	</if>
	<if test="uid != null">
		and uid = #{uid,jdbcType=VARCHAR}
	</if>
	order by year desc
    <if test="page_sql != null">
    	${page_sql}
    </if>
    
  </select>
  
  <select id="findOrgAnnualReportCount" resultType="java.lang.Integer" parameterType="String">
	   	select count(1) 
		    from org_annual_report
	    where deleted_sign = 0
		<if test="year != null">
		    and  year = #{year,jdbcType=INTEGER}
		</if>
		<if test="state != null">
			and state = #{state,jdbcType=INTEGER}
		</if>
		<if test="uid != null">
			and uid = #{uid,jdbcType=VARCHAR}
		</if>
  </select>
    
  <update id="batchDeleteByPrimaryKey" parameterType="java.util.List">
	  	update org_annual_report set deleted_sign = 1
	  	where  id in 
		<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
			#{item}
		</foreach>
  </update>
  
  <select id="selectAnnualReportPropertyRightBoByYearAndUid" 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},'-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},'-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},'-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},'-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},'-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},'-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},'-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},'-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},'-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},'-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="selectAnnualReportTerritoryByYearAndUid" resultType="java.math.BigDecimal">
  	select   sum(m.territory) as territory from(
			select uid,  
			external_abroad_cost as territory
			 from org_activity_cost 
       where uid = #{1} and 
     (start_date >= (select  str_to_date(CONCAT(#{0},'-01-01'), '%Y-%m-%d %H') )
      and start_date <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H')  )
			or (end_date  >= (select  str_to_date(CONCAT(#{0},'-01-01'), '%Y-%m-%d %H') )
      and end_date <![CDATA[ < ]]> (select  str_to_date(CONCAT(#{0}+1,'-01-01'), '%Y-%m-%d %H') ) ))
      and deleted_sign = 0
		) m
  </select>
  
  <select id="selectAnnualReportMainBoByYearAndUid" resultType="com.goafanti.cognizance.bo.AnnualReportMainBo">
		  select u.id as uid, o.unit_name as unitName, o.org_code as orgCode, o.location_province as locationProvince,
			o.location_city as locationCity, o.location_area as locationArea, o.listed, o.listed_date as listedDate, 
			o.listed_type as listedType, o.stock_code as stockCode, h.firm_total as firmTotal, h.tech_total as techTotal,
			h.new_employment as newEmployment, h.graduate_number as graduateNumber,
			f.sales_revenue as salesRevenue, f.gross_profit as grossProfit, f.net_asset as netAsset, 
			c.certificate_number as certificateNumber, c.issuing_date as issuingDate, c.contacts, r.research_cost as researchCost,
			x.totalRevenue, y.lastYearRevenue from 
			  (select id from user where id = #{1}) u
			LEFT JOIN organization_identity o
			on u.id = o.uid  AND o.audit_status = 5  
			LEFT JOIN org_human_resource h on h.uid = u.id  AND h.year = #{0} AND h.deleted_sign = 0
			LEFT JOIN org_finance f on f.uid = u.id  AND f.year = #{0}  AND f.deleted_sign = 0
			LEFT JOIN org_cognizance c on c.uid = u.id  AND c.year <![CDATA[ <= ]]> #{0} AND c.year <![CDATA[ > ]]> (#{0} - 3) AND c.deleted_sign = 0
			LEFT JOIN org_ratepay r on r.uid = u.id  AND r.year = #{0} AND r.deleted_sign = 0
			LEFT JOIN
			     (SELECT uid,(operating_income + non_operating_income - non_taxable_income) as totalRevenue
							from org_ratepay where uid = #{1}
							and year = #{0} and deleted_sign = 0) 
			      x on x.uid = u.id 
			LEFT JOIN 
			     (
			       SELECT uid, sum(last_year_revenue) as lastYearRevenue
						from org_tech_product where uid = #{1}
						and year = #{0} and deleted_sign = 0
			      )   y on y.uid = u.id 
  </select>
  
  <select id="selectAnnualReportByYearAndUid" resultMap="BaseResultMap">
  	 select 
    <include refid="Base_Column_List" />
    from org_annual_report
    where year = #{0} and uid = #{1} and deleted_sign = 0
  </select>
  
</mapper>