<?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.handlinggroup.HandlingGroupMapper">

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

    <select id="selectPageList"
            resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlinggroup.HandlingGroupDO">
        select
        a.*,
        b.short_name as factoryName
        from sf_handling_group a
        left join sf_factory_info as b on a.factory_id = b.id
        <where>
            a.deleted = 0
            and b.deleted = 0
            <if test="vo.factoryId != null">
                and a.factory_id = #{vo.factoryId}
            </if>
            <if test="vo.handlingGroupName != null and vo.handlingGroupName != ''">
                and a.handling_group_name like concat('%', #{vo.handlingGroupName}, '%')
            </if>
        </where>
    </select>

    <resultMap id="handlingSummaryMap"
               type="cn.iocoder.yudao.module.smartfactory.controller.admin.handlinggroupamountspecifications.vo.HandlingGroupAmountSpecificationsVO">
        <!-- 基础字段映射 -->
        <id property="id" column="id"/>
        <result property="factoryId" column="factory_id"/>
        <result property="handlingGroupId" column="handling_group_id"/>
        <result property="handlingSpecificationsId" column="handling_specifications_id"/>
        <result property="staffId" column="staff_id"/>
        <result property="totalCount" column="total_count"/>
        <result property="dateStr" column="date_str"/>
        <result property="amount" column="amount"/>
        <result property="creator" column="creator"/>
        <result property="createTime" column="create_time"/>
        <result property="updater" column="updater"/>
        <result property="updateTime" column="update_time"/>
        <result property="deleted" column="deleted"/>

        <!-- 关联字段 -->
        <result property="factoryName" column="factoryName"/>
        <result property="handlingGroupName" column="handlingGroupName"/>
        <result property="staffName" column="staffName"/>

        <!-- 聚合字段 -->
        <result property="totalAmount" column="totalAmount"/>
        <result property="totalNum" column="totalNum"/>

        <!-- 自定义对象列表 (JSON 映射) -->
        <result property="specificationDetails" column="specificationDetails"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"
                javaType="java.util.List"
        />
    </resultMap>

    <select id="selectPageListByDateStr"
            resultMap="handlingSummaryMap">
        SELECT
        a.*,
        b.short_name as factoryName,
        d.handling_group_name as handlingGroupName,
        c.nick_name as staffName,
        SUM( a.amount ) AS totalAmount,
        sum( a.total_count ) AS totalNum,
        JSON_ARRAYAGG(
        JSON_OBJECT(
        'handlingSpecificationsId', a.handling_specifications_id,
        'totalCount', a.total_count,
        'amount', a.amount,
        'handlingSpecificationsName', (
        SELECT hs.specifications_name
        FROM sf_handling_specifications as hs
        WHERE hs.id = a.handling_specifications_id
        )
        )
        ) AS specificationDetails
        FROM
        sf_handling_group_amount_specifications AS a
        left join sf_factory_info as b on a.factory_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="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.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.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>
            <if test="vo.staffName != null and vo.staffName != ''">
                and c.nick_name like concat('%', #{vo.staffName}, '%')
            </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>
        </where>
        GROUP BY
        a.factory_id,
        a.date_str,
        a.handling_group_id
    </select>
    <select id="getHandlingGroup"
            resultType="cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlinggroup.HandlingGroupDO">
        SELECT a.*,
               b.short_name as factoryName
        FROM sf_handling_group AS a
                 left join sf_factory_info as b on a.factory_id = b.id
        WHERE a.deleted = 0
          and a.id = #{id}
    </select>
</mapper>
