package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.Discount;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DiscountMapper {
    /**
     * 新增折扣率功能
     *
     * @param discount
     * @return
     */
    @Insert("INSERT INTO t_discount(name,dept_type,dept_no,discount_begin_time,discount_end_time,charge_item_codes,discount_type,discount_rate,discount_price,delete_flag,create_id,create_time,update_id,update_time) VALUES" +
            "(#{name},#{deptType},#{deptNo},#{discountBeginTime},#{discountEndTime},#{chargeItemCodes},#{discountType},#{discountRate,jdbcType=DECIMAL},#{discountPrice,jdbcType=DECIMAL},#{deleteFlag},#{createId},#{createTime},#{updateId},#{updateTime})")
    int insert(Discount discount);

    /**
     * 修改折扣率
     *
     * @param discount
     * @return
     */
    @Update({"<script>",
            "update t_discount ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id = #{id,jdbcType=BIGINT} ' >",
            "<when test='name!=null'>",
            ",name =#{name}",
            "</when>",
            "<when test='deleteFlag!=null'>",
            ",delete_flag =#{deleteFlag}",
            "</when>",
            "<when test='updateId!=null'>",
            ",update_id =#{updateId}",
            "</when>",
            "<when test='updateTime!=null'>",
            ",update_time =#{updateTime}",
            "</when>",
            "<when test='chargeItemCodes!=null'>",
            ",charge_item_codes =#{chargeItemCodes}",
            "</when>",
            "<when test='discountRate!=null'>",
            ",discount_rate =#{discountRate}",
            "</when>",
            "<when test='deptType!=null'>",
            ",dept_type =#{deptType}",
            "</when>",
            "<when test='deptNo!=null'>",
            ",dept_no =#{deptNo}",
            "</when>",
            "<when test='discountType!=null'>",
            ",discount_type =#{discountType}",
            "</when>",
            "<when test='discountPrice!=null'>",
            ",discount_price =#{discountPrice}",
            "</when>",
            "<when test='discountBeginTime!=null'>",
            ",discount_begin_time =#{discountBeginTime}",
            "</when>",
            "<when test='discountEndTime!=null'>",
            ",discount_end_time =#{discountEndTime}",
            "</when>",
            "</trim>",
            "</script>"})
    int update(Discount discount);


    /**
     * 分页查询
     *
     * @param discount
     * @return
     */
    @Select({"<script>" +
            "select a.id,name,dept_type,dept_no,discount_begin_time,discount_end_time,charge_item_codes,discount_type,discount_rate,discount_price,delete_flag,create_id,create_time,update_id,update_time from t_discount a join  (" +
            "select id from t_discount where 1=1" +
            "<when test='deleteFlag!=null'>",
            " and delete_flag =#{deleteFlag}",
            "</when>",
            "<when test='updateId!=null'>",
            " and update_id =#{updateId}",
            "</when>",
            "<when test='updateTime!=null'>",
            " and update_time =#{updateTime}",
            "</when>",
            "<when test='chargeItemCodes!=null'>",
            " and locate(#{chargeItemCodes},charge_item_codes) ",
            "</when>",
            "<when test='discountRate!=null'>",
            " and discount_rate =#{discountRate}",
            "</when>",
            "<when test='deptType!=null'>",
            " and dept_type =#{deptType}",
            "</when>",
            "<when test='deptNo!=null'>",
            " and dept_no =#{deptNo}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and (discount_begin_time &gt;=#{beginTime} or discount_end_time &gt;=#{beginTime})",
            "</when>",
            "<when test='endTime!=null'>",
            " and  (discount_end_time &lt;=#{endTime} or discount_end_time &lt;=#{endTime}) ",
            "</when>",
            " order by id limit #{pageIndex},#{pageSize} )b on a.id=b.id"
                    + "</script>"})
    List<Discount> selectDiscountWithPage(Discount discount);


    /**
     * 查询符合条件的总数
     *
     * @param discount
     * @return
     */
    @Select({"<script>" +
            "select count(1) from t_discount where 1=1" +
            "<when test='name!=null'>",
            " and name =#{name}",
            "</when>",
            "<when test='deleteFlag!=null'>",
            " and delete_flag =#{deleteFlag}",
            "</when>",
            "<when test='updateId!=null'>",
            " and update_id =#{updateId}",
            "</when>",
            "<when test='updateTime!=null'>",
            " and update_time =#{updateTime}",
            "</when>",
            "<when test='chargeItemCodes!=null'>",
            " and locate(#{chargeItemCodes},charge_item_codes)",
            "</when>",
            "<when test='discountRate!=null'>",
            " and discount_rate =#{discountRate}",
            "</when>",
            "<when test='deptType!=null'>",
            " and dept_type =#{deptType}",
            "</when>",
            "<when test='deptNo!=null'>",
            " and dept_no =#{deptNo}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and (discount_begin_time &gt;=#{beginTime} or discount_end_time &gt;=#{beginTime})",
            "</when>",
            "<when test='endTime!=null'>",
            " and  (discount_end_time &lt;=#{endTime} or discount_end_time &lt;=#{endTime}) ",
            "</when>",
            "</script>"})
    Integer selectDiscount(Discount discount);


    /**
     * 查询当天有效的折扣率
     *
     * @return
     */
    @Select("select * from t_discount where delete_flag =0 and discount_begin_time <=now() and discount_end_time>=now() ")
    List<Discount> selectDiscountForToday();

    /**
     * 按照主键查询营销计划
     * @param id
     * @return
     */
    @Select("select * from t_discount where id =#{id}")
    Discount selectById(@Param("id") Long id);

    /**
     * 删除营销计划
     * @param id
     * @return
     */
    @Delete("delete  from t_discount where id =#{id} ")
    int deleteById(@Param("id") Long id);
}
