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

import cn.hutool.core.collection.CollectionUtil;
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.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.system.config.BdAreaCurrency;
import com.hzs.common.domain.system.config.BdAreaCurrencyDetail;
import com.hzs.common.domain.system.config.ext.BdAreaCurrencyDetailExt;
import com.hzs.common.domain.system.config.ext.BdAreaCurrencyExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.mapper.BdAreaCurrencyMapper;
import com.hzs.system.config.service.IBdAreaCurrencyDetailService;
import com.hzs.system.config.service.IBdAreaCurrencyService;
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 BdAreaCurrencyServiceImpl extends ServiceImpl<BdAreaCurrencyMapper, BdAreaCurrency> implements IBdAreaCurrencyService {

    @Autowired
    private IBdAreaCurrencyDetailService iBdAreaCurrencyDetailService;
    @Autowired
    private RedisService redisService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAreaCurrency(BdAreaCurrencyExt areaCurrencyExt) {
        List<BdAreaCurrencyDetailExt> detailsList = areaCurrencyExt.getDetailsList();
        baseMapper.insert(areaCurrencyExt);
        if (CollectionUtil.isNotEmpty(detailsList)) {
            for (BdAreaCurrencyDetailExt bcd : detailsList) {
                bcd.setPkAreaCurrency(areaCurrencyExt.getPkId().intValue());
                bcd.setPkAccount(bcd.getPkAccount());
                bcd.setDeductSort(bcd.getDeductSort());
                bcd.setDeductRatio(bcd.getDeductRatio());
                bcd.setDeductMaxRatio(bcd.getDeductMaxRatio());
                bcd.setPkCountry(SecurityUtils.getPkCountry());
                bcd.setPkCreator(SecurityUtils.getUserId());
                bcd.setCreationTime(new Date());
                iBdAreaCurrencyDetailService.save(bcd);
            }
        }
        return true;
    }

    @Override
    public boolean updateAreaCurrency(BdAreaCurrencyExt areaCurrencyExt) {
        List<BdAreaCurrencyDetailExt> detailsList = areaCurrencyExt.getDetailsList();
        baseMapper.updateById(areaCurrencyExt);
        BdAreaCurrencyDetail areaCurrencyDetail = new BdAreaCurrencyDetail();
        areaCurrencyDetail.setPkAreaCurrency(areaCurrencyExt.getPkId().intValue());
        LambdaUpdateWrapper<BdAreaCurrencyDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(BdAreaCurrencyDetail::getDelFlag, EYesNo.NO.getIntValue());
        updateWrapper.eq(BdAreaCurrencyDetail::getPkAreaCurrency, areaCurrencyExt.getPkId());
        updateWrapper.eq(BdAreaCurrencyDetail::getDelFlag, EYesNo.YES.getIntValue());
        iBdAreaCurrencyDetailService.update(updateWrapper);
        if (CollectionUtil.isNotEmpty(detailsList)) {
            for (BdAreaCurrencyDetailExt bcd : detailsList) {
                bcd.setPkAreaCurrency(areaCurrencyExt.getPkId().intValue());
                bcd.setPkCountry(SecurityUtils.getPkCountry());
                bcd.setPkCreator(SecurityUtils.getUserId());
                bcd.setCreationTime(new Date());
                bcd.setDelFlag(bcd.getDelAreaCurrency());
                bcd.setPkModified(SecurityUtils.getUserId());
                bcd.setModifiedTime(new Date());
                bcd.setDeductSort(bcd.getDeductSort());
                bcd.setDeductMaxRatio(bcd.getDeductMaxRatio());
                iBdAreaCurrencyDetailService.save(bcd);
            }
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delAreaCurrency(BdAreaCurrency bdAreaCurrency) {
        BdAreaCurrency areaCurrencyParam = baseMapper.selectById(bdAreaCurrency.getPkId());
        LambdaUpdateWrapper<BdAreaCurrencyDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdAreaCurrencyDetail::getPkAreaCurrency, bdAreaCurrency.getPkId());
        updateWrapper.set(BdAreaCurrencyDetail::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdAreaCurrencyDetail::getPkModified, bdAreaCurrency.getPkModified());
        updateWrapper.set(BdAreaCurrencyDetail::getModifiedTime, bdAreaCurrency.getModifiedTime());

        iBdAreaCurrencyDetailService.update(updateWrapper);
        bdAreaCurrency.setDelFlag(EDelFlag.DELETE.getValue());
        LambdaUpdateWrapper<BdAreaCurrency> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(BdAreaCurrency::getPkId, bdAreaCurrency.getPkId());
        updateWrapper1.set(BdAreaCurrency::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper1.set(BdAreaCurrency::getPkModified, bdAreaCurrency.getPkModified());
        updateWrapper1.set(BdAreaCurrency::getModifiedTime, bdAreaCurrency.getModifiedTime());
        baseMapper.update(null, updateWrapper1);
        String key = CacheConstants.BD_AREA_CURRENCY + areaCurrencyParam.getPkCountry().toString() + areaCurrencyParam.getSpecialArea();
        if (redisService.hasKey(key)) {
            redisService.deleteObject(key);
        }
        return true;
    }
}
