package com.cyksj.mapper;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cyksj.model.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author chan
 * @date 2021/10/10 下午3:28
 */
public interface UserMapper extends BaseMapper<User> {

    @Update("update user set lottery_time = lottery_time - 1 where id = #{userId} and lottery_time > 0")
    int decrLotteryTime(@Param("userId") Long userId);

    @Update("update user set lottery_time = lottery_time + 1 where id = #{id}")
    void incrLotteryTime(@Param("id") Long userId);

    @Update("update user set points = points - #{points} where id = #{id} and points >= #{points} and points > 0")
    Integer subPoints(@Param("id") Long id, @Param("points") BigDecimal points);

    @Update("update user set points = points + #{addPoints} where id = #{id} and points = #{originalPoints}")
    Integer updatePoints(@Param("id") Long id, @Param("originalPoints") BigDecimal originalPoints, @Param("addPoints") BigDecimal addPoints);

    @Update("update user set galaxy_coin = galaxy_coin - #{subGalaxyCoin} where galaxy_coin = #{userGalaxyCoin} and galaxy_coin >= #{subGalaxyCoin} and id = #{userId}")
    Integer subUserGalaxyCoin(@Param("userId") Long userId, @Param("subGalaxyCoin") BigDecimal subGalaxyCoin, @Param("userGalaxyCoin") BigDecimal userGalaxyCoin);

    @Select("select id from user where nickname like concat('%',#{nickname},'%')")
    List<Long> selectListByNickname(String nickname);

    @Update("update user set points = points - #{deductPoints} where id = #{id} and points = #{originalPoints}")
    Integer subPointsNegative(@Param("id") Long userId, @Param("originalPoints") BigDecimal originalPoints, @Param("deductPoints") BigDecimal deductPoints);

    @Update("update user set balance = balance - #{deduct} where id = #{userId} and balance = #{userBalance} and balance >= #{deduct}")
    Integer deductBalance(@Param("userId") Long userId, @Param("deduct") BigDecimal deduct, @Param("userBalance") BigDecimal userBalance);

    @Update("update user set balance = balance + #{returnBalance} where id = #{userId} and balance = #{userBalance}")
    Integer returnBalance(@Param("userId") Long userId, @Param("returnBalance") BigDecimal returnBalance, @Param("userBalance") BigDecimal userBalance);

    @Update("update user set balance = balance + #{addBalance} where id = #{userId} and balance = #{userBalance}")
    Integer addUserBalance(@Param("userId") Long userId, @Param("userBalance") BigDecimal userBalance, @Param("addBalance") BigDecimal addBalance);

    @Update("update user set balance = balance - #{subBalance} where id = #{userId} and balance = #{userBalance}")
    Integer deductUserBalance(@Param("userId") Long userId, @Param("userBalance") BigDecimal userBalance, @Param("subBalance") BigDecimal subBalance);

    @Select("select id from user where show_id = #{showId} limit 1")
    Long getUserIdByShowId(String showId);

    @Select("select cast(ifnull(sum(balance),0)/100 as decimal(11,2)) from user")
	BigDecimal statisticsRemainBalance();

    @Update("update user set yhs_money = yhs_money + #{addYhsMoney} where id = #{userId} and yhs_money = #{yhsMoney}")
    Integer addYhsMoney(@Param("userId") Long id, @Param("yhsMoney") BigDecimal yhsMoney, @Param("addYhsMoney") BigDecimal addYhsMoney);

    @Update("update user set yhs_money = yhs_money - #{deductMoney} where id = #{shopUserId} and yhs_money = #{yhsMoney}")
    Integer subYhShopUserMoney(@Param("shopUserId") Long shopUserId, @Param("yhsMoney") BigDecimal yhsMoney, @Param("deductMoney") BigDecimal deductMoney);

    @Update("update user set yhs_money = yhs_money - #{subMoney} where id = #{shopUserId} and yhs_money >= #{subMoney} and yhs_money > 0")
    Integer subYhsMoney(@Param("shopUserId") Long shopUserId, @Param("subMoney") BigDecimal subMoney);

	Integer selectUnBindUserNum();

    Integer getLargeNewConsumer(@Param("beginOfDay") DateTime beginOfDay, @Param("largeLimit") String largeLimit);

    @Update("update user set sub_apply_money = sub_apply_money - #{needApplyMoney} where id = #{id} and sub_apply_money >= #{needApplyMoney} and sub_apply_money > 0")
    Integer suUserSubApplyMoney(@Param("id") Long id, @Param("needApplyMoney") BigDecimal needApplyMoney);

    @Update("update user set sub_apply_money = sub_apply_money + #{addApplyMoney} where id = #{id} and sub_apply_money = #{subApplyMoney}")
    Integer addDistributeSubMoney(@Param("id") Long id, @Param("subApplyMoney") BigDecimal subApplyMoney, @Param("addApplyMoney") BigDecimal addApplyMoney);

    Integer getNewMaliceUser(DateTime beginOfDay);

    List<User> selectUserById(@Param("pid") Long phoneUserId, @Param("eid") Long emailUserId);

    @Update("update user set mp_money = mp_money + #{addMoney} where id = #{userId} and mp_money = #{mpMoney}")
    Integer addMpMoney(@Param("userId") Long userId, @Param("mpMoney") BigDecimal mpMoney, @Param("addMoney") BigDecimal addMoney);

    @Update("update user set mp_money = mp_money - #{deductMoney} where id = #{userId} and mp_money = #{mpMoney}")
    Integer subUserMirrorShopMoney(@Param("userId") Long userId, @Param("mpMoney") BigDecimal mpMoney, @Param("deductMoney") BigDecimal deductMoney);
}
