<?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.xxfc.platform.vehicle.mapper.VehiclePublishReceiveMapper">


    <select id="selectList"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehiclePublishReceiveVo" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehiclePublishReceiveDTO">
        SELECT r.*,
        b.cn_name as brandName,
        c.`name` categoryName,
        bc.`name` as receiveCompanyName,
        bc1.`name` as publishCompanyName,
        g.`publish_id` as publishId,
        g.`receive_amount` as receiveAmount,
        g.`publish_amount` as publishAmount
        FROM vehicle_publish_receive r
        LEFT JOIN vehicle_brand b ON r.brand_id=b.id
        LEFT JOIN vehicle_category c ON r.category_id=c.id
        LEFT JOIN branch_company bc ON r.receive_company_id = bc.id
        LEFT JOIN branch_company bc1 ON r.publish_company_id = bc1.id
        LEFT JOIN vehicle_publish_goods g ON r.publish_goods_id=g.id
        <where>
              r.is_del = 0
            <if test="publishId != null and publishId > 0">
                AND  g.`publish_id`= #{publishId}
            </if>
            <if test="orderNo != null and orderNo != '' ">
                AND  r.`receive_order_no`= #{orderNo}
            </if>
            <if test="ids != null and ids.size > 0">
                and r.id  in
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by r.id DESC
    </select>

    <update id="updReceiveById" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehiclePublishReceiveDTO">
        UPDATE vehicle_publish_receive SET
        <choose>
            <when test="type != null and type == 1">
                receive_order_no = #{orderNo},receive_time=#{nowTime}
            </when>
            <otherwise>
                publish_order_no = #{orderNo},ack_time=#{nowTime}
            </otherwise>
        </choose>
         WHERE
         id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <select id="getOrderNoStr" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehiclePublishReceiveDTO" resultType="java.lang.String">
       SELECT GROUP_CONCAT(distinct receive_order_no) FROM vehicle_publish_receive
       <where>
           <if test="publishOrderNo != null and publishOrderNo != '' ">
               and publish_order_no =#{publishOrderNo}
           </if>
           <if test="ids != null and ids.size > 0">
               and  id  in
               <foreach collection="ids" item="id" open="(" separator="," close=")">
                   #{id}
               </foreach>
           </if>
       </where>
    </select>
    
    <select id="getListByUserId" resultType="com.xxfc.platform.vehicle.pojo.vo.VehiclePublishReceiveVo" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehiclePublishFindDTO">
        SELECT
        COUNT(id) as number,
        r.receive_user_id as receiveUserId,
        r.receive_time as receiveTime,
        GROUP_CONCAT(r.`status`) as statusStr,
        GROUP_CONCAT(r.id) as ids
        FROM  vehicle_publish_receive r
        WHERE r.publish_goods_id = #{publishGoodsId} 
        <choose>
            <when test="receivetype != null and receivetype == 1">
                AND r.`status` in (1,3,5)
            </when>
            <when test="receivetype != null and receivetype == 2">
                AND r.`status` in (1,2,3,5)
            </when>
        </choose>

        GROUP BY r.receive_user_id
    </select>



    <select id="selectListByReceive"  resultType="com.xxfc.platform.vehicle.pojo.vo.ReceiveVo" parameterType="com.xxfc.platform.vehicle.pojo.dto.VehiclePublishReceiveDTO">
        SELECT
        r.receive_order_no as receiveOrderNo,
        COUNT(r.id) as number,
        r.receive_company_id as receiveCompanyId,
        r.crt_time as crtTime,
        p.start_time as startTime,
        p.end_time as endTime,
        p.id as publishId,
        p.type,
        GROUP_CONCAT(r.`status`) statusStr,
        GROUP_CONCAT(r.`id`) ids,
        r.cover,
        r.publish_company_id as publishCompanyId,
        IFNULL(SUM(g.receive_amount),0) as payAmount,
        IFNULL(g.receive_amount,0) as goodsAmount
        FROM vehicle_publish_receive r
        LEFT JOIN vehicle_publish_goods g ON r.publish_goods_id = g.id
        LEFT JOIN vehicle_publish p ON g.publish_id = p.id
        <where>
            r.is_del = 0
            <if test="companyId != null and companyId > 0">
                AND  r.receive_company_id= #{companyId}
            </if>
            <if test="orderNo != null and orderNo != '' ">
                AND  r.receive_order_no= #{orderNo}
            </if>
        </where>
        GROUP BY r.receive_order_no
        ORDER BY r.crt_time DESC
    </select>


    <select id="getListByNumber"  resultType="com.xxfc.platform.vehicle.pojo.vo.VehiclePublishGoodsVo">
        SELECT
        COUNT(r.id)as goodsNumber,
        g.publish_id as publishId,
        IFNULL(b1.cn_name,b.cn_name) as brandName,
        IFNULL(c1.`name`,c.`name`) as categoryName
        FROM vehicle_publish_receive r
        LEFT JOIN vehicle_publish_goods g ON r.publish_goods_id = g.id
        LEFT JOIN vehicle v ON g.vehicle_id=v.id
        LEFT JOIN vehicle_brand b1 ON v.brand_id=b1.id
        LEFT JOIN vehicle_category c1 ON v.category_id=c1.id
        LEFT JOIN vehicle_brand b ON g.brand_id=b.id
        LEFT JOIN vehicle_category c ON g.category_id=c.id
        <where>
            r.is_del = 0  and r.receive_order_no=#{receiveOrderNo}
        </where>
        GROUP BY r.publish_goods_id
        ORDER BY goodsNumber DESC
    </select>

</mapper>