<?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.CgRjzzqMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.CgRjzzqEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="RJMC" property="rjmc"/>
        <result column="RJJC" property="rjjc"/>
        <result column="BBH" property="bbh"/>
        <result column="DJH" property="djh"/>
        <result column="KFWCRQ" property="kfwcrq"/>
        <result column="SCFBRQ" property="scfbrq"/>
        <result column="DJPZRQ" property="djpzrq"/>
        <result column="QDFS" property="qdfs"/>
        <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.CgRjzzqEntity">
        SELECT
        r.id,r.tenant_id,r.RJMC,r.RJJC,r.BBH,r.DJH,r.KFWCRQ,r.SCFBRQ,r.DJPZRQ,r.QDFS,r.XMBH,r.XM_ID,r.attachment,
        xm.XMMC
        FROM kd_cg_rjzzq r
        left join
        (
            select
                m.id,
                COALESCE(get_xm_info(m.id,'XMMC',null), XMMC) as XMMC
            from kd_xm m
        ) xm on xm.id = r.xm_id
        where r.tenant_id = #{ew.tenantId}
        <if test="ew.rjmc !=null and ew.rjmc!=''">and r.RJMC like concat('%',#{ew.rjmc},'%')</if>
        <if test="ew.rjjc !=null and ew.rjjc!=''">and r.RJJC like concat('%',#{ew.rjjc},'%')</if>
        <if test="ew.bbh !=null and ew.bbh!=''">and r.BBH like concat('%',#{ew.bbh},'%')</if>
        <if test="ew.djh !=null and ew.djh!=''">and r.DJH like concat('%',#{ew.djh},'%')</if>
        <if test="ew.qdfs !=null and ew.qdfs!=''">and r.QDFS =#{ew.qdfs}</if>
        <if test="ew.xmmc !=null and ew.xmmc!=''">and r.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.xmbh !=null and ew.xmbh!=''">and r.XMBH like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.kfwcrqMin !=null and ew.kfwcrqMax!=''">and date_format(r.kfwcrq,'%Y-%m-%d') between #{ew.kfwcrqMin} and #{ew.kfwcrqMax}</if>
        <if test="ew.scfbrqMin !=null and ew.scfbrqMax!=''">and date_format(r.scfbrq,'%Y-%m-%d') between #{ew.scfbrqMin} and #{ew.scfbrqMax}</if>
        <if test="ew.djpzrqMin !=null and ew.djpzrqMax!=''">and date_format(r.djpzrq,'%Y-%m-%d') between #{ew.djpzrqMin} and #{ew.djpzrqMax}</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 r.XMBH like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.xmIdList!=null and ew.xmIdList.size()>0">and r.xm_id in <foreach item="xmId" collection="ew.xmIdList" separator="," open="(" close=")">#{xmId}</foreach></if>
    </select>

    <select id="countByYear" resultType="java.lang.Long">
        select ifnull(count(*),0) from kd_cg_rjzzq where DJPZRQ is not null and left(DJPZRQ,4) = #{year} and tenant_id = #{tenantId}
    </select>

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