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


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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.ERechargeStatus;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.member.account.CuMemberRechargeApplication;
import com.hzs.common.domain.member.account.CuMemberRechargeMatch;
import com.hzs.common.domain.member.account.CuMemberRechargeMatchExt;
import com.hzs.member.account.mapper.CuMemberRechargeMatchMapper;
import com.hzs.member.account.service.ICuMemberRechargeApplicationService;
import com.hzs.member.account.service.ICuMemberRechargeMatchService;
import com.hzs.member.account.vo.CuMemberRechargeMatchVO;
import com.hzs.member.account.vo.RechargeBatchTemplateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 充值匹配 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2024-04-17
 */
@Service
public class CuMemberRechargeMatchServiceImpl extends ServiceImpl<CuMemberRechargeMatchMapper, CuMemberRechargeMatch> implements ICuMemberRechargeMatchService {


    @Autowired
    private ICuMemberRechargeApplicationService cuMemberRechargeApplicationService;

    @Override
    public List<CuMemberRechargeMatch> selectRepeatRecord(List<RechargeBatchTemplateVO> rechargeBatchTemplateVOList, Integer pkCountry) {
        return baseMapper.selectRepeatRecord(rechargeBatchTemplateVOList, pkCountry);
    }


    @Override
    public Object[] findRechargeMatchList(CuMemberRechargeMatchVO rechargeMatchVO) {
        CuMemberRechargeMatchVO memberRechargeMatchVO;
        List<CuMemberRechargeMatchVO> memberRechargeMatchVOList = new ArrayList<>();
        List<CuMemberRechargeMatchExt> rechargeMatchList = baseMapper.findRechargeMatchList(rechargeMatchVO);
        if (CollUtil.isNotEmpty(rechargeMatchList)) {
            for (CuMemberRechargeMatchExt memberRechargeMatch : rechargeMatchList) {
                memberRechargeMatchVO = BeanUtil.copyProperties(memberRechargeMatch, CuMemberRechargeMatchVO.class);
                memberRechargeMatchVOList.add(memberRechargeMatchVO);
            }
        }
        Object[] objects = new Object[]{rechargeMatchList, memberRechargeMatchVOList};
        return objects;
    }

    @Override
    public Boolean deleteRechargeMatchById(Long pkId, Long updateId) {
        UpdateWrapper<CuMemberRechargeMatch> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, updateId);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
        return update(updateWrapper);
    }


    @Override
    public List<CuMemberRechargeMatchVO> findRechargeMatchMemberList(CuMemberRechargeMatchVO rechargeMatchVO) {
        CuMemberRechargeMatchVO memberRechargeMatchVO;
        List<CuMemberRechargeMatchVO> memberRechargeMatchVOList = new ArrayList<>();
        List<CuMemberRechargeMatch> rechargeMatchMemberList = baseMapper.findRechargeMatchMemberList(rechargeMatchVO);
        if (CollUtil.isNotEmpty(rechargeMatchMemberList)) {
            for (CuMemberRechargeMatch memberRechargeMatch : rechargeMatchMemberList) {
                memberRechargeMatchVO = BeanUtil.copyProperties(memberRechargeMatch, CuMemberRechargeMatchVO.class);
                memberRechargeMatchVOList.add(memberRechargeMatchVO);
            }
        }
        return memberRechargeMatchVOList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean memberConfirmRecharge(Long pkMember, CuMemberRechargeMatch rechargeMatch) {
        //更新汇款确认信息
        updateRechargerMatch(pkMember, rechargeMatch);
        CuMemberRechargeApplication application = BeanUtil.copyProperties(rechargeMatch, CuMemberRechargeApplication.class);
        String remarks = rechargeMatch.getRemarks();
        application.setRemittanceInstructions(remarks);
        //充值、交易流水、余额
        return cuMemberRechargeApplicationService.applicationAgree(null, pkMember, application);
    }

    /**
     * 更新汇款的确认信息
     *
     * @param pkMember      会员ID
     * @param rechargeMatch 汇款信息
     * @return Boolean
     */
    private Boolean updateRechargerMatch(Long pkMember, CuMemberRechargeMatch rechargeMatch) {
        UpdateWrapper<CuMemberRechargeMatch> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("PK_CONFIRM", pkMember)
                .set("CONFIRM_TIME", new Date())
                .set("STATUS", ERechargeStatus.CONFIRMED.getValue())
                .eq("PK_ID", rechargeMatch.getPkId());
        return update(updateWrapper);
    }

}
