<?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.executor.dal.mysql.goods.GoodsMapper">

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


    <select id="selectGoodsList" resultType="com.cf.imes.module.executor.controller.admin.plan.vo.GoodsReqVO">


        select
            og.*,
            orp.id as remainId
        from order_goods og
                 left join  order_remain_plate orp on og.organ_id = orp.organ_id and og.id = orp.goods_id
        where  og.organ_id = #{organId} and og.deleted = false and og.order_id = #{orderId}

    </select>



    <select id="selectGoodsListByOrderIds"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.GoodsReqVO">

        select distinct
            og.goods_id,
            og.goods_name,
            og.material,
            og.color,
            og.thickness,
            og.brand,
            og.texture,
            og.remark,
            orp.id as remainId

        from order_plate op
                 join order_goods og on op.organ_id = og.organ_id and op.goods_id = og.id
                 left join order_remain_plate orp on og.organ_id = orp.organ_id and og.id = orp.goods_id

        where op.organ_id = #{organId} and op.plan_id = #{planId}


    </select>



    <update id="updateDeletedById">
        UPDATE order_goods
        SET deleted = true,plan_id = 0
        WHERE  organ_id = #{organId}
        and order_id = #{orderId}
        and id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <delete id="deletedById">
        DELETE FROM order_goods
        WHERE id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND organ_id = #{organId}
    </delete>



    <select id="selectPlanGoodsDataSource" resultType="java.util.Map">


        select ${filed} from order_goods where  organ_id = #{organId}
                                           and deleted = false
                                            and plan_id = #{planId}

    </select>



    <resultMap id="OrderGoodsMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.OrderGoodsResp">

        <result property="goodsName" column="goodsName"/>
        <result property="material" column="material"/>
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="thickness" column="thickness"/>
        <result property="texture" column="texture"/>
        <result property="color" column="color"/>
        <result property="goodsId" column="goodsId"/>

        <collection property="orderIds" ofType="com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds" resultMap="OrderIdMap"/>

    </resultMap>





    <resultMap id="GoodsListMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.PlateInfoVO">
        <result property="planId" column="planId"/>
        <result property="material" column="material"/>
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="thickness" column="thickness"/>
        <result property="color" column="color"/>
        <result property="count" column="plateNum"/>
        <result property="area" column="area"/>
        <result property="goodsId" column="goodsId"/>

        <collection property="orderIdList" ofType="com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds" resultMap="OrderIdMap"/>

        <collection property="platePages" ofType="com.cf.imes.module.executor.controller.admin.plan.vo.PlatePage" resultMap="PlatePageMap"/>

    </resultMap>


    <resultMap id="OrderIdMap" type="com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds">

        <result property="id" column="id"/>
        <result property="orderId" column="orderId"/>
        <result property="goodsId" column="goodsId"/>

    </resultMap>


    <resultMap id="PlatePageMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.PlatePage">


        <result property="plateId" column="plateId"/>
        <result property="area" column="plateArea"/>


    </resultMap>


    <select id="selectOrderGoodsPlateList" resultMap="GoodsListMap" parameterType="java.lang.Long">



        select distinct
        og.plan_id as planId,
        og.material as material,
        og.width as width,
        og.height as height,
        og.thickness as thickness ,
        og.color as color,

        og.goods_id as goodsId,
        og.order_id as orderId,
        og.id as id,

        op.id as plateId,
        op.area as plateArea

        from order_goods og

        left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id and og.deleted = op.deleted

        where og.organ_id = #{organId}
        and og.deleted = false
        and og.plan_id  in
        <foreach collection="planIds" item="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>
--         and op.is_optimized = true




    </select>



    <select id="selectPlanGoodsListByPlanId"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.PlateResList">

        select distinct op.id as plateId,
                        op.order_id,
                        op.plate_no ,
                        op.name as plateName,
                        op.is_special_shaped,
                        op.is_sculpt,
                        op.area,
                        op.seal_left,
                        op.seal_right,
                        op.seal_up,
                        op.seal_down,
                        og.material,
                        og.color,
                        op.width,
                        op.height,
                        op.thickness,
                        oi.room_id,
                        oi.body_id

        from order_goods og
                 left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id and og.deleted = op.deleted
                 left join order_item oi on op.organ_id = oi.organ_id and op.order_id = oi.order_id and op.id = oi.plate_id

            ${ew.customSqlSegment}


    </select>


    <select id="selectGoodsAndRemainPlate"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.GoodsReqVO">

        select
            og.*

        from order_goods og

        where og.organ_id = #{organId}
          and og.deleted = false
          and og.order_id in
        <foreach collection="orderIds" item="orderIds" open="(" close=")" separator=",">
            #{orderIds}
        </foreach>
        and og.goods_id in
        <foreach collection="goodsIdList" item="goodsIdList" open="(" close=")" separator=",">
            #{goodsIdList}
        </foreach>


    </select>




    <select id="selectPlateListByPlanId"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.PlateResList" parameterType="java.lang.Long">


        select distinct op.id as plateId,
                        op.order_id,
                        op.plate_no ,
                        op.name as plateName,
                        op.is_special_shaped,
                        op.is_sculpt,
                        op.area,
                        op.seal_left,
                        op.seal_right,
                        op.seal_up,
                        op.seal_down,
                        og.material,
                        og.color,
                        op.width,
                        op.height,
                        op.thickness,
                        oi.room_id,
                        oi.body_id

        from order_goods og
                 left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id and og.deleted = op.deleted
                 left join order_item oi on op.organ_id = oi.organ_id and op.order_id = oi.order_id and op.id = oi.plate_id

        where  og.organ_id = #{organId}
                and og.deleted = false
                and og.plan_id in
        <foreach collection="planIds" item="planIds" open="(" close=")" separator=",">
            #{planIds}
        </foreach>


    </select>


    <resultMap id="RoomBodyListMap" type="com.cf.imes.module.executor.controller.admin.plan.bo.OrderRoomBodyList">

        <result property="orderId" column="orderId"/>

        <collection property="orderRoomIds" ofType="com.cf.imes.module.executor.controller.admin.plan.bo.OrderRoomIds" resultMap="OrderRoomIdsMap"/>

    </resultMap>


    <resultMap id="OrderRoomIdsMap" type="com.cf.imes.module.executor.controller.admin.plan.bo.OrderRoomIds">

        <result property="roomId" column="roomId"/>
        <result property="roomName" column="roomName"/>

        <collection property="orderBodyIds" ofType="com.cf.imes.module.executor.controller.admin.plan.bo.OrderBodyIds" resultMap="OrderBodyIdsMap"/>

    </resultMap>


    <resultMap id="OrderBodyIdsMap" type="com.cf.imes.module.executor.controller.admin.plan.bo.OrderBodyIds">

        <result property="bodyId" column="bodyId"/>
        <result property="bodyName" column="bodyName"/>

    </resultMap>


    <select id="selectNoPlanRoomBody"
            resultMap="RoomBodyListMap"
            parameterType="java.lang.Long">


        select distinct ob.order_id as orderId,
                        ob.id as bodyId,
                        ob.name as bodyName,
                        ob.room_id as roomId,
                        ob.room_name as roomName

        from order_body ob

        where ob.organ_id = #{organId}
            and ob.order_id in
        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>

          and ob.deleted = false
    </select>


    <select id="selectPlanPlateList" resultType="java.lang.Long">


        select distinct
            op.id
        from order_goods og
                 left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id and og.deleted = op.deleted

        where og.organ_id = #{organId} and og.deleted = false and og.plan_id = #{planId}





    </select>


    <select id="selectPlanBodyIds" resultType="java.lang.Long">


        select distinct
            oi.body_id

        from order_goods og
                 left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id
                 left join order_item oi on op.organ_id = oi.organ_id and op.order_id = oi.order_id and op.id = oi.plate_id

        where og.organ_id = #{organId} and og.deleted = false and og.plan_id = #{planId}



    </select>


    <select id="selectPlanGroupListDataSource" resultType="java.lang.Long">


        select distinct
            oi.group_id

        from order_goods og
                 left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id
                 left join order_item oi on op.organ_id = oi.organ_id and op.order_id = oi.order_id and op.id = oi.plate_id and oi.group_id !=0

        where og.organ_id  = #{organId} and og.deleted = false and og.plan_id = #{planId}



    </select>

    <select id="selectNoPlanGoodsList" resultType="com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO">

        select *
        from order_goods
                 where organ_id = #{organId}
                   and deleted = false
                   and order_id = #{orderId}
                   and id in
        <foreach item="goodsIds" collection="goodsIds" open="(" separator="," close=")">
            #{goodsIds}
        </foreach>
                   and plan_id !=0



    </select>


    <select id="selectGoodsId" resultType="com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO">

        select id,thickness

        from order_goods

        where organ_id = #{organId}
          and deleted = false
          and order_id = #{orderId}
          and goods_id = #{goodsId}
--           and plan_id !=0
        limit 1;


    </select>


    <select id="selectNoPlanOrdersList" resultType="java.lang.Long">

        select distinct order_id
        from order_goods
        where organ_id = #{organId}
          and deleted = false
          and order_id in
        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>
          and plan_id !=0;



    </select>

    <select id="selectNoPlanOrders" resultType="java.lang.Long">

        select distinct plan_id
        from order_goods
        where organ_id = #{organId}
        and deleted = false
        and order_id = #{orderId};



    </select>
    <select id="selectOrderPlanList" resultType="java.lang.Long">

        select distinct og.order_id

        from order_goods og
                 left join order_plan op on og.organ_id = op.organ_id and og.deleted = op.deleted and og.plan_id = op.id

        where og.organ_id = #{organId}
          and og.deleted = false
          and og.order_id in
        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>
          and op.status in (0,1);

    </select>


    <select id="selectPlanOrderListByOrderIds" resultType="java.lang.Long">

        select distinct og.order_id

        from order_goods og
        left join order_plan op on og.organ_id = op.organ_id and og.deleted = op.deleted and og.plan_id = op.id

        where og.organ_id = #{organId}
        and og.deleted = false
        and og.order_id in
        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>
        and op.status in (1,2)


    </select>


    <select id="selectListByGoodsIdList" resultType="com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO">

        select distinct goods_id,
                        goods_name,
                        material,
                        color,
                        width,
                        height,
                        thickness,
                        texture,
                        brand,
                        spec
        from order_goods

        where organ_id = #{organId}
          and deleted = false
          and plan_id = 0

          and goods_id in
        <foreach item="goodsId" collection="goodsId" open="(" separator="," close=")">
            #{goodsId}
        </foreach>


    </select>

    <select id="selectPlateGoodIds"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsPlateIds">

        select distinct
            og.id as goodsId,
            og.order_id as orderId,
            og.goods_id as actualGoodsId,
            op.id as id
        from order_goods og
            left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.deleted = op.deleted and og.id = op.goods_id
        where og.organ_id = #{organId}
          and og.deleted = false
          and og.order_id in
        <foreach collection="orderIds" item="orderIds" open="(" close=")" separator=",">
            #{orderIds}
        </foreach>

    </select>


    <select id="selectInsertGoodsIds" resultType="java.lang.Long">


        select distinct
            og.id
        from order_goods og
                 left join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id
        where og.organ_id = #{organId}

          and og.order_id in
        <foreach collection="orderIds" item="orderIds" open="(" close=")" separator=",">
            #{orderIds}
        </foreach>

          and og.id in
        <foreach collection="goodsIds" item="goodsIds" open="(" close=")" separator=",">
            #{goodsIds}
        </foreach>

          and og.raw_goods_id = '0'
          and op.id is null;




    </select>

    <select id="selectNoOrderPlanGoods"
            resultType="com.cf.imes.module.executor.controller.admin.goods.vo.GoodsRespVO">



        select distinct
            goods_name,
            material,
            color,
            thickness,
            brand

        from order_goods
        where organ_id = #{organId}
          and deleted = false
          and id in
        <foreach collection="ids" item="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>


    </select>

    <select id="selectPlannedPlateNumAndArea" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlanPlateNumAndAreaRespVO">
        select count(op.id) as num, IFNULL(sum(op.area), 0) as area
        from order_goods og
                 join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id
        where og.id = #{orderGoodsId}
        and op.plan_id != 0
        and op.deleted = false
        and og.deleted = false
    </select>

    <select id="selectPlannedPlateNumAndAreaGroup" resultType="com.cf.imes.module.executor.dal.dataobject.goods.OrderGoodsPlateNumAndAreaDO">
        select op.goods_id as orderGoodsId, count(op.id) as num, IFNULL(sum(op.area), 0) as area
        from order_goods og
        join order_plate op on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = op.goods_id
        where og.id in
        <foreach collection="orderGoodsIds" item="orderGoodsId" open="(" separator="," close=")">
            #{orderGoodsId}
        </foreach>
        and op.plan_id != 0
        and op.deleted = false
        and og.deleted = false
        group by op.goods_id
    </select>

    <select id="selectPlateNumAndArea" resultType="com.cf.imes.module.executor.dal.dataobject.goods.OrderGoodsPlateNumAndAreaDO">
        SELECT
            op.goods_id as order_goods_id,
            COUNT(*) AS num,
            IFNULL(SUM(op.area),0) AS area
        FROM order_plate op
        WHERE op.goods_id IN
        <foreach collection="orderGoodsIds" item="orderGoodsId" open="(" close=")" separator=",">
            #{orderGoodsId}
        </foreach>
        AND op.deleted = #{delete}
        GROUP BY op.goods_id
    </select>

    <select id="getParentOrderIdsByGoodsIds" resultType="java.lang.Long">
        SELECT DISTINCT o.id
        FROM order_goods og
        JOIN orders o
        ON o.id = og.order_id

        WHERE og.plate_num - og.planned_plate_num > 0

        <if test="goodsIds != null and goodsIds.size() > 0">
            AND og.goods_id IN
            <foreach collection="goodsIds"
                     item="goodsId"
                     open="("
                     separator=","
                     close=")">
                #{goodsId}
            </foreach>
        </if>

        <if test="orderIds != null and orderIds.size() > 0">
            AND og.order_id IN
            <foreach collection="orderIds"
                     item="orderId"
                     open="("
                     separator=","
                     close=")">
                #{orderId}
            </foreach>
        </if>

    </select>

    <delete id="physicalDeleteByOrderIds">
        delete from order_goods
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


</mapper>