package com.fs.his.mapper;

import java.util.List;
import com.fs.his.domain.FsCoupon;
import com.fs.his.param.FsCouponListUParam;
import com.fs.his.param.FsCouponParam;
import com.fs.his.vo.FsCouponAllListVO;
import com.fs.his.vo.FsCouponListUVO;
import com.fs.his.vo.FsCouponListVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 优惠券Mapper接口
 *
 * @author fs
 * @date 2023-09-04
 */
public interface FsCouponMapper
{
    /**
     * 查询优惠券
     *
     * @param couponId 优惠券主键
     * @return 优惠券
     */
    public FsCoupon selectFsCouponByCouponId(Long couponId);

    /**
     * 查询优惠券列表
     *
     * @param fsCoupon 优惠券
     * @return 优惠券集合
     */
    public List<FsCoupon> selectFsCouponList(FsCoupon fsCoupon);

    /**
     * 新增优惠券
     *
     * @param fsCoupon 优惠券
     * @return 结果
     */
    public int insertFsCoupon(FsCoupon fsCoupon);

    /**
     * 修改优惠券
     *
     * @param fsCoupon 优惠券
     * @return 结果
     */
    public int updateFsCoupon(FsCoupon fsCoupon);

    /**
     * 删除优惠券
     *
     * @param couponId 优惠券主键
     * @return 结果
     */
    public int deleteFsCouponByCouponId(Long couponId);

    /**
     * 批量删除优惠券
     *
     * @param couponIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsCouponByCouponIds(Long[] couponIds);
    @Select({"<script> " +
            "select * from fs_coupon"+
            "<where> " +
            "            <if test=\"title != null  and title != ''\">and title like concat('%', #{title}, '%')</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(limit_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(limit_time) &lt;= DATE(#{eTime})</if>\n" +
            "            <if test=\"couponType != null \"> and coupon_type = #{couponType}</if>\n" +
            "            <if test=\"price != null \"> and price = #{price}</if>\n" +
            "</where>"+
            "order by coupon_id desc"+
            "</script>"})
    List<FsCouponListVO> selectFsCouponListVO(FsCouponParam fsCoupon);

    @Select("select * from fs_coupon where DATE(limit_time) > DATE(NOW()) and `status`=1")
    List<FsCouponAllListVO> selectFsCouponListAll();
    @Select({"<script> " +
            "select c.*  from   fs_coupon c   " +
            "where c.status=1  " +
            "<if test = 'maps.keyword != null     '> " +
            "and c.title like CONCAT('%',#{maps.keyword},'%')" +
            "</if>"+
            "<if test = 'maps.couponType != null     '> " +
            "and c.coupon_type = #{maps.couponType} " +
            "</if>" +
            "<if test = 'maps.cateId != null     '> " +
            "and find_in_set(#{maps.cateId},REPLACE(REPLACE(c.cate_ids, '[', ''), ']', ''))  " +
            "</if>" +
            " order by c.coupon_id desc "+
            "</script>"})
    List<FsCouponListUVO> selectFsCouponListUVO(@Param("maps")FsCouponListUParam param);
    @Update("update fs_coupon set remain_number=(select ifnull(count(1),0) from fs_user_coupon uc where uc.coupon_id=#{couponId}) where coupon_id=#{couponId}")
    int updateRemainCount(Long couponId);
}
