<?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.smartfactory.dal.mysql.handlinggroupamountspecifications.HandlingGroupAmountSpecificationsMapper">

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

    <select id="getHandlingGroupAmountSpecificationsPage"
            resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlinggroupamountspecifications.HandlingGroupAmountSpecificationsDO">
        SELECT a.*,
        b.specifications_name as handlingSpecificationsName,
        c.nick_name as staffName,
        f.short_name as factoryName,
        d.handling_group_name as handlingGroupName
        FROM sf_handling_group_amount_specifications AS a
        left join sf_factory_info as f on a.factory_id = f.id
        LEFT JOIN sf_handling_specifications AS b ON a.handling_specifications_id = b.id
        LEFT JOIN sf_staff AS c ON c.id = a.staff_id
        left join sf_handling_group as d on a.handling_group_id = d.id
        <where>
            a.deleted = 0
            and b.deleted = 0
            and d.deleted = 0
            <if test="vo.handlingSpecificationsId != null">
                AND a.handling_specifications_id = #{vo.handlingSpecificationsId}
            </if>
            <if test="vo.handlingGroupId != null">
                and a.handling_group_id = #{vo.handlingGroupId}
            </if>
            <if test="vo.staffName != null and vo.staffName != ''">
                and c.nick_name like concat('%', #{vo.staffName}, '%')
            </if>
            <if test="vo.factoryId != null">
                AND a.factory_id = #{vo.factoryId}
            </if>
            <if test="vo.factoryIds != null and vo.factoryIds.size() > 0">
                and a.factory_id in
                <foreach collection="vo.factoryIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="vo.porterName != null and vo.porterName != ''">
                AND EXISTS (
                select e.id from sf_handling_group_user_amount as e
                left join sf_staff as g on g.id = e.user_id
                where a.factory_id = e.factory_id
                and a.date_str = e.date_str
                and a.handling_group_id = e.handling_group_id
                and g.nick_name like concat('%', #{vo.porterName}, '%')
                and e.deleted = 0
                )
            </if>
            <if test="vo.createTime != null and vo.createTime.length > 0">
                <if test="vo.createTime[0] != null">
                    and STR_TO_DATE( a.date_str, '%Y-%m-%d' ) &gt;= #{vo.createTime[0]}
                </if>
                <if test="vo.createTime[1] != null">
                    and STR_TO_DATE( a.date_str, '%Y-%m-%d' ) &lt;= #{vo.createTime[1]}
                </if>
            </if>
        </where>
    </select>
    <select id="getHandlingGroupAmountSpecifications"
            resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlinggroupamountspecifications.HandlingGroupAmountSpecificationsDO">
        SELECT a.*,
        b.specifications_name as handlingSpecificationsName
        FROM sf_handling_group_amount_specifications AS a
        LEFT JOIN sf_handling_specifications AS b ON a.handling_specifications_id = b.id
        <where>
            a.deleted = 0
            and b.deleted = 0
            and a.id = #{id}
        </where>
    </select>
    <select id="getHandlingGroupAmountSpecificationsList"
            resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlinggroupamountspecifications.HandlingGroupAmountSpecificationsDO">
        SELECT a.*,
        b.specifications_name as handlingSpecificationsName,
        c.nick_name as staffName,
        f.short_name as factoryName,
        d.handling_group_name as handlingGroupName
        FROM sf_handling_group_amount_specifications AS a
        left join sf_factory_info as f on a.factory_id = f.id
        LEFT JOIN sf_handling_specifications AS b ON a.handling_specifications_id = b.id
        LEFT JOIN sf_staff AS c ON c.id = a.staff_id
        left join sf_handling_group as d on a.handling_group_id = d.id
        <where>
            a.deleted = 0
            and b.deleted = 0
            <if test="handlingGroupId != null">
                and a.handling_group_id = #{handlingGroupId}
            </if>
            <if test="factoryId != null">
                AND a.factory_id = #{factoryId}
            </if>
            <if test="dateStr != null and dateStr != ''">
                and a.date_str like concat('%', #{dateStr}, '%')
            </if>
        </where>
    </select>
    <select id="getTotalNum"
            resultType="cn.iocoder.yudao.module.smartfactory.controller.admin.handlinggroupamountspecifications.vo.HandlingGroupAmountSpecificationsTotalNumVO">
        select
            sum(a.total_count) as num,
            sum(a.amount) as amount
        from sf_handling_group_amount_specifications AS a
        left join sf_staff as b on a.staff_id = b.id
        <where>
            a.deleted = 0
            <if test="dto.factoryId != null">
                and a.factory_id = #{dto.factoryId}
            </if>
            <if test="dto.factoryIds != null and dto.factoryIds.size() > 0">
                and a.factory_id in
                <foreach collection="dto.factoryIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.handlingSpecificationsId != null">
                and a.handling_specifications_id = #{dto.handlingSpecificationsId}
            </if>
            <if test="dto.forkliftDriverName != null and dto.forkliftDriverName != ''">
                and b.nick_name like concat('%', #{dto.forkliftDriverName}, '%')
            </if>
            <if test="dto.porterName != null and dto.porterName != ''">
                AND EXISTS (
                SELECT
                c.id
                FROM
                sf_handling_group_user_amount AS c
                LEFT JOIN sf_staff AS d ON d.id = c.user_id
                WHERE
                c.deleted = 0
                and a.factory_id = c.factory_id
                and a.date_str = c.date_str
                and a.handling_group_id = c.handling_group_id
                and d.nick_name like concat('%', #{dto.porterName}, '%')
                )
            </if>
            <if test="dto.createTime != null and dto.createTime.length > 0">
                <if test="dto.createTime[0] != null">
                    and STR_TO_DATE( a.date_str, '%Y-%m-%d' ) &gt;= #{dto.createTime[0]}
                </if>
                <if test="dto.createTime[1] != null">
                    and STR_TO_DATE( a.date_str, '%Y-%m-%d' ) &lt;= #{dto.createTime[1]}
                </if>
            </if>
        </where>
    </select>
</mapper>
