<?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.cslg.report.mapper.ReportFieldMapper">
    <sql id="Patent_Query_Source_Base_Select">
        select ifnull(${label}, '未选择') as label, count(${count}) as `count`, ifnull(${value}, 'null') as `value`,
        group_concat(distinct a.id) as ids
        from os_patent a
        <if test="params.folder != null and params.folder != ''">
            left join os_portfolio_link b on b.pid = a.id
        </if>
        <if test="params.folder == null or params.folder == ''">
            left join os_thematic_pid b on b.pid = a.id
        </if>
    </sql>
    <sql id="Patent_Query_Source_Base_Where">
        <where>
            <if test="params.projectId != null">
                and b.zid = #{params.projectId}
            </if>
            <if test="params.folder != '' and params.folder != null">
                and b.fid = #{params.folder}
            </if>
            <if test="params.label != '' and params.label != null">
                and ${label} like concat('%', #{params.label}, '%')
            </if>
            <if test="ids != null and ids.size() != 0">
                and b.pid in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </sql>
    <sql id="Patent_Query_Source_Base_Group">
        group by
        ${groupBy}
        order
        by
        `${orderBy}`
        desc,
        `value`
        asc
    </sql>
    <select id="find" resultType="cn.cslg.report.entity.ReportField">
     select * from OS_PATENT_FIELD where REPORT_ID=#{reportId} and ID=#{id}
    </select>
    <delete id="deleteReportId">
        delete from OS_PATENT_FIELD where REPORT_ID=#{reportId} and ID=#{id}
    </delete>
    <update id="updateReportId" parameterType="cn.cslg.report.entity.ReportField">
      update OS_PATENT_FIELD set`NAME`=#{name},PTYPE=#{ptype},`TYPE`=#{type},STATUS=#{status},REMARK=#{remark},
                                CID=#{createBy},CREATE_TIME=#{createTime},REPORT_TYPE=#{reportType} where REPORT_ID=#{reportId} and ID=#{id}

    </update>
    <select id="getPatentQuerySourcePageList" parameterType="cn.cslg.report.common.model.vo.PatentQueryFieldSourceVO"
            resultType="cn.cslg.report.common.model.dto.PatentQueryFieldSourceDTO">

        <if test="params.type != 'query'">
            select temp.* from ((
            <include refid="Patent_Query_Source_Base_Select">
                <property name="label" value="null"/>
                <property name="count" value="a.id"/>
                <property name="value" value="null"/>
            </include>
            <include refid="Patent_Query_Source_Base_Where">
                <property name="label" value="null"/>
            </include>
            and a.id not in (select pid from os_patent_field_patent_link where cid = #{params.customFieldId})
            ) union all (
            <include refid="Patent_Query_Source_Base_Select">
                <property name="label" value="d.name"/>
                <property name="count" value="distinct e.pid"/>
                <property name="value" value="d.id"/>
            </include>
            left join os_patent_field_patent_link e on e.pid = b.pid
            left join os_patent_field c1 on c1.id = e.cid
            <if test="params.type == 6">
                left join os_patent_field_tree d on d.cid = e.cid
            </if>
            <if test="params.type == 0 or params.type == 1 or params.type == 2">
                left join os_patent_field_text d on d.cid = e.cid and d.name is not null and d.name != ''
            </if>
            <if test="params.type == 3 or params.type == 4 or params.type == 5">
                left join os_patent_field_multiple d on d.cid = e.cid
            </if>
            <include refid="Patent_Query_Source_Base_Where">
                <property name="label" value="d.name"/>
            </include>
            and c1.id = #{params.customFieldId}
            <if test="params.customFieldTreeType == 0">
                and e.fid in (select id from os_patent_field_tree where cid = c1.id and find_in_set(d.id , replace(path
                , '/', ',')))
            </if>
            <if test="params.customFieldTreeType == null">
                and e.fid = d.id
            </if>
            group by d.id
            )) as temp where 1 = 1 order by `count` desc, `value` asc
        </if>
    </select>

    <select id="getFieldPatentNos" parameterType="java.util.List" resultType="java.lang.String">

        select distinct c.PATENT_NO from COMPARE_FILES c <where>
        <foreach collection="params" item="item" separator=" or ">
            c.PATENT_NO in (
            select distinct a.PATENT_NO from COMPARE_FILES a left join  OS_PATENT_FIELD_PATENT_LINK b on a.PATENT_NO =b.PATENT_NO
            and b.CID=#{item.field}
            <where>
                a.REPORT_ID=#{reportId}
                and (
                b.FID in
                <foreach collection="item.optionList" item="item1" separator="," open="(" close=")">
                    #{item1.key}
                </foreach>
                <if test="item.haveNull == 1">
                    or b.CID is null
                </if>
                )
            </where>
            )
        </foreach>
    </where>
    </select>

    <select id="getFieldTaskPNos" parameterType="java.util.List" resultType="java.lang.String">
        select distinct c.PATENT_NO from COMPARE_FILES c <where>
        <foreach collection="params" item="item" separator=" and ">
            c.PATENT_NO in (
            select  distinct a.PATENT_NO from ASSO_TASK_PATENT a left join  OS_PATENT_FIELD_PATENT_LINK b on a.PATENT_NO =b.PATENT_NO
            and a.REPORT_ID=b.REPORT_ID and b.CID=#{item.field}
            <where>
                a.TASK_ID=#{taskId}
                <if test="readState!=3">
                    and  a.STATE =#{readState}
                </if>
                and (
                b.FID in
                <foreach collection="item.optionList" item="item1" separator="," open="(" close=")">
                #{item1.key}
                </foreach>
                <if test="item.haveNull == 1">
                or b.CID is null
                </if>
                )
            </where>
            )
        </foreach>
    </where>
    </select>

</mapper>