package com.fs.his.mapper;

import java.util.List;
import com.fs.his.domain.FsUserCoupon;
import com.fs.his.param.FsUserCouponParam;
import com.fs.his.param.FsUserCouponUParam;
import com.fs.his.vo.FsUserCouponListUVO;
import com.fs.his.vo.FsUserCouponListVO;
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 FsUserCouponMapper
{
    /**
     * 查询会员优惠券
     *
     * @param id 会员优惠券主键
     * @return 会员优惠券
     */
    public FsUserCoupon selectFsUserCouponById(Long id);

    /**
     * 查询会员优惠券列表
     *
     * @param fsUserCoupon 会员优惠券
     * @return 会员优惠券集合
     */
    public List<FsUserCoupon> selectFsUserCouponList(FsUserCoupon fsUserCoupon);

    /**
     * 新增会员优惠券
     *
     * @param fsUserCoupon 会员优惠券
     * @return 结果
     */
    public int insertFsUserCoupon(FsUserCoupon fsUserCoupon);

    /**
     * 修改会员优惠券
     *
     * @param fsUserCoupon 会员优惠券
     * @return 结果
     */
    public int updateFsUserCoupon(FsUserCoupon fsUserCoupon);

    /**
     * 删除会员优惠券
     *
     * @param id 会员优惠券主键
     * @return 结果
     */
    public int deleteFsUserCouponById(Long id);

    /**
     * 批量删除会员优惠券
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserCouponByIds(Long[] ids);

    @Select({"<script> " +
            " select uc.*,u.nick_name,u.phone,c.title,c.price,c.coupon_type,su.nick_name sendUserName,com.company_name,comu.user_name  company_user_name FROM fs_user_coupon uc LEFT JOIN fs_user u ON uc.user_id=u.user_id LEFT JOIN fs_coupon c on c.coupon_id=uc.coupon_id LEFT JOIN sys_user su ON su.user_id =uc.send_user_id LEFT JOIN company com ON com.company_id=uc.company_id LEFT JOIN company_user comu on comu.user_id=uc.company_user_id "+
            " <where>  \n" +
            "            <if test=\"couponId != null \"> and uc.coupon_id = #{couponId}</if>\n" +
            "            <if test=\"userId != null \"> and uc.user_id = #{userId}</if>\n" +
            "            <if test=\"couponCode != null  and couponCode != ''\"> and uc.coupon_code = #{couponCode}</if>\n" +
            "            <if test=\"phone != null \">and u.phone like concat( #{phone}, '%') </if>\n" +
            "            <if test=\"title != null \">and c.title like concat( #{title}, '%') </if>\n" +
            "            <if test=\"sUseTime != null \">  and DATE(uc.use_time) &gt;= DATE(#{sUseTime})</if>\n" +
            "            <if test=\"eUseTime != null \">  and DATE(uc.use_time) &lt;= DATE(#{eUseTime})</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(uc.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(uc.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "            <if test=\"businessId != null \"> and uc.business_id = #{businessId}</if>\n" +
            "            <if test=\"businessType != null \"> and uc.business_type = #{businessType}</if>\n" +
            "            <if test=\"status != null \"> and uc.status = #{status}</if>\n" +
            "            <if test=\"companyName != null and companyName != ''\">and com.company_name like concat('%', #{companyName}, '%') </if>\n" +
            "            <if test=\"companyUserName != null and companyUserName != ''\">and comu.user_name like concat('%', #{companyUserName}, '%') </if>\n" +
            "            <if test=\"companyId != null \">and uc.company_id = #{companyId} </if>\n" +
            "        </where> "+
            " order by id desc"+
            "</script>"})
    List<FsUserCouponListVO> selectFsUserCouponListVO(FsUserCouponParam fsUserCoupon);


    @Select("SELECT COUNT(c.coupon_id) FROM fs_user_coupon u LEFT JOIN fs_coupon c ON u.coupon_id=c.coupon_id WHERE u.user_id=#{userId} and c.coupon_id=#{couponId}")
    Integer selectFsUserCouponByUserIdTypeCount(@Param("userId") Long userId,@Param("couponId")Long couponId);
    @Select({"<script> " +
            "select uc.coupon_code,uc.status,uc.use_time,c.limit_time,c.title,c.price,c.min_price  from fs_user_coupon uc left join fs_coupon c on c.coupon_id=uc.coupon_id    " +
            "where 1=1  " +
            "<if test = ' maps.userId != null '> " +
            "and uc.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.couponType != null '> " +
            "and uc.coupon_type = #{maps.couponType} " +
            "</if>" +
            "<if test = 'maps.status != null      '> " +
            "and uc.status = #{maps.status} " +
            "</if>" +
            " order by uc.create_time desc "+
            "</script>"})
    List<FsUserCouponListUVO> selectFsUserCouponListUVO(@Param("maps")FsUserCouponUParam param);

    @Select("SELECT COUNT(c.coupon_type) FROM fs_user_coupon u LEFT JOIN fs_coupon c ON u.coupon_id=c.coupon_id WHERE u.user_id=#{userId} and c.coupon_type=#{couponType}")
    Integer selectFsUserCouponByUserIdCount(@Param("userId") Long userId, @Param("couponType")Integer couponType);
    @Update("UPDATE fs_user_coupon SET `status`=2 WHERE id in (SELECT * FROM(SELECT uc.id FROM fs_user_coupon uc LEFT JOIN fs_coupon c ON c.coupon_id=uc.coupon_id WHERE uc.status=0 and DATE(c.limit_time)<DATE(NOW())) t)")
    Integer updateFsUserCouponStatusByLimtTime();
    @Select({"<script> " +
            "select uc.id,uc.coupon_code,uc.status,uc.use_time,c.limit_time,c.title,c.price,c.min_price  from fs_user_coupon uc left join fs_coupon c on c.coupon_id=uc.coupon_id    " +
            "where uc.status=0    " +
            "<if test = ' maps.userId != null '> " +
            "and uc.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.couponType != null and maps.cateId!=0 '> " +
            "and find_in_set(c.coupon_type,#{maps.couponType})  and find_in_set(#{maps.cateId},REPLACE(REPLACE(c.cate_ids, '[', ''), ']', '')) " +
            "</if>" +
            "<if test = ' maps.couponType != null and maps.cateId==0 '> " +
            "and find_in_set(c.coupon_type,#{maps.couponType})  " +
            "</if>" +
            " order by uc.create_time desc "+
            "</script>"})
    List<FsUserCouponListUVO> getMyEnableCouponList(@Param("maps")FsUserCouponUParam param);
    @Select("select ifnull(count(1),0) from fs_user_coupon where coupon_id=#{couponId} and user_id=#{userId}")
    int checkReceive(@Param("userId")Long userId,@Param("couponId")Long couponId);
}
