<?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="com.repair.mapper.RepairConsumablesMapper">

    <resultMap type="com.repair.model.vo.ConsumablePageVo" id="repairConsumablePageMap">
        <result property="id" column="id"/>
        <result property="consumeName" column="consume_name"/>
        <result property="price" column="price"/>
        <result property="number" column="number"/>
        <result property="totalPrice" column="total_price"/>
        <result property="articleName" column="article_name"/>
        <result property="recordNo" column="record_no"/>
        <result property="reportTime" column="report_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="queryConsumablePageList" resultType="com.repair.model.vo.ConsumablePageVo" resultMap="repairConsumablePageMap">
        select rcs.id,rcs.consume_name,rcs.price,rcs.number,rcs.total_price,(select GROUP_CONCAT(name) from repair_article_type where find_in_set(id,rc.article_id) and deleted = 0) as article_name
        ,rr.record_no,rr.report_time,rcs.update_time from repair_consumables rcs
        left join repair_consume rc on rc.deleted = 0 and rc.id = rcs.consume_id
        left join repair_record rr on rr.deleted = 0 and rr.id = rcs.record_id
        <where>
            and rcs.deleted = 0
            <if test="keyWord != null and keyWord != ''">
                and (rcs.consume_name like '%' #{keyWord} '%' or rr.record_no like '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rcs.update_time BETWEEN #{startTime} and #{endTime}
            </if>
        </where>
        order by rr.report_time desc,rcs.update_time desc
    </select>

    <resultMap type="com.repair.model.vo.ConsumableExcelVo" id="repairConsumableExcelMap">
        <result property="id" column="id"/>
        <result property="consumeName" column="consume_name"/>
        <result property="price" column="price"/>
        <result property="number" column="number"/>
        <result property="totalPrice" column="total_price"/>
        <result property="articleName" column="article_name"/>
        <result property="recordNo" column="record_no"/>
        <result property="reportTime" column="report_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="queryConsumableExcelList" resultType="com.repair.model.vo.ConsumableExcelVo" resultMap="repairConsumableExcelMap">
        select rcs.id,rcs.consume_name,rcs.price,rcs.number,rcs.total_price,(select GROUP_CONCAT(name) from repair_article_type where find_in_set(id,rc.article_id) and deleted = 0) as article_name
        ,rr.record_no,rr.report_time,rcs.update_time from repair_consumables rcs
        left join repair_consume rc on rc.deleted = 0 and rc.id = rcs.consume_id
        left join repair_record rr on rr.deleted = 0 and rr.id = rcs.record_id
        <where>
            and rcs.deleted = 0
            <if test="keyWord != null and keyWord != ''">
                and (rcs.consume_name like '%' #{keyWord} '%' or rr.record_no like '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rcs.update_time BETWEEN #{startTime} and #{endTime}
            </if>
        </where>
        order by rr.report_time desc,rcs.update_time desc
    </select>

    <resultMap type="com.repair.model.vo.ChangeConsumesVo" id="repairChangeConsumesMap">
        <result property="id" column="id"/>
        <result property="articleId" column="article_id"/>
        <result property="consumeId" column="consume_id"/>
        <result property="consumeName" column="consume_name"/>
        <result property="price" column="price"/>
        <result property="number" column="number"/>
    </resultMap>
    <select id="repairChangeConsumes" resultType="com.repair.model.vo.ChangeConsumesVo" resultMap="repairChangeConsumesMap">
        select id,article_id,consume_id,consume_name,price,number from repair_consumables
        <where>
            and deleted = 0
            <if test="recordId != null and recordId != ''">
                and record_id = #{recordId}
            </if>
        </where>
        order by update_time desc
    </select>

</mapper>
