<?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.zt.plat.module.erp.dal.mysql.erp.ErpWarehouseMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectMaxCode" resultType="java.lang.String">
        SELECT MAX(NUM)
        FROM sply_erp_wrh
    </select>

    <select id="getPageByReq" resultType="com.zt.plat.module.erp.dal.dataobject.erp.ErpWarehouseDO">
        select f.id,
        f.name as name,
        f.NUM as number,
        f.TP as type,
        f.REL_NAME as relName,
        f.REL_NUM as relnumber,
        f.IS_ENB as isEnable,
        f.FACT_NUM as factoryNumber,
        c.NAME as factoryName
        from sply_erp_wrh f left join sply_erp_fact c on f.FACT_NUM = c.NUM
        where f.DELETED = 0
        <if test="name != null">
            and f.NAME like concat('%', #{name}, '%')
        </if>
        <if test="number != null">
            and f.NUM like concat('%', #{number}, '%')
        </if>
        <if test="factoryNumber != null">
            and f.FACT_NUM like concat('%', #{factoryNumber}, '%')
        </if>
        <if test="factoryName != null">
            and c.NAME like concat('%', #{factoryName}, '%')
        </if>
        <if test="type != null">
            and f.TP like concat('%', #{type}, '%')
        </if>
    </select>

    <!-- 在 ErpWarehouseMapper.xml 中配置 -->
    <resultMap id="WarehouseWithFactoryResultMap" type="com.zt.plat.module.erp.dal.dataobject.erp.ErpWarehouseDO">
        <id column="id" property="id"/>
        <result column="factoryNumber" property="factoryNumber"/>
        <result column="companyNumber" property="companyNumber"/>
        <result column="factoryName" property="factoryName"/> <!-- 虚拟字段 -->
        <result column="name" property="name"/>
        <result column="number" property="number"/>
        <result column="type" property="type"/>
        <result column="relName" property="relName"/>
        <result column="relnumber" property="relnumber"/>
        <result column="isEnable" property="isEnable"/>
        <result column="companyIdCustom" property="companyIdCustom"/>
        <result column="companyNameCustom" property="companyNameCustom"/>
        <!-- BaseDO 继承字段 -->
        <result column="createTime" property="createTime"/>
        <result column="updateTime" property="updateTime"/>
        <result column="creator" property="creator"/>
        <result column="updater" property="updater"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <select id="selectWarehouseByDynamicParams" resultMap="WarehouseWithFactoryResultMap">
        SELECT
        sew.ID AS id,
        sew.FACT_NUM AS factoryNumber,
        sef.NAME AS factoryName,
        sew.NAME AS name,
        sew.NUM AS number,
        sew.TP AS type,
        sew.REL_NAME AS relName,
        sew.REL_NUM AS relnumber,
        sew.IS_ENB AS isEnable,
        sew.CPN_ID_CST AS companyIdCustom,
        sew.CPN_NAME_CST AS companyNameCustom,
        sew.CREATE_TIME AS createTime,
        sew.UPDATE_TIME AS updateTime,
        sew.CREATOR AS creator,
        sew.UPDATER AS updater,
        sew.DELETED AS deleted
        FROM JYGK_TEST.SPLY_ERP_FACT sef
        INNER JOIN JYGK_TEST.SPLY_ERP_WRH sew
        ON sew.FACT_NUM = sef.NUM
        WHERE 1=1
        AND sef.DELETED = 0
        AND sew.DELETED = 0
        <if test="paramReqVO.companyIdCustom != null and paramReqVO.companyIdCustom != ''">
            AND sef.CPN_ID LIKE CONCAT('%', #{paramReqVO.companyIdCustom}, '%')
        </if>
        <if test="paramReqVO.type != null and paramReqVO.type != ''">
            AND sef.TP LIKE CONCAT('%', #{paramReqVO.type}, '%')
        </if>
        <if test="paramReqVO.number != null and paramReqVO.number != ''">
            AND sew.NUM LIKE CONCAT('%', #{paramReqVO.number}, '%')
        </if>
        <if test="paramReqVO.factoryNumber != null and paramReqVO.factoryNumber != ''">
            AND sef.NUM LIKE CONCAT('%', #{paramReqVO.factoryNumber}, '%')
        </if>
        <if test="paramReqVO.factoryName != null and paramReqVO.factoryName != ''">
            AND sef.NAME LIKE CONCAT('%', #{paramReqVO.factoryName}, '%')
        </if>
        ORDER BY sew.CREATE_TIME DESC
        <!-- 分页参数直接引用 @Param 指定的 pageNum/pageSize -->
        LIMIT #{pageNum}, #{pageSize}
    </select>

    <!-- 总数查询方法：同样修正参数引用 -->
    <select id="selectWarehouseByDynamicParamsCount" resultType="java.lang.Long">
        SELECT
        COUNT(*)
        FROM JYGK_TEST.SPLY_ERP_FACT sef
        INNER JOIN JYGK_TEST.SPLY_ERP_WRH sew
        ON sew.FACT_NUM = sef.NUM
        WHERE 1=1
        AND sef.DELETED = 0
        AND sew.DELETED = 0
        <if test="paramReqVO.companyIdCustom != null and paramReqVO.companyIdCustom != ''">
            AND sef.CPN_ID LIKE CONCAT('%', #{paramReqVO.companyIdCustom}, '%')
        </if>
        <if test="paramReqVO.type != null and paramReqVO.type != ''">
            AND sef.TP LIKE CONCAT('%', #{paramReqVO.type}, '%')
        </if>
        <if test="paramReqVO.number != null and paramReqVO.number != ''">
            AND sew.NUM LIKE CONCAT('%', #{paramReqVO.number}, '%')
        </if>
        <if test="paramReqVO.factoryNumber != null and paramReqVO.factoryNumber != ''">
            AND sef.NUM LIKE CONCAT('%', #{paramReqVO.factoryNumber}, '%')
        </if>
        <if test="paramReqVO.factoryName != null and paramReqVO.factoryName != ''">
            AND sef.NAME LIKE CONCAT('%', #{paramReqVO.factoryName}, '%')
        </if>
    </select>

</mapper>