<?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/
     -->

    <resultMap id="map" type="com.cf.imes.module.executor.controller.admin.plan.vo.PlateOptimize">
        <result column="goods_name" property="goodsName"/>
        <result column="material" property="material"/>
        <result column="color" property="color"/>
        <result column="width" property="width"/>
        <result column="height" property="height"/>
        <result column="plateNum" property="plateNum"/>
        <collection property="plateDOList" javaType="java.util.List" ofType="com.cf.imes.module.executor.dal.dataobject.remainplaten.RemainPlateDO"/>
    </resultMap>

    <resultMap id="optimizeMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.OptimizeParamRespVO">
        <result column="plan_id" property="planId"/>
        <result column="plan_no" property="planNo"/>
        <result column="sort" property="sort"/>
        <result column="op_type" property="type"/>
        <result column="op_status" property="status"/>
        <result column="machine_id" property="machineId"/>
        <result column="plan_time" property="planTime"/>
        <result column="op_remark" property="remark"/>
        <result column="produce_time" property="produceTime" />
        <result column="operator" property="operator"/>
        <collection property="plateList" javaType="java.util.List" ofType="com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO" >
            <result column="plateId" property="id"/>
            <result column="plan_no" property="plateNo"/>
            <result column="opl_order_id" property="orderId"/>
            <result column="opl_type" property="type"/>
            <result column="opl_name" property="name"/>
            <result column="opl_goods_id" property="goodsId"/>
            <result column="opl_width" property="width"/>
            <result column="opl_height" property="height"/>
            <result column="opl_thickness" property="thickness"/>
            <result column="split_width" property="splitWidth"/>
            <result column="split_height" property="splitHeight"/>
            <result column="split_thickness" property="splitThickness"/>
            <result property="sealLeft" column="seal_left"/>
            <result property="sealRight" column="seal_right"/>
            <result property="sealUp" column="seal_up"/>
            <result property="sealDown" column="seal_down"/>
            <result property="area" column="area"/>
            <result property="texture" column="texture"/>
            <result property="holeFace" column="hole_face"/>
            <result property="holeArrange" column="hole_arrange"/>
            <result property="unregularPointCount" column="unregular_point_count"/>
            <result property="frontHoleCount" column="front_hole_count"/>
            <result property="backHoleCount" column="back_hole_count"/>
            <result property="sideHoleCount" column="side_hole_count"/>
            <result property="frontModelCount" column="front_model_count"/>
            <result property="backModelCount" column="back_model_count"/>
            <result property="isDoor" column="is_door"/>
            <result property="openDoorType" column="open_door_type"/>
            <result property="offsetX" column="offset_x"/>
            <result property="offsetY" column="offset_y"/>
            <result property="isArcAcross" column="is_arc_across"/>
            <result property="moduleTypeId" column="module_type_id"/>
            <result property="filterType" column="filter_type"/>
            <result property="isCancel" column="is_cancel"/>
            <result property="remark" column="opl_remark"/>
            <result property="createTime" column="opl_create_time"/>
        </collection>
        <collection property="orderList" javaType="java.util.List" ofType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderResp">
            <result property="orderId" column="o_id"/>
            <result property="type" column="o_type"/>
            <result property="status" column="o_status"/>
            <result property="dataType" column="data_type"/>
            <result property="customOrderNo" column="custom_order_no"/>
            <result column="customer" property="customer"/>
            <result column="address" property="address"/>
            <result column="phone_number" property="phoneNumber"/>
            <result column="dealer" property="dealer"/>
            <result column="dealer_phone_number" property="dealerPhoneNumber"/>
            <result column="salesman" property="salesman"/>
            <result column="splitter" property="splitter"/>
            <result column="o_status" property="status"/>
            <result column="delivery_date" property="deliveryDate"/>
            <result column="o_remark" property="remark"/>
        </collection>

    </resultMap>


    <select id="getOptimizePlanParam" resultMap="optimizeMap">
        select op.id as plan_id, op.plan_no, op.type as op_type, op.status as op_status, op.machine_id, op.plan_time, op.produce_time, op.remark as op_remark, op.operator,
               oi.order_id, opl.id as plateId, opl.order_id opl_order_id,  opl.name opl_name, opl.plate_no, opl.type as opl_type, opl.goods_id as opl_goods_id, opl.width opl_width,
               opl.height opl_height, opl.thickness as opl_thickness, opl.split_width, opl.split_height, opl.split_thickness, opl.seal_left, opl.seal_right,
               opl.seal_up, opl.seal_down, opl.area, opl.texture, opl.hole_face, opl.hole_arrange, opl.unregular_point_count, opl.front_hole_count,
               opl.back_hole_count, opl.side_hole_count, opl.front_model_count, opl.back_model_count, opl.is_door, opl.open_door_type, opl.offset_x, opl.offset_y,
               opl.is_arc_across, opl.module_type_id, opl.filter_type, opl.remark opl_remark, opl.is_cancel, opl.create_time as opl_create_time,
               o.id as o_id, o.type as o_type, o.data_type, o.status o_status, o.custom_order_no, o.customer,
               o.address, o.phone_number, o.dealer, o.dealer_phone_number, o.salesman, o.splitter, o.remark as o_remark, o.delivery_date
        from order_plan op
                 left JOIN order_plan_item opi on op.id = opi.plan_id
                 left join order_item oi on opi.item_id = oi.id
                 left join order_plate opl on oi.plate_id = opl.id
                 left join `orders` o on oi.order_id = o.id
        where op.id = #{planId}

    </select>

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

        select 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>

</mapper>