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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.MemberFieldConstants;
import com.hzs.common.core.constant.SaOrderFieldConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.base.CuMemberUpgrade;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.member.base.mapper.CuMemberUpgradeMapper;
import com.hzs.member.base.service.ICuMemberUpgradeService;
import com.hzs.system.base.ICountryServiceApi;
import com.hzs.system.base.dto.CountryDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 会员升级权限表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-05-22
 */
@Service
public class CuMemberUpgradeServiceImpl extends ServiceImpl<CuMemberUpgradeMapper, CuMemberUpgrade> implements ICuMemberUpgradeService {

    @DubboReference
    private ICountryServiceApi countryServiceApi;

    @Override
    public void saveCuMemberUpgrade(CuMember cuMember) {
        CountryDTO countryDTO = countryServiceApi.getCountry(cuMember.getPkSettleCountry()).getData();
        if(MagicNumberConstants.BENGAL_COUNTRY_CODE.equals(countryDTO.getCode())){
            CuMemberUpgrade cuMemberUpgrade = CuMemberUpgrade.builder()
                    .pkMember(cuMember.getPkId())
                    .quantity(1)
                    .expiresDate(DateUtils.afterDate(60, ChronoUnit.DAYS, cuMember.getPayTime()))
                    .build();
            cuMemberUpgrade.setPkCreator(cuMember.getPkId());
            cuMemberUpgrade.setPkCountry(cuMember.getPkSettleCountry());
            save(cuMemberUpgrade);
        }
    }

    @Override
    public CuMemberUpgrade queryCuMemberUpgradeByCondition(Integer pkCountry, Long pkMember) {
        QueryWrapper<CuMemberUpgrade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry)
                .eq(MemberFieldConstants.PK_MEMBER, pkMember)
                .ge(MemberFieldConstants.QUANTITY, 1);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateCuMemberUpgrade(SaOrder saOrder) {
        CountryDTO countryDTO = countryServiceApi.getCountry(saOrder.getPkCountry()).getData();
        if(MagicNumberConstants.BENGAL_COUNTRY_CODE.equals(countryDTO.getCode())) {
            CuMemberUpgrade cuMemberUpgrade = queryCuMemberUpgradeByCondition(saOrder.getPkCountry(), saOrder.getPkMember());
            if (cuMemberUpgrade != null && cuMemberUpgrade.getQuantity() > 0 && DateUtils.compareDateAfter(cuMemberUpgrade.getExpiresDate(), DateUtils.currentDateTime())) {
                UpdateWrapper<CuMemberUpgrade> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set(MemberFieldConstants.QUANTITY, cuMemberUpgrade.getQuantity() - 1)
                        .set(SaOrderFieldConstants.PK_ORDER, saOrder.getPkId())
                        .set(SystemFieldConstants.PK_MODIFIED, saOrder.getPkMember())
                        .set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDateTime())
                        .eq(SystemFieldConstants.PK_ID, cuMemberUpgrade.getPkId())
                        .ge(MemberFieldConstants.QUANTITY, 1)
                        .ge("EXPIRES_DATE", DateUtils.currentDateTime());
                if (!update(updateWrapper)) {
                    throw new RuntimeException("该会员没有一次性补差的权益，请重新升级");
                }
            }
        }
    }

    @Override
    public void deleteCuMemberUpgrade(Integer pkCountry, Long pkMember) {

    }
}
