<?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.tour.mapper.TourGoodSpePriceMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.xxfc.platform.tour.entity.TourGoodSpePrice" id="tourGoodSpePriceMap">
        <result property="id" column="id"/>
        <result property="goodId" column="good_id"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="speId" column="spe_id"/>
        <result property="price" column="price"/>
        <result property="chidPrice" column="chid_price"/>
        <result property="memberPrice" column="member_price"/>
        <result property="stock" column="stock"/>
        <result property="crtTime" column="crt_time"/>
        <result property="updTime" column="upd_time"/>
        <result property="isDel" column="is_del"/>
    </resultMap>

    <resultMap type="com.xxfc.platform.tour.vo.TourDepartTimeVo" id="tourDepartTimeVoMap">
        <result property="id" column="id"/>
        <result property="goodId" column="good_id"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="speId" column="spe_id"/>
        <result property="price" column="price"/>
        <result property="childPrice" column="child_price"/>
        <result property="memberPrice" column="member_price"/>
        <result property="stock" column="stock"/>
        <result property="startWeek" column="startWeek"/>
        <result property="endWeek" column="endWeek"/>
        <result property="overdue" column="overdue"/>
    </resultMap>

    <!-- 获取出行时间 -->
    <select id="getAllByGoodId"  resultMap="tourDepartTimeVoMap">
        SELECT id,good_id,DATE_FORMAT(start_time,'%Y-%m-%d') as start_time,DATE_FORMAT(end_time,'%Y-%m-%d') as end_time,spe_id,price,child_price,member_price,stock,
        DATE_FORMAT(start_time,'%w') as startWeek,DATE_FORMAT(end_time,'%w') as endWeek,IF(IFNULL(DATEDIFF(start_time,NOW()),0)>=#{days},'0','1') as overdue
        FROM tour_good_spe_price WHERE good_id=#{goodId} and is_del=0 ORDER BY start_time
    </select>

    <!-- 获取出行时间 -->
    <select id="getTotalStock" resultType="Integer" >
      SELECT IFNULL(SUM(stock),0) as stock FROM tour_good_spe_price WHERE good_id=#{goodId} and is_del=0 and IFNULL(DATEDIFF(start_time,NOW()),0)>0
    </select>

    <!-- 减库存-->
    <update id="updStockById">
        update  tour_good_spe_price set
        <if test="type==1">
            stock=stock-#{number}
        </if>
        <if test="type==2">
        stock=stock+#{number}
        </if>
         where  id=#{id}
    </update>

    <update id="delSpe">
        update  tour_good_spe_price set  is_del=1
        where 1=1
        <if test="goodId !=null and  goodId >0 ">
            and good_id=#{goodId}

        </if>
        <if test="list!=null ">
            and id not in (
            <trim suffixOverrides=",">
                <foreach collection="list" item="id">
                    #{id},
                </foreach>
            </trim>
            )
        </if>
    </update>



</mapper>