<?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.yiqi.order.mapper.OrderPurchaseGoodsMapper">

    <resultMap type="OrderPurchaseGoods" id="OrderPurchaseGoodsResult">
        <result property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="sourceType" column="source_type"/>
        <result property="orgId" column="org_id"/>
        <result property="goodsType" column="goods_type"/>
        <result property="costTotalPrice" column="cost_total_price"/>
        <result property="orderStatus" column="order_status"/>
        <result property="parentSourceType" column="parent_source_type"/>
        <result property="parentOrgId" column="parent_org_id"/>
        <result property="payTime" column="pay_time"/>
        <result property="sendTime" column="send_time"/>
        <result property="finishTime" column="finish_time"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="refundReason" column="refund_reason"/>
        <result property="refundTime" column="refund_time"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="payType" column="pay_type"/>
        <result property="outTradeOrderNo" column="out_trade_order_no"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectOrderPurchaseGoodsVo">
        select id, order_no, source_type, org_id, goods_type, cost_total_price, order_status, parent_source_type, parent_org_id, pay_time, send_time, finish_time, refund_status, refund_reason, refund_time, refund_amount, pay_type, out_trade_order_no, del_flag, create_by_id, create_by, create_time, update_by_id, update_by, update_time, remark
        from order_purchase_goods
    </sql>

    <select id="selectOrderPurchaseGoodsList" parameterType="OrderPurchaseGoods" resultMap="OrderPurchaseGoodsResult">
        <include refid="selectOrderPurchaseGoodsVo"/>
        <where>
            and del_flag='0'
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="sourceType != null  and sourceType != ''">
                and source_type = #{sourceType}
            </if>
            <if test="orgId != null ">
                and org_id = #{orgId}
            </if>
            <if test="goodsType != null  and goodsType != ''">
                and goods_type = #{goodsType}
            </if>
            <if test="costTotalPrice != null ">
                and cost_total_price = #{costTotalPrice}
            </if>
            <if test="orderStatus != null  and orderStatus != ''">
                and order_status = #{orderStatus}
            </if>
            <if test="parentSourceType != null  and parentSourceType != ''">
                and parent_source_type = #{parentSourceType}
            </if>
            <if test="parentOrgId != null ">
                and parent_org_id = #{parentOrgId}
            </if>
            <if test="payTime != null ">
                and pay_time = #{payTime}
            </if>
            <if test="sendTime != null ">
                and send_time = #{sendTime}
            </if>
            <if test="finishTime != null ">
                and finish_time = #{finishTime}
            </if>
            <if test="refundStatus != null  and refundStatus != ''">
                and refund_status = #{refundStatus}
            </if>
            <if test="refundReason != null  and refundReason != ''">
                and refund_reason = #{refundReason}
            </if>
            <if test="refundTime != null ">
                and refund_time = #{refundTime}
            </if>
            <if test="refundAmount != null ">
                and refund_amount = #{refundAmount}
            </if>
            <if test="payType != null  and payType != ''">
                and pay_type = #{payType}
            </if>
            <if test="outTradeOrderNo != null  and outTradeOrderNo != ''">
                and out_trade_order_no = #{outTradeOrderNo}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectOrderPurchaseGoodsById" parameterType="Long"
            resultType="com.yiqi.order.domain.vo.OrderPurchaseGoodsVO">
        select c.goods_category_name as goodsCategoryName,
               e.goods_name as goodsName, a.sku_name as skuName, a.buy_num as buyNum,
               a.cost_price as costPrice, b.cost_total_price as costTotalPrice
        from order_purchase_goods_sku a
                 left join order_purchase_goods b on a.order_id = b.id
                 left join order_purchase_goods_item c on a.order_id = a.order_id and a.order_purchase_goods_item_id = c.id
                 left join goods_info e on e.id = a.goods_id
        where a.order_id = #{id}
    </select>

    <select id="sellGoodsListByFactory" resultType="com.yiqi.order.domain.vo.OrderSellGoodsListVO">
        select a.id as id , a.order_no as orderNo, a.source_type as sourceType, a.org_id as orgId,
               a.goods_type as goodsType, a.delivery_way as deliveryWay, a.delivery_company as deliveryCompany,
               a.cost_total_price as costTotalPrice, a.order_status as orderStatus, a.pay_time as payTime, a.send_time as sendTime,
               a.finish_time as finishTime, a.pay_type as payType, a.create_time as createTime, b.name as orgName,
        b.province_name as province , b.city_name as city, b.area_name as area, b.address, b.address_detail as addressDetail
        from order_purchase_goods a
        left join sys_store b on a.org_id = b.id

        where a.del_flag='0'
        <if test="orderNo != null  and orderNo != ''">
            and a.order_no = #{orderNo}
        </if>
        <if test="parentSourceType != null  and parentSourceType != ''">
            and a.parent_source_type = #{parentSourceType}
        </if>
        <if test="parentOrgId != null ">
            and a.parent_org_id = #{parentOrgId}
        </if>
        <if test="sourceType != null  and sourceType != ''">
            and a.source_type = #{sourceType}
        </if>
        <if test="orgId != null ">
            and a.org_id = #{orgId}
        </if>
        <if test="goodsType != null  and goodsType != ''">
            and a.goods_type = #{goodsType}
        </if>
        <if test="payType != null  and payType != ''">
            and a.pay_type = #{payType}
        </if>
        <if test="orderStatus != null  and orderStatus != ''">
            and a.order_status = #{orderStatus}
        </if>
        order by a.create_time desc
    </select>
    <select id="sellGoodsListByHq" resultType="com.yiqi.order.domain.vo.OrderSellGoodsListVO">
        select a.id as id , a.order_no as orderNo, a.source_type as sourceType, a.org_id as orgId,
               a.goods_type as goodsType, a.delivery_way as deliveryWay, a.delivery_company as deliveryCompany,
        a.cost_total_price as costTotalPrice, a.order_status as orderStatus, a.pay_time as payTime, a.send_time as sendTime,
        a.finish_time as finishTime, a.pay_type as payType, a.create_time as createTime, b.factory_name as orgName,
        b.province , b.city, b.area, b.address, b.detail_address as addressDetail
        from order_purchase_goods a
        left join manage_factory b on a.org_id = b.id
        where a.del_flag='0'
        <if test="orderNo != null  and orderNo != ''">
            and a.order_no = #{orderNo}
        </if>
        <if test="parentSourceType != null  and parentSourceType != ''">
            and a.parent_source_type = #{parentSourceType}
        </if>
        <if test="parentOrgId != null ">
            and a.parent_org_id = #{parentOrgId}
        </if>
        <if test="sourceType != null  and sourceType != ''">
            and a.source_type = #{sourceType}
        </if>
        <if test="orgId != null ">
            and a.org_id = #{orgId}
        </if>
        <if test="goodsType != null  and goodsType != ''">
            and a.goods_type = #{goodsType}
        </if>
        <if test="payType != null  and payType != ''">
            and a.pay_type = #{payType}
        </if>
        <if test="orderStatus != null  and orderStatus != ''">
            and a.order_status = #{orderStatus}
        </if>
        order by a.create_time desc
    </select>

    <insert id="insertOrderPurchaseGoods" parameterType="OrderPurchaseGoods" useGeneratedKeys="true"
            keyProperty="id">
        insert into order_purchase_goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,
            </if>
            <if test="sourceType != null">source_type,
            </if>
            <if test="orgId != null">org_id,
            </if>
            <if test="goodsType != null">goods_type,
            </if>
            <if test="costTotalPrice != null">cost_total_price,
            </if>
            <if test="orderStatus != null">order_status,
            </if>
            <if test="parentSourceType != null">parent_source_type,
            </if>
            <if test="parentOrgId != null">parent_org_id,
            </if>
            <if test="payTime != null">pay_time,
            </if>
            <if test="sendTime != null">send_time,
            </if>
            <if test="finishTime != null">finish_time,
            </if>
            <if test="refundStatus != null">refund_status,
            </if>
            <if test="refundReason != null">refund_reason,
            </if>
            <if test="refundTime != null">refund_time,
            </if>
            <if test="refundAmount != null">refund_amount,
            </if>
            <if test="payType != null">pay_type,
            </if>
            <if test="outTradeOrderNo != null">out_trade_order_no,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">#{orderNo},
            </if>
            <if test="sourceType != null">#{sourceType},
            </if>
            <if test="orgId != null">#{orgId},
            </if>
            <if test="goodsType != null">#{goodsType},
            </if>
            <if test="costTotalPrice != null">#{costTotalPrice},
            </if>
            <if test="orderStatus != null">#{orderStatus},
            </if>
            <if test="parentSourceType != null">#{parentSourceType},
            </if>
            <if test="parentOrgId != null">#{parentOrgId},
            </if>
            <if test="payTime != null">#{payTime},
            </if>
            <if test="sendTime != null">#{sendTime},
            </if>
            <if test="finishTime != null">#{finishTime},
            </if>
            <if test="refundStatus != null">#{refundStatus},
            </if>
            <if test="refundReason != null">#{refundReason},
            </if>
            <if test="refundTime != null">#{refundTime},
            </if>
            <if test="refundAmount != null">#{refundAmount},
            </if>
            <if test="payType != null">#{payType},
            </if>
            <if test="outTradeOrderNo != null">#{outTradeOrderNo},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
        </trim>
    </insert>

    <update id="updateOrderPurchaseGoods" parameterType="OrderPurchaseGoods">
        update order_purchase_goods
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null">order_no =
                #{orderNo},
            </if>
            <if test="sourceType != null">source_type =
                #{sourceType},
            </if>
            <if test="orgId != null">org_id =
                #{orgId},
            </if>
            <if test="goodsType != null">goods_type =
                #{goodsType},
            </if>
            <if test="costTotalPrice != null">cost_total_price =
                #{costTotalPrice},
            </if>
            <if test="orderStatus != null">order_status =
                #{orderStatus},
            </if>
            <if test="parentSourceType != null">parent_source_type =
                #{parentSourceType},
            </if>
            <if test="parentOrgId != null">parent_org_id =
                #{parentOrgId},
            </if>
            <if test="payTime != null">pay_time =
                #{payTime},
            </if>
            <if test="sendTime != null">send_time =
                #{sendTime},
            </if>
            <if test="finishTime != null">finish_time =
                #{finishTime},
            </if>
            <if test="refundStatus != null">refund_status =
                #{refundStatus},
            </if>
            <if test="refundReason != null">refund_reason =
                #{refundReason},
            </if>
            <if test="refundTime != null">refund_time =
                #{refundTime},
            </if>
            <if test="refundAmount != null">refund_amount =
                #{refundAmount},
            </if>
            <if test="payType != null">pay_type =
                #{payType},
            </if>
            <if test="outTradeOrderNo != null">out_trade_order_no =
                #{outTradeOrderNo},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderPurchaseGoodsById" parameterType="Long">
        update order_purchase_goods set del_flag='2' where id = #{id}
    </delete>

    <delete id="deleteOrderPurchaseGoodsByIds" parameterType="String">
        update order_purchase_goods set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>