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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.CgJsbzEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="ZBH" property="zbh"/>
        <result column="ZBMC" property="zbmc"/>
        <result column="BZLB" property="bzlb"/>
        <result column="BBBM" property="bbbm"/>
        <result column="QCDW" property="qcdw"/>
        <result column="SSRQ" property="ssrq"/>
        <result column="state" property="state"/>
        <result column="CYR" property="cyr"/>
        <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.CgJsbzEntity">
        SELECT
        z.id,z.tenant_id,z.ZBH,z.ZBMC,z.BZLB,z.BBBM,z.QCDW,z.CYR,z.SSRQ,z.state,z.CY_ZC,z.XMBH,z.XM_ID,z.attachment,
        xm.XMMC
        FROM kd_cg_jsbz z
        left join
        (
            select
                m.id,
                COALESCE(get_xm_info(m.id,'XMMC', null), XMMC) as XMMC
            from kd_xm m
        ) xm on xm.id = z.xm_id
        where z.tenant_id = #{ew.tenantId}
        <if test="ew.zbh!=null and ew.zbh!=''">and z.ZBH like concat('%',#{ew.zbh},'%')</if>
        <if test="ew.zbmc!=null and ew.zbmc!=''">and z.ZBMC like concat('%',#{ew.zbmc},'%')</if>
        <if test="ew.bzlb!=null and ew.bzlb!=''">and Z.BZLB = #{ew.bzlb}</if>
        <if test="ew.qcdw!=null and ew.qcdw!=''">and z.qcdw like concat('%',#{ew.qcdw},'%')</if>
        <if test="ew.cyr != null and ew.cyr!=''">and z.cyr like concat('%',#{ew.cyr},'%')</if>
        <if test="ew.state != null and ew.state!=''">and z.state like concat('%',#{ew.state},'%')</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 z.XMBH like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.xmIdList!=null and ew.xmIdList.size()>0">and z.xm_id in <foreach item="xmId" collection="ew.xmIdList" separator="," open="(" close=")">#{xmId}</foreach></if>
    </select>

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