<?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="org.sky.scientific.mapper.CgQtMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.CgQtEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="CGBH" property="cgbh"/>
        <result column="CGMC" property="cgmc"/>
        <result column="CMLX" property="cmlx"/>
        <result column="BBBM" property="bbbm"/>
        <result column="QLR" property="qlr"/>
        <result column="FMR" property="fmr"/>
        <result column="SHENQING_RQ" property="shenqingRq"/>
        <result column="SHOUQUAN_RQ" property="shouquanRq"/>
        <result column="CANYUN_ZHUCHI" property="canyunZhuchi"/>
        <result column="attachment" property="attachment"/>
        <result column="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>
        <result column="XM_ID" property="xmId"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>


    <select id="selectPageByCondition" resultMap="ResultMap" parameterType="org.sky.scientific.pojo.entity.CgQtEntity">
        SELECT
            q.id,q.tenant_id,q.CGBH,q.CGMC,q.CMLX,q.BBBM,q.QLR,q.FMR,q.SHENQING_RQ,q.SHOUQUAN_RQ,q.CANYUN_ZHUCHI,q.XMBH,q.XM_ID,q.attachment,
            xm.XMMC
        FROM kd_cg_qt q
        left join
        (
            select
                m.id,
                COALESCE(get_xm_info(m.id,'XMMC',null), XMMC) as XMMC
            from kd_xm m
        ) xm on xm.id = q.xm_id
        where q.tenant_id = #{ew.tenantId}
        <if test="ew.cgbh != null and ew.cgbh != ''">and q.CGBH like concat('%',#{ew.cgbh},'%')</if>
        <if test="ew.cgmc != null and ew.cgmc != ''">and q.CGMC like concat('%',#{ew.cgmc},'%')</if>
        <if test="ew.cmlx != null and ew.cmlx != ''">and q.CMLX =#{ew.cmlx}</if>
        <if test="ew.bbbm != null and ew.bbbm != ''">and q.BBBM like concat('%',#{ew.bbbm},'%')</if>
        <if test="ew.qlr != null and ew.qlr != ''">and q.QLR like concat('%',#{ew.qlr},'%')</if>
        <if test="ew.shenqingRqMix != null and ew.shenqingRqMax != ''">and date_format(q.SHENQING_RQ,'%Y-%m-%d') between #{ew.shenqingRqMix} and #{ew.shenqingRqMax}</if>
        <if test="ew.shouquanRqMix != null and ew.shouquanRqMax != ''">and date_format(q.SHOUQUAN_RQ,'%Y-%m-%d') between #{ew.shouquanRqMix} and #{ew.shouquanRqMax}</if>
        <if test="ew.xmmc != null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.xmbh != null and ew.xmbh!=''">and q.XMBH like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.xmIdList!=null and ew.xmIdList.size()>0">and q.xm_id in <foreach item="xmId" collection="ew.xmIdList" separator="," open="(" close=")">#{xmId}</foreach></if>
    </select>

    <select id="xmCgSummary" resultType="org.sky.scientific.pojo.vo.XmCGSummary">
        SELECT
        cg.*,xm.xmbh,xm.xmmc
        FROM
        (
            SELECT tenant_id,ZBMC as cgmc,ZBH as cgbh,SSRQ as date1,SSRQ as date2, xm_id, '技术标准' as cglx,attachment FROM kd_cg_jsbz where xm_id is not null
            UNION
            SELECT tenant_id,LWMC as cgmc,KJH as cgbh,FBRQ as date1,FBRQ as date2, xm_id,'科研论文' as cglx,attachment FROM kd_cg_kylw where xm_id is not null
            UNION
            SELECT tenant_id,CGMC as cgmc,CGBH as cgbh,SHENQING_RQ as date1,SHOUQUAN_RQ as date2, xm_id,'其他成果' as cglx,attachment FROM kd_cg_qt where xm_id is not null
            UNION
            SELECT tenant_id,RJMC as cgmc,DJH as cgbh,SCFBRQ as date1,DJPZRQ as date2, xm_id,'软件著作' as cglx,attachment FROM kd_cg_rjzzq where xm_id is not null
            UNION
            SELECT tenant_id,ZLMC as cgmc,ZLH as cgbh,SHENQING_RQ as date1,SHOUQUAN_RQ as date2, xm_id,ZLLX as cglx,attachment FROM kd_cg_zl where xm_id is not null
        ) cg
        LEFT JOIN
        (
            SELECT
                xm.id as xm_id,xmbh,
                COALESCE(get_xm_info(xm.id,'XMMC',null),XMMC) AS xmmc
            FROM kd_xm xm
        )xm on cg.xm_id = xm.xm_id
        WHERE cg.tenant_id=#{tenantId}
        <if test="xmId != null and xmId!=''">and cg.xm_id = #{xmId}</if>
    </select>

    <delete id="physicalDeleteByIds">
        DELETE FROM kd_cg_qt WHERE id IN
        <foreach item="id" collection="list" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
