<?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.PatentCostMapper">
	<resultMap id="BaseResultMap" type="com.goafanti.common.model.PatentCost">
		<id column="id" property="id" jdbcType="VARCHAR" />
		<result column="pid" property="pid" jdbcType="VARCHAR" />
		<result column="annual_fee" property="annualFee" jdbcType="INTEGER" />
		<result column="annual_fee_state" property="annualFeeState"
			jdbcType="INTEGER" />
		<result column="application_fee" property="applicationFee"
			jdbcType="INTEGER" />
		<result column="trial_fee" property="trialFee" jdbcType="INTEGER" />
		<result column="printing_fee" property="printingFee" jdbcType="INTEGER" />
		<result column="funds" property="funds" jdbcType="INTEGER" />
		<result column="reimbursement" property="reimbursement"
			jdbcType="INTEGER" />
		<result column="payment_state" property="paymentState"
			jdbcType="INTEGER" />
	</resultMap>
	<sql id="Base_Column_List">
		id, pid, annual_fee, annual_fee_state, application_fee, trial_fee,
		printing_fee,
		funds, reimbursement, payment_state
	</sql>
	<select id="selectByPrimaryKey" resultMap="BaseResultMap"
		parameterType="java.lang.String">
		select
		<include refid="Base_Column_List" />
		from patent_cost
		where id = #{id,jdbcType=VARCHAR}
	</select>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
		delete from patent_cost
		where id = #{id,jdbcType=VARCHAR}
	</delete>
	<insert id="insert" parameterType="com.goafanti.common.model.PatentCost">
		insert into patent_cost (id, pid, annual_fee,
		annual_fee_state, application_fee, trial_fee,
		printing_fee, funds, reimbursement,
		payment_state)
		values (#{id,jdbcType=VARCHAR}, #{pid,jdbcType=VARCHAR},
		#{annualFee,jdbcType=INTEGER},
		#{annualFeeState,jdbcType=INTEGER}, #{applicationFee,jdbcType=INTEGER}, #{trialFee,jdbcType=INTEGER},
		#{printingFee,jdbcType=INTEGER}, #{funds,jdbcType=INTEGER},
		#{reimbursement,jdbcType=INTEGER},
		#{paymentState,jdbcType=INTEGER})
	</insert>
	<insert id="insertSelective" parameterType="com.goafanti.common.model.PatentCost">
		insert into patent_cost
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">
				id,
			</if>
			<if test="pid != null">
				pid,
			</if>
			<if test="annualFee != null">
				annual_fee,
			</if>
			<if test="annualFeeState != null">
				annual_fee_state,
			</if>
			<if test="applicationFee != null">
				application_fee,
			</if>
			<if test="trialFee != null">
				trial_fee,
			</if>
			<if test="printingFee != null">
				printing_fee,
			</if>
			<if test="funds != null">
				funds,
			</if>
			<if test="reimbursement != null">
				reimbursement,
			</if>
			<if test="paymentState != null">
				payment_state,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">
				#{id,jdbcType=VARCHAR},
			</if>
			<if test="pid != null">
				#{pid,jdbcType=VARCHAR},
			</if>
			<if test="annualFee != null">
				#{annualFee,jdbcType=INTEGER},
			</if>
			<if test="annualFeeState != null">
				#{annualFeeState,jdbcType=INTEGER},
			</if>
			<if test="applicationFee != null">
				#{applicationFee,jdbcType=INTEGER},
			</if>
			<if test="trialFee != null">
				#{trialFee,jdbcType=INTEGER},
			</if>
			<if test="printingFee != null">
				#{printingFee,jdbcType=INTEGER},
			</if>
			<if test="funds != null">
				#{funds,jdbcType=INTEGER},
			</if>
			<if test="reimbursement != null">
				#{reimbursement,jdbcType=INTEGER},
			</if>
			<if test="paymentState != null">
				#{paymentState,jdbcType=INTEGER},
			</if>
		</trim>
	</insert>
	<update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.PatentCost">
		update patent_cost
		<set>
			<if test="pid != null">
				pid = #{pid,jdbcType=VARCHAR},
			</if>
			<if test="annualFee != null">
				annual_fee = #{annualFee,jdbcType=INTEGER},
			</if>
			<if test="annualFeeState != null">
				annual_fee_state = #{annualFeeState,jdbcType=INTEGER},
			</if>
			<if test="applicationFee != null">
				application_fee = #{applicationFee,jdbcType=INTEGER},
			</if>
			<if test="trialFee != null">
				trial_fee = #{trialFee,jdbcType=INTEGER},
			</if>
			<if test="printingFee != null">
				printing_fee = #{printingFee,jdbcType=INTEGER},
			</if>
			<if test="funds != null">
				funds = #{funds,jdbcType=INTEGER},
			</if>
			<if test="reimbursement != null">
				reimbursement = #{reimbursement,jdbcType=INTEGER},
			</if>
			<if test="paymentState != null">
				payment_state = #{paymentState,jdbcType=INTEGER},
			</if>
		</set>
		where id = #{id,jdbcType=VARCHAR}
	</update>
	<update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.PatentCost">
		update patent_cost
		set pid = #{pid,jdbcType=VARCHAR},
		annual_fee = #{annualFee,jdbcType=INTEGER},
		annual_fee_state = #{annualFeeState,jdbcType=INTEGER},
		application_fee = #{applicationFee,jdbcType=INTEGER},
		trial_fee = #{trialFee,jdbcType=INTEGER},
		printing_fee = #{printingFee,jdbcType=INTEGER},
		funds = #{funds,jdbcType=INTEGER},
		reimbursement = #{reimbursement,jdbcType=INTEGER},
		payment_state = #{paymentState,jdbcType=INTEGER}
		where id = #{id,jdbcType=VARCHAR}
	</update>

	<select id="findApplicationFeeListByPage" parameterType="String"
		resultType="com.goafanti.patent.bo.PatentApplicationFeeBo">
		select p.uid as uid , p.id as pid ,o.id as oid, c.id as cid,
		p.serial_number as serialNumber ,p.patent_number as patentNumber ,
		o.licence_province as locationProvince , o.unit_name as unitName ,
		p.patent_name as patentName , p.patent_application_date as
		patentApplicationDate,
		c.payment_state as paymentState,c.application_fee as applicationFee ,
		c.trial_fee as trialFee , c.printing_fee as printingFee ,
		c.funds as funds , c.reimbursement as reimbursement
		from patent_info p
		LEFT JOIN organization_identity o on o.uid = p.uid
		LEFT JOIN patent_cost c on p.id = c.pid 
		<if test="principal != null">
			left join contract_log l on p.contract_id=l.cid
		</if>
		WHERE p.deleted_sign = 0
		<if test="locationProvince != null">
			and o.licence_province = #{locationProvince,jdbcType=INTEGER}
		</if>
		<if test="pStart != null">
			and p.patent_application_date <![CDATA[ >= ]]>
			#{pStart,jdbcType=TIMESTAMP}
		</if>
		<if test="pEnd">
			and p.patent_application_date <![CDATA[ < ]]>
			#{pEnd,jdbcType=TIMESTAMP}
		</if>
		<if test="principal != null">
			group by p.id,o.id,c.id
		</if>
		order by p.serial_number DESC
		<if test="page_sql!=null">
			${page_sql}
		</if>
	</select>

	<select id="findApplicationFeeCount" resultType="java.lang.Integer"
		parameterType="java.lang.String">
		select count(distinct(p.id))
		from patent_info p
		LEFT JOIN organization_identity o on o.uid = p.uid
		LEFT JOIN patent_cost c on p.id = c.pid
		<if test="principal != null">
			left join contract_log l on p.contract_id=l.cid
		</if>
		 WHERE p.deleted_sign = 0
		<if test="locationProvince != null">
			and o.licence_province = #{locationProvince,jdbcType=INTEGER}
		</if>
		<if test="pStart != null">
			and p.patent_application_date <![CDATA[ >= ]]>
			#{pStart,jdbcType=TIMESTAMP}
		</if>
		<if test="pEnd">
			and p.patent_application_date <![CDATA[ < ]]>
			#{pEnd,jdbcType=TIMESTAMP}
		</if>
	</select>
	
 <insert id="batchInsert" parameterType="com.goafanti.common.model.PatentCost">
   insert into patent_cost (id, pid, annual_fee,
		annual_fee_state, application_fee, trial_fee,
		printing_fee, funds, reimbursement,
		payment_state)
	values 
    <foreach item="item" index="index" collection="list" separator=",">
      (
      	#{item.id,jdbcType=VARCHAR}, 
      	#{item.pid,jdbcType=VARCHAR},
		#{item.annualFee,jdbcType=INTEGER},
		#{item.annualFeeState,jdbcType=INTEGER}, 
		#{item.applicationFee,jdbcType=INTEGER}, 
		#{item.trialFee,jdbcType=INTEGER},
		#{item.printingFee,jdbcType=INTEGER}, 
		#{item.funds,jdbcType=INTEGER},
		#{item.reimbursement,jdbcType=INTEGER},
		#{item.paymentState,jdbcType=INTEGER}
	  )
    </foreach>
  </insert>
  
  <update id="batchUpdateByPrimaryKey" parameterType="java.util.List">
		update patent_cost set annual_fee_state = 0
		where id in
		<foreach item="item" index="index" collection="list" open="("
			separator="," close=")">
			#{item}
		</foreach>
	</update>
</mapper>