package com.hzs.report.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.report.report.mapper.CuMemberSettlePeriodMapper;
import com.hzs.report.report.service.ICuMemberSettlePeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员结算期间 服务实现类
 */
@Service
public class CuMemberSettlePeriodServiceImpl extends ServiceImpl<CuMemberSettlePeriodMapper, CuMemberSettlePeriod> implements ICuMemberSettlePeriodService {

    @Autowired
    private RedisService redisService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CuMemberSettlePeriod getCuMemberSettlePeriodByDate(String date) {
        String key = CacheConstants.CU_MEMBER_SETTLE_PERIOD + date;
        CuMemberSettlePeriod cuMemberSettlePeriod = redisService.getCacheObject(key);
        if (cuMemberSettlePeriod == null) {
            cuMemberSettlePeriod = getCuMemberSettlePeriod(date);
            if (cuMemberSettlePeriod != null) {
                redisService.setCacheObject(key, cuMemberSettlePeriod);
            }
        }
        return cuMemberSettlePeriod;
    }

    private CuMemberSettlePeriod getCuMemberSettlePeriod(String date) {
        QueryWrapper<CuMemberSettlePeriod> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SETTLE_DATE", DateUtils.parseStringToDate(date));
        return baseMapper.selectOne(queryWrapper);
    }

}
