<?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.system.dal.mysql.assets.DeptAssetsMapper">

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

    <select id="selectAssetsChangeByIds"
            resultType="cn.iocoder.yudao.module.system.controller.admin.assets.dto.AssetsChangeDTO">
        SELECT
        a.id as id,
        a.assets_id as assetsId,
        b.name,
        b.assets_no,
        a.stock,
        b.type_id,
        a.status,
        c.type
        FROM zc_dept_assets a
        LEFT JOIN zc_assets b ON a.assets_id = b.id
        LEFT JOIN zc_assets_type c ON b.type_id = c.id
        <where>
            a.deleted = 0
            <if test="deptId != null">
                and a.dept_id = #{deptId}
            </if>
            <if test="assetsIds != null and assetsIds.size() > 0">
                and a.assets_id IN
                <foreach collection="assetsIds" item="assetsId" separator="," open="(" close=")">
                    #{assetsId}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getDeptAssetsPage"
            resultType="cn.iocoder.yudao.module.system.controller.admin.assets.vo.DeptAssetsRespVO">
        SELECT
        a.id,
        a.assets_id as assetsId,
        a.dept_id as deptId,
        a.status,
        a.stock,
        a.create_time as createTime,
        b.name as assetsName,
        c.name as deptName
        FROM zc_dept_assets as a
        left join zc_assets as b on a.assets_id = b.id
        left join system_dept as c on a.dept_id = c.id
        <where>
            a.deleted = 0
            <if test="vo.deptId != null">
                and a.dept_id = #{vo.deptId}
            </if>
            <if test="vo.filterStockFlag != null and vo.filterStockFlag == 1">
                and a.stock > 0
            </if>
            <if test="vo.typeId != null">
                and b.type_id = #{vo.typeId}
            </if>
            <if test="vo.assetsId != null">
                and a.assets_id = #{vo.assetsId}
            </if>
            <if test="vo.status != null">
                and a.status = #{vo.status}
            </if>
            <if test="vo.assetsName != null and vo.assetsName != ''">
                and b.name like concat('%', #{vo.assetsName}, '%')
            </if>
        </where>
    </select>
    <select id="deptAssetsLittlePage"
            resultType="cn.iocoder.yudao.module.system.controller.admin.assets.vo.DeptAssetsLittleVO">
        SELECT
        a.assets_id as id,
        a.dept_id as deptId,
        a.status,
        a.stock,
        a.create_time as createTime,
        b.name as name,
        c.type as type
        FROM zc_dept_assets as a
        left join zc_assets as b on a.assets_id = b.id
        left join zc_assets_type as c on b.type_id = c.id
        <where>
            a.deleted = 0
            <if test="vo.deptId != null">
                and a.dept_id = #{vo.deptId}
            </if>
            <if test="vo.filterStockFlag != null and vo.filterStockFlag == 1">
                and a.stock > 0
            </if>
            <if test="vo.typeId != null">
                and b.type_id = #{vo.typeId}
            </if>
            <if test="vo.assetsId != null">
                and a.assets_id = #{vo.assetsId}
            </if>
            <if test="vo.status != null">
                and a.status = #{vo.status}
            </if>
            <if test="vo.assetsName != null and vo.assetsName != ''">
                and b.name like concat('%', #{vo.assetsName}, '%')
            </if>
        </where>
    </select>
</mapper>
