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


import cn.hutool.core.collection.CollUtil;
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.SystemFieldConstants;
import com.hzs.common.core.enums.ETradeStatus;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.account.CuMemberTradeExt;
import com.hzs.common.domain.member.account.CuMemberTradeRecoil;
import com.hzs.common.domain.system.config.BdAccount;
import com.hzs.member.account.mapper.CuMemberTradeRecoilMapper;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberTradeRecoilService;
import com.hzs.member.account.vo.CuMemberTradeDetailVO;
import com.hzs.member.account.vo.CuMemberTradeRecoilVO;
import com.hzs.member.account.vo.CuMemberTradeVO;
import com.hzs.system.base.dto.CurrencyDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 会员活动发放复消币明细 服务实现类
 *
 * @author hzs
 * @since 2024-07-19
 */
@Service
public class CuMemberTradeRecoilServiceImpl extends ServiceImpl<CuMemberTradeRecoilMapper, CuMemberTradeRecoil> implements ICuMemberTradeRecoilService {

    @Autowired
    ICuMemberBaseService cuMemberBaseService;

    @Override
    public int cancelOrderHandle(Long pkMember, Integer pkGrade) {
        return baseMapper.cancelOrderHandle(pkMember, pkGrade);
    }

    @Override
    public BigDecimal selectUnissuedAmount(Long pkMember, Integer pkCountry) {
        return this.baseMapper.selectUnissuedAmount(pkMember, pkCountry);
    }

    @Override
    public List<CuMemberTradeVO> selectRecoilDetail(CuMemberTradeVO cuMemberTradeVO) {
        List<CuMemberTradeVO> cuMemberTradeVOList = baseMapper.selectRecoilDetail(cuMemberTradeVO);
        if (CollUtil.isNotEmpty(cuMemberTradeVOList)) {
            Map<Integer, Integer> accountTranValue = this.getTranslateAccountValue(cuMemberTradeVO.getPkCountry());
            for (CuMemberTradeVO memberTradeVO : cuMemberTradeVOList) {
                memberTradeVO.setPkAccount(accountTranValue.get(memberTradeVO.getPkAccount()));
            }
        }
        return cuMemberTradeVOList;
    }

    private Map<Integer, Integer> getTranslateAccountValue(Integer pkCountry) {
        List<BdAccount> accountList = cuMemberBaseService.getAccountByCountry(null, pkCountry);
        return accountList.stream().collect(Collectors.toMap(BdAccount::getPkId, BdAccount::getPkTransactionKey));
    }


    @Override
    public Object[] selectMemberRecoilDetailList(CuMemberTradeDetailVO memberTradeDetailVO) {
        CuMemberTradeRecoilVO memberTradeRecoilVO;
        List<CuMemberTradeRecoilVO> memberTradeRecoilVOList = new ArrayList<>();
        List<CuMemberTradeExt> cuMemberTradeExtList = baseMapper.selectMemberRecoilDetailList(memberTradeDetailVO);
        if (CollUtil.isNotEmpty(cuMemberTradeExtList)) {
            Integer pkCountry = memberTradeDetailVO.getPkCountry();
            //根据国家查询汇率
            CurrencyDTO currency = cuMemberBaseService.getCurrency(pkCountry);
            //汇率
            BigDecimal exchangeRate = currency.getInExchangeRate();
            //查询账户的翻译值
            Map<Integer, Integer> accountTranValue = this.getTranslateAccountValue(pkCountry);
            for (CuMemberTradeExt cuMemberTradeExt : cuMemberTradeExtList) {
                memberTradeRecoilVO = new CuMemberTradeRecoilVO();
                BeanUtils.copyProperties(cuMemberTradeExt, memberTradeRecoilVO);
                //交易金额
                BigDecimal settleTradeAmount = cuMemberTradeExt.getTradeAmount().divide(exchangeRate, 4, RoundingMode.HALF_UP);
                memberTradeRecoilVO.setSettleTradeAmount(settleTradeAmount);
                memberTradeRecoilVO.setPkAccount(accountTranValue.get(memberTradeRecoilVO.getPkAccount()));

                memberTradeRecoilVOList.add(memberTradeRecoilVO);
            }
        }
        return new Object[]{cuMemberTradeExtList, memberTradeRecoilVOList};
    }

    @Override
    public List<CuMemberTradeRecoil> queryCuMemberTradeRecoil(Date settleDate) {
        Date endDate = DateUtils.afterDate(1, ChronoUnit.DAYS, settleDate);
        QueryWrapper<CuMemberTradeRecoil> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("pk_member,pk_account,pk_country,pk_rate,sum(trade_amount) trade_amount");
        queryWrapper.eq("TRADE_STATUE", ETradeStatus.CONSUMABLE.getValue());
        queryWrapper.ge(SystemFieldConstants.CREATION_TIME, settleDate);
        queryWrapper.lt(SystemFieldConstants.CREATION_TIME, endDate);
        queryWrapper.groupBy("pk_member,pk_account,pk_country,pk_rate");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean updateCuMemberTradeRecoil(Date settleDate) {
        Date endDate = DateUtils.afterDate(1, ChronoUnit.DAYS, settleDate);
        UpdateWrapper<CuMemberTradeRecoil> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("TRADE_STATUE", ETradeStatus.TAKE_EFFECT.getValue());
        updateWrapper.eq("TRADE_STATUE", ETradeStatus.CONSUMABLE.getValue());
        updateWrapper.ge(SystemFieldConstants.CREATION_TIME, settleDate);
        updateWrapper.lt(SystemFieldConstants.CREATION_TIME, endDate);
        update(updateWrapper);
        return Boolean.TRUE;
    }
}
