<?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.cf.imes.module.plan.dal.mysql.orderImport.OrderImportTempDataMapper">
    <select id="selectCountGoodsIdGroup" resultType="java.lang.Integer">
        select count(distinct outer_goods_id) from order_import_temp_data
        where task_id = #{reqVO.taskId} and type = #{reqVO.type}
    </select>

    <select id="selectGoodsIdGroup"
            resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataGoodsIdGroupDO">
        select outer_goods_id
        <if test="reqVO.doPaging">
        , min(sort) as sortn
        </if>
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and type = #{reqVO.type}
        group by outer_goods_id
        <if test="reqVO.doPaging">
            order by sortn
            limit #{reqVO.offset}, #{reqVO.size}
        </if>
    </select>

    <select id="selectCountPlateGoodsGroup" resultType="java.lang.Integer">
        select count(1) from (
        select goods_name,
               material,
               color,
               thickness,
               brand
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and type = #{reqVO.type} and outer_goods_id = #{reqVO.goodsId}
        group by goods_name,material, color, thickness, brand) temp
    </select>

    <select id="selectCountPartGoodsGroup" resultType="java.lang.Integer">
        select count(1) from (
                                 select goods_name,
                                        material,
                                        color,
                                        is_composite,
                                        category,
                                        sub_type,
                                        factory,
                                        model,
                                        spec,
                                        height,
                                        width,
                                        thickness,
                                        unit,
                                        brand
                                 from order_import_temp_data
                                 where task_id = #{reqVO.taskId} and type = #{reqVO.type} and outer_goods_id = #{reqVO.goodsId}
                                 group by goods_name, material, color, is_composite, category, sub_type, factory, model, spec, height, width, thickness, unit, brand) temp
    </select>

    <select id="selectPlateGoodsGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataGoodsGroupDO">
        select goods_name,
               material,
               color,
               thickness,
               brand
        <if test="reqVO.doPaging">
            ,min(sort) as sortn
        </if>
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and type = #{reqVO.type} and outer_goods_id = #{reqVO.goodsId}
        group by goods_name, material, color, thickness, brand
        <if test="reqVO.doPaging">
            order by sortn
            limit #{reqVO.offset}, #{reqVO.size}
        </if>
    </select>

    <select id="selectPartGoodsGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataGoodsGroupDO">
        select
            goods_name,
            material,
            color,
            is_composite,
            category,
            sub_type,
            factory,
            model,
            spec,
            height,
            width,
            thickness,
            unit,
            brand,
            min(sort) as sortn
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and type = #{reqVO.type} and outer_goods_id = #{reqVO.goodsId}
        group by goods_name, material, color, is_composite, category, sub_type, factory, model, spec, height, width, thickness, unit, brand
        <if test="reqVO.doPaging">
            order by sortn
            limit #{reqVO.offset}, #{reqVO.size}
        </if>
    </select>

    <select id="selectCountRoomGroup" resultType="java.lang.Integer">
        select count(1)
        from (select room_name
              from order_import_temp_data
              where task_id = #{taskId} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
              group by room_name) temp
    </select>

    <select id="selectRoomGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataGoodsGroupDO">
        select room_name
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
        group by room_name
        <if test="reqVO.doPaging">
            order by sort
            limit #{reqVO.offset}, #{reqVO.size}
        </if>
    </select>

    <select id="selectCountBodyGroup" resultType="java.lang.Integer">
        select count(1)
        from (select body_name
              from order_import_temp_data
              where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
              group by body_name) temp
    </select>

    <select id="selectBodyGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataGoodsGroupDO">
        select body_name,sum(case type when '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@PLATE.type}' then num else 0 end) as bodyPlateNum
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
        group by body_name
        <if test="reqVO.doPaging">
            order by sort
            limit #{reqVO.offset}, #{reqVO.size}
        </if>
    </select>

    <select id="selectCountGroupTypeNameGroup" resultType="java.lang.Integer">
        select count(1)
        from (select group_type_name
              from order_import_temp_data
              where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and body_name = #{reqVO.bodyName} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
              group by group_type_name) temp
    </select>

    <select id="selectGroupTypeNameGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataGoodsGroupDO">
        select group_type_name, count(1) as count
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and body_name = #{reqVO.bodyName} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
        group by group_type_name
        <if test="reqVO.doPaging">
            order by sort
            limit #{reqVO.offset}, #{reqVO.size}
        </if>
    </select>

    <select id="selectCountGroupNameGroup" resultType="java.lang.Integer">
        select count(1)
        from (select group_name
              from order_import_temp_data
              where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and body_name = #{reqVO.bodyName} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
              <choose>
                  <when test="reqVO.groupTypeName != null and reqVO.groupTypeName != ''">
                      and group_type_name = #{reqVO.groupTypeName}
                  </when>
                  <otherwise>
                      and group_type_name is null
                  </otherwise>
              </choose>
              group by group_name) temp
    </select>

    <select id="selectGroupNameGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataGoodsGroupDO">
        select group_name, sum(case type when '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@PLATE.type}' then num else 0 end) as groupNameNum
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and body_name = #{reqVO.bodyName} and type != '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@ORDER.type}'
        <choose>
            <when test="reqVO.groupTypeName != null and reqVO.groupTypeName != ''">
                and group_type_name = #{reqVO.groupTypeName}
            </when>
            <otherwise>
                and group_type_name is null
            </otherwise>
        </choose>
        group by group_name
        <if test="reqVO.doPaging">
            order by sort
            limit #{reqVO.offset}, #{reqVO.size}
        </if>
    </select>

    <select id="selectCountGroupName" resultType="java.lang.Integer">
        select count(1)
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and body_name = #{reqVO.bodyName} and type = #{reqVO.type}
        <choose>
            <when test="reqVO.groupTypeName != null and reqVO.groupTypeName != ''">
                and group_type_name = #{reqVO.groupTypeName}
            </when>
            <otherwise>
                and group_type_name is null
            </otherwise>
        </choose>
        <choose>
            <when test="reqVO.groupName != null and reqVO.groupName != ''">
                and group_name = #{reqVO.groupName}
            </when>
            <otherwise>
                and group_name is null
            </otherwise>
        </choose>
    </select>

    <select id="selectGroupNameList" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataDO">
        select *
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and room_name = #{reqVO.roomName} and body_name = #{reqVO.bodyName} and type = #{reqVO.type}
        <choose>
            <when test="reqVO.groupTypeName != null and reqVO.groupTypeName != ''">
                and group_type_name = #{reqVO.groupTypeName}
            </when>
            <otherwise>
                and group_type_name is null
            </otherwise>
        </choose>
        <choose>
            <when test="reqVO.groupName != null and reqVO.groupName != ''">
                and group_name = #{reqVO.groupName}
            </when>
            <otherwise>
                and group_name is null
            </otherwise>
        </choose>
        order by sort
        limit #{reqVO.offset}, #{reqVO.size}
    </select>

    <select id="selectPartList" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataDO">
        select *
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and type = '${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@PART.type}' and inner_goods_id = #{reqVO.goodsId}
    </select>

    <select id="selectPartItemCodeGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataPartItemCodeGroupDO">
        select outer_id as item_code, type, min(sort) as sortn
        from order_import_temp_data
        where task_id = #{reqVO.taskId}
        and type in ('${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@PART_EDGING.type}','${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@PART_HARDWARE.type}','${@com.cf.imes.module.executor.enums.OrderImportTmpDataTypeEnum@PART_ASSEMBLY.type}')
        group by outer_id, type
    </select>

    <select id="selectPartRoomBodyGroupGroup" resultType="com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTempDataPartRoomBodyGroupGroupDO">
        select room_code, body_code, group_code, sum(num) as partsNum
        from order_import_temp_data
        where task_id = #{reqVO.taskId} and type = #{reqVO.type} and outer_id = #{reqVO.outerId}
        group by room_code, body_code, group_code
    </select>
</mapper>