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

    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmFixedAsset">
        <id column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="xm_id" property="xmId"/>
        <result column="xmmc" property="xmmc"/>
        <result column="xmbh" property="xmbh"/>
        <result column="zcbm" property="zcbm"/>
        <result column="yfftv" property="yanfaFenTanLv"/>
        <result column="ftvqdyj" property="fenTanLvQueDingYiJu"/>
        <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="selectXmFixedAssetByYearAndMonth"-->
<!--            resultMap="ResultMap">-->
<!--        SELECT xa.*,asset.zcbm, asset.status as 'asset.status'-->
<!--        FROM kd_xm_fixed_asset xa-->
<!--        left JOIN kd_asset asset ON xa.zcbm = asset.zcbm and asset.year_and_month = xa.year_and_month-->
<!--        LEFT JOIN-->
<!--        (-->
<!--            SELECT-->
<!--                xm.id,-->
<!--                xm.XMKSSJ,-->
<!--                COALESCE(get_xm_info(xm.id,'XMJSSJ',#{yearAndMonth}),XMJSSJ) AS XMJSSJ-->
<!--            FROM kd_xm xm-->
<!--            WHERE xm.is_deleted = 0 and DATE_FORMAT(XMKSSJ, '%Y%m') <![CDATA[<=]]> #{yearAndMonth}-->
<!--        ) xm on xm.id = xa.xm_id-->
<!--        WHERE xa.year_and_month = #{yearAndMonth} and #{yearAndMonth} between DATE_FORMAT(xm.XMKSSJ, '%Y%m') and DATE_FORMAT(xm.XMJSSJ,'%Y%m')-->
<!--        and asset.ZCLB = #{zclb}-->
<!--    </select>-->

    <select id="selectXmFixedAssetByYearAndMonth"
            resultMap="ResultMap">
        SELECT xa.*, asset.status as 'asset.status'
        FROM kd_xm_fixed_asset xa
        left JOIN kd_asset asset ON xa.zcbm = asset.zcbm and asset.year_and_month = xa.year_and_month
        LEFT JOIN
        (
            SELECT
                xm.id,
                xm.XMKSSJ,
                COALESCE(get_xm_info(xm.id,'XMJSSJ',#{yearAndMonth}),XMJSSJ) AS XMJSSJ
            FROM kd_xm xm
            WHERE xm.is_deleted = 0 and DATE_FORMAT(XMKSSJ, '%Y%m') <![CDATA[<=]]> #{yearAndMonth}
        ) xm on xm.id = xa.xm_id
        WHERE xa.year_and_month = #{yearAndMonth} and #{yearAndMonth} between DATE_FORMAT(xm.XMKSSJ, '%Y%m') and DATE_FORMAT(xm.XMJSSJ,'%Y%m')
          and xa.type=#{type}
    </select>

    <select id="selectXmFixedAssetPageByYearAndMonth" resultMap="ResultMap">
        SELECT xa.*,xm.xmmc, xm.xmbh,
        <include refid="org.sky.scientific.mapper.AssetMapper.basicColumns"/>
        FROM kd_xm_fixed_asset xa
        LEFT JOIN kd_asset asset on asset.zcbm = xa.zcbm and asset.year_and_month =  xa.year_and_month
        LEFT JOIN
        (
            SELECT
                xm.id,
                xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC',#{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
            WHERE xm.is_deleted = 0
        ) xm on xm.id = xa.xm_id
        WHERE xa.year_and_month = #{ew.yearAndMonth} and xa.type = #{type}
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.zcmc!=null and ew.zcmc!=''">and asset.zcmc like concat('%',#{ew.zcmc},'%')</if>
        <if test="ew.yt!=null and ew.yt!=''">and asset.yt= #{ew.yt}</if>
        <if test="ew.sysmMin!=null and ew.sysmMax!=null">and asset.sysm between #{ew.sysmMin} and #{ew.sysmMax}</if>
        <if test="ew.ysysmMin!=null and ew.ysysmMax!=null">and asset.ysysm between #{ew.ysysmMin} and #{ew.ysysmMax}</if>
        <if test="ew.zcyzMin!=null and ew.zcyzMax!=null">and asset.zcyz between #{ew.zcyzMin} and #{ew.zcyzMax}</if>
        <if test="ew.yzjeMin!=null and ew.yzjeMax!=null">and asset.yzje between #{ew.yzjeMin} and #{ew.yzjeMax}</if>
        <if test="ew.status!=null">and asset.status = #{ew.status}</if>
    </select>

    <select id="selectXmFixedAssetPageByYear" resultMap="ResultMap">
        SELECT xa.xm_id, xm.XMBH as xmbh, xm.XMMC as xmmc, asset.id as asset_id, xa.zcbm, <include refid="org.sky.scientific.mapper.AssetMapper.basicColumns"/>
        FROM kd_xm_fixed_asset xa
        left join kd_asset asset on asset.zcbm = xa.zcbm and asset.year_and_month = (SELECT max(year_and_month) from kd_asset t WHERE t.zcbm = zcbm and left(t.year_and_month, 4) = #{ew.yearAndMonth})
        LEFT JOIN
        (
            SELECT
                xm.id,
                xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC',#{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
        ) xm on xm.id = xa.xm_id
        WHERE left(xa.year_and_month,4 ) = #{ew.yearAndMonth} and xa.type = #{type}
        GROUP BY asset.ZCBM
    </select>

    <resultMap id="XmFixedAssetCostVo" type="org.sky.scientific.pojo.vo.XmFixedAssetCostVo" extends="ResultMap">
        <result column="work_hours" property="workHours"/>
        <result column="yanfa_fen_tan_lv" property="yanfaFenTanLv"/>
        <result column="yanfa_tan_xiao" property="yanfaTanXiao"/>
    </resultMap>

    <select id="keyanyiqishebeizhejiufeiyongfenpeibiao"
            resultMap="XmFixedAssetCostVo"
            parameterType="org.sky.scientific.pojo.dto.XmAssetQueryParam">
        SELECT xm.XMBH, xm.XMMC,
            v1.xm_id, v1.asset_id, v1.year_and_month,
            v1.ZCBM as 'asset.ZCBM' , v1.ZCMC as 'asset.ZCMC', v1.ZCLB as 'asset.ZCLB' , v1.YT as 'asset.YT',  v1.ZCYZ as 'asset.ZCYZ', v1.JCZ as 'asset.JCZ' , v1.YZJE as 'asset.YZJE', v1.zgs as 'asset.ZGS',
            v1.work_hours,
            v1.yanfa_fen_tan_lv, v1.yanfa_tan_xiao
        from yiqishebeiAssetCostView v1
        LEFT JOIN
        (
            SELECT
                xm.id,
                xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC',#{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
            WHERE xm.is_deleted = 0
        ) xm on xm.id = v1.xm_id
        WHERE v1.tenant_id = #{ew.tenantId} AND v1.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmId!=null">and v1.xm_id=#{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.zcmc!=null and ew.zcmc!=''">and zcmc like concat('%',#{ew.zcmc},'%')</if>
        <if test="ew.zclb!=null and ew.zclb!=''">and zclb like concat('%',#{ew.zclb},'%')</if>
        <if test="ew.yt!=null and ew.yt!=''">and yt like concat('%',#{ew.yt},'%')</if>
        <if test="ew.kssjMin!=null and ew.kssjMax!=''">and DATE_FORMAT(kssj,'%Y-%m-%d') between #{ew.kssjMin} and #{ew.jssjEnd}</if>
        <if test="ew.zcyzMin!=null and ew.zcyzMax!=null">and zcyz between #{ew.zcyzMin} and #{ew.zcyzMax}</if>
        <if test="ew.jczMin!=null and ew.jczMax!=null">and jcz between #{ew.jczMin} and #{ew.jczMax}</if>
        <if test="ew.yzjeMin!=null and ew.yzjeMax!=null">and yzje between #{ew.yzjeMin} and #{ew.yzjeMax}</if>
        <if test="ew.attendanceHoursMin!=null and ew.attendanceHoursMax!=null">and v1.zgs between #{ew.attendanceHoursMin} and #{ew.attendanceHoursMax}</if>
        <if test="ew.workHoursMin!=null and ew.workHoursMax!=null">and v1.work_hours between #{ew.workHoursMin} and #{ew.workHoursMax}</if>
    </select>

    <select id="keyanfangwujianzhuwuzhejiufeiyongfenpeibiao"
            resultMap="XmFixedAssetCostVo"
            parameterType="org.sky.scientific.pojo.dto.XmAssetQueryParam">
        SELECT
            xa.id,xa.xm_id, asset.id as asset_id, xm.XMBH, xm.XMMC,
            asset.ZCBM as 'asset.ZCBM' ,asset.ZCMC as 'asset.ZCMC' ,asset.ZCLB as 'asset.ZCLB',
            asset.YT as 'asset.YT', asset.ZCYZ as 'asset.ZCYZ',asset.JCZ as 'asset.JCZ',asset.YZJE as 'asset.YZJE', asset.KSSJ as 'asset.KSSJ',
            xa.yfftv as yanfa_fen_tan_lv,
            xa.ftvqdyj
        FROM kd_xm_fixed_asset xa
        LEFT JOIN kd_asset asset ON xa.zcbm = asset.zcbm and asset.year_and_month = xa.year_and_month and asset.tenant_id = #{ew.tenantId}
        LEFT JOIN
        (
            SELECT
                xm.id,
                xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC',#{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
        ) xm on xm.id = xa.xm_id
        WHERE xa.tenant_id = #{ew.tenantId} AND xa.year_and_month = #{ew.yearAndMonth} and xa.type = 2
        <if test="ew.xmId!=null">and xa.xm_id  = #{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.zcmc!=null and ew.zcmc!=''">and asset.zcmc like concat('%',#{ew.zcmc},'%')</if>
        <if test="ew.zclb!=null and ew.zclb!=''">and asset.zclb like concat('%',#{ew.zclb},'%')</if>
        <if test="ew.yt!=null and ew.yt!=''">and asset.yt like concat('%',#{ew.yt},'%')</if>
        <if test="ew.kssjMin!=null and ew.kssjMax!=''">and DATE_FORMAT(asset.kssj,'%Y-%m-%d') between #{ew.kssjMin} and #{ew.jssjEnd}</if>
        <if test="ew.zcyzMin!=null and ew.zcyzMax!=null">and asset.zcyz between #{ew.zcyzMin} and #{ew.zcyzMax}</if>
        <if test="ew.jczMin!=null and ew.jczMax!=null">and asset.jcz between #{ew.jczMin} and #{ew.jczMax}</if>
        <if test="ew.yzjeMin!=null and ew.yzjeMax!=null">and asset.yzje between #{ew.yzjeMin} and #{ew.yzjeMax}</if>
    </select>

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