<?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.basic.dal.mysql.goodsinfo.GoodsInfoMapper">

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

<!--    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.basic.dal.dataobject.goodsinfo.GoodsInfoDO">-->
<!--        <id column="id" jdbcType="BIGINT" property="id"/>-->
<!--        <result column="goods_name" jdbcType="VARCHAR" property="goodsName"/>-->
<!--        <result column="goods_id" jdbcType="VARCHAR" property="goodsId"/>-->
<!--        <result column="goods_type" jdbcType="VARCHAR" property="goodsType"/>-->
<!--        <result column="goods_type_id" jdbcType="BIGINT" property="goodsTypeId"/>-->
<!--        <result column="goods_unit" jdbcType="VARCHAR" property="goodsUnit"/>-->
<!--        <result column="goods_specs" jdbcType="VARCHAR" property="goodsSpecs"/>-->
<!--        <result column="goods_remarks" jdbcType="VARCHAR" property="goodsRemarks"/>-->
<!--        <result column="goods_img_url" jdbcType="VARCHAR" property="goodsImgUrl"/>-->
<!--        <result column="shelf_life" jdbcType="INTEGER" property="shelfLife"/>-->
<!--        <result column="belong_business_id" jdbcType="BIGINT" property="belongBusinessId"/>-->
<!--        <result column="belong_business_name" jdbcType="VARCHAR" property="belongBusinessName"/>-->
<!--    </resultMap>-->
<!--    <select id="selectPageByInfo" resultMap="BaseResultMap">-->
<!--        select bgi.*,bbi.id,bbi.business_name from basic_goods_info bgi-->
<!--        left join basic_business_info bbi on bbi.id = bgi.belong_business_id-->
<!--        ${ew.customSqlSegment}-->
<!--        order by bgi.id DESC-->
<!--    </select>-->
    <select id="selectGoodsInfoById"
            resultType="cn.iocoder.yudao.module.basic.dal.dataobject.goodsinfo.GoodsInfoDO">
    SELECT a.*,b.unit_name goods_unit_name
    FROM basic_goods_info a
    LEFT JOIN basic_unit b ON goods_unit_id = b.id
    WHERE  a.deleted = 0
        <if test="goodsName != null and goodsName != ''"> and a.goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>
        <if test="goodsId != null and goodsId != ''"> and a.goods_id = #{goodsId} </if>
        <if test="belongBusinessName != null and belongBusinessName != ''"> and a.belong_business_name LIKE CONCAT('%', #{belongBusinessName}, '%') </if>
        ORDER BY a.id DESC
    </select>

    <select id="getGoodsListByBusinessInfoId" resultType="cn.iocoder.yudao.module.basic.controller.admin.goodsinfo.vo.GoodsInfoRespVO">
        SELECT
            bgi.*,
            CASE
                bgi.is_base_unit
            WHEN 1 THEN
                'kg'
            ELSE bu.unit_name
            END unitName
        FROM
            basic_goods_info bgi
        LEFT JOIN basic_unit bu ON bgi.goods_unit_id = bu.id
        WHERE
            bgi.deleted = 0
            and bgi.belong_business_id  = #{businessInfoId}
    </select>

    <select id="queryListOfMiniResult"
            resultType="cn.iocoder.yudao.module.basic.controller.admin.goodsinfo.vo.GoodsInfoForTransferRespVO">
          SELECT
            bii.id, bii.goods_id, bii.goods_name,bii.belong_business_id,bii.belong_business_name,
            CASE
                bii.goods_unit_id
            WHEN 0 THEN
               'kg'
            ELSE bu.unit_name
            END goodsUnit
        FROM basic_goods_info bii
        LEFT JOIN basic_unit bu ON bii.goods_unit_id = bu.id
        WHERE  bii.deleted = 0 AND bii.belong_business_id = #{belongBusinessId}
    </select>

    <select id="getByGoodsId" resultType="cn.iocoder.yudao.module.basic.controller.admin.goodsinfo.vo.GoodsInfoRespVO">
        SELECT
            bgi.*,
            CASE
                bgi.is_base_unit
            WHEN 1 THEN
                'kg'
            ELSE bu.unit_name
            END unitName
        FROM
            basic_goods_info bgi
        LEFT JOIN basic_unit bu ON bgi.goods_unit_id = bu.id
        WHERE
            bgi.deleted = 0
          and bgi.goods_id = #{goodsId}
    </select>
</mapper>