<?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.plan.PlanMapper">

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



    <select id="selectPlanPageList" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">

        select distinct op.*
        from order_plan op
                join order_plate opt on op.organ_id = opt.organ_id and op.id = opt.plan_id
                 left join orders o on op.organ_id = o.organ_id and opt.order_id = o.id

                 ${ew.customSqlSegment}

        ORDER BY op.is_top DESC,
            CASE WHEN op.is_top = true THEN op.update_time END DESC,
         CASE WHEN op.is_top = false THEN op.sort END ;
    </select>



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

        select distinct a.id,  a.goods_name, a.material, a.color, a.width, a.height, a.thickness, count(b.id) as plateNum, d.*
        from order_goods a
                 left join order_plate b on a.id = b.goods_id and a.organ_id = b.organ_id
                 left join order_item c on c.order_id = b.order_id and c.organ_id = b.organ_id
                 left join order_remain_plate d on a.id = d.goods_id  and d.plan_id = c.plan_id and a.organ_id = d.organ_id
        where c.plan_id = #{planId} and c.organ_id = #{organId}
        GROUP BY a.id,  a.goods_name, a.material, a.color, a.width, a.height, a.thickness,d.id

    </select>

    <select id="selectPlanCountGroupByCreateTime"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderPlanStatisticsCountRespVO">
        select
        count(p.id) as count
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', QUARTER(p.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', MONTH(p.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', MONTH(p.create_time), '-', FLOOR((DayOfMonth(p.create_time)-1)/7)+1) AS date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', MONTH(p.create_time), '-', DAY(p.create_time)) as date
        </if>
        from order_plan p
        where p.create_time between #{req.createTime[0]} and #{req.createTime[1]}
        and p.deleted = 0
        group by date;
    </select>

    <select id="selectPlanAreaGroupByCreateTime"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderPlanStatisticsAreaRespVO">
    select sum(op.area) areaSum
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', QUARTER(p.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', MONTH(p.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', MONTH(p.create_time), '-', FLOOR((DayOfMonth(p.create_time)-1)/7)+1) AS date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(p.create_time), '-', MONTH(p.create_time), '-', DAY(p.create_time)) as date
        </if>
        from order_plan p
        join order_goods g on g.plan_id = p.id
        join order_plate op on op.goods_id = g.id
        where p.create_time between #{req.createTime[0]} and #{req.createTime[1]}
        and p.deleted = 0
        group by date;
    </select>

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


        select ${filed}  from order_plan where organ_id = #{organId} and deleted = false and id = #{planId}


    </select>


    <select id="selectPlanIds" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">


        SELECT *
        FROM order_plan

        WHERE organ_id = #{organId}
          and deleted = false
          and is_top = false
          and status in (0,1)
          and sort &lt;  #{sort}
        ORDER BY sort desc
        limit 1;


    </select>


    <select id="selectPlanIdsFalse" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">

        SELECT *
        FROM order_plan

        WHERE organ_id = #{organId}
          AND deleted = false
          and is_top = false
          and status in (0,1)
          and sort &gt; #{sort}
        ORDER BY sort
        limit 1;




    </select>

    <select id="selectPlanStatus" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">

        select id
        from order_plan
        where organ_id = #{organId}
          and deleted = false
          and id in
        <foreach item="planIds" collection="planIds" open="(" separator="," close=")">
            #{planIds}
        </foreach>
          and status !=0;


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

        select id
        from order_plan
        where organ_id = #{organId}
        and deleted = false
        and id in
        <foreach item="planIds" collection="planIds" open="(" separator="," close=")">
            #{planIds}
        </foreach>
        and status in (0,1);


    </select>

    <update id="updatePlanStatus">

        UPDATE order_plan

        SET status = #{status},
            produce_time = null

        WHERE organ_id = #{organId}
          and deleted = false
          and id = #{planId}


    </update>


    <select id="selectSortOnePlan" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">


        SELECT *
        FROM order_plan
        WHERE organ_id = #{organId}
        AND deleted = false
        and is_top = false
        and sort &lt; #{sort}
        ORDER BY sort desc
        limit 1;


    </select>


    <select id="selectLastPlan" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">


        SELECT *
        FROM order_plan
        WHERE organ_id = #{organId}
          AND deleted = false
          and is_top = false
          and sort &gt; #{sort}
        ORDER BY sort
        limit 1;



    </select>


    <select id="selectPlanSortList" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">

        SELECT *
        FROM order_plan
        WHERE organ_id = #{organId}
          AND deleted = false
            and status in
        <foreach item="status" collection="status" open="(" separator="," close=")">
            #{status}
        </foreach>

        ORDER BY is_top DESC,
                 CASE WHEN is_top = true THEN update_time END DESC,
                 CASE WHEN is_top = false THEN sort END ;


    </select>


    <select id="selectIsTopUpPlan" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">

        select *
        from order_plan
        where organ_id = #{organId}
          and deleted = false
          and is_top = true
          and status in (0,1)
          and update_time &gt; #{updateTime}
        order by update_time
            limit 1;


    </select>


    <select id="selectIsTopDownPlan" resultType="com.cf.imes.module.executor.dal.dataobject.plan.PlanDO">

        select  *
        from order_plan
        where organ_id = #{organId}
          and deleted = false
          and status in (0,1)
        and update_time &lt;  #{updateTime}
        ORDER BY is_top DESC,
        CASE WHEN is_top = true THEN update_time END DESC,
        CASE WHEN is_top = false THEN sort END
            limit 1;



    </select>

    <select id="selectPlanGoodsList"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderGoodsResp">
        select distinct og.thickness as thickness,
                        og.material as material,
                        og.color as color,
                        og.brand as brand,
                        og.texture as texture
        from order_plate op
                 left join order_goods og on op.organ_id = og.organ_id and op.goods_id = og.id
        where op.organ_id = #{organId}
          and op.plan_id = #{planId};
    </select>

    <select id="countDeletePlanPlate" resultType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderPlanPlateStatisticsRespVO">
        select op.plan_id as planId,
               count(op.id) as count
        from order_plate op
        where
        op.organ_id = #{organId}
        and op.plan_id != 0
        and op.plan_id in
        <foreach collection="planIds" item="planId" open="(" close=")" separator=",">
            #{planId}
        </foreach>
        and op.order_id in
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
        and op.deleted = false
        group by op.plan_id;
    </select>

    <select id="selectPlanOrderIds" resultType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderPlanPlateStatisticsRespVO">
        select distinct op.plan_id, op.order_id
        from order_plate op
        where
        op.organ_id = #{organId}
        and op.plan_id != 0
        and op.plan_id in
        <foreach collection="planIds" item="planId" open="(" close=")" separator=",">
            #{planId}
        </foreach>
        and op.order_id in
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
        and op.deleted = false
    </select>

    <select id="selectPlanOrderGoodsId" resultType="java.lang.Long">
        select distinct og.id
        from order_plan p
        join order_plate  op on p.organ_id = op.organ_id and p.id = op.plan_id
        join order_goods og on p.organ_id = og.organ_id and p.goods_id = p.goods_id and op.order_id = og.order_id and op.goods_id = og.id
        where p.organ_id = #{organId} and p.id = #{planId}
        and op.order_id in
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
    </select>

</mapper>