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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmJdxbgEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="XM_ID" property="xmId"/>
        <result column="type" property="type"/>
        <result column="attachment" property="attachment"/>
        <result column="fill_date" property="fillDate"/>
        <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.XmJdxbgVO" extends="ResultMap">

     </resultMap>

    <delete id="deleteByIds">
        DELETE FROM kd_xm_jdxbg
        WHERE id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <select id="selectByCondition" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.entity.XmJdxbgEntity">
        SELECT
            kxj.id, kxj.XM_ID,kxj.type,kxj.fill_date, kxj.attachment,
            xm.xmbh as 'xmEntity.xmbh',
            xm.xmmc AS 'xmEntity.xmmc',
            xm.xmfzr AS 'xmEntity.xmfzr',
            (SELECT name from kd_technician t where t.id = xm.xmfzr ) 'xmEntity.xmfzrxm',
            xm.xmsqdwid AS 'xmEntity.xmsqdwid',
            xm.xmkssj as 'xmEntity.xmkssj',
            xm.xmjssj as 'xmEntity.xmjssj',
            xm.yjlx AS 'xmEntity.yjlx',
            xm.xmly AS 'xmEntity.xmly',
            xm.xmkzxs AS 'xmEntity.xmkzxs',
            xm.yqcgxs AS 'xmEntity.yqcgxs',
            xm.xmjsjjmb AS 'xmEntity.xmjsjjmb'
        FROM kd_xm_jdxbg kxj
        left join
        (
            SELECT
                id, xmbh, xmkssj,yjlx,xmly,xmkzxs,yqcgxs,xmjsjjmb,
                COALESCE(get_xm_info(m.id, 'XMMC', null), m.XMMC) as xmmc,
                COALESCE(get_xm_info(m.id, 'XMJSSJ', null),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 kxj.xm_id = xm.id
        WHERE kxj.tenant_id = #{ew.tenantId}
        <if test="ew.xmId!=null">and kxj.XM_ID = #{ew.xmId}</if>
        order by kxj.fill_date asc
    </select>


    <select id="selectByPrimaryKey" resultType="org.sky.scientific.pojo.vo.XmJdxbgVO">
        SELECT
            kxj.id, kxj.XM_ID,kxj.type,kxj.fill_date, kxj.attachment,
            xm.xmbh as 'xmEntity.xmbh',
            COALESCE(get_xm_info(kxj.XM_ID, 'XMMC', date_format(kxj.fill_date, '%Y%m%d')), xm.XMMC ) AS 'xmEntity.xmmc',
            COALESCE(get_xm_info(kxj.XM_ID, 'XMFZR', date_format(kxj.fill_date, '%Y%m%d')),xm.XMFZR) AS 'xmEntity.xmfzr',
            COALESCE(get_xm_info(kxj.XM_ID, 'XMSQDWID', date_format(kxj.fill_date, '%Y%m%d')),xm.XMSQDWID) AS 'xmEntity.xmsqdwid',
            xm.XMKSSJ as 'xmEntity.xmkssj',
            COALESCE(get_xm_info(kxj.XM_ID, 'XMJSSJ', date_format(kxj.fill_date, '%Y%m%d')),XMJSSJ) AS 'xmEntity.xmjssj',
            xm.YJLX as 'xmEntity.yjlx',
            xm.XMLY as 'xmEntity.xmly',
            xm.XMKZXS as 'xmEntity.xmkzxs',
            xm.YQCGXS as 'xmEntity.yqcgxs',
            xm.XMJSJJMB as 'xmEntity.xmjsjjmb'
        FROM kd_xm_jdxbg kxj
        left join kd_xm xm on kxj.xm_id = xm.id
        WHERE kxj.id = #{id}
    </select>

</mapper>
