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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.AssetEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="ZCBM" property="zcbm"/>
        <result column="ZCMC" property="zcmc"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="ZCLB" property="zclb"/>
        <result column="GL" property="gl"/>
        <result column="YT" property="yt"/>
        <result column="KSSJ" property="kssj"/>
        <result column="SYSM" property="sysm"/>
        <result column="YSYSM" property="ysysm"/>
        <result column="ZCYZ" property="zcyz"/>
        <result column="JCZ" property="jcz"/>
        <result column="YZJE" property="yzje"/>
        <result column="ZGS" property="zgs"/>
        <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.AssetVO" extends="ResultMap">

    </resultMap>

    <sql id="basicColumns">
        asset.id as 'asset.id',
        asset.ZCBM as 'asset.ZCBM' ,asset.ZCMC as 'asset.ZCMC' ,asset.ZCLB as 'asset.ZCLB',
        asset.GL as 'asset.GL',asset.YT as 'asset.YT',asset.KSSJ as 'asset.KSSJ',asset.SYSM as 'asset.SYSM',
        asset.YSYSM as 'asset.YSYSM',asset.ZCYZ as 'asset.ZCYZ',asset.JCZ as 'asset.JCZ',asset.YZJE as 'asset.YZJE',asset.status AS 'asset.status'
    </sql>

    <update id="updateStatus">
        update kd_asset
        set status = #{status}
        where id = #{assetId}
    </update>

    <select id="selectAssetPage" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.AssetDTO">
        SELECT
            t.*
        FROM kd_asset t
        INNER JOIN (
            SELECT ZCBM, MAX(year_and_month) as max_month FROM kd_asset
            WHERE is_deleted = 0
            <if test="ew.yearAndMonth.length()==4">AND left(year_and_month, 4) = #{ew.yearAndMonth}</if>
            <if test="ew.yearAndMonth.length()==6">AND year_and_month = #{ew.yearAndMonth}</if>
            GROUP BY ZCBM
        ) tm ON t.ZCBM = tm.ZCBM AND t.year_and_month = tm.max_month
        <where>
            <if test="ew.zcbm != null and ew.zcbm != ''">and t.zcbm like concat('%',#{ew.zcbm},'%')</if>
            <if test="ew.zcmc != null and ew.zcmc != ''">and t.zcmc like concat('%',#{ew.zcmc},'%')</if>
            <if test="ew.zclbList!=null">
                and t.zclb in
                <foreach collection="ew.zclbList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="ew.yt != null and ew.yt != ''">and t.yt = #{ew.yt}</if>
            <if test="ew.kssjMin != null and ew.kssjMax != null">and DATE_FORMAT(t.kssj,'%Y-%m-%d') between #{ew.kssjMin} and #{ew.kssjMax}</if>
            <if test="ew.sysmMin != null and ew.sysmMax!=null">and t.SYSM between #{ew.sysmMin} and #{ew.sysmMax}</if>
            <if test="ew.status != null">and t.status = #{ew.status}</if>
        </where>
        ORDER BY t.ZCBM, t.year_and_month DESC
    </select>

    <select id="selectLastOneByZcbm" resultMap="ResultVoMap">
        select * from kd_asset z
        where z.ZCBM = #{zcbm} and z.year_and_month = (select max(year_and_month) from kd_asset where ZCBM = #{zcbm})
    </select>

    <select id="selectAssetList" resultMap="ResultVoMap">
        SELECT t.*
        FROM kd_asset t
        INNER JOIN (
            SELECT ZCBM,MAX(year_and_month) as max_month
            FROM kd_asset
            where status !=2 and status!=4
            GROUP BY ZCBM
        ) tm ON t.ZCBM = tm.ZCBM AND t.year_and_month = tm.max_month
        ORDER BY t.ZCBM, t.year_and_month DESC
    </select>

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

    <delete id="deleteAssesByZcbmAfterMonth">
        DELETE FROM kd_asset
        WHERE zcbm = #{zcbm} and year_and_month <![CDATA[>=]]> #{yearAndMonth}
    </delete>

    <select id="selectByYearAndMonthAndBmOrMc" resultMap="ResultVoMap">
        SELECT * FROM kd_asset
        WHERE year_and_month = #{yearAndMonth}
        <if test="zcbm != null and zcbm != ''">
            and ZCBM = #{zcbm}
        </if>
        <if test="zcmc != null and zcmc != ''">
            and ZCMC = #{zcmc}
        </if>
    </select>

    <select id="zjCqdtWxzctxFyHzbByYearAndMonth" resultType="org.sky.scientific.pojo.vo.XmAssetCostSummary">
        SELECT DISTINCT(xmIds.xm_id) as xm_id,xm.XMBH,xm.XMMC,
        (SELECT IFNULL(sum(v1.yanfa_tan_xiao),0) FROM yiqishebeiassetcostview v1 WHERE v1.xm_id = xmIds.xm_id and v1.year_and_month = #{ew.yearAndMonth} ) as yqsbzjfy,
        (SELECT IFNULL(sum(v2.yanfa_fen_tan),0) FROM fangwujianzhuwuAssetCostView v2 WHERE v2.xm_id = xmIds.xm_id and v2.year_and_month =#{ew.yearAndMonth} ) as fwzjfy,
        (SELECT IFNULL(sum(v3.ytxe*v3.syzb),0) FROM kd_xm_cqdtfy v3 WHERE v3.xm_id = xmIds.xm_id and v3.year_and_month = #{ew.yearAndMonth}) as cqdtfy,
        (SELECT IFNULL(sum(v4.yanfaTanXiao),0) FROM wuxingAssetCostView v4 WHERE v4.xm_id = xmIds.xm_id and v4.year_and_month = #{ew.yearAndMonth}) as wxzcdtfy
        FROM
        (
            SELECT xm_id FROM kd_xm_cqdtfy WHERE year_and_month = #{ew.yearAndMonth} and tenant_id = #{ew.tenantId}
            UNION
            SELECT xm_id FROM kd_xm_fixed_asset WHERE year_and_month = #{ew.yearAndMonth} and tenant_id = #{ew.tenantId}
            UNION
            SELECT xm_id FROM kd_xm_intangible_asset WHERE year_and_month = #{ew.yearAndMonth} and tenant_id = #{ew.tenantId}
        )as xmIds
        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 = xmIds.xm_id
        <where>
            <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        </where>
        GROUP BY xmIds.xm_id
    </select>

    <select id="zjCqdtWxzctxFyHzbByYear" resultType="org.sky.scientific.pojo.vo.XmAssetCostSummary">
        SELECT DISTINCT(xmIds.xm_id) as xm_id,xm.XMBH,xm.XMMC,
        (SELECT IFNULL(sum(v1.yanfa_tan_xiao),0) FROM yiqishebeiassetcostview v1 WHERE v1.xm_id = xmIds.xm_id and LEFT(v1.year_and_month,4) = #{ew.yearAndMonth} ) as yqsbzjfy,
        (SELECT IFNULL(sum(v2.yanfa_fen_tan),0) FROM fangwujianzhuwuAssetCostView v2 WHERE v2.xm_id = xmIds.xm_id and LEFT(v2.year_and_month,4) =#{ew.yearAndMonth} ) as fwzjfy,
        (SELECT IFNULL(sum(v3.ytxe*v3.syzb),0) FROM kd_xm_cqdtfy v3 WHERE v3.xm_id = xmIds.xm_id and LEFT(v3.year_and_month,4) = #{ew.yearAndMonth}) as cqdtfy,
        (SELECT IFNULL(sum(v4.yanfaTanXiao),0) FROM wuxingAssetCostView v4 WHERE v4.xm_id = xmIds.xm_id and LEFT(v4.year_and_month,4) = #{ew.yearAndMonth}) as wxzcdtfy
        FROM
        (
        SELECT xm_id FROM kd_xm_cqdtfy WHERE LEFT(year_and_month,4) = #{ew.yearAndMonth} and tenant_id = #{ew.tenantId}
        UNION
        SELECT xm_id FROM kd_xm_fixed_asset WHERE LEFT(year_and_month,4) = #{ew.yearAndMonth} and tenant_id = #{ew.tenantId}
        UNION
        SELECT xm_id FROM kd_xm_intangible_asset WHERE LEFT(year_and_month,4) = #{ew.yearAndMonth} and tenant_id = #{ew.tenantId}
        )as xmIds
        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 = xmIds.xm_id
        <where>
            <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        </where>
        GROUP BY xmIds.xm_id
    </select>

    <select id="costSummary" resultType="org.sky.scientific.pojo.vo.CostSummaryVO" parameterType="org.sky.scientific.pojo.dto.CostSummaryDTO">
        SELECT
            m.month AS yearAndMonth,
            (select ROUND(sum(averageMonthlySalary)+sum(totalInsurance)
                          +sum(providentFund)+sum(bonus)+sum(benefit)+sum(pensionInsuranceBc)+sum(medicalInsuranceBc), 2) from ryrgfyhz_view where tenant_id = #{tenantId} and year_and_month = m.month ) as ryrgfy,
            (SELECT ROUND(IFNULL(sum(amount),0),2)
            from
            (
                SELECT xm_id, amount ,year_and_month from kd_xm_goods where tenant_id = #{tenantId}
                union all
                SELECT xm_id, amount,year_and_month from kd_xm_lease where tenant_id = #{tenantId}
                union all
                select xm_id, amount ,year_and_month from kd_xm_direct_cost where tenant_id = #{tenantId}
            )cost where cost.year_and_month = m.month) as zjtrfy,
            (ROUND((SELECT IFNULL(sum(yanfa_tan_xiao),0) FROM yiqishebeiassetcostview WHERE  tenant_id = #{tenantId} and year_and_month = m.month ) + (SELECT IFNULL(sum(yanfa_fen_tan),0) FROM fangwujianzhuwuAssetCostView WHERE  tenant_id = #{tenantId} and year_and_month = m.month ) + (SELECT IFNULL(sum(ytxe*syzb),0) FROM kd_xm_cqdtfy WHERE  tenant_id = #{tenantId} and year_and_month = m.month),2)) as zjfyycqdtfy,
            (SELECT ROUND(IFNULL(sum(amount),0),2) FROM kd_xm_design_cost WHERE tenant_id = #{tenantId} and cost_type='设计费用' and year_and_month = m.month) as sjfy,
            (SELECT ROUND(IFNULL(sum(yanfaTanXiao),0),2) FROM wuxingAssetCostView WHERE  tenant_id = #{tenantId} and year_and_month = m.month) as wxzctxfy,
            (SELECT ROUND(IFNULL(sum(amount),0),2) FROM kd_xm_design_cost WHERE tenant_id = #{tenantId} and cost_type in ('装备调试费用','试验费用') and year_and_month = m.month) as zbtsfyysyfy,
            (SELECT ROUND(IFNULL(sum(amount),0),2)  FROM kd_xm_other_cost WHERE tenant_id = #{tenantId} and year_and_month = m.month) as qtfy,
            (SELECT ROUND(IFNULL(sum(pay_amount),0),2)  FROM kd_xm_wwxm_cost WHERE tenant_id = #{tenantId} and year_and_month = m.month) as wwfy
        FROM
            (SELECT
                 -- 动态计算：从传入的年月字符串的第一天开始，加上序列月数
                 DATE_FORMAT(DATE_ADD(STR_TO_DATE(CONCAT(#{startYearMonth}, '01'), '%Y%m%d'), INTERVAL seq MONTH), '%Y%m') AS month
             FROM
                 (SELECT @row := @row + 1 AS seq
                  FROM mysql.help_topic, (SELECT @row := -1) r
                  LIMIT #{monthCount} -- 动态限制生成的月份数量
                 ) AS numbers
            ) AS m
        GROUP BY m.month
    </select>

    <select id="checkAssetUsage" resultType="java.lang.Long">
        SELECT
            asset_id
        FROM
        (
            SELECT asset_id FROM kd_ys_zjfyycqdtfy
            UNION
            SELECT asset.id as asset_id FROM kd_ys_wxzctxfy y left join kd_asset asset on asset.zcbm = y.zcbm and asset.year_and_month = #{yearAndMonth} where y.tenant_id = #{tenantId}
            UNION
            SELECT asset.id as asset_id FROM kd_xm_fixed_asset f left join kd_asset asset on asset.zcbm = f.zcbm and asset.year_and_month = #{yearAndMonth} where f.tenant_id = #{tenantId}
            UNION
            SELECT asset.id as asset_id FROM kd_xm_intangible_asset i left join kd_asset asset on asset.zcbm = i.zcbm and asset.year_and_month = #{yearAndMonth} where i.tenant_id = #{tenantId}
        ) tmp
        WHERE tmp.asset_id 	in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>
