package com.hzs.system.config.service.impl;


import cn.hutool.core.bean.BeanUtil;
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.EYesNo;
import com.hzs.common.domain.system.config.BdAccount;
import com.hzs.common.domain.system.config.BdTradeWhiteConfig;
import com.hzs.common.domain.system.config.CuMemberTransferWhite;
import com.hzs.common.domain.system.config.ext.CuMemberTransferWhiteExt;
import com.hzs.system.config.mapper.CuMemberTransferWhiteMapper;
import com.hzs.system.config.service.IBdAccountService;
import com.hzs.system.config.service.ICuMemberTransferWhiteService;
import com.hzs.system.config.vo.CuMemberTransferWhiteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员转账币种白名单  服务实现类
 * </p>
 *
 * @author hzs
 * @since 2024-07-03
 */
@Service
public class CuMemberTransferWhiteServiceImpl extends ServiceImpl<CuMemberTransferWhiteMapper, CuMemberTransferWhite> implements ICuMemberTransferWhiteService {


    @Autowired
    private IBdAccountService bdAccountService;

    @Override
    public CuMemberTransferWhite selectByPkMemberAndPkAcount(Long pkMember, Integer pkAccount) {
        QueryWrapper<CuMemberTransferWhite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pk_member", pkMember);
        queryWrapper.eq("PK_ACCOUNT", pkAccount);
        return baseMapper.selectOne(queryWrapper);
    }


    @Override
    public Integer addTransferWhiteConfig(CuMemberTransferWhiteVO cuMemberTransferWhiteVO) {
        return baseMapper.insert(BeanUtil.copyProperties(cuMemberTransferWhiteVO, CuMemberTransferWhite.class));
    }


    @Override
    public Object[] selectTransferWhiteConfigList(CuMemberTransferWhiteVO transferWhiteVO) {
        List<CuMemberTransferWhiteVO> cuMemberTransferWhiteVOS = new ArrayList<>();
        CuMemberTransferWhiteVO cuMemberTransferWhiteVO;
        List<CuMemberTransferWhiteExt> memberTransferWhiteExtList = baseMapper.selectTransferWhiteConfigList(transferWhiteVO);
        if (CollUtil.isNotEmpty(memberTransferWhiteExtList)) {
            for (CuMemberTransferWhiteExt cuMemberTransferWhiteExt : memberTransferWhiteExtList) {
                cuMemberTransferWhiteVO = BeanUtil.copyProperties(cuMemberTransferWhiteExt, CuMemberTransferWhiteVO.class);
                Integer pkAccount = cuMemberTransferWhiteExt.getPkAccount();
                BdAccount bdAccount = bdAccountService.getBdAccount(pkAccount);
                cuMemberTransferWhiteVO.setPkAccount(bdAccount.getPkTransactionKey());
                cuMemberTransferWhiteVOS.add(cuMemberTransferWhiteVO);
            }

        }
        return new Object[]{memberTransferWhiteExtList, cuMemberTransferWhiteVOS};
    }

    @Override
    public Integer updateTransferWhite(CuMemberTransferWhiteVO transferWhiteVO) {
        CuMemberTransferWhite cuMemberTransferWhite = BeanUtil.copyProperties(transferWhiteVO, CuMemberTransferWhite.class);
        cuMemberTransferWhite.setPkModified(transferWhiteVO.getPkCreator());
        cuMemberTransferWhite.setModifiedTime(new Date());
        return baseMapper.updateById(cuMemberTransferWhite);
    }


    @Override
    public Integer deleteTransferWhiteConfig(String pkIds, Long pkCreator) {
        List<Long> pkIdList = Arrays.stream(pkIds.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return baseMapper.deleteTransferWhiteByIds(pkIdList, pkCreator);
    }

    @Override
    public CuMemberTransferWhiteVO selectTransferWhiteById(Long pkId) {
        CuMemberTransferWhiteExt cuMemberTransferWhiteExt = baseMapper.selectTransferWhiteById(pkId);
        CuMemberTransferWhiteVO cuMemberTransferWhiteVO = BeanUtil.copyProperties(cuMemberTransferWhiteExt, CuMemberTransferWhiteVO.class);
        Integer pkAccount = cuMemberTransferWhiteExt.getPkAccount();
        cuMemberTransferWhiteVO.setPkAccountId(pkAccount);
        BdAccount bdAccount = bdAccountService.getBdAccount(pkAccount);
        cuMemberTransferWhiteVO.setPkAccount(bdAccount.getPkTransactionKey());
        return cuMemberTransferWhiteVO;

    }

    @Override
    public List<Integer> selectTransferWhiteByPkMember(Long pkMember, Integer pkCountry){
        return baseMapper.selectTransferWhiteByPkMember(pkMember, pkCountry);
    }
}
