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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmBgrzEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="XM_ID" property="xmId"/>
        <result column="ZDMC" property="zdmc"/>
        <result column="BGQDZ" property="bgqdz"/>
        <result column="BGHDZ" property="bghdz"/>
        <result column="BGSXSJ" property="bgsxsj"/>
        <result column="BGYY" property="bgyy"/>
        <result column="attachment" property="attachment"/>
        <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>

    <resultMap id="ResultVoMap" type="org.sky.scientific.pojo.vo.XmBgrzVO" extends="ResultMap">
        <result column="xmbh" property="xmbh"/>
        <result column="xmmc" property="xmmc"/>
        <result column="xmsqdwid" property="xmsqdwid"/>
        <result column="xmsqdw" property="xmsqdw"/>
        <result column="xmfzrxm" property="xmfzrxm"/>
        <result column="attachment" property="attachment"/>
    </resultMap>

    <delete id="deleteBgrzByJtb">
        delete from kd_xm_bgrz where xm_id = #{xmId} and ZDMC = 'STATUS' and BGHDZ = 2
    </delete>

<!--    <select id="selectBgrzPage" resultMap="ResultVoMap">-->
<!--        SELECT-->
<!--            XM_ID,-->
<!--            xmbh,-->
<!--            xmmc,-->
<!--            xmsqdwid,-->
<!--            CONCAT_WS('/', ( select name from kd_department d2 where d2.id = (SELECT parent_id from kd_department where id = xm.xmsqdwid )), (SELECT name from kd_department where id = xm.xmsqdwid )) as xmsqdw ,-->
<!--        xmfzr,-->
<!--            (SELECT name from kd_technician t where t.id = xm.xmfzr ) xmfzrxm,-->
<!--            rz.ZDMC,BGQDZ,BGHDZ,BGSXSJ,BGYY,rz.attachment-->
<!--        FROM kd_xm_bgrz rz-->
<!--        left join-->
<!--        (-->
<!--            SELECT-->
<!--                id, xmbh, xmkssj,-->
<!--                COALESCE(get_xm_info(m.id, 'XMMC', null),m.XMMC) as xmmc,-->
<!--                COALESCE(get_xm_info(m.id, 'XMJSSJ', null),m.XMJSSJ) AS XMJSSJ,-->
<!--                COALESCE(get_xm_info(m.id, 'XMFZR', null),m.XMFZR) as xmfzr,-->
<!--                COALESCE(get_xm_info(m.id, 'XMSQDWID', null),m.XMSQDWID) as xmsqdwid-->
<!--            FROM kd_xm m-->
<!--        )xm on rz.xm_id = xm.id-->
<!--         <where>-->
<!--            <if test="ew.xmId!=null">and rz.XM_ID = #{ew.xmId}</if>-->
<!--        </where>-->
<!--    </select>-->

    <select id="selectBgrzPage" resultMap="ResultVoMap">
        SELECT
            XM_ID,
            xmbh,
            xmmc,
            xmsqdwid,
            CONCAT_WS('/', ( select name from kd_department d2 where d2.id = (SELECT parent_id from kd_department where id = xm.xmsqdwid )), (SELECT name from kd_department where id = xm.xmsqdwid )) as xmsqdw ,
            xmfzr,
            t.name as xmfzrxm,
            rz.ZDMC,BGQDZ,BGHDZ,BGSXSJ,BGYY,rz.attachment
        FROM kd_xm_bgrz rz
        left join
        (
            SELECT
                id, xmbh, xmkssj,
                COALESCE(get_xm_info(m.id, 'XMMC', null),m.XMMC) as xmmc,
                COALESCE(get_xm_info(m.id, 'XMJSSJ', null),m.XMJSSJ) AS XMJSSJ,
                COALESCE(get_xm_info(m.id, 'XMFZR', null),m.XMFZR) as xmfzr,
                COALESCE(get_xm_info(m.id, 'XMSQDWID', null),m.XMSQDWID) as xmsqdwid
            FROM kd_xm m
        )xm on rz.xm_id = xm.id
        left join kd_technician t on t.id = xm.xmfzr
        <where>
            <if test="ew.xmId!=null">and rz.XM_ID = #{ew.xmId}</if>
        </where>
    </select>
</mapper>
