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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmGoodsEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="use_date" property="useDate"/>
        <result column="xm_id" property="xmId"/>

        <result column="type" property="type"/>
        <result column="code" property="code"/>
        <result column="name" property="name"/>
        <result column="model" property="model"/>
        <result column="unit" property="unit"/>
        <result column="unit_price" property="unitPrice"/>
        <result column="use_quantity" property="useQuantity"/>
        <result column="amount" property="amount"/>
        <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.XmGoodsVO" extends="ResultMap">
        <result column="xmmc" property="xmmc"/>
        <result column="xmbh" property="xmbh"/>
    </resultMap>

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


    <select id="selectXmGoodsPage" resultMap="ResultVoMap">
        select kxg.*, xm.xmmc, xm.xmbh
        from kd_xm_goods kxg
        left join
        (
            SELECT
                m.id, m.xmbh, m.XMKSSJ,
                COALESCE(get_xm_info(m.id, 'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm m
        ) xm on xm.id = kxg.xm_id
        where kxg.is_deleted = 0 and kxg.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmId!=null">and kxg.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 kxg.type = #{ew.type}</if>
        <if test="ew.code!=null and ew.code!=''">and kxg.code like concat('%',#{ew.code},'%')</if>
        <if test="ew.name!=null and ew.name!=''">and kxg.name like concat('%',#{ew.name},'%')</if>
        <if test="ew.model!=null and ew.model!=''">and kxg.model like concat('%',#{ew.model},'%')</if>
    </select>

    <select id="yanfalingliaodan" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.XmGoodsDTO">
        SELECT
            xm.xmbh,xm.xmmc,xg.*
        FROM kd_xm_goods xg
        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 = xg.xm_id
        WHERE xg.type = #{type} and xg.year_and_month = #{yearAndMonth}
          <if test="xmmc!=null">and xm.xmmc like concat('%',#{xmmc},'%')</if>
          <if test="name!=null">and xg.name like concat('%',#{name},'%')</if>
          <if test="model!=null">and xg.model like concat('%',#{model},'%')</if>
          <if test="dateMin!=null and dateMax!=null">and date_format(xg.use_date,'%Y-%m-%d') between #{dateMin} and #{dateMax}</if>
          <if test="xmId!=null">and xg.xm_id = #{xmId}</if>
          <if test="useDate!=null">and xg.use_date = #{useDate}</if>
          <if test="type!=null and type!=''">and xg.type = #{type}</if>
    </select>

</mapper>
