package com.hzs.bonus.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.base.service.ICuMemberRegisterService;
import com.hzs.bonus.base.vo.CuMemberVO;
import com.hzs.bonus.param.MemberParam;
import com.hzs.common.core.constant.MemberFieldConstants;
import com.hzs.common.core.constant.SaOrderFieldConstants;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.base.CuMemberRegister;
import com.hzs.bonus.base.mapper.CuMemberRegisterMapper;
import org.springframework.stereotype.Service;

import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * <p>
 * 会员历史记录 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-08-22
 */
@Service
public class CuMemberRegisterServiceImpl extends ServiceImpl<CuMemberRegisterMapper, CuMemberRegister> implements ICuMemberRegisterService {

    @Override
    public List<CuMemberVO> queryCuMemberRegisterExeByCondition(MemberParam memberParam) {
        if(memberParam.getEndDate() != null){
            memberParam.setEndDate(DateUtils.afterDate(1, ChronoUnit.DAYS, memberParam.getEndDate()));
        }
        return baseMapper.queryCuMemberRegisterExeByCondition(memberParam);
    }

    @Override
    public CuMemberRegister queryCuMemberRegister(Long pkMember) {
        QueryWrapper<CuMemberRegister> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(MemberFieldConstants.PK_MEMBER, pkMember);
        queryWrapper.orderByAsc(SaOrderFieldConstants.PAY_TIME);
        List<CuMemberRegister> cuMemberRegisters = baseMapper.selectList(queryWrapper);
        return cuMemberRegisters.get(0);
    }

    @Override
    public void updateCuMemberRegister(CuMemberRegister cuMemberRegister) {
        baseMapper.updateCuMemberRegister(cuMemberRegister);
    }

}
