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

    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmIntangibleAsset">
        <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="xmbh" property="xmbh"/>
        <result column="xmmc" property="xmmc"/>
        <result column="zcbm" property="zcbm"/>
        <result column="user_unicode" property="userUnicode"/>
        <result column="user_name" property="userName"/>
        <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="selectListByYearAndMonth"
            resultMap="ResultMap">
        SELECT xa.*,asset.zcbm,t.name as user_name,asset.status as 'asset.status'
        FROM kd_xm_intangible_asset xa
        LEFT JOIN kd_asset asset ON xa.ZCBM=asset.ZCBM and asset.year_and_month = #{yearAndMonth}
        LEFT JOIN kd_technician t on t.unicode=xa.user_unicode and t.year_and_month = #{yearAndMonth}
        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')
     </select>

    <select id="selectPageByYearAndMonth" resultMap="ResultMap">
        SELECT xa.*,xm.xmmc, xm.xmbh,t.name as user_name,
        <include refid="org.sky.scientific.mapper.AssetMapper.basicColumns"/>
        FROM kd_xm_intangible_asset xa
        LEFT JOIN kd_asset asset ON xa.zcbm = asset.ZCBM and asset.year_and_month = xa.year_and_month
        LEFT JOIN kd_technician t on t.unicode=xa.user_unicode and t.year_and_month = #{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
            WHERE xm.is_deleted = 0
        ) xm on xm.id = xa.xm_id
        WHERE xa.year_and_month = #{ew.yearAndMonth}
        <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="selectPageByYear" resultMap="ResultMap">
        SELECT xa.xm_id, xm.XMMC as xmmc, xm.XMBH as xmbh, xa.zcbm, t.name as user_name,<include refid="org.sky.scientific.mapper.AssetMapper.basicColumns"/>
        FROM kd_xm_intangible_asset xa
        left join kd_asset asset on asset.zcbm = xa.zcbm and asset.year_and_month = (SELECT max(year_and_month) from kd_xm_intangible_asset WHERE zcbm = xa.zcbm and left(year_and_month,4) =#{ew.yearAndMonth})
        LEFT JOIN kd_technician t on t.unicode=xa.user_unicode and t.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 left(xa.year_and_month,4 ) = #{ew.yearAndMonth}
        <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 asset.sysm <![CDATA[>=]]> #{ew.sysmMin}</if>
        <if test="ew.sysmMax!=null">and asset.sysm <![CDATA[<=]]> #{ew.sysmMax}</if>
        <if test="ew.zcyzMin!=null">and asset.zcyz <![CDATA[>=]]> #{ew.zcyzMin}</if>
        <if test="ew.zcyzMax!=null">and asset.zcyz <![CDATA[<=]]> #{ew.zcyzMax}</if>
        <if test="ew.yzjeMin!=null">and asset.YZJE <![CDATA[>=]]> #{ew.yzjeMin}</if>
        <if test="ew.yzjeMax!=null">and asset.YZJE <![CDATA[<=]]> #{ew.yzjeMax}</if>
        <if test="ew.status!=null">and asset.status = #{status}</if>
        GROUP BY xa.xm_id,xa.ZCBM
    </select>

    <resultMap id="XmIntangibleAssetCostVo" type="org.sky.scientific.pojo.vo.XmIntangibleAssetCostVo" extends="ResultMap">
        <result column="attendanceHours" property="attendanceHours"/>
        <result column="workHours" property="workHours"/>
        <result column="yanfaFenTanLv" property="yanfaFenTanLv"/>
        <result column="yanfaTanXiao" property="yanfaTanXiao"/>
    </resultMap>
    <select id="keyanwuxingzichantanxiaofeiyongfenpeibiao"
            resultMap="XmIntangibleAssetCostVo"
            parameterType="org.sky.scientific.pojo.dto.XmAssetQueryParam">
        SELECT
            v1.xm_id, xm.xmmc, xm.xmbh,v1.year_and_month,
            ZCBM as 'asset.ZCBM',ZCMC as 'asset.ZCMC', ZCLB as 'asset.ZCLB', GL as 'asset.GL',YT as 'asset.YT',ZCYZ as 'asset.ZCYZ',JCZ as 'asset.JCZ', ifnull(YZJE,0) as 'asset.YZJE',
            v1.attendanceHours,v1.workHours,v1.yanfaFenTanLv,v1.yanfaTanXiao
        FROM wuxingAssetCostView v1
        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 = 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 v1.zcmc like concat('%',#{ew.zcmc},'%')</if>
        <if test="ew.zclb!=null and ew.zclb!=''">and v1.zclb like concat('%',#{ew.zclb},'%')</if>
        <if test="ew.yt!=null and ew.yt!=''">and v1.yt like concat('%',#{ew.yt},'%')</if>
        <if test="ew.zcyzMin!=null and ew.zcyzMax!=null">and v1.zcyz between #{ew.zcyzMin} and #{ew.zcyzMax}</if>
        <if test="ew.jczMin!=null and ew.jczMax!=null">and v1.jcz between #{ew.jczMin} and #{ew.jczMax}</if>
        <if test="ew.yzjeMin!=null and ew.yzjeMax!=null">and v1.yzje between #{ew.yzjeMin} and #{ew.yzjeMax}</if>
        <if test="ew.attendanceHoursMin!=null and ew.attendanceHoursMax!=null">and v1.attendanceHours between #{ew.attendanceHoursMin} and #{ew.attendanceHoursMax}</if>
        <if test="ew.workHoursMin!=null and ew.workHoursMax!=null">and v1.workHours between #{ew.workHoursMin} and #{ew.workHoursMax}</if>
    </select>

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