<?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="cn.iocoder.yudao.module.stockOperate.mapper.DlRepairSoiMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.stockOperate.entity.DlRepairSoi">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="soId" column="so_id" jdbcType="VARCHAR"/>
        <result property="soiType" column="soi_type" jdbcType="VARCHAR"/>
        <result property="goodsId" column="goods_id" jdbcType="VARCHAR"/>
        <result property="goodsType" column="goods_type" jdbcType="VARCHAR"/>
        <result property="wareId" column="ware_id" jdbcType="VARCHAR"/>
        <result property="goodsCount" column="goods_count" jdbcType="INTEGER"/>
        <result property="goodsPrice" column="goods_price" jdbcType="DECIMAL"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_SQL">
        select soi.id,
               so_id,
               soi_type,
               goods_id,
               goods_type,
               ware_id,
               goods_count,
               goods_price,
               soi.remark,
               so.create_time,
               so.so_status,
               so.corp_id
        from dl_repair_soi soi
                 left join
             dl_repair_so so
             on soi.so_id = so.id
        left join dl_repair_wares drw on soi.goods_id = drw.id
        where soi.deleted = '0' and so.so_status != '06'
    </sql>

    <select id="getRepairSoiPage" resultMap="BaseResultMap">
        <include refid="Base_SQL" />
        <if test="map.goodsType != null and map.goodsType != ''">
            and soi.goods_type = #{map.goodsType}
        </if>
        <if test="map.soiType != null and map.soiType != ''">
            and soi.soi_type = #{map.soiType}
        </if>
        <if test="map.searchTimeArray != null and map.searchTimeArray.length > 0">
            and (so.create_time between #{map.searchTimeArray[0]} and #{map.searchTimeArray[1]})
        </if>
        <if test="map.soStatus != null and map.soStatus != ''">
            and so.so_status = #{map.soStatus}
        </if>
        <if test="map.corpId != null and map.corpId != ''">
            and so.corp_id = #{map.corpId}
        </if>
        <if test="map.wareId != null and map.wareId != ''">
            and soi.ware_id = #{map.wareId}
        </if>
        <if test="map.query != null and map.query != null">
            and (
            so.so_no like concat('%', #{map.query}, '%')
            or
            drw.name like concat('%', #{map.query}, '%')
            or
            drw.code like concat('%', #{map.query}, '%')
            or
            drw.model like concat('%', #{map.query}, '%')
            )
        </if>
    </select>

    <resultMap id="SoiAndWares" type="cn.iocoder.yudao.module.stockOperate.vo.DlRepairSoiRespVO">
        <id property="id" column="id" />
        <result property="soId" column="so_id" />
        <result property="soiType" column="soi_type" />
        <result property="goodsId" column="goods_id" />
        <result property="goodsType" column="goods_type" />
        <result property="wareId" column="ware_id" />
        <result property="goodsCount" column="goods_count" />
        <result property="goodsPrice" column="goods_price" />
        <result property="remark" column="remark" />
        <result property="inCount" column="in_count" />
        <result property="rawId" column="raw_id" />
        <result property="typeName" column="type_name" />

        <!-- 嵌套的结果映射 -->
        <association property="wares" javaType="cn.iocoder.yudao.module.project.entity.RepairWares">
            <id property="id" column="wares_id" />
            <result property="barCode" column="bar_code" />
            <result property="code" column="code" />
            <result property="name" column="name" />
            <result property="model" column="model" />
            <result property="price" column="price" />
            <result property="purPrice" column="pur_price" />
            <result property="type" column="type" />
            <result property="unit" column="unit" />
            <result property="warehouse" column="warehouse" />
            <result property="miniStock" column="mini_stock" />
            <result property="maxStock" column="max_stock" />
            <result property="stock" column="stock" />
            <result property="img" column="img" />
            <result property="attribute" column="attribute" />
            <result property="corpId" column="corp_id" />
            <result property="coverImg" column="cover_img" />
            <result property="carModel" column="car_model" />
            <result property="remark" column="wares_remark" />
            <result property="status" column="status" />
            <result property="dataForm" column="data_form" />
        </association>
    </resultMap>
    <select id="getMapBySoIdAndQuery" resultMap="SoiAndWares">
        select
        drs.*,
        drw.id AS wares_id,
        drw.bar_code,
        drw.code,
        drw.name,
        drw.model,
        drw.price,
        drw.pur_price,
        drw.type,
        drw.unit,
        drw.warehouse,
        drw.mini_stock,
        drw.max_stock,
        drw.stock,
        drw.img,
        drw.attribute,
        drw.corp_id,
        drw.cover_img,
        drw.car_model,
        drw.remark AS wares_remark,
        drw.status,
        drw.data_form
        from dl_repair_soi drs
        left join dl_repair_wares drw
        on drs.goods_id = drw.id
        where drs.deleted = '0' and drw.deleted = '0'
        and drs.so_id = #{id}
        <if test="query != null and query != '' and query != 'null'">
            and (
            drw.name like concat('%', #{query}, '%')
            or
            drw.code like concat('%', #{query}, '%')
            or
            drw.model like concat('%', #{query}, '%')
            )
        </if>
    </select>

    <select id="getSoBySupplier" resultMap="SoiAndWares">
        select drsi.*,
        drw.id AS wares_id,
        drw.bar_code,
        drw.code,
        drw.name,
        drw.model,
        drw.price,
        drw.pur_price,
        drw.type,
        drw.unit,
        drw.warehouse,
        drw.mini_stock,
        drw.max_stock,
        drw.stock,
        drw.img,
        drw.attribute,
        drw.corp_id,
        drw.cover_img,
        drw.car_model,
        drw.remark AS wares_remark,
        drw.status,
        drw.data_form
        from dl_repair_soi drsi
        join dl_repair_so drs on drsi.so_id = drs.id
        join dl_repair_wares drw on drsi.goods_id = drw.id
        where drs.so_type = '01'
        <if test="map.supplierId != null and map.supplierId != ''">
            and drs.supplier_id = #{map.supplierId}
        </if>
        <if test="map.query != null and map.query != ''">
            and (
            drw.name like concat('%', #{map.query}, '%')
            or
            drw.code like concat('%', #{map.query}, '%')
            or
            drw.model like concat('%', #{map.query}, '%')
            )
        </if>
    </select>
</mapper>
