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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.enums.retail.ETradeMemberWhite;
import com.hzs.common.domain.system.config.BdTradeWhiteConfig;
import com.hzs.common.domain.system.config.ext.BdTradeWhiteConfigExt;
import com.hzs.system.config.mapper.BdTradeWhiteConfigMapper;
import com.hzs.system.config.service.IBdTradeWhiteConfigService;
import com.hzs.system.config.vo.BdTradeWhiteConfigVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 交易白名单配置表 服务实现类
 */
@Service
public class BdTradeWhiteConfigServiceImpl extends ServiceImpl<BdTradeWhiteConfigMapper, BdTradeWhiteConfig> implements IBdTradeWhiteConfigService {

    @Override
    public Integer addTradeWhiteConfig(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        BdTradeWhiteConfig bdTradeWhiteConfig = BeanUtil.copyProperties(tradeWhiteConfigVO, BdTradeWhiteConfig.class);
        return baseMapper.insert(bdTradeWhiteConfig);
    }

    @Override
    public Integer updateTradeWhiteConfig(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        BdTradeWhiteConfig bdTradeWhiteConfig = BeanUtil.copyProperties(tradeWhiteConfigVO, BdTradeWhiteConfig.class);
        bdTradeWhiteConfig.setPkModified(tradeWhiteConfigVO.getPkCreator());
        bdTradeWhiteConfig.setModifiedTime(new Date());
        return baseMapper.updateById(bdTradeWhiteConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTradeWhiteConfig(String pkId, Long pkCreator) {
        List<Integer> pkIdList = Arrays.stream(pkId.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        for (Integer id : pkIdList) {
            LambdaUpdateWrapper<BdTradeWhiteConfig> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(BdTradeWhiteConfig::getDelFlag, EYesNo.NO.getIntValue());
            updateWrapper.set(BdTradeWhiteConfig::getPkModified, pkCreator);
            updateWrapper.set(BdTradeWhiteConfig::getModifiedTime, new Date());
            updateWrapper.eq(BdTradeWhiteConfig::getPkId, id);
            baseMapper.update(null, updateWrapper);
        }
        return true;
    }

    @Override
    public Object[] showTradeWhiteConfigList(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        List<BdTradeWhiteConfigVO> bdTradeWhiteConfigVOList = new ArrayList<>();
        BdTradeWhiteConfigVO whiteConfigVO;
        List<BdTradeWhiteConfigExt> bdTradeWhiteConfigExts = baseMapper.selectTradeWhiteConfigList(tradeWhiteConfigVO);
        if (CollUtil.isNotEmpty(bdTradeWhiteConfigExts)) {
            for (BdTradeWhiteConfigExt bdTradeWhiteConfigExt : bdTradeWhiteConfigExts) {
                whiteConfigVO = BeanUtil.copyProperties(bdTradeWhiteConfigExt, BdTradeWhiteConfigVO.class);
                whiteConfigVO.setMemberTypeVal(ETradeMemberWhite.getByValue(bdTradeWhiteConfigExt.getMemberType()));
                bdTradeWhiteConfigVOList.add(whiteConfigVO);
            }
        }
        return new Object[]{bdTradeWhiteConfigExts, bdTradeWhiteConfigVOList};
    }

    @Override
    public BdTradeWhiteConfig selectTradeWhiteConfigByCondition(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        return baseMapper.selectTradeWhiteConfigByCondition(tradeWhiteConfigVO);
    }

    @Override
    public BdTradeWhiteConfig selectTradeWhiteConfigByPkMember(Long pkMember) {
        LambdaQueryWrapper<BdTradeWhiteConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdTradeWhiteConfig::getPkMember, pkMember);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<Long> selectTradeWhiteConfigByUnderBlood(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        return baseMapper.selectTradeWhiteConfigByUnderBlood(tradeWhiteConfigVO);
    }

}
