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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
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.constant.CacheConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.config.BdWithdrawalTax;
import com.hzs.common.domain.system.config.BdWithdrawalTaxDetails;
import com.hzs.common.domain.system.config.ext.BdWithdrawalTaxExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.mapper.BdWithdrawalTaxMapper;
import com.hzs.system.config.service.IBdWithdrawalTaxDetailsService;
import com.hzs.system.config.service.IBdWithdrawalTaxService;
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;

/**
 * 个税明细表 服务实现类
 */
@Service
public class BdWithdrawalTaxServiceImpl extends ServiceImpl<BdWithdrawalTaxMapper, BdWithdrawalTax> implements IBdWithdrawalTaxService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private IBdWithdrawalTaxDetailsService iBdWithdrawalTaxDetailsService;

    /**
     * @description: 根据国家查询个税明细
     **/
    @Override
    public BdWithdrawalTaxExt queryWithdrawalTax(Integer pkCountry) {
        BdWithdrawalTaxExt withdrawalTaxExt = redisService.getCacheObject(CacheConstants.BD_WITHDRAWAL_TAX + pkCountry);
        if (StringUtils.isNull(withdrawalTaxExt)) {
            LambdaQueryWrapper<BdWithdrawalTax> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdWithdrawalTax::getPkCountry, pkCountry);
            queryWrapper.le(BdWithdrawalTax::getEffectiveDate, new Date());
            queryWrapper.orderByDesc(BdWithdrawalTax::getCreationTime);
            BdWithdrawalTax bwt = baseMapper.selectOne(queryWrapper);
            BdWithdrawalTaxExt bwtExt = BeanUtil.copyProperties(bwt, BdWithdrawalTaxExt.class);
            if (StringUtils.isNotNull(bwtExt) && StringUtils.isNotNull(bwt)) {
                LambdaQueryWrapper<BdWithdrawalTaxDetails> queryWrapperDetails = new LambdaQueryWrapper<>();
                queryWrapperDetails.eq(BdWithdrawalTaxDetails::getPkCountry, pkCountry);
                queryWrapperDetails.eq(BdWithdrawalTaxDetails::getPkWithdrawalTax, bwtExt.getPkId());
                List<BdWithdrawalTaxDetails> bwtdList = iBdWithdrawalTaxDetailsService.list(queryWrapperDetails);
                if (CollectionUtil.isNotEmpty(bwtdList)) {
                    bwtExt.setWithdrawalTaxDetailsList(bwtdList);
                } else {
                    bwtExt.setWithdrawalTaxDetailsList(new ArrayList<>());
                }
                withdrawalTaxExt = bwtExt;
            } else {
                bwtExt = new BdWithdrawalTaxExt();
                bwtExt.setWithdrawalTaxDetailsList(new ArrayList<>());
                withdrawalTaxExt = bwtExt;
            }
            redisService.setCacheObject(CacheConstants.BD_WITHDRAWAL_TAX + pkCountry, withdrawalTaxExt);
        }
        return withdrawalTaxExt;
    }

    /**
     * @description: 添加或修改提现个税
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithdrawalTax(BdWithdrawalTaxExt withdrawalTaxExt) {
        LambdaQueryWrapper<BdWithdrawalTax> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWithdrawalTax::getPkCountry, withdrawalTaxExt.getPkCountry());
        BdWithdrawalTax bdWithdrawalTax = baseMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(bdWithdrawalTax)) {
            LambdaUpdateWrapper<BdWithdrawalTax> updateWrapperWt = new LambdaUpdateWrapper<>();
            updateWrapperWt.eq(BdWithdrawalTax::getPkCountry, withdrawalTaxExt.getPkCountry());
            updateWrapperWt.eq(BdWithdrawalTax::getDelFlag, EDelFlag.UN_DELETE.getValue());
            updateWrapperWt.set(BdWithdrawalTax::getServiceType, withdrawalTaxExt.getServiceType());
            updateWrapperWt.set(BdWithdrawalTax::getTaxCycle, withdrawalTaxExt.getTaxCycle());
            updateWrapperWt.set(BdWithdrawalTax::getEffectiveDate, withdrawalTaxExt.getEffectiveDate());
            baseMapper.update(null, updateWrapperWt);
            for (BdWithdrawalTaxDetails bwtd : withdrawalTaxExt.getWithdrawalTaxDetailsList()) {
                bwtd.setPkWithdrawalTax(bdWithdrawalTax.getPkId());
                if (StringUtils.isNotNull(bwtd.getPkId())) {
                    iBdWithdrawalTaxDetailsService.updateById(bwtd);
                } else {
                    iBdWithdrawalTaxDetailsService.save(bwtd);
                }
            }
        } else {
            baseMapper.insert(withdrawalTaxExt);
            for (BdWithdrawalTaxDetails bwtd : withdrawalTaxExt.getWithdrawalTaxDetailsList()) {
                bwtd.setPkWithdrawalTax(withdrawalTaxExt.getPkId());
                iBdWithdrawalTaxDetailsService.save(bwtd);
            }
        }
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_TAX + SecurityUtils.getPkCountry());
        return true;
    }

    /**
     * @description: 删除提现个税
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteWithdrawalTax(Long pkId) {
        LambdaQueryWrapper<BdWithdrawalTaxDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWithdrawalTaxDetails::getPkId, pkId);
        BdWithdrawalTaxDetails withdrawalTaxDetails = iBdWithdrawalTaxDetailsService.getOne(queryWrapper);

        LambdaUpdateWrapper<BdWithdrawalTaxDetails> updateWrapperDetails = new LambdaUpdateWrapper<>();
        updateWrapperDetails.eq(BdWithdrawalTaxDetails::getPkId, pkId);
        updateWrapperDetails.set(BdWithdrawalTaxDetails::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapperDetails.set(BdWithdrawalTaxDetails::getPkModified, SecurityUtils.getUserId());
        updateWrapperDetails.set(BdWithdrawalTaxDetails::getModifiedTime, new Date());
        iBdWithdrawalTaxDetailsService.update(updateWrapperDetails);

        LambdaQueryWrapper<BdWithdrawalTaxDetails> queryWrapperList = new LambdaQueryWrapper<>();
        queryWrapperList.eq(BdWithdrawalTaxDetails::getPkWithdrawalTax, withdrawalTaxDetails.getPkWithdrawalTax());
        List<BdWithdrawalTaxDetails> bwtdList = iBdWithdrawalTaxDetailsService.list(queryWrapperList);
        if (CollectionUtil.isEmpty(bwtdList)) {
            LambdaUpdateWrapper<BdWithdrawalTax> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(BdWithdrawalTax::getPkId, withdrawalTaxDetails.getPkWithdrawalTax());
            updateWrapper.set(BdWithdrawalTax::getDelFlag, EDelFlag.DELETE.getValue());
            updateWrapper.set(BdWithdrawalTax::getPkModified, SecurityUtils.getUserId());
            updateWrapper.set(BdWithdrawalTax::getModifiedTime, new Date());
            baseMapper.update(null, updateWrapper);
        }
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_TAX + SecurityUtils.getPkCountry());
        return true;
    }

    /**
     * @description: 查询单条个税数据
     **/
    @Override
    public BdWithdrawalTaxExt getEithdrawalTaxExt(Long pkId) {
        LambdaQueryWrapper<BdWithdrawalTaxDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWithdrawalTaxDetails::getPkId, pkId);
        List<BdWithdrawalTaxDetails> withdrawalTaxDetailsList = iBdWithdrawalTaxDetailsService.list(queryWrapper);
        LambdaQueryWrapper<BdWithdrawalTax> queryWrapperbwt = new LambdaQueryWrapper<>();
        queryWrapperbwt.eq(BdWithdrawalTax::getPkId, withdrawalTaxDetailsList.get(0).getPkWithdrawalTax());
        queryWrapperbwt.eq(BdWithdrawalTax::getPkCountry, SecurityUtils.getPkCountry());
        BdWithdrawalTax bwt = baseMapper.selectOne(queryWrapperbwt);
        BdWithdrawalTaxExt bdWithdrawalTaxExt = BeanUtil.copyProperties(bwt, BdWithdrawalTaxExt.class);
        bdWithdrawalTaxExt.setWithdrawalTaxDetailsList(withdrawalTaxDetailsList);
        return bdWithdrawalTaxExt;
    }

}
