<?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.CopyrightInfoMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.CopyrightInfo">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="uid" jdbcType="VARCHAR" property="uid" />
    <result column="serial_number" jdbcType="INTEGER" property="serialNumber" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="accept_time" jdbcType="TIMESTAMP" property="acceptTime" />
    <result column="principal" jdbcType="VARCHAR" property="principal" />
    <result column="contact" jdbcType="INTEGER" property="contact" />
    <result column="copyright_info" jdbcType="VARCHAR" property="copyrightInfo" />
    <result column="copyright_name" jdbcType="VARCHAR" property="copyrightName" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="work_issue" jdbcType="VARCHAR" property="workIssue" />
    <result column="comment" jdbcType="VARCHAR" property="comment" />
    <result column="outsource" jdbcType="VARCHAR" property="outsource" />
    <result column="in_urgent" jdbcType="INTEGER" property="inUrgent" />
    <result column="authorized_date" jdbcType="TIMESTAMP" property="authorizedDate" />
    <result column="certificate_url" jdbcType="VARCHAR" property="certificateUrl" />
    <result column="copyright_number" jdbcType="VARCHAR" property="copyrightNumber" />
    <result column="application_url" jdbcType="VARCHAR" property="applicationUrl" />
    <result column="delete_sign" jdbcType="INTEGER" property="deleteSign" />
    <result column="founder" jdbcType="VARCHAR" property="founder" />
    <result column="contract_id" jdbcType="VARCHAR" property="contractId" />
  </resultMap>
  <sql id="Base_Column_List">
    id, uid, serial_number, create_time, accept_time, principal, contact, copyright_info, 
    copyright_name, status, work_issue, comment, outsource, in_urgent, authorized_date, 
    certificate_url, application_url, delete_sign, copyright_number, founder, contract_id
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from copyright_info
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from copyright_info
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.CopyrightInfo">
    insert into copyright_info (id, uid, serial_number, 
      create_time, accept_time, principal, 
      contact, copyright_info, copyright_name, 
      status, work_issue, comment, 
      outsource, in_urgent, authorized_date, 
      certificate_url, copyright_number, application_url, delete_sign, founder, contract_id
      )
    values (#{id,jdbcType=VARCHAR}, #{uid,jdbcType=VARCHAR}, #{serialNumber,jdbcType=INTEGER}, 
      #{createTime,jdbcType=TIMESTAMP}, #{acceptTime,jdbcType=TIMESTAMP}, #{principal,jdbcType=VARCHAR}, 
      #{contact,jdbcType=INTEGER}, #{copyrightInfo,jdbcType=VARCHAR}, #{copyrightName,jdbcType=VARCHAR}, 
      #{status,jdbcType=INTEGER}, #{workIssue,jdbcType=VARCHAR}, #{comment,jdbcType=VARCHAR}, 
      #{outsource,jdbcType=VARCHAR}, #{inUrgent,jdbcType=INTEGER}, #{authorizedDate,jdbcType=TIMESTAMP}, 
      #{certificateUrl,jdbcType=VARCHAR}, 
      #{copyrightNumber,jdbcType=VARCHAR}, 
      #{applicationUrl,jdbcType=VARCHAR}, #{deleteSign,jdbcType=INTEGER}, #{founder,jdbcType=VARCHAR},
      #{contractId,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.CopyrightInfo">
    insert into copyright_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="uid != null">
        uid,
      </if>
      <if test="serialNumber != null">
        serial_number,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="acceptTime != null">
        accept_time,
      </if>
      <if test="principal != null">
        principal,
      </if>
      <if test="contact != null">
        contact,
      </if>
      <if test="copyrightInfo != null">
        copyright_info,
      </if>
      <if test="copyrightName != null">
        copyright_name,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="workIssue != null">
        work_issue,
      </if>
      <if test="comment != null">
        comment,
      </if>
      <if test="outsource != null">
        outsource,
      </if>
      <if test="inUrgent != null">
        in_urgent,
      </if>
      <if test="authorizedDate != null">
        authorized_date,
      </if>
      <if test="certificateUrl != null">
        certificate_url,
      </if>
      <if test="copyrightNumber != null">
        copyright_number,
      </if>
      <if test="applicationUrl != null">
        application_url,
      </if>
      <if test="deleteSign != null">
        delete_sign,
      </if>
      <if test="founder != null">
        founder,
      </if>
       <if test="contractId != null">
        contract_id,
      </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="serialNumber != null">
        #{serialNumber,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="acceptTime != null">
        #{acceptTime,jdbcType=TIMESTAMP},
      </if>
      <if test="principal != null">
        #{principal,jdbcType=VARCHAR},
      </if>
      <if test="contact != null">
        #{contact,jdbcType=INTEGER},
      </if>
      <if test="copyrightInfo != null">
        #{copyrightInfo,jdbcType=VARCHAR},
      </if>
      <if test="copyrightName != null">
        #{copyrightName,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="workIssue != null">
        #{workIssue,jdbcType=VARCHAR},
      </if>
      <if test="comment != null">
        #{comment,jdbcType=VARCHAR},
      </if>
      <if test="outsource != null">
        #{outsource,jdbcType=VARCHAR},
      </if>
      <if test="inUrgent != null">
        #{inUrgent,jdbcType=INTEGER},
      </if>
      <if test="authorizedDate != null">
        #{authorizedDate,jdbcType=TIMESTAMP},
      </if>
      <if test="certificateUrl != null">
        #{certificateUrl,jdbcType=VARCHAR},
      </if>
      <if test="copyrightNumber != null">
        #{copyrightNumber,jdbcType=VARCHAR},
      </if>
      <if test="applicationUrl != null">
        #{applicationUrl,jdbcType=VARCHAR},
      </if>
      <if test="deleteSign != null">
        #{deleteSign,jdbcType=INTEGER},
      </if>
      <if test="founder != null">
        #{founder,jdbcType=VARCHAR},
      </if>
      <if test="contractId != null">
        #{contractId,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.CopyrightInfo">
    update copyright_info
    <set>
      <if test="uid != null">
        uid = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="serialNumber != null">
        serial_number = #{serialNumber,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="acceptTime != null">
        accept_time = #{acceptTime,jdbcType=TIMESTAMP},
      </if>
      <if test="principal != null">
        principal = #{principal,jdbcType=VARCHAR},
      </if>
      <if test="contact != null">
        contact = #{contact,jdbcType=INTEGER},
      </if>
      <if test="copyrightInfo != null">
        copyright_info = #{copyrightInfo,jdbcType=VARCHAR},
      </if>
      <if test="copyrightName != null">
        copyright_name = #{copyrightName,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="workIssue != null">
        work_issue = #{workIssue,jdbcType=VARCHAR},
      </if>
      <if test="comment != null">
        comment = #{comment,jdbcType=VARCHAR},
      </if>
      <if test="outsource != null">
        outsource = #{outsource,jdbcType=VARCHAR},
      </if>
      <if test="inUrgent != null">
        in_urgent = #{inUrgent,jdbcType=INTEGER},
      </if>
      <if test="authorizedDate != null">
        authorized_date = #{authorizedDate,jdbcType=TIMESTAMP},
      </if>
      <if test="certificateUrl != null">
        certificate_url = #{certificateUrl,jdbcType=VARCHAR},
      </if>
      <if test="copyrightNumber != null">
        copyright_number = #{copyrightNumber,jdbcType=VARCHAR},
      </if>
      <if test="applicationUrl != null">
        application_url = #{applicationUrl,jdbcType=VARCHAR},
      </if>
      <if test="deleteSign != null">
        delete_sign = #{deleteSign,jdbcType=INTEGER},
      </if>
      <if test="founder != null">
        founder = #{founder,jdbcType=VARCHAR},
      </if>
       <if test="contractId != null">
        contract_id = #{contractId,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.CopyrightInfo">
    update copyright_info
    set uid = #{uid,jdbcType=VARCHAR},
      serial_number = #{serialNumber,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      accept_time = #{acceptTime,jdbcType=TIMESTAMP},
      principal = #{principal,jdbcType=VARCHAR},
      contact = #{contact,jdbcType=INTEGER},
      copyright_info = #{copyrightInfo,jdbcType=VARCHAR},
      copyright_name = #{copyrightName,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      work_issue = #{workIssue,jdbcType=VARCHAR},
      comment = #{comment,jdbcType=VARCHAR},
      outsource = #{outsource,jdbcType=VARCHAR},
      in_urgent = #{inUrgent,jdbcType=INTEGER},
      authorized_date = #{authorizedDate,jdbcType=TIMESTAMP},
      certificate_url = #{certificateUrl,jdbcType=VARCHAR},
      copyright_number = #{copyrightNumber,jdbcType=VARCHAR},
      application_url = #{applicationUrl,jdbcType=VARCHAR},
      delete_sign = #{deleteSign,jdbcType=INTEGER},
      founder = #{founder,jdbcType=VARCHAR},
      contract_id = #{contractId,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <select id="findListByPage" resultType="com.goafanti.copyright.bo.CopyrightInfoSummary"  parameterType="String">
  	select ci.id,ci.uid,ci.serial_number as serialNumber, ci.create_time as createTime, 
	ci.accept_time as acceptTime, ci.contact,
	ci.copyright_name as copyrightName, ci.status, ci.comment, ci.outsource, ci.in_urgent as inUrgent, 
	ci.authorized_date as authorizedDate, ci.work_issue as workIssue, ci.copyright_number as copyrightNumber,
	oi.licence_province as province,oi.unit_name as unitName, 

	a.name as principal, ad.name as founder, c.serial_number as contractNumber
	from copyright_info ci 
	left join organization_identity oi on ci.uid = oi.uid
	left join admin a on ci.principal = a.id
	left join admin ad on ci.founder = ad.id
	left join contract c on c.id = ci.contract_id
	<if test="principal != null">
		left join contract_log cl on ci.contract_id=cl.cid
	</if>
	where ci.delete_sign = 0
	<if test="principal != null">
		and cl.principal = #{principal,jdbcType=VARCHAR}
	</if>
	<if test="province != null">
	    and oi.licence_province =  #{province,jdbcType=INTEGER}
	</if>
	<if test="status != null">
	    and ci.status = #{status,jdbcType=INTEGER}
	</if>
	<if test="contractId != null">
	    and ci.contract_id = #{contractId,jdbcType=INTEGER}
	</if>
	<if test="unitName != null">
	    and oi.unit_name like CONCAT('%', #{unitName,jdbcType=VARCHAR}, '%')
	</if>
	<if test="unitId != null">
	    and ci.uid = #{unitId,jdbcType=VARCHAR}
	</if>
	<if test="copyrightName != null">
	    and ci.copyright_name like CONCAT('%', #{copyrightName,jdbcType=VARCHAR}, '%')
	</if>
	<if test="createStartTime != null">
	    and ci.create_time <![CDATA[ >= ]]> #{createStartTime,jdbcType=TIMESTAMP}
	</if>
	<if test="createEndTime != null">
	    and ci.create_time <![CDATA[ < ]]> #{createEndTime,jdbcType=TIMESTAMP}
	</if>
	<if test="acceptStartTime != null">
	    and ci.accept_time <![CDATA[ >= ]]> #{acceptStartTime,jdbcType=TIMESTAMP}
	</if>
	<if test="acceptEndTime != null">
	    and ci.accept_time <![CDATA[ < ]]> #{acceptEndTime,jdbcType=TIMESTAMP}
	</if>
	<if test="authStartTime != null">
	    and ci.authorized_date <![CDATA[ >= ]]> #{authStartTime,jdbcType=TIMESTAMP}
	</if>
	<if test="authEndTime != null">
	    and ci.authorized_date <![CDATA[ < ]]> #{authEndTime,jdbcType=TIMESTAMP}
	</if>
	<if test="principal != null">
		group by ci.id,oi.id
	</if>
	order by ci.serial_number desc
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findListCount" resultType="java.lang.Integer"  parameterType="String">
  	select count(distinct(ci.id))
	from copyright_info ci 
	left join organization_identity oi on ci.uid = oi.uid
	<if test="principal != null">
	left join contract_log cl on ci.contract_id=cl.cid
	</if>
	where ci.delete_sign = 0
	<if test="principal != null">
		and cl.principal = #{principal,jdbcType=VARCHAR}
	</if>
	<if test="province != null">
	    and oi.licence_province =  #{province,jdbcType=INTEGER}
	</if>
	<if test="contractId != null">
	    and ci.contract_id = #{contractId,jdbcType=INTEGER}
	</if>
	<if test="status != null">
	    and ci.status = #{status,jdbcType=INTEGER}
	</if>
	<if test="unitName != null">
	    and oi.unit_name like CONCAT('%', #{unitName,jdbcType=VARCHAR}, '%')
	</if>
	<if test="unitId != null">
	    and ci.uid = #{unitId,jdbcType=VARCHAR}
	</if>
	<if test="copyrightName != null">
	    and ci.copyright_name like CONCAT('%', #{copyrightName,jdbcType=VARCHAR}, '%')
	</if>
	<if test="createStartTime != null">
	    and ci.create_time <![CDATA[ >= ]]> #{createStartTime,jdbcType=TIMESTAMP}
	</if>
	<if test="createEndTime != null">
	    and ci.create_time <![CDATA[ < ]]> #{createEndTime,jdbcType=TIMESTAMP}
	</if>
	<if test="acceptStartTime != null">
	    and ci.accept_time <![CDATA[ >= ]]> #{acceptStartTime,jdbcType=TIMESTAMP}
	</if>
	<if test="acceptEndTime != null">
	    and ci.accept_time <![CDATA[ < ]]> #{acceptEndTime,jdbcType=TIMESTAMP}
	</if>
	<if test="authStartTime != null">
	    and ci.authorized_date <![CDATA[ >= ]]> #{authStartTime,jdbcType=TIMESTAMP}
	</if>
	<if test="authEndTime != null">
	    and ci.authorized_date <![CDATA[ < ]]> #{authEndTime,jdbcType=TIMESTAMP}
	</if>
  </select>
  
  <update id="deleteByPrimaryKeys" parameterType="java.util.List">
  	update copyright_info set delete_sign = 1
  	where id in 
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
		#{item}
	</foreach>
  </update>
  <select id="findByPrimaryKey" parameterType="java.lang.String" resultType="com.goafanti.copyright.bo.CopyrightInfoDetail">
    select ci.id,ci.uid,ci.serial_number as serialNumber, ci.create_time as createTime, 
	ci.accept_time as acceptTime, ci.contact, ci.copyright_name as copyrightName, ci.copyright_info as copyrightInfo, 
	ci.status, ci.comment, ci.outsource, ci.in_urgent as inUrgent, ci.authorized_date as authorizedDate, ci.copyright_number as copyrightNumber,  
	ci.work_issue as workIssue,ci.application_url as applicationUrl,ci.certificate_url as certificateUrl,
	oi.licence_province as province, oi.licence_city as city,oi.licence_area as area, oi.postal_address as postalAddress, oi.postcode,
	oi.unit_name as unitName, oi.org_code as orgCode,
	a.name as principal
	from copyright_info ci 
	left join organization_identity oi on ci.uid = oi.uid
	left join admin a on ci.principal = a.id
    where ci.id = #{id,jdbcType=VARCHAR}
  </select>
  
  <select id="selectByPrimaryKeys" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
		<include refid="Base_Column_List" /> 
	from copyright_info where id in 
		<foreach item="item" index="index" collection="list" open="("
				separator="," close=")">
				#{item}
		</foreach>
  </select>
  
  <select id="countCopyrightDone"  resultType="java.lang.Integer">
   select 
   		count(1) 
   from copyright_info 
   where uid = #{uid,jdbcType=VARCHAR}
   and delete_sign = 0 and status in (8,9) 
   and authorized_date >= 
						(
							select  
								str_to_date(CONCAT(#{year,jdbcType=INTEGER}-3,'-01-01'), '%Y-%m-%d %H')
						)
   and authorized_date  <![CDATA[ < ]]> 
						(
							select  
								str_to_date(CONCAT(#{year,jdbcType=INTEGER},'-01-01'), '%Y-%m-%d %H')
						)
   
  </select>
  
  <select id="countCopyrightUndone"  resultType="java.lang.Integer">
   select 
   		count(1) 
   from copyright_info 
   where uid = #{uid,jdbcType=VARCHAR}
   and delete_sign = 0 
   and status <![CDATA[ < ]]> 8 
   and authorized_date >= 
						(
							select  
								str_to_date(CONCAT(#{year,jdbcType=INTEGER}-3,'-01-01'), '%Y-%m-%d %H')
						)
   and authorized_date  <![CDATA[ < ]]> 
						(
							select  
								str_to_date(CONCAT(#{year,jdbcType=INTEGER},'-01-01'), '%Y-%m-%d %H')
						)
  </select>
  
  <select id="listContractCopyrightByContractId" parameterType="java.lang.String" resultType="com.goafanti.copyright.bo.CopyrightContractListBo">
  	select 
  			ci.id, ci.uid, ci.contract_id as contractId, 
  			ci.serial_number as serialNumber, ci.copyright_name as copyrightName, ci.status,
  			ci.in_urgent as inUrgent, a.name as principal
  	from copyright_info ci 
  	left join admin a on a.id = ci.principal
  	where ci.contract_id = #{contractId,jdbcType=VARCHAR}
  </select>
  
   <insert id="batchInsert" parameterType="com.goafanti.common.model.CopyrightInfo">
    insert into copyright_info (id, uid, serial_number, 
      create_time, accept_time, principal, 
      contact, copyright_info, copyright_name, 
      status, work_issue, comment, 
      outsource, in_urgent, authorized_date, 
      certificate_url, copyright_number, application_url, delete_sign, founder, contract_id
      )
    values 
    <foreach item="item" index="index" collection="list" separator=",">
     (
      #{item.id,jdbcType=VARCHAR}, #{item.uid,jdbcType=VARCHAR}, #{item.serialNumber,jdbcType=INTEGER}, 
      #{item.createTime,jdbcType=TIMESTAMP}, #{item.acceptTime,jdbcType=TIMESTAMP}, #{item.principal,jdbcType=VARCHAR}, 
      #{item.contact,jdbcType=INTEGER}, #{item.copyrightInfo,jdbcType=VARCHAR}, #{item.copyrightName,jdbcType=VARCHAR}, 
      #{item.status,jdbcType=INTEGER}, #{item.workIssue,jdbcType=VARCHAR}, #{item.comment,jdbcType=VARCHAR}, 
      #{item.outsource,jdbcType=VARCHAR}, #{item.inUrgent,jdbcType=INTEGER}, #{item.authorizedDate,jdbcType=TIMESTAMP}, 
      #{item.certificateUrl,jdbcType=VARCHAR}, 
      #{item.copyrightNumber,jdbcType=VARCHAR}, 
      #{item.applicationUrl,jdbcType=VARCHAR}, #{item.deleteSign,jdbcType=INTEGER}, #{item.founder,jdbcType=VARCHAR},
      #{item.contractId,jdbcType=VARCHAR}
      )
    </foreach>
  </insert>
  
   <update id="batchDeleteByContractId" parameterType="java.util.List">
  	update copyright_info set delete_sign = 1
  	where  contract_id in 
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
		#{item}
	</foreach>
  </update>
  
  <select id="findCopyrightInfoNumByUid" parameterType="java.lang.String" resultType="java.lang.Integer">
  	select 
  		count(1)
  	from copyright_info
  	where
  		delete_sign = 0
  	and uid = #{uid,jdbcType=VARCHAR}
  </select>
  
   <select id="countCopyrightUnDone" parameterType="java.lang.String" resultType="java.lang.Integer">
   select count(1) from copyright_info 
   where uid = #{uid,jdbcType=VARCHAR}
   and delete_sign = 0 and status not in (8,9) 
  </select>
</mapper>