<?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.manage.dal.mysql.query.OrderPartsMapper">

    <resultMap id="partsMap" type="com.cf.imes.module.manage.controller.admin.query.vo.pack.PackageDetailsRespVO">

        <result property="partsName" column="name"/>
        <result property="model" column="model"/>
        <result property="spec" column="spec"/>
        <result property="num" column="num"/>
        <result property="unit" column="unit"/>
        <result property="brand" column="brand"/>
        <result property="factory" column="factory"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <select id="selectParts"
            parameterType="java.lang.Long"
            resultMap="partsMap">


        select distinct
            op.name as name,
            op.model as model,
            op.spec as spec,
            op.unit as unit,
            op.brand as brand,
            op.material as material,
            op.factory as factory,
            oi.num as num

        from order_parts op
                 join order_item oi on op.id = oi.parts_id and op.organ_id = oi.organ_id

        where oi.package_id =#{packageId}  and oi.organ_id = #{organId}


    </select>





    <resultMap id="PartsListMap" type="com.cf.imes.module.manage.controller.admin.pack.vo.OrderPartsRespVO">

        <result property="partsId" column="partsId"/>
        <result property="type" column="type"/>
        <result property="isComposite" column="isComposite"/>

        <result property="name" column="name"/>
        <result property="model" column="model"/>
        <result property="spec" column="spec"/>
        <result property="num" column="num"/>
        <result property="unit" column="unit"/>
        <result property="brand" column="brand"/>
        <result property="factory" column="factory"/>
        <result property="remark" column="remark"/>
    </resultMap>
    <select id="selectPartsList"
            parameterType="java.lang.Long"
            resultMap="PartsListMap">

        select
            id as partsId,name,model,spec,count(name) as num,unit,brand,factory,remark
        from order_parts where order_id = #{orderId} and organ_id = #{organId}
        group by partsId,name,model,spec,unit,brand,factory,remark;


    </select>





    <select id="selectOrderParts"
            parameterType="java.lang.Long"
            resultMap="PartsListMap">


        select
            op.id as partsId,
            op.name as name,
            op.model as model ,
            op.spec as spec,
            oi.num as num,
            op.unit as unit,
            op.brand as brand,
            op.factory as factory,
            op.type as type,
            op.is_composite as isComposite,
            op.remark as remark

        from order_parts op
                 join order_item oi on op.id = oi.parts_id
        where op.order_id = #{orderNo} and oi.package_id =#{packageNo};



    </select>



    <update id="deleteOrderParts"
            parameterType="java.lang.Long">

        update order_parts op set op.deleted = true where op.id in (
            select oi.parts_id  from order_item oi where package_id =#{packageId});


    </update>


    <update id="updateParts"
            parameterType="java.lang.Long">

        UPDATE order_parts op
        SET op.deleted = true
        WHERE op.id = #{partsId};

    </update>


</mapper>