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.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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;

/**
 * <p>
 * 个税明细表 服务实现类
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-06
 */
@Service
public class BdWithdrawalTaxServiceImpl extends ServiceImpl<BdWithdrawalTaxMapper, BdWithdrawalTax> implements IBdWithdrawalTaxService {

    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Autowired
    private IBdWithdrawalTaxDetailsService withdrawalTaxDetailsService;


    /**
     * @description: 根据国家查询个税明细
     * @author: zhang jing
     * @date: 2022/9/27 16:04
     * @param: [pkCountry]
     * @return: java.util.List<com.hzs.common.domain.system.config.BdWithdrawalTax>
     **/
    @Override
    public BdWithdrawalTaxExt queryWithdrawalTax(Integer pkCountry) {
        BdWithdrawalTaxExt withdrawalTaxExt = redisService.getCacheObject(CacheConstants.BD_WITHDRAWAL_TAX + pkCountry);
        if(StringUtils.isNull(withdrawalTaxExt)){
            QueryWrapper<BdWithdrawalTax> queryWrapper = new QueryWrapper();
            queryWrapper.eq("PK_COUNTRY", pkCountry);
            queryWrapper.le("EFFECTIVE_DATE",new Date());
            queryWrapper.orderByDesc("CREATION_TIME");
            BdWithdrawalTax bwt = baseMapper.selectOne(queryWrapper);
            BdWithdrawalTaxExt bwtExt = BeanUtil.copyProperties(bwt, BdWithdrawalTaxExt.class);
            if(StringUtils.isNotNull(bwtExt)&&StringUtils.isNotNull(bwt)){
                QueryWrapper<BdWithdrawalTaxDetails> queryWrapperDetails = new QueryWrapper();
                queryWrapperDetails.eq("PK_COUNTRY",pkCountry);
                queryWrapperDetails.eq("PK_WITHDRAWAL_TAX",bwtExt.getPkId());
                List<BdWithdrawalTaxDetails> bwtdList =withdrawalTaxDetailsService.list(queryWrapperDetails);
                if(CollectionUtil.isNotEmpty(bwtdList)){
                    bwtExt.setWithdrawalTaxDetailsList(bwtdList);
                }else{
                    bwtdList=new ArrayList<>();
                    bwtExt.setWithdrawalTaxDetailsList(bwtdList);
                }
                withdrawalTaxExt=bwtExt;
            }else{
                bwtExt=new BdWithdrawalTaxExt();
                List<BdWithdrawalTaxDetails> bwtdList=new ArrayList<>();
                bwtExt.setWithdrawalTaxDetailsList(bwtdList);
                withdrawalTaxExt=bwtExt;
            }
            if(null != withdrawalTaxExt){
                redisService.setCacheObject(CacheConstants.BD_WITHDRAWAL_TAX + pkCountry, withdrawalTaxExt);
            }
        }
        return withdrawalTaxExt;
    }

    /**
     * @description: 添加或修改提现个税
     * @author: zhang jing
     * @date: 2022/11/9 9:26
     * @param: [withdrawalTaxExt]
     * @return: boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithdrawalTax(BdWithdrawalTaxExt withdrawalTaxExt) {
        QueryWrapper<BdWithdrawalTax> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY",withdrawalTaxExt.getPkCountry());
        BdWithdrawalTax bdWithdrawalTax= baseMapper.selectOne(queryWrapper);
        if(StringUtils.isNotNull(bdWithdrawalTax)){
            UpdateWrapper<BdWithdrawalTax> updateWrapperWt = new UpdateWrapper();
            updateWrapperWt.eq("PK_COUNTRY",withdrawalTaxExt.getPkCountry());
            updateWrapperWt.eq("DEL_FLAG", EDelFlag.UN_DELETE.getValue());
            updateWrapperWt.set("SERVICE_TYPE",withdrawalTaxExt.getServiceType());
            updateWrapperWt.set("TAX_CYCLE",withdrawalTaxExt.getTaxCycle());
            updateWrapperWt.set("EFFECTIVE_DATE",withdrawalTaxExt.getEffectiveDate());
            baseMapper.update(null,updateWrapperWt);
            for(BdWithdrawalTaxDetails bwtd:withdrawalTaxExt.getWithdrawalTaxDetailsList()){
                bwtd.setPkWithdrawalTax(bdWithdrawalTax.getPkId());
                if(StringUtils.isNotNull(bwtd.getPkId())){
                    withdrawalTaxDetailsService.updateById(bwtd);
                }else{
                    withdrawalTaxDetailsService.save(bwtd);
                }
            }
        }else{
            baseMapper.insert(withdrawalTaxExt);
            for(BdWithdrawalTaxDetails bwtd:withdrawalTaxExt.getWithdrawalTaxDetailsList()){
                bwtd.setPkWithdrawalTax(withdrawalTaxExt.getPkId());
                withdrawalTaxDetailsService.save(bwtd);
            }
        }
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_TAX + SecurityUtils.getPkCountry());
        return true;
    }

    /**
     * @description: 删除提现个税
     * @author: zhang jing
     * @date: 2022/11/7 18:05
     * @param: [pkId]
     * @return: boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteWithdrawalTax(Long pkId) {
        QueryWrapper<BdWithdrawalTaxDetails> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_ID",pkId);
        BdWithdrawalTaxDetails withdrawalTaxDetails = withdrawalTaxDetailsService.getOne(queryWrapper);

        UpdateWrapper<BdWithdrawalTaxDetails> updateWrapperDetails = new UpdateWrapper<>();
        updateWrapperDetails.eq("PK_ID", pkId);
        updateWrapperDetails.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapperDetails.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapperDetails.set("MODIFIED_TIME", new Date());
        withdrawalTaxDetailsService.update(updateWrapperDetails);

        QueryWrapper<BdWithdrawalTaxDetails> queryWrapperList = new QueryWrapper();
        queryWrapperList.eq("PK_WITHDRAWAL_TAX",withdrawalTaxDetails.getPkWithdrawalTax());
        List<BdWithdrawalTaxDetails> bwtdList = withdrawalTaxDetailsService.list(queryWrapperList);
        if(CollectionUtil.isEmpty(bwtdList)){
                UpdateWrapper<BdWithdrawalTax> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("PK_ID", withdrawalTaxDetails.getPkWithdrawalTax());
                updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
                updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
                updateWrapper.set("MODIFIED_TIME", new Date());
                baseMapper.update(null,updateWrapper);
        }
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_TAX + SecurityUtils.getPkCountry());
        return true;
    }

    /**
     * @description: 查询单条个税数据
     * @author: zhang jing
     * @date: 2022/11/9 9:51
     * @param: [pkId]
     * @return: com.hzs.common.domain.system.config.ext.BdWithdrawalTaxExt
     **/
    @Override
    public BdWithdrawalTaxExt getEithdrawalTaxExt(Long pkId) {
        QueryWrapper<BdWithdrawalTaxDetails> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_ID",pkId);
        List<BdWithdrawalTaxDetails> withdrawalTaxDetailsList = withdrawalTaxDetailsService.list(queryWrapper);
        QueryWrapper<BdWithdrawalTax> queryWrapperbwt = new QueryWrapper();
        queryWrapperbwt.eq("PK_ID", withdrawalTaxDetailsList.get(0).getPkWithdrawalTax());
        queryWrapperbwt.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        BdWithdrawalTax bwt = baseMapper.selectOne(queryWrapperbwt);
        BdWithdrawalTaxExt bdWithdrawalTaxExt =BeanUtil.copyProperties(bwt, BdWithdrawalTaxExt.class);
        bdWithdrawalTaxExt.setWithdrawalTaxDetailsList(withdrawalTaxDetailsList);
        return bdWithdrawalTaxExt;
    }

}
