package cn.iocoder.yudao.module.member.dal.mysql.card;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.CardPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.card.vo.AppCardMonthVO;
import cn.iocoder.yudao.module.member.controller.app.card.vo.AppCardPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.card.vo.FaceVo;
import cn.iocoder.yudao.module.member.dal.dataobject.card.CardDO;
import cn.iocoder.yudao.module.member.util.MemberConstants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * 余额变动明细 Mapper
 *
 * @author 开发账号
 */
@Mapper
public interface CardMapper extends BaseMapperX<CardDO> {

    default PageResult<CardDO> selectPage(CardPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CardDO>()
                .eqIfPresent(CardDO::getUserId, reqVO.getUserId())
                .eqIfPresent(CardDO::getMoney, reqVO.getMoney())
                .eqIfPresent(CardDO::getFlag, reqVO.getFlag())
                .eqIfPresent(CardDO::getChangeMoney, reqVO.getChangeMoney())
                .betweenIfPresent(CardDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CardDO::getId));
    }

    default PageResult<CardDO> selectPage(AppCardPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CardDO>()
                .eqIfPresent(CardDO::getUserId, reqVO.getUserId())
                .eqIfPresent(CardDO::getMoney, reqVO.getMoney())
                .eqIfPresent(CardDO::getFlag, reqVO.getFlag())
                .eqIfPresent(CardDO::getChangeMoney, reqVO.getChangeMoney())
                .betweenIfPresent(CardDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CardDO::getId));
    }

    @Select("select IFNULL(sum(change_money),0) as money,count(*) as count FROM member_card where user_id = #{userId} and flag = #{flag} and  create_time BETWEEN #{startTime} and #{endTime}")
    AppCardMonthVO selectMonth(@Param("userId") Long userId,@Param("flag") String flag, @Param("startTime") LocalDateTime startTime, @Param("endTime")LocalDateTime endTime);
    @Select("select SUM(change_money) from member_card where user_id= #{userId} and flag= #{flag}")
    BigDecimal selectAddMoney(@Param("userId") Long userId,@Param("flag") String flag);


    List<CardDO> selectMoneyList(List<Long> list);

    FaceVo getFaceData(String faceId);

    default BigDecimal getMoneyByUsr(Long userId){
        CardDO lastCardDO = selectOne(Wrappers.<CardDO>lambdaQuery().eq(CardDO::getUserId, userId)
                .orderByDesc(CardDO::getCreateTime)
                .orderByDesc(CardDO::getId)
                .last(MemberConstants.LIMIT_ONE));
        if (lastCardDO == null || lastCardDO.getMoney() == null) {
            return BigDecimal.ZERO;
        }
        return lastCardDO.getMoney();
    }

}