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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.system.config.BdWithdrawal;
import com.hzs.common.domain.system.config.BdWithdrawalAuto;
import com.hzs.common.domain.system.config.ext.BdWithdrawalAutoExt;
import com.hzs.common.domain.system.config.ext.BdWithdrawalExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.mapper.BdWithdrawalMapper;
import com.hzs.system.config.service.IBdWithdrawalAutoService;
import com.hzs.system.config.service.IBdWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 提现设置主表 服务实现类
 */
@Service
public class BdWithdrawalServiceImpl extends ServiceImpl<BdWithdrawalMapper, BdWithdrawal> implements IBdWithdrawalService {

    @Autowired
    private IBdWithdrawalAutoService iBdWithdrawalAutoService;

    @Override
    public BdWithdrawal queryWithdrawal(Integer pkCountry, Integer pkAccount) {
        BdWithdrawal withdrawal = new BdWithdrawal();
        List<BdWithdrawal> withdrawallist = baseMapper.queryWithdrawal(pkCountry, pkAccount);
        if (CollectionUtil.isNotEmpty(withdrawallist)) {
            withdrawal = withdrawallist.get(0);
        }
        return withdrawal;
    }

    @Override
    public Integer checkWithdraw(BdWithdrawalAutoExt withdrawalAutoExt) {
        return baseMapper.checkWithdraw(withdrawalAutoExt);
    }

    @Override
    public List<BdWithdrawalExt> listExt(BdWithdrawalExt withdrawalExt) {
        return baseMapper.listExt(withdrawalExt);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithdrawal(BdWithdrawalExt withdrawalExt) {
        baseMapper.insert(withdrawalExt);
        List<BdWithdrawalAutoExt> withdrawalAutoList = withdrawalExt.getWithdrawalAutoList();
        for (BdWithdrawalAuto wao : withdrawalAutoList) {
            wao.setPkWithdrawal(withdrawalExt.getPkId());
            wao.setPkCountry(SecurityUtils.getPkCountry());
            wao.setPkCreator(SecurityUtils.getUserId());
            wao.setCreationTime(new Date());
            iBdWithdrawalAutoService.save(wao);
        }
        return true;
    }

    @Override
    public boolean updateWithdrawal(BdWithdrawalExt withdrawalExt) {
        baseMapper.updateById(withdrawalExt);
        UpdateWrapper<BdWithdrawalAuto> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_WITHDRAWAL", withdrawalExt.getPkId());
        updateWrapper.eq("DEL_FLAG", EDelFlag.UN_DELETE.getValue());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        iBdWithdrawalAutoService.update(updateWrapper);
        List<BdWithdrawalAutoExt> withdrawalAutoList = withdrawalExt.getWithdrawalAutoList();
        for (BdWithdrawalAuto wao : withdrawalAutoList) {
            wao.setPkWithdrawal(withdrawalExt.getPkId());
            wao.setPkCountry(SecurityUtils.getPkCountry());
            wao.setPkCreator(SecurityUtils.getUserId());
            wao.setCreationTime(new Date());
            iBdWithdrawalAutoService.save(wao);
        }
        return true;
    }

    @Override
    public List<BdWithdrawalAutoExt> getWithdrawalAutoList(BdWithdrawalAutoExt withdrawalAutoExt) {
        return baseMapper.getWithdrawalAutoList(withdrawalAutoExt);
    }

}
