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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.YsWxzctxfyEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="XM_ID" property="xmId"/>
        <result column="asset_id" property="assetId"/>
        <result column="ZCBM" property="zcbm"/>
        <result column="ZZSC" property="zzsc"/>
        <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.YsWxzctxfyVO" extends="ResultMap">
        <association property="zcEntity" column="asset_id" select="org.sky.scientific.mapper.AssetMapper.selectById"/>
    </resultMap>

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


    <select id="selectYsWxzctxfyPage" resultMap="ResultVoMap">
        select * from kd_ys_wxzctxfy
        where is_deleted = 0
        <if test="ysWxzctxfy.xmId!=null">and XM_ID = #{ysWxzctxfy.xmId}</if>
    </select>

    <select id="selectByYearAndMonth" resultMap="ResultVoMap">
        SELECT xa.*, s.YZJE,s.zcbm,s.status as 'zcEntity.status'
        FROM kd_ys_wxzctxfy xa
        left join kd_asset s on s.id = XA.asset_id
        left join
        (
            SELECT
                xm.id,
                xm.XMKSSJ,
                COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{yearAndMonth}),XMJSSJ) AS XMJSSJ
            FROM kd_xm xm
         ) xm on xm.id = xa.xm_id
        WHERE #{yearAndMonth} between DATE_FORMAT(xm.XMKSSJ, '%Y%m') and DATE_FORMAT(xm.XMJSSJ,'%Y%m')
    </select>

    <select id="countByZcbmAfterMonth" resultType="java.lang.Long">
        select count(*)
        from kd_ys_wxzctxfy h
        left join kd_asset a on h.asset_id = a.id
        where a.zcbm = #{zcbm} and a.year_and_month <![CDATA[>=]]> #{yearAndMonth}
    </select>

</mapper>
