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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmLeaseEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="lease_date" property="leaseDate"/>
        <result column="xm_id" property="xmId"/>
        <result column="xmmc" property="xmmc"/>
        <result column="xmbh" property="xmbh"/>
        <result column="type" property="type"/>
        <result column="name" property="name"/>
        <result column="model" property="model"/>
        <result column="unit_price" property="unitPrice"/>
        <result column="quantity" property="quantity"/>
        <result column="amount" property="amount"/>
        <result column="remark" property="remark"/>
        <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>

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

    <select id="selectXmLeasePage" resultMap="ResultMap">
        select kxl.*, xm.xmmc, xm.xmbh
        from kd_xm_lease kxl
        left join (
            SELECT
                temp.id,xmbh,
                COALESCE(get_xm_info(temp.id,'XMMC', #{ew.yearAndMonth}),XMMC) AS xmmc
            FROM kd_xm temp
        ) xm on xm.id = kxl.xm_id
        where kxl.is_deleted = 0 and kxl.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmId!=null and ew.xmId>0">and kxl.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmbh!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.xmbh!=null and ew.xmbh!=''">and xm.xmbh like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.type!=null and ew.type!=''">and kxl.type = #{ew.type}</if>
        <if test="ew.name!=null and ew.name!=''">and kxl.name like concat('%',#{ew.name},'%')</if>
        <if test="ew.model!=null and ew.model!=''">and kxl.model like concat('%',#{ew.model},'%')</if>
    </select>

    <select id="exportXmLease" resultType="org.sky.scientific.excel.XmLeaseExcel">
        select kxl.*, xm.xmmc, xm.xmbh
        from kd_xm_lease kxl
        left join kd_xm xm on xm.id = kxl.xm_id
        where kxl.is_deleted = 0 and kxl.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.xmbh!=null and ew.xmbh!=''">and xm.xmbh like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.type!=null and ew.type!=''">and xm.type = #{ew.type}</if>
        <if test="ew.name!=null and ew.name!=''">and kxl.name like concat('%',#{ew.name},'%')</if>
        <if test="ew.model!=null and ew.model!=''">and kxl.model like concat('%',#{ew.model},'%')</if>
    </select>

    <select id="yanfazulinzichanfeiyongfenpeibiao" resultMap="ResultMap" parameterType="org.sky.scientific.pojo.entity.XmLeaseEntity">
        SELECT
            xm.xmbh,xm.xmmc,xl.*
        FROM kd_xm_lease xl
        LEFT JOIN
        (
            SELECT
                temp.id,xmbh,
                COALESCE(get_xm_info(temp.id,'XMMC', #{yearAndMonth}),XMMC) AS xmmc
            FROM kd_xm temp
        ) xm on xm.id = xl.xm_id
        WHERE  xl.year_and_month = #{yearAndMonth}
        <if test="xmmc!=null and xmmc!=''">and xm.xmmc like concat('%',#{xmmc},'%')</if>
        <if test="type!=null and type!=''" >and xl.type like concat('%',#{type},'%')</if>
        <if test="name!=null and name!=''">and xl.name like concat('%',#{name},'%')</if>
        <if test="xmId!=null">and xl.xm_id = #{xmId}</if>
        <if test="leaseDate!=null">and xl.lease_date = #{leaseDate}</if>
     </select>

</mapper>
